accounts-qt  1.2
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 using namespace Accounts;
33 
34 namespace Accounts {
46 }; // namespace
47 
48 ServiceType::ServiceType(AgServiceType *serviceType, ReferenceMode mode):
49  m_serviceType(serviceType),
50  m_tags(0)
51 {
52  TRACE();
53  if (m_serviceType != 0 && mode == AddReference)
54  ag_service_type_ref(m_serviceType);
55 }
56 
61  m_serviceType(0),
62  m_tags(0)
63 {
64 }
65 
71  m_serviceType(other.m_serviceType),
72  m_tags(0)
73 {
74  if (m_serviceType != 0)
75  ag_service_type_ref(m_serviceType);
76 }
77 
78 ServiceType &ServiceType::operator=(const ServiceType &other)
79 {
80  if (m_serviceType == other.m_serviceType) return *this;
81  if (m_serviceType != 0)
82  ag_service_type_unref(m_serviceType);
83  m_serviceType = other.m_serviceType;
84  if (m_serviceType != 0)
85  ag_service_type_ref(m_serviceType);
86  return *this;
87 }
88 
89 ServiceType::~ServiceType()
90 {
91  TRACE();
92  if (m_serviceType != 0) {
93  ag_service_type_unref(m_serviceType);
94  m_serviceType = 0;
95  }
96  if (m_tags != 0) {
97  delete m_tags;
98  m_tags = 0;
99  }
100 }
101 
107 {
108  return m_serviceType != 0;
109 }
110 
114 QString ServiceType::name() const
115 {
116  return UTF8(ag_service_type_get_name(m_serviceType));
117 }
118 
128 {
129  const gchar *id;
130 
131  /* libaccounts-glib returns the display name untranslated. */
132  id = ag_service_type_get_display_name(m_serviceType);
133  if (id != NULL) {
134  return qtTrId(id);
135  } else {
136  return QString();
137  }
138 }
139 
144 QString ServiceType::trCatalog() const
145 {
146  return ASCII(ag_service_type_get_i18n_domain(m_serviceType));
147 }
148 
152 QString ServiceType::iconName() const
153 {
154  return ASCII(ag_service_type_get_icon_name(m_serviceType));
155 }
156 
164 bool ServiceType::hasTag(const QString &tag) const
165 {
166  return ag_service_type_has_tag(m_serviceType, tag.toUtf8().constData());
167 }
168 
174 QSet<QString> ServiceType::tags() const
175 {
176  if (m_tags)
177  return *m_tags;
178 
179  m_tags = new QSet<QString>;
180  GList *list = ag_service_type_get_tags(m_serviceType);
181  GList *iter = list;
182  while (iter != NULL) {
183  m_tags->insert(UTF8(reinterpret_cast<const gchar *> (iter->data)));
184  iter = g_list_next(iter);
185  }
186  g_list_free(list);
187  return *m_tags;
188 }
189 
193 const QDomDocument ServiceType::domDocument() const
194 {
195  const gchar *data;
196  gsize len;
197 
198  ag_service_type_get_file_contents(m_serviceType, &data, &len);
199 
200  QDomDocument doc;
201  QString errorStr;
202  int errorLine;
203  int errorColumn;
204  if (!doc.setContent(QByteArray(data, len), true,
205  &errorStr, &errorLine, &errorColumn)) {
206  QString message(ASCII("Parse error reading serviceType file "
207  "at line %1, column %2:\n%3"));
208  message.arg(errorLine).arg(errorColumn).arg(errorStr);
209  qWarning() << __PRETTY_FUNCTION__ << message;
210  }
211 
212  return doc;
213 }
214