on 428ff55 help sign in
unity8/plugins/Unity/Launcher/launchermodel.cpp
342 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>
26using namespace unity::shell::application;
28LauncherModel::LauncherModel(QObject *parent):
29 LauncherModelInterface(parent),
30 m_backend(new LauncherBackend(true, this)),
31 m_appManager(0)
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),
40 this);
41 if (m_backend->isPinned(entry)) {
42 item->setPinned(true);
43 } else {
44 item->setRecent(true);
45 }
46 m_list.append(item);
47 }
50LauncherModel::~LauncherModel()
52 while (!m_list.empty()) {
53 m_list.takeFirst()->deleteLater();
54 }
57int LauncherModel::rowCount(const QModelIndex &parent) const
59 Q_UNUSED(parent)
60 return m_list.count();
63QVariant LauncherModel::data(const QModelIndex &index, int role) const
65 LauncherItem *item = m_list.at(index.row());
66 switch(role) {
67 case RoleAppId:
68 return item->appId();
69 case RoleName:
70 return item->name();
71 case RoleIcon:
72 return item->icon();
73 case RolePinned:
74 return item->pinned();
75 case RoleCount:
76 return item->count();
77 case RoleProgress:
78 return item->progress();
79 case RoleFocused:
80 return item->focused();
81 }
83 return QVariant();
86unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
88 if (index < 0 || index >= m_list.count()) {
89 return 0;
90 }
91 return m_list.at(index);
94void LauncherModel::move(int oldIndex, int newIndex)
96 // Make sure its not moved outside the lists
97 if (newIndex < 0) {
98 newIndex = 0;
99 }
100 if (newIndex >= m_list.count()) {
101 newIndex = m_list.count()-1;
102 }
104 // Nothing to do?
105 if (oldIndex == newIndex) {
106 return;
107 }
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);
116 endMoveRows();
118 storeAppList();
120 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 m_backend->setPinned(appId, true);
131 QModelIndex modelIndex = this->index(currentIndex);
132 Q_EMIT dataChanged(modelIndex, modelIndex);
133 } else {
134 move(currentIndex, index);
135 }
136 } else {
137 if (index == -1) {
138 index = m_list.count();
139 }
140 beginInsertRows(QModelIndex(), index, index);
141 LauncherItem *item = new LauncherItem(appId,
142 m_backend->displayName(appId),
143 m_backend->icon(appId));
144 item->setPinned(true);
145 m_backend->setPinned(appId, true);
146 m_list.insert(index, item);
147 endInsertRows();
148 }
151void LauncherModel::requestRemove(const QString &appId)
153 int index = findApplication(appId);
154 if (index < 0) {
155 return;
156 }
158 beginRemoveRows(QModelIndex(), index, index);
159 m_list.takeAt(index)->deleteLater();
160 endRemoveRows();
162 storeAppList();
165void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
167 int index = findApplication(appId);
168 if (index < 0) {
169 return;
170 }
172 LauncherItem *item = m_list.at(index);
173 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
174 if (model) {
175 QString actionId = model->get(actionIndex).actionId();
177 // Check if this is one of the launcher actions we handle ourselves
178 if (actionId == "pin_item") {
179 if (item->pinned()) {
180 requestRemove(appId);
181 } else {
182 pin(appId);
183 }
185 // Nope, we don't know this action, let the backend forward it to the application
186 } else {
187 m_backend->triggerQuickListAction(appId, actionId);
188 }
189 }
192void LauncherModel::setUser(const QString &username)
194 m_backend->setUser(username);
197ApplicationManagerInterface *LauncherModel::applicationManager() const
199 return m_appManager;
202void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
204 // Is there already another appmanager set?
205 if (m_appManager) {
206 // Disconnect any signals
207 disconnect(this, SLOT(applicationAdded(QModelIndex,int)));
208 disconnect(this, SLOT(applicationRemoved(QModelIndex,int)));
209 disconnect(this, SLOT(focusedAppIdChanged()));
211 // remove any recent/running apps from the launcher
212 QList<int> recentAppIndices;
213 for (int i = 0; i < m_list.count(); ++i) {
214 if (m_list.at(i)->recent()) {
215 recentAppIndices << i;
216 }
217 }
218 int run = 0;
219 while (recentAppIndices.count() > 0) {
220 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
221 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
222 endRemoveRows();
223 recentAppIndices.takeFirst();
224 ++run;
225 }
226 }
228 m_appManager = appManager;
229 connect(m_appManager, SIGNAL(rowsInserted(QModelIndex, int, int)), SLOT(applicationAdded(QModelIndex,int)));
230 connect(m_appManager, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), SLOT(applicationRemoved(QModelIndex,int)));
231 connect(m_appManager, SIGNAL(focusedApplicationIdChanged()), SLOT(focusedAppIdChanged()));
233 Q_EMIT applicationManagerChanged();
235 for (int i = 0; i < appManager->count(); ++i) {
236 applicationAdded(QModelIndex(), i);
237 }
241void LauncherModel::storeAppList()
243 QStringList appIds;
244 Q_FOREACH(LauncherItem *item, m_list) {
245 if (item->pinned()) {
246 appIds << item->appId();
247 }
248 }
249 m_backend->setStoredApplications(appIds);
252int LauncherModel::findApplication(const QString &appId)
254 for (int i = 0; i < m_list.count(); ++i) {
255 LauncherItem *item = m_list.at(i);
256 if (item->appId() == appId) {
257 return i;
258 }
259 }
260 return -1;
263void LauncherModel::progressChanged(const QString &appId, int progress)
265 int idx = findApplication(appId);
266 if (idx >= 0) {
267 LauncherItem *item = m_list.at(idx);
268 item->setProgress(progress);
269 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleProgress);
270 }
274void LauncherModel::countChanged(const QString &appId, int count)
276 int idx = findApplication(appId);
277 if (idx >= 0) {
278 LauncherItem *item = m_list.at(idx);
279 item->setCount(count);
280 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleCount);
281 }
284void LauncherModel::applicationAdded(const QModelIndex &parent, int row)
286 Q_UNUSED(parent);
288 ApplicationInfoInterface *app = m_appManager->get(row);
290 bool found = false;
291 Q_FOREACH(LauncherItem *item, m_list) {
292 if (app->appId() == item->appId()) {
293 found = true;
294 break;
295 }
296 }
297 if (found) {
298 // Shall we paint some running/recent app highlight? If yes, do it here.
299 } else {
300 LauncherItem *item = new LauncherItem(app->appId(), app->name(), app->icon().toString());
301 item->setRecent(true);
302 item->setFocused(app->focused());
304 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
305 m_list.append(item);
306 endInsertRows();
307 }
310void LauncherModel::applicationRemoved(const QModelIndex &parent, int row)
312 Q_UNUSED(parent)
314 int appIndex = -1;
315 for (int i = 0; i < m_list.count(); ++i) {
316 if (m_list.at(i)->appId() == m_appManager->get(row)->appId()) {
317 appIndex = i;
318 break;
319 }
320 }
322 if (appIndex > -1 && !m_list.at(appIndex)->pinned()) {
323 beginRemoveRows(QModelIndex(), appIndex, appIndex);
324 m_list.takeAt(appIndex)->deleteLater();
325 endRemoveRows();
326 }
329void LauncherModel::focusedAppIdChanged()
331 QString appId = m_appManager->focusedApplicationId();
332 for (int i = 0; i < m_list.count(); ++i) {
333 LauncherItem *item = m_list.at(i);
334 if (!item->focused() && item->appId() == appId) {
335 item->setFocused(true);
336 Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);
337 } else if (item->focused() && item->appId() != appId) {
338 item->setFocused(false);
339 Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);
340 }
341 }