2 * Copyright 2013-2014 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 * Michael Zanetti <michael.zanetti@canonical.com>
20#include "launchermodel.h"
21#include "launcheritem.h"
23#include "dbusinterface.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>
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);
54LauncherModel::~LauncherModel()
56 while (!m_list.empty()) {
57 m_list.takeFirst()->deleteLater();
63int LauncherModel::rowCount(const QModelIndex &parent) const
66 return m_list.count();
69QVariant LauncherModel::data(const QModelIndex &index, int role) const
71 LauncherItem *item = m_list.at(index.row());
80 return item->pinned();
83 case RoleCountVisible:
84 return item->countVisible();
86 return item->progress();
88 return item->focused();
90 return item->alerting();
92 return item->running();
93 case RoleSurfaceCount:
94 return item->surfaceCount();
96 qWarning() << Q_FUNC_INFO << "missing role, implement me";
103unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
105 if (index < 0 || index >= m_list.count()) {
108 return m_list.at(index);
111void LauncherModel::move(int oldIndex, int newIndex)
113 // Make sure its not moved outside the lists
117 if (newIndex >= m_list.count()) {
118 newIndex = m_list.count()-1;
122 if (oldIndex == newIndex) {
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);
135 if (!m_list.at(newIndex)->pinned()) {
136 pin(m_list.at(newIndex)->appId());
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});
152 move(currentIndex, index);
153 // move() will store the list to the backend itself, so just exit at this point.
158 index = m_list.count();
161 auto appInfo = getApplicationInfo(appId);
163 qWarning() << "Can't pin application, appId not found:" << appId;
167 beginInsertRows(QModelIndex(), index, index);
168 LauncherItem *item = new LauncherItem(appId,
169 QString::fromStdString(appInfo->info()->name()),
170 QString::fromStdString(appInfo->info()->iconPath()),
172 item->setPinned(true);
173 m_list.insert(index, item);
180void LauncherModel::requestRemove(const QString &appId)
186void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
188 const int index = findApplication(appId);
193 LauncherItem *item = m_list.at(index);
194 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
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);
205 } else if (actionId == QLatin1String("launch_item")) {
206 QDesktopServices::openUrl(getUrlForAppId(appId));
207 } else if (actionId == QLatin1String("stop_item")) { // Quit
209 m_appManager->stopApplication(appId);
211 // Nope, we don't know this action, let the backend forward it to the application
213 // TODO: forward quicklist action to app, possibly via m_dbusIface
218void LauncherModel::setUser(const QString &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()) {
231 if (!appId.contains('_')) {
232 return "application:///" + appId + ".desktop";
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
246void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
248 // Is there already another appmanager set?
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;
263 while (recentAppIndices.count() > 0) {
264 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
265 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
267 recentAppIndices.takeFirst();
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);
284bool LauncherModel::onlyPinned() const
289void LauncherModel::setOnlyPinned(bool onlyPinned) {
290 Q_UNUSED(onlyPinned);
291 qWarning() << "This launcher implementation does not support showing only pinned apps";
294void LauncherModel::storeAppList()
297 Q_FOREACH(LauncherItem *item, m_list) {
298 if (item->pinned()) {
299 appIds << item->appId();
302 m_settings->setStoredApplications(appIds);
303 m_asAdapter->syncItems(m_list);
306void LauncherModel::unpin(const QString &appId)
308 const int index = findApplication(appId);
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});
320 beginRemoveRows(QModelIndex(), index, index);
321 m_list.takeAt(index)->deleteLater();
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) {
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()) {
346 return ubuntu::app_launch::Application::create(ualAppId, m_ualRegistry);
348 catch (std::runtime_error &e)
350 qWarning() << "Couldn't find application info for" << appId << "-" << e.what();
355void LauncherModel::progressChanged(const QString &appId, int progress)
357 const int idx = findApplication(appId);
359 LauncherItem *item = m_list.at(idx);
360 item->setProgress(progress);
361 Q_EMIT dataChanged(index(idx), index(idx), {RoleProgress});
365void LauncherModel::countChanged(const QString &appId, int count)
367 const int idx = findApplication(appId);
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);
376 m_asAdapter->syncItems(m_list);
377 Q_EMIT dataChanged(index(idx), index(idx), changedRoles);
381void LauncherModel::countVisibleChanged(const QString &appId, bool countVisible)
383 int idx = findApplication(appId);
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);
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();
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()),
408 item->setCountVisible(true);
409 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
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());
424 // Application no longer available => drop it!
426 } else if (!m_settings->storedApplications().contains(item->appId())) {
427 // Item not in settings any more => drop it!
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});
442 // now set the icon for real
443 item->setIcon(QString::fromStdString(appInfo->info()->iconPath()));
444 Q_EMIT dataChanged(index(idx), index(idx), {RoleIcon});
448 Q_FOREACH (LauncherItem* item, toBeRemoved) {
449 unpin(item->appId());
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.
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);
464 for (int i = 0; i < m_list.count(); ++i) {
465 if (m_list.at(i)->appId() == entry) {
471 if (itemIndex == -1) {
472 // Need to add it. Just add it into the addedIndex to keep same ordering as the list
474 auto appInfo = getApplicationInfo(entry);
479 LauncherItem *item = new LauncherItem(entry,
480 QString::fromStdString(appInfo->info()->name()),
481 QString::fromStdString(appInfo->info()->iconPath()),
483 item->setPinned(true);
484 beginInsertRows(QModelIndex(), addedIndex, addedIndex);
485 m_list.insert(addedIndex, item);
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);
497 // Just like settingsIndex, this will increase with every item, except the ones we
498 // skipped with the "continue" call above.
506 m_asAdapter->syncItems(m_list);
509void LauncherModel::alert(const QString &appId)
511 int idx = findApplication(appId);
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});
521void LauncherModel::applicationAdded(const QModelIndex &parent, int row)
525 ApplicationInfoInterface *app = m_appManager->get(row);
527 qWarning() << "LauncherModel received an applicationAdded signal, but there's no such application!";
531 if (app->appId() == QLatin1String("unity8-dash")) {
532 // Not adding the dash app
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});
543 if (item->surfaceCount() != app->surfaceCount()) {
544 item->setSurfaceCount(app->surfaceCount());
545 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), {RoleSurfaceCount});
548 item->setRunning(true);
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());
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());
569 qWarning() << "Received a surface count changed event from an app that's not in the Launcher model";
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});
579void LauncherModel::applicationRemoved(const QModelIndex &parent, int row)
583 ApplicationInfoInterface *app = m_appManager->get(row);
585 for (int i = 0; i < m_list.count(); ++i) {
586 if (m_list.at(i)->appId() == app->appId()) {
593 qWarning() << Q_FUNC_INFO << "appIndex not found";
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();
605 m_asAdapter->syncItems(m_list);
607 QVector<int> changedRoles = {RoleRunning};
608 item->setRunning(false);
609 if (item->focused()) {
610 changedRoles << RoleFocused;
611 item->setFocused(false);
613 Q_EMIT dataChanged(index(appIndex), index(appIndex), changedRoles);
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);
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});
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