on 96870d5 help sign in
unity8/plugins/Greeter/Unity/Launcher/launchermodelas.cpp
280 lines 8.9 kB history raw
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 */
20#include "launchermodelas.h"
21#include "launcheritem.h"
22#include "AccountsServiceDBusAdaptor.h"
23#include <unity/shell/application/ApplicationInfoInterface.h>
25#include <QDesktopServices>
26#include <QDebug>
27#include <QDBusArgument>
29using namespace unity::shell::application;
31LauncherModel::LauncherModel(QObject *parent):
32 LauncherModelInterface(parent),
33 m_accounts(new AccountsServiceDBusAdaptor(this)),
34 m_onlyPinned(true)
36 connect(m_accounts, &AccountsServiceDBusAdaptor::propertiesChanged, this, &LauncherModel::propertiesChanged);
37 refresh();
40LauncherModel::~LauncherModel()
42 while (!m_list.empty()) {
43 m_list.takeFirst()->deleteLater();
44 }
47int LauncherModel::rowCount(const QModelIndex &parent) const
49 Q_UNUSED(parent)
50 return m_list.count();
53QVariant LauncherModel::data(const QModelIndex &index, int role) const
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 }
75 return QVariant();
78unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
80 if (index < 0 || index >= m_list.count()) {
81 return 0;
82 }
83 return m_list.at(index);
86void LauncherModel::move(int oldIndex, int newIndex)
88 Q_UNUSED(oldIndex)
89 Q_UNUSED(newIndex)
90 qWarning() << "This is a read only implementation. Cannot move items.";
93void LauncherModel::pin(const QString &appId, int index)
95 Q_UNUSED(appId)
96 Q_UNUSED(index)
97 qWarning() << "This is a read only implementation. Cannot pin items";
100void LauncherModel::requestRemove(const QString &appId)
102 Q_UNUSED(appId)
103 qWarning() << "This is a read only implementation. Cannot remove items";
106void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
108 int index = findApplication(appId);
109 if (index < 0) {
110 return;
111 }
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();
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
123 } else {
124 // TODO: forward quicklist action to app, possibly via m_dbusIface
125 }
126 }
129void LauncherModel::setUser(const QString &username)
131 if (m_user != username) {
132 m_user = username;
133 refresh();
134 }
137QString 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()) {
141 return QString();
142 }
144 if (!appId.contains("_")) {
145 return "application:///" + appId + ".desktop";
146 }
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";
154ApplicationManagerInterface *LauncherModel::applicationManager() const
156 return nullptr;
159void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
161 Q_UNUSED(appManager)
162 qWarning() << "This plugin syncs all its states from AccountsService. Not using ApplicationManager.";
163 return;
166bool LauncherModel::onlyPinned() const
168 return m_onlyPinned;
171void LauncherModel::setOnlyPinned(bool onlyPinned)
173 if (m_onlyPinned != onlyPinned) {
174 m_onlyPinned = onlyPinned;
175 Q_EMIT onlyPinnedChanged();
176 refresh();
177 }
180int LauncherModel::findApplication(const QString &appId)
182 for (int i = 0; i < m_list.count(); ++i) {
183 LauncherItem *item = m_list.at(i);
184 if (item->appId() == appId) {
185 return i;
186 }
187 }
188 return -1;
191typedef QList<QVariantMap> VariantMapList;
192void LauncherModel::refresh()
194 QList<QVariantMap> items;
196 if (m_accounts && !m_user.isEmpty()) {
197 items = m_accounts->getUserProperty<VariantMapList>(m_user, "com.canonical.unity.AccountsService", "LauncherItems");
198 }
200 // First walk through all the existing items and see if we need to remove something
201 QList<LauncherItem*> toBeRemoved;
203 Q_FOREACH (LauncherItem* item, m_list) {
204 bool found = false;
205 Q_FOREACH(const QVariant &asItem, items) {
206 if (asItem.toMap().value("id").toString() == item->appId()) {
207 // Only keep and update it if we either show unpinned or it is pinned
208 if (!m_onlyPinned || asItem.toMap().value("pinned").toBool()) {
209 found = true;
210 item->setName(asItem.toMap().value("name").toString());
211 item->setIcon(asItem.toMap().value("icon").toString());
212 item->setCount(asItem.toMap().value("count").toInt());
213 item->setCountVisible(asItem.toMap().value("countVisible").toBool());
214 int idx = m_list.indexOf(item);
215 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleName << RoleIcon);
216 }
217 break;
218 }
219 }
220 if (!found) {
221 toBeRemoved.append(item);
222 }
223 }
225 Q_FOREACH (LauncherItem* item, toBeRemoved) {
226 int idx = m_list.indexOf(item);
227 beginRemoveRows(QModelIndex(), idx, idx);
228 m_list.takeAt(idx)->deleteLater();
229 endRemoveRows();
230 }
232 // Now walk through list and see if we need to add something
233 int skipped = 0;
234 for (int asIndex = 0; asIndex < items.count(); ++asIndex) {
235 QVariant entry = items.at(asIndex);
237 if (m_onlyPinned && !entry.toMap().value("pinned").toBool()) {
238 // Skipping it as we only show pinned and it is not
239 skipped++;
240 continue;
241 }
242 int newPosition = asIndex - skipped;
244 int itemIndex = -1;
245 for (int i = 0; i < m_list.count(); ++i) {
246 if (m_list.at(i)->appId() == entry.toMap().value("id").toString()) {
247 itemIndex = i;
248 break;
249 }
250 }
252 if (itemIndex == -1) {
253 // Need to add it. Just add it into the addedIndex to keep same ordering as the list in AS.
254 LauncherItem *item = new LauncherItem(entry.toMap().value("id").toString(),
255 entry.toMap().value("name").toString(),
256 entry.toMap().value("icon").toString(),
257 this);
258 item->setPinned(true);
259 item->setCount(entry.toMap().value("count").toInt());
260 item->setCountVisible(entry.toMap().value("countVisible").toBool());
261 beginInsertRows(QModelIndex(), newPosition, newPosition);
262 m_list.insert(newPosition, item);
263 endInsertRows();
264 } else if (itemIndex != newPosition) {
265 // The item is already there, but it is in a different place than in the settings.
266 // Move it to the addedIndex
267 beginMoveRows(QModelIndex(), itemIndex, itemIndex, QModelIndex(), newPosition);
268 m_list.move(itemIndex, newPosition);
269 endMoveRows();
270 }
271 }
274void LauncherModel::propertiesChanged(const QString &user, const QString &interface, const QStringList &changed)
276 if (user != m_user || interface != "com.canonical.unity.AccountsService" || !changed.contains("LauncherItems")) {
277 return;
278 }
279 refresh();