on 4ffc752 help sign in
unity8/plugins/Unity/Launcher/launchermodel.cpp
613 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 // Nope, we don't know this action, let the backend forward it to the application
208 } else {
209 // TODO: forward quicklist action to app, possibly via m_dbusIface
210 }
211 }
214void LauncherModel::setUser(const QString &username)
216 Q_UNUSED(username)
219QString LauncherModel::getUrlForAppId(const QString &appId) const
221 // appId is either an appId or a legacy app name. Let's find out which
222 if (appId.isEmpty()) {
223 return QString();
224 }
226 if (!appId.contains('_')) {
227 return "application:///" + appId + ".desktop";
228 }
230 QStringList parts = appId.split('_');
231 QString package = parts.value(0);
232 QString app = parts.value(1, QStringLiteral("first-listed-app"));
233 return "appid://" + package + "/" + app + "/current-user-version";
236ApplicationManagerInterface *LauncherModel::applicationManager() const
238 return m_appManager;
241void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
243 // Is there already another appmanager set?
244 if (m_appManager) {
245 // Disconnect any signals
246 disconnect(this, &LauncherModel::applicationAdded, 0, nullptr);
247 disconnect(this, &LauncherModel::applicationRemoved, 0, nullptr);
248 disconnect(this, &LauncherModel::focusedAppIdChanged, 0, nullptr);
250 // remove any recent/running apps from the launcher
251 QList<int> recentAppIndices;
252 for (int i = 0; i < m_list.count(); ++i) {
253 if (m_list.at(i)->recent()) {
254 recentAppIndices << i;
255 }
256 }
257 int run = 0;
258 while (recentAppIndices.count() > 0) {
259 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
260 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
261 endRemoveRows();
262 recentAppIndices.takeFirst();
263 ++run;
264 }
265 }
267 m_appManager = appManager;
268 connect(m_appManager, &ApplicationManagerInterface::rowsInserted, this, &LauncherModel::applicationAdded);
269 connect(m_appManager, &ApplicationManagerInterface::rowsAboutToBeRemoved, this, &LauncherModel::applicationRemoved);
270 connect(m_appManager, &ApplicationManagerInterface::focusedApplicationIdChanged, this, &LauncherModel::focusedAppIdChanged);
272 Q_EMIT applicationManagerChanged();
274 for (int i = 0; i < appManager->count(); ++i) {
275 applicationAdded(QModelIndex(), i);
276 }
279bool LauncherModel::onlyPinned() const
281 return false;
284void LauncherModel::setOnlyPinned(bool onlyPinned) {
285 Q_UNUSED(onlyPinned);
286 qWarning() << "This launcher implementation does not support showing only pinned apps";
289void LauncherModel::storeAppList()
291 QStringList appIds;
292 Q_FOREACH(LauncherItem *item, m_list) {
293 if (item->pinned()) {
294 appIds << item->appId();
295 }
296 }
297 m_settings->setStoredApplications(appIds);
298 m_asAdapter->syncItems(m_list);
301void LauncherModel::unpin(const QString &appId)
303 const int index = findApplication(appId);
304 if (index < 0) {
305 return;
306 }
308 if (m_appManager->findApplication(appId)) {
309 if (m_list.at(index)->pinned()) {
310 m_list.at(index)->setPinned(false);
311 QModelIndex modelIndex = this->index(index);
312 Q_EMIT dataChanged(modelIndex, modelIndex, {RolePinned});
313 }
314 } else {
315 beginRemoveRows(QModelIndex(), index, index);
316 m_list.takeAt(index)->deleteLater();
317 endRemoveRows();
318 }
321int LauncherModel::findApplication(const QString &appId)
323 for (int i = 0; i < m_list.count(); ++i) {
324 LauncherItem *item = m_list.at(i);
325 if (item->appId() == appId) {
326 return i;
327 }
328 }
329 return -1;
332void LauncherModel::progressChanged(const QString &appId, int progress)
334 const int idx = findApplication(appId);
335 if (idx >= 0) {
336 LauncherItem *item = m_list.at(idx);
337 item->setProgress(progress);
338 Q_EMIT dataChanged(index(idx), index(idx), {RoleProgress});
339 }
342void LauncherModel::countChanged(const QString &appId, int count)
344 const int idx = findApplication(appId);
345 if (idx >= 0) {
346 LauncherItem *item = m_list.at(idx);
347 item->setCount(count);
348 QVector<int> changedRoles = {RoleCount};
349 if (item->countVisible() && !item->alerting() && !item->focused()) {
350 changedRoles << RoleAlerting;
351 item->setAlerting(true);
352 }
353 m_asAdapter->syncItems(m_list);
354 Q_EMIT dataChanged(index(idx), index(idx), changedRoles);
355 }
358void LauncherModel::countVisibleChanged(const QString &appId, bool countVisible)
360 int idx = findApplication(appId);
361 if (idx >= 0) {
362 LauncherItem *item = m_list.at(idx);
363 item->setCountVisible(countVisible);
364 QVector<int> changedRoles = {RoleCountVisible};
365 if (countVisible && !item->alerting() && !item->focused()) {
366 changedRoles << RoleAlerting;
367 item->setAlerting(true);
368 }
369 Q_EMIT dataChanged(index(idx), index(idx), changedRoles);
371 // If countVisible goes to false, and the item is neither pinned nor recent we can drop it
372 if (!countVisible && !item->pinned() && !item->recent()) {
373 beginRemoveRows(QModelIndex(), idx, idx);
374 m_list.takeAt(idx)->deleteLater();
375 endRemoveRows();
376 }
377 } else {
378 // Need to create a new LauncherItem and show the highlight
379 UalWrapper::AppInfo appInfo = UalWrapper::getApplicationInfo(appId);
380 if (countVisible && appInfo.valid) {
381 LauncherItem *item = new LauncherItem(appId,
382 appInfo.name,
383 appInfo.icon,
384 this);
385 item->setCountVisible(true);
386 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
387 m_list.append(item);
388 endInsertRows();
389 }
390 }
391 m_asAdapter->syncItems(m_list);
394void LauncherModel::refresh()
396 // First walk through all the existing items and see if we need to remove something
397 QList<LauncherItem*> toBeRemoved;
398 Q_FOREACH (LauncherItem* item, m_list) {
399 UalWrapper::AppInfo appInfo = UalWrapper::getApplicationInfo(item->appId());
400 if (!appInfo.valid) {
401 // Application no longer available => drop it!
402 toBeRemoved << item;
403 } else if (!m_settings->storedApplications().contains(item->appId())) {
404 // Item not in settings any more => drop it!
405 toBeRemoved << item;
406 } else {
407 int idx = m_list.indexOf(item);
408 item->setName(appInfo.name);
409 item->setPinned(item->pinned()); // update pinned text if needed
410 item->setRunning(item->running());
411 Q_EMIT dataChanged(index(idx), index(idx), {RoleName, RoleRunning});
413 const QString oldIcon = item->icon();
414 if (oldIcon == appInfo.icon) { // same icon file, perhaps different contents, simulate changing the icon name to force reload
415 item->setIcon(QString());
416 Q_EMIT dataChanged(index(idx), index(idx), {RoleIcon});
417 }
419 // now set the icon for real
420 item->setIcon(appInfo.icon);
421 Q_EMIT dataChanged(index(idx), index(idx), {RoleIcon});
422 }
423 }
425 Q_FOREACH (LauncherItem* item, toBeRemoved) {
426 unpin(item->appId());
427 }
429 bool changed = toBeRemoved.count() > 0;
431 // This brings the Launcher into sync with the settings backend again. There's an issue though:
432 // If we can't find a .desktop file for an entry we need to skip it. That makes our settingsIndex
433 // go out of sync with the actual index of items. So let's also use an addedIndex which reflects
434 // the settingsIndex minus the skipped items.
435 int addedIndex = 0;
437 // Now walk through settings and see if we need to add something
438 for (int settingsIndex = 0; settingsIndex < m_settings->storedApplications().count(); ++settingsIndex) {
439 const QString entry = m_settings->storedApplications().at(settingsIndex);
440 int itemIndex = -1;
441 for (int i = 0; i < m_list.count(); ++i) {
442 if (m_list.at(i)->appId() == entry) {
443 itemIndex = i;
444 break;
445 }
446 }
448 if (itemIndex == -1) {
449 // Need to add it. Just add it into the addedIndex to keep same ordering as the list
450 // in the settings.
451 UalWrapper::AppInfo appInfo = UalWrapper::getApplicationInfo(entry);
452 if (!appInfo.valid) {
453 continue;
454 }
456 LauncherItem *item = new LauncherItem(entry,
457 appInfo.name,
458 appInfo.icon,
459 this);
460 item->setPinned(true);
461 beginInsertRows(QModelIndex(), addedIndex, addedIndex);
462 m_list.insert(addedIndex, item);
463 endInsertRows();
464 changed = true;
465 } else if (itemIndex != addedIndex) {
466 // The item is already there, but it is in a different place than in the settings.
467 // Move it to the addedIndex
468 beginMoveRows(QModelIndex(), itemIndex, itemIndex, QModelIndex(), addedIndex);
469 m_list.move(itemIndex, addedIndex);
470 endMoveRows();
471 changed = true;
472 }
474 // Just like settingsIndex, this will increase with every item, except the ones we
475 // skipped with the "continue" call above.
476 addedIndex++;
477 }
479 if (changed) {
480 Q_EMIT hint();
481 }
483 m_asAdapter->syncItems(m_list);
486void LauncherModel::alert(const QString &appId)
488 int idx = findApplication(appId);
489 if (idx >= 0) {
490 LauncherItem *item = m_list.at(idx);
491 if (!item->focused() && !item->alerting()) {
492 item->setAlerting(true);
493 Q_EMIT dataChanged(index(idx), index(idx), {RoleAlerting});
494 }
495 }
498void LauncherModel::applicationAdded(const QModelIndex &parent, int row)
500 Q_UNUSED(parent);
502 ApplicationInfoInterface *app = m_appManager->get(row);
503 if (!app) {
504 qWarning() << "LauncherModel received an applicationAdded signal, but there's no such application!";
505 return;
506 }
508 if (app->appId() == QLatin1String("unity8-dash")) {
509 // Not adding the dash app
510 return;
511 }
513 const int itemIndex = findApplication(app->appId());
514 if (itemIndex != -1) {
515 LauncherItem *item = m_list.at(itemIndex);
516 if (!item->recent()) {
517 item->setRecent(true);
518 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), {RoleRecent});
519 }
520 if (item->surfaceCount() != app->surfaceCount()) {
521 item->setSurfaceCount(app->surfaceCount());
522 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), {RoleSurfaceCount});
523 }
525 item->setRunning(true);
526 } else {
527 LauncherItem *item = new LauncherItem(app->appId(), app->name(), app->icon().toString(), this);
528 item->setRecent(true);
529 item->setRunning(true);
530 item->setFocused(app->focused());
531 item->setSurfaceCount(app->surfaceCount());
532 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
533 m_list.append(item);
534 endInsertRows();
535 }
536 connect(app, &ApplicationInfoInterface::surfaceCountChanged, this, &LauncherModel::applicationSurfaceCountChanged);
537 m_asAdapter->syncItems(m_list);
538 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), {RoleRunning});
541void LauncherModel::applicationSurfaceCountChanged(int count)
543 ApplicationInfoInterface *app = static_cast<ApplicationInfoInterface*>(sender());
544 int idx = findApplication(app->appId());
545 if (idx < 0) {
546 qWarning() << "Received a surface count changed event from an app that's not in the Launcher model";
547 return;
548 }
549 LauncherItem *item = m_list.at(idx);
550 if (item->surfaceCount() != count) {
551 item->setSurfaceCount(count);
552 Q_EMIT dataChanged(index(idx), index(idx), {RoleSurfaceCount});
553 }
556void LauncherModel::applicationRemoved(const QModelIndex &parent, int row)
558 Q_UNUSED(parent)
560 ApplicationInfoInterface *app = m_appManager->get(row);
561 int appIndex = -1;
562 for (int i = 0; i < m_list.count(); ++i) {
563 if (m_list.at(i)->appId() == app->appId()) {
564 appIndex = i;
565 break;
566 }
567 }
569 if (appIndex < 0) {
570 qWarning() << Q_FUNC_INFO << "appIndex not found";
571 return;
572 }
574 disconnect(app, &ApplicationInfoInterface::surfaceCountChanged, this, &LauncherModel::applicationSurfaceCountChanged);
576 LauncherItem * item = m_list.at(appIndex);
578 if (!item->pinned()) {
579 beginRemoveRows(QModelIndex(), appIndex, appIndex);
580 m_list.takeAt(appIndex)->deleteLater();
581 endRemoveRows();
582 m_asAdapter->syncItems(m_list);
583 } else {
584 QVector<int> changedRoles = {RoleRunning};
585 item->setRunning(false);
586 if (item->focused()) {
587 changedRoles << RoleFocused;
588 item->setFocused(false);
589 }
590 Q_EMIT dataChanged(index(appIndex), index(appIndex), changedRoles);
591 }
594void LauncherModel::focusedAppIdChanged()
596 const QString appId = m_appManager->focusedApplicationId();
597 for (int i = 0; i < m_list.count(); ++i) {
598 LauncherItem *item = m_list.at(i);
599 if (!item->focused() && item->appId() == appId) {
600 QVector<int> changedRoles;
601 changedRoles << RoleFocused;
602 item->setFocused(true);
603 if (item->alerting()) {
604 changedRoles << RoleAlerting;
605 item->setAlerting(false);
606 }
607 Q_EMIT dataChanged(index(i), index(i), changedRoles);
608 } else if (item->focused() && item->appId() != appId) {
609 item->setFocused(false);
610 Q_EMIT dataChanged(index(i), index(i), {RoleFocused});
611 }
612 }