on 8401926 help sign in
unity8/plugins/Unity/Launcher/launchermodel.cpp
646 lines 22 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>
26#include <unity/shell/application/MirSurfaceInterface.h>
28#include <QDesktopServices>
29#include <QDebug>
31using namespace unity::shell::application;
33LauncherModel::LauncherModel(QObject *parent):
34 LauncherModelInterface(parent),
35 m_settings(new GSettings(this)),
36 m_dbusIface(new DBusInterface(this)),
37 m_asAdapter(new ASAdapter()),
38 m_appManager(nullptr)
40 connect(m_dbusIface, &DBusInterface::countChanged, this, &LauncherModel::countChanged);
41 connect(m_dbusIface, &DBusInterface::countVisibleChanged, this, &LauncherModel::countVisibleChanged);
42 connect(m_dbusIface, &DBusInterface::progressChanged, this, &LauncherModel::progressChanged);
43 connect(m_dbusIface, &DBusInterface::refreshCalled, this, &LauncherModel::refresh);
44 connect(m_dbusIface, &DBusInterface::alertCalled, this, &LauncherModel::alert);
46 connect(m_settings, &GSettings::changed, this, &LauncherModel::refresh);
48 refresh();
51LauncherModel::~LauncherModel()
53 while (!m_list.empty()) {
54 m_list.takeFirst()->deleteLater();
55 }
57 delete m_asAdapter;
60int LauncherModel::rowCount(const QModelIndex &parent) const
62 Q_UNUSED(parent)
63 return m_list.count();
66QVariant LauncherModel::data(const QModelIndex &index, int role) const
68 LauncherItem *item = m_list.at(index.row());
69 switch(role) {
70 case RoleAppId:
71 return item->appId();
72 case RoleName:
73 return item->name();
74 case RoleIcon:
75 return item->icon();
76 case RolePinned:
77 return item->pinned();
78 case RoleCount:
79 return item->count();
80 case RoleCountVisible:
81 return item->countVisible();
82 case RoleProgress:
83 return item->progress();
84 case RoleFocused:
85 return item->focused();
86 case RoleAlerting:
87 return item->alerting();
88 case RoleRunning:
89 return item->running();
90 case RoleSurfaceCount:
91 return item->surfaceCount();
92 default:
93 qWarning() << Q_FUNC_INFO << "missing role, implement me";
94 return QVariant();
95 }
97 return QVariant();
100unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
102 if (index < 0 || index >= m_list.count()) {
103 return 0;
104 }
105 return m_list.at(index);
108void LauncherModel::move(int oldIndex, int newIndex)
110 // Make sure its not moved outside the lists
111 if (newIndex < 0) {
112 newIndex = 0;
113 }
114 if (newIndex >= m_list.count()) {
115 newIndex = m_list.count()-1;
116 }
118 // Nothing to do?
119 if (oldIndex == newIndex) {
120 return;
121 }
123 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
124 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
125 // adjust the model's index by +1 in case we're moving upwards
126 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
128 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
129 m_list.move(oldIndex, newIndex);
130 endMoveRows();
132 if (!m_list.at(newIndex)->pinned()) {
133 pin(m_list.at(newIndex)->appId());
134 } else {
135 storeAppList();
136 }
139void LauncherModel::pin(const QString &appId, int index)
141 int currentIndex = findApplication(appId);
143 if (currentIndex >= 0) {
144 if (index == -1 || index == currentIndex) {
145 m_list.at(currentIndex)->setPinned(true);
146 QModelIndex modelIndex = this->index(currentIndex);
147 Q_EMIT dataChanged(modelIndex, modelIndex, {RolePinned});
148 } else {
149 move(currentIndex, index);
150 // move() will store the list to the backend itself, so just exit at this point.
151 return;
152 }
153 } else {
154 if (index == -1) {
155 index = m_list.count();
156 }
158 UalWrapper::AppInfo appInfo = UalWrapper::getApplicationInfo(appId);
159 if (!appInfo.valid) {
160 qWarning() << "Can't pin application, appId not found:" << appId;
161 return;
162 }
164 beginInsertRows(QModelIndex(), index, index);
165 LauncherItem *item = new LauncherItem(appId,
166 appInfo.name,
167 appInfo.icon,
168 this);
169 item->setPinned(true);
170 m_list.insert(index, item);
171 endInsertRows();
172 }
174 storeAppList();
177void LauncherModel::requestRemove(const QString &appId)
179 unpin(appId);
180 storeAppList();
183void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
185 const int index = findApplication(appId);
186 if (index < 0) {
187 return;
188 }
190 LauncherItem *item = m_list.at(index);
191 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
192 if (model) {
193 const QString actionId = model->get(actionIndex).actionId();
195 // Check if this is one of the launcher actions we handle ourselves
196 if (actionId == QLatin1String("pin_item")) {
197 if (item->pinned()) {
198 requestRemove(appId);
199 } else {
200 pin(appId);
201 }
202 } else if (actionId == QStringLiteral("launch_item")) {
203 QDesktopServices::openUrl(getUrlForAppId(appId));
204 } else if (actionId == QStringLiteral("stop_item")) { // Quit
205 if (m_appManager) {
206 m_appManager->stopApplication(appId);
207 }
208 } else if (actionId.startsWith(QStringLiteral("surface_"))){
209 ApplicationInfoInterface *appInfo = m_appManager->findApplication(appId);
210 if (!appInfo) {
211 qWarning() << "App for" << appId << "not found in launcher. Cannot invoke quicklist action";
212 }
213 for (int i = 0; i < appInfo->surfaceList()->count(); ++i) {
214 MirSurfaceInterface *iface = appInfo->surfaceList()->get(i);
215 QString id = actionId;
216 id.remove(QRegExp("^surface_"));
217 if (id == iface->persistentId()) {
218 iface->activate();
219 }
220 }
221 // Nope, we don't know this action, let the backend forward it to the application
222 } else {
223 // TODO: forward quicklist action to app, possibly via m_dbusIface
224 }
225 }
228void LauncherModel::setUser(const QString &username)
230 Q_UNUSED(username)
233QString LauncherModel::getUrlForAppId(const QString &appId) const
235 // appId is either an appId or a legacy app name. Let's find out which
236 if (appId.isEmpty()) {
237 return QString();
238 }
240 if (!appId.contains('_')) {
241 return "application:///" + appId + ".desktop";
242 }
244 QStringList parts = appId.split('_');
245 QString package = parts.value(0);
246 QString app = parts.value(1, QStringLiteral("first-listed-app"));
247 return "appid://" + package + "/" + app + "/current-user-version";
250ApplicationManagerInterface *LauncherModel::applicationManager() const
252 return m_appManager;
255void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
257 // Is there already another appmanager set?
258 if (m_appManager) {
259 // Disconnect any signals
260 disconnect(this, &LauncherModel::applicationAdded, 0, nullptr);
261 disconnect(this, &LauncherModel::applicationRemoved, 0, nullptr);
262 disconnect(this, &LauncherModel::focusedAppIdChanged, 0, nullptr);
264 // remove any recent/running apps from the launcher
265 QList<int> recentAppIndices;
266 for (int i = 0; i < m_list.count(); ++i) {
267 if (m_list.at(i)->recent()) {
268 recentAppIndices << i;
269 }
270 }
271 int run = 0;
272 while (recentAppIndices.count() > 0) {
273 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
274 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
275 endRemoveRows();
276 recentAppIndices.takeFirst();
277 ++run;
278 }
279 }
281 m_appManager = appManager;
282 connect(m_appManager, &ApplicationManagerInterface::rowsInserted, this, &LauncherModel::applicationAdded);
283 connect(m_appManager, &ApplicationManagerInterface::rowsAboutToBeRemoved, this, &LauncherModel::applicationRemoved);
284 connect(m_appManager, &ApplicationManagerInterface::focusedApplicationIdChanged, this, &LauncherModel::focusedAppIdChanged);
286 Q_EMIT applicationManagerChanged();
288 for (int i = 0; i < appManager->count(); ++i) {
289 applicationAdded(QModelIndex(), i);
290 }
293bool LauncherModel::onlyPinned() const
295 return false;
298void LauncherModel::setOnlyPinned(bool onlyPinned) {
299 Q_UNUSED(onlyPinned);
300 qWarning() << "This launcher implementation does not support showing only pinned apps";
303void LauncherModel::storeAppList()
305 QStringList appIds;
306 Q_FOREACH(LauncherItem *item, m_list) {
307 if (item->pinned()) {
308 appIds << item->appId();
309 }
310 }
311 m_settings->setStoredApplications(appIds);
312 m_asAdapter->syncItems(m_list);
315void LauncherModel::unpin(const QString &appId)
317 const int index = findApplication(appId);
318 if (index < 0) {
319 return;
320 }
322 if (m_appManager->findApplication(appId)) {
323 if (m_list.at(index)->pinned()) {
324 m_list.at(index)->setPinned(false);
325 QModelIndex modelIndex = this->index(index);
326 Q_EMIT dataChanged(modelIndex, modelIndex, {RolePinned});
327 }
328 } else {
329 beginRemoveRows(QModelIndex(), index, index);
330 m_list.takeAt(index)->deleteLater();
331 endRemoveRows();
332 }
335int LauncherModel::findApplication(const QString &appId)
337 for (int i = 0; i < m_list.count(); ++i) {
338 LauncherItem *item = m_list.at(i);
339 if (item->appId() == appId) {
340 return i;
341 }
342 }
343 return -1;
346void LauncherModel::progressChanged(const QString &appId, int progress)
348 const int idx = findApplication(appId);
349 if (idx >= 0) {
350 LauncherItem *item = m_list.at(idx);
351 item->setProgress(progress);
352 Q_EMIT dataChanged(index(idx), index(idx), {RoleProgress});
353 }
356void LauncherModel::countChanged(const QString &appId, int count)
358 const int idx = findApplication(appId);
359 if (idx >= 0) {
360 LauncherItem *item = m_list.at(idx);
361 item->setCount(count);
362 QVector<int> changedRoles = {RoleCount};
363 if (item->countVisible() && !item->alerting() && !item->focused()) {
364 changedRoles << RoleAlerting;
365 item->setAlerting(true);
366 }
367 m_asAdapter->syncItems(m_list);
368 Q_EMIT dataChanged(index(idx), index(idx), changedRoles);
369 }
372void LauncherModel::countVisibleChanged(const QString &appId, bool countVisible)
374 int idx = findApplication(appId);
375 if (idx >= 0) {
376 LauncherItem *item = m_list.at(idx);
377 item->setCountVisible(countVisible);
378 QVector<int> changedRoles = {RoleCountVisible};
379 if (countVisible && !item->alerting() && !item->focused()) {
380 changedRoles << RoleAlerting;
381 item->setAlerting(true);
382 }
383 Q_EMIT dataChanged(index(idx), index(idx), changedRoles);
385 // If countVisible goes to false, and the item is neither pinned nor recent we can drop it
386 if (!countVisible && !item->pinned() && !item->recent()) {
387 beginRemoveRows(QModelIndex(), idx, idx);
388 m_list.takeAt(idx)->deleteLater();
389 endRemoveRows();
390 }
391 } else {
392 // Need to create a new LauncherItem and show the highlight
393 UalWrapper::AppInfo appInfo = UalWrapper::getApplicationInfo(appId);
394 if (countVisible && appInfo.valid) {
395 LauncherItem *item = new LauncherItem(appId,
396 appInfo.name,
397 appInfo.icon,
398 this);
399 item->setCountVisible(true);
400 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
401 m_list.append(item);
402 endInsertRows();
403 }
404 }
405 m_asAdapter->syncItems(m_list);
408void LauncherModel::refresh()
410 // First walk through all the existing items and see if we need to remove something
411 QList<LauncherItem*> toBeRemoved;
412 Q_FOREACH (LauncherItem* item, m_list) {
413 UalWrapper::AppInfo appInfo = UalWrapper::getApplicationInfo(item->appId());
414 if (!appInfo.valid) {
415 // Application no longer available => drop it!
416 toBeRemoved << item;
417 } else if (!m_settings->storedApplications().contains(item->appId())) {
418 // Item not in settings any more => drop it!
419 toBeRemoved << item;
420 } else {
421 int idx = m_list.indexOf(item);
422 item->setName(appInfo.name);
423 item->setPinned(item->pinned()); // update pinned text if needed
424 item->setRunning(item->running());
425 Q_EMIT dataChanged(index(idx), index(idx), {RoleName, RoleRunning});
427 const QString oldIcon = item->icon();
428 if (oldIcon == appInfo.icon) { // same icon file, perhaps different contents, simulate changing the icon name to force reload
429 item->setIcon(QString());
430 Q_EMIT dataChanged(index(idx), index(idx), {RoleIcon});
431 }
433 // now set the icon for real
434 item->setIcon(appInfo.icon);
435 Q_EMIT dataChanged(index(idx), index(idx), {RoleIcon});
436 }
437 }
439 Q_FOREACH (LauncherItem* item, toBeRemoved) {
440 unpin(item->appId());
441 }
443 bool changed = toBeRemoved.count() > 0;
445 // This brings the Launcher into sync with the settings backend again. There's an issue though:
446 // If we can't find a .desktop file for an entry we need to skip it. That makes our settingsIndex
447 // go out of sync with the actual index of items. So let's also use an addedIndex which reflects
448 // the settingsIndex minus the skipped items.
449 int addedIndex = 0;
451 // Now walk through settings and see if we need to add something
452 for (int settingsIndex = 0; settingsIndex < m_settings->storedApplications().count(); ++settingsIndex) {
453 const QString entry = m_settings->storedApplications().at(settingsIndex);
454 int itemIndex = -1;
455 for (int i = 0; i < m_list.count(); ++i) {
456 if (m_list.at(i)->appId() == entry) {
457 itemIndex = i;
458 break;
459 }
460 }
462 if (itemIndex == -1) {
463 // Need to add it. Just add it into the addedIndex to keep same ordering as the list
464 // in the settings.
465 UalWrapper::AppInfo appInfo = UalWrapper::getApplicationInfo(entry);
466 if (!appInfo.valid) {
467 continue;
468 }
470 LauncherItem *item = new LauncherItem(entry,
471 appInfo.name,
472 appInfo.icon,
473 this);
474 item->setPinned(true);
475 beginInsertRows(QModelIndex(), addedIndex, addedIndex);
476 m_list.insert(addedIndex, item);
477 endInsertRows();
478 changed = true;
479 } else if (itemIndex != addedIndex) {
480 // The item is already there, but it is in a different place than in the settings.
481 // Move it to the addedIndex
482 beginMoveRows(QModelIndex(), itemIndex, itemIndex, QModelIndex(), addedIndex);
483 m_list.move(itemIndex, addedIndex);
484 endMoveRows();
485 changed = true;
486 }
488 // Just like settingsIndex, this will increase with every item, except the ones we
489 // skipped with the "continue" call above.
490 addedIndex++;
491 }
493 if (changed) {
494 Q_EMIT hint();
495 }
497 m_asAdapter->syncItems(m_list);
500void LauncherModel::alert(const QString &appId)
502 int idx = findApplication(appId);
503 if (idx >= 0) {
504 LauncherItem *item = m_list.at(idx);
505 if (!item->focused() && !item->alerting()) {
506 item->setAlerting(true);
507 Q_EMIT dataChanged(index(idx), index(idx), {RoleAlerting});
508 }
509 }
512void LauncherModel::applicationAdded(const QModelIndex &parent, int row)
514 Q_UNUSED(parent);
516 ApplicationInfoInterface *app = m_appManager->get(row);
517 if (!app) {
518 qWarning() << "LauncherModel received an applicationAdded signal, but there's no such application!";
519 return;
520 }
522 if (app->appId() == QLatin1String("unity8-dash")) {
523 // Not adding the dash app
524 return;
525 }
527 const int itemIndex = findApplication(app->appId());
528 if (itemIndex != -1) {
529 LauncherItem *item = m_list.at(itemIndex);
530 if (!item->recent()) {
531 item->setRecent(true);
532 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), {RoleRecent});
533 }
534 item->setRunning(true);
535 } else {
536 LauncherItem *item = new LauncherItem(app->appId(), app->name(), app->icon().toString(), this);
537 item->setRecent(true);
538 item->setRunning(true);
539 item->setFocused(app->focused());
540 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
541 m_list.append(item);
542 endInsertRows();
543 }
544 connect(app, &ApplicationInfoInterface::surfaceCountChanged, this, &LauncherModel::updateSurfaceList);
545 m_asAdapter->syncItems(m_list);
546 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), {RoleRunning});
549void LauncherModel::updateSurfaceList()
551 ApplicationInfoInterface *app = static_cast<ApplicationInfoInterface*>(sender());
552 updateSurfaceListForApp(app);
555void LauncherModel::updateSurfaceListForSurface()
557 MirSurfaceInterface *iface = static_cast<MirSurfaceInterface*>(sender());
558 ApplicationInfoInterface* app = m_appManager->findApplication(iface->appId());
559 if (!app) {
560 return;
561 }
562 updateSurfaceListForApp(app);
565void LauncherModel::updateSurfaceListForApp(ApplicationInfoInterface* app)
567 int idx = findApplication(app->appId());
568 if (idx < 0) {
569 qWarning() << "Received a surface count changed event from an app that's not in the Launcher model";
570 return;
571 }
572 LauncherItem *item = m_list.at(idx);
573 QList<QPair<QString, QString> > surfaces;
574 for (int i = 0; i < app->surfaceList()->count(); ++i) {
575 MirSurfaceInterface* iface = app->surfaceList()->get(i);
576 // Avoid duplicate connections, so let's just disconnect first to be sure
577 disconnect(iface, &MirSurfaceInterface::nameChanged, this, &LauncherModel::updateSurfaceListForSurface);
578 connect(iface, &MirSurfaceInterface::nameChanged, this, &LauncherModel::updateSurfaceListForSurface);
579 QString name = iface->name();
580 if (name.isEmpty()) {
581 name = app->name();
582 }
583 surfaces.append({iface->persistentId(), name});
584 }
585 item->setSurfaces(surfaces);
586 Q_EMIT dataChanged(index(idx), index(idx), {RoleSurfaceCount});
589void LauncherModel::applicationRemoved(const QModelIndex &parent, int row)
591 Q_UNUSED(parent)
593 ApplicationInfoInterface *app = m_appManager->get(row);
594 int appIndex = -1;
595 for (int i = 0; i < m_list.count(); ++i) {
596 if (m_list.at(i)->appId() == app->appId()) {
597 appIndex = i;
598 break;
599 }
600 }
602 if (appIndex < 0) {
603 qWarning() << Q_FUNC_INFO << "appIndex not found";
604 return;
605 }
607 disconnect(app, &ApplicationInfoInterface::surfaceCountChanged, this, &LauncherModel::updateSurfaceList);
609 LauncherItem * item = m_list.at(appIndex);
611 if (!item->pinned()) {
612 beginRemoveRows(QModelIndex(), appIndex, appIndex);
613 m_list.takeAt(appIndex)->deleteLater();
614 endRemoveRows();
615 m_asAdapter->syncItems(m_list);
616 } else {
617 QVector<int> changedRoles = {RoleRunning};
618 item->setRunning(false);
619 if (item->focused()) {
620 changedRoles << RoleFocused;
621 item->setFocused(false);
622 }
623 Q_EMIT dataChanged(index(appIndex), index(appIndex), changedRoles);
624 }
627void LauncherModel::focusedAppIdChanged()
629 const QString appId = m_appManager->focusedApplicationId();
630 for (int i = 0; i < m_list.count(); ++i) {
631 LauncherItem *item = m_list.at(i);
632 if (!item->focused() && item->appId() == appId) {
633 QVector<int> changedRoles;
634 changedRoles << RoleFocused;
635 item->setFocused(true);
636 if (item->alerting()) {
637 changedRoles << RoleAlerting;
638 item->setAlerting(false);
639 }
640 Q_EMIT dataChanged(index(i), index(i), changedRoles);
641 } else if (item->focused() && item->appId() != appId) {
642 item->setFocused(false);
643 Q_EMIT dataChanged(index(i), index(i), {RoleFocused});
644 }
645 }