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>
26using namespace unity::shell::application;
28LauncherModel::LauncherModel(QObject *parent):
29 LauncherModelInterface(parent),
30 m_backend(new LauncherBackend(true, this)),
33 connect(m_backend, SIGNAL(countChanged(QString,int)), SLOT(countChanged(QString,int)));
34 connect(m_backend, SIGNAL(progressChanged(QString,int)), SLOT(progressChanged(QString,int)));
36 Q_FOREACH (const QString &entry, m_backend->storedApplications()) {
37 LauncherItem *item = new LauncherItem(entry,
38 m_backend->displayName(entry),
39 m_backend->icon(entry),
41 if (m_backend->isPinned(entry)) {
42 item->setPinned(true);
44 item->setRecent(true);
50LauncherModel::~LauncherModel()
52 while (!m_list.empty()) {
53 m_list.takeFirst()->deleteLater();
57int LauncherModel::rowCount(const QModelIndex &parent) const
60 return m_list.count();
63QVariant LauncherModel::data(const QModelIndex &index, int role) const
65 LauncherItem *item = m_list.at(index.row());
74 return item->pinned();
78 return item->progress();
80 return item->focused();
86unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
88 if (index < 0 || index >= m_list.count()) {
91 return m_list.at(index);
94void LauncherModel::move(int oldIndex, int newIndex)
96 // Make sure its not moved outside the lists
100 if (newIndex >= m_list.count()) {
101 newIndex = m_list.count()-1;
105 if (oldIndex == newIndex) {
109 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
110 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
111 // adjust the model's index by +1 in case we're moving upwards
112 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
114 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
115 m_list.move(oldIndex, newIndex);
118 if (!m_list.at(newIndex)->pinned()) {
119 pin(m_list.at(newIndex)->appId());
125void LauncherModel::pin(const QString &appId, int index)
127 int currentIndex = findApplication(appId);
129 if (currentIndex >= 0) {
130 if (index == -1 || index == currentIndex) {
131 m_list.at(currentIndex)->setPinned(true);
132 m_backend->setPinned(appId, true);
133 QModelIndex modelIndex = this->index(currentIndex);
134 Q_EMIT dataChanged(modelIndex, modelIndex);
136 move(currentIndex, index);
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));
146 item->setPinned(true);
147 m_backend->setPinned(appId, 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);
167 beginRemoveRows(QModelIndex(), index, index);
168 m_list.takeAt(index)->deleteLater();
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);
194 // Nope, we don't know this action, let the backend forward it to the application
196 m_backend->triggerQuickListAction(appId, actionId);
201void LauncherModel::setUser(const QString &username)
203 m_backend->setUser(username);
206ApplicationManagerInterface *LauncherModel::applicationManager() const
211void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
213 // Is there already another appmanager set?
215 // Disconnect any signals
216 disconnect(this, SLOT(applicationAdded(QModelIndex,int)));
217 disconnect(this, SLOT(applicationRemoved(QModelIndex,int)));
218 disconnect(this, SLOT(focusedAppIdChanged()));
220 // remove any recent/running apps from the launcher
221 QList<int> recentAppIndices;
222 for (int i = 0; i < m_list.count(); ++i) {
223 if (m_list.at(i)->recent()) {
224 recentAppIndices << i;
228 while (recentAppIndices.count() > 0) {
229 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
230 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
232 recentAppIndices.takeFirst();
237 m_appManager = appManager;
238 connect(m_appManager, SIGNAL(rowsInserted(QModelIndex, int, int)), SLOT(applicationAdded(QModelIndex,int)));
239 connect(m_appManager, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), SLOT(applicationRemoved(QModelIndex,int)));
240 connect(m_appManager, SIGNAL(focusedApplicationIdChanged()), SLOT(focusedAppIdChanged()));
242 Q_EMIT applicationManagerChanged();
244 for (int i = 0; i < appManager->count(); ++i) {
245 applicationAdded(QModelIndex(), i);
250void LauncherModel::storeAppList()
253 Q_FOREACH(LauncherItem *item, m_list) {
254 if (item->pinned()) {
255 appIds << item->appId();
258 m_backend->setStoredApplications(appIds);
261int LauncherModel::findApplication(const QString &appId)
263 for (int i = 0; i < m_list.count(); ++i) {
264 LauncherItem *item = m_list.at(i);
265 if (item->appId() == appId) {
272void LauncherModel::progressChanged(const QString &appId, int progress)
274 int idx = findApplication(appId);
276 LauncherItem *item = m_list.at(idx);
277 item->setProgress(progress);
278 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleProgress);
283void LauncherModel::countChanged(const QString &appId, int count)
285 int idx = findApplication(appId);
287 LauncherItem *item = m_list.at(idx);
288 item->setCount(count);
289 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleCount);
293void LauncherModel::applicationAdded(const QModelIndex &parent, int row)
297 ApplicationInfoInterface *app = m_appManager->get(row);
300 Q_FOREACH(LauncherItem *item, m_list) {
301 if (app->appId() == item->appId()) {
307 // Shall we paint some running/recent app highlight? If yes, do it here.
309 LauncherItem *item = new LauncherItem(app->appId(), app->name(), app->icon().toString());
310 item->setRecent(true);
311 item->setFocused(app->focused());
313 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
319void LauncherModel::applicationRemoved(const QModelIndex &parent, int row)
324 for (int i = 0; i < m_list.count(); ++i) {
325 if (m_list.at(i)->appId() == m_appManager->get(row)->appId()) {
331 if (appIndex > -1 && !m_list.at(appIndex)->pinned()) {
332 beginRemoveRows(QModelIndex(), appIndex, appIndex);
333 m_list.takeAt(appIndex)->deleteLater();
338void LauncherModel::focusedAppIdChanged()
340 QString appId = m_appManager->focusedApplicationId();
341 for (int i = 0; i < m_list.count(); ++i) {
342 LauncherItem *item = m_list.at(i);
343 if (!item->focused() && item->appId() == appId) {
344 item->setFocused(true);
345 Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);
346 } else if (item->focused() && item->appId() != appId) {
347 item->setFocused(false);
348 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