service-type.cpp
/* vi: set et sw=4 ts=4 cino=t0,(0: */
/*
* This file is part of libaccounts-qt
*
* Copyright (C) 2009-2011 Nokia Corporation.
* Copyright (C) 2012-2016 Canonical Ltd.
* Copyright (C) 2012 Intel Corporation.
*
* Contact: Alberto Mardegan <alberto.mardegan@canonical.com>
* Contact: Jussi Laako <jussi.laako@linux.intel.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2.1 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#include "service-type.h"
#undef signals
#include <libaccounts-glib.h>
using namespace Accounts;
namespace Accounts {
}; // namespace
ServiceType::ServiceType(AgServiceType *serviceType, ReferenceMode mode):
m_serviceType(serviceType),
m_tags(nullptr)
{
if (m_serviceType != nullptr && mode == AddReference)
ag_service_type_ref(m_serviceType);
}
ServiceType::ServiceType():
m_serviceType(nullptr),
m_tags(nullptr)
{
}
ServiceType::ServiceType(const ServiceType &other):
m_serviceType(other.m_serviceType),
m_tags(nullptr)
{
if (m_serviceType != nullptr)
ag_service_type_ref(m_serviceType);
}
ServiceType &ServiceType::operator=(const ServiceType &other)
{
if (m_serviceType == other.m_serviceType) return *this;
if (m_serviceType != nullptr)
ag_service_type_unref(m_serviceType);
m_serviceType = other.m_serviceType;
if (m_serviceType != nullptr)
ag_service_type_ref(m_serviceType);
return *this;
}
ServiceType::~ServiceType()
{
if (m_serviceType != nullptr) {
ag_service_type_unref(m_serviceType);
m_serviceType = nullptr;
}
if (m_tags != nullptr) {
delete m_tags;
m_tags = nullptr;
}
}
bool ServiceType::isValid() const
{
return m_serviceType != nullptr;
}
QString ServiceType::name() const
{
if (Q_UNLIKELY(!isValid())) return QString();
return UTF8(ag_service_type_get_name(m_serviceType));
}
QString ServiceType::description() const
{
return UTF8(ag_service_type_get_description(m_serviceType));
}
QString ServiceType::displayName() const
{
const gchar *id;
/* libaccounts-glib returns the display name untranslated. */
id = ag_service_type_get_display_name(m_serviceType);
if (id != NULL) {
return qtTrId(id);
} else {
return QString();
}
}
QString ServiceType::trCatalog() const
{
return ASCII(ag_service_type_get_i18n_domain(m_serviceType));
}
QString ServiceType::iconName() const
{
return ASCII(ag_service_type_get_icon_name(m_serviceType));
}
bool ServiceType::hasTag(const QString &tag) const
{
return ag_service_type_has_tag(m_serviceType, tag.toUtf8().constData());
}
QSet<QString> ServiceType::tags() const
{
if (m_tags)
return *m_tags;
m_tags = new QSet<QString>;
GList *list = ag_service_type_get_tags(m_serviceType);
GList *iter = list;
while (iter != NULL) {
m_tags->insert(UTF8(reinterpret_cast<const gchar *> (iter->data)));
iter = g_list_next(iter);
}
g_list_free(list);
return *m_tags;
}
const QDomDocument ServiceType::domDocument() const
{
const gchar *data;
gsize len;
ag_service_type_get_file_contents(m_serviceType, &data, &len);
QDomDocument doc;
QString errorStr;
int errorLine;
int errorColumn;
if (!doc.setContent(QByteArray(data, len), true,
&errorStr, &errorLine, &errorColumn)) {
QString message(QStringLiteral("Parse error reading serviceType file "
"at line %1, column %2:\n%3"));
message = message.arg(errorLine).arg(errorColumn).arg(errorStr);
qWarning() << __PRETTY_FUNCTION__ << message;
}
return doc;
}
Accounts::ServiceType
Representation of an account service type.
Definition service-type.h:50
Accounts::ServiceType::tags
QSet< QString > tags() const
Return all tags of the service type as a set.
Definition service-type.cpp:181
Accounts::ServiceType::ServiceType
ServiceType()
Construct an invalid serviceType.
Definition service-type.cpp:59
Accounts::ServiceType::isValid
bool isValid() const
Check whether this object represents a ServiceType.
Definition service-type.cpp:104
Accounts::ServiceType::name
QString name() const
Returns the name (ID) of the service type.
Definition service-type.cpp:112
Accounts::ServiceType::iconName
QString iconName() const
Definition service-type.cpp:159
Accounts::ServiceType::displayName
QString displayName() const
Definition service-type.cpp:134
Accounts::ServiceType::description
QString description() const
Definition service-type.cpp:121
Accounts::ServiceType::hasTag
bool hasTag(const QString &tag) const
Check if this service type has a tag.
Definition service-type.cpp:171
Accounts::ServiceType::trCatalog
QString trCatalog() const
Definition service-type.cpp:151
Accounts::ServiceType::domDocument
const QDomDocument domDocument() const
Definition service-type.cpp:200