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)));
37 connect(m_backend, SIGNAL(appUninstalled(QString)), SLOT(requestRemove(QString)));
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);
196 // Nope, we don't know this action, let the backend forward it to the application
198 m_backend->triggerQuickListAction(appId, actionId);
203void LauncherModel::setUser(const QString &username)
205 m_backend->setUser(username);
208QString LauncherModel::getUrlForAppId(const QString &appId) const
210 // appId is either an appId or a legacy app name. Let's find out which
214 QString df = m_backend->desktopFile(appId + ".desktop");
216 return "application:///" + appId + ".desktop";
218 QStringList parts = appId.split('_');
219 QString package = parts.value(0);
220 QString app = parts.value(1, "first-listed-app");
221 return "appid://" + package + "/" + app + "/current-user-version";
224ApplicationManagerInterface *LauncherModel::applicationManager() const
229void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
231 // Is there already another appmanager set?
233 // Disconnect any signals
234 disconnect(this, SLOT(applicationAdded(QModelIndex,int)));
235 disconnect(this, SLOT(applicationRemoved(QModelIndex,int)));
236 disconnect(this, SLOT(focusedAppIdChanged()));
238 // remove any recent/running apps from the launcher
239 QList<int> recentAppIndices;
240 for (int i = 0; i < m_list.count(); ++i) {
241 if (m_list.at(i)->recent()) {
242 recentAppIndices << i;
246 while (recentAppIndices.count() > 0) {
247 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
248 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
250 recentAppIndices.takeFirst();
255 m_appManager = appManager;
256 connect(m_appManager, SIGNAL(rowsInserted(QModelIndex, int, int)), SLOT(applicationAdded(QModelIndex,int)));
257 connect(m_appManager, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), SLOT(applicationRemoved(QModelIndex,int)));
258 connect(m_appManager, SIGNAL(focusedApplicationIdChanged()), SLOT(focusedAppIdChanged()));
260 Q_EMIT applicationManagerChanged();
262 for (int i = 0; i < appManager->count(); ++i) {
263 applicationAdded(QModelIndex(), i);
268void LauncherModel::storeAppList()
271 Q_FOREACH(LauncherItem *item, m_list) {
272 if (item->pinned()) {
273 appIds << item->appId();
276 m_backend->setStoredApplications(appIds);
279int LauncherModel::findApplication(const QString &appId)
281 for (int i = 0; i < m_list.count(); ++i) {
282 LauncherItem *item = m_list.at(i);
283 if (item->appId() == appId) {
290void LauncherModel::progressChanged(const QString &appId, int progress)
292 int idx = findApplication(appId);
294 LauncherItem *item = m_list.at(idx);
295 item->setProgress(progress);
296 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleProgress);
301void LauncherModel::countChanged(const QString &appId, int count)
303 int idx = findApplication(appId);
305 LauncherItem *item = m_list.at(idx);
306 item->setCount(count);
307 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleCount);
311void LauncherModel::applicationAdded(const QModelIndex &parent, int row)
315 ApplicationInfoInterface *app = m_appManager->get(row);
317 qWarning() << "LauncherModel received an applicationAdded signal, but there's no such application!";
321 if (app->appId() == "unity8-dash") {
322 // Not adding the dash app
327 Q_FOREACH(LauncherItem *item, m_list) {
328 if (app->appId() == item->appId()) {
334 // Shall we paint some running/recent app highlight? If yes, do it here.
336 LauncherItem *item = new LauncherItem(app->appId(), app->name(), app->icon().toString(), this);
337 item->setRecent(true);
338 item->setFocused(app->focused());
340 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
346void LauncherModel::applicationRemoved(const QModelIndex &parent, int row)
351 for (int i = 0; i < m_list.count(); ++i) {
352 if (m_list.at(i)->appId() == m_appManager->get(row)->appId()) {
358 if (appIndex > -1 && !m_list.at(appIndex)->pinned()) {
359 beginRemoveRows(QModelIndex(), appIndex, appIndex);
360 m_list.takeAt(appIndex)->deleteLater();
365void LauncherModel::focusedAppIdChanged()
367 QString appId = m_appManager->focusedApplicationId();
368 for (int i = 0; i < m_list.count(); ++i) {
369 LauncherItem *item = m_list.at(i);
370 if (!item->focused() && item->appId() == appId) {
371 item->setFocused(true);
372 Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);
373 } else if (item->focused() && item->appId() != appId) {
374 item->setFocused(false);
375 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