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);
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();
91unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
93 if (index < 0 || index >= m_list.count()) {
96 return m_list.at(index);
99void LauncherModel::move(int oldIndex, int newIndex)
101 // Make sure its not moved outside the lists
105 if (newIndex >= m_list.count()) {
106 newIndex = m_list.count()-1;
110 if (oldIndex == newIndex) {
114 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
115 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
116 // adjust the model's index by +1 in case we're moving upwards
117 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
119 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
120 m_list.move(oldIndex, newIndex);
123 if (!m_list.at(newIndex)->pinned()) {
124 pin(m_list.at(newIndex)->appId());
130void LauncherModel::pin(const QString &appId, int index)
132 int currentIndex = findApplication(appId);
134 if (currentIndex >= 0) {
135 if (index == -1 || index == currentIndex) {
136 m_list.at(currentIndex)->setPinned(true);
137 QModelIndex modelIndex = this->index(currentIndex);
138 Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RolePinned);
140 move(currentIndex, index);
141 // move() will store the list to the backend itself, so just exit at this point.
146 index = m_list.count();
149 DesktopFileHandler desktopFile(appId);
150 if (!desktopFile.isValid()) {
151 qWarning() << "Can't pin this application, there is no .destkop file available.";
155 beginInsertRows(QModelIndex(), index, index);
156 LauncherItem *item = new LauncherItem(appId,
157 desktopFile.displayName(),
160 item->setPinned(true);
161 m_list.insert(index, item);
168void LauncherModel::requestRemove(const QString &appId)
174void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
176 int index = findApplication(appId);
181 LauncherItem *item = m_list.at(index);
182 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
184 QString actionId = model->get(actionIndex).actionId();
186 // Check if this is one of the launcher actions we handle ourselves
187 if (actionId == "pin_item") {
188 if (item->pinned()) {
189 requestRemove(appId);
193 } else if (actionId == "launch_item") {
194 QDesktopServices::openUrl(getUrlForAppId(appId));
196 // Nope, we don't know this action, let the backend forward it to the application
198 // TODO: forward quicklist action to app, possibly via m_dbusIface
203void LauncherModel::setUser(const QString &username)
206 qWarning() << "This backend doesn't support multiple users";
209QString LauncherModel::getUrlForAppId(const QString &appId) const
211 // appId is either an appId or a legacy app name. Let's find out which
212 if (appId.isEmpty()) {
216 if (!appId.contains("_")) {
217 return "application:///" + appId + ".desktop";
220 QStringList parts = appId.split('_');
221 QString package = parts.value(0);
222 QString app = parts.value(1, "first-listed-app");
223 return "appid://" + package + "/" + app + "/current-user-version";
226ApplicationManagerInterface *LauncherModel::applicationManager() const
231void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
233 // Is there already another appmanager set?
235 // Disconnect any signals
236 disconnect(this, SLOT(applicationAdded(QModelIndex,int)));
237 disconnect(this, SLOT(applicationRemoved(QModelIndex,int)));
238 disconnect(this, SLOT(focusedAppIdChanged()));
240 // remove any recent/running apps from the launcher
241 QList<int> recentAppIndices;
242 for (int i = 0; i < m_list.count(); ++i) {
243 if (m_list.at(i)->recent()) {
244 recentAppIndices << i;
248 while (recentAppIndices.count() > 0) {
249 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
250 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
252 recentAppIndices.takeFirst();
257 m_appManager = appManager;
258 connect(m_appManager, SIGNAL(rowsInserted(QModelIndex, int, int)), SLOT(applicationAdded(QModelIndex,int)));
259 connect(m_appManager, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), SLOT(applicationRemoved(QModelIndex,int)));
260 connect(m_appManager, SIGNAL(focusedApplicationIdChanged()), SLOT(focusedAppIdChanged()));
262 Q_EMIT applicationManagerChanged();
264 for (int i = 0; i < appManager->count(); ++i) {
265 applicationAdded(QModelIndex(), i);
269bool LauncherModel::onlyPinned() const
274void LauncherModel::setOnlyPinned(bool onlyPinned) {
275 Q_UNUSED(onlyPinned);
276 qWarning() << "This launcher implementation does not support showing only pinned apps";
279void LauncherModel::storeAppList()
282 Q_FOREACH(LauncherItem *item, m_list) {
283 if (item->pinned()) {
284 appIds << item->appId();
287 m_settings->setStoredApplications(appIds);
288 m_asAdapter->syncItems(m_list);
291void LauncherModel::unpin(const QString &appId)
293 int index = findApplication(appId);
298 if (m_appManager->findApplication(appId)) {
299 if (m_list.at(index)->pinned()) {
300 m_list.at(index)->setPinned(false);
301 QModelIndex modelIndex = this->index(index);
302 Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RolePinned);
305 beginRemoveRows(QModelIndex(), index, index);
306 m_list.takeAt(index)->deleteLater();
311int LauncherModel::findApplication(const QString &appId)
313 for (int i = 0; i < m_list.count(); ++i) {
314 LauncherItem *item = m_list.at(i);
315 if (item->appId() == appId) {
322void LauncherModel::progressChanged(const QString &appId, int progress)
324 int idx = findApplication(appId);
326 LauncherItem *item = m_list.at(idx);
327 item->setProgress(progress);
328 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleProgress);
332void LauncherModel::countChanged(const QString &appId, int count)
334 int idx = findApplication(appId);
336 LauncherItem *item = m_list.at(idx);
337 item->setCount(count);
338 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleCount);
339 m_asAdapter->syncItems(m_list);
343void LauncherModel::countVisibleChanged(const QString &appId, int countVisible)
345 int idx = findApplication(appId);
347 LauncherItem *item = m_list.at(idx);
348 item->setCountVisible(countVisible);
349 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleCountVisible);
351 // If countVisible goes to false, and the item is neither pinned nor recent we can drop it
352 if (!countVisible && !item->pinned() && !item->recent()) {
353 beginRemoveRows(QModelIndex(), idx, idx);
354 m_list.takeAt(idx)->deleteLater();
358 // Need to create a new LauncherItem and show the highlight
359 DesktopFileHandler desktopFile(appId);
360 if (countVisible && desktopFile.isValid()) {
361 LauncherItem *item = new LauncherItem(appId,
362 desktopFile.displayName(),
364 item->setCountVisible(true);
365 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
371 m_asAdapter->syncItems(m_list);
374void LauncherModel::refresh()
376 // First walk through all the existing items and see if we need to remove something
377 QList<LauncherItem*> toBeRemoved;
378 Q_FOREACH (LauncherItem* item, m_list) {
379 DesktopFileHandler desktopFile(item->appId());
380 if (!desktopFile.isValid()) {
381 // Desktop file not available for this app => drop it!
383 } else if (!m_settings->storedApplications().contains(item->appId())) {
384 // Item not in settings any more => drop it!
387 int idx = m_list.indexOf(item);
388 item->setName(desktopFile.displayName());
389 item->setIcon(desktopFile.icon());
390 item->setPinned(item->pinned()); // update pinned text if needed
391 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleName << RoleIcon);
395 Q_FOREACH (LauncherItem* item, toBeRemoved) {
396 unpin(item->appId());
399 bool changed = toBeRemoved.count() > 0;
401 // This brings the Launcher into sync with the settings backend again. There's an issue though:
402 // If we can't find a .desktop file for an entry we need to skip it. That makes our settingsIndex
403 // go out of sync with the actual index of items. So let's also use an addedIndex which reflects
404 // the settingsIndex minus the skipped items.
407 // Now walk through settings and see if we need to add something
408 for (int settingsIndex = 0; settingsIndex < m_settings->storedApplications().count(); ++settingsIndex) {
409 QString entry = m_settings->storedApplications().at(settingsIndex);
411 for (int i = 0; i < m_list.count(); ++i) {
412 if (m_list.at(i)->appId() == entry) {
418 if (itemIndex == -1) {
419 // Need to add it. Just add it into the addedIndex to keep same ordering as the list
421 DesktopFileHandler desktopFile(entry);
422 if (!desktopFile.isValid()) {
423 qWarning() << "Couldn't find a .desktop file for" << entry << ". Skipping...";
427 LauncherItem *item = new LauncherItem(entry,
428 desktopFile.displayName(),
431 item->setPinned(true);
432 beginInsertRows(QModelIndex(), addedIndex, addedIndex);
433 m_list.insert(addedIndex, item);
436 } else if (itemIndex != addedIndex) {
437 // The item is already there, but it is in a different place than in the settings.
438 // Move it to the addedIndex
439 beginMoveRows(QModelIndex(), itemIndex, itemIndex, QModelIndex(), addedIndex);
440 m_list.move(itemIndex, addedIndex);
445 // Just like settingsIndex, this will increase with every item, except the ones we
446 // skipped with the "continue" call above.
454 m_asAdapter->syncItems(m_list);
457void LauncherModel::applicationAdded(const QModelIndex &parent, int row)
461 ApplicationInfoInterface *app = m_appManager->get(row);
463 qWarning() << "LauncherModel received an applicationAdded signal, but there's no such application!";
467 if (app->appId() == "unity8-dash") {
468 // Not adding the dash app
472 int itemIndex = findApplication(app->appId());
473 if (itemIndex != -1) {
474 LauncherItem *item = m_list.at(itemIndex);
475 if (!item->recent()) {
476 item->setRecent(true);
477 m_asAdapter->syncItems(m_list);
478 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), QVector<int>() << RoleRecent);
480 // Shall we paint some running/recent app highlight? If yes, do it here.
482 LauncherItem *item = new LauncherItem(app->appId(), app->name(), app->icon().toString(), this);
483 item->setRecent(true);
484 item->setFocused(app->focused());
486 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
489 m_asAdapter->syncItems(m_list);
493void LauncherModel::applicationRemoved(const QModelIndex &parent, int row)
498 for (int i = 0; i < m_list.count(); ++i) {
499 if (m_list.at(i)->appId() == m_appManager->get(row)->appId()) {
505 if (appIndex > -1 && !m_list.at(appIndex)->pinned()) {
506 beginRemoveRows(QModelIndex(), appIndex, appIndex);
507 m_list.takeAt(appIndex)->deleteLater();
509 m_asAdapter->syncItems(m_list);
513void LauncherModel::focusedAppIdChanged()
515 QString appId = m_appManager->focusedApplicationId();
516 for (int i = 0; i < m_list.count(); ++i) {
517 LauncherItem *item = m_list.at(i);
518 if (!item->focused() && item->appId() == appId) {
519 item->setFocused(true);
520 Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);
521 } else if (item->focused() && item->appId() != appId) {
522 item->setFocused(false);
523 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