@@ -0,0 +1,254 @@
1+/*
2+ * Copyright 2013-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 "launchermodelas.h"
21+#include "launcheritem.h"
22+#include "AccountsServiceDBusAdaptor.h"
23+#include <unity/shell/application/ApplicationInfoInterface.h>
24+
25+#include <QDesktopServices>
26+#include <QDebug>
27+#include <QDBusArgument>
28+
29+using namespace unity::shell::application;
30+
31+LauncherModel::LauncherModel(QObject *parent):
32+ LauncherModelInterface(parent),
33+ m_accounts(new AccountsServiceDBusAdaptor(this))
34+{
35+ qDebug() << "Loading AS based launcher model";
36+ connect(m_accounts, &AccountsServiceDBusAdaptor::propertiesChanged, this, &LauncherModel::propertiesChanged);
37+ refresh();
38+}
39+
40+LauncherModel::~LauncherModel()
41+{
42+ while (!m_list.empty()) {
43+ m_list.takeFirst()->deleteLater();
44+ }
45+}
46+
47+int LauncherModel::rowCount(const QModelIndex &parent) const
48+{
49+ Q_UNUSED(parent)
50+ return m_list.count();
51+}
52+
53+QVariant LauncherModel::data(const QModelIndex &index, int role) const
54+{
55+ LauncherItem *item = m_list.at(index.row());
56+ switch(role) {
57+ case RoleAppId:
58+ return item->appId();
59+ case RoleName:
60+ return item->name();
61+ case RoleIcon:
62+ return item->icon();
63+ case RolePinned:
64+ return item->pinned();
65+ case RoleCount:
66+ return item->count();
67+ case RoleCountVisible:
68+ return item->countVisible();
69+ case RoleProgress:
70+ return item->progress();
71+ case RoleFocused:
72+ return item->focused();
73+ }
74+
75+ return QVariant();
76+}
77+
78+unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
79+{
80+ if (index < 0 || index >= m_list.count()) {
81+ return 0;
82+ }
83+ return m_list.at(index);
84+}
85+
86+void LauncherModel::move(int oldIndex, int newIndex)
87+{
88+ Q_UNUSED(oldIndex)
89+ Q_UNUSED(newIndex)
90+ qWarning() << "This is a read only implementation. Cannot move items.";
91+}
92+
93+void LauncherModel::pin(const QString &appId, int index)
94+{
95+ Q_UNUSED(appId)
96+ Q_UNUSED(index)
97+ qWarning() << "This is a read only implementation. Cannot pin items";
98+}
99+
100+void LauncherModel::requestRemove(const QString &appId)
101+{
102+ Q_UNUSED(appId)
103+ qWarning() << "This is a read only implementation. Cannot remove items";
104+}
105+
106+void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
107+{
108+ int index = findApplication(appId);
109+ if (index < 0) {
110+ return;
111+ }
112+
113+ LauncherItem *item = m_list.at(index);
114+ QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
115+ if (model) {
116+ QString actionId = model->get(actionIndex).actionId();
117+
118+ // Check if this is one of the launcher actions we handle ourselves
119+ if (actionId == "launch_item") {
120+ QDesktopServices::openUrl(getUrlForAppId(appId));
121+
122+ // Nope, we don't know this action, let the backend forward it to the application
123+ } else {
124+ // TODO: forward quicklist action to app, possibly via m_dbusIface
125+ }
126+ }
127+}
128+
129+void LauncherModel::setUser(const QString &username)
130+{
131+ if (m_user != username) {
132+ m_user = username;
133+ refresh();
134+ }
135+}
136+
137+QString LauncherModel::getUrlForAppId(const QString &appId) const
138+{
139+ // appId is either an appId or a legacy app name. Let's find out which
140+ if (appId.isEmpty()) {
141+ return QString();
142+ }
143+
144+ if (!appId.contains("_")) {
145+ return "application:///" + appId + ".desktop";
146+ }
147+
148+ QStringList parts = appId.split('_');
149+ QString package = parts.value(0);
150+ QString app = parts.value(1, "first-listed-app");
151+ return "appid://" + package + "/" + app + "/current-user-version";
152+}
153+
154+ApplicationManagerInterface *LauncherModel::applicationManager() const
155+{
156+ return nullptr;
157+}
158+
159+void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
160+{
161+ Q_UNUSED(appManager)
162+ qWarning() << "This plugin syncs all its states from AccountsService. Not using ApplicationManager.";
163+ return;
164+}
165+
166+int LauncherModel::findApplication(const QString &appId)
167+{
168+ for (int i = 0; i < m_list.count(); ++i) {
169+ LauncherItem *item = m_list.at(i);
170+ if (item->appId() == appId) {
171+ return i;
172+ }
173+ }
174+ return -1;
175+}
176+
177+typedef QList<QVariantMap> VariantMapList;
178+void LauncherModel::refresh()
179+{
180+ QList<QVariantMap> items;
181+
182+ if (m_accounts && !m_user.isEmpty()) {
183+ items = m_accounts->getUserProperty<VariantMapList>(m_user, "com.canonical.unity.AccountsService", "launcher-items");
184+ }
185+
186+ // First walk through all the existing items and see if we need to remove something
187+ QList<LauncherItem*> toBeRemoved;
188+
189+ Q_FOREACH (LauncherItem* item, m_list) {
190+ bool found = false;
191+ Q_FOREACH(const QVariant &asItem, items) {
192+ if (asItem.toMap().value("id").toString() == item->appId()) {
193+ found = true;
194+ item->setName(asItem.toMap().value("name").toString());
195+ item->setIcon(asItem.toMap().value("icon").toString());
196+ item->setCount(asItem.toMap().value("count").toInt());
197+ item->setCountVisible(asItem.toMap().value("countVisible").toBool());
198+ int idx = m_list.indexOf(item);
199+ Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleName << RoleIcon);
200+ }
201+ }
202+ if (!found) {
203+ toBeRemoved.append(item);
204+ }
205+ }
206+
207+ Q_FOREACH (LauncherItem* item, toBeRemoved) {
208+ int idx = m_list.indexOf(item);
209+ beginRemoveRows(QModelIndex(), idx, idx);
210+ m_list.takeAt(idx)->deleteLater();
211+ endRemoveRows();
212+ }
213+
214+ // Now walk through list and see if we need to add something
215+ for (int asIndex = 0; asIndex < items.count(); ++asIndex) {
216+ QVariant entry = items.at(asIndex);
217+ qDebug() << "have AS item" << entry.toMap().value("id").toString();
218+ int itemIndex = -1;
219+ for (int i = 0; i < m_list.count(); ++i) {
220+ if (m_list.at(i)->appId() == entry.toMap().value("id").toString()) {
221+ itemIndex = i;
222+ break;
223+ }
224+ }
225+
226+ if (itemIndex == -1) {
227+ // Need to add it. Just add it into the addedIndex to keep same ordering as the list in AS.
228+ LauncherItem *item = new LauncherItem(entry.toMap().value("id").toString(),
229+ entry.toMap().value("name").toString(),
230+ entry.toMap().value("icon").toString(),
231+ this);
232+ item->setPinned(true);
233+ item->setCount(entry.toMap().value("count").toInt());
234+ item->setCountVisible(entry.toMap().value("countVisible").toBool());
235+ beginInsertRows(QModelIndex(), asIndex, asIndex);
236+ m_list.insert(asIndex, item);
237+ endInsertRows();
238+ } else if (itemIndex != asIndex) {
239+ // The item is already there, but it is in a different place than in the settings.
240+ // Move it to the addedIndex
241+ beginMoveRows(QModelIndex(), itemIndex, itemIndex, QModelIndex(), asIndex);
242+ m_list.move(itemIndex, asIndex);
243+ endMoveRows();
244+ }
245+ }
246+}
247+
248+void LauncherModel::propertiesChanged(const QString &user, const QString &interface, const QStringList &changed)
249+{
250+ if (user != m_user || interface != "com.canonical.unity.AccountsService" || !changed.contains("launcher-items")) {
251+ return;
252+ }
253+ refresh();
254+}