accounts-qt  1.16
service.cpp
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.h"
28 
29 #undef signals
30 #include <libaccounts-glib/ag-service.h>
31 
32 using namespace Accounts;
33 
34 namespace Accounts {
46 }; // namespace
47 
48 Service::Service(AgService *service, ReferenceMode mode):
49  m_service(service),
50  m_tags(nullptr)
51 {
52  if (m_service != nullptr && mode == AddReference)
53  ag_service_ref(m_service);
54 }
55 
60  m_service(nullptr),
61  m_tags(nullptr)
62 {
63 }
64 
69 Service::Service(const Service &other):
70  m_service(other.m_service),
71  m_tags(nullptr)
72 {
73  if (m_service != nullptr)
74  ag_service_ref(m_service);
75 }
76 
77 Service &Service::operator=(const Service &other)
78 {
79  if (m_service == other.m_service) return *this;
80  if (m_service != nullptr)
81  ag_service_unref(m_service);
82  m_service = other.m_service;
83  if (m_service != nullptr)
84  ag_service_ref(m_service);
85  return *this;
86 }
87 
88 Service::~Service()
89 {
90  if (m_service != nullptr) {
91  ag_service_unref(m_service);
92  m_service = nullptr;
93  }
94  if (m_tags != nullptr) {
95  delete m_tags;
96  m_tags = nullptr;
97  }
98 }
99 
104 bool Service::isValid() const
105 {
106  return m_service != nullptr;
107 }
108 
114 QString Service::name() const
115 {
116  if (Q_UNLIKELY(!isValid())) return QString();
117  return UTF8(ag_service_get_name(m_service));
118 }
119 
124 QString Service::description() const
125 {
126  return UTF8(ag_service_get_description(m_service));
127 }
128 
133 QString Service::displayName() const
134 {
135  return UTF8(ag_service_get_display_name(m_service));
136 }
137 
142 QString Service::serviceType() const
143 {
144  return ASCII(ag_service_get_service_type(m_service));
145 }
146 
150 QString Service::trCatalog() const
151 {
152  return ASCII(ag_service_get_i18n_domain(m_service));
153 }
154 
159 QString Service::provider() const
160 {
161  return UTF8(ag_service_get_provider(m_service));
162 }
163 
168 QString Service::iconName() const
169 {
170  return ASCII(ag_service_get_icon_name(m_service));
171 }
172 
180 bool Service::hasTag(const QString &tag) const
181 {
182  return ag_service_has_tag(m_service, tag.toUtf8().constData());
183 }
184 
190 QSet<QString> Service::tags() const
191 {
192  if (m_tags)
193  return *m_tags;
194 
195  m_tags = new QSet<QString>;
196  GList *list = ag_service_get_tags(m_service);
197  GList *iter = list;
198  while (iter != NULL) {
199  m_tags->insert(UTF8(reinterpret_cast<const gchar *> (iter->data)));
200  iter = g_list_next(iter);
201  }
202  g_list_free(list);
203  return *m_tags;
204 }
205 
210 const QDomDocument Service::domDocument() const
211 {
212  const gchar *data;
213 
214  ag_service_get_file_contents(m_service, &data, NULL);
215 
216  QDomDocument doc;
217  QString errorStr;
218  int errorLine;
219  int errorColumn;
220  if (!doc.setContent(QByteArray(data), true,
221  &errorStr, &errorLine, &errorColumn))
222  {
223  QString message(QStringLiteral("Parse error reading account service file "
224  "at line %1, column %2:\n%3"));
225  message = message.arg(errorLine).arg(errorColumn).arg(errorStr);
226  qWarning() << __PRETTY_FUNCTION__ << message;
227  }
228  return doc;
229 }
230 
231 AgService *Service::service() const
232 {
233  return m_service;
234 }
235 
Accounts::Service::Service
Service()
Construct an invalid service.
Definition: service.cpp:59
Accounts::Service::tags
QSet< QString > tags() const
Return all tags of the service as a set.
Definition: service.cpp:190
Accounts::Service::provider
QString provider() const
Get the provider ID of the service.
Definition: service.cpp:159
Accounts::Service::name
QString name() const
Get the name of the service.
Definition: service.cpp:114
Accounts::Service::iconName
QString iconName() const
Get the icon name for this service.
Definition: service.cpp:168
Accounts::Service::description
QString description() const
Get the description of the service.
Definition: service.cpp:124
Accounts::Service::hasTag
bool hasTag(const QString &tag) const
Check if this service has a tag.
Definition: service.cpp:180
Accounts::Service
Representation of an account service.
Definition: service.h:49
Accounts::Service::displayName
QString displayName() const
Get the display name of the service, untranslated.
Definition: service.cpp:133
Accounts::Service::domDocument
const QDomDocument domDocument() const
Get the contents of the service XML file.
Definition: service.cpp:210
Accounts::Service::isValid
bool isValid() const
Check whether this object represents a Service.
Definition: service.cpp:104
Accounts::Service::serviceType
QString serviceType() const
Get the service type ID of the service.
Definition: service.cpp:142
Accounts::Service::trCatalog
QString trCatalog() const
Definition: service.cpp:150