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"
22#include "ualwrapper.h"
24#include <unity/shell/application/ApplicationInfoInterface.h>
25#include <unity/shell/application/MirSurfaceListInterface.h>
26#include <unity/shell/application/MirSurfaceInterface.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()),
40 connect(m_dbusIface, &DBusInterface::countChanged, this, &LauncherModel::countChanged);
41 connect(m_dbusIface, &DBusInterface::countVisibleChanged, this, &LauncherModel::countVisibleChanged);
42 connect(m_dbusIface, &DBusInterface::progressChanged, this, &LauncherModel::progressChanged);
43 connect(m_dbusIface, &DBusInterface::refreshCalled, this, &LauncherModel::refresh);
44 connect(m_dbusIface, &DBusInterface::alertCalled, this, &LauncherModel::alert);
46 connect(m_settings, &GSettings::changed, this, &LauncherModel::refresh);
51LauncherModel::~LauncherModel()
53 while (!m_list.empty()) {
54 m_list.takeFirst()->deleteLater();
60int LauncherModel::rowCount(const QModelIndex &parent) const
63 return m_list.count();
66QVariant LauncherModel::data(const QModelIndex &index, int role) const
68 LauncherItem *item = m_list.at(index.row());
77 return item->pinned();
80 case RoleCountVisible:
81 return item->countVisible();
83 return item->progress();
85 return item->focused();
87 return item->alerting();
89 return item->running();
90 case RoleSurfaceCount:
91 return item->surfaceCount();
93 qWarning() << Q_FUNC_INFO << "missing role, implement me";
100unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
102 if (index < 0 || index >= m_list.count()) {
105 return m_list.at(index);
108void LauncherModel::move(int oldIndex, int newIndex)
110 // Make sure its not moved outside the lists
114 if (newIndex >= m_list.count()) {
115 newIndex = m_list.count()-1;
119 if (oldIndex == newIndex) {
123 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
124 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
125 // adjust the model's index by +1 in case we're moving upwards
126 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
128 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
129 m_list.move(oldIndex, newIndex);
132 if (!m_list.at(newIndex)->pinned()) {
133 pin(m_list.at(newIndex)->appId());
139void LauncherModel::pin(const QString &appId, int index)
141 int currentIndex = findApplication(appId);
143 if (currentIndex >= 0) {
144 if (index == -1 || index == currentIndex) {
145 m_list.at(currentIndex)->setPinned(true);
146 QModelIndex modelIndex = this->index(currentIndex);
147 Q_EMIT dataChanged(modelIndex, modelIndex, {RolePinned});
149 move(currentIndex, index);
150 // move() will store the list to the backend itself, so just exit at this point.
155 index = m_list.count();
158 UalWrapper::AppInfo appInfo = UalWrapper::getApplicationInfo(appId);
159 if (!appInfo.valid) {
160 qWarning() << "Can't pin application, appId not found:" << appId;
164 beginInsertRows(QModelIndex(), index, index);
165 LauncherItem *item = new LauncherItem(appId,
169 item->setPinned(true);
170 m_list.insert(index, item);
177void LauncherModel::requestRemove(const QString &appId)
183void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
185 const int index = findApplication(appId);
190 LauncherItem *item = m_list.at(index);
191 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
193 const QString actionId = model->get(actionIndex).actionId();
195 // Check if this is one of the launcher actions we handle ourselves
196 if (actionId == QLatin1String("pin_item")) {
197 if (item->pinned()) {
198 requestRemove(appId);
202 } else if (actionId == QStringLiteral("launch_item")) {
203 QDesktopServices::openUrl(getUrlForAppId(appId));
204 } else if (actionId == QStringLiteral("stop_item")) { // Quit
206 m_appManager->stopApplication(appId);
208 } else if (actionId.startsWith(QStringLiteral("surface_"))){
209 ApplicationInfoInterface *appInfo = m_appManager->findApplication(appId);
211 for (int i = 0; i < appInfo->surfaceList()->count(); ++i) {
212 MirSurfaceInterface *iface = appInfo->surfaceList()->get(i);
213 QString id = actionId;
214 id.remove(QRegExp("^surface_"));
215 if (id == iface->persistentId()) {
220 qWarning() << "App for" << appId << "not found in launcher. Cannot invoke quicklist action";
222 // Nope, we don't know this action, let the backend forward it to the application
224 // TODO: forward quicklist action to app, possibly via m_dbusIface
229void LauncherModel::setUser(const QString &username)
234QString LauncherModel::getUrlForAppId(const QString &appId) const
236 // appId is either an appId or a legacy app name. Let's find out which
237 if (appId.isEmpty()) {
241 if (!appId.contains('_')) {
242 return "application:///" + appId + ".desktop";
245 QStringList parts = appId.split('_');
246 QString package = parts.value(0);
247 QString app = parts.value(1, QStringLiteral("first-listed-app"));
248 return "appid://" + package + "/" + app + "/current-user-version";
251ApplicationManagerInterface *LauncherModel::applicationManager() const
256void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
258 // Is there already another appmanager set?
260 // Disconnect any signals
261 disconnect(this, &LauncherModel::applicationAdded, 0, nullptr);
262 disconnect(this, &LauncherModel::applicationRemoved, 0, nullptr);
263 disconnect(this, &LauncherModel::focusedAppIdChanged, 0, nullptr);
265 // remove any recent/running apps from the launcher
266 QList<int> recentAppIndices;
267 for (int i = 0; i < m_list.count(); ++i) {
268 if (m_list.at(i)->recent()) {
269 recentAppIndices << i;
273 while (recentAppIndices.count() > 0) {
274 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
275 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
277 recentAppIndices.takeFirst();
282 m_appManager = appManager;
283 connect(m_appManager, &ApplicationManagerInterface::rowsInserted, this, &LauncherModel::applicationAdded);
284 connect(m_appManager, &ApplicationManagerInterface::rowsAboutToBeRemoved, this, &LauncherModel::applicationRemoved);
285 connect(m_appManager, &ApplicationManagerInterface::focusedApplicationIdChanged, this, &LauncherModel::focusedAppIdChanged);
287 Q_EMIT applicationManagerChanged();
289 for (int i = 0; i < appManager->count(); ++i) {
290 applicationAdded(QModelIndex(), i);
294bool LauncherModel::onlyPinned() const
299void LauncherModel::setOnlyPinned(bool onlyPinned) {
300 Q_UNUSED(onlyPinned);
301 qWarning() << "This launcher implementation does not support showing only pinned apps";
304void LauncherModel::storeAppList()
307 Q_FOREACH(LauncherItem *item, m_list) {
308 if (item->pinned()) {
309 appIds << item->appId();
312 m_settings->setStoredApplications(appIds);
313 m_asAdapter->syncItems(m_list);
316void LauncherModel::unpin(const QString &appId)
318 const int index = findApplication(appId);
323 if (m_appManager->findApplication(appId)) {
324 if (m_list.at(index)->pinned()) {
325 m_list.at(index)->setPinned(false);
326 QModelIndex modelIndex = this->index(index);
327 Q_EMIT dataChanged(modelIndex, modelIndex, {RolePinned});
330 beginRemoveRows(QModelIndex(), index, index);
331 m_list.takeAt(index)->deleteLater();
336int LauncherModel::findApplication(const QString &appId)
338 for (int i = 0; i < m_list.count(); ++i) {
339 LauncherItem *item = m_list.at(i);
340 if (item->appId() == appId) {
347void LauncherModel::progressChanged(const QString &appId, int progress)
349 const int idx = findApplication(appId);
351 LauncherItem *item = m_list.at(idx);
352 item->setProgress(progress);
353 Q_EMIT dataChanged(index(idx), index(idx), {RoleProgress});
357void LauncherModel::countChanged(const QString &appId, int count)
359 const int idx = findApplication(appId);
361 LauncherItem *item = m_list.at(idx);
362 item->setCount(count);
363 QVector<int> changedRoles = {RoleCount};
364 if (item->countVisible() && !item->alerting() && !item->focused()) {
365 changedRoles << RoleAlerting;
366 item->setAlerting(true);
368 m_asAdapter->syncItems(m_list);
369 Q_EMIT dataChanged(index(idx), index(idx), changedRoles);
373void LauncherModel::countVisibleChanged(const QString &appId, bool countVisible)
375 int idx = findApplication(appId);
377 LauncherItem *item = m_list.at(idx);
378 item->setCountVisible(countVisible);
379 QVector<int> changedRoles = {RoleCountVisible};
380 if (countVisible && !item->alerting() && !item->focused()) {
381 changedRoles << RoleAlerting;
382 item->setAlerting(true);
384 Q_EMIT dataChanged(index(idx), index(idx), changedRoles);
386 // If countVisible goes to false, and the item is neither pinned nor recent we can drop it
387 if (!countVisible && !item->pinned() && !item->recent()) {
388 beginRemoveRows(QModelIndex(), idx, idx);
389 m_list.takeAt(idx)->deleteLater();
393 // Need to create a new LauncherItem and show the highlight
394 UalWrapper::AppInfo appInfo = UalWrapper::getApplicationInfo(appId);
395 if (countVisible && appInfo.valid) {
396 LauncherItem *item = new LauncherItem(appId,
400 item->setCountVisible(true);
401 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
406 m_asAdapter->syncItems(m_list);
409void LauncherModel::refresh()
411 // First walk through all the existing items and see if we need to remove something
412 QList<LauncherItem*> toBeRemoved;
413 Q_FOREACH (LauncherItem* item, m_list) {
414 UalWrapper::AppInfo appInfo = UalWrapper::getApplicationInfo(item->appId());
415 if (!appInfo.valid) {
416 // Application no longer available => drop it!
418 } else if (!m_settings->storedApplications().contains(item->appId())) {
419 // Item not in settings any more => drop it!
422 int idx = m_list.indexOf(item);
423 item->setName(appInfo.name);
424 item->setPinned(item->pinned()); // update pinned text if needed
425 item->setRunning(item->running());
426 Q_EMIT dataChanged(index(idx), index(idx), {RoleName, RoleRunning});
428 const QString oldIcon = item->icon();
429 if (oldIcon == appInfo.icon) { // same icon file, perhaps different contents, simulate changing the icon name to force reload
430 item->setIcon(QString());
431 Q_EMIT dataChanged(index(idx), index(idx), {RoleIcon});
434 // now set the icon for real
435 item->setIcon(appInfo.icon);
436 Q_EMIT dataChanged(index(idx), index(idx), {RoleIcon});
440 Q_FOREACH (LauncherItem* item, toBeRemoved) {
441 unpin(item->appId());
444 bool changed = toBeRemoved.count() > 0;
446 // This brings the Launcher into sync with the settings backend again. There's an issue though:
447 // If we can't find a .desktop file for an entry we need to skip it. That makes our settingsIndex
448 // go out of sync with the actual index of items. So let's also use an addedIndex which reflects
449 // the settingsIndex minus the skipped items.
452 // Now walk through settings and see if we need to add something
453 for (int settingsIndex = 0; settingsIndex < m_settings->storedApplications().count(); ++settingsIndex) {
454 const QString entry = m_settings->storedApplications().at(settingsIndex);
456 for (int i = 0; i < m_list.count(); ++i) {
457 if (m_list.at(i)->appId() == entry) {
463 if (itemIndex == -1) {
464 // Need to add it. Just add it into the addedIndex to keep same ordering as the list
466 UalWrapper::AppInfo appInfo = UalWrapper::getApplicationInfo(entry);
467 if (!appInfo.valid) {
471 LauncherItem *item = new LauncherItem(entry,
475 item->setPinned(true);
476 beginInsertRows(QModelIndex(), addedIndex, addedIndex);
477 m_list.insert(addedIndex, item);
480 } else if (itemIndex != addedIndex) {
481 // The item is already there, but it is in a different place than in the settings.
482 // Move it to the addedIndex
483 beginMoveRows(QModelIndex(), itemIndex, itemIndex, QModelIndex(), addedIndex);
484 m_list.move(itemIndex, addedIndex);
489 // Just like settingsIndex, this will increase with every item, except the ones we
490 // skipped with the "continue" call above.
498 m_asAdapter->syncItems(m_list);
501void LauncherModel::alert(const QString &appId)
503 int idx = findApplication(appId);
505 LauncherItem *item = m_list.at(idx);
506 if (!item->focused() && !item->alerting()) {
507 item->setAlerting(true);
508 Q_EMIT dataChanged(index(idx), index(idx), {RoleAlerting});
513void LauncherModel::applicationAdded(const QModelIndex &parent, int row)
517 ApplicationInfoInterface *app = m_appManager->get(row);
519 qWarning() << "LauncherModel received an applicationAdded signal, but there's no such application!";
523 if (app->appId() == QLatin1String("unity8-dash")) {
524 // Not adding the dash app
528 const int itemIndex = findApplication(app->appId());
529 if (itemIndex != -1) {
530 LauncherItem *item = m_list.at(itemIndex);
531 if (!item->recent()) {
532 item->setRecent(true);
533 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), {RoleRecent});
535 item->setRunning(true);
537 LauncherItem *item = new LauncherItem(app->appId(), app->name(), app->icon().toString(), this);
538 item->setRecent(true);
539 item->setRunning(true);
540 item->setFocused(app->focused());
541 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
545 connect(app, &ApplicationInfoInterface::surfaceCountChanged, this, &LauncherModel::updateSurfaceList);
546 m_asAdapter->syncItems(m_list);
547 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), {RoleRunning});
550void LauncherModel::updateSurfaceList()
552 ApplicationInfoInterface *app = static_cast<ApplicationInfoInterface*>(sender());
553 updateSurfaceListForApp(app);
556void LauncherModel::updateSurfaceListForSurface()
558 MirSurfaceInterface *iface = static_cast<MirSurfaceInterface*>(sender());
559 ApplicationInfoInterface* app = m_appManager->findApplication(iface->appId());
563 updateSurfaceListForApp(app);
566void LauncherModel::updateSurfaceListForApp(ApplicationInfoInterface* app)
568 int idx = findApplication(app->appId());
570 qWarning() << "Received a surface count changed event from an app that's not in the Launcher model";
573 LauncherItem *item = m_list.at(idx);
574 QList<QPair<QString, QString> > surfaces;
575 for (int i = 0; i < app->surfaceList()->count(); ++i) {
576 MirSurfaceInterface* iface = app->surfaceList()->get(i);
577 if (iface->type() == Mir::NormalType || iface->type() == Mir::DialogType) {
578 // Avoid duplicate connections, so let's just disconnect first to be sure
579 disconnect(iface, &MirSurfaceInterface::nameChanged, this, &LauncherModel::updateSurfaceListForSurface);
580 connect(iface, &MirSurfaceInterface::nameChanged, this, &LauncherModel::updateSurfaceListForSurface);
581 QString name = iface->name();
582 if (name.isEmpty()) {
585 surfaces.append({iface->persistentId(), name});
588 item->setSurfaces(surfaces);
589 Q_EMIT dataChanged(index(idx), index(idx), {RoleSurfaceCount});
592void LauncherModel::applicationRemoved(const QModelIndex &parent, int row)
596 ApplicationInfoInterface *app = m_appManager->get(row);
598 for (int i = 0; i < m_list.count(); ++i) {
599 if (m_list.at(i)->appId() == app->appId()) {
606 qWarning() << Q_FUNC_INFO << "appIndex not found";
610 disconnect(app, &ApplicationInfoInterface::surfaceCountChanged, this, &LauncherModel::updateSurfaceList);
612 LauncherItem * item = m_list.at(appIndex);
614 if (!item->pinned()) {
615 beginRemoveRows(QModelIndex(), appIndex, appIndex);
616 m_list.takeAt(appIndex)->deleteLater();
618 m_asAdapter->syncItems(m_list);
620 QVector<int> changedRoles = {RoleRunning};
621 item->setRunning(false);
622 if (item->focused()) {
623 changedRoles << RoleFocused;
624 item->setFocused(false);
626 Q_EMIT dataChanged(index(appIndex), index(appIndex), changedRoles);
630void LauncherModel::focusedAppIdChanged()
632 const QString appId = m_appManager->focusedApplicationId();
633 for (int i = 0; i < m_list.count(); ++i) {
634 LauncherItem *item = m_list.at(i);
635 if (!item->focused() && item->appId() == appId) {
636 QVector<int> changedRoles;
637 changedRoles << RoleFocused;
638 item->setFocused(true);
639 if (item->alerting()) {
640 changedRoles << RoleAlerting;
641 item->setAlerting(false);
643 Q_EMIT dataChanged(index(i), index(i), changedRoles);
644 } else if (item->focused() && item->appId() != appId) {
645 item->setFocused(false);
646 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