accounts-qt  1.14
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 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 #if QT_VERSION < QT_VERSION_CHECK(5 ,0 ,0)
33 #define QStringLiteral QString::fromUtf8
34 #endif
35 
36 using namespace Accounts;
37 
38 namespace Accounts {
50 }; // namespace
51 
52 Service::Service(AgService *service, ReferenceMode mode):
53  m_service(service),
54  m_tags(0)
55 {
56  if (m_service != 0 && mode == AddReference)
57  ag_service_ref(m_service);
58 }
59 
64  m_service(0),
65  m_tags(0)
66 {
67 }
68 
73 Service::Service(const Service &other):
74  m_service(other.m_service),
75  m_tags(0)
76 {
77  if (m_service != 0)
78  ag_service_ref(m_service);
79 }
80 
81 Service &Service::operator=(const Service &other)
82 {
83  if (m_service == other.m_service) return *this;
84  if (m_service != 0)
85  ag_service_unref(m_service);
86  m_service = other.m_service;
87  if (m_service != 0)
88  ag_service_ref(m_service);
89  return *this;
90 }
91 
92 Service::~Service()
93 {
94  if (m_service != 0) {
95  ag_service_unref(m_service);
96  m_service = 0;
97  }
98  if (m_tags != 0) {
99  delete m_tags;
100  m_tags = 0;
101  }
102 }
103 
108 bool Service::isValid() const
109 {
110  return m_service != 0;
111 }
112 
118 QString Service::name() const
119 {
120  if (Q_UNLIKELY(!isValid())) return QString();
121  return UTF8(ag_service_get_name(m_service));
122 }
123 
128 QString Service::displayName() const
129 {
130  return UTF8(ag_service_get_display_name(m_service));
131 }
132 
137 QString Service::serviceType() const
138 {
139  return ASCII(ag_service_get_service_type(m_service));
140 }
141 
145 QString Service::trCatalog() const
146 {
147  return ASCII(ag_service_get_i18n_domain(m_service));
148 }
149 
154 QString Service::provider() const
155 {
156  return UTF8(ag_service_get_provider(m_service));
157 }
158 
163 QString Service::iconName() const
164 {
165  return ASCII(ag_service_get_icon_name(m_service));
166 }
167 
175 bool Service::hasTag(const QString &tag) const
176 {
177  return ag_service_has_tag(m_service, tag.toUtf8().constData());
178 }
179 
185 QSet<QString> Service::tags() const
186 {
187  if (m_tags)
188  return *m_tags;
189 
190  m_tags = new QSet<QString>;
191  GList *list = ag_service_get_tags(m_service);
192  GList *iter = list;
193  while (iter != NULL) {
194  m_tags->insert(UTF8(reinterpret_cast<const gchar *> (iter->data)));
195  iter = g_list_next(iter);
196  }
197  g_list_free(list);
198  return *m_tags;
199 }
200 
205 const QDomDocument Service::domDocument() const
206 {
207  const gchar *data;
208 
209  ag_service_get_file_contents(m_service, &data, NULL);
210 
211  QDomDocument doc;
212  QString errorStr;
213  int errorLine;
214  int errorColumn;
215  if (!doc.setContent(QByteArray(data), true,
216  &errorStr, &errorLine, &errorColumn))
217  {
218  QString message(QStringLiteral("Parse error reading account service file "
219  "at line %1, column %2:\n%3"));
220  message = message.arg(errorLine).arg(errorColumn).arg(errorStr);
221  qWarning() << __PRETTY_FUNCTION__ << message;
222  }
223  return doc;
224 }
225 
226 AgService *Service::service() const
227 {
228  return m_service;
229 }
230 
bool isValid() const
Check whether this object represents a Service.
Definition: service.cpp:108
QSet< QString > tags() const
Return all tags of the service as a set.
Definition: service.cpp:185
const QDomDocument domDocument() const
Get the contents of the service XML file.
Definition: service.cpp:205
Representation of an account service.
Definition: service.h:48
QString serviceType() const
Get the service type ID of the service.
Definition: service.cpp:137
bool hasTag(const QString &tag) const
Check if this service has a tag.
Definition: service.cpp:175
Service()
Construct an invalid service.
Definition: service.cpp:63
QString trCatalog() const
Definition: service.cpp:145
QString iconName() const
Get the icon name for this service.
Definition: service.cpp:163
QString displayName() const
Get the display name of the service, untranslated.
Definition: service.cpp:128
QString name() const
Get the name of the service.
Definition: service.cpp:118
QString provider() const
Get the provider ID of the service.
Definition: service.cpp:154