on ae9bc90 help sign in
unity8/plugins/Unity/Launcher/launchermodel.cpp
624 lines 21 kB history raw
1/*
2 * Copyright 2013-2016 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 "launchermodel.h"
18#include "launcheritem.h"
19#include "gsettings.h"
20#include "dbusinterface.h"
21#include "asadapter.h"
22#include "ualwrapper.h"
24#include <unity/shell/application/ApplicationInfoInterface.h>
25#include <unity/shell/application/MirSurfaceListInterface.h>
27#include <QDesktopServices>
28#include <QDebug>
30using namespace unity::shell::application;
32LauncherModel::LauncherModel(QObject *parent):
33 LauncherModelInterface(parent),
34 m_settings(new GSettings(this)),
35 m_dbusIface(new DBusInterface(this)),
36 m_asAdapter(new ASAdapter()),
37 m_appManager(nullptr)
39 connect(m_dbusIface, &DBusInterface::countChanged, this, &LauncherModel::countChanged);
40 connect(m_dbusIface, &DBusInterface::countVisibleChanged, this, &LauncherModel::countVisibleChanged);
41 connect(m_dbusIface, &DBusInterface::progressChanged, this, &LauncherModel::progressChanged);
42 connect(m_dbusIface, &DBusInterface::refreshCalled, this, &LauncherModel::refresh);
43 connect(m_dbusIface, &DBusInterface::alertCalled, this, &LauncherModel::alert);
45 connect(m_settings, &GSettings::changed, this, &LauncherModel::refresh);
47 refresh();
50LauncherModel::~LauncherModel()
52 while (!m_list.empty()) {
53 m_list.takeFirst()->deleteLater();
54 }
56 delete m_asAdapter;
59int LauncherModel::rowCount(const QModelIndex &parent) const
61 Q_UNUSED(parent)
62 return m_list.count();
65QVariant LauncherModel::data(const QModelIndex &index, int role) const
67 LauncherItem *item = m_list.at(index.row());
68 switch(role) {
69 case RoleAppId:
70 return item->appId();
71 case RoleName:
72 return item->name();
73 case RoleIcon:
74 return item->icon();
75 case RolePinned:
76 return item->pinned();
77 case RoleCount:
78 return item->count();
79 case RoleCountVisible:
80 return item->countVisible();
81 case RoleProgress:
82 return item->progress();
83 case RoleFocused:
84 return item->focused();
85 case RoleAlerting:
86 return item->alerting();
87 case RoleRunning:
88 return item->running();
89 case RoleSurfaceCount:
90 return item->surfaceCount();
91 default:
92 qWarning() << Q_FUNC_INFO << "missing role, implement me";
93 return QVariant();
94 }
96 return QVariant();
99unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
101 if (index < 0 || index >= m_list.count()) {
102 return 0;
103 }
104 return m_list.at(index);
107void LauncherModel::move(int oldIndex, int newIndex)
109 // Make sure its not moved outside the lists
110 if (newIndex < 0) {
111 newIndex = 0;
112 }
113 if (newIndex >= m_list.count()) {
114 newIndex = m_list.count()-1;
115 }
117 // Nothing to do?
118 if (oldIndex == newIndex) {
119 return;
120 }
122 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
123 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
124 // adjust the model's index by +1 in case we're moving upwards
125 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
127 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
128 m_list.move(oldIndex, newIndex);
129 endMoveRows();
131 if (!m_list.at(newIndex)->pinned()) {
132 pin(m_list.at(newIndex)->appId());
133 } else {
134 storeAppList();
135 }
138void LauncherModel::pin(const QString &appId, int index)
140 int currentIndex = findApplication(appId);
142 if (currentIndex >= 0) {
143 if (index == -1 || index == currentIndex) {
144 m_list.at(currentIndex)->setPinned(true);
145 QModelIndex modelIndex = this->index(currentIndex);
146 Q_EMIT dataChanged(modelIndex, modelIndex, {RolePinned});
147 } else {
148 move(currentIndex, index);
149 // move() will store the list to the backend itself, so just exit at this point.
150 return;
151 }
152 } else {
153 if (index == -1) {
154 index = m_list.count();
155 }
157 UalWrapper::AppInfo appInfo = UalWrapper::getApplicationInfo(appId);
158 if (!appInfo.valid) {
159 qWarning() << "Can't pin application, appId not found:" << appId;
160 return;
161 }
163 beginInsertRows(QModelIndex(), index, index);
164 LauncherItem *item = new LauncherItem(appId,
165 appInfo.name,
166 appInfo.icon,
167 this);
168 item->setPinned(true);
169 m_list.insert(index, item);
170 endInsertRows();
171 }
173 storeAppList();
176void LauncherModel::requestRemove(const QString &appId)
178 unpin(appId);
179 storeAppList();
182void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
184 const int index = findApplication(appId);
185 if (index < 0) {
186 return;
187 }
189 LauncherItem *item = m_list.at(index);
190 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
191 if (model) {
192 const QString actionId = model->get(actionIndex).actionId();
194 // Check if this is one of the launcher actions we handle ourselves
195 if (actionId == QLatin1String("pin_item")) {
196 if (item->pinned()) {
197 requestRemove(appId);
198 } else {
199 pin(appId);
200 }
201 } else if (actionId == QLatin1String("launch_item")) {
202 QDesktopServices::openUrl(getUrlForAppId(appId));
203 } else if (actionId == QLatin1String("stop_item")) { // Quit
204 if (m_appManager) {
205 m_appManager->stopApplication(appId);
206 }
207 } else if (actionId.startsWith("surface_")){
208 ApplicationInfoInterface *appInfo = m_appManager->findApplication(appId);
209 if (!appInfo) {
210 qWarning() << "App for" << appId << "not found in launcher. Cannot invoke quicklist action";
211 }
212 for (int i = 0; i < appInfo->surfaceList()->count(); ++i) {
213 MirSurfaceInterface *iface = appInfo->surfaceList()->get(i);
214 QString id = actionId;
215 id.remove(QRegExp("^surface_"));
216 if (id == iface->persistentId()) {
217 iface->activate();
218 }
219 }
220 // Nope, we don't know this action, let the backend forward it to the application
221 } else {
222 // TODO: forward quicklist action to app, possibly via m_dbusIface
223 }
224 }
227void LauncherModel::setUser(const QString &username)
229 Q_UNUSED(username)
232QString LauncherModel::getUrlForAppId(const QString &appId) const
234 // appId is either an appId or a legacy app name. Let's find out which
235 if (appId.isEmpty()) {
236 return QString();
237 }
239 if (!appId.contains('_')) {
240 return "application:///" + appId + ".desktop";
241 }
243 QStringList parts = appId.split('_');
244 QString package = parts.value(0);
245 QString app = parts.value(1, QStringLiteral("first-listed-app"));
246 return "appid://" + package + "/" + app + "/current-user-version";
249ApplicationManagerInterface *LauncherModel::applicationManager() const
251 return m_appManager;
254void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
256 // Is there already another appmanager set?
257 if (m_appManager) {
258 // Disconnect any signals
259 disconnect(this, &LauncherModel::applicationAdded, 0, nullptr);
260 disconnect(this, &LauncherModel::applicationRemoved, 0, nullptr);
261 disconnect(this, &LauncherModel::focusedAppIdChanged, 0, nullptr);
263 // remove any recent/running apps from the launcher
264 QList<int> recentAppIndices;
265 for (int i = 0; i < m_list.count(); ++i) {
266 if (m_list.at(i)->recent()) {
267 recentAppIndices << i;
268 }
269 }
270 int run = 0;
271 while (recentAppIndices.count() > 0) {
272 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
273 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
274 endRemoveRows();
275 recentAppIndices.takeFirst();
276 ++run;
277 }
278 }
280 m_appManager = appManager;
281 connect(m_appManager, &ApplicationManagerInterface::rowsInserted, this, &LauncherModel::applicationAdded);
282 connect(m_appManager, &ApplicationManagerInterface::rowsAboutToBeRemoved, this, &LauncherModel::applicationRemoved);
283 connect(m_appManager, &ApplicationManagerInterface::focusedApplicationIdChanged, this, &LauncherModel::focusedAppIdChanged);
285 Q_EMIT applicationManagerChanged();
287 for (int i = 0; i < appManager->count(); ++i) {
288 applicationAdded(QModelIndex(), i);
289 }
292bool LauncherModel::onlyPinned() const
294 return false;
297void LauncherModel::setOnlyPinned(bool onlyPinned) {
298 Q_UNUSED(onlyPinned);
299 qWarning() << "This launcher implementation does not support showing only pinned apps";
302void LauncherModel::storeAppList()
304 QStringList appIds;
305 Q_FOREACH(LauncherItem *item, m_list) {
306 if (item->pinned()) {
307 appIds << item->appId();
308 }
309 }
310 m_settings->setStoredApplications(appIds);
311 m_asAdapter->syncItems(m_list);
314void LauncherModel::unpin(const QString &appId)
316 const int index = findApplication(appId);
317 if (index < 0) {
318 return;
319 }
321 if (m_appManager->findApplication(appId)) {
322 if (m_list.at(index)->pinned()) {
323 m_list.at(index)->setPinned(false);
324 QModelIndex modelIndex = this->index(index);
325 Q_EMIT dataChanged(modelIndex, modelIndex, {RolePinned});
326 }
327 } else {
328 beginRemoveRows(QModelIndex(), index, index);
329 m_list.takeAt(index)->deleteLater();
330 endRemoveRows();
331 }
334int LauncherModel::findApplication(const QString &appId)
336 for (int i = 0; i < m_list.count(); ++i) {
337 LauncherItem *item = m_list.at(i);
338 if (item->appId() == appId) {
339 return i;
340 }
341 }
342 return -1;
345void LauncherModel::progressChanged(const QString &appId, int progress)
347 const int idx = findApplication(appId);
348 if (idx >= 0) {
349 LauncherItem *item = m_list.at(idx);
350 item->setProgress(progress);
351 Q_EMIT dataChanged(index(idx), index(idx), {RoleProgress});
352 }
355void LauncherModel::countChanged(const QString &appId, int count)
357 const int idx = findApplication(appId);
358 if (idx >= 0) {
359 LauncherItem *item = m_list.at(idx);
360 item->setCount(count);
361 QVector<int> changedRoles = {RoleCount};
362 if (item->countVisible() && !item->alerting() && !item->focused()) {
363 changedRoles << RoleAlerting;
364 item->setAlerting(true);
365 }
366 m_asAdapter->syncItems(m_list);
367 Q_EMIT dataChanged(index(idx), index(idx), changedRoles);
368 }
371void LauncherModel::countVisibleChanged(const QString &appId, bool countVisible)
373 int idx = findApplication(appId);
374 if (idx >= 0) {
375 LauncherItem *item = m_list.at(idx);
376 item->setCountVisible(countVisible);
377 QVector<int> changedRoles = {RoleCountVisible};
378 if (countVisible && !item->alerting() && !item->focused()) {
379 changedRoles << RoleAlerting;
380 item->setAlerting(true);
381 }
382 Q_EMIT dataChanged(index(idx), index(idx), changedRoles);
384 // If countVisible goes to false, and the item is neither pinned nor recent we can drop it
385 if (!countVisible && !item->pinned() && !item->recent()) {
386 beginRemoveRows(QModelIndex(), idx, idx);
387 m_list.takeAt(idx)->deleteLater();
388 endRemoveRows();
389 }
390 } else {
391 // Need to create a new LauncherItem and show the highlight
392 UalWrapper::AppInfo appInfo = UalWrapper::getApplicationInfo(appId);
393 if (countVisible && appInfo.valid) {
394 LauncherItem *item = new LauncherItem(appId,
395 appInfo.name,
396 appInfo.icon,
397 this);
398 item->setCountVisible(true);
399 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
400 m_list.append(item);
401 endInsertRows();
402 }
403 }
404 m_asAdapter->syncItems(m_list);
407void LauncherModel::refresh()
409 // First walk through all the existing items and see if we need to remove something
410 QList<LauncherItem*> toBeRemoved;
411 Q_FOREACH (LauncherItem* item, m_list) {
412 UalWrapper::AppInfo appInfo = UalWrapper::getApplicationInfo(item->appId());
413 if (!appInfo.valid) {
414 // Application no longer available => drop it!
415 toBeRemoved << item;
416 } else if (!m_settings->storedApplications().contains(item->appId())) {
417 // Item not in settings any more => drop it!
418 toBeRemoved << item;
419 } else {
420 int idx = m_list.indexOf(item);
421 item->setName(appInfo.name);
422 item->setPinned(item->pinned()); // update pinned text if needed
423 item->setRunning(item->running());
424 Q_EMIT dataChanged(index(idx), index(idx), {RoleName, RoleRunning});
426 const QString oldIcon = item->icon();
427 if (oldIcon == appInfo.icon) { // same icon file, perhaps different contents, simulate changing the icon name to force reload
428 item->setIcon(QString());
429 Q_EMIT dataChanged(index(idx), index(idx), {RoleIcon});
430 }
432 // now set the icon for real
433 item->setIcon(appInfo.icon);
434 Q_EMIT dataChanged(index(idx), index(idx), {RoleIcon});
435 }
436 }
438 Q_FOREACH (LauncherItem* item, toBeRemoved) {
439 unpin(item->appId());
440 }
442 bool changed = toBeRemoved.count() > 0;
444 // This brings the Launcher into sync with the settings backend again. There's an issue though:
445 // If we can't find a .desktop file for an entry we need to skip it. That makes our settingsIndex
446 // go out of sync with the actual index of items. So let's also use an addedIndex which reflects
447 // the settingsIndex minus the skipped items.
448 int addedIndex = 0;
450 // Now walk through settings and see if we need to add something
451 for (int settingsIndex = 0; settingsIndex < m_settings->storedApplications().count(); ++settingsIndex) {
452 const QString entry = m_settings->storedApplications().at(settingsIndex);
453 int itemIndex = -1;
454 for (int i = 0; i < m_list.count(); ++i) {
455 if (m_list.at(i)->appId() == entry) {
456 itemIndex = i;
457 break;
458 }
459 }
461 if (itemIndex == -1) {
462 // Need to add it. Just add it into the addedIndex to keep same ordering as the list
463 // in the settings.
464 UalWrapper::AppInfo appInfo = UalWrapper::getApplicationInfo(entry);
465 if (!appInfo.valid) {
466 continue;
467 }
469 LauncherItem *item = new LauncherItem(entry,
470 appInfo.name,
471 appInfo.icon,
472 this);
473 item->setPinned(true);
474 beginInsertRows(QModelIndex(), addedIndex, addedIndex);
475 m_list.insert(addedIndex, item);
476 endInsertRows();
477 changed = true;
478 } else if (itemIndex != addedIndex) {
479 // The item is already there, but it is in a different place than in the settings.
480 // Move it to the addedIndex
481 beginMoveRows(QModelIndex(), itemIndex, itemIndex, QModelIndex(), addedIndex);
482 m_list.move(itemIndex, addedIndex);
483 endMoveRows();
484 changed = true;
485 }
487 // Just like settingsIndex, this will increase with every item, except the ones we
488 // skipped with the "continue" call above.
489 addedIndex++;
490 }
492 if (changed) {
493 Q_EMIT hint();
494 }
496 m_asAdapter->syncItems(m_list);
499void LauncherModel::alert(const QString &appId)
501 int idx = findApplication(appId);
502 if (idx >= 0) {
503 LauncherItem *item = m_list.at(idx);
504 if (!item->focused() && !item->alerting()) {
505 item->setAlerting(true);
506 Q_EMIT dataChanged(index(idx), index(idx), {RoleAlerting});
507 }
508 }
511void LauncherModel::applicationAdded(const QModelIndex &parent, int row)
513 Q_UNUSED(parent);
515 ApplicationInfoInterface *app = m_appManager->get(row);
516 if (!app) {
517 qWarning() << "LauncherModel received an applicationAdded signal, but there's no such application!";
518 return;
519 }
521 if (app->appId() == QLatin1String("unity8-dash")) {
522 // Not adding the dash app
523 return;
524 }
526 const int itemIndex = findApplication(app->appId());
527 if (itemIndex != -1) {
528 LauncherItem *item = m_list.at(itemIndex);
529 if (!item->recent()) {
530 item->setRecent(true);
531 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), {RoleRecent});
532 }
533 item->setRunning(true);
534 } else {
535 LauncherItem *item = new LauncherItem(app->appId(), app->name(), app->icon().toString(), this);
536 item->setRecent(true);
537 item->setRunning(true);
538 item->setFocused(app->focused());
539 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
540 m_list.append(item);
541 endInsertRows();
542 }
543 connect(app, &ApplicationInfoInterface::surfaceCountChanged, this, &LauncherModel::applicationSurfaceCountChanged);
544 m_asAdapter->syncItems(m_list);
545 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), {RoleRunning});
548void LauncherModel::applicationSurfaceCountChanged(int count)
550 Q_UNUSED(count)
551 ApplicationInfoInterface *app = static_cast<ApplicationInfoInterface*>(sender());
552 int idx = findApplication(app->appId());
553 if (idx < 0) {
554 qWarning() << "Received a surface count changed event from an app that's not in the Launcher model";
555 return;
556 }
557 LauncherItem *item = m_list.at(idx);
558 QList<QPair<QString, QString> > surfaces;
559 for (int i = 0; i < app->surfaceList()->count(); ++i) {
560 MirSurfaceInterface* iface = app->surfaceList()->get(i);
561 surfaces.append(qMakePair<QString, QString>(iface->persistentId(), iface->name()));
562 }
563 item->setSurfaces(surfaces);
564 Q_EMIT dataChanged(index(idx), index(idx), {RoleSurfaceCount});
567void LauncherModel::applicationRemoved(const QModelIndex &parent, int row)
569 Q_UNUSED(parent)
571 ApplicationInfoInterface *app = m_appManager->get(row);
572 int appIndex = -1;
573 for (int i = 0; i < m_list.count(); ++i) {
574 if (m_list.at(i)->appId() == app->appId()) {
575 appIndex = i;
576 break;
577 }
578 }
580 if (appIndex < 0) {
581 qWarning() << Q_FUNC_INFO << "appIndex not found";
582 return;
583 }
585 disconnect(app, &ApplicationInfoInterface::surfaceCountChanged, this, &LauncherModel::applicationSurfaceCountChanged);
587 LauncherItem * item = m_list.at(appIndex);
589 if (!item->pinned()) {
590 beginRemoveRows(QModelIndex(), appIndex, appIndex);
591 m_list.takeAt(appIndex)->deleteLater();
592 endRemoveRows();
593 m_asAdapter->syncItems(m_list);
594 } else {
595 QVector<int> changedRoles = {RoleRunning};
596 item->setRunning(false);
597 if (item->focused()) {
598 changedRoles << RoleFocused;
599 item->setFocused(false);
600 }
601 Q_EMIT dataChanged(index(appIndex), index(appIndex), changedRoles);
602 }
605void LauncherModel::focusedAppIdChanged()
607 const QString appId = m_appManager->focusedApplicationId();
608 for (int i = 0; i < m_list.count(); ++i) {
609 LauncherItem *item = m_list.at(i);
610 if (!item->focused() && item->appId() == appId) {
611 QVector<int> changedRoles;
612 changedRoles << RoleFocused;
613 item->setFocused(true);
614 if (item->alerting()) {
615 changedRoles << RoleAlerting;
616 item->setAlerting(false);
617 }
618 Q_EMIT dataChanged(index(i), index(i), changedRoles);
619 } else if (item->focused() && item->appId() != appId) {
620 item->setFocused(false);
621 Q_EMIT dataChanged(index(i), index(i), {RoleFocused});
622 }
623 }