on 95db08b help sign in
unity8/plugins/Unity/Launcher/launchermodel.cpp
351 lines 11 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(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 if (!m_list.at(newIndex)->pinned()) {
119 pin(m_list.at(newIndex)->appId());
120 } else {
121 storeAppList();
122 }
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 }
152 storeAppList();
155void LauncherModel::requestRemove(const QString &appId)
157 int index = findApplication(appId);
158 if (index < 0) {
159 return;
160 }
162 if (m_appManager->findApplication(appId)) {
163 m_list.at(index)->setPinned(false);
164 return;
165 }
167 beginRemoveRows(QModelIndex(), index, index);
168 m_list.takeAt(index)->deleteLater();
169 endRemoveRows();
171 storeAppList();
174void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
176 int index = findApplication(appId);
177 if (index < 0) {
178 return;
179 }
181 LauncherItem *item = m_list.at(index);
182 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
183 if (model) {
184 QString actionId = model->get(actionIndex).actionId();
186 // Check if this is one of the launcher actions we handle ourselves
187 if (actionId == "pin_item") {
188 if (item->pinned()) {
189 requestRemove(appId);
190 } else {
191 pin(appId);
192 }
194 // Nope, we don't know this action, let the backend forward it to the application
195 } else {
196 m_backend->triggerQuickListAction(appId, actionId);
197 }
198 }
201void LauncherModel::setUser(const QString &username)
203 m_backend->setUser(username);
206ApplicationManagerInterface *LauncherModel::applicationManager() const
208 return m_appManager;
211void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
213 // Is there already another appmanager set?
214 if (m_appManager) {
215 // Disconnect any signals
216 disconnect(this, SLOT(applicationAdded(QModelIndex,int)));
217 disconnect(this, SLOT(applicationRemoved(QModelIndex,int)));
218 disconnect(this, SLOT(focusedAppIdChanged()));
220 // remove any recent/running apps from the launcher
221 QList<int> recentAppIndices;
222 for (int i = 0; i < m_list.count(); ++i) {
223 if (m_list.at(i)->recent()) {
224 recentAppIndices << i;
225 }
226 }
227 int run = 0;
228 while (recentAppIndices.count() > 0) {
229 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
230 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
231 endRemoveRows();
232 recentAppIndices.takeFirst();
233 ++run;
234 }
235 }
237 m_appManager = appManager;
238 connect(m_appManager, SIGNAL(rowsInserted(QModelIndex, int, int)), SLOT(applicationAdded(QModelIndex,int)));
239 connect(m_appManager, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), SLOT(applicationRemoved(QModelIndex,int)));
240 connect(m_appManager, SIGNAL(focusedApplicationIdChanged()), SLOT(focusedAppIdChanged()));
242 Q_EMIT applicationManagerChanged();
244 for (int i = 0; i < appManager->count(); ++i) {
245 applicationAdded(QModelIndex(), i);
246 }
250void LauncherModel::storeAppList()
252 QStringList appIds;
253 Q_FOREACH(LauncherItem *item, m_list) {
254 if (item->pinned()) {
255 appIds << item->appId();
256 }
257 }
258 m_backend->setStoredApplications(appIds);
261int LauncherModel::findApplication(const QString &appId)
263 for (int i = 0; i < m_list.count(); ++i) {
264 LauncherItem *item = m_list.at(i);
265 if (item->appId() == appId) {
266 return i;
267 }
268 }
269 return -1;
272void LauncherModel::progressChanged(const QString &appId, int progress)
274 int idx = findApplication(appId);
275 if (idx >= 0) {
276 LauncherItem *item = m_list.at(idx);
277 item->setProgress(progress);
278 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleProgress);
279 }
283void LauncherModel::countChanged(const QString &appId, int count)
285 int idx = findApplication(appId);
286 if (idx >= 0) {
287 LauncherItem *item = m_list.at(idx);
288 item->setCount(count);
289 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleCount);
290 }
293void LauncherModel::applicationAdded(const QModelIndex &parent, int row)
295 Q_UNUSED(parent);
297 ApplicationInfoInterface *app = m_appManager->get(row);
299 bool found = false;
300 Q_FOREACH(LauncherItem *item, m_list) {
301 if (app->appId() == item->appId()) {
302 found = true;
303 break;
304 }
305 }
306 if (found) {
307 // Shall we paint some running/recent app highlight? If yes, do it here.
308 } else {
309 LauncherItem *item = new LauncherItem(app->appId(), app->name(), app->icon().toString());
310 item->setRecent(true);
311 item->setFocused(app->focused());
313 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
314 m_list.append(item);
315 endInsertRows();
316 }
319void LauncherModel::applicationRemoved(const QModelIndex &parent, int row)
321 Q_UNUSED(parent)
323 int appIndex = -1;
324 for (int i = 0; i < m_list.count(); ++i) {
325 if (m_list.at(i)->appId() == m_appManager->get(row)->appId()) {
326 appIndex = i;
327 break;
328 }
329 }
331 if (appIndex > -1 && !m_list.at(appIndex)->pinned()) {
332 beginRemoveRows(QModelIndex(), appIndex, appIndex);
333 m_list.takeAt(appIndex)->deleteLater();
334 endRemoveRows();
335 }
338void LauncherModel::focusedAppIdChanged()
340 QString appId = m_appManager->focusedApplicationId();
341 for (int i = 0; i < m_list.count(); ++i) {
342 LauncherItem *item = m_list.at(i);
343 if (!item->focused() && item->appId() == appId) {
344 item->setFocused(true);
345 Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);
346 } else if (item->focused() && item->appId() != appId) {
347 item->setFocused(false);
348 Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);
349 }
350 }