Unity 8
gsettings.cpp
1 /*
2  * Copyright 2014 Canonical Ltd.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation; version 3.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * Authors:
17  * Michael Zanetti <michael.zanetti@canonical.com>
18  */
19 
20 #include "gsettings.h"
21 
22 #include <QVariant>
23 
24 GSettings::GSettings(QObject *parent):
25  QObject(parent)
26 {
27  m_gSettings = new QGSettings("com.canonical.Unity.Launcher", "/com/canonical/unity/launcher/", this);
28  connect(m_gSettings, &QGSettings::changed, this, &GSettings::onSettingsChanged);
29 }
30 
31 QStringList GSettings::storedApplications() const
32 {
33  QStringList storedApps;
34 
35  const QString items = QStringLiteral("items");
36  Q_FOREACH(const QString &entry, m_gSettings->get(items).toStringList()) {
37  if (entry.startsWith(QLatin1String("application:///"))) {
38  // convert legacy entries to new world appids
39  QString appId = entry;
40  // Transform "application://foobar.desktop" to "foobar"
41  appId.remove(QRegExp(QStringLiteral("^application:///")));
42  appId.remove(QRegExp(QStringLiteral(".desktop$")));
43  storedApps << appId;
44  } else if (entry.startsWith(QLatin1String("appid://"))) {
45  QString appId = entry;
46  appId.remove(QStringLiteral("appid://"));
47  const QStringList splittedAppId = appId.split('/');
48  if (splittedAppId.count() == 3) {
49  // Strip current-user-version in case its there
50  appId = splittedAppId.first() + "_" + splittedAppId.at(1);
51  }
52  storedApps << appId;
53  }
54  }
55  return storedApps;
56 }
57 
58 void GSettings::setStoredApplications(const QStringList &storedApplications)
59 {
60  QStringList gSettingsList;
61  gSettingsList.reserve(storedApplications.count());
62  Q_FOREACH(const QString &entry, storedApplications) {
63  gSettingsList << QStringLiteral("appid://%1").arg(entry);
64  }
65  // GSettings will emit a changed signal to ourselves. Let's cache the items
66  // and only forward the changed signal when the list did actually change.
67  m_cachedItems = gSettingsList;
68  m_gSettings->set(QStringLiteral("items"), gSettingsList);
69 }
70 
71 void GSettings::onSettingsChanged(const QString &key)
72 {
73  if (key == QLatin1String("items")) {
74  const QStringList cachedItems = m_gSettings->get(QStringLiteral("items")).toStringList();
75  if (m_cachedItems != cachedItems) {
76  m_cachedItems = cachedItems;
77  Q_EMIT changed();
78  }
79  }
80 }