Branch data Line data Source code
1 : : /* vi: set et sw=4 ts=4 cino=t0,(0: */
2 : : /*
3 : : * This file is part of libaccounts-qt
4 : : *
5 : : * Copyright (C) 2009-2011 Nokia Corporation.
6 : : * Copyright (C) 2012 Canonical Ltd.
7 : : * Copyright (C) 2012 Intel Corporation.
8 : : *
9 : : * Contact: Alberto Mardegan <alberto.mardegan@canonical.com>
10 : : * Contact: Jussi Laako <jussi.laako@linux.intel.com>
11 : : *
12 : : * This library is free software; you can redistribute it and/or
13 : : * modify it under the terms of the GNU Lesser General Public License
14 : : * version 2.1 as published by the Free Software Foundation.
15 : : *
16 : : * This library is distributed in the hope that it will be useful, but
17 : : * WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 : : * Lesser General Public License for more details.
20 : : *
21 : : * You should have received a copy of the GNU Lesser General Public
22 : : * License along with this library; if not, write to the Free Software
23 : : * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
24 : : * 02110-1301 USA
25 : : */
26 : :
27 : : #include "service.h"
28 : :
29 : : #undef signals
30 : : #include <libaccounts-glib/ag-service.h>
31 : :
32 : : using namespace Accounts;
33 : :
34 : : namespace Accounts {
35 : : /*!
36 : : * @class Service
37 : : * @headerfile service.h Accounts/Service
38 : : *
39 : : * @brief Representation of an account service.
40 : : *
41 : : * @details The Service object represents an account service. It can be used to
42 : : * retrieve some basic properties of the service (such as name, type and
43 : : * provider) and to get access to the contents of the XML file which defines
44 : : * it.
45 : : */
46 : : }; // namespace
47 : :
48 : 21 : Service::Service(AgService *service, ReferenceMode mode):
49 : : m_service(service),
50 : 21 : m_tags(0)
51 : : {
52 [ + + ][ + + ]: 21 : if (m_service != 0 && mode == AddReference)
53 : 2 : ag_service_ref(m_service);
54 : 21 : }
55 : :
56 : : /*!
57 : : * Construct an invalid service.
58 : : */
59 : 6 : Service::Service():
60 : : m_service(0),
61 : 6 : m_tags(0)
62 : : {
63 : 6 : }
64 : :
65 : : /*!
66 : : * Copy constructor. Copying a Service object is very cheap, because the
67 : : * data is shared among copies.
68 : : */
69 : 4 : Service::Service(const Service &other):
70 : : m_service(other.m_service),
71 : 4 : m_tags(0)
72 : : {
73 [ + - ]: 4 : if (m_service != 0)
74 : 4 : ag_service_ref(m_service);
75 : 4 : }
76 : :
77 : 2 : Service &Service::operator=(const Service &other)
78 : : {
79 [ + - ]: 2 : if (m_service == other.m_service) return *this;
80 [ + - ]: 2 : if (m_service != 0)
81 : 2 : ag_service_unref(m_service);
82 : 2 : m_service = other.m_service;
83 [ + + ]: 2 : if (m_service != 0)
84 : 2 : ag_service_ref(m_service);
85 : : return *this;
86 : : }
87 : :
88 : 31 : Service::~Service()
89 : : {
90 : 31 : TRACE();
91 : :
92 [ + + ]: 31 : if (m_service != 0) {
93 : 22 : ag_service_unref(m_service);
94 : 22 : m_service = 0;
95 : : }
96 [ - + ]: 31 : if (m_tags != 0) {
97 [ # # ]: 0 : delete m_tags;
98 : 0 : m_tags = 0;
99 : : }
100 : 31 : }
101 : :
102 : : /*!
103 : : * Check whether this object represents a Service.
104 : : * @return true if the Service is a valid one.
105 : : */
106 : 35 : bool Service::isValid() const
107 : : {
108 : 35 : return m_service != 0;
109 : : }
110 : :
111 : : /*!
112 : : * Get the name of the service. This can be used as a unique identifier for
113 : : * this service.
114 : : * @return The unique name of the service.
115 : : */
116 : 4 : QString Service::name() const
117 : : {
118 : 4 : return UTF8(ag_service_get_name(m_service));
119 : : }
120 : :
121 : : /*!
122 : : * Get the display name of the service, untranslated.
123 : : * @return The display name of the service.
124 : : */
125 : 1 : QString Service::displayName() const
126 : : {
127 : 1 : return UTF8(ag_service_get_display_name(m_service));
128 : : }
129 : :
130 : : /*!
131 : : * Get the service type ID of the service.
132 : : * @return The service type of the service.
133 : : */
134 : 1 : QString Service::serviceType() const
135 : : {
136 : 1 : return ASCII(ag_service_get_service_type(m_service));
137 : : }
138 : :
139 : : /*!
140 : : * @return The translation catalog of the service.
141 : : */
142 : 0 : QString Service::trCatalog() const
143 : : {
144 : 0 : return ASCII(ag_service_get_i18n_domain(m_service));
145 : : }
146 : :
147 : : /*!
148 : : * Get the provider ID of the service.
149 : : * @return The provider of the service.
150 : : */
151 : 1 : QString Service::provider() const
152 : : {
153 : 1 : return UTF8(ag_service_get_provider(m_service));
154 : : }
155 : :
156 : : /*!
157 : : * Get the icon name for this service.
158 : : * @return The icon name.
159 : : */
160 : 0 : QString Service::iconName() const
161 : : {
162 : 0 : return ASCII(ag_service_get_icon_name(m_service));
163 : : }
164 : :
165 : : /*!
166 : : * Check if this service has a tag.
167 : : *
168 : : * @param tag Tag to look for
169 : : *
170 : : * @return Service has the tag?
171 : : */
172 : 1 : bool Service::hasTag(const QString &tag) const
173 : : {
174 : 1 : return ag_service_has_tag(m_service, tag.toUtf8().constData());
175 : : }
176 : :
177 : : /*!
178 : : * Return all tags of the service as a set.
179 : : *
180 : : * @return Set of tags
181 : : */
182 : 0 : QSet<QString> Service::tags() const
183 : : {
184 [ # # ]: 0 : if (m_tags)
185 : 0 : return *m_tags;
186 : :
187 : 0 : m_tags = new QSet<QString>;
188 : 0 : GList *list = ag_service_get_tags(m_service);
189 : : GList *iter = list;
190 [ # # ]: 0 : while (iter != NULL) {
191 : 0 : m_tags->insert(UTF8(reinterpret_cast<const gchar *> (iter->data)));
192 [ # # ]: 0 : iter = g_list_next(iter);
193 : : }
194 : 0 : g_list_free(list);
195 : 0 : return *m_tags;
196 : : }
197 : :
198 : : /*!
199 : : * Get the contents of the service XML file.
200 : : * @return The DOM of the whole XML service file
201 : : */
202 : 1 : const QDomDocument Service::domDocument() const
203 : : {
204 : : const gchar *data;
205 : :
206 : 1 : ag_service_get_file_contents(m_service, &data, NULL);
207 : :
208 : 1 : QDomDocument doc;
209 : : QString errorStr;
210 : : int errorLine;
211 : : int errorColumn;
212 [ - + ]: 1 : if (!doc.setContent(QByteArray(data), true,
213 : 1 : &errorStr, &errorLine, &errorColumn))
214 : : {
215 : : QString message(ASCII("Parse error reading account service file "
216 : 0 : "at line %1, column %2:\n%3"));
217 : 0 : message.arg(errorLine).arg(errorColumn).arg(errorStr);
218 : 0 : qWarning() << __PRETTY_FUNCTION__ << message;
219 : : }
220 : 1 : return doc;
221 : : }
222 : :
223 : 18 : AgService *Service::service() const
224 : : {
225 : 18 : return m_service;
226 : : }
227 : :
|