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