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 "desktopfilehandler.h"
24#include "dbusinterface.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()),
42 connect(m_dbusIface, &DBusInterface::countChanged, this, &LauncherModel::countChanged);
43 connect(m_dbusIface, &DBusInterface::countVisibleChanged, this, &LauncherModel::countVisibleChanged);
44 connect(m_dbusIface, &DBusInterface::progressChanged, this, &LauncherModel::progressChanged);
45 connect(m_dbusIface, &DBusInterface::refreshCalled, this, &LauncherModel::refresh);
46 connect(m_dbusIface, &DBusInterface::alertCalled, this, &LauncherModel::alert);
48 connect(m_settings, &GSettings::changed, this, &LauncherModel::refresh);
53LauncherModel::~LauncherModel()
55 while (!m_list.empty()) {
56 m_list.takeFirst()->deleteLater();
62int LauncherModel::rowCount(const QModelIndex &parent) const
65 return m_list.count();
68QVariant LauncherModel::data(const QModelIndex &index, int role) const
70 LauncherItem *item = m_list.at(index.row());
79 return item->pinned();
82 case RoleCountVisible:
83 return item->countVisible();
85 return item->progress();
87 return item->focused();
89 return item->alerting();
91 return item->running();
92 case RoleSurfaceCount:
93 return item->surfaceCount();
95 qWarning() << Q_FUNC_INFO << "missing role, implement me";
102void LauncherModel::setAlerting(const QString &appId, bool alerting) {
103 int index = findApplication(appId);
105 QModelIndex modelIndex = this->index(index);
106 LauncherItem *item = m_list.at(index);
107 if (!item->focused()) {
108 item->setAlerting(alerting);
109 Q_EMIT dataChanged(modelIndex, modelIndex, {RoleAlerting});
114unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
116 if (index < 0 || index >= m_list.count()) {
119 return m_list.at(index);
122void LauncherModel::move(int oldIndex, int newIndex)
124 // Make sure its not moved outside the lists
128 if (newIndex >= m_list.count()) {
129 newIndex = m_list.count()-1;
133 if (oldIndex == newIndex) {
137 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
138 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
139 // adjust the model's index by +1 in case we're moving upwards
140 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
142 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
143 m_list.move(oldIndex, newIndex);
146 if (!m_list.at(newIndex)->pinned()) {
147 pin(m_list.at(newIndex)->appId());
153void LauncherModel::pin(const QString &appId, int index)
155 int currentIndex = findApplication(appId);
157 if (currentIndex >= 0) {
158 if (index == -1 || index == currentIndex) {
159 m_list.at(currentIndex)->setPinned(true);
160 QModelIndex modelIndex = this->index(currentIndex);
161 Q_EMIT dataChanged(modelIndex, modelIndex, {RolePinned});
163 move(currentIndex, index);
164 // move() will store the list to the backend itself, so just exit at this point.
169 index = m_list.count();
172 DesktopFileHandler desktopFile(appId);
173 if (!desktopFile.isValid()) {
174 qWarning() << "Can't pin this application, there is no .desktop file available.";
178 beginInsertRows(QModelIndex(), index, index);
179 LauncherItem *item = new LauncherItem(appId,
180 desktopFile.displayName(),
183 item->setPinned(true);
184 m_list.insert(index, item);
191void LauncherModel::requestRemove(const QString &appId)
197void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
199 const int index = findApplication(appId);
204 LauncherItem *item = m_list.at(index);
205 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
207 const QString actionId = model->get(actionIndex).actionId();
209 // Check if this is one of the launcher actions we handle ourselves
210 if (actionId == QLatin1String("pin_item")) {
211 if (item->pinned()) {
212 requestRemove(appId);
216 } else if (actionId == QLatin1String("launch_item")) {
217 QDesktopServices::openUrl(getUrlForAppId(appId));
218 } else if (actionId == QLatin1String("stop_item")) { // Quit
220 m_appManager->stopApplication(appId);
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)
232 qWarning() << "This backend doesn't support multiple users";
235QString LauncherModel::getUrlForAppId(const QString &appId) const
237 // appId is either an appId or a legacy app name. Let's find out which
238 if (appId.isEmpty()) {
242 if (!appId.contains('_')) {
243 return "application:///" + appId + ".desktop";
246 QStringList parts = appId.split('_');
247 QString package = parts.value(0);
248 QString app = parts.value(1, QStringLiteral("first-listed-app"));
249 return "appid://" + package + "/" + app + "/current-user-version";
252ApplicationManagerInterface *LauncherModel::applicationManager() const
257void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
259 // Is there already another appmanager set?
261 // Disconnect any signals
262 disconnect(this, &LauncherModel::applicationAdded, 0, nullptr);
263 disconnect(this, &LauncherModel::applicationRemoved, 0, nullptr);
264 disconnect(this, &LauncherModel::focusedAppIdChanged, 0, nullptr);
266 // remove any recent/running apps from the launcher
267 QList<int> recentAppIndices;
268 for (int i = 0; i < m_list.count(); ++i) {
269 if (m_list.at(i)->recent()) {
270 recentAppIndices << i;
274 while (recentAppIndices.count() > 0) {
275 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
276 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
278 recentAppIndices.takeFirst();
283 m_appManager = appManager;
284 connect(m_appManager, &ApplicationManagerInterface::rowsInserted, this, &LauncherModel::applicationAdded);
285 connect(m_appManager, &ApplicationManagerInterface::rowsAboutToBeRemoved, this, &LauncherModel::applicationRemoved);
286 connect(m_appManager, &ApplicationManagerInterface::focusedApplicationIdChanged, this, &LauncherModel::focusedAppIdChanged);
288 Q_EMIT applicationManagerChanged();
290 for (int i = 0; i < appManager->count(); ++i) {
291 applicationAdded(QModelIndex(), i);
295bool LauncherModel::onlyPinned() const
300void LauncherModel::setOnlyPinned(bool onlyPinned) {
301 Q_UNUSED(onlyPinned);
302 qWarning() << "This launcher implementation does not support showing only pinned apps";
305void LauncherModel::storeAppList()
308 Q_FOREACH(LauncherItem *item, m_list) {
309 if (item->pinned()) {
310 appIds << item->appId();
313 m_settings->setStoredApplications(appIds);
314 m_asAdapter->syncItems(m_list);
317void LauncherModel::unpin(const QString &appId)
319 const int index = findApplication(appId);
324 if (m_appManager->findApplication(appId)) {
325 if (m_list.at(index)->pinned()) {
326 m_list.at(index)->setPinned(false);
327 QModelIndex modelIndex = this->index(index);
328 Q_EMIT dataChanged(modelIndex, modelIndex, {RolePinned});
331 beginRemoveRows(QModelIndex(), index, index);
332 m_list.takeAt(index)->deleteLater();
337int LauncherModel::findApplication(const QString &appId)
339 for (int i = 0; i < m_list.count(); ++i) {
340 LauncherItem *item = m_list.at(i);
341 if (item->appId() == appId) {
348void LauncherModel::progressChanged(const QString &appId, int progress)
350 const int idx = findApplication(appId);
352 LauncherItem *item = m_list.at(idx);
353 item->setProgress(progress);
354 Q_EMIT dataChanged(index(idx), index(idx), {RoleProgress});
358void LauncherModel::countChanged(const QString &appId, int count)
360 const int idx = findApplication(appId);
362 LauncherItem *item = m_list.at(idx);
363 item->setCount(count);
364 if (item->countVisible()) {
365 setAlerting(item->appId(), true);
367 m_asAdapter->syncItems(m_list);
368 Q_EMIT dataChanged(index(idx), index(idx), {RoleCount});
372void LauncherModel::countVisibleChanged(const QString &appId, bool countVisible)
374 int idx = findApplication(appId);
376 LauncherItem *item = m_list.at(idx);
377 item->setCountVisible(countVisible);
379 setAlerting(item->appId(), true);
381 Q_EMIT dataChanged(index(idx), index(idx), {RoleCountVisible});
383 // If countVisible goes to false, and the item is neither pinned nor recent we can drop it
384 if (!countVisible && !item->pinned() && !item->recent()) {
385 beginRemoveRows(QModelIndex(), idx, idx);
386 m_list.takeAt(idx)->deleteLater();
390 // Need to create a new LauncherItem and show the highlight
391 DesktopFileHandler desktopFile(appId);
392 if (countVisible && desktopFile.isValid()) {
393 LauncherItem *item = new LauncherItem(appId,
394 desktopFile.displayName(),
397 item->setCountVisible(true);
398 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
403 m_asAdapter->syncItems(m_list);
406void LauncherModel::refresh()
408 // First walk through all the existing items and see if we need to remove something
409 QList<LauncherItem*> toBeRemoved;
410 Q_FOREACH (LauncherItem* item, m_list) {
411 DesktopFileHandler desktopFile(item->appId());
412 if (!desktopFile.isValid()) {
413 // Desktop file not available for this app => drop it!
415 } else if (!m_settings->storedApplications().contains(item->appId())) {
416 // Item not in settings any more => drop it!
419 int idx = m_list.indexOf(item);
420 item->setName(desktopFile.displayName());
421 item->setPinned(item->pinned()); // update pinned text if needed
422 item->setRunning(item->running());
423 Q_EMIT dataChanged(index(idx), index(idx), {RoleName, RoleRunning});
425 const QString oldIcon = item->icon();
426 if (oldIcon == desktopFile.icon()) { // same icon file, perhaps different contents, simulate changing the icon name to force reload
427 item->setIcon(QString());
428 Q_EMIT dataChanged(index(idx), index(idx), {RoleIcon});
431 // now set the icon for real
432 item->setIcon(desktopFile.icon());
433 Q_EMIT dataChanged(index(idx), index(idx), {RoleIcon});
437 Q_FOREACH (LauncherItem* item, toBeRemoved) {
438 unpin(item->appId());
441 bool changed = toBeRemoved.count() > 0;
443 // This brings the Launcher into sync with the settings backend again. There's an issue though:
444 // If we can't find a .desktop file for an entry we need to skip it. That makes our settingsIndex
445 // go out of sync with the actual index of items. So let's also use an addedIndex which reflects
446 // the settingsIndex minus the skipped items.
449 // Now walk through settings and see if we need to add something
450 for (int settingsIndex = 0; settingsIndex < m_settings->storedApplications().count(); ++settingsIndex) {
451 const QString entry = m_settings->storedApplications().at(settingsIndex);
453 for (int i = 0; i < m_list.count(); ++i) {
454 if (m_list.at(i)->appId() == entry) {
460 if (itemIndex == -1) {
461 // Need to add it. Just add it into the addedIndex to keep same ordering as the list
463 DesktopFileHandler desktopFile(entry);
464 if (!desktopFile.isValid()) {
465 qWarning() << "Couldn't find a .desktop file for" << entry << ". Skipping...";
469 LauncherItem *item = new LauncherItem(entry,
470 desktopFile.displayName(),
473 item->setPinned(true);
474 beginInsertRows(QModelIndex(), addedIndex, addedIndex);
475 m_list.insert(addedIndex, item);
478 } else if (itemIndex != addedIndex) {
479 // The item is already there, but it is in a different place than in the settings.
480 // Move it to the addedIndex
481 beginMoveRows(QModelIndex(), itemIndex, itemIndex, QModelIndex(), addedIndex);
482 m_list.move(itemIndex, addedIndex);
487 // Just like settingsIndex, this will increase with every item, except the ones we
488 // skipped with the "continue" call above.
496 m_asAdapter->syncItems(m_list);
499void LauncherModel::alert(const QString &appId)
501 int idx = findApplication(appId);
503 LauncherItem *item = m_list.at(idx);
504 setAlerting(item->appId(), true);
505 Q_EMIT dataChanged(index(idx), index(idx), {RoleAlerting});
509void LauncherModel::applicationAdded(const QModelIndex &parent, int row)
513 ApplicationInfoInterface *app = m_appManager->get(row);
515 qWarning() << "LauncherModel received an applicationAdded signal, but there's no such application!";
519 if (app->appId() == QLatin1String("unity8-dash")) {
520 // Not adding the dash app
524 const int itemIndex = findApplication(app->appId());
525 if (itemIndex != -1) {
526 LauncherItem *item = m_list.at(itemIndex);
527 if (!item->recent()) {
528 item->setRecent(true);
529 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), {RoleRecent});
531 if (item->surfaceCount() != app->surfaceCount()) {
532 item->setSurfaceCount(app->surfaceList()->count());
533 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), {RoleSurfaceCount});
536 item->setRunning(true);
538 LauncherItem *item = new LauncherItem(app->appId(), app->name(), app->icon().toString(), this);
539 item->setRecent(true);
540 item->setRunning(true);
541 item->setFocused(app->focused());
542 item->setSurfaceCount(app->surfaceCount());
543 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
547 connect(app, &ApplicationInfoInterface::surfaceCountChanged, this, &LauncherModel::applicationSurfaceCountChanged);
548 m_asAdapter->syncItems(m_list);
549 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), {RoleRunning});
552void LauncherModel::applicationSurfaceCountChanged(int count)
554 ApplicationInfoInterface *app = static_cast<ApplicationInfoInterface*>(sender());
555 int idx = findApplication(app->appId());
557 qWarning() << "Received a surface count changed event from an app that's not in the Launcher model";
560 LauncherItem *item = m_list.at(idx);
561 if (item->surfaceCount() != count) {
562 item->setSurfaceCount(count);
563 Q_EMIT dataChanged(index(idx), index(idx), {RoleSurfaceCount});
567void LauncherModel::applicationRemoved(const QModelIndex &parent, int row)
572 for (int i = 0; i < m_list.count(); ++i) {
573 if (m_list.at(i)->appId() == m_appManager->get(row)->appId()) {
580 qWarning() << Q_FUNC_INFO << "appIndex not found";
584 LauncherItem * item = m_list.at(appIndex);
585 item->setRunning(false);
587 if (!item->pinned()) {
588 beginRemoveRows(QModelIndex(), appIndex, appIndex);
589 m_list.takeAt(appIndex)->deleteLater();
591 m_asAdapter->syncItems(m_list);
592 Q_EMIT dataChanged(index(appIndex), index(appIndex), {RolePinned});
594 Q_EMIT dataChanged(index(appIndex), index(appIndex), {RoleRunning});
597void LauncherModel::focusedAppIdChanged()
599 const QString appId = m_appManager->focusedApplicationId();
600 for (int i = 0; i < m_list.count(); ++i) {
601 LauncherItem *item = m_list.at(i);
602 if (!item->focused() && item->appId() == appId) {
603 item->setFocused(true);
604 Q_EMIT dataChanged(index(i), index(i), {RoleFocused});
605 } else if (item->focused() && item->appId() != appId) {
606 item->setFocused(false);
607 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