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 qWarning() << "App for" << appId << "not found in launcher. Cannot invoke quicklist action";
213 for (int i = 0; i < appInfo->surfaceList()->count(); ++i) {
214 MirSurfaceInterface *iface = appInfo->surfaceList()->get(i);
215 QString id = actionId;
216 id.remove(QRegExp("^surface_"));
217 if (id == iface->persistentId()) {
221 // Nope, we don't know this action, let the backend forward it to the application
223 // TODO: forward quicklist action to app, possibly via m_dbusIface
228void LauncherModel::setUser(const QString &username)
233QString LauncherModel::getUrlForAppId(const QString &appId) const
235 // appId is either an appId or a legacy app name. Let's find out which
236 if (appId.isEmpty()) {
240 if (!appId.contains('_')) {
241 return "application:///" + appId + ".desktop";
244 QStringList parts = appId.split('_');
245 QString package = parts.value(0);
246 QString app = parts.value(1, QStringLiteral("first-listed-app"));
247 return "appid://" + package + "/" + app + "/current-user-version";
250ApplicationManagerInterface *LauncherModel::applicationManager() const
255void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
257 // Is there already another appmanager set?
259 // Disconnect any signals
260 disconnect(this, &LauncherModel::applicationAdded, 0, nullptr);
261 disconnect(this, &LauncherModel::applicationRemoved, 0, nullptr);
262 disconnect(this, &LauncherModel::focusedAppIdChanged, 0, nullptr);
264 // remove any recent/running apps from the launcher
265 QList<int> recentAppIndices;
266 for (int i = 0; i < m_list.count(); ++i) {
267 if (m_list.at(i)->recent()) {
268 recentAppIndices << i;
272 while (recentAppIndices.count() > 0) {
273 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
274 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
276 recentAppIndices.takeFirst();
281 m_appManager = appManager;
282 connect(m_appManager, &ApplicationManagerInterface::rowsInserted, this, &LauncherModel::applicationAdded);
283 connect(m_appManager, &ApplicationManagerInterface::rowsAboutToBeRemoved, this, &LauncherModel::applicationRemoved);
284 connect(m_appManager, &ApplicationManagerInterface::focusedApplicationIdChanged, this, &LauncherModel::focusedAppIdChanged);
286 Q_EMIT applicationManagerChanged();
288 for (int i = 0; i < appManager->count(); ++i) {
289 applicationAdded(QModelIndex(), i);
293bool LauncherModel::onlyPinned() const
298void LauncherModel::setOnlyPinned(bool onlyPinned) {
299 Q_UNUSED(onlyPinned);
300 qWarning() << "This launcher implementation does not support showing only pinned apps";
303void LauncherModel::storeAppList()
306 Q_FOREACH(LauncherItem *item, m_list) {
307 if (item->pinned()) {
308 appIds << item->appId();
311 m_settings->setStoredApplications(appIds);
312 m_asAdapter->syncItems(m_list);
315void LauncherModel::unpin(const QString &appId)
317 const int index = findApplication(appId);
322 if (m_appManager->findApplication(appId)) {
323 if (m_list.at(index)->pinned()) {
324 m_list.at(index)->setPinned(false);
325 QModelIndex modelIndex = this->index(index);
326 Q_EMIT dataChanged(modelIndex, modelIndex, {RolePinned});
329 beginRemoveRows(QModelIndex(), index, index);
330 m_list.takeAt(index)->deleteLater();
335int LauncherModel::findApplication(const QString &appId)
337 for (int i = 0; i < m_list.count(); ++i) {
338 LauncherItem *item = m_list.at(i);
339 if (item->appId() == appId) {
346void LauncherModel::progressChanged(const QString &appId, int progress)
348 const int idx = findApplication(appId);
350 LauncherItem *item = m_list.at(idx);
351 item->setProgress(progress);
352 Q_EMIT dataChanged(index(idx), index(idx), {RoleProgress});
356void LauncherModel::countChanged(const QString &appId, int count)
358 const int idx = findApplication(appId);
360 LauncherItem *item = m_list.at(idx);
361 item->setCount(count);
362 QVector<int> changedRoles = {RoleCount};
363 if (item->countVisible() && !item->alerting() && !item->focused()) {
364 changedRoles << RoleAlerting;
365 item->setAlerting(true);
367 m_asAdapter->syncItems(m_list);
368 Q_EMIT dataChanged(index(idx), index(idx), changedRoles);
372void LauncherModel::countVisibleChanged(const QString &appId, bool countVisible)
374 int idx = findApplication(appId);
376 LauncherItem *item = m_list.at(idx);
377 item->setCountVisible(countVisible);
378 QVector<int> changedRoles = {RoleCountVisible};
379 if (countVisible && !item->alerting() && !item->focused()) {
380 changedRoles << RoleAlerting;
381 item->setAlerting(true);
383 Q_EMIT dataChanged(index(idx), index(idx), changedRoles);
385 // If countVisible goes to false, and the item is neither pinned nor recent we can drop it
386 if (!countVisible && !item->pinned() && !item->recent()) {
387 beginRemoveRows(QModelIndex(), idx, idx);
388 m_list.takeAt(idx)->deleteLater();
392 // Need to create a new LauncherItem and show the highlight
393 UalWrapper::AppInfo appInfo = UalWrapper::getApplicationInfo(appId);
394 if (countVisible && appInfo.valid) {
395 LauncherItem *item = new LauncherItem(appId,
399 item->setCountVisible(true);
400 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
405 m_asAdapter->syncItems(m_list);
408void LauncherModel::refresh()
410 // First walk through all the existing items and see if we need to remove something
411 QList<LauncherItem*> toBeRemoved;
412 Q_FOREACH (LauncherItem* item, m_list) {
413 UalWrapper::AppInfo appInfo = UalWrapper::getApplicationInfo(item->appId());
414 if (!appInfo.valid) {
415 // Application no longer available => drop it!
417 } else if (!m_settings->storedApplications().contains(item->appId())) {
418 // Item not in settings any more => drop it!
421 int idx = m_list.indexOf(item);
422 item->setName(appInfo.name);
423 item->setPinned(item->pinned()); // update pinned text if needed
424 item->setRunning(item->running());
425 Q_EMIT dataChanged(index(idx), index(idx), {RoleName, RoleRunning});
427 const QString oldIcon = item->icon();
428 if (oldIcon == appInfo.icon) { // same icon file, perhaps different contents, simulate changing the icon name to force reload
429 item->setIcon(QString());
430 Q_EMIT dataChanged(index(idx), index(idx), {RoleIcon});
433 // now set the icon for real
434 item->setIcon(appInfo.icon);
435 Q_EMIT dataChanged(index(idx), index(idx), {RoleIcon});
439 Q_FOREACH (LauncherItem* item, toBeRemoved) {
440 unpin(item->appId());
443 bool changed = toBeRemoved.count() > 0;
445 // This brings the Launcher into sync with the settings backend again. There's an issue though:
446 // If we can't find a .desktop file for an entry we need to skip it. That makes our settingsIndex
447 // go out of sync with the actual index of items. So let's also use an addedIndex which reflects
448 // the settingsIndex minus the skipped items.
451 // Now walk through settings and see if we need to add something
452 for (int settingsIndex = 0; settingsIndex < m_settings->storedApplications().count(); ++settingsIndex) {
453 const QString entry = m_settings->storedApplications().at(settingsIndex);
455 for (int i = 0; i < m_list.count(); ++i) {
456 if (m_list.at(i)->appId() == entry) {
462 if (itemIndex == -1) {
463 // Need to add it. Just add it into the addedIndex to keep same ordering as the list
465 UalWrapper::AppInfo appInfo = UalWrapper::getApplicationInfo(entry);
466 if (!appInfo.valid) {
470 LauncherItem *item = new LauncherItem(entry,
474 item->setPinned(true);
475 beginInsertRows(QModelIndex(), addedIndex, addedIndex);
476 m_list.insert(addedIndex, item);
479 } else if (itemIndex != addedIndex) {
480 // The item is already there, but it is in a different place than in the settings.
481 // Move it to the addedIndex
482 beginMoveRows(QModelIndex(), itemIndex, itemIndex, QModelIndex(), addedIndex);
483 m_list.move(itemIndex, addedIndex);
488 // Just like settingsIndex, this will increase with every item, except the ones we
489 // skipped with the "continue" call above.
497 m_asAdapter->syncItems(m_list);
500void LauncherModel::alert(const QString &appId)
502 int idx = findApplication(appId);
504 LauncherItem *item = m_list.at(idx);
505 if (!item->focused() && !item->alerting()) {
506 item->setAlerting(true);
507 Q_EMIT dataChanged(index(idx), index(idx), {RoleAlerting});
512void LauncherModel::applicationAdded(const QModelIndex &parent, int row)
516 ApplicationInfoInterface *app = m_appManager->get(row);
518 qWarning() << "LauncherModel received an applicationAdded signal, but there's no such application!";
522 if (app->appId() == QLatin1String("unity8-dash")) {
523 // Not adding the dash app
527 const int itemIndex = findApplication(app->appId());
528 if (itemIndex != -1) {
529 LauncherItem *item = m_list.at(itemIndex);
530 if (!item->recent()) {
531 item->setRecent(true);
532 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), {RoleRecent});
534 item->setRunning(true);
536 LauncherItem *item = new LauncherItem(app->appId(), app->name(), app->icon().toString(), this);
537 item->setRecent(true);
538 item->setRunning(true);
539 item->setFocused(app->focused());
540 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
544 connect(app, &ApplicationInfoInterface::surfaceCountChanged, this, &LauncherModel::updateSurfaceList);
545 m_asAdapter->syncItems(m_list);
546 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), {RoleRunning});
549void LauncherModel::updateSurfaceList()
551 ApplicationInfoInterface *app = static_cast<ApplicationInfoInterface*>(sender());
552 updateSurfaceListForApp(app);
555void LauncherModel::updateSurfaceListForSurface()
557 MirSurfaceInterface *iface = static_cast<MirSurfaceInterface*>(sender());
558 ApplicationInfoInterface* app = m_appManager->findApplication(iface->appId());
562 updateSurfaceListForApp(app);
565void LauncherModel::updateSurfaceListForApp(ApplicationInfoInterface* app)
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 QList<QPair<QString, QString> > surfaces;
574 for (int i = 0; i < app->surfaceList()->count(); ++i) {
575 MirSurfaceInterface* iface = app->surfaceList()->get(i);
576 // Avoid duplicate connections, so let's just disconnect first to be sure
577 disconnect(iface, &MirSurfaceInterface::nameChanged, this, &LauncherModel::updateSurfaceListForSurface);
578 connect(iface, &MirSurfaceInterface::nameChanged, this, &LauncherModel::updateSurfaceListForSurface);
579 QString name = iface->name();
580 if (name.isEmpty()) {
583 surfaces.append({iface->persistentId(), name});
585 item->setSurfaces(surfaces);
586 Q_EMIT dataChanged(index(idx), index(idx), {RoleSurfaceCount});
589void LauncherModel::applicationRemoved(const QModelIndex &parent, int row)
593 ApplicationInfoInterface *app = m_appManager->get(row);
595 for (int i = 0; i < m_list.count(); ++i) {
596 if (m_list.at(i)->appId() == app->appId()) {
603 qWarning() << Q_FUNC_INFO << "appIndex not found";
607 disconnect(app, &ApplicationInfoInterface::surfaceCountChanged, this, &LauncherModel::updateSurfaceList);
609 LauncherItem * item = m_list.at(appIndex);
611 if (!item->pinned()) {
612 beginRemoveRows(QModelIndex(), appIndex, appIndex);
613 m_list.takeAt(appIndex)->deleteLater();
615 m_asAdapter->syncItems(m_list);
617 QVector<int> changedRoles = {RoleRunning};
618 item->setRunning(false);
619 if (item->focused()) {
620 changedRoles << RoleFocused;
621 item->setFocused(false);
623 Q_EMIT dataChanged(index(appIndex), index(appIndex), changedRoles);
627void LauncherModel::focusedAppIdChanged()
629 const QString appId = m_appManager->focusedApplicationId();
630 for (int i = 0; i < m_list.count(); ++i) {
631 LauncherItem *item = m_list.at(i);
632 if (!item->focused() && item->appId() == appId) {
633 QVector<int> changedRoles;
634 changedRoles << RoleFocused;
635 item->setFocused(true);
636 if (item->alerting()) {
637 changedRoles << RoleAlerting;
638 item->setAlerting(false);
640 Q_EMIT dataChanged(index(i), index(i), changedRoles);
641 } else if (item->focused() && item->appId() != appId) {
642 item->setFocused(false);
643 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