2 * Copyright 2013-2016 Canonical Ltd.
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.
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.
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/>.
17#include "launchermodel.h"
18#include "launcheritem.h"
20#include "dbusinterface.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>
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);
52LauncherModel::~LauncherModel()
54 while (!m_list.empty()) {
55 m_list.takeFirst()->deleteLater();
61int LauncherModel::rowCount(const QModelIndex &parent) const
64 return m_list.count();
67QVariant LauncherModel::data(const QModelIndex &index, int role) const
69 LauncherItem *item = m_list.at(index.row());
78 return item->pinned();
81 case RoleCountVisible:
82 return item->countVisible();
84 return item->progress();
86 return item->focused();
88 return item->alerting();
90 return item->running();
91 case RoleSurfaceCount:
92 return item->surfaceCount();
94 qWarning() << Q_FUNC_INFO << "missing role, implement me";
101unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
103 if (index < 0 || index >= m_list.count()) {
106 return m_list.at(index);
109void LauncherModel::move(int oldIndex, int newIndex)
111 // Make sure its not moved outside the lists
115 if (newIndex >= m_list.count()) {
116 newIndex = m_list.count()-1;
120 if (oldIndex == newIndex) {
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);
133 if (!m_list.at(newIndex)->pinned()) {
134 pin(m_list.at(newIndex)->appId());
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});
150 move(currentIndex, index);
151 // move() will store the list to the backend itself, so just exit at this point.
156 index = m_list.count();
159 auto appInfo = getApplicationInfo(appId);
161 qWarning() << "Can't pin application, appId not found:" << appId;
165 beginInsertRows(QModelIndex(), index, index);
166 LauncherItem *item = new LauncherItem(appId,
167 QString::fromStdString(appInfo->info()->name()),
168 QString::fromStdString(appInfo->info()->iconPath()),
170 item->setPinned(true);
171 m_list.insert(index, item);
178void LauncherModel::requestRemove(const QString &appId)
184void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
186 const int index = findApplication(appId);
191 LauncherItem *item = m_list.at(index);
192 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
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);
203 } else if (actionId == QLatin1String("launch_item")) {
204 QDesktopServices::openUrl(getUrlForAppId(appId));
205 } else if (actionId == QLatin1String("stop_item")) { // Quit
207 m_appManager->stopApplication(appId);
209 // Nope, we don't know this action, let the backend forward it to the application
211 // TODO: forward quicklist action to app, possibly via m_dbusIface
216void LauncherModel::setUser(const QString &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()) {
229 if (!appId.contains('_')) {
230 return "application:///" + appId + ".desktop";
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
244void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
246 // Is there already another appmanager set?
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;
261 while (recentAppIndices.count() > 0) {
262 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
263 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
265 recentAppIndices.takeFirst();
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);
282bool LauncherModel::onlyPinned() const
287void LauncherModel::setOnlyPinned(bool onlyPinned) {
288 Q_UNUSED(onlyPinned);
289 qWarning() << "This launcher implementation does not support showing only pinned apps";
292void LauncherModel::storeAppList()
295 Q_FOREACH(LauncherItem *item, m_list) {
296 if (item->pinned()) {
297 appIds << item->appId();
300 m_settings->setStoredApplications(appIds);
301 m_asAdapter->syncItems(m_list);
304void LauncherModel::unpin(const QString &appId)
306 const int index = findApplication(appId);
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});
318 beginRemoveRows(QModelIndex(), index, index);
319 m_list.takeAt(index)->deleteLater();
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) {
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()) {
344 return ubuntu::app_launch::Application::create(ualAppId, m_ualRegistry);
346 catch (std::runtime_error &e)
348 qWarning() << "Couldn't find application info for" << appId << "-" << e.what();
353void LauncherModel::progressChanged(const QString &appId, int progress)
355 const int idx = findApplication(appId);
357 LauncherItem *item = m_list.at(idx);
358 item->setProgress(progress);
359 Q_EMIT dataChanged(index(idx), index(idx), {RoleProgress});
363void LauncherModel::countChanged(const QString &appId, int count)
365 const int idx = findApplication(appId);
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);
374 m_asAdapter->syncItems(m_list);
375 Q_EMIT dataChanged(index(idx), index(idx), changedRoles);
379void LauncherModel::countVisibleChanged(const QString &appId, bool countVisible)
381 int idx = findApplication(appId);
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);
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();
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()),
406 item->setCountVisible(true);
407 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
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());
422 // Application no longer available => drop it!
424 } else if (!m_settings->storedApplications().contains(item->appId())) {
425 // Item not in settings any more => drop it!
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});
440 // now set the icon for real
441 item->setIcon(QString::fromStdString(appInfo->info()->iconPath()));
442 Q_EMIT dataChanged(index(idx), index(idx), {RoleIcon});
446 Q_FOREACH (LauncherItem* item, toBeRemoved) {
447 unpin(item->appId());
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.
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);
462 for (int i = 0; i < m_list.count(); ++i) {
463 if (m_list.at(i)->appId() == entry) {
469 if (itemIndex == -1) {
470 // Need to add it. Just add it into the addedIndex to keep same ordering as the list
472 auto appInfo = getApplicationInfo(entry);
477 LauncherItem *item = new LauncherItem(entry,
478 QString::fromStdString(appInfo->info()->name()),
479 QString::fromStdString(appInfo->info()->iconPath()),
481 item->setPinned(true);
482 beginInsertRows(QModelIndex(), addedIndex, addedIndex);
483 m_list.insert(addedIndex, item);
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);
495 // Just like settingsIndex, this will increase with every item, except the ones we
496 // skipped with the "continue" call above.
504 m_asAdapter->syncItems(m_list);
507void LauncherModel::alert(const QString &appId)
509 int idx = findApplication(appId);
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});
519void LauncherModel::applicationAdded(const QModelIndex &parent, int row)
523 ApplicationInfoInterface *app = m_appManager->get(row);
525 qWarning() << "LauncherModel received an applicationAdded signal, but there's no such application!";
529 if (app->appId() == QLatin1String("unity8-dash")) {
530 // Not adding the dash app
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});
541 if (item->surfaceCount() != app->surfaceCount()) {
542 item->setSurfaceCount(app->surfaceCount());
543 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), {RoleSurfaceCount});
546 item->setRunning(true);
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());
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());
567 qWarning() << "Received a surface count changed event from an app that's not in the Launcher model";
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});
577void LauncherModel::applicationRemoved(const QModelIndex &parent, int row)
581 ApplicationInfoInterface *app = m_appManager->get(row);
583 for (int i = 0; i < m_list.count(); ++i) {
584 if (m_list.at(i)->appId() == app->appId()) {
591 qWarning() << Q_FUNC_INFO << "appIndex not found";
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();
603 m_asAdapter->syncItems(m_list);
605 QVector<int> changedRoles = {RoleRunning};
606 item->setRunning(false);
607 if (item->focused()) {
608 changedRoles << RoleFocused;
609 item->setFocused(false);
611 Q_EMIT dataChanged(index(appIndex), index(appIndex), changedRoles);
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);
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});
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list