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-2016 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.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(nullptr)
51 : {
52 2 : if (m_serviceType != nullptr && 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(nullptr),
61 1 : m_tags(nullptr)
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 1 : m_serviceType(other.m_serviceType),
71 1 : m_tags(nullptr)
72 : {
73 1 : if (m_serviceType != nullptr)
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 != nullptr)
81 0 : ag_service_type_unref(m_serviceType);
82 1 : m_serviceType = other.m_serviceType;
83 1 : if (m_serviceType != nullptr)
84 1 : ag_service_type_ref(m_serviceType);
85 1 : return *this;
86 : }
87 :
88 8 : ServiceType::~ServiceType()
89 : {
90 4 : if (m_serviceType != nullptr) {
91 3 : ag_service_type_unref(m_serviceType);
92 3 : m_serviceType = nullptr;
93 : }
94 4 : if (m_tags != nullptr) {
95 1 : delete m_tags;
96 1 : m_tags = nullptr;
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 3 : bool ServiceType::isValid() const
105 : {
106 3 : return m_serviceType != nullptr;
107 : }
108 :
109 : /*!
110 : * Returns the name (ID) of the service type.
111 : */
112 1 : QString ServiceType::name() const
113 : {
114 1 : if (Q_UNLIKELY(!isValid())) return QString();
115 1 : return UTF8(ag_service_type_get_name(m_serviceType));
116 : }
117 :
118 : /*!
119 : * @return The description of the service type.
120 : */
121 1 : QString ServiceType::description() const
122 : {
123 1 : return UTF8(ag_service_type_get_description(m_serviceType));
124 : }
125 :
126 : /*!
127 : * @return The display name of the service type; this is a string that
128 : * could be shown in the UI to describe the service type to the user.
129 : *
130 : * The library attempts to translate this string by passing it to the
131 : * qtTrId() function; in order for this to work you must make sure that
132 : * the translation catalogue has been loaded before, if needed.
133 : */
134 2 : QString ServiceType::displayName() const
135 : {
136 : const gchar *id;
137 :
138 : /* libaccounts-glib returns the display name untranslated. */
139 2 : id = ag_service_type_get_display_name(m_serviceType);
140 2 : if (id != NULL) {
141 2 : return qtTrId(id);
142 : } else {
143 0 : return QString();
144 : }
145 : }
146 :
147 : /*!
148 : * @return The name of the translation catalog, which can be used to
149 : * translate the displayName()
150 : */
151 1 : QString ServiceType::trCatalog() const
152 : {
153 1 : return ASCII(ag_service_type_get_i18n_domain(m_serviceType));
154 : }
155 :
156 : /*!
157 : * @return The icon name
158 : */
159 1 : QString ServiceType::iconName() const
160 : {
161 1 : return ASCII(ag_service_type_get_icon_name(m_serviceType));
162 : }
163 :
164 : /*!
165 : * Check if this service type has a tag.
166 : *
167 : * @param tag Tag to look for
168 : *
169 : * @return Service type has the tag?
170 : */
171 2 : bool ServiceType::hasTag(const QString &tag) const
172 : {
173 2 : return ag_service_type_has_tag(m_serviceType, tag.toUtf8().constData());
174 : }
175 :
176 : /*!
177 : * Return all tags of the service type as a set.
178 : *
179 : * @return Set of tags
180 : */
181 2 : QSet<QString> ServiceType::tags() const
182 : {
183 2 : if (m_tags)
184 1 : return *m_tags;
185 :
186 1 : m_tags = new QSet<QString>;
187 1 : GList *list = ag_service_type_get_tags(m_serviceType);
188 1 : GList *iter = list;
189 5 : while (iter != NULL) {
190 2 : m_tags->insert(UTF8(reinterpret_cast<const gchar *> (iter->data)));
191 2 : iter = g_list_next(iter);
192 : }
193 1 : g_list_free(list);
194 1 : return *m_tags;
195 : }
196 :
197 : /*!
198 : * @return The DOM of the whole XML service file
199 : */
200 1 : const QDomDocument ServiceType::domDocument() const
201 : {
202 : const gchar *data;
203 : gsize len;
204 :
205 1 : ag_service_type_get_file_contents(m_serviceType, &data, &len);
206 :
207 1 : QDomDocument doc;
208 2 : QString errorStr;
209 : int errorLine;
210 : int errorColumn;
211 1 : if (!doc.setContent(QByteArray(data, len), true,
212 : &errorStr, &errorLine, &errorColumn)) {
213 0 : QString message(QStringLiteral("Parse error reading serviceType file "
214 0 : "at line %1, column %2:\n%3"));
215 0 : message = message.arg(errorLine).arg(errorColumn).arg(errorStr);
216 0 : qWarning() << __PRETTY_FUNCTION__ << message;
217 : }
218 :
219 2 : return doc;
220 : }
221 :
|