on 8caafb4 help sign in
unity8/plugins/Greeter/Unity/Launcher/launchermodelas.cpp
310 lines 11 kB history raw
1/*
2 * Copyright 2014-2015 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 */
17#include "launchermodelas.h"
18#include "launcheritem.h"
19#include "AccountsServiceDBusAdaptor.h"
20#include <unity/shell/application/ApplicationInfoInterface.h>
22#include <QDesktopServices>
23#include <QDebug>
24#include <QDBusArgument>
26using namespace unity::shell::application;
28LauncherModel::LauncherModel(QObject *parent):
29 LauncherModelInterface(parent),
30 m_accounts(new AccountsServiceDBusAdaptor(this)),
31 m_onlyPinned(true)
33 connect(m_accounts, &AccountsServiceDBusAdaptor::propertiesChanged, this, &LauncherModel::propertiesChanged);
34 refresh();
37LauncherModel::~LauncherModel()
39 while (!m_list.empty()) {
40 m_list.takeFirst()->deleteLater();
41 }
44int LauncherModel::rowCount(const QModelIndex &parent) const
46 Q_UNUSED(parent)
47 return m_list.count();
50QVariant LauncherModel::data(const QModelIndex &index, int role) const
52 LauncherItem *item = m_list.at(index.row());
53 switch(role) {
54 case RoleAppId:
55 return item->appId();
56 case RoleName:
57 return item->name();
58 case RoleIcon:
59 return item->icon();
60 case RolePinned:
61 return item->pinned();
62 case RoleCount:
63 return item->count();
64 case RoleCountVisible:
65 return item->countVisible();
66 case RoleProgress:
67 return item->progress();
68 case RoleFocused:
69 return item->focused();
70 case RoleRunning:
71 return item->running();
72 case RoleSurfaceCount:
73 return item->surfaceCount();
74 }
76 return QVariant();
79void LauncherModel::setAlerting(const QString &appId, bool alerting)
81 Q_UNUSED(appId)
82 Q_UNUSED(alerting)
83 qWarning() << "This is a read only implementation. Cannot set alert-state of items.";
86unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
88 if (index < 0 || index >= m_list.count()) {
89 return 0;
90 }
91 return m_list.at(index);
94void LauncherModel::move(int oldIndex, int newIndex)
96 Q_UNUSED(oldIndex)
97 Q_UNUSED(newIndex)
98 qWarning() << "This is a read only implementation. Cannot move items.";
101void LauncherModel::pin(const QString &appId, int index)
103 Q_UNUSED(appId)
104 Q_UNUSED(index)
105 qWarning() << "This is a read only implementation. Cannot pin items";
108void LauncherModel::requestRemove(const QString &appId)
110 Q_UNUSED(appId)
111 qWarning() << "This is a read only implementation. Cannot remove items";
114void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
116 int index = findApplication(appId);
117 if (index < 0) {
118 return;
119 }
121 LauncherItem *item = m_list.at(index);
122 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
123 if (model) {
124 QString actionId = model->get(actionIndex).actionId();
126 // Check if this is one of the launcher actions we handle ourselves
127 if (actionId == QLatin1String("launch_item")) {
128 QDesktopServices::openUrl(getUrlForAppId(appId));
130 // Nope, we don't know this action, let the backend forward it to the application
131 } else {
132 // TODO: forward quicklist action to app, possibly via m_dbusIface
133 }
134 }
137void LauncherModel::setUser(const QString &username)
139 if (m_user != username) {
140 m_user = username;
141 refresh();
142 }
145QString LauncherModel::getUrlForAppId(const QString &appId) const
147 // appId is either an appId or a legacy app name. Let's find out which
148 if (appId.isEmpty()) {
149 return QString();
150 }
152 if (!appId.contains('_')) {
153 return "application:///" + appId + ".desktop";
154 }
156 QStringList parts = appId.split('_');
157 QString package = parts.value(0);
158 QString app = parts.value(1, QStringLiteral("first-listed-app"));
159 return "appid://" + package + "/" + app + "/current-user-version";
162ApplicationManagerInterface *LauncherModel::applicationManager() const
164 return nullptr;
167void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
169 Q_UNUSED(appManager)
170 qWarning() << "This plugin syncs all its states from AccountsService. Not using ApplicationManager.";
171 return;
174bool LauncherModel::onlyPinned() const
176 return m_onlyPinned;
179void LauncherModel::setOnlyPinned(bool onlyPinned)
181 if (m_onlyPinned != onlyPinned) {
182 m_onlyPinned = onlyPinned;
183 Q_EMIT onlyPinnedChanged();
184 refresh();
185 }
188int LauncherModel::findApplication(const QString &appId)
190 for (int i = 0; i < m_list.count(); ++i) {
191 LauncherItem *item = m_list.at(i);
192 if (item->appId() == appId) {
193 return i;
194 }
195 }
196 return -1;
199void LauncherModel::refresh()
201 if (!m_accounts || m_user.isEmpty()) {
202 refreshWithItems(QList<QVariantMap>());
203 } else {
204 QDBusPendingCall pendingCall = m_accounts->getUserPropertyAsync(m_user, QStringLiteral("com.canonical.unity.AccountsService"), QStringLiteral("LauncherItems"));
205 QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingCall, this);
206 connect(watcher, &QDBusPendingCallWatcher::finished,
207 this, [this](QDBusPendingCallWatcher* watcher) {
209 QDBusPendingReply<QVariant> reply = *watcher;
210 watcher->deleteLater();
211 if (reply.isError()) {
212 qWarning() << "Failed to refresh LauncherItems" << reply.error().message();
213 return;
214 }
216 refreshWithItems(qdbus_cast<QList<QVariantMap>>(reply.value().value<QDBusArgument>()));
217 });
218 }
221void LauncherModel::refreshWithItems(const QList<QVariantMap> &items)
223 // First walk through all the existing items and see if we need to remove something
224 QList<LauncherItem*> toBeRemoved;
226 Q_FOREACH (LauncherItem* item, m_list) {
227 bool found = false;
228 Q_FOREACH(const QVariant &asItem, items) {
229 QVariantMap cachedMap = asItem.toMap();
230 if (cachedMap.value(QStringLiteral("id")).toString() == item->appId()) {
231 // Only keep and update it if we either show unpinned or it is pinned
232 if (!m_onlyPinned || cachedMap.value(QStringLiteral("pinned")).toBool()) {
233 found = true;
234 item->setName(cachedMap.value(QStringLiteral("name")).toString());
235 item->setIcon(cachedMap.value(QStringLiteral("icon")).toString());
236 item->setCount(cachedMap.value(QStringLiteral("count")).toInt());
237 item->setCountVisible(cachedMap.value(QStringLiteral("countVisible")).toBool());
238 item->setProgress(cachedMap.value(QStringLiteral("progress")).toInt());
239 item->setRunning(cachedMap.value(QStringLiteral("running")).toBool());
241 int idx = m_list.indexOf(item);
242 Q_EMIT dataChanged(index(idx), index(idx), {RoleName, RoleIcon, RoleCount, RoleCountVisible, RoleRunning, RoleProgress});
243 }
244 break;
245 }
246 }
247 if (!found) {
248 toBeRemoved.append(item);
249 }
250 }
252 Q_FOREACH (LauncherItem* item, toBeRemoved) {
253 int idx = m_list.indexOf(item);
254 beginRemoveRows(QModelIndex(), idx, idx);
255 m_list.takeAt(idx)->deleteLater();
256 endRemoveRows();
257 }
259 // Now walk through list and see if we need to add something
260 int skipped = 0;
261 for (int asIndex = 0; asIndex < items.count(); ++asIndex) {
262 QVariant entry = items.at(asIndex);
264 if (m_onlyPinned && !entry.toMap().value(QStringLiteral("pinned")).toBool()) {
265 // Skipping it as we only show pinned and it is not
266 skipped++;
267 continue;
268 }
269 int newPosition = asIndex - skipped;
271 int itemIndex = -1;
272 for (int i = 0; i < m_list.count(); ++i) {
273 if (m_list.at(i)->appId() == entry.toMap().value(QStringLiteral("id")).toString()) {
274 itemIndex = i;
275 break;
276 }
277 }
279 if (itemIndex == -1) {
280 QVariantMap cachedMap = entry.toMap();
281 // Need to add it. Just add it into the addedIndex to keep same ordering as the list in AS.
282 LauncherItem *item = new LauncherItem(cachedMap.value(QStringLiteral("id")).toString(),
283 cachedMap.value(QStringLiteral("name")).toString(),
284 cachedMap.value(QStringLiteral("icon")).toString(),
285 this);
286 item->setPinned(true);
287 item->setCount(cachedMap.value(QStringLiteral("count")).toInt());
288 item->setCountVisible(cachedMap.value(QStringLiteral("countVisible")).toBool());
289 item->setProgress(cachedMap.value(QStringLiteral("progress")).toInt());
290 item->setRunning(cachedMap.value(QStringLiteral("running")).toBool());
291 beginInsertRows(QModelIndex(), newPosition, newPosition);
292 m_list.insert(newPosition, item);
293 endInsertRows();
294 } else if (itemIndex != newPosition) {
295 // The item is already there, but it is in a different place than in the settings.
296 // Move it to the addedIndex
297 beginMoveRows(QModelIndex(), itemIndex, itemIndex, QModelIndex(), newPosition);
298 m_list.move(itemIndex, newPosition);
299 endMoveRows();
300 }
301 }
304void LauncherModel::propertiesChanged(const QString &user, const QString &interface, const QStringList &changed)
306 if (user != m_user || interface != QLatin1String("com.canonical.unity.AccountsService") || !changed.contains(QStringLiteral("LauncherItems"))) {
307 return;
308 }
309 refresh();