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>
28using namespace unity::shell::application;
30LauncherModel::LauncherModel(QObject *parent):
31 LauncherModelInterface(parent),
32 m_greeterMode(qgetenv("XDG_SESSION_CLASS") == "greeter"),
33 m_backend(new LauncherBackend(m_greeterMode, 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)));
38 connect(m_backend, SIGNAL(refreshApplications()), SLOT(refreshStoredApplications()));
40 refreshStoredApplications();
43LauncherModel::~LauncherModel()
45 while (!m_list.empty()) {
46 m_list.takeFirst()->deleteLater();
50int LauncherModel::rowCount(const QModelIndex &parent) const
53 return m_list.count();
56QVariant LauncherModel::data(const QModelIndex &index, int role) const
58 LauncherItem *item = m_list.at(index.row());
67 return item->pinned();
71 return item->progress();
73 return item->focused();
79unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
81 if (index < 0 || index >= m_list.count()) {
84 return m_list.at(index);
87void LauncherModel::move(int oldIndex, int newIndex)
89 // Make sure its not moved outside the lists
93 if (newIndex >= m_list.count()) {
94 newIndex = m_list.count()-1;
98 if (oldIndex == newIndex) {
102 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
103 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
104 // adjust the model's index by +1 in case we're moving upwards
105 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
107 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
108 m_list.move(oldIndex, newIndex);
111 if (!m_list.at(newIndex)->pinned()) {
112 pin(m_list.at(newIndex)->appId());
118void LauncherModel::pin(const QString &appId, int index)
120 int currentIndex = findApplication(appId);
122 if (currentIndex >= 0) {
123 if (index == -1 || index == currentIndex) {
124 m_list.at(currentIndex)->setPinned(true);
125 QModelIndex modelIndex = this->index(currentIndex);
126 Q_EMIT dataChanged(modelIndex, modelIndex);
128 move(currentIndex, index);
129 // move() will store the list to the backend itself, so just exit at this point.
134 index = m_list.count();
136 beginInsertRows(QModelIndex(), index, index);
137 LauncherItem *item = new LauncherItem(appId,
138 m_backend->displayName(appId),
139 m_backend->icon(appId),
141 item->setPinned(true);
142 m_list.insert(index, item);
149void LauncherModel::requestRemove(const QString &appId)
151 int index = findApplication(appId);
156 if (m_appManager->findApplication(appId)) {
157 m_list.at(index)->setPinned(false);
161 beginRemoveRows(QModelIndex(), index, index);
162 m_list.takeAt(index)->deleteLater();
168void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
170 int index = findApplication(appId);
175 LauncherItem *item = m_list.at(index);
176 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
178 QString actionId = model->get(actionIndex).actionId();
180 // Check if this is one of the launcher actions we handle ourselves
181 if (actionId == "pin_item") {
182 if (item->pinned()) {
183 requestRemove(appId);
188 // Nope, we don't know this action, let the backend forward it to the application
190 m_backend->triggerQuickListAction(appId, actionId);
195void LauncherModel::setUser(const QString &username)
197 m_backend->setUser(username);
200QString LauncherModel::getUrlForAppId(const QString &appId) const
202 // appId is either an appId or a legacy app name. Let's find out which
206 QString df = m_backend->desktopFile(appId + ".desktop");
208 return "application:///" + appId + ".desktop";
210 QStringList parts = appId.split('_');
211 QString package = parts.value(0);
212 QString app = parts.value(1, "first-listed-app");
213 return "appid://" + package + "/" + app + "/current-user-version";
216void LauncherModel::refreshStoredApplications()
218 // First remove any existing ones
219 QList<int> storedAppIndices;
220 for (int i = 0; i < m_list.count(); ++i) {
221 if (!m_list.at(i)->recent()) {
222 storedAppIndices << i;
226 while (storedAppIndices.count() > 0) {
227 beginRemoveRows(QModelIndex(), storedAppIndices.first() - run, storedAppIndices.first() - run);
228 m_list.takeAt(storedAppIndices.first() - run)->deleteLater();
230 storedAppIndices.takeFirst();
234 // Now insert all stored apps at beginning of list
235 QStringList storedApplications = m_backend->storedApplications();
236 beginInsertRows(QModelIndex(), 0, storedApplications.size() - 1);
238 Q_FOREACH (const QString &entry, storedApplications) {
239 LauncherItem *item = new LauncherItem(entry,
240 m_backend->displayName(entry),
241 m_backend->icon(entry),
244 item->setPinned(true);
245 m_list.insert(run++, item);
250ApplicationManagerInterface *LauncherModel::applicationManager() const
255void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
257 // Is there already another appmanager set?
259 // Disconnect any signals
260 disconnect(this, SLOT(applicationAdded(QModelIndex,int)));
261 disconnect(this, SLOT(applicationRemoved(QModelIndex,int)));
262 disconnect(this, SLOT(focusedAppIdChanged()));
264 // remove any recent/running apps from the launcher
265 QList<int> recentAppIndices;
266 for (int i = 0; i < m_list.count(); ++i) {
267 if (m_list.at(i)->recent()) {
268 recentAppIndices << i;
272 while (recentAppIndices.count() > 0) {
273 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
274 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
276 recentAppIndices.takeFirst();
281 m_appManager = appManager;
282 connect(m_appManager, SIGNAL(rowsInserted(QModelIndex, int, int)), SLOT(applicationAdded(QModelIndex,int)));
283 connect(m_appManager, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), SLOT(applicationRemoved(QModelIndex,int)));
284 connect(m_appManager, SIGNAL(focusedApplicationIdChanged()), SLOT(focusedAppIdChanged()));
286 Q_EMIT applicationManagerChanged();
288 for (int i = 0; i < appManager->count(); ++i) {
289 applicationAdded(QModelIndex(), i);
294void LauncherModel::storeAppList()
297 Q_FOREACH(LauncherItem *item, m_list) {
298 if (item->pinned()) {
299 appIds << item->appId();
302 m_backend->setStoredApplications(appIds);
305int LauncherModel::findApplication(const QString &appId)
307 for (int i = 0; i < m_list.count(); ++i) {
308 LauncherItem *item = m_list.at(i);
309 if (item->appId() == appId) {
316void LauncherModel::progressChanged(const QString &appId, int progress)
318 int idx = findApplication(appId);
320 LauncherItem *item = m_list.at(idx);
321 item->setProgress(progress);
322 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleProgress);
327void LauncherModel::countChanged(const QString &appId, int count)
329 int idx = findApplication(appId);
331 LauncherItem *item = m_list.at(idx);
332 item->setCount(count);
333 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleCount);
337void LauncherModel::applicationAdded(const QModelIndex &parent, int row)
341 ApplicationInfoInterface *app = m_appManager->get(row);
343 qWarning() << "LauncherModel received an applicationAdded signal, but there's no such application!";
348 Q_FOREACH(LauncherItem *item, m_list) {
349 if (app->appId() == item->appId()) {
355 // Shall we paint some running/recent app highlight? If yes, do it here.
357 LauncherItem *item = new LauncherItem(app->appId(), app->name(), app->icon().toString(), !m_greeterMode);
358 item->setRecent(true);
359 item->setFocused(app->focused());
361 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
367void LauncherModel::applicationRemoved(const QModelIndex &parent, int row)
372 for (int i = 0; i < m_list.count(); ++i) {
373 if (m_list.at(i)->appId() == m_appManager->get(row)->appId()) {
379 if (appIndex > -1 && !m_list.at(appIndex)->pinned()) {
380 beginRemoveRows(QModelIndex(), appIndex, appIndex);
381 m_list.takeAt(appIndex)->deleteLater();
386void LauncherModel::focusedAppIdChanged()
388 QString appId = m_appManager->focusedApplicationId();
389 for (int i = 0; i < m_list.count(); ++i) {
390 LauncherItem *item = m_list.at(i);
391 if (!item->focused() && item->appId() == appId) {
392 item->setFocused(true);
393 Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);
394 } else if (item->focused() && item->appId() != appId) {
395 item->setFocused(false);
396 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