@@ -0,0 +1,254 @@
2+ * Copyright 2013-2014 Canonical Ltd.
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.
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.
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/>.
17+ * Michael Zanetti <michael.zanetti@canonical.com>
20+#include "launchermodelas.h"
21+#include "launcheritem.h"
22+#include "AccountsServiceDBusAdaptor.h"
23+#include <unity/shell/application/ApplicationInfoInterface.h>
25+#include <QDesktopServices>
27+#include <QDBusArgument>
29+using namespace unity::shell::application;
31+LauncherModel::LauncherModel(QObject *parent):
32+ LauncherModelInterface(parent),
33+ m_accounts(new AccountsServiceDBusAdaptor(this))
35+ qDebug() << "Loading AS based launcher model";
36+ connect(m_accounts, &AccountsServiceDBusAdaptor::propertiesChanged, this, &LauncherModel::propertiesChanged);
40+LauncherModel::~LauncherModel()
42+ while (!m_list.empty()) {
43+ m_list.takeFirst()->deleteLater();
47+int LauncherModel::rowCount(const QModelIndex &parent) const
50+ return m_list.count();
53+QVariant LauncherModel::data(const QModelIndex &index, int role) const
55+ LauncherItem *item = m_list.at(index.row());
58+ return item->appId();
64+ return item->pinned();
66+ return item->count();
67+ case RoleCountVisible:
68+ return item->countVisible();
70+ return item->progress();
72+ return item->focused();
78+unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
80+ if (index < 0 || index >= m_list.count()) {
83+ return m_list.at(index);
86+void LauncherModel::move(int oldIndex, int newIndex)
90+ qWarning() << "This is a read only implementation. Cannot move items.";
93+void LauncherModel::pin(const QString &appId, int index)
97+ qWarning() << "This is a read only implementation. Cannot pin items";
100+void LauncherModel::requestRemove(const QString &appId)
103+ qWarning() << "This is a read only implementation. Cannot remove items";
106+void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
108+ int index = findApplication(appId);
113+ LauncherItem *item = m_list.at(index);
114+ QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
116+ QString actionId = model->get(actionIndex).actionId();
118+ // Check if this is one of the launcher actions we handle ourselves
119+ if (actionId == "launch_item") {
120+ QDesktopServices::openUrl(getUrlForAppId(appId));
122+ // Nope, we don't know this action, let the backend forward it to the application
124+ // TODO: forward quicklist action to app, possibly via m_dbusIface
129+void LauncherModel::setUser(const QString &username)
131+ if (m_user != username) {
137+QString LauncherModel::getUrlForAppId(const QString &appId) const
139+ // appId is either an appId or a legacy app name. Let's find out which
140+ if (appId.isEmpty()) {
144+ if (!appId.contains("_")) {
145+ return "application:///" + appId + ".desktop";
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";
154+ApplicationManagerInterface *LauncherModel::applicationManager() const
159+void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
161+ Q_UNUSED(appManager)
162+ qWarning() << "This plugin syncs all its states from AccountsService. Not using ApplicationManager.";
166+int LauncherModel::findApplication(const QString &appId)
168+ for (int i = 0; i < m_list.count(); ++i) {
169+ LauncherItem *item = m_list.at(i);
170+ if (item->appId() == appId) {
177+typedef QList<QVariantMap> VariantMapList;
178+void LauncherModel::refresh()
180+ QList<QVariantMap> items;
182+ if (m_accounts && !m_user.isEmpty()) {
183+ items = m_accounts->getUserProperty<VariantMapList>(m_user, "com.canonical.unity.AccountsService", "launcher-items");
186+ // First walk through all the existing items and see if we need to remove something
187+ QList<LauncherItem*> toBeRemoved;
189+ Q_FOREACH (LauncherItem* item, m_list) {
191+ Q_FOREACH(const QVariant &asItem, items) {
192+ if (asItem.toMap().value("id").toString() == item->appId()) {
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);
203+ toBeRemoved.append(item);
207+ Q_FOREACH (LauncherItem* item, toBeRemoved) {
208+ int idx = m_list.indexOf(item);
209+ beginRemoveRows(QModelIndex(), idx, idx);
210+ m_list.takeAt(idx)->deleteLater();
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();
219+ for (int i = 0; i < m_list.count(); ++i) {
220+ if (m_list.at(i)->appId() == entry.toMap().value("id").toString()) {
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(),
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);
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);
248+void LauncherModel::propertiesChanged(const QString &user, const QString &interface, const QStringList &changed)
250+ if (user != m_user || interface != "com.canonical.unity.AccountsService" || !changed.contains("launcher-items")) {