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::progressChanged, this, &LauncherModel::progressChanged);
44 connect(m_dbusIface, &DBusInterface::refreshCalled, this, &LauncherModel::refresh);
45 connect(m_dbusIface, &DBusInterface::alertCalled, this, &LauncherModel::alert);
47 connect(m_settings, &GSettings::changed, this, &LauncherModel::refresh);
52LauncherModel::~LauncherModel()
54 while (!m_list.empty()) {
55 m_list.takeFirst()->deleteLater();
61int LauncherModel::rowCount(const QModelIndex &parent) const
64 return m_list.count();
67QVariant LauncherModel::data(const QModelIndex &index, int role) const
69 LauncherItem *item = m_list.at(index.row());
78 return item->pinned();
81 case RoleCountVisible:
82 return item->countVisible();
84 return item->progress();
86 return item->focused();
88 return item->alerting();
94void LauncherModel::setAlerting(const QString &appId, bool alerting) {
95 int index = findApplication(appId);
97 QModelIndex modelIndex = this->index(index);
98 LauncherItem *item = m_list.at(index);
99 if (!item->focused()) {
100 item->setAlerting(alerting);
101 Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RoleAlerting);
106unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
108 if (index < 0 || index >= m_list.count()) {
111 return m_list.at(index);
114void LauncherModel::move(int oldIndex, int newIndex)
116 // Make sure its not moved outside the lists
120 if (newIndex >= m_list.count()) {
121 newIndex = m_list.count()-1;
125 if (oldIndex == newIndex) {
129 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
130 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
131 // adjust the model's index by +1 in case we're moving upwards
132 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
134 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
135 m_list.move(oldIndex, newIndex);
138 if (!m_list.at(newIndex)->pinned()) {
139 pin(m_list.at(newIndex)->appId());
145void LauncherModel::pin(const QString &appId, int index)
147 int currentIndex = findApplication(appId);
149 if (currentIndex >= 0) {
150 if (index == -1 || index == currentIndex) {
151 m_list.at(currentIndex)->setPinned(true);
152 QModelIndex modelIndex = this->index(currentIndex);
153 Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RolePinned);
155 move(currentIndex, index);
156 // move() will store the list to the backend itself, so just exit at this point.
161 index = m_list.count();
164 DesktopFileHandler desktopFile(appId);
165 if (!desktopFile.isValid()) {
166 qWarning() << "Can't pin this application, there is no .destkop file available.";
170 beginInsertRows(QModelIndex(), index, index);
171 LauncherItem *item = new LauncherItem(appId,
172 desktopFile.displayName(),
175 item->setPinned(true);
176 m_list.insert(index, item);
183void LauncherModel::requestRemove(const QString &appId)
189void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
191 int index = findApplication(appId);
196 LauncherItem *item = m_list.at(index);
197 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
199 QString actionId = model->get(actionIndex).actionId();
201 // Check if this is one of the launcher actions we handle ourselves
202 if (actionId == "pin_item") {
203 if (item->pinned()) {
204 requestRemove(appId);
208 } else if (actionId == "launch_item") {
209 QDesktopServices::openUrl(getUrlForAppId(appId));
211 // Nope, we don't know this action, let the backend forward it to the application
213 // TODO: forward quicklist action to app, possibly via m_dbusIface
218void LauncherModel::setUser(const QString &username)
221 qWarning() << "This backend doesn't support multiple users";
224QString LauncherModel::getUrlForAppId(const QString &appId) const
226 // appId is either an appId or a legacy app name. Let's find out which
227 if (appId.isEmpty()) {
231 if (!appId.contains("_")) {
232 return "application:///" + appId + ".desktop";
235 QStringList parts = appId.split('_');
236 QString package = parts.value(0);
237 QString app = parts.value(1, "first-listed-app");
238 return "appid://" + package + "/" + app + "/current-user-version";
241ApplicationManagerInterface *LauncherModel::applicationManager() const
246void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
248 // Is there already another appmanager set?
250 // Disconnect any signals
251 disconnect(this, SLOT(applicationAdded(QModelIndex,int)));
252 disconnect(this, SLOT(applicationRemoved(QModelIndex,int)));
253 disconnect(this, SLOT(focusedAppIdChanged()));
255 // remove any recent/running apps from the launcher
256 QList<int> recentAppIndices;
257 for (int i = 0; i < m_list.count(); ++i) {
258 if (m_list.at(i)->recent()) {
259 recentAppIndices << i;
263 while (recentAppIndices.count() > 0) {
264 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
265 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
267 recentAppIndices.takeFirst();
272 m_appManager = appManager;
273 connect(m_appManager, SIGNAL(rowsInserted(QModelIndex, int, int)), SLOT(applicationAdded(QModelIndex,int)));
274 connect(m_appManager, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), SLOT(applicationRemoved(QModelIndex,int)));
275 connect(m_appManager, SIGNAL(focusedApplicationIdChanged()), SLOT(focusedAppIdChanged()));
277 Q_EMIT applicationManagerChanged();
279 for (int i = 0; i < appManager->count(); ++i) {
280 applicationAdded(QModelIndex(), i);
284bool LauncherModel::onlyPinned() const
289void LauncherModel::setOnlyPinned(bool onlyPinned) {
290 Q_UNUSED(onlyPinned);
291 qWarning() << "This launcher implementation does not support showing only pinned apps";
294void LauncherModel::storeAppList()
297 Q_FOREACH(LauncherItem *item, m_list) {
298 if (item->pinned()) {
299 appIds << item->appId();
302 m_settings->setStoredApplications(appIds);
303 m_asAdapter->syncItems(m_list);
306void LauncherModel::unpin(const QString &appId)
308 int index = findApplication(appId);
313 if (m_appManager->findApplication(appId)) {
314 if (m_list.at(index)->pinned()) {
315 m_list.at(index)->setPinned(false);
316 QModelIndex modelIndex = this->index(index);
317 Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RolePinned);
320 beginRemoveRows(QModelIndex(), index, index);
321 m_list.takeAt(index)->deleteLater();
326int LauncherModel::findApplication(const QString &appId)
328 for (int i = 0; i < m_list.count(); ++i) {
329 LauncherItem *item = m_list.at(i);
330 if (item->appId() == appId) {
337void LauncherModel::progressChanged(const QString &appId, int progress)
339 int idx = findApplication(appId);
341 LauncherItem *item = m_list.at(idx);
342 item->setProgress(progress);
343 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleProgress);
347void LauncherModel::countChanged(const QString &appId, int count)
349 int idx = findApplication(appId);
351 LauncherItem *item = m_list.at(idx);
352 item->setCount(count);
353 if (item->countVisible()) {
354 setAlerting(item->appId(), true);
356 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleCount);
357 m_asAdapter->syncItems(m_list);
361void LauncherModel::countVisibleChanged(const QString &appId, int countVisible)
363 int idx = findApplication(appId);
365 LauncherItem *item = m_list.at(idx);
366 item->setCountVisible(countVisible);
368 setAlerting(item->appId(), true);
370 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleCountVisible);
372 // If countVisible goes to false, and the item is neither pinned nor recent we can drop it
373 if (!countVisible && !item->pinned() && !item->recent()) {
374 beginRemoveRows(QModelIndex(), idx, idx);
375 m_list.takeAt(idx)->deleteLater();
379 // Need to create a new LauncherItem and show the highlight
380 DesktopFileHandler desktopFile(appId);
381 if (countVisible && desktopFile.isValid()) {
382 LauncherItem *item = new LauncherItem(appId,
383 desktopFile.displayName(),
385 item->setCountVisible(true);
386 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
392 m_asAdapter->syncItems(m_list);
395void LauncherModel::refresh()
397 // First walk through all the existing items and see if we need to remove something
398 QList<LauncherItem*> toBeRemoved;
399 Q_FOREACH (LauncherItem* item, m_list) {
400 DesktopFileHandler desktopFile(item->appId());
401 if (!desktopFile.isValid()) {
402 // Desktop file not available for this app => drop it!
404 } else if (!m_settings->storedApplications().contains(item->appId())) {
405 // Item not in settings any more => drop it!
408 int idx = m_list.indexOf(item);
409 item->setName(desktopFile.displayName());
410 item->setIcon(desktopFile.icon());
411 item->setPinned(item->pinned()); // update pinned text if needed
412 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleName << RoleIcon);
416 Q_FOREACH (LauncherItem* item, toBeRemoved) {
417 unpin(item->appId());
420 bool changed = toBeRemoved.count() > 0;
422 // This brings the Launcher into sync with the settings backend again. There's an issue though:
423 // If we can't find a .desktop file for an entry we need to skip it. That makes our settingsIndex
424 // go out of sync with the actual index of items. So let's also use an addedIndex which reflects
425 // the settingsIndex minus the skipped items.
428 // Now walk through settings and see if we need to add something
429 for (int settingsIndex = 0; settingsIndex < m_settings->storedApplications().count(); ++settingsIndex) {
430 QString entry = m_settings->storedApplications().at(settingsIndex);
432 for (int i = 0; i < m_list.count(); ++i) {
433 if (m_list.at(i)->appId() == entry) {
439 if (itemIndex == -1) {
440 // Need to add it. Just add it into the addedIndex to keep same ordering as the list
442 DesktopFileHandler desktopFile(entry);
443 if (!desktopFile.isValid()) {
444 qWarning() << "Couldn't find a .desktop file for" << entry << ". Skipping...";
448 LauncherItem *item = new LauncherItem(entry,
449 desktopFile.displayName(),
452 item->setPinned(true);
453 beginInsertRows(QModelIndex(), addedIndex, addedIndex);
454 m_list.insert(addedIndex, item);
457 } else if (itemIndex != addedIndex) {
458 // The item is already there, but it is in a different place than in the settings.
459 // Move it to the addedIndex
460 beginMoveRows(QModelIndex(), itemIndex, itemIndex, QModelIndex(), addedIndex);
461 m_list.move(itemIndex, addedIndex);
466 // Just like settingsIndex, this will increase with every item, except the ones we
467 // skipped with the "continue" call above.
475 m_asAdapter->syncItems(m_list);
478void LauncherModel::alert(const QString &appId)
480 int idx = findApplication(appId);
482 LauncherItem *item = m_list.at(idx);
483 setAlerting(item->appId(), true);
484 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleAlerting);
488void LauncherModel::applicationAdded(const QModelIndex &parent, int row)
492 ApplicationInfoInterface *app = m_appManager->get(row);
494 qWarning() << "LauncherModel received an applicationAdded signal, but there's no such application!";
498 if (app->appId() == "unity8-dash") {
499 // Not adding the dash app
503 int itemIndex = findApplication(app->appId());
504 if (itemIndex != -1) {
505 LauncherItem *item = m_list.at(itemIndex);
506 if (!item->recent()) {
507 item->setRecent(true);
508 m_asAdapter->syncItems(m_list);
509 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), QVector<int>() << RoleRecent);
511 // Shall we paint some running/recent app highlight? If yes, do it here.
513 LauncherItem *item = new LauncherItem(app->appId(), app->name(), app->icon().toString(), this);
514 item->setRecent(true);
515 item->setFocused(app->focused());
517 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
520 m_asAdapter->syncItems(m_list);
524void LauncherModel::applicationRemoved(const QModelIndex &parent, int row)
529 for (int i = 0; i < m_list.count(); ++i) {
530 if (m_list.at(i)->appId() == m_appManager->get(row)->appId()) {
536 if (appIndex > -1 && !m_list.at(appIndex)->pinned()) {
537 beginRemoveRows(QModelIndex(), appIndex, appIndex);
538 m_list.takeAt(appIndex)->deleteLater();
540 m_asAdapter->syncItems(m_list);
544void LauncherModel::focusedAppIdChanged()
546 QString appId = m_appManager->focusedApplicationId();
547 for (int i = 0; i < m_list.count(); ++i) {
548 LauncherItem *item = m_list.at(i);
549 if (!item->focused() && item->appId() == appId) {
550 item->setFocused(true);
551 Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);
552 } else if (item->focused() && item->appId() != appId) {
553 item->setFocused(false);
554 Q_EMIT dataChanged(index(i), index(i), QVector<int>() << 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