on c269263 help sign in
unity8/plugins/Unity/Launcher/launchermodel.cpp
610 lines 21 kB history raw
1/*
2 * Copyright 2013-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 "launchermodel.h"
21#include "launcheritem.h"
22#include "gsettings.h"
23#include "desktopfilehandler.h"
24#include "dbusinterface.h"
25#include "asadapter.h"
27#include <unity/shell/application/ApplicationInfoInterface.h>
28#include <unity/shell/application/MirSurfaceListInterface.h>
30#include <QDesktopServices>
31#include <QDebug>
33using namespace unity::shell::application;
35LauncherModel::LauncherModel(QObject *parent):
36 LauncherModelInterface(parent),
37 m_settings(new GSettings(this)),
38 m_dbusIface(new DBusInterface(this)),
39 m_asAdapter(new ASAdapter()),
40 m_appManager(nullptr)
42 connect(m_dbusIface, &DBusInterface::countChanged, this, &LauncherModel::countChanged);
43 connect(m_dbusIface, &DBusInterface::countVisibleChanged, this, &LauncherModel::countVisibleChanged);
44 connect(m_dbusIface, &DBusInterface::progressChanged, this, &LauncherModel::progressChanged);
45 connect(m_dbusIface, &DBusInterface::refreshCalled, this, &LauncherModel::refresh);
46 connect(m_dbusIface, &DBusInterface::alertCalled, this, &LauncherModel::alert);
48 connect(m_settings, &GSettings::changed, this, &LauncherModel::refresh);
50 refresh();
53LauncherModel::~LauncherModel()
55 while (!m_list.empty()) {
56 m_list.takeFirst()->deleteLater();
57 }
59 delete m_asAdapter;
62int LauncherModel::rowCount(const QModelIndex &parent) const
64 Q_UNUSED(parent)
65 return m_list.count();
68QVariant LauncherModel::data(const QModelIndex &index, int role) const
70 LauncherItem *item = m_list.at(index.row());
71 switch(role) {
72 case RoleAppId:
73 return item->appId();
74 case RoleName:
75 return item->name();
76 case RoleIcon:
77 return item->icon();
78 case RolePinned:
79 return item->pinned();
80 case RoleCount:
81 return item->count();
82 case RoleCountVisible:
83 return item->countVisible();
84 case RoleProgress:
85 return item->progress();
86 case RoleFocused:
87 return item->focused();
88 case RoleAlerting:
89 return item->alerting();
90 case RoleRunning:
91 return item->running();
92 case RoleSurfaceCount:
93 return item->surfaceCount();
94 default:
95 qWarning() << Q_FUNC_INFO << "missing role, implement me";
96 return QVariant();
97 }
99 return QVariant();
102void LauncherModel::setAlerting(const QString &appId, bool alerting) {
103 int index = findApplication(appId);
104 if (index >= 0) {
105 QModelIndex modelIndex = this->index(index);
106 LauncherItem *item = m_list.at(index);
107 if (!item->focused()) {
108 item->setAlerting(alerting);
109 Q_EMIT dataChanged(modelIndex, modelIndex, {RoleAlerting});
110 }
111 }
114unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
116 if (index < 0 || index >= m_list.count()) {
117 return 0;
118 }
119 return m_list.at(index);
122void LauncherModel::move(int oldIndex, int newIndex)
124 // Make sure its not moved outside the lists
125 if (newIndex < 0) {
126 newIndex = 0;
127 }
128 if (newIndex >= m_list.count()) {
129 newIndex = m_list.count()-1;
130 }
132 // Nothing to do?
133 if (oldIndex == newIndex) {
134 return;
135 }
137 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
138 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
139 // adjust the model's index by +1 in case we're moving upwards
140 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
142 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
143 m_list.move(oldIndex, newIndex);
144 endMoveRows();
146 if (!m_list.at(newIndex)->pinned()) {
147 pin(m_list.at(newIndex)->appId());
148 } else {
149 storeAppList();
150 }
153void LauncherModel::pin(const QString &appId, int index)
155 int currentIndex = findApplication(appId);
157 if (currentIndex >= 0) {
158 if (index == -1 || index == currentIndex) {
159 m_list.at(currentIndex)->setPinned(true);
160 QModelIndex modelIndex = this->index(currentIndex);
161 Q_EMIT dataChanged(modelIndex, modelIndex, {RolePinned});
162 } else {
163 move(currentIndex, index);
164 // move() will store the list to the backend itself, so just exit at this point.
165 return;
166 }
167 } else {
168 if (index == -1) {
169 index = m_list.count();
170 }
172 DesktopFileHandler desktopFile(appId);
173 if (!desktopFile.isValid()) {
174 qWarning() << "Can't pin this application, there is no .desktop file available.";
175 return;
176 }
178 beginInsertRows(QModelIndex(), index, index);
179 LauncherItem *item = new LauncherItem(appId,
180 desktopFile.displayName(),
181 desktopFile.icon(),
182 this);
183 item->setPinned(true);
184 m_list.insert(index, item);
185 endInsertRows();
186 }
188 storeAppList();
191void LauncherModel::requestRemove(const QString &appId)
193 unpin(appId);
194 storeAppList();
197void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
199 const int index = findApplication(appId);
200 if (index < 0) {
201 return;
202 }
204 LauncherItem *item = m_list.at(index);
205 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
206 if (model) {
207 const QString actionId = model->get(actionIndex).actionId();
209 // Check if this is one of the launcher actions we handle ourselves
210 if (actionId == QLatin1String("pin_item")) {
211 if (item->pinned()) {
212 requestRemove(appId);
213 } else {
214 pin(appId);
215 }
216 } else if (actionId == QLatin1String("launch_item")) {
217 QDesktopServices::openUrl(getUrlForAppId(appId));
218 } else if (actionId == QLatin1String("stop_item")) { // Quit
219 if (m_appManager) {
220 m_appManager->stopApplication(appId);
221 }
222 // Nope, we don't know this action, let the backend forward it to the application
223 } else {
224 // TODO: forward quicklist action to app, possibly via m_dbusIface
225 }
226 }
229void LauncherModel::setUser(const QString &username)
231 Q_UNUSED(username)
232 qWarning() << "This backend doesn't support multiple users";
235QString LauncherModel::getUrlForAppId(const QString &appId) const
237 // appId is either an appId or a legacy app name. Let's find out which
238 if (appId.isEmpty()) {
239 return QString();
240 }
242 if (!appId.contains('_')) {
243 return "application:///" + appId + ".desktop";
244 }
246 QStringList parts = appId.split('_');
247 QString package = parts.value(0);
248 QString app = parts.value(1, QStringLiteral("first-listed-app"));
249 return "appid://" + package + "/" + app + "/current-user-version";
252ApplicationManagerInterface *LauncherModel::applicationManager() const
254 return m_appManager;
257void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
259 // Is there already another appmanager set?
260 if (m_appManager) {
261 // Disconnect any signals
262 disconnect(this, &LauncherModel::applicationAdded, 0, nullptr);
263 disconnect(this, &LauncherModel::applicationRemoved, 0, nullptr);
264 disconnect(this, &LauncherModel::focusedAppIdChanged, 0, nullptr);
266 // remove any recent/running apps from the launcher
267 QList<int> recentAppIndices;
268 for (int i = 0; i < m_list.count(); ++i) {
269 if (m_list.at(i)->recent()) {
270 recentAppIndices << i;
271 }
272 }
273 int run = 0;
274 while (recentAppIndices.count() > 0) {
275 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
276 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
277 endRemoveRows();
278 recentAppIndices.takeFirst();
279 ++run;
280 }
281 }
283 m_appManager = appManager;
284 connect(m_appManager, &ApplicationManagerInterface::rowsInserted, this, &LauncherModel::applicationAdded);
285 connect(m_appManager, &ApplicationManagerInterface::rowsAboutToBeRemoved, this, &LauncherModel::applicationRemoved);
286 connect(m_appManager, &ApplicationManagerInterface::focusedApplicationIdChanged, this, &LauncherModel::focusedAppIdChanged);
288 Q_EMIT applicationManagerChanged();
290 for (int i = 0; i < appManager->count(); ++i) {
291 applicationAdded(QModelIndex(), i);
292 }
295bool LauncherModel::onlyPinned() const
297 return false;
300void LauncherModel::setOnlyPinned(bool onlyPinned) {
301 Q_UNUSED(onlyPinned);
302 qWarning() << "This launcher implementation does not support showing only pinned apps";
305void LauncherModel::storeAppList()
307 QStringList appIds;
308 Q_FOREACH(LauncherItem *item, m_list) {
309 if (item->pinned()) {
310 appIds << item->appId();
311 }
312 }
313 m_settings->setStoredApplications(appIds);
314 m_asAdapter->syncItems(m_list);
317void LauncherModel::unpin(const QString &appId)
319 const int index = findApplication(appId);
320 if (index < 0) {
321 return;
322 }
324 if (m_appManager->findApplication(appId)) {
325 if (m_list.at(index)->pinned()) {
326 m_list.at(index)->setPinned(false);
327 QModelIndex modelIndex = this->index(index);
328 Q_EMIT dataChanged(modelIndex, modelIndex, {RolePinned});
329 }
330 } else {
331 beginRemoveRows(QModelIndex(), index, index);
332 m_list.takeAt(index)->deleteLater();
333 endRemoveRows();
334 }
337int LauncherModel::findApplication(const QString &appId)
339 for (int i = 0; i < m_list.count(); ++i) {
340 LauncherItem *item = m_list.at(i);
341 if (item->appId() == appId) {
342 return i;
343 }
344 }
345 return -1;
348void LauncherModel::progressChanged(const QString &appId, int progress)
350 const int idx = findApplication(appId);
351 if (idx >= 0) {
352 LauncherItem *item = m_list.at(idx);
353 item->setProgress(progress);
354 Q_EMIT dataChanged(index(idx), index(idx), {RoleProgress});
355 }
358void LauncherModel::countChanged(const QString &appId, int count)
360 const int idx = findApplication(appId);
361 if (idx >= 0) {
362 LauncherItem *item = m_list.at(idx);
363 item->setCount(count);
364 if (item->countVisible()) {
365 setAlerting(item->appId(), true);
366 }
367 m_asAdapter->syncItems(m_list);
368 Q_EMIT dataChanged(index(idx), index(idx), {RoleCount});
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 if (countVisible) {
379 setAlerting(item->appId(), true);
380 }
381 Q_EMIT dataChanged(index(idx), index(idx), {RoleCountVisible});
383 // If countVisible goes to false, and the item is neither pinned nor recent we can drop it
384 if (!countVisible && !item->pinned() && !item->recent()) {
385 beginRemoveRows(QModelIndex(), idx, idx);
386 m_list.takeAt(idx)->deleteLater();
387 endRemoveRows();
388 }
389 } else {
390 // Need to create a new LauncherItem and show the highlight
391 DesktopFileHandler desktopFile(appId);
392 if (countVisible && desktopFile.isValid()) {
393 LauncherItem *item = new LauncherItem(appId,
394 desktopFile.displayName(),
395 desktopFile.icon(),
396 this);
397 item->setCountVisible(true);
398 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
399 m_list.append(item);
400 endInsertRows();
401 }
402 }
403 m_asAdapter->syncItems(m_list);
406void LauncherModel::refresh()
408 // First walk through all the existing items and see if we need to remove something
409 QList<LauncherItem*> toBeRemoved;
410 Q_FOREACH (LauncherItem* item, m_list) {
411 DesktopFileHandler desktopFile(item->appId());
412 if (!desktopFile.isValid()) {
413 // Desktop file not available for this app => drop it!
414 toBeRemoved << item;
415 } else if (!m_settings->storedApplications().contains(item->appId())) {
416 // Item not in settings any more => drop it!
417 toBeRemoved << item;
418 } else {
419 int idx = m_list.indexOf(item);
420 item->setName(desktopFile.displayName());
421 item->setPinned(item->pinned()); // update pinned text if needed
422 item->setRunning(item->running());
423 Q_EMIT dataChanged(index(idx), index(idx), {RoleName, RoleRunning});
425 const QString oldIcon = item->icon();
426 if (oldIcon == desktopFile.icon()) { // same icon file, perhaps different contents, simulate changing the icon name to force reload
427 item->setIcon(QString());
428 Q_EMIT dataChanged(index(idx), index(idx), {RoleIcon});
429 }
431 // now set the icon for real
432 item->setIcon(desktopFile.icon());
433 Q_EMIT dataChanged(index(idx), index(idx), {RoleIcon});
434 }
435 }
437 Q_FOREACH (LauncherItem* item, toBeRemoved) {
438 unpin(item->appId());
439 }
441 bool changed = toBeRemoved.count() > 0;
443 // This brings the Launcher into sync with the settings backend again. There's an issue though:
444 // If we can't find a .desktop file for an entry we need to skip it. That makes our settingsIndex
445 // go out of sync with the actual index of items. So let's also use an addedIndex which reflects
446 // the settingsIndex minus the skipped items.
447 int addedIndex = 0;
449 // Now walk through settings and see if we need to add something
450 for (int settingsIndex = 0; settingsIndex < m_settings->storedApplications().count(); ++settingsIndex) {
451 const QString entry = m_settings->storedApplications().at(settingsIndex);
452 int itemIndex = -1;
453 for (int i = 0; i < m_list.count(); ++i) {
454 if (m_list.at(i)->appId() == entry) {
455 itemIndex = i;
456 break;
457 }
458 }
460 if (itemIndex == -1) {
461 // Need to add it. Just add it into the addedIndex to keep same ordering as the list
462 // in the settings.
463 DesktopFileHandler desktopFile(entry);
464 if (!desktopFile.isValid()) {
465 qWarning() << "Couldn't find a .desktop file for" << entry << ". Skipping...";
466 continue;
467 }
469 LauncherItem *item = new LauncherItem(entry,
470 desktopFile.displayName(),
471 desktopFile.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 setAlerting(item->appId(), true);
505 Q_EMIT dataChanged(index(idx), index(idx), {RoleAlerting});
506 }
509void LauncherModel::applicationAdded(const QModelIndex &parent, int row)
511 Q_UNUSED(parent);
513 ApplicationInfoInterface *app = m_appManager->get(row);
514 if (!app) {
515 qWarning() << "LauncherModel received an applicationAdded signal, but there's no such application!";
516 return;
517 }
519 if (app->appId() == QLatin1String("unity8-dash")) {
520 // Not adding the dash app
521 return;
522 }
524 const int itemIndex = findApplication(app->appId());
525 if (itemIndex != -1) {
526 LauncherItem *item = m_list.at(itemIndex);
527 if (!item->recent()) {
528 item->setRecent(true);
529 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), {RoleRecent});
530 }
531 if (item->surfaceCount() != app->surfaceCount()) {
532 item->setSurfaceCount(app->surfaceList()->count());
533 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), {RoleSurfaceCount});
534 }
536 item->setRunning(true);
537 } else {
538 LauncherItem *item = new LauncherItem(app->appId(), app->name(), app->icon().toString(), this);
539 item->setRecent(true);
540 item->setRunning(true);
541 item->setFocused(app->focused());
542 item->setSurfaceCount(app->surfaceCount());
543 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
544 m_list.append(item);
545 endInsertRows();
546 }
547 connect(app, &ApplicationInfoInterface::surfaceCountChanged, this, &LauncherModel::applicationSurfaceCountChanged);
548 m_asAdapter->syncItems(m_list);
549 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), {RoleRunning});
552void LauncherModel::applicationSurfaceCountChanged(int count)
554 ApplicationInfoInterface *app = static_cast<ApplicationInfoInterface*>(sender());
555 int idx = findApplication(app->appId());
556 if (idx < 0) {
557 qWarning() << "Received a surface count changed event from an app that's not in the Launcher model";
558 return;
559 }
560 LauncherItem *item = m_list.at(idx);
561 if (item->surfaceCount() != count) {
562 item->setSurfaceCount(count);
563 Q_EMIT dataChanged(index(idx), index(idx), {RoleSurfaceCount});
564 }
567void LauncherModel::applicationRemoved(const QModelIndex &parent, int row)
569 Q_UNUSED(parent)
571 int appIndex = -1;
572 for (int i = 0; i < m_list.count(); ++i) {
573 if (m_list.at(i)->appId() == m_appManager->get(row)->appId()) {
574 appIndex = i;
575 break;
576 }
577 }
579 if (appIndex < 0) {
580 qWarning() << Q_FUNC_INFO << "appIndex not found";
581 return;
582 }
584 LauncherItem * item = m_list.at(appIndex);
585 item->setRunning(false);
587 if (!item->pinned()) {
588 beginRemoveRows(QModelIndex(), appIndex, appIndex);
589 m_list.takeAt(appIndex)->deleteLater();
590 endRemoveRows();
591 m_asAdapter->syncItems(m_list);
592 Q_EMIT dataChanged(index(appIndex), index(appIndex), {RolePinned});
593 }
594 Q_EMIT dataChanged(index(appIndex), index(appIndex), {RoleRunning});
597void LauncherModel::focusedAppIdChanged()
599 const QString appId = m_appManager->focusedApplicationId();
600 for (int i = 0; i < m_list.count(); ++i) {
601 LauncherItem *item = m_list.at(i);
602 if (!item->focused() && item->appId() == appId) {
603 item->setFocused(true);
604 Q_EMIT dataChanged(index(i), index(i), {RoleFocused});
605 } else if (item->focused() && item->appId() != appId) {
606 item->setFocused(false);
607 Q_EMIT dataChanged(index(i), index(i), {RoleFocused});
608 }
609 }