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