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"
22#include "backend/launcherbackend.h"
24#include <unity/shell/application/ApplicationInfoInterface.h>
26#include <QDesktopServices>
29using namespace unity::shell::application;
31LauncherModel::LauncherModel(QObject *parent):
32 LauncherModelInterface(parent),
33 m_backend(new LauncherBackend(this)),
36 connect(m_backend, SIGNAL(countChanged(QString,int)), SLOT(countChanged(QString,int)));
37 connect(m_backend, SIGNAL(progressChanged(QString,int)), SLOT(progressChanged(QString,int)));
39 Q_FOREACH (const QString &entry, m_backend->storedApplications()) {
40 LauncherItem *item = new LauncherItem(entry,
41 m_backend->displayName(entry),
42 m_backend->icon(entry),
44 item->setPinned(true);
49LauncherModel::~LauncherModel()
51 while (!m_list.empty()) {
52 m_list.takeFirst()->deleteLater();
56int LauncherModel::rowCount(const QModelIndex &parent) const
59 return m_list.count();
62QVariant LauncherModel::data(const QModelIndex &index, int role) const
64 LauncherItem *item = m_list.at(index.row());
73 return item->pinned();
77 return item->progress();
79 return item->focused();
85unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
87 if (index < 0 || index >= m_list.count()) {
90 return m_list.at(index);
93void LauncherModel::move(int oldIndex, int newIndex)
95 // Make sure its not moved outside the lists
99 if (newIndex >= m_list.count()) {
100 newIndex = m_list.count()-1;
104 if (oldIndex == newIndex) {
108 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
109 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
110 // adjust the model's index by +1 in case we're moving upwards
111 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
113 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
114 m_list.move(oldIndex, newIndex);
117 if (!m_list.at(newIndex)->pinned()) {
118 pin(m_list.at(newIndex)->appId());
124void LauncherModel::pin(const QString &appId, int index)
126 int currentIndex = findApplication(appId);
128 if (currentIndex >= 0) {
129 if (index == -1 || index == currentIndex) {
130 m_list.at(currentIndex)->setPinned(true);
131 QModelIndex modelIndex = this->index(currentIndex);
132 Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RolePinned);
134 move(currentIndex, index);
135 // move() will store the list to the backend itself, so just exit at this point.
140 index = m_list.count();
142 beginInsertRows(QModelIndex(), index, index);
143 LauncherItem *item = new LauncherItem(appId,
144 m_backend->displayName(appId),
145 m_backend->icon(appId),
147 item->setPinned(true);
148 m_list.insert(index, item);
155void LauncherModel::requestRemove(const QString &appId)
157 int index = findApplication(appId);
162 if (m_appManager->findApplication(appId)) {
163 m_list.at(index)->setPinned(false);
164 QModelIndex modelIndex = this->index(index);
165 Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RolePinned);
169 beginRemoveRows(QModelIndex(), index, index);
170 m_list.takeAt(index)->deleteLater();
176void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
178 int index = findApplication(appId);
183 LauncherItem *item = m_list.at(index);
184 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
186 QString actionId = model->get(actionIndex).actionId();
188 // Check if this is one of the launcher actions we handle ourselves
189 if (actionId == "pin_item") {
190 if (item->pinned()) {
191 requestRemove(appId);
195 } else if (actionId == "launch_item") {
196 QDesktopServices::openUrl(getUrlForAppId(appId));
198 // Nope, we don't know this action, let the backend forward it to the application
200 m_backend->triggerQuickListAction(appId, actionId);
205void LauncherModel::setUser(const QString &username)
207 m_backend->setUser(username);
210QString LauncherModel::getUrlForAppId(const QString &appId) const
212 // appId is either an appId or a legacy app name. Let's find out which
216 QString df = m_backend->desktopFile(appId + ".desktop");
218 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);
270void LauncherModel::storeAppList()
273 Q_FOREACH(LauncherItem *item, m_list) {
274 if (item->pinned()) {
275 appIds << item->appId();
278 m_backend->setStoredApplications(appIds);
281int LauncherModel::findApplication(const QString &appId)
283 for (int i = 0; i < m_list.count(); ++i) {
284 LauncherItem *item = m_list.at(i);
285 if (item->appId() == appId) {
292void LauncherModel::progressChanged(const QString &appId, int progress)
294 int idx = findApplication(appId);
296 LauncherItem *item = m_list.at(idx);
297 item->setProgress(progress);
298 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleProgress);
303void LauncherModel::countChanged(const QString &appId, int count)
305 int idx = findApplication(appId);
307 LauncherItem *item = m_list.at(idx);
308 item->setCount(count);
309 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleCount);
313void LauncherModel::applicationAdded(const QModelIndex &parent, int row)
317 ApplicationInfoInterface *app = m_appManager->get(row);
319 qWarning() << "LauncherModel received an applicationAdded signal, but there's no such application!";
323 if (app->appId() == "unity8-dash") {
324 // Not adding the dash app
329 Q_FOREACH(LauncherItem *item, m_list) {
330 if (app->appId() == item->appId()) {
336 // Shall we paint some running/recent app highlight? If yes, do it here.
338 LauncherItem *item = new LauncherItem(app->appId(), app->name(), app->icon().toString(), this);
339 item->setRecent(true);
340 item->setFocused(app->focused());
342 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
348void LauncherModel::applicationRemoved(const QModelIndex &parent, int row)
353 for (int i = 0; i < m_list.count(); ++i) {
354 if (m_list.at(i)->appId() == m_appManager->get(row)->appId()) {
360 if (appIndex > -1 && !m_list.at(appIndex)->pinned()) {
361 beginRemoveRows(QModelIndex(), appIndex, appIndex);
362 m_list.takeAt(appIndex)->deleteLater();
367void LauncherModel::focusedAppIdChanged()
369 QString appId = m_appManager->focusedApplicationId();
370 for (int i = 0; i < m_list.count(); ++i) {
371 LauncherItem *item = m_list.at(i);
372 if (!item->focused() && item->appId() == appId) {
373 item->setFocused(true);
374 Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);
375 } else if (item->focused() && item->appId() != appId) {
376 item->setFocused(false);
377 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