2 * Copyright 2013 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"
26#include <unity/shell/application/ApplicationInfoInterface.h>
30using namespace unity::shell::application;
32LauncherModel::LauncherModel(QObject *parent):
33 LauncherModelInterface(parent),
34 m_settings(new GSettings(this)),
35 m_dbusIface(new DBusInterface(this)),
38 connect(m_dbusIface, &DBusInterface::countChanged, this, &LauncherModel::countChanged);
39 connect(m_dbusIface, &DBusInterface::countVisibleChanged, this, &LauncherModel::countVisibleChanged);
40 connect(m_dbusIface, &DBusInterface::refreshCalled, this, &LauncherModel::refresh);
42 Q_FOREACH (const QString &entry, m_settings->storedApplications()) {
43 if (DesktopFileHandler::findDesktopFile(entry).isEmpty()) {
44 qWarning() << "Couldn't find a .desktop file for" << entry << ". Skipping...";
48 LauncherItem *item = new LauncherItem(entry,
49 DesktopFileHandler::displayName(entry),
50 DesktopFileHandler::icon(entry),
52 item->setPinned(true);
57LauncherModel::~LauncherModel()
59 while (!m_list.empty()) {
60 m_list.takeFirst()->deleteLater();
64int LauncherModel::rowCount(const QModelIndex &parent) const
67 return m_list.count();
70QVariant LauncherModel::data(const QModelIndex &index, int role) const
72 LauncherItem *item = m_list.at(index.row());
81 return item->pinned();
84 case RoleCountVisible:
85 return item->countVisible();
87 return item->progress();
89 return item->focused();
95unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
97 if (index < 0 || index >= m_list.count()) {
100 return m_list.at(index);
103void LauncherModel::move(int oldIndex, int newIndex)
105 // Make sure its not moved outside the lists
109 if (newIndex >= m_list.count()) {
110 newIndex = m_list.count()-1;
114 if (oldIndex == newIndex) {
118 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
119 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
120 // adjust the model's index by +1 in case we're moving upwards
121 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
123 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
124 m_list.move(oldIndex, newIndex);
127 if (!m_list.at(newIndex)->pinned()) {
128 pin(m_list.at(newIndex)->appId());
134void LauncherModel::pin(const QString &appId, int index)
136 int currentIndex = findApplication(appId);
138 if (currentIndex >= 0) {
139 if (index == -1 || index == currentIndex) {
140 m_list.at(currentIndex)->setPinned(true);
141 QModelIndex modelIndex = this->index(currentIndex);
142 Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RolePinned);
144 move(currentIndex, index);
145 // move() will store the list to the backend itself, so just exit at this point.
150 index = m_list.count();
153 if (DesktopFileHandler::findDesktopFile(appId).isEmpty()) {
154 qWarning() << "Can't pin this application, there is no .destkop file available.";
158 beginInsertRows(QModelIndex(), index, index);
159 LauncherItem *item = new LauncherItem(appId,
160 DesktopFileHandler::displayName(appId),
161 DesktopFileHandler::icon(appId),
163 item->setPinned(true);
164 m_list.insert(index, item);
171void LauncherModel::requestRemove(const QString &appId)
173 int index = findApplication(appId);
178 if (m_appManager->findApplication(appId)) {
179 m_list.at(index)->setPinned(false);
180 QModelIndex modelIndex = this->index(index);
181 Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RolePinned);
185 beginRemoveRows(QModelIndex(), index, index);
186 m_list.takeAt(index)->deleteLater();
192void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
194 int index = findApplication(appId);
199 LauncherItem *item = m_list.at(index);
200 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
202 QString actionId = model->get(actionIndex).actionId();
204 // Check if this is one of the launcher actions we handle ourselves
205 if (actionId == "pin_item") {
206 if (item->pinned()) {
207 requestRemove(appId);
212 // Nope, we don't know this action, let the backend forward it to the application
214 // TODO: forward quicklist action to app, possibly via m_dbusIface
219void LauncherModel::setUser(const QString &username)
222 qWarning() << "This backend doesn't support multiple users";
225QString LauncherModel::getUrlForAppId(const QString &appId) const
227 // appId is either an appId or a legacy app name. Let's find out which
228 if (appId.isEmpty()) {
232 if (!appId.contains("_")) {
233 return "application:///" + appId + ".desktop";
236 QStringList parts = appId.split('_');
237 QString package = parts.value(0);
238 QString app = parts.value(1, "first-listed-app");
239 return "appid://" + package + "/" + app + "/current-user-version";
242ApplicationManagerInterface *LauncherModel::applicationManager() const
247void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
249 // Is there already another appmanager set?
251 // Disconnect any signals
252 disconnect(this, SLOT(applicationAdded(QModelIndex,int)));
253 disconnect(this, SLOT(applicationRemoved(QModelIndex,int)));
254 disconnect(this, SLOT(focusedAppIdChanged()));
256 // remove any recent/running apps from the launcher
257 QList<int> recentAppIndices;
258 for (int i = 0; i < m_list.count(); ++i) {
259 if (m_list.at(i)->recent()) {
260 recentAppIndices << i;
264 while (recentAppIndices.count() > 0) {
265 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
266 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
268 recentAppIndices.takeFirst();
273 m_appManager = appManager;
274 connect(m_appManager, SIGNAL(rowsInserted(QModelIndex, int, int)), SLOT(applicationAdded(QModelIndex,int)));
275 connect(m_appManager, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), SLOT(applicationRemoved(QModelIndex,int)));
276 connect(m_appManager, SIGNAL(focusedApplicationIdChanged()), SLOT(focusedAppIdChanged()));
278 Q_EMIT applicationManagerChanged();
280 for (int i = 0; i < appManager->count(); ++i) {
281 applicationAdded(QModelIndex(), i);
286void LauncherModel::storeAppList()
289 Q_FOREACH(LauncherItem *item, m_list) {
290 if (item->pinned()) {
291 appIds << item->appId();
294 m_settings->setStoredApplications(appIds);
297int LauncherModel::findApplication(const QString &appId)
299 for (int i = 0; i < m_list.count(); ++i) {
300 LauncherItem *item = m_list.at(i);
301 if (item->appId() == appId) {
308void LauncherModel::progressChanged(const QString &appId, int progress)
310 int idx = findApplication(appId);
312 LauncherItem *item = m_list.at(idx);
313 item->setProgress(progress);
314 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleProgress);
319void LauncherModel::countChanged(const QString &appId, int count)
321 int idx = findApplication(appId);
323 LauncherItem *item = m_list.at(idx);
324 item->setCount(count);
325 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleCount);
329void LauncherModel::countVisibleChanged(const QString &appId, int countVisible)
331 int idx = findApplication(appId);
333 LauncherItem *item = m_list.at(idx);
334 item->setCountVisible(countVisible);
335 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleCountVisible);
337 // If countVisible goes to false, and the item is neither pinned nor recent we can drop it
338 if (!countVisible && !item->pinned() && !item->recent()) {
339 beginRemoveRows(QModelIndex(), idx, idx);
340 m_list.takeAt(idx)->deleteLater();
344 // Need to create a new LauncherItem and show the highlight
345 if (countVisible && !DesktopFileHandler::findDesktopFile(appId).isEmpty()) {
346 LauncherItem *item = new LauncherItem(appId,
347 DesktopFileHandler::displayName(appId),
348 DesktopFileHandler::icon(appId));
349 item->setCountVisible(true);
350 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
357void LauncherModel::refresh()
359 QList<LauncherItem*> toBeRemoved;
360 Q_FOREACH (LauncherItem* item, m_list) {
361 if (DesktopFileHandler::findDesktopFile(item->appId()).isEmpty()) {
364 int idx = m_list.indexOf(item);
365 item->setName(DesktopFileHandler::displayName(item->appId()));
366 item->setIcon(DesktopFileHandler::icon(item->appId()));
367 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleName << RoleIcon);
371 Q_FOREACH (LauncherItem* item, toBeRemoved) {
372 requestRemove(item->appId());
376void LauncherModel::applicationAdded(const QModelIndex &parent, int row)
380 ApplicationInfoInterface *app = m_appManager->get(row);
382 qWarning() << "LauncherModel received an applicationAdded signal, but there's no such application!";
386 if (app->appId() == "unity8-dash") {
387 // Not adding the dash app
392 Q_FOREACH(LauncherItem *item, m_list) {
393 if (app->appId() == item->appId()) {
399 // Shall we paint some running/recent app highlight? If yes, do it here.
401 LauncherItem *item = new LauncherItem(app->appId(), app->name(), app->icon().toString(), this);
402 item->setRecent(true);
403 item->setFocused(app->focused());
405 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
411void LauncherModel::applicationRemoved(const QModelIndex &parent, int row)
416 for (int i = 0; i < m_list.count(); ++i) {
417 if (m_list.at(i)->appId() == m_appManager->get(row)->appId()) {
423 if (appIndex > -1 && !m_list.at(appIndex)->pinned()) {
424 beginRemoveRows(QModelIndex(), appIndex, appIndex);
425 m_list.takeAt(appIndex)->deleteLater();
430void LauncherModel::focusedAppIdChanged()
432 QString appId = m_appManager->focusedApplicationId();
433 for (int i = 0; i < m_list.count(); ++i) {
434 LauncherItem *item = m_list.at(i);
435 if (!item->focused() && item->appId() == appId) {
436 item->setFocused(true);
437 Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);
438 } else if (item->focused() && item->appId() != appId) {
439 item->setFocused(false);
440 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