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"
24LauncherModel::LauncherModel(QObject *parent):
25 LauncherModelInterface(parent),
26 m_backend(new LauncherBackend(true, this))
28 connect(m_backend, SIGNAL(countChanged(QString,int)), SLOT(countChanged(QString,int)));
29 connect(m_backend, SIGNAL(progressChanged(QString,int)), SLOT(progressChanged(QString,int)));
31 Q_FOREACH (const QString &entry, m_backend->storedApplications()) {
32 LauncherItem *item = new LauncherItem(entry,
33 m_backend->desktopFile(entry),
34 m_backend->displayName(entry),
35 m_backend->icon(entry),
37 if (m_backend->isPinned(entry)) {
38 item->setPinned(true);
40 item->setRecent(true);
46LauncherModel::~LauncherModel()
48 while (!m_list.empty()) {
49 m_list.takeFirst()->deleteLater();
53int LauncherModel::rowCount(const QModelIndex &parent) const
56 return m_list.count();
59QVariant LauncherModel::data(const QModelIndex &index, int role) const
61 LauncherItem *item = m_list.at(index.row());
64 return item->desktopFile();
66 return item->desktopFile();
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);
118 pin(m_list.at(newIndex)->appId());
121void LauncherModel::pin(const QString &appId, int index)
123 int currentIndex = findApplication(appId);
125 if (currentIndex >= 0) {
126 if (index == -1 || index == currentIndex) {
127 m_list.at(currentIndex)->setPinned(true);
128 m_backend->setPinned(appId, true);
129 QModelIndex modelIndex = this->index(currentIndex);
130 Q_EMIT dataChanged(modelIndex, modelIndex);
132 move(currentIndex, index);
136 index = m_list.count();
138 beginInsertRows(QModelIndex(), index, index);
139 LauncherItem *item = new LauncherItem(appId,
140 m_backend->desktopFile(appId),
141 m_backend->displayName(appId),
142 m_backend->icon(appId));
143 item->setPinned(true);
144 m_backend->setPinned(appId, true);
145 m_list.insert(index, item);
150void LauncherModel::requestRemove(const QString &appId)
152 int index = findApplication(appId);
157 beginRemoveRows(QModelIndex(), index, index);
158 m_list.takeAt(index)->deleteLater();
164void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
166 int index = findApplication(appId);
171 LauncherItem *item = m_list.at(index);
172 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
174 QString actionId = model->get(actionIndex).actionId();
176 // Check if this is one of the launcher actions we handle ourselves
177 if (actionId == "pin_item") {
178 if (item->pinned()) {
179 requestRemove(appId);
184 // Nope, we don't know this action, let the backend forward it to the application
186 m_backend->triggerQuickListAction(appId, actionId);
191void LauncherModel::setUser(const QString &username)
193 m_backend->setUser(username);
196void LauncherModel::applicationFocused(const QString &appId)
199 for(int i = 0; i < m_list.count(); ++i) {
200 LauncherItem *item = m_list.at(i);
201 if (item->focused()) {
202 item->setFocused(false);
203 Q_EMIT dataChanged(this->index(i), this->index(i), QVector<int>() << RoleFocused);
207 if (appId.isEmpty()) {
211 // FIXME: drop this once we get real appIds from the AppManager
212 QString helper = appId.split('/').last();
213 if (helper.endsWith(".desktop")) {
217 int index = findApplication(helper);
219 m_list.at(index)->setFocused(true);
220 Q_EMIT dataChanged(this->index(index), this->index(index), QVector<int>() << RoleFocused);
222 // Add app to recent apps
223 QString desktopFile = m_backend->desktopFile(helper);
224 QString appName = m_backend->displayName(helper);
225 QString icon = m_backend->icon(helper);
227 LauncherItem *item = new LauncherItem(helper, desktopFile, appName, icon);
228 item->setRecent(true);
229 item->setFocused(true);
230 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
234 // Clean up old recent apps
235 QList<int> recentAppIndices;
236 for (int i = 0; i < m_list.count(); ++i) {
237 if (m_list.at(i)->recent()) {
238 recentAppIndices << i;
242 while (recentAppIndices.count() > 5) {
243 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
244 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
246 recentAppIndices.takeFirst();
253void LauncherModel::storeAppList()
256 Q_FOREACH(LauncherItem *item, m_list) {
257 if (item->pinned() || item->recent()) {
258 appIds << item->appId();
261 m_backend->setStoredApplications(appIds);
264int LauncherModel::findApplication(const QString &appId)
266 for (int i = 0; i < m_list.count(); ++i) {
267 LauncherItem *item = m_list.at(i);
268 if (item->appId() == appId) {
275void LauncherModel::progressChanged(const QString &appId, int progress)
277 int idx = findApplication(appId);
279 LauncherItem *item = m_list.at(idx);
280 item->setProgress(progress);
281 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleProgress);
286void LauncherModel::countChanged(const QString &appId, int count)
288 int idx = findApplication(appId);
290 LauncherItem *item = m_list.at(idx);
291 item->setCount(count);
292 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleCount);
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