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  Q_FOREACH(const QString &entry, m_gSettings->get("items").toStringList()) {
36  if (entry.startsWith("application:///")) {
37  // convert legacy entries to new world appids
38  QString appId = entry;
39  // Transform "application://foobar.desktop" to "foobar"
40  appId.remove(QRegExp("^application:///"));
41  appId.remove(QRegExp(".desktop$"));
42  storedApps << appId;
43  } else if (entry.startsWith("appid://")) {
44  QString appId = entry;
45  appId.remove("appid://");
46  if (appId.split('/').count() == 3) {
47  // Strip current-user-version in case its there
48  appId = appId.split('/').first() + "_" + appId.split('/').at(1);
49  }
50  storedApps << appId;
51  }
52  }
53  return storedApps;
54 }
55 
56 void GSettings::setStoredApplications(const QStringList &storedApplications)
57 {
58  QStringList gSettingsList;
59  Q_FOREACH(const QString &entry, storedApplications) {
60  gSettingsList << QString("appid://%1").arg(entry);
61  }
62  // GSettings will emit a changed signal to ourselves. Let's cache the items
63  // and only forward the changed signal when the list did actually change.
64  m_cachedItems = gSettingsList;
65  m_gSettings->set("items", gSettingsList);
66 }
67 
68 void GSettings::onSettingsChanged(const QString &key)
69 {
70  if (key == "items") {
71  QStringList cachedItems = m_gSettings->get("items").toStringList();
72  if (m_cachedItems != cachedItems) {
73  m_cachedItems = cachedItems;
74  Q_EMIT changed();
75  }
76  }
77 }