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>
27#include <QDesktopServices>
30using namespace unity::shell::application;
32LauncherModel::LauncherModel(QObject *parent):
33 LauncherModelInterface(parent),
34 m_settings(new GSettings(this)),
35 m_dbusIface(new DBusInterface(this)),
36 m_asAdapter(new ASAdapter()),
39 connect(m_dbusIface, &DBusInterface::countChanged, this, &LauncherModel::countChanged);
40 connect(m_dbusIface, &DBusInterface::countVisibleChanged, this, &LauncherModel::countVisibleChanged);
41 connect(m_dbusIface, &DBusInterface::progressChanged, this, &LauncherModel::progressChanged);
42 connect(m_dbusIface, &DBusInterface::refreshCalled, this, &LauncherModel::refresh);
43 connect(m_dbusIface, &DBusInterface::alertCalled, this, &LauncherModel::alert);
45 connect(m_settings, &GSettings::changed, this, &LauncherModel::refresh);
50LauncherModel::~LauncherModel()
52 while (!m_list.empty()) {
53 m_list.takeFirst()->deleteLater();
59int LauncherModel::rowCount(const QModelIndex &parent) const
62 return m_list.count();
65QVariant LauncherModel::data(const QModelIndex &index, int role) const
67 LauncherItem *item = m_list.at(index.row());
76 return item->pinned();
79 case RoleCountVisible:
80 return item->countVisible();
82 return item->progress();
84 return item->focused();
86 return item->alerting();
88 return item->running();
89 case RoleSurfaceCount:
90 return item->surfaceCount();
92 qWarning() << Q_FUNC_INFO << "missing role, implement me";
99unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
101 if (index < 0 || index >= m_list.count()) {
104 return m_list.at(index);
107void LauncherModel::move(int oldIndex, int newIndex)
109 // Make sure its not moved outside the lists
113 if (newIndex >= m_list.count()) {
114 newIndex = m_list.count()-1;
118 if (oldIndex == newIndex) {
122 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
123 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
124 // adjust the model's index by +1 in case we're moving upwards
125 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
127 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
128 m_list.move(oldIndex, newIndex);
131 if (!m_list.at(newIndex)->pinned()) {
132 pin(m_list.at(newIndex)->appId());
138void LauncherModel::pin(const QString &appId, int index)
140 int currentIndex = findApplication(appId);
142 if (currentIndex >= 0) {
143 if (index == -1 || index == currentIndex) {
144 m_list.at(currentIndex)->setPinned(true);
145 QModelIndex modelIndex = this->index(currentIndex);
146 Q_EMIT dataChanged(modelIndex, modelIndex, {RolePinned});
148 move(currentIndex, index);
149 // move() will store the list to the backend itself, so just exit at this point.
154 index = m_list.count();
157 UalWrapper::AppInfo appInfo = UalWrapper::getApplicationInfo(appId);
158 if (!appInfo.valid) {
159 qWarning() << "Can't pin application, appId not found:" << appId;
163 beginInsertRows(QModelIndex(), index, index);
164 LauncherItem *item = new LauncherItem(appId,
168 item->setPinned(true);
169 m_list.insert(index, item);
176void LauncherModel::requestRemove(const QString &appId)
182void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
184 const int index = findApplication(appId);
189 LauncherItem *item = m_list.at(index);
190 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
192 const QString actionId = model->get(actionIndex).actionId();
194 // Check if this is one of the launcher actions we handle ourselves
195 if (actionId == QLatin1String("pin_item")) {
196 if (item->pinned()) {
197 requestRemove(appId);
201 } else if (actionId == QLatin1String("launch_item")) {
202 QDesktopServices::openUrl(getUrlForAppId(appId));
203 } else if (actionId == QLatin1String("stop_item")) { // Quit
205 m_appManager->stopApplication(appId);
207 // Nope, we don't know this action, let the backend forward it to the application
209 // TODO: forward quicklist action to app, possibly via m_dbusIface
214void LauncherModel::setUser(const QString &username)
219QString LauncherModel::getUrlForAppId(const QString &appId) const
221 // appId is either an appId or a legacy app name. Let's find out which
222 if (appId.isEmpty()) {
226 if (!appId.contains('_')) {
227 return "application:///" + appId + ".desktop";
230 QStringList parts = appId.split('_');
231 QString package = parts.value(0);
232 QString app = parts.value(1, QStringLiteral("first-listed-app"));
233 return "appid://" + package + "/" + app + "/current-user-version";
236ApplicationManagerInterface *LauncherModel::applicationManager() const
241void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
243 // Is there already another appmanager set?
245 // Disconnect any signals
246 disconnect(this, &LauncherModel::applicationAdded, 0, nullptr);
247 disconnect(this, &LauncherModel::applicationRemoved, 0, nullptr);
248 disconnect(this, &LauncherModel::focusedAppIdChanged, 0, nullptr);
250 // remove any recent/running apps from the launcher
251 QList<int> recentAppIndices;
252 for (int i = 0; i < m_list.count(); ++i) {
253 if (m_list.at(i)->recent()) {
254 recentAppIndices << i;
258 while (recentAppIndices.count() > 0) {
259 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
260 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
262 recentAppIndices.takeFirst();
267 m_appManager = appManager;
268 connect(m_appManager, &ApplicationManagerInterface::rowsInserted, this, &LauncherModel::applicationAdded);
269 connect(m_appManager, &ApplicationManagerInterface::rowsAboutToBeRemoved, this, &LauncherModel::applicationRemoved);
270 connect(m_appManager, &ApplicationManagerInterface::focusedApplicationIdChanged, this, &LauncherModel::focusedAppIdChanged);
272 Q_EMIT applicationManagerChanged();
274 for (int i = 0; i < appManager->count(); ++i) {
275 applicationAdded(QModelIndex(), i);
279bool LauncherModel::onlyPinned() const
284void LauncherModel::setOnlyPinned(bool onlyPinned) {
285 Q_UNUSED(onlyPinned);
286 qWarning() << "This launcher implementation does not support showing only pinned apps";
289void LauncherModel::storeAppList()
292 Q_FOREACH(LauncherItem *item, m_list) {
293 if (item->pinned()) {
294 appIds << item->appId();
297 m_settings->setStoredApplications(appIds);
298 m_asAdapter->syncItems(m_list);
301void LauncherModel::unpin(const QString &appId)
303 const int index = findApplication(appId);
308 if (m_appManager->findApplication(appId)) {
309 if (m_list.at(index)->pinned()) {
310 m_list.at(index)->setPinned(false);
311 QModelIndex modelIndex = this->index(index);
312 Q_EMIT dataChanged(modelIndex, modelIndex, {RolePinned});
315 beginRemoveRows(QModelIndex(), index, index);
316 m_list.takeAt(index)->deleteLater();
321int LauncherModel::findApplication(const QString &appId)
323 for (int i = 0; i < m_list.count(); ++i) {
324 LauncherItem *item = m_list.at(i);
325 if (item->appId() == appId) {
332void LauncherModel::progressChanged(const QString &appId, int progress)
334 const int idx = findApplication(appId);
336 LauncherItem *item = m_list.at(idx);
337 item->setProgress(progress);
338 Q_EMIT dataChanged(index(idx), index(idx), {RoleProgress});
342void LauncherModel::countChanged(const QString &appId, int count)
344 const int idx = findApplication(appId);
346 LauncherItem *item = m_list.at(idx);
347 item->setCount(count);
348 QVector<int> changedRoles = {RoleCount};
349 if (item->countVisible() && !item->alerting() && !item->focused()) {
350 changedRoles << RoleAlerting;
351 item->setAlerting(true);
353 m_asAdapter->syncItems(m_list);
354 Q_EMIT dataChanged(index(idx), index(idx), changedRoles);
358void LauncherModel::countVisibleChanged(const QString &appId, bool countVisible)
360 int idx = findApplication(appId);
362 LauncherItem *item = m_list.at(idx);
363 item->setCountVisible(countVisible);
364 QVector<int> changedRoles = {RoleCountVisible};
365 if (countVisible && !item->alerting() && !item->focused()) {
366 changedRoles << RoleAlerting;
367 item->setAlerting(true);
369 Q_EMIT dataChanged(index(idx), index(idx), changedRoles);
371 // If countVisible goes to false, and the item is neither pinned nor recent we can drop it
372 if (!countVisible && !item->pinned() && !item->recent()) {
373 beginRemoveRows(QModelIndex(), idx, idx);
374 m_list.takeAt(idx)->deleteLater();
378 // Need to create a new LauncherItem and show the highlight
379 UalWrapper::AppInfo appInfo = UalWrapper::getApplicationInfo(appId);
380 if (countVisible && appInfo.valid) {
381 LauncherItem *item = new LauncherItem(appId,
385 item->setCountVisible(true);
386 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
391 m_asAdapter->syncItems(m_list);
394void LauncherModel::refresh()
396 // First walk through all the existing items and see if we need to remove something
397 QList<LauncherItem*> toBeRemoved;
398 Q_FOREACH (LauncherItem* item, m_list) {
399 UalWrapper::AppInfo appInfo = UalWrapper::getApplicationInfo(item->appId());
400 if (!appInfo.valid) {
401 // Application no longer available => drop it!
403 } else if (!m_settings->storedApplications().contains(item->appId())) {
404 // Item not in settings any more => drop it!
407 int idx = m_list.indexOf(item);
408 item->setName(appInfo.name);
409 item->setPinned(item->pinned()); // update pinned text if needed
410 item->setRunning(item->running());
411 Q_EMIT dataChanged(index(idx), index(idx), {RoleName, RoleRunning});
413 const QString oldIcon = item->icon();
414 if (oldIcon == appInfo.icon) { // same icon file, perhaps different contents, simulate changing the icon name to force reload
415 item->setIcon(QString());
416 Q_EMIT dataChanged(index(idx), index(idx), {RoleIcon});
419 // now set the icon for real
420 item->setIcon(appInfo.icon);
421 Q_EMIT dataChanged(index(idx), index(idx), {RoleIcon});
425 Q_FOREACH (LauncherItem* item, toBeRemoved) {
426 unpin(item->appId());
429 bool changed = toBeRemoved.count() > 0;
431 // This brings the Launcher into sync with the settings backend again. There's an issue though:
432 // If we can't find a .desktop file for an entry we need to skip it. That makes our settingsIndex
433 // go out of sync with the actual index of items. So let's also use an addedIndex which reflects
434 // the settingsIndex minus the skipped items.
437 // Now walk through settings and see if we need to add something
438 for (int settingsIndex = 0; settingsIndex < m_settings->storedApplications().count(); ++settingsIndex) {
439 const QString entry = m_settings->storedApplications().at(settingsIndex);
441 for (int i = 0; i < m_list.count(); ++i) {
442 if (m_list.at(i)->appId() == entry) {
448 if (itemIndex == -1) {
449 // Need to add it. Just add it into the addedIndex to keep same ordering as the list
451 UalWrapper::AppInfo appInfo = UalWrapper::getApplicationInfo(entry);
452 if (!appInfo.valid) {
456 LauncherItem *item = new LauncherItem(entry,
460 item->setPinned(true);
461 beginInsertRows(QModelIndex(), addedIndex, addedIndex);
462 m_list.insert(addedIndex, item);
465 } else if (itemIndex != addedIndex) {
466 // The item is already there, but it is in a different place than in the settings.
467 // Move it to the addedIndex
468 beginMoveRows(QModelIndex(), itemIndex, itemIndex, QModelIndex(), addedIndex);
469 m_list.move(itemIndex, addedIndex);
474 // Just like settingsIndex, this will increase with every item, except the ones we
475 // skipped with the "continue" call above.
483 m_asAdapter->syncItems(m_list);
486void LauncherModel::alert(const QString &appId)
488 int idx = findApplication(appId);
490 LauncherItem *item = m_list.at(idx);
491 if (!item->focused() && !item->alerting()) {
492 item->setAlerting(true);
493 Q_EMIT dataChanged(index(idx), index(idx), {RoleAlerting});
498void LauncherModel::applicationAdded(const QModelIndex &parent, int row)
502 ApplicationInfoInterface *app = m_appManager->get(row);
504 qWarning() << "LauncherModel received an applicationAdded signal, but there's no such application!";
508 if (app->appId() == QLatin1String("unity8-dash")) {
509 // Not adding the dash app
513 const int itemIndex = findApplication(app->appId());
514 if (itemIndex != -1) {
515 LauncherItem *item = m_list.at(itemIndex);
516 if (!item->recent()) {
517 item->setRecent(true);
518 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), {RoleRecent});
520 if (item->surfaceCount() != app->surfaceCount()) {
521 item->setSurfaceCount(app->surfaceCount());
522 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), {RoleSurfaceCount});
525 item->setRunning(true);
527 LauncherItem *item = new LauncherItem(app->appId(), app->name(), app->icon().toString(), this);
528 item->setRecent(true);
529 item->setRunning(true);
530 item->setFocused(app->focused());
531 item->setSurfaceCount(app->surfaceCount());
532 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
536 connect(app, &ApplicationInfoInterface::surfaceCountChanged, this, &LauncherModel::applicationSurfaceCountChanged);
537 m_asAdapter->syncItems(m_list);
538 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), {RoleRunning});
541void LauncherModel::applicationSurfaceCountChanged(int count)
543 ApplicationInfoInterface *app = static_cast<ApplicationInfoInterface*>(sender());
544 int idx = findApplication(app->appId());
546 qWarning() << "Received a surface count changed event from an app that's not in the Launcher model";
549 LauncherItem *item = m_list.at(idx);
550 if (item->surfaceCount() != count) {
551 item->setSurfaceCount(count);
552 Q_EMIT dataChanged(index(idx), index(idx), {RoleSurfaceCount});
556void LauncherModel::applicationRemoved(const QModelIndex &parent, int row)
560 ApplicationInfoInterface *app = m_appManager->get(row);
562 for (int i = 0; i < m_list.count(); ++i) {
563 if (m_list.at(i)->appId() == app->appId()) {
570 qWarning() << Q_FUNC_INFO << "appIndex not found";
574 disconnect(app, &ApplicationInfoInterface::surfaceCountChanged, this, &LauncherModel::applicationSurfaceCountChanged);
576 LauncherItem * item = m_list.at(appIndex);
578 if (!item->pinned()) {
579 beginRemoveRows(QModelIndex(), appIndex, appIndex);
580 m_list.takeAt(appIndex)->deleteLater();
582 m_asAdapter->syncItems(m_list);
584 QVector<int> changedRoles = {RoleRunning};
585 item->setRunning(false);
586 if (item->focused()) {
587 changedRoles << RoleFocused;
588 item->setFocused(false);
590 Q_EMIT dataChanged(index(appIndex), index(appIndex), changedRoles);
594void LauncherModel::focusedAppIdChanged()
596 const QString appId = m_appManager->focusedApplicationId();
597 for (int i = 0; i < m_list.count(); ++i) {
598 LauncherItem *item = m_list.at(i);
599 if (!item->focused() && item->appId() == appId) {
600 QVector<int> changedRoles;
601 changedRoles << RoleFocused;
602 item->setFocused(true);
603 if (item->alerting()) {
604 changedRoles << RoleAlerting;
605 item->setAlerting(false);
607 Q_EMIT dataChanged(index(i), index(i), changedRoles);
608 } else if (item->focused() && item->appId() != appId) {
609 item->setFocused(false);
610 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