on 4cb1dd6 help sign in
unity8/plugins/Unity/Launcher/launchermodel.cpp
347 lines 10 kB history raw
1/*
2 * Copyright 2013 Canonical Ltd.
3 *
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.
7 *
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.
12 *
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/>.
15 *
16 * Authors:
17 * Michael Zanetti <michael.zanetti@canonical.com>
18 */
20#include "launchermodel.h"
21#include "launcheritem.h"
22#include "backend/launcherbackend.h"
24#include <unity/shell/application/ApplicationInfoInterface.h>
26#include <QDebug>
28using namespace unity::shell::application;
30LauncherModel::LauncherModel(QObject *parent):
31 LauncherModelInterface(parent),
32 m_backend(new LauncherBackend(true, this)),
33 m_appManager(0)
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),
42 this);
43 if (m_backend->isPinned(entry)) {
44 item->setPinned(true);
45 } else {
46 item->setRecent(true);
47 }
48 m_list.append(item);
49 }
52LauncherModel::~LauncherModel()
54 while (!m_list.empty()) {
55 m_list.takeFirst()->deleteLater();
56 }
59int LauncherModel::rowCount(const QModelIndex &parent) const
61 Q_UNUSED(parent)
62 return m_list.count();
65QVariant LauncherModel::data(const QModelIndex &index, int role) const
67 LauncherItem *item = m_list.at(index.row());
68 switch(role) {
69 case RoleAppId:
70 return item->appId();
71 case RoleName:
72 return item->name();
73 case RoleIcon:
74 return item->icon();
75 case RolePinned:
76 return item->pinned();
77 case RoleCount:
78 return item->count();
79 case RoleProgress:
80 return item->progress();
81 case RoleFocused:
82 return item->focused();
83 }
85 return QVariant();
88unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
90 if (index < 0 || index >= m_list.count()) {
91 return 0;
92 }
93 return m_list.at(index);
96void LauncherModel::move(int oldIndex, int newIndex)
98 // Make sure its not moved outside the lists
99 if (newIndex < 0) {
100 newIndex = 0;
101 }
102 if (newIndex >= m_list.count()) {
103 newIndex = m_list.count()-1;
104 }
106 // Nothing to do?
107 if (oldIndex == newIndex) {
108 return;
109 }
111 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
112 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
113 // adjust the model's index by +1 in case we're moving upwards
114 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
116 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
117 m_list.move(oldIndex, newIndex);
118 endMoveRows();
120 storeAppList();
122 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);
135 } else {
136 move(currentIndex, index);
137 }
138 } else {
139 if (index == -1) {
140 index = m_list.count();
141 }
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);
149 endInsertRows();
150 }
153void LauncherModel::requestRemove(const QString &appId)
155 int index = findApplication(appId);
156 if (index < 0) {
157 return;
158 }
160 beginRemoveRows(QModelIndex(), index, index);
161 m_list.takeAt(index)->deleteLater();
162 endRemoveRows();
164 storeAppList();
167void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
169 int index = findApplication(appId);
170 if (index < 0) {
171 return;
172 }
174 LauncherItem *item = m_list.at(index);
175 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
176 if (model) {
177 QString actionId = model->get(actionIndex).actionId();
179 // Check if this is one of the launcher actions we handle ourselves
180 if (actionId == "pin_item") {
181 if (item->pinned()) {
182 requestRemove(appId);
183 } else {
184 pin(appId);
185 }
187 // Nope, we don't know this action, let the backend forward it to the application
188 } else {
189 m_backend->triggerQuickListAction(appId, actionId);
190 }
191 }
194void LauncherModel::setUser(const QString &username)
196 m_backend->setUser(username);
199ApplicationManagerInterface *LauncherModel::applicationManager() const
201 return m_appManager;
204void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
206 qDebug() << "setting appmanager" << appManager;
208 // Is there already another appmanager set?
209 if (m_appManager) {
210 // Disconnect any signals
211 disconnect(this, SLOT(applicationAdded(QModelIndex,int)));
212 disconnect(this, SLOT(applicationRemoved(QModelIndex,int)));
213 disconnect(this, SLOT(focusedAppIdChanged()));
215 // remove any recent/running apps from the launcher
216 QList<int> recentAppIndices;
217 for (int i = 0; i < m_list.count(); ++i) {
218 if (m_list.at(i)->recent()) {
219 recentAppIndices << i;
220 }
221 }
222 int run = 0;
223 while (recentAppIndices.count() > 0) {
224 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
225 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
226 endRemoveRows();
227 recentAppIndices.takeFirst();
228 ++run;
229 }
230 }
232 m_appManager = appManager;
233 connect(m_appManager, SIGNAL(rowsInserted(QModelIndex, int, int)), SLOT(applicationAdded(QModelIndex,int)));
234 connect(m_appManager, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), SLOT(applicationRemoved(QModelIndex,int)));
235 connect(m_appManager, SIGNAL(focusedApplicationIdChanged()), SLOT(focusedAppIdChanged()));
237 Q_EMIT applicationManagerChanged();
239 for (int i = 0; i < appManager->count(); ++i) {
240 applicationAdded(QModelIndex(), i);
241 }
245void LauncherModel::storeAppList()
247 QStringList appIds;
248 Q_FOREACH(LauncherItem *item, m_list) {
249 if (item->pinned()) {
250 appIds << item->appId();
251 }
252 }
253 m_backend->setStoredApplications(appIds);
256int LauncherModel::findApplication(const QString &appId)
258 for (int i = 0; i < m_list.count(); ++i) {
259 LauncherItem *item = m_list.at(i);
260 if (item->appId() == appId) {
261 return i;
262 }
263 }
264 return -1;
267void LauncherModel::progressChanged(const QString &appId, int progress)
269 int idx = findApplication(appId);
270 if (idx >= 0) {
271 LauncherItem *item = m_list.at(idx);
272 item->setProgress(progress);
273 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleProgress);
274 }
278void LauncherModel::countChanged(const QString &appId, int count)
280 int idx = findApplication(appId);
281 if (idx >= 0) {
282 LauncherItem *item = m_list.at(idx);
283 item->setCount(count);
284 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleCount);
285 }
288void LauncherModel::applicationAdded(const QModelIndex &parent, int row)
290 Q_UNUSED(parent);
292 ApplicationInfoInterface *app = m_appManager->get(row);
294 bool found = false;
295 Q_FOREACH(LauncherItem *item, m_list) {
296 if (app->appId() == item->appId()) {
297 found = true;
298 break;
299 }
300 }
301 if (found) {
302 // Shall we paint some running/recent app highlight? If yes, do it here.
303 } else {
304 qDebug() << "Adding app icon" << app->icon();
305 LauncherItem *item = new LauncherItem(app->appId(), app->name(), app->icon().toString());
306 item->setRecent(true);
307 item->setFocused(app->focused());
309 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
310 m_list.append(item);
311 endInsertRows();
312 }
315void LauncherModel::applicationRemoved(const QModelIndex &parent, int row)
317 Q_UNUSED(parent)
319 int appIndex = -1;
320 for (int i = 0; i < m_list.count(); ++i) {
321 if (m_list.at(i)->appId() == m_appManager->get(row)->appId()) {
322 appIndex = i;
323 break;
324 }
325 }
327 if (appIndex > -1 && !m_list.at(appIndex)->pinned()) {
328 beginRemoveRows(QModelIndex(), appIndex, appIndex);
329 m_list.takeAt(appIndex)->deleteLater();
330 endRemoveRows();
331 }
334void LauncherModel::focusedAppIdChanged()
336 QString appId = m_appManager->focusedApplicationId();
337 for (int i = 0; i < m_list.count(); ++i) {
338 LauncherItem *item = m_list.at(i);
339 if (!item->focused() && item->appId() == appId) {
340 item->setFocused(true);
341 Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);
342 } else if (item->focused() && item->appId() != appId) {
343 item->setFocused(false);
344 Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);
345 }
346 }