on a69ada0 help sign in
unity8/plugins/Unity/Launcher/launchermodel.cpp
636 lines 22 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 "dbusinterface.h"
24#include "asadapter.h"
26#include <ubuntu-app-launch/application.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),
41 m_ualRegistry(std::make_shared<ubuntu::app_launch::Registry>())
43 connect(m_dbusIface, &DBusInterface::countChanged, this, &LauncherModel::countChanged);
44 connect(m_dbusIface, &DBusInterface::countVisibleChanged, this, &LauncherModel::countVisibleChanged);
45 connect(m_dbusIface, &DBusInterface::progressChanged, this, &LauncherModel::progressChanged);
46 connect(m_dbusIface, &DBusInterface::refreshCalled, this, &LauncherModel::refresh);
47 connect(m_dbusIface, &DBusInterface::alertCalled, this, &LauncherModel::alert);
49 connect(m_settings, &GSettings::changed, this, &LauncherModel::refresh);
51 refresh();
54LauncherModel::~LauncherModel()
56 while (!m_list.empty()) {
57 m_list.takeFirst()->deleteLater();
58 }
60 delete m_asAdapter;
63int LauncherModel::rowCount(const QModelIndex &parent) const
65 Q_UNUSED(parent)
66 return m_list.count();
69QVariant LauncherModel::data(const QModelIndex &index, int role) const
71 LauncherItem *item = m_list.at(index.row());
72 switch(role) {
73 case RoleAppId:
74 return item->appId();
75 case RoleName:
76 return item->name();
77 case RoleIcon:
78 return item->icon();
79 case RolePinned:
80 return item->pinned();
81 case RoleCount:
82 return item->count();
83 case RoleCountVisible:
84 return item->countVisible();
85 case RoleProgress:
86 return item->progress();
87 case RoleFocused:
88 return item->focused();
89 case RoleAlerting:
90 return item->alerting();
91 case RoleRunning:
92 return item->running();
93 case RoleSurfaceCount:
94 return item->surfaceCount();
95 default:
96 qWarning() << Q_FUNC_INFO << "missing role, implement me";
97 return QVariant();
98 }
100 return QVariant();
103unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
105 if (index < 0 || index >= m_list.count()) {
106 return 0;
107 }
108 return m_list.at(index);
111void LauncherModel::move(int oldIndex, int newIndex)
113 // Make sure its not moved outside the lists
114 if (newIndex < 0) {
115 newIndex = 0;
116 }
117 if (newIndex >= m_list.count()) {
118 newIndex = m_list.count()-1;
119 }
121 // Nothing to do?
122 if (oldIndex == newIndex) {
123 return;
124 }
126 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
127 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
128 // adjust the model's index by +1 in case we're moving upwards
129 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
131 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
132 m_list.move(oldIndex, newIndex);
133 endMoveRows();
135 if (!m_list.at(newIndex)->pinned()) {
136 pin(m_list.at(newIndex)->appId());
137 } else {
138 storeAppList();
139 }
142void LauncherModel::pin(const QString &appId, int index)
144 int currentIndex = findApplication(appId);
146 if (currentIndex >= 0) {
147 if (index == -1 || index == currentIndex) {
148 m_list.at(currentIndex)->setPinned(true);
149 QModelIndex modelIndex = this->index(currentIndex);
150 Q_EMIT dataChanged(modelIndex, modelIndex, {RolePinned});
151 } else {
152 move(currentIndex, index);
153 // move() will store the list to the backend itself, so just exit at this point.
154 return;
155 }
156 } else {
157 if (index == -1) {
158 index = m_list.count();
159 }
161 auto appInfo = getApplicationInfo(appId);
162 if (!appInfo) {
163 qWarning() << "Can't pin application, appId not found:" << appId;
164 return;
165 }
167 beginInsertRows(QModelIndex(), index, index);
168 LauncherItem *item = new LauncherItem(appId,
169 QString::fromStdString(appInfo->info()->name()),
170 QString::fromStdString(appInfo->info()->iconPath()),
171 this);
172 item->setPinned(true);
173 m_list.insert(index, item);
174 endInsertRows();
175 }
177 storeAppList();
180void LauncherModel::requestRemove(const QString &appId)
182 unpin(appId);
183 storeAppList();
186void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
188 const int index = findApplication(appId);
189 if (index < 0) {
190 return;
191 }
193 LauncherItem *item = m_list.at(index);
194 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
195 if (model) {
196 const QString actionId = model->get(actionIndex).actionId();
198 // Check if this is one of the launcher actions we handle ourselves
199 if (actionId == QLatin1String("pin_item")) {
200 if (item->pinned()) {
201 requestRemove(appId);
202 } else {
203 pin(appId);
204 }
205 } else if (actionId == QLatin1String("launch_item")) {
206 QDesktopServices::openUrl(getUrlForAppId(appId));
207 } else if (actionId == QLatin1String("stop_item")) { // Quit
208 if (m_appManager) {
209 m_appManager->stopApplication(appId);
210 }
211 // Nope, we don't know this action, let the backend forward it to the application
212 } else {
213 // TODO: forward quicklist action to app, possibly via m_dbusIface
214 }
215 }
218void LauncherModel::setUser(const QString &username)
220 Q_UNUSED(username)
221 qWarning() << "This backend doesn't support multiple users";
224QString LauncherModel::getUrlForAppId(const QString &appId) const
226 // appId is either an appId or a legacy app name. Let's find out which
227 if (appId.isEmpty()) {
228 return QString();
229 }
231 if (!appId.contains('_')) {
232 return "application:///" + appId + ".desktop";
233 }
235 QStringList parts = appId.split('_');
236 QString package = parts.value(0);
237 QString app = parts.value(1, QStringLiteral("first-listed-app"));
238 return "appid://" + package + "/" + app + "/current-user-version";
241ApplicationManagerInterface *LauncherModel::applicationManager() const
243 return m_appManager;
246void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
248 // Is there already another appmanager set?
249 if (m_appManager) {
250 // Disconnect any signals
251 disconnect(this, &LauncherModel::applicationAdded, 0, nullptr);
252 disconnect(this, &LauncherModel::applicationRemoved, 0, nullptr);
253 disconnect(this, &LauncherModel::focusedAppIdChanged, 0, nullptr);
255 // remove any recent/running apps from the launcher
256 QList<int> recentAppIndices;
257 for (int i = 0; i < m_list.count(); ++i) {
258 if (m_list.at(i)->recent()) {
259 recentAppIndices << i;
260 }
261 }
262 int run = 0;
263 while (recentAppIndices.count() > 0) {
264 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
265 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
266 endRemoveRows();
267 recentAppIndices.takeFirst();
268 ++run;
269 }
270 }
272 m_appManager = appManager;
273 connect(m_appManager, &ApplicationManagerInterface::rowsInserted, this, &LauncherModel::applicationAdded);
274 connect(m_appManager, &ApplicationManagerInterface::rowsAboutToBeRemoved, this, &LauncherModel::applicationRemoved);
275 connect(m_appManager, &ApplicationManagerInterface::focusedApplicationIdChanged, this, &LauncherModel::focusedAppIdChanged);
277 Q_EMIT applicationManagerChanged();
279 for (int i = 0; i < appManager->count(); ++i) {
280 applicationAdded(QModelIndex(), i);
281 }
284bool LauncherModel::onlyPinned() const
286 return false;
289void LauncherModel::setOnlyPinned(bool onlyPinned) {
290 Q_UNUSED(onlyPinned);
291 qWarning() << "This launcher implementation does not support showing only pinned apps";
294void LauncherModel::storeAppList()
296 QStringList appIds;
297 Q_FOREACH(LauncherItem *item, m_list) {
298 if (item->pinned()) {
299 appIds << item->appId();
300 }
301 }
302 m_settings->setStoredApplications(appIds);
303 m_asAdapter->syncItems(m_list);
306void LauncherModel::unpin(const QString &appId)
308 const int index = findApplication(appId);
309 if (index < 0) {
310 return;
311 }
313 if (m_appManager->findApplication(appId)) {
314 if (m_list.at(index)->pinned()) {
315 m_list.at(index)->setPinned(false);
316 QModelIndex modelIndex = this->index(index);
317 Q_EMIT dataChanged(modelIndex, modelIndex, {RolePinned});
318 }
319 } else {
320 beginRemoveRows(QModelIndex(), index, index);
321 m_list.takeAt(index)->deleteLater();
322 endRemoveRows();
323 }
326int LauncherModel::findApplication(const QString &appId)
328 for (int i = 0; i < m_list.count(); ++i) {
329 LauncherItem *item = m_list.at(i);
330 if (item->appId() == appId) {
331 return i;
332 }
333 }
334 return -1;
337std::shared_ptr<ubuntu::app_launch::Application> LauncherModel::getApplicationInfo(const QString &appId)
339 auto ualAppId = ubuntu::app_launch::AppID::find(appId.toStdString());
340 if (ualAppId.empty()) {
341 return nullptr;
342 }
344 try
345 {
346 return ubuntu::app_launch::Application::create(ualAppId, m_ualRegistry);
347 }
348 catch (std::runtime_error &e)
349 {
350 qWarning() << "Couldn't find application info for" << appId << "-" << e.what();
351 return nullptr;
352 }
355void LauncherModel::progressChanged(const QString &appId, int progress)
357 const int idx = findApplication(appId);
358 if (idx >= 0) {
359 LauncherItem *item = m_list.at(idx);
360 item->setProgress(progress);
361 Q_EMIT dataChanged(index(idx), index(idx), {RoleProgress});
362 }
365void LauncherModel::countChanged(const QString &appId, int count)
367 const int idx = findApplication(appId);
368 if (idx >= 0) {
369 LauncherItem *item = m_list.at(idx);
370 item->setCount(count);
371 QVector<int> changedRoles = {RoleCount};
372 if (item->countVisible() && !item->alerting() && !item->focused()) {
373 changedRoles << RoleAlerting;
374 item->setAlerting(true);
375 }
376 m_asAdapter->syncItems(m_list);
377 Q_EMIT dataChanged(index(idx), index(idx), changedRoles);
378 }
381void LauncherModel::countVisibleChanged(const QString &appId, bool countVisible)
383 int idx = findApplication(appId);
384 if (idx >= 0) {
385 LauncherItem *item = m_list.at(idx);
386 item->setCountVisible(countVisible);
387 QVector<int> changedRoles = {RoleCountVisible};
388 if (countVisible && !item->alerting() && !item->focused()) {
389 changedRoles << RoleAlerting;
390 item->setAlerting(true);
391 }
392 Q_EMIT dataChanged(index(idx), index(idx), changedRoles);
394 // If countVisible goes to false, and the item is neither pinned nor recent we can drop it
395 if (!countVisible && !item->pinned() && !item->recent()) {
396 beginRemoveRows(QModelIndex(), idx, idx);
397 m_list.takeAt(idx)->deleteLater();
398 endRemoveRows();
399 }
400 } else {
401 // Need to create a new LauncherItem and show the highlight
402 auto appInfo = getApplicationInfo(appId);
403 if (countVisible && appInfo) {
404 LauncherItem *item = new LauncherItem(appId,
405 QString::fromStdString(appInfo->info()->name()),
406 QString::fromStdString(appInfo->info()->iconPath()),
407 this);
408 item->setCountVisible(true);
409 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
410 m_list.append(item);
411 endInsertRows();
412 }
413 }
414 m_asAdapter->syncItems(m_list);
417void LauncherModel::refresh()
419 // First walk through all the existing items and see if we need to remove something
420 QList<LauncherItem*> toBeRemoved;
421 Q_FOREACH (LauncherItem* item, m_list) {
422 auto appInfo = getApplicationInfo(item->appId());
423 if (!appInfo) {
424 // Application no longer available => drop it!
425 toBeRemoved << item;
426 } else if (!m_settings->storedApplications().contains(item->appId())) {
427 // Item not in settings any more => drop it!
428 toBeRemoved << item;
429 } else {
430 int idx = m_list.indexOf(item);
431 item->setName(QString::fromStdString(appInfo->info()->name()));
432 item->setPinned(item->pinned()); // update pinned text if needed
433 item->setRunning(item->running());
434 Q_EMIT dataChanged(index(idx), index(idx), {RoleName, RoleRunning});
436 const QString oldIcon = item->icon();
437 if (oldIcon == QString::fromStdString(appInfo->info()->iconPath())) { // same icon file, perhaps different contents, simulate changing the icon name to force reload
438 item->setIcon(QString());
439 Q_EMIT dataChanged(index(idx), index(idx), {RoleIcon});
440 }
442 // now set the icon for real
443 item->setIcon(QString::fromStdString(appInfo->info()->iconPath()));
444 Q_EMIT dataChanged(index(idx), index(idx), {RoleIcon});
445 }
446 }
448 Q_FOREACH (LauncherItem* item, toBeRemoved) {
449 unpin(item->appId());
450 }
452 bool changed = toBeRemoved.count() > 0;
454 // This brings the Launcher into sync with the settings backend again. There's an issue though:
455 // If we can't find a .desktop file for an entry we need to skip it. That makes our settingsIndex
456 // go out of sync with the actual index of items. So let's also use an addedIndex which reflects
457 // the settingsIndex minus the skipped items.
458 int addedIndex = 0;
460 // Now walk through settings and see if we need to add something
461 for (int settingsIndex = 0; settingsIndex < m_settings->storedApplications().count(); ++settingsIndex) {
462 const QString entry = m_settings->storedApplications().at(settingsIndex);
463 int itemIndex = -1;
464 for (int i = 0; i < m_list.count(); ++i) {
465 if (m_list.at(i)->appId() == entry) {
466 itemIndex = i;
467 break;
468 }
469 }
471 if (itemIndex == -1) {
472 // Need to add it. Just add it into the addedIndex to keep same ordering as the list
473 // in the settings.
474 auto appInfo = getApplicationInfo(entry);
475 if (!appInfo) {
476 continue;
477 }
479 LauncherItem *item = new LauncherItem(entry,
480 QString::fromStdString(appInfo->info()->name()),
481 QString::fromStdString(appInfo->info()->iconPath()),
482 this);
483 item->setPinned(true);
484 beginInsertRows(QModelIndex(), addedIndex, addedIndex);
485 m_list.insert(addedIndex, item);
486 endInsertRows();
487 changed = true;
488 } else if (itemIndex != addedIndex) {
489 // The item is already there, but it is in a different place than in the settings.
490 // Move it to the addedIndex
491 beginMoveRows(QModelIndex(), itemIndex, itemIndex, QModelIndex(), addedIndex);
492 m_list.move(itemIndex, addedIndex);
493 endMoveRows();
494 changed = true;
495 }
497 // Just like settingsIndex, this will increase with every item, except the ones we
498 // skipped with the "continue" call above.
499 addedIndex++;
500 }
502 if (changed) {
503 Q_EMIT hint();
504 }
506 m_asAdapter->syncItems(m_list);
509void LauncherModel::alert(const QString &appId)
511 int idx = findApplication(appId);
512 if (idx >= 0) {
513 LauncherItem *item = m_list.at(idx);
514 if (!item->focused() && !item->alerting()) {
515 item->setAlerting(true);
516 Q_EMIT dataChanged(index(idx), index(idx), {RoleAlerting});
517 }
518 }
521void LauncherModel::applicationAdded(const QModelIndex &parent, int row)
523 Q_UNUSED(parent);
525 ApplicationInfoInterface *app = m_appManager->get(row);
526 if (!app) {
527 qWarning() << "LauncherModel received an applicationAdded signal, but there's no such application!";
528 return;
529 }
531 if (app->appId() == QLatin1String("unity8-dash")) {
532 // Not adding the dash app
533 return;
534 }
536 const int itemIndex = findApplication(app->appId());
537 if (itemIndex != -1) {
538 LauncherItem *item = m_list.at(itemIndex);
539 if (!item->recent()) {
540 item->setRecent(true);
541 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), {RoleRecent});
542 }
543 if (item->surfaceCount() != app->surfaceCount()) {
544 item->setSurfaceCount(app->surfaceCount());
545 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), {RoleSurfaceCount});
546 }
548 item->setRunning(true);
549 } else {
550 LauncherItem *item = new LauncherItem(app->appId(), app->name(), app->icon().toString(), this);
551 item->setRecent(true);
552 item->setRunning(true);
553 item->setFocused(app->focused());
554 item->setSurfaceCount(app->surfaceCount());
555 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
556 m_list.append(item);
557 endInsertRows();
558 }
559 connect(app, &ApplicationInfoInterface::surfaceCountChanged, this, &LauncherModel::applicationSurfaceCountChanged);
560 m_asAdapter->syncItems(m_list);
561 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), {RoleRunning});
564void LauncherModel::applicationSurfaceCountChanged(int count)
566 ApplicationInfoInterface *app = static_cast<ApplicationInfoInterface*>(sender());
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 if (item->surfaceCount() != count) {
574 item->setSurfaceCount(count);
575 Q_EMIT dataChanged(index(idx), index(idx), {RoleSurfaceCount});
576 }
579void LauncherModel::applicationRemoved(const QModelIndex &parent, int row)
581 Q_UNUSED(parent)
583 ApplicationInfoInterface *app = m_appManager->get(row);
584 int appIndex = -1;
585 for (int i = 0; i < m_list.count(); ++i) {
586 if (m_list.at(i)->appId() == app->appId()) {
587 appIndex = i;
588 break;
589 }
590 }
592 if (appIndex < 0) {
593 qWarning() << Q_FUNC_INFO << "appIndex not found";
594 return;
595 }
597 disconnect(app, &ApplicationInfoInterface::surfaceCountChanged, this, &LauncherModel::applicationSurfaceCountChanged);
599 LauncherItem * item = m_list.at(appIndex);
601 if (!item->pinned()) {
602 beginRemoveRows(QModelIndex(), appIndex, appIndex);
603 m_list.takeAt(appIndex)->deleteLater();
604 endRemoveRows();
605 m_asAdapter->syncItems(m_list);
606 } else {
607 QVector<int> changedRoles = {RoleRunning};
608 item->setRunning(false);
609 if (item->focused()) {
610 changedRoles << RoleFocused;
611 item->setFocused(false);
612 }
613 Q_EMIT dataChanged(index(appIndex), index(appIndex), changedRoles);
614 }
617void LauncherModel::focusedAppIdChanged()
619 const QString appId = m_appManager->focusedApplicationId();
620 for (int i = 0; i < m_list.count(); ++i) {
621 LauncherItem *item = m_list.at(i);
622 if (!item->focused() && item->appId() == appId) {
623 QVector<int> changedRoles;
624 changedRoles << RoleFocused;
625 item->setFocused(true);
626 if (item->alerting()) {
627 changedRoles << RoleAlerting;
628 item->setAlerting(false);
629 }
630 Q_EMIT dataChanged(index(i), index(i), changedRoles);
631 } else if (item->focused() && item->appId() != appId) {
632 item->setFocused(false);
633 Q_EMIT dataChanged(index(i), index(i), {RoleFocused});
634 }
635 }