on cd74009 help sign in
unity8/plugins/Unity/Launcher/launchermodel.cpp
645 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"
23#include <ubuntu-app-launch/appid.h>
24#include <ubuntu-app-launch/application.h>
25#include <ubuntu-app-launch/registry.h>
26#include <unity/shell/application/ApplicationInfoInterface.h>
27#include <unity/shell/application/MirSurfaceListInterface.h>
29#include <QDesktopServices>
30#include <QDebug>
32namespace ual = ubuntu::app_launch;
34using namespace unity::shell::application;
36LauncherModel::LauncherModel(QObject *parent):
37 LauncherModelInterface(parent),
38 m_settings(new GSettings(this)),
39 m_dbusIface(new DBusInterface(this)),
40 m_asAdapter(new ASAdapter()),
41 m_appManager(nullptr),
42 m_ualRegistry(std::make_shared<ual::Registry>())
44 connect(m_dbusIface, &DBusInterface::countChanged, this, &LauncherModel::countChanged);
45 connect(m_dbusIface, &DBusInterface::countVisibleChanged, this, &LauncherModel::countVisibleChanged);
46 connect(m_dbusIface, &DBusInterface::progressChanged, this, &LauncherModel::progressChanged);
47 connect(m_dbusIface, &DBusInterface::refreshCalled, this, &LauncherModel::refresh);
48 connect(m_dbusIface, &DBusInterface::alertCalled, this, &LauncherModel::alert);
50 connect(m_settings, &GSettings::changed, this, &LauncherModel::refresh);
52 refresh();
55LauncherModel::~LauncherModel()
57 while (!m_list.empty()) {
58 m_list.takeFirst()->deleteLater();
59 }
61 delete m_asAdapter;
64int LauncherModel::rowCount(const QModelIndex &parent) const
66 Q_UNUSED(parent)
67 return m_list.count();
70QVariant LauncherModel::data(const QModelIndex &index, int role) const
72 LauncherItem *item = m_list.at(index.row());
73 switch(role) {
74 case RoleAppId:
75 return item->appId();
76 case RoleName:
77 return item->name();
78 case RoleIcon:
79 return item->icon();
80 case RolePinned:
81 return item->pinned();
82 case RoleCount:
83 return item->count();
84 case RoleCountVisible:
85 return item->countVisible();
86 case RoleProgress:
87 return item->progress();
88 case RoleFocused:
89 return item->focused();
90 case RoleAlerting:
91 return item->alerting();
92 case RoleRunning:
93 return item->running();
94 case RoleSurfaceCount:
95 return item->surfaceCount();
96 default:
97 qWarning() << Q_FUNC_INFO << "missing role, implement me";
98 return QVariant();
99 }
101 return QVariant();
104unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
106 if (index < 0 || index >= m_list.count()) {
107 return 0;
108 }
109 return m_list.at(index);
112void LauncherModel::move(int oldIndex, int newIndex)
114 // Make sure its not moved outside the lists
115 if (newIndex < 0) {
116 newIndex = 0;
117 }
118 if (newIndex >= m_list.count()) {
119 newIndex = m_list.count()-1;
120 }
122 // Nothing to do?
123 if (oldIndex == newIndex) {
124 return;
125 }
127 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
128 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
129 // adjust the model's index by +1 in case we're moving upwards
130 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
132 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
133 m_list.move(oldIndex, newIndex);
134 endMoveRows();
136 if (!m_list.at(newIndex)->pinned()) {
137 pin(m_list.at(newIndex)->appId());
138 } else {
139 storeAppList();
140 }
143void LauncherModel::pin(const QString &appId, int index)
145 int currentIndex = findApplication(appId);
147 if (currentIndex >= 0) {
148 if (index == -1 || index == currentIndex) {
149 m_list.at(currentIndex)->setPinned(true);
150 QModelIndex modelIndex = this->index(currentIndex);
151 Q_EMIT dataChanged(modelIndex, modelIndex, {RolePinned});
152 } else {
153 move(currentIndex, index);
154 // move() will store the list to the backend itself, so just exit at this point.
155 return;
156 }
157 } else {
158 if (index == -1) {
159 index = m_list.count();
160 }
162 auto appInfo = getApplicationInfo(appId);
163 if (!appInfo.valid) {
164 qWarning() << "Can't pin application, appId not found:" << appId;
165 return;
166 }
168 beginInsertRows(QModelIndex(), index, index);
169 LauncherItem *item = new LauncherItem(appId,
170 appInfo.name,
171 appInfo.icon,
172 this);
173 item->setPinned(true);
174 m_list.insert(index, item);
175 endInsertRows();
176 }
178 storeAppList();
181void LauncherModel::requestRemove(const QString &appId)
183 unpin(appId);
184 storeAppList();
187void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
189 const int index = findApplication(appId);
190 if (index < 0) {
191 return;
192 }
194 LauncherItem *item = m_list.at(index);
195 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
196 if (model) {
197 const QString actionId = model->get(actionIndex).actionId();
199 // Check if this is one of the launcher actions we handle ourselves
200 if (actionId == QLatin1String("pin_item")) {
201 if (item->pinned()) {
202 requestRemove(appId);
203 } else {
204 pin(appId);
205 }
206 } else if (actionId == QLatin1String("launch_item")) {
207 QDesktopServices::openUrl(getUrlForAppId(appId));
208 } else if (actionId == QLatin1String("stop_item")) { // Quit
209 if (m_appManager) {
210 m_appManager->stopApplication(appId);
211 }
212 // Nope, we don't know this action, let the backend forward it to the application
213 } else {
214 // TODO: forward quicklist action to app, possibly via m_dbusIface
215 }
216 }
219void LauncherModel::setUser(const QString &username)
221 Q_UNUSED(username)
222 qWarning() << "This backend doesn't support multiple users";
225QString LauncherModel::getUrlForAppId(const QString &appId) const
227 // appId is either an appId or a legacy app name. Let's find out which
228 if (appId.isEmpty()) {
229 return QString();
230 }
232 if (!appId.contains('_')) {
233 return "application:///" + appId + ".desktop";
234 }
236 QStringList parts = appId.split('_');
237 QString package = parts.value(0);
238 QString app = parts.value(1, QStringLiteral("first-listed-app"));
239 return "appid://" + package + "/" + app + "/current-user-version";
242ApplicationManagerInterface *LauncherModel::applicationManager() const
244 return m_appManager;
247void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
249 // Is there already another appmanager set?
250 if (m_appManager) {
251 // Disconnect any signals
252 disconnect(this, &LauncherModel::applicationAdded, 0, nullptr);
253 disconnect(this, &LauncherModel::applicationRemoved, 0, nullptr);
254 disconnect(this, &LauncherModel::focusedAppIdChanged, 0, nullptr);
256 // remove any recent/running apps from the launcher
257 QList<int> recentAppIndices;
258 for (int i = 0; i < m_list.count(); ++i) {
259 if (m_list.at(i)->recent()) {
260 recentAppIndices << i;
261 }
262 }
263 int run = 0;
264 while (recentAppIndices.count() > 0) {
265 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
266 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
267 endRemoveRows();
268 recentAppIndices.takeFirst();
269 ++run;
270 }
271 }
273 m_appManager = appManager;
274 connect(m_appManager, &ApplicationManagerInterface::rowsInserted, this, &LauncherModel::applicationAdded);
275 connect(m_appManager, &ApplicationManagerInterface::rowsAboutToBeRemoved, this, &LauncherModel::applicationRemoved);
276 connect(m_appManager, &ApplicationManagerInterface::focusedApplicationIdChanged, this, &LauncherModel::focusedAppIdChanged);
278 Q_EMIT applicationManagerChanged();
280 for (int i = 0; i < appManager->count(); ++i) {
281 applicationAdded(QModelIndex(), i);
282 }
285bool LauncherModel::onlyPinned() const
287 return false;
290void LauncherModel::setOnlyPinned(bool onlyPinned) {
291 Q_UNUSED(onlyPinned);
292 qWarning() << "This launcher implementation does not support showing only pinned apps";
295void LauncherModel::storeAppList()
297 QStringList appIds;
298 Q_FOREACH(LauncherItem *item, m_list) {
299 if (item->pinned()) {
300 appIds << item->appId();
301 }
302 }
303 m_settings->setStoredApplications(appIds);
304 m_asAdapter->syncItems(m_list);
307void LauncherModel::unpin(const QString &appId)
309 const int index = findApplication(appId);
310 if (index < 0) {
311 return;
312 }
314 if (m_appManager->findApplication(appId)) {
315 if (m_list.at(index)->pinned()) {
316 m_list.at(index)->setPinned(false);
317 QModelIndex modelIndex = this->index(index);
318 Q_EMIT dataChanged(modelIndex, modelIndex, {RolePinned});
319 }
320 } else {
321 beginRemoveRows(QModelIndex(), index, index);
322 m_list.takeAt(index)->deleteLater();
323 endRemoveRows();
324 }
327int LauncherModel::findApplication(const QString &appId)
329 for (int i = 0; i < m_list.count(); ++i) {
330 LauncherItem *item = m_list.at(i);
331 if (item->appId() == appId) {
332 return i;
333 }
334 }
335 return -1;
338LauncherModel::AppInfo LauncherModel::getApplicationInfo(const QString &appId)
340 AppInfo info;
342 ual::AppID ualAppId = ual::AppID::find(m_ualRegistry, appId.toStdString());
343 if (ualAppId.empty()) {
344 return info;
345 }
347 std::shared_ptr<ual::Application> ualApp;
348 try
349 {
350 ualApp = ual::Application::create(ualAppId, m_ualRegistry);
351 }
352 catch (std::runtime_error &e)
353 {
354 qWarning() << "Couldn't find application info for" << appId << "-" << e.what();
355 return info;
356 }
358 info.valid = true;
359 info.name = QString::fromStdString(ualApp->info()->name());
360 info.icon = QString::fromStdString(ualApp->info()->iconPath());
361 return info;
364void LauncherModel::progressChanged(const QString &appId, int progress)
366 const int idx = findApplication(appId);
367 if (idx >= 0) {
368 LauncherItem *item = m_list.at(idx);
369 item->setProgress(progress);
370 Q_EMIT dataChanged(index(idx), index(idx), {RoleProgress});
371 }
374void LauncherModel::countChanged(const QString &appId, int count)
376 const int idx = findApplication(appId);
377 if (idx >= 0) {
378 LauncherItem *item = m_list.at(idx);
379 item->setCount(count);
380 QVector<int> changedRoles = {RoleCount};
381 if (item->countVisible() && !item->alerting() && !item->focused()) {
382 changedRoles << RoleAlerting;
383 item->setAlerting(true);
384 }
385 m_asAdapter->syncItems(m_list);
386 Q_EMIT dataChanged(index(idx), index(idx), changedRoles);
387 }
390void LauncherModel::countVisibleChanged(const QString &appId, bool countVisible)
392 int idx = findApplication(appId);
393 if (idx >= 0) {
394 LauncherItem *item = m_list.at(idx);
395 item->setCountVisible(countVisible);
396 QVector<int> changedRoles = {RoleCountVisible};
397 if (countVisible && !item->alerting() && !item->focused()) {
398 changedRoles << RoleAlerting;
399 item->setAlerting(true);
400 }
401 Q_EMIT dataChanged(index(idx), index(idx), changedRoles);
403 // If countVisible goes to false, and the item is neither pinned nor recent we can drop it
404 if (!countVisible && !item->pinned() && !item->recent()) {
405 beginRemoveRows(QModelIndex(), idx, idx);
406 m_list.takeAt(idx)->deleteLater();
407 endRemoveRows();
408 }
409 } else {
410 // Need to create a new LauncherItem and show the highlight
411 auto appInfo = getApplicationInfo(appId);
412 if (countVisible && appInfo.valid) {
413 LauncherItem *item = new LauncherItem(appId,
414 appInfo.name,
415 appInfo.icon,
416 this);
417 item->setCountVisible(true);
418 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
419 m_list.append(item);
420 endInsertRows();
421 }
422 }
423 m_asAdapter->syncItems(m_list);
426void LauncherModel::refresh()
428 // First walk through all the existing items and see if we need to remove something
429 QList<LauncherItem*> toBeRemoved;
430 Q_FOREACH (LauncherItem* item, m_list) {
431 auto appInfo = getApplicationInfo(item->appId());
432 if (!appInfo.valid) {
433 // Application no longer available => drop it!
434 toBeRemoved << item;
435 } else if (!m_settings->storedApplications().contains(item->appId())) {
436 // Item not in settings any more => drop it!
437 toBeRemoved << item;
438 } else {
439 int idx = m_list.indexOf(item);
440 item->setName(appInfo.name);
441 item->setPinned(item->pinned()); // update pinned text if needed
442 item->setRunning(item->running());
443 Q_EMIT dataChanged(index(idx), index(idx), {RoleName, RoleRunning});
445 const QString oldIcon = item->icon();
446 if (oldIcon == appInfo.icon) { // same icon file, perhaps different contents, simulate changing the icon name to force reload
447 item->setIcon(QString());
448 Q_EMIT dataChanged(index(idx), index(idx), {RoleIcon});
449 }
451 // now set the icon for real
452 item->setIcon(appInfo.icon);
453 Q_EMIT dataChanged(index(idx), index(idx), {RoleIcon});
454 }
455 }
457 Q_FOREACH (LauncherItem* item, toBeRemoved) {
458 unpin(item->appId());
459 }
461 bool changed = toBeRemoved.count() > 0;
463 // This brings the Launcher into sync with the settings backend again. There's an issue though:
464 // If we can't find a .desktop file for an entry we need to skip it. That makes our settingsIndex
465 // go out of sync with the actual index of items. So let's also use an addedIndex which reflects
466 // the settingsIndex minus the skipped items.
467 int addedIndex = 0;
469 // Now walk through settings and see if we need to add something
470 for (int settingsIndex = 0; settingsIndex < m_settings->storedApplications().count(); ++settingsIndex) {
471 const QString entry = m_settings->storedApplications().at(settingsIndex);
472 int itemIndex = -1;
473 for (int i = 0; i < m_list.count(); ++i) {
474 if (m_list.at(i)->appId() == entry) {
475 itemIndex = i;
476 break;
477 }
478 }
480 if (itemIndex == -1) {
481 // Need to add it. Just add it into the addedIndex to keep same ordering as the list
482 // in the settings.
483 auto appInfo = getApplicationInfo(entry);
484 if (!appInfo.valid) {
485 continue;
486 }
488 LauncherItem *item = new LauncherItem(entry,
489 appInfo.name,
490 appInfo.icon,
491 this);
492 item->setPinned(true);
493 beginInsertRows(QModelIndex(), addedIndex, addedIndex);
494 m_list.insert(addedIndex, item);
495 endInsertRows();
496 changed = true;
497 } else if (itemIndex != addedIndex) {
498 // The item is already there, but it is in a different place than in the settings.
499 // Move it to the addedIndex
500 beginMoveRows(QModelIndex(), itemIndex, itemIndex, QModelIndex(), addedIndex);
501 m_list.move(itemIndex, addedIndex);
502 endMoveRows();
503 changed = true;
504 }
506 // Just like settingsIndex, this will increase with every item, except the ones we
507 // skipped with the "continue" call above.
508 addedIndex++;
509 }
511 if (changed) {
512 Q_EMIT hint();
513 }
515 m_asAdapter->syncItems(m_list);
518void LauncherModel::alert(const QString &appId)
520 int idx = findApplication(appId);
521 if (idx >= 0) {
522 LauncherItem *item = m_list.at(idx);
523 if (!item->focused() && !item->alerting()) {
524 item->setAlerting(true);
525 Q_EMIT dataChanged(index(idx), index(idx), {RoleAlerting});
526 }
527 }
530void LauncherModel::applicationAdded(const QModelIndex &parent, int row)
532 Q_UNUSED(parent);
534 ApplicationInfoInterface *app = m_appManager->get(row);
535 if (!app) {
536 qWarning() << "LauncherModel received an applicationAdded signal, but there's no such application!";
537 return;
538 }
540 if (app->appId() == QLatin1String("unity8-dash")) {
541 // Not adding the dash app
542 return;
543 }
545 const int itemIndex = findApplication(app->appId());
546 if (itemIndex != -1) {
547 LauncherItem *item = m_list.at(itemIndex);
548 if (!item->recent()) {
549 item->setRecent(true);
550 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), {RoleRecent});
551 }
552 if (item->surfaceCount() != app->surfaceCount()) {
553 item->setSurfaceCount(app->surfaceCount());
554 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), {RoleSurfaceCount});
555 }
557 item->setRunning(true);
558 } else {
559 LauncherItem *item = new LauncherItem(app->appId(), app->name(), app->icon().toString(), this);
560 item->setRecent(true);
561 item->setRunning(true);
562 item->setFocused(app->focused());
563 item->setSurfaceCount(app->surfaceCount());
564 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
565 m_list.append(item);
566 endInsertRows();
567 }
568 connect(app, &ApplicationInfoInterface::surfaceCountChanged, this, &LauncherModel::applicationSurfaceCountChanged);
569 m_asAdapter->syncItems(m_list);
570 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), {RoleRunning});
573void LauncherModel::applicationSurfaceCountChanged(int count)
575 ApplicationInfoInterface *app = static_cast<ApplicationInfoInterface*>(sender());
576 int idx = findApplication(app->appId());
577 if (idx < 0) {
578 qWarning() << "Received a surface count changed event from an app that's not in the Launcher model";
579 return;
580 }
581 LauncherItem *item = m_list.at(idx);
582 if (item->surfaceCount() != count) {
583 item->setSurfaceCount(count);
584 Q_EMIT dataChanged(index(idx), index(idx), {RoleSurfaceCount});
585 }
588void LauncherModel::applicationRemoved(const QModelIndex &parent, int row)
590 Q_UNUSED(parent)
592 ApplicationInfoInterface *app = m_appManager->get(row);
593 int appIndex = -1;
594 for (int i = 0; i < m_list.count(); ++i) {
595 if (m_list.at(i)->appId() == app->appId()) {
596 appIndex = i;
597 break;
598 }
599 }
601 if (appIndex < 0) {
602 qWarning() << Q_FUNC_INFO << "appIndex not found";
603 return;
604 }
606 disconnect(app, &ApplicationInfoInterface::surfaceCountChanged, this, &LauncherModel::applicationSurfaceCountChanged);
608 LauncherItem * item = m_list.at(appIndex);
610 if (!item->pinned()) {
611 beginRemoveRows(QModelIndex(), appIndex, appIndex);
612 m_list.takeAt(appIndex)->deleteLater();
613 endRemoveRows();
614 m_asAdapter->syncItems(m_list);
615 } else {
616 QVector<int> changedRoles = {RoleRunning};
617 item->setRunning(false);
618 if (item->focused()) {
619 changedRoles << RoleFocused;
620 item->setFocused(false);
621 }
622 Q_EMIT dataChanged(index(appIndex), index(appIndex), changedRoles);
623 }
626void LauncherModel::focusedAppIdChanged()
628 const QString appId = m_appManager->focusedApplicationId();
629 for (int i = 0; i < m_list.count(); ++i) {
630 LauncherItem *item = m_list.at(i);
631 if (!item->focused() && item->appId() == appId) {
632 QVector<int> changedRoles;
633 changedRoles << RoleFocused;
634 item->setFocused(true);
635 if (item->alerting()) {
636 changedRoles << RoleAlerting;
637 item->setAlerting(false);
638 }
639 Q_EMIT dataChanged(index(i), index(i), changedRoles);
640 } else if (item->focused() && item->appId() != appId) {
641 item->setFocused(false);
642 Q_EMIT dataChanged(index(i), index(i), {RoleFocused});
643 }
644 }