accounts-qt  1.14
service-type.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-type.h"
28 
29 #undef signals
30 #include <libaccounts-glib/ag-service-type.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 ServiceType::ServiceType(AgServiceType *serviceType, ReferenceMode mode):
53  m_serviceType(serviceType),
54  m_tags(0)
55 {
56  if (m_serviceType != 0 && mode == AddReference)
57  ag_service_type_ref(m_serviceType);
58 }
59 
64  m_serviceType(0),
65  m_tags(0)
66 {
67 }
68 
74  m_serviceType(other.m_serviceType),
75  m_tags(0)
76 {
77  if (m_serviceType != 0)
78  ag_service_type_ref(m_serviceType);
79 }
80 
81 ServiceType &ServiceType::operator=(const ServiceType &other)
82 {
83  if (m_serviceType == other.m_serviceType) return *this;
84  if (m_serviceType != 0)
85  ag_service_type_unref(m_serviceType);
86  m_serviceType = other.m_serviceType;
87  if (m_serviceType != 0)
88  ag_service_type_ref(m_serviceType);
89  return *this;
90 }
91 
92 ServiceType::~ServiceType()
93 {
94  if (m_serviceType != 0) {
95  ag_service_type_unref(m_serviceType);
96  m_serviceType = 0;
97  }
98  if (m_tags != 0) {
99  delete m_tags;
100  m_tags = 0;
101  }
102 }
103 
109 {
110  return m_serviceType != 0;
111 }
112 
116 QString ServiceType::name() const
117 {
118  if (Q_UNLIKELY(!isValid())) return QString();
119  return UTF8(ag_service_type_get_name(m_serviceType));
120 }
121 
131 {
132  const gchar *id;
133 
134  /* libaccounts-glib returns the display name untranslated. */
135  id = ag_service_type_get_display_name(m_serviceType);
136  if (id != NULL) {
137  return qtTrId(id);
138  } else {
139  return QString();
140  }
141 }
142 
147 QString ServiceType::trCatalog() const
148 {
149  return ASCII(ag_service_type_get_i18n_domain(m_serviceType));
150 }
151 
155 QString ServiceType::iconName() const
156 {
157  return ASCII(ag_service_type_get_icon_name(m_serviceType));
158 }
159 
167 bool ServiceType::hasTag(const QString &tag) const
168 {
169  return ag_service_type_has_tag(m_serviceType, tag.toUtf8().constData());
170 }
171 
177 QSet<QString> ServiceType::tags() const
178 {
179  if (m_tags)
180  return *m_tags;
181 
182  m_tags = new QSet<QString>;
183  GList *list = ag_service_type_get_tags(m_serviceType);
184  GList *iter = list;
185  while (iter != NULL) {
186  m_tags->insert(UTF8(reinterpret_cast<const gchar *> (iter->data)));
187  iter = g_list_next(iter);
188  }
189  g_list_free(list);
190  return *m_tags;
191 }
192 
196 const QDomDocument ServiceType::domDocument() const
197 {
198  const gchar *data;
199  gsize len;
200 
201  ag_service_type_get_file_contents(m_serviceType, &data, &len);
202 
203  QDomDocument doc;
204  QString errorStr;
205  int errorLine;
206  int errorColumn;
207  if (!doc.setContent(QByteArray(data, len), true,
208  &errorStr, &errorLine, &errorColumn)) {
209  QString message(QStringLiteral("Parse error reading serviceType file "
210  "at line %1, column %2:\n%3"));
211  message = message.arg(errorLine).arg(errorColumn).arg(errorStr);
212  qWarning() << __PRETTY_FUNCTION__ << message;
213  }
214 
215  return doc;
216 }
217 
QString iconName() const
QString trCatalog() const
Representation of an account service type.
Definition: service-type.h:49
ServiceType()
Construct an invalid serviceType.
bool hasTag(const QString &tag) const
Check if this service type has a tag.
const QDomDocument domDocument() const
QString displayName() const
bool isValid() const
Check whether this object represents a ServiceType.
QSet< QString > tags() const
Return all tags of the service type as a set.
QString name() const
Returns the name (ID) of the service type.