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 } else if (actionId.startsWith("surface_")){
208 ApplicationInfoInterface *appInfo = m_appManager->findApplication(appId);
210 qWarning() << "App for" << appId << "not found in launcher. Cannot invoke quicklist action";
212 for (int i = 0; i < appInfo->surfaceList()->count(); ++i) {
213 MirSurfaceInterface *iface = appInfo->surfaceList()->get(i);
214 QString id = actionId;
215 id.remove(QRegExp("^surface_"));
216 if (id == iface->persistentId()) {
220 // Nope, we don't know this action, let the backend forward it to the application
222 // TODO: forward quicklist action to app, possibly via m_dbusIface
227void LauncherModel::setUser(const QString &username)
232QString LauncherModel::getUrlForAppId(const QString &appId) const
234 // appId is either an appId or a legacy app name. Let's find out which
235 if (appId.isEmpty()) {
239 if (!appId.contains('_')) {
240 return "application:///" + appId + ".desktop";
243 QStringList parts = appId.split('_');
244 QString package = parts.value(0);
245 QString app = parts.value(1, QStringLiteral("first-listed-app"));
246 return "appid://" + package + "/" + app + "/current-user-version";
249ApplicationManagerInterface *LauncherModel::applicationManager() const
254void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
256 // Is there already another appmanager set?
258 // Disconnect any signals
259 disconnect(this, &LauncherModel::applicationAdded, 0, nullptr);
260 disconnect(this, &LauncherModel::applicationRemoved, 0, nullptr);
261 disconnect(this, &LauncherModel::focusedAppIdChanged, 0, nullptr);
263 // remove any recent/running apps from the launcher
264 QList<int> recentAppIndices;
265 for (int i = 0; i < m_list.count(); ++i) {
266 if (m_list.at(i)->recent()) {
267 recentAppIndices << i;
271 while (recentAppIndices.count() > 0) {
272 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
273 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
275 recentAppIndices.takeFirst();
280 m_appManager = appManager;
281 connect(m_appManager, &ApplicationManagerInterface::rowsInserted, this, &LauncherModel::applicationAdded);
282 connect(m_appManager, &ApplicationManagerInterface::rowsAboutToBeRemoved, this, &LauncherModel::applicationRemoved);
283 connect(m_appManager, &ApplicationManagerInterface::focusedApplicationIdChanged, this, &LauncherModel::focusedAppIdChanged);
285 Q_EMIT applicationManagerChanged();
287 for (int i = 0; i < appManager->count(); ++i) {
288 applicationAdded(QModelIndex(), i);
292bool LauncherModel::onlyPinned() const
297void LauncherModel::setOnlyPinned(bool onlyPinned) {
298 Q_UNUSED(onlyPinned);
299 qWarning() << "This launcher implementation does not support showing only pinned apps";
302void LauncherModel::storeAppList()
305 Q_FOREACH(LauncherItem *item, m_list) {
306 if (item->pinned()) {
307 appIds << item->appId();
310 m_settings->setStoredApplications(appIds);
311 m_asAdapter->syncItems(m_list);
314void LauncherModel::unpin(const QString &appId)
316 const int index = findApplication(appId);
321 if (m_appManager->findApplication(appId)) {
322 if (m_list.at(index)->pinned()) {
323 m_list.at(index)->setPinned(false);
324 QModelIndex modelIndex = this->index(index);
325 Q_EMIT dataChanged(modelIndex, modelIndex, {RolePinned});
328 beginRemoveRows(QModelIndex(), index, index);
329 m_list.takeAt(index)->deleteLater();
334int LauncherModel::findApplication(const QString &appId)
336 for (int i = 0; i < m_list.count(); ++i) {
337 LauncherItem *item = m_list.at(i);
338 if (item->appId() == appId) {
345void LauncherModel::progressChanged(const QString &appId, int progress)
347 const int idx = findApplication(appId);
349 LauncherItem *item = m_list.at(idx);
350 item->setProgress(progress);
351 Q_EMIT dataChanged(index(idx), index(idx), {RoleProgress});
355void LauncherModel::countChanged(const QString &appId, int count)
357 const int idx = findApplication(appId);
359 LauncherItem *item = m_list.at(idx);
360 item->setCount(count);
361 QVector<int> changedRoles = {RoleCount};
362 if (item->countVisible() && !item->alerting() && !item->focused()) {
363 changedRoles << RoleAlerting;
364 item->setAlerting(true);
366 m_asAdapter->syncItems(m_list);
367 Q_EMIT dataChanged(index(idx), index(idx), changedRoles);
371void LauncherModel::countVisibleChanged(const QString &appId, bool countVisible)
373 int idx = findApplication(appId);
375 LauncherItem *item = m_list.at(idx);
376 item->setCountVisible(countVisible);
377 QVector<int> changedRoles = {RoleCountVisible};
378 if (countVisible && !item->alerting() && !item->focused()) {
379 changedRoles << RoleAlerting;
380 item->setAlerting(true);
382 Q_EMIT dataChanged(index(idx), index(idx), changedRoles);
384 // If countVisible goes to false, and the item is neither pinned nor recent we can drop it
385 if (!countVisible && !item->pinned() && !item->recent()) {
386 beginRemoveRows(QModelIndex(), idx, idx);
387 m_list.takeAt(idx)->deleteLater();
391 // Need to create a new LauncherItem and show the highlight
392 UalWrapper::AppInfo appInfo = UalWrapper::getApplicationInfo(appId);
393 if (countVisible && appInfo.valid) {
394 LauncherItem *item = new LauncherItem(appId,
398 item->setCountVisible(true);
399 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
404 m_asAdapter->syncItems(m_list);
407void LauncherModel::refresh()
409 // First walk through all the existing items and see if we need to remove something
410 QList<LauncherItem*> toBeRemoved;
411 Q_FOREACH (LauncherItem* item, m_list) {
412 UalWrapper::AppInfo appInfo = UalWrapper::getApplicationInfo(item->appId());
413 if (!appInfo.valid) {
414 // Application no longer available => drop it!
416 } else if (!m_settings->storedApplications().contains(item->appId())) {
417 // Item not in settings any more => drop it!
420 int idx = m_list.indexOf(item);
421 item->setName(appInfo.name);
422 item->setPinned(item->pinned()); // update pinned text if needed
423 item->setRunning(item->running());
424 Q_EMIT dataChanged(index(idx), index(idx), {RoleName, RoleRunning});
426 const QString oldIcon = item->icon();
427 if (oldIcon == appInfo.icon) { // same icon file, perhaps different contents, simulate changing the icon name to force reload
428 item->setIcon(QString());
429 Q_EMIT dataChanged(index(idx), index(idx), {RoleIcon});
432 // now set the icon for real
433 item->setIcon(appInfo.icon);
434 Q_EMIT dataChanged(index(idx), index(idx), {RoleIcon});
438 Q_FOREACH (LauncherItem* item, toBeRemoved) {
439 unpin(item->appId());
442 bool changed = toBeRemoved.count() > 0;
444 // This brings the Launcher into sync with the settings backend again. There's an issue though:
445 // If we can't find a .desktop file for an entry we need to skip it. That makes our settingsIndex
446 // go out of sync with the actual index of items. So let's also use an addedIndex which reflects
447 // the settingsIndex minus the skipped items.
450 // Now walk through settings and see if we need to add something
451 for (int settingsIndex = 0; settingsIndex < m_settings->storedApplications().count(); ++settingsIndex) {
452 const QString entry = m_settings->storedApplications().at(settingsIndex);
454 for (int i = 0; i < m_list.count(); ++i) {
455 if (m_list.at(i)->appId() == entry) {
461 if (itemIndex == -1) {
462 // Need to add it. Just add it into the addedIndex to keep same ordering as the list
464 UalWrapper::AppInfo appInfo = UalWrapper::getApplicationInfo(entry);
465 if (!appInfo.valid) {
469 LauncherItem *item = new LauncherItem(entry,
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 if (!item->focused() && !item->alerting()) {
505 item->setAlerting(true);
506 Q_EMIT dataChanged(index(idx), index(idx), {RoleAlerting});
511void LauncherModel::applicationAdded(const QModelIndex &parent, int row)
515 ApplicationInfoInterface *app = m_appManager->get(row);
517 qWarning() << "LauncherModel received an applicationAdded signal, but there's no such application!";
521 if (app->appId() == QLatin1String("unity8-dash")) {
522 // Not adding the dash app
526 const int itemIndex = findApplication(app->appId());
527 if (itemIndex != -1) {
528 LauncherItem *item = m_list.at(itemIndex);
529 if (!item->recent()) {
530 item->setRecent(true);
531 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), {RoleRecent});
533 item->setRunning(true);
535 LauncherItem *item = new LauncherItem(app->appId(), app->name(), app->icon().toString(), this);
536 item->setRecent(true);
537 item->setRunning(true);
538 item->setFocused(app->focused());
539 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
543 connect(app, &ApplicationInfoInterface::surfaceCountChanged, this, &LauncherModel::applicationSurfaceCountChanged);
544 m_asAdapter->syncItems(m_list);
545 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), {RoleRunning});
548void LauncherModel::applicationSurfaceCountChanged(int count)
551 ApplicationInfoInterface *app = static_cast<ApplicationInfoInterface*>(sender());
552 int idx = findApplication(app->appId());
554 qWarning() << "Received a surface count changed event from an app that's not in the Launcher model";
557 LauncherItem *item = m_list.at(idx);
558 QList<QPair<QString, QString> > surfaces;
559 for (int i = 0; i < app->surfaceList()->count(); ++i) {
560 MirSurfaceInterface* iface = app->surfaceList()->get(i);
561 surfaces.append(qMakePair<QString, QString>(iface->persistentId(), iface->name()));
563 item->setSurfaces(surfaces);
564 Q_EMIT dataChanged(index(idx), index(idx), {RoleSurfaceCount});
567void LauncherModel::applicationRemoved(const QModelIndex &parent, int row)
571 ApplicationInfoInterface *app = m_appManager->get(row);
573 for (int i = 0; i < m_list.count(); ++i) {
574 if (m_list.at(i)->appId() == app->appId()) {
581 qWarning() << Q_FUNC_INFO << "appIndex not found";
585 disconnect(app, &ApplicationInfoInterface::surfaceCountChanged, this, &LauncherModel::applicationSurfaceCountChanged);
587 LauncherItem * item = m_list.at(appIndex);
589 if (!item->pinned()) {
590 beginRemoveRows(QModelIndex(), appIndex, appIndex);
591 m_list.takeAt(appIndex)->deleteLater();
593 m_asAdapter->syncItems(m_list);
595 QVector<int> changedRoles = {RoleRunning};
596 item->setRunning(false);
597 if (item->focused()) {
598 changedRoles << RoleFocused;
599 item->setFocused(false);
601 Q_EMIT dataChanged(index(appIndex), index(appIndex), changedRoles);
605void LauncherModel::focusedAppIdChanged()
607 const QString appId = m_appManager->focusedApplicationId();
608 for (int i = 0; i < m_list.count(); ++i) {
609 LauncherItem *item = m_list.at(i);
610 if (!item->focused() && item->appId() == appId) {
611 QVector<int> changedRoles;
612 changedRoles << RoleFocused;
613 item->setFocused(true);
614 if (item->alerting()) {
615 changedRoles << RoleAlerting;
616 item->setAlerting(false);
618 Q_EMIT dataChanged(index(i), index(i), changedRoles);
619 } else if (item->focused() && item->appId() != appId) {
620 item->setFocused(false);
621 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