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-type.h"
28 : :
29 : : #undef signals
30 : : #include <libaccounts-glib/ag-service-type.h>
31 : :
32 : : using namespace Accounts;
33 : :
34 : : namespace Accounts {
35 : : /*!
36 : : * @class ServiceType
37 : : * @headerfile service-type.h Accounts/ServiceType
38 : : *
39 : : * @brief Representation of an account service type.
40 : : *
41 : : * @details The ServiceType object represents an account service type. It can
42 : : * be used to retrieve some basic properties of the service type (such as
43 : : * name and icon) and to get access to the contents of the XML file which
44 : : * defines it.
45 : : */
46 : : }; // namespace
47 : :
48 : 2 : ServiceType::ServiceType(AgServiceType *serviceType, ReferenceMode mode):
49 : : m_serviceType(serviceType),
50 : 2 : m_tags(0)
51 : : {
52 : 2 : TRACE();
53 [ + + ][ - + ]: 2 : if (m_serviceType != 0 && mode == AddReference)
54 : 0 : ag_service_type_ref(m_serviceType);
55 : 2 : }
56 : :
57 : : /*!
58 : : * Construct an invalid serviceType.
59 : : */
60 : 1 : ServiceType::ServiceType():
61 : : m_serviceType(0),
62 : 1 : m_tags(0)
63 : : {
64 : 1 : }
65 : :
66 : : /*!
67 : : * Copy constructor. Copying a ServiceType object is very cheap, because the
68 : : * data is shared among copies.
69 : : */
70 : 0 : ServiceType::ServiceType(const ServiceType &other):
71 : : m_serviceType(other.m_serviceType),
72 : 0 : m_tags(0)
73 : : {
74 [ # # ]: 0 : if (m_serviceType != 0)
75 : 0 : ag_service_type_ref(m_serviceType);
76 : 0 : }
77 : :
78 : 2 : ServiceType &ServiceType::operator=(const ServiceType &other)
79 : : {
80 [ + + ]: 2 : if (m_serviceType == other.m_serviceType) return *this;
81 [ - + ]: 1 : if (m_serviceType != 0)
82 : 0 : ag_service_type_unref(m_serviceType);
83 : 1 : m_serviceType = other.m_serviceType;
84 [ + - ]: 1 : if (m_serviceType != 0)
85 : 2 : ag_service_type_ref(m_serviceType);
86 : : return *this;
87 : : }
88 : :
89 : 3 : ServiceType::~ServiceType()
90 : : {
91 : 3 : TRACE();
92 [ + + ]: 3 : if (m_serviceType != 0) {
93 : 2 : ag_service_type_unref(m_serviceType);
94 : 2 : m_serviceType = 0;
95 : : }
96 [ + + ]: 3 : if (m_tags != 0) {
97 [ + - ]: 1 : delete m_tags;
98 : 1 : m_tags = 0;
99 : : }
100 : 3 : }
101 : :
102 : : /*!
103 : : * Check whether this object represents a ServiceType.
104 : : * @return true if the ServiceType is a valid one.
105 : : */
106 : 2 : bool ServiceType::isValid() const
107 : : {
108 : 2 : return m_serviceType != 0;
109 : : }
110 : :
111 : : /*!
112 : : * Returns the name (ID) of the service type.
113 : : */
114 : 1 : QString ServiceType::name() const
115 : : {
116 : 1 : return UTF8(ag_service_type_get_name(m_serviceType));
117 : : }
118 : :
119 : : /*!
120 : : * @return The display name of the service type; this is a string that
121 : : * could be shown in the UI to describe the service type to the user.
122 : : *
123 : : * The library attempts to translate this string by passing it to the
124 : : * qtTrId() function; in order for this to work you must make sure that
125 : : * the translation catalogue has been loaded before, if needed.
126 : : */
127 : 1 : QString ServiceType::displayName() const
128 : : {
129 : : const gchar *id;
130 : :
131 : : /* libaccounts-glib returns the display name untranslated. */
132 : 1 : id = ag_service_type_get_display_name(m_serviceType);
133 [ + - ]: 1 : if (id != NULL) {
134 : 1 : return qtTrId(id);
135 : : } else {
136 : : return QString();
137 : : }
138 : : }
139 : :
140 : : /*!
141 : : * @return The name of the translation catalog, which can be used to
142 : : * translate the displayName()
143 : : */
144 : 1 : QString ServiceType::trCatalog() const
145 : : {
146 : 1 : return ASCII(ag_service_type_get_i18n_domain(m_serviceType));
147 : : }
148 : :
149 : : /*!
150 : : * @return The icon name
151 : : */
152 : 1 : QString ServiceType::iconName() const
153 : : {
154 : 1 : return ASCII(ag_service_type_get_icon_name(m_serviceType));
155 : : }
156 : :
157 : : /*!
158 : : * Check if this service type has a tag.
159 : : *
160 : : * @param tag Tag to look for
161 : : *
162 : : * @return Service type has the tag?
163 : : */
164 : 0 : bool ServiceType::hasTag(const QString &tag) const
165 : : {
166 : 0 : return ag_service_type_has_tag(m_serviceType, tag.toUtf8().constData());
167 : : }
168 : :
169 : : /*!
170 : : * Return all tags of the service type as a set.
171 : : *
172 : : * @return Set of tags
173 : : */
174 : 1 : QSet<QString> ServiceType::tags() const
175 : : {
176 [ - + ]: 1 : if (m_tags)
177 : 0 : return *m_tags;
178 : :
179 : 1 : m_tags = new QSet<QString>;
180 : 1 : GList *list = ag_service_type_get_tags(m_serviceType);
181 : : GList *iter = list;
182 [ + + ]: 3 : while (iter != NULL) {
183 : 2 : m_tags->insert(UTF8(reinterpret_cast<const gchar *> (iter->data)));
184 [ + - ]: 2 : iter = g_list_next(iter);
185 : : }
186 : 1 : g_list_free(list);
187 : 1 : return *m_tags;
188 : : }
189 : :
190 : : /*!
191 : : * @return The DOM of the whole XML service file
192 : : */
193 : 0 : const QDomDocument ServiceType::domDocument() const
194 : : {
195 : : const gchar *data;
196 : : gsize len;
197 : :
198 : 0 : ag_service_type_get_file_contents(m_serviceType, &data, &len);
199 : :
200 : 0 : QDomDocument doc;
201 : : QString errorStr;
202 : : int errorLine;
203 : : int errorColumn;
204 [ # # ]: 0 : if (!doc.setContent(QByteArray(data, len), true,
205 : 0 : &errorStr, &errorLine, &errorColumn)) {
206 : : QString message(ASCII("Parse error reading serviceType file "
207 : 0 : "at line %1, column %2:\n%3"));
208 : 0 : message.arg(errorLine).arg(errorColumn).arg(errorStr);
209 : 0 : qWarning() << __PRETTY_FUNCTION__ << message;
210 : : }
211 : :
212 : 0 : return doc;
213 : : }
214 : :
|