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)),
36 m_desktopFileHandler(new DesktopFileHandler(this)),
39 connect(m_dbusIface, &DBusInterface::countChanged, this, &LauncherModel::countChanged);
40 connect(m_dbusIface, &DBusInterface::countVisibleChanged, this, &LauncherModel::countVisibleChanged);
41 connect(m_dbusIface, &DBusInterface::refreshCalled, this, &LauncherModel::refresh);
43 Q_FOREACH (const QString &entry, m_settings->storedApplications()) {
44 qDebug() << "got stored item:" << entry;
45 if (m_desktopFileHandler->findDesktopFile(entry).isEmpty()) {
46 qWarning() << "Couldn't find a .desktop file for" << entry << ". Skipping...";
50 LauncherItem *item = new LauncherItem(entry,
51 m_desktopFileHandler->displayName(entry),
52 m_desktopFileHandler->icon(entry),
54 item->setPinned(true);
56 qDebug() << "Loaded item" << item->name() << item->icon();
60LauncherModel::~LauncherModel()
62 while (!m_list.empty()) {
63 m_list.takeFirst()->deleteLater();
67int LauncherModel::rowCount(const QModelIndex &parent) const
70 return m_list.count();
73QVariant LauncherModel::data(const QModelIndex &index, int role) const
75 LauncherItem *item = m_list.at(index.row());
84 return item->pinned();
87 case RoleCountVisible:
88 return item->countVisible();
90 return item->progress();
92 return item->focused();
98unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
100 if (index < 0 || index >= m_list.count()) {
103 return m_list.at(index);
106void LauncherModel::move(int oldIndex, int newIndex)
108 // Make sure its not moved outside the lists
112 if (newIndex >= m_list.count()) {
113 newIndex = m_list.count()-1;
117 if (oldIndex == newIndex) {
121 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
122 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
123 // adjust the model's index by +1 in case we're moving upwards
124 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
126 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
127 m_list.move(oldIndex, newIndex);
130 if (!m_list.at(newIndex)->pinned()) {
131 pin(m_list.at(newIndex)->appId());
137void LauncherModel::pin(const QString &appId, int index)
139 int currentIndex = findApplication(appId);
141 if (currentIndex >= 0) {
142 if (index == -1 || index == currentIndex) {
143 m_list.at(currentIndex)->setPinned(true);
144 QModelIndex modelIndex = this->index(currentIndex);
145 Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RolePinned);
147 move(currentIndex, index);
148 // move() will store the list to the backend itself, so just exit at this point.
153 index = m_list.count();
156 if (m_desktopFileHandler->findDesktopFile(appId).isEmpty()) {
157 qWarning() << "Can't pin this application, there is no .destkop file available.";
161 beginInsertRows(QModelIndex(), index, index);
162 LauncherItem *item = new LauncherItem(appId,
163 m_desktopFileHandler->displayName(appId),
164 m_desktopFileHandler->icon(appId),
166 item->setPinned(true);
167 m_list.insert(index, item);
174void LauncherModel::requestRemove(const QString &appId)
176 int index = findApplication(appId);
181 if (m_appManager->findApplication(appId)) {
182 m_list.at(index)->setPinned(false);
183 QModelIndex modelIndex = this->index(index);
184 Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RolePinned);
188 beginRemoveRows(QModelIndex(), index, index);
189 m_list.takeAt(index)->deleteLater();
195void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
197 int index = findApplication(appId);
202 LauncherItem *item = m_list.at(index);
203 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
205 QString actionId = model->get(actionIndex).actionId();
207 // Check if this is one of the launcher actions we handle ourselves
208 if (actionId == "pin_item") {
209 if (item->pinned()) {
210 requestRemove(appId);
215 // Nope, we don't know this action, let the backend forward it to the application
217 // TODO: forward quicklist action to app, possibly via m_dbusIface
222void LauncherModel::setUser(const QString &username)
225 qWarning() << "This backend doesn't support multiple users";
228QString LauncherModel::getUrlForAppId(const QString &appId) const
230 // appId is either an appId or a legacy app name. Let's find out which
231 if (appId.isEmpty()) {
235 if (!appId.contains("_")) {
236 return "application:///" + appId + ".desktop";
239 QStringList parts = appId.split('_');
240 QString package = parts.value(0);
241 QString app = parts.value(1, "first-listed-app");
242 return "appid://" + package + "/" + app + "/current-user-version";
245ApplicationManagerInterface *LauncherModel::applicationManager() const
250void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
252 // Is there already another appmanager set?
254 // Disconnect any signals
255 disconnect(this, SLOT(applicationAdded(QModelIndex,int)));
256 disconnect(this, SLOT(applicationRemoved(QModelIndex,int)));
257 disconnect(this, SLOT(focusedAppIdChanged()));
259 // remove any recent/running apps from the launcher
260 QList<int> recentAppIndices;
261 for (int i = 0; i < m_list.count(); ++i) {
262 if (m_list.at(i)->recent()) {
263 recentAppIndices << i;
267 while (recentAppIndices.count() > 0) {
268 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
269 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
271 recentAppIndices.takeFirst();
276 m_appManager = appManager;
277 connect(m_appManager, SIGNAL(rowsInserted(QModelIndex, int, int)), SLOT(applicationAdded(QModelIndex,int)));
278 connect(m_appManager, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), SLOT(applicationRemoved(QModelIndex,int)));
279 connect(m_appManager, SIGNAL(focusedApplicationIdChanged()), SLOT(focusedAppIdChanged()));
281 Q_EMIT applicationManagerChanged();
283 for (int i = 0; i < appManager->count(); ++i) {
284 applicationAdded(QModelIndex(), i);
289void LauncherModel::storeAppList()
292 Q_FOREACH(LauncherItem *item, m_list) {
293 if (item->pinned()) {
294 appIds << item->appId();
297 m_settings->setStoredApplications(appIds);
300int LauncherModel::findApplication(const QString &appId)
302 for (int i = 0; i < m_list.count(); ++i) {
303 LauncherItem *item = m_list.at(i);
304 if (item->appId() == appId) {
311void LauncherModel::progressChanged(const QString &appId, int progress)
313 int idx = findApplication(appId);
315 LauncherItem *item = m_list.at(idx);
316 item->setProgress(progress);
317 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleProgress);
322void LauncherModel::countChanged(const QString &appId, int count)
324 int idx = findApplication(appId);
326 LauncherItem *item = m_list.at(idx);
327 item->setCount(count);
328 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleCount);
332void LauncherModel::countVisibleChanged(const QString &appId, int countVisible)
334 int idx = findApplication(appId);
336 LauncherItem *item = m_list.at(idx);
337 item->setCountVisible(countVisible);
338 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleCountVisible);
340 // If countVisible goes to false, and the item is neither pinned nor recent we can drop it
341 if (!countVisible && !item->pinned() && !item->recent()) {
342 beginRemoveRows(QModelIndex(), idx, idx);
343 m_list.takeAt(idx)->deleteLater();
347 // Need to create a new LauncherItem and show the highlight
348 if (countVisible && !m_desktopFileHandler->findDesktopFile(appId).isEmpty()) {
349 LauncherItem *item = new LauncherItem(appId,
350 m_desktopFileHandler->displayName(appId),
351 m_desktopFileHandler->icon(appId));
352 item->setCountVisible(true);
353 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
360void LauncherModel::refresh()
362 QList<LauncherItem*> toBeRemoved;
363 Q_FOREACH (LauncherItem* item, m_list) {
364 if (m_desktopFileHandler->findDesktopFile(item->appId()).isEmpty()) {
367 int idx = m_list.indexOf(item);
368 item->setName(m_desktopFileHandler->displayName(item->appId()));
369 item->setIcon(m_desktopFileHandler->icon(item->appId()));
370 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleName << RoleIcon);
374 Q_FOREACH (LauncherItem* item, toBeRemoved) {
375 requestRemove(item->appId());
379void LauncherModel::applicationAdded(const QModelIndex &parent, int row)
383 ApplicationInfoInterface *app = m_appManager->get(row);
385 qWarning() << "LauncherModel received an applicationAdded signal, but there's no such application!";
389 if (app->appId() == "unity8-dash") {
390 // Not adding the dash app
395 Q_FOREACH(LauncherItem *item, m_list) {
396 if (app->appId() == item->appId()) {
402 // Shall we paint some running/recent app highlight? If yes, do it here.
404 LauncherItem *item = new LauncherItem(app->appId(), app->name(), app->icon().toString(), this);
405 item->setRecent(true);
406 item->setFocused(app->focused());
408 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
414void LauncherModel::applicationRemoved(const QModelIndex &parent, int row)
419 for (int i = 0; i < m_list.count(); ++i) {
420 if (m_list.at(i)->appId() == m_appManager->get(row)->appId()) {
426 if (appIndex > -1 && !m_list.at(appIndex)->pinned()) {
427 beginRemoveRows(QModelIndex(), appIndex, appIndex);
428 m_list.takeAt(appIndex)->deleteLater();
433void LauncherModel::focusedAppIdChanged()
435 QString appId = m_appManager->focusedApplicationId();
436 for (int i = 0; i < m_list.count(); ++i) {
437 LauncherItem *item = m_list.at(i);
438 if (!item->focused() && item->appId() == appId) {
439 item->setFocused(true);
440 Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);
441 } else if (item->focused() && item->appId() != appId) {
442 item->setFocused(false);
443 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