accounts-qt  1.13
service-type.cpp
00001 /* vi: set et sw=4 ts=4 cino=t0,(0: */
00002 /*
00003  * This file is part of libaccounts-qt
00004  *
00005  * Copyright (C) 2009-2011 Nokia Corporation.
00006  * Copyright (C) 2012 Canonical Ltd.
00007  * Copyright (C) 2012 Intel Corporation.
00008  *
00009  * Contact: Alberto Mardegan <alberto.mardegan@canonical.com>
00010  * Contact: Jussi Laako <jussi.laako@linux.intel.com>
00011  *
00012  * This library is free software; you can redistribute it and/or
00013  * modify it under the terms of the GNU Lesser General Public License
00014  * version 2.1 as published by the Free Software Foundation.
00015  *
00016  * This library is distributed in the hope that it will be useful, but
00017  * WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00019  * Lesser General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU Lesser General Public
00022  * License along with this library; if not, write to the Free Software
00023  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
00024  * 02110-1301 USA
00025  */
00026 
00027 #include "service-type.h"
00028 
00029 #undef signals
00030 #include <libaccounts-glib/ag-service-type.h>
00031 
00032 using namespace Accounts;
00033 
00034 namespace Accounts {
00046 }; // namespace
00047 
00048 ServiceType::ServiceType(AgServiceType *serviceType, ReferenceMode mode):
00049     m_serviceType(serviceType),
00050     m_tags(0)
00051 {
00052     if (m_serviceType != 0 && mode == AddReference)
00053         ag_service_type_ref(m_serviceType);
00054 }
00055 
00059 ServiceType::ServiceType():
00060     m_serviceType(0),
00061     m_tags(0)
00062 {
00063 }
00064 
00069 ServiceType::ServiceType(const ServiceType &other):
00070     m_serviceType(other.m_serviceType),
00071     m_tags(0)
00072 {
00073     if (m_serviceType != 0)
00074         ag_service_type_ref(m_serviceType);
00075 }
00076 
00077 ServiceType &ServiceType::operator=(const ServiceType &other)
00078 {
00079     if (m_serviceType == other.m_serviceType) return *this;
00080     if (m_serviceType != 0)
00081         ag_service_type_unref(m_serviceType);
00082     m_serviceType = other.m_serviceType;
00083     if (m_serviceType != 0)
00084         ag_service_type_ref(m_serviceType);
00085     return *this;
00086 }
00087 
00088 ServiceType::~ServiceType()
00089 {
00090     if (m_serviceType != 0) {
00091         ag_service_type_unref(m_serviceType);
00092         m_serviceType = 0;
00093     }
00094     if (m_tags != 0) {
00095         delete m_tags;
00096         m_tags = 0;
00097     }
00098 }
00099 
00104 bool ServiceType::isValid() const
00105 {
00106     return m_serviceType != 0;
00107 }
00108 
00112 QString ServiceType::name() const
00113 {
00114     if (Q_UNLIKELY(!isValid())) return QString();
00115     return UTF8(ag_service_type_get_name(m_serviceType));
00116 }
00117 
00126 QString ServiceType::displayName() const
00127 {
00128     const gchar *id;
00129 
00130     /* libaccounts-glib returns the display name untranslated. */
00131     id = ag_service_type_get_display_name(m_serviceType);
00132     if (id != NULL) {
00133         return qtTrId(id);
00134     } else {
00135         return QString();
00136     }
00137 }
00138 
00143 QString ServiceType::trCatalog() const
00144 {
00145     return ASCII(ag_service_type_get_i18n_domain(m_serviceType));
00146 }
00147 
00151 QString ServiceType::iconName() const
00152 {
00153     return ASCII(ag_service_type_get_icon_name(m_serviceType));
00154 }
00155 
00163 bool ServiceType::hasTag(const QString &tag) const
00164 {
00165     return ag_service_type_has_tag(m_serviceType, tag.toUtf8().constData());
00166 }
00167 
00173 QSet<QString> ServiceType::tags() const
00174 {
00175     if (m_tags)
00176         return *m_tags;
00177 
00178     m_tags = new QSet<QString>;
00179     GList *list = ag_service_type_get_tags(m_serviceType);
00180     GList *iter = list;
00181     while (iter != NULL) {
00182         m_tags->insert(UTF8(reinterpret_cast<const gchar *> (iter->data)));
00183         iter = g_list_next(iter);
00184     }
00185     g_list_free(list);
00186     return *m_tags;
00187 }
00188 
00192 const QDomDocument ServiceType::domDocument() const
00193 {
00194     const gchar *data;
00195     gsize len;
00196 
00197     ag_service_type_get_file_contents(m_serviceType, &data, &len);
00198 
00199     QDomDocument doc;
00200     QString errorStr;
00201     int errorLine;
00202     int errorColumn;
00203     if (!doc.setContent(QByteArray(data, len), true,
00204                         &errorStr, &errorLine, &errorColumn)) {
00205         QString message(ASCII("Parse error reading serviceType file "
00206                               "at line %1, column %2:\n%3"));
00207         message.arg(errorLine).arg(errorColumn).arg(errorStr);
00208         qWarning() << __PRETTY_FUNCTION__ << message;
00209     }
00210 
00211     return doc;
00212 }
00213