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_backend(new LauncherBackend(this)),
35 connect(m_backend, SIGNAL(countChanged(QString,int)), SLOT(countChanged(QString,int)));
36 connect(m_backend, SIGNAL(progressChanged(QString,int)), SLOT(progressChanged(QString,int)));
38 Q_FOREACH (const QString &entry, m_backend->storedApplications()) {
39 LauncherItem *item = new LauncherItem(entry,
40 m_backend->displayName(entry),
41 m_backend->icon(entry),
43 item->setPinned(true);
48LauncherModel::~LauncherModel()
50 while (!m_list.empty()) {
51 m_list.takeFirst()->deleteLater();
55int LauncherModel::rowCount(const QModelIndex &parent) const
58 return m_list.count();
61QVariant LauncherModel::data(const QModelIndex &index, int role) const
63 LauncherItem *item = m_list.at(index.row());
72 return item->pinned();
76 return item->progress();
78 return item->focused();
84unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
86 if (index < 0 || index >= m_list.count()) {
89 return m_list.at(index);
92void LauncherModel::move(int oldIndex, int newIndex)
94 // Make sure its not moved outside the lists
98 if (newIndex >= m_list.count()) {
99 newIndex = m_list.count()-1;
103 if (oldIndex == newIndex) {
107 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
108 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
109 // adjust the model's index by +1 in case we're moving upwards
110 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
112 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
113 m_list.move(oldIndex, newIndex);
116 if (!m_list.at(newIndex)->pinned()) {
117 pin(m_list.at(newIndex)->appId());
123void LauncherModel::pin(const QString &appId, int index)
125 int currentIndex = findApplication(appId);
127 if (currentIndex >= 0) {
128 if (index == -1 || index == currentIndex) {
129 m_list.at(currentIndex)->setPinned(true);
130 QModelIndex modelIndex = this->index(currentIndex);
131 Q_EMIT dataChanged(modelIndex, modelIndex);
133 move(currentIndex, index);
134 // move() will store the list to the backend itself, so just exit at this point.
139 index = m_list.count();
141 beginInsertRows(QModelIndex(), index, index);
142 LauncherItem *item = new LauncherItem(appId,
143 m_backend->displayName(appId),
144 m_backend->icon(appId));
145 item->setPinned(true);
146 m_list.insert(index, item);
153void LauncherModel::requestRemove(const QString &appId)
155 int index = findApplication(appId);
160 if (m_appManager->findApplication(appId)) {
161 m_list.at(index)->setPinned(false);
165 beginRemoveRows(QModelIndex(), index, index);
166 m_list.takeAt(index)->deleteLater();
172void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
174 int index = findApplication(appId);
179 LauncherItem *item = m_list.at(index);
180 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
182 QString actionId = model->get(actionIndex).actionId();
184 // Check if this is one of the launcher actions we handle ourselves
185 if (actionId == "pin_item") {
186 if (item->pinned()) {
187 requestRemove(appId);
192 // Nope, we don't know this action, let the backend forward it to the application
194 m_backend->triggerQuickListAction(appId, actionId);
199void LauncherModel::setUser(const QString &username)
201 m_backend->setUser(username);
204ApplicationManagerInterface *LauncherModel::applicationManager() const
209void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
211 // Is there already another appmanager set?
213 // Disconnect any signals
214 disconnect(this, SLOT(applicationAdded(QModelIndex,int)));
215 disconnect(this, SLOT(applicationRemoved(QModelIndex,int)));
216 disconnect(this, SLOT(focusedAppIdChanged()));
218 // remove any recent/running apps from the launcher
219 QList<int> recentAppIndices;
220 for (int i = 0; i < m_list.count(); ++i) {
221 if (m_list.at(i)->recent()) {
222 recentAppIndices << i;
226 while (recentAppIndices.count() > 0) {
227 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
228 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
230 recentAppIndices.takeFirst();
235 m_appManager = appManager;
236 connect(m_appManager, SIGNAL(rowsInserted(QModelIndex, int, int)), SLOT(applicationAdded(QModelIndex,int)));
237 connect(m_appManager, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), SLOT(applicationRemoved(QModelIndex,int)));
238 connect(m_appManager, SIGNAL(focusedApplicationIdChanged()), SLOT(focusedAppIdChanged()));
240 Q_EMIT applicationManagerChanged();
242 for (int i = 0; i < appManager->count(); ++i) {
243 applicationAdded(QModelIndex(), i);
248void LauncherModel::storeAppList()
251 Q_FOREACH(LauncherItem *item, m_list) {
252 if (item->pinned()) {
253 appIds << item->appId();
256 m_backend->setStoredApplications(appIds);
259int LauncherModel::findApplication(const QString &appId)
261 for (int i = 0; i < m_list.count(); ++i) {
262 LauncherItem *item = m_list.at(i);
263 if (item->appId() == appId) {
270void LauncherModel::progressChanged(const QString &appId, int progress)
272 int idx = findApplication(appId);
274 LauncherItem *item = m_list.at(idx);
275 item->setProgress(progress);
276 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleProgress);
281void LauncherModel::countChanged(const QString &appId, int count)
283 int idx = findApplication(appId);
285 LauncherItem *item = m_list.at(idx);
286 item->setCount(count);
287 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleCount);
291void LauncherModel::applicationAdded(const QModelIndex &parent, int row)
295 ApplicationInfoInterface *app = m_appManager->get(row);
297 qDebug() << "Huh? Can't get newly added Application from ApplicationManager... Ignoring this app...";
302 Q_FOREACH(LauncherItem *item, m_list) {
303 if (app->appId() == item->appId()) {
309 // Shall we paint some running/recent app highlight? If yes, do it here.
311 LauncherItem *item = new LauncherItem(app->appId(), app->name(), app->icon().toString());
312 item->setRecent(true);
313 item->setFocused(app->focused());
315 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
321void LauncherModel::applicationRemoved(const QModelIndex &parent, int row)
326 for (int i = 0; i < m_list.count(); ++i) {
327 if (m_list.at(i)->appId() == m_appManager->get(row)->appId()) {
333 if (appIndex > -1 && !m_list.at(appIndex)->pinned()) {
334 beginRemoveRows(QModelIndex(), appIndex, appIndex);
335 m_list.takeAt(appIndex)->deleteLater();
340void LauncherModel::focusedAppIdChanged()
342 QString appId = m_appManager->focusedApplicationId();
343 for (int i = 0; i < m_list.count(); ++i) {
344 LauncherItem *item = m_list.at(i);
345 if (!item->focused() && item->appId() == appId) {
346 item->setFocused(true);
347 Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);
348 } else if (item->focused() && item->appId() != appId) {
349 item->setFocused(false);
350 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