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>
29#include <QDesktopServices>
32using namespace unity::shell::application;
34LauncherModel::LauncherModel(QObject *parent):
35 LauncherModelInterface(parent),
36 m_settings(new GSettings(this)),
37 m_dbusIface(new DBusInterface(this)),
38 m_asAdapter(new ASAdapter()),
41 connect(m_dbusIface, &DBusInterface::countChanged, this, &LauncherModel::countChanged);
42 connect(m_dbusIface, &DBusInterface::countVisibleChanged, this, &LauncherModel::countVisibleChanged);
43 connect(m_dbusIface, &DBusInterface::runningChanged, this, &LauncherModel::runningChanged);
44 connect(m_dbusIface, &DBusInterface::refreshCalled, this, &LauncherModel::refresh);
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->running();
89 qWarning() << Q_FUNC_INFO << "missing role, implement me";
96unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
98 if (index < 0 || index >= m_list.count()) {
101 return m_list.at(index);
104void LauncherModel::move(int oldIndex, int newIndex)
106 // Make sure its not moved outside the lists
110 if (newIndex >= m_list.count()) {
111 newIndex = m_list.count()-1;
115 if (oldIndex == newIndex) {
119 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
120 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
121 // adjust the model's index by +1 in case we're moving upwards
122 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
124 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
125 m_list.move(oldIndex, newIndex);
128 if (!m_list.at(newIndex)->pinned()) {
129 pin(m_list.at(newIndex)->appId());
135void LauncherModel::pin(const QString &appId, int index)
137 int currentIndex = findApplication(appId);
139 if (currentIndex >= 0) {
140 if (index == -1 || index == currentIndex) {
141 m_list.at(currentIndex)->setPinned(true);
142 QModelIndex modelIndex = this->index(currentIndex);
143 Q_EMIT dataChanged(modelIndex, modelIndex, {RolePinned});
145 move(currentIndex, index);
146 // move() will store the list to the backend itself, so just exit at this point.
151 index = m_list.count();
154 DesktopFileHandler desktopFile(appId);
155 if (!desktopFile.isValid()) {
156 qWarning() << "Can't pin this application, there is no .desktop file available.";
160 beginInsertRows(QModelIndex(), index, index);
161 LauncherItem *item = new LauncherItem(appId,
162 desktopFile.displayName(),
165 item->setPinned(true);
166 m_list.insert(index, item);
173void LauncherModel::requestRemove(const QString &appId)
179void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
181 const int index = findApplication(appId);
186 LauncherItem *item = m_list.at(index);
187 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
189 const QString actionId = model->get(actionIndex).actionId();
191 // Check if this is one of the launcher actions we handle ourselves
192 if (actionId == "pin_item") {
193 if (item->pinned()) {
194 requestRemove(appId);
198 } else if (actionId == "launch_item") {
199 QDesktopServices::openUrl(getUrlForAppId(appId));
200 } else if (actionId == "stop_item") { // Quit
201 m_appManager->stopApplication(appId);
202 // Nope, we don't know this action, let the backend forward it to the application
204 // TODO: forward quicklist action to app, possibly via m_dbusIface
209void LauncherModel::setUser(const QString &username)
212 qWarning() << "This backend doesn't support multiple users";
215QString LauncherModel::getUrlForAppId(const QString &appId) const
217 // appId is either an appId or a legacy app name. Let's find out which
218 if (appId.isEmpty()) {
222 if (!appId.contains("_")) {
223 return "application:///" + appId + ".desktop";
226 QStringList parts = appId.split('_');
227 QString package = parts.value(0);
228 QString app = parts.value(1, "first-listed-app");
229 return "appid://" + package + "/" + app + "/current-user-version";
232ApplicationManagerInterface *LauncherModel::applicationManager() const
237void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
239 // Is there already another appmanager set?
241 // Disconnect any signals
242 disconnect(this, SLOT(applicationAdded(QModelIndex,int)));
243 disconnect(this, SLOT(applicationRemoved(QModelIndex,int)));
244 disconnect(this, SLOT(focusedAppIdChanged()));
246 // remove any recent/running apps from the launcher
247 QList<int> recentAppIndices;
248 for (int i = 0; i < m_list.count(); ++i) {
249 if (m_list.at(i)->recent()) {
250 recentAppIndices << i;
254 while (recentAppIndices.count() > 0) {
255 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
256 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
258 recentAppIndices.takeFirst();
263 m_appManager = appManager;
264 connect(m_appManager, SIGNAL(rowsInserted(QModelIndex, int, int)), SLOT(applicationAdded(QModelIndex,int)));
265 connect(m_appManager, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), SLOT(applicationRemoved(QModelIndex,int)));
266 connect(m_appManager, SIGNAL(focusedApplicationIdChanged()), SLOT(focusedAppIdChanged()));
268 Q_EMIT applicationManagerChanged();
270 for (int i = 0; i < appManager->count(); ++i) {
271 applicationAdded(QModelIndex(), i);
275bool LauncherModel::onlyPinned() const
280void LauncherModel::setOnlyPinned(bool onlyPinned) {
281 Q_UNUSED(onlyPinned);
282 qWarning() << "This launcher implementation does not support showing only pinned apps";
285void LauncherModel::storeAppList()
288 Q_FOREACH(LauncherItem *item, m_list) {
289 if (item->pinned()) {
290 appIds << item->appId();
293 m_settings->setStoredApplications(appIds);
294 m_asAdapter->syncItems(m_list);
297void LauncherModel::unpin(const QString &appId)
299 const int index = findApplication(appId);
304 if (m_appManager->findApplication(appId)) {
305 if (m_list.at(index)->pinned()) {
306 m_list.at(index)->setPinned(false);
307 QModelIndex modelIndex = this->index(index);
308 Q_EMIT dataChanged(modelIndex, modelIndex, {RolePinned});
311 beginRemoveRows(QModelIndex(), index, index);
312 m_list.takeAt(index)->deleteLater();
317int LauncherModel::findApplication(const QString &appId)
319 for (int i = 0; i < m_list.count(); ++i) {
320 LauncherItem *item = m_list.at(i);
321 if (item->appId() == appId) {
328void LauncherModel::progressChanged(const QString &appId, int progress)
330 const int idx = findApplication(appId);
332 LauncherItem *item = m_list.at(idx);
333 item->setProgress(progress);
334 Q_EMIT dataChanged(index(idx), index(idx), {RoleProgress});
338void LauncherModel::runningChanged(const QString &appId, bool running)
340 const int idx = findApplication(appId);
342 LauncherItem *item = m_list.at(idx);
343 item->setRunning(running);
344 Q_EMIT dataChanged(index(idx), index(idx), {RoleRunning});
348void LauncherModel::countChanged(const QString &appId, int count)
350 const int idx = findApplication(appId);
352 LauncherItem *item = m_list.at(idx);
353 item->setCount(count);
354 Q_EMIT dataChanged(index(idx), index(idx), {RoleCount});
355 m_asAdapter->syncItems(m_list);
359void LauncherModel::countVisibleChanged(const QString &appId, int countVisible)
361 int idx = findApplication(appId);
363 LauncherItem *item = m_list.at(idx);
364 item->setCountVisible(countVisible);
365 Q_EMIT dataChanged(index(idx), index(idx), {RoleCountVisible});
367 // If countVisible goes to false, and the item is neither pinned nor recent we can drop it
368 if (!countVisible && !item->pinned() && !item->recent()) {
369 beginRemoveRows(QModelIndex(), idx, idx);
370 m_list.takeAt(idx)->deleteLater();
374 // Need to create a new LauncherItem and show the highlight
375 DesktopFileHandler desktopFile(appId);
376 if (countVisible && desktopFile.isValid()) {
377 LauncherItem *item = new LauncherItem(appId,
378 desktopFile.displayName(),
380 item->setCountVisible(true);
381 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
387 m_asAdapter->syncItems(m_list);
390void LauncherModel::refresh()
392 // First walk through all the existing items and see if we need to remove something
393 QList<LauncherItem*> toBeRemoved;
394 Q_FOREACH (LauncherItem* item, m_list) {
395 DesktopFileHandler desktopFile(item->appId());
396 if (!desktopFile.isValid()) {
397 // Desktop file not available for this app => drop it!
399 } else if (!m_settings->storedApplications().contains(item->appId())) {
400 // Item not in settings any more => drop it!
403 int idx = m_list.indexOf(item);
404 item->setName(desktopFile.displayName());
405 item->setIcon(desktopFile.icon());
406 item->setPinned(item->pinned()); // update pinned text if needed
407 Q_EMIT dataChanged(index(idx), index(idx), {RoleName, RoleIcon});
411 Q_FOREACH (LauncherItem* item, toBeRemoved) {
412 unpin(item->appId());
415 bool changed = toBeRemoved.count() > 0;
417 // This brings the Launcher into sync with the settings backend again. There's an issue though:
418 // If we can't find a .desktop file for an entry we need to skip it. That makes our settingsIndex
419 // go out of sync with the actual index of items. So let's also use an addedIndex which reflects
420 // the settingsIndex minus the skipped items.
423 // Now walk through settings and see if we need to add something
424 for (int settingsIndex = 0; settingsIndex < m_settings->storedApplications().count(); ++settingsIndex) {
425 const QString entry = m_settings->storedApplications().at(settingsIndex);
427 for (int i = 0; i < m_list.count(); ++i) {
428 if (m_list.at(i)->appId() == entry) {
434 if (itemIndex == -1) {
435 // Need to add it. Just add it into the addedIndex to keep same ordering as the list
437 DesktopFileHandler desktopFile(entry);
438 if (!desktopFile.isValid()) {
439 qWarning() << "Couldn't find a .desktop file for" << entry << ". Skipping...";
443 LauncherItem *item = new LauncherItem(entry,
444 desktopFile.displayName(),
447 item->setPinned(true);
448 beginInsertRows(QModelIndex(), addedIndex, addedIndex);
449 m_list.insert(addedIndex, item);
452 } else if (itemIndex != addedIndex) {
453 // The item is already there, but it is in a different place than in the settings.
454 // Move it to the addedIndex
455 beginMoveRows(QModelIndex(), itemIndex, itemIndex, QModelIndex(), addedIndex);
456 m_list.move(itemIndex, addedIndex);
461 // Just like settingsIndex, this will increase with every item, except the ones we
462 // skipped with the "continue" call above.
470 m_asAdapter->syncItems(m_list);
473void LauncherModel::applicationAdded(const QModelIndex &parent, int row)
477 ApplicationInfoInterface *app = m_appManager->get(row);
479 qWarning() << "LauncherModel received an applicationAdded signal, but there's no such application!";
483 if (app->appId() == "unity8-dash") {
484 // Not adding the dash app
488 const int itemIndex = findApplication(app->appId());
489 if (itemIndex != -1) {
490 LauncherItem *item = m_list.at(itemIndex);
491 if (!item->recent()) {
492 item->setRecent(true);
493 m_asAdapter->syncItems(m_list);
494 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), {RoleRecent});
496 // TODO Shall we paint some running/recent app highlight? If yes, do it here.
498 LauncherItem *item = new LauncherItem(app->appId(), app->name(), app->icon().toString(), this);
499 item->setRecent(true);
500 item->setRunning(true);
501 item->setFocused(app->focused());
503 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
506 m_asAdapter->syncItems(m_list);
510void LauncherModel::applicationRemoved(const QModelIndex &parent, int row)
515 for (int i = 0; i < m_list.count(); ++i) {
516 if (m_list.at(i)->appId() == m_appManager->get(row)->appId()) {
523 qWarning() << Q_FUNC_INFO << "appIndex not found";
527 LauncherItem * item = m_list.at(appIndex);
528 item->setRunning(false);
530 if (!item->pinned()) {
531 beginRemoveRows(QModelIndex(), appIndex, appIndex);
532 m_list.takeAt(appIndex)->deleteLater();
534 m_asAdapter->syncItems(m_list);
538void LauncherModel::focusedAppIdChanged()
540 const QString appId = m_appManager->focusedApplicationId();
541 for (int i = 0; i < m_list.count(); ++i) {
542 LauncherItem *item = m_list.at(i);
543 if (!item->focused() && item->appId() == appId) {
544 item->setFocused(true);
545 Q_EMIT dataChanged(index(i), index(i), {RoleFocused});
546 } else if (item->focused() && item->appId() != appId) {
547 item->setFocused(false);
548 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