on 0fd30cf help sign in
unity8/plugins/Unity/Launcher/launchermodel.cpp
377 lines 12 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_greeterMode(qgetenv("XDG_SESSION_CLASS") == "greeter"),
31 m_backend(new LauncherBackend(m_greeterMode, this)),
32 m_appManager(0)
34 connect(m_backend, SIGNAL(countChanged(QString,int)), SLOT(countChanged(QString,int)));
35 connect(m_backend, SIGNAL(progressChanged(QString,int)), SLOT(progressChanged(QString,int)));
36 connect(m_backend, SIGNAL(refreshApplications()), SLOT(refreshStoredApplications()));
38 refreshStoredApplications();
41LauncherModel::~LauncherModel()
43 while (!m_list.empty()) {
44 m_list.takeFirst()->deleteLater();
45 }
48int LauncherModel::rowCount(const QModelIndex &parent) const
50 Q_UNUSED(parent)
51 return m_list.count();
54QVariant LauncherModel::data(const QModelIndex &index, int role) const
56 LauncherItem *item = m_list.at(index.row());
57 switch(role) {
58 case RoleAppId:
59 return item->appId();
60 case RoleName:
61 return item->name();
62 case RoleIcon:
63 return item->icon();
64 case RolePinned:
65 return item->pinned();
66 case RoleCount:
67 return item->count();
68 case RoleProgress:
69 return item->progress();
70 case RoleFocused:
71 return item->focused();
72 }
74 return QVariant();
77unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
79 if (index < 0 || index >= m_list.count()) {
80 return 0;
81 }
82 return m_list.at(index);
85void LauncherModel::move(int oldIndex, int newIndex)
87 // Make sure its not moved outside the lists
88 if (newIndex < 0) {
89 newIndex = 0;
90 }
91 if (newIndex >= m_list.count()) {
92 newIndex = m_list.count()-1;
93 }
95 // Nothing to do?
96 if (oldIndex == newIndex) {
97 return;
98 }
100 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
101 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
102 // adjust the model's index by +1 in case we're moving upwards
103 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
105 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
106 m_list.move(oldIndex, newIndex);
107 endMoveRows();
109 if (!m_list.at(newIndex)->pinned()) {
110 pin(m_list.at(newIndex)->appId());
111 } else {
112 storeAppList();
113 }
116void LauncherModel::pin(const QString &appId, int index)
118 int currentIndex = findApplication(appId);
120 if (currentIndex >= 0) {
121 if (index == -1 || index == currentIndex) {
122 m_list.at(currentIndex)->setPinned(true);
123 QModelIndex modelIndex = this->index(currentIndex);
124 Q_EMIT dataChanged(modelIndex, modelIndex);
125 } else {
126 move(currentIndex, index);
127 // move() will store the list to the backend itself, so just exit at this point.
128 return;
129 }
130 } else {
131 if (index == -1) {
132 index = m_list.count();
133 }
134 beginInsertRows(QModelIndex(), index, index);
135 LauncherItem *item = new LauncherItem(appId,
136 m_backend->displayName(appId),
137 m_backend->icon(appId),
138 !m_greeterMode);
139 item->setPinned(true);
140 m_list.insert(index, item);
141 endInsertRows();
142 }
144 storeAppList();
147void LauncherModel::requestRemove(const QString &appId)
149 int index = findApplication(appId);
150 if (index < 0) {
151 return;
152 }
154 if (m_appManager->findApplication(appId)) {
155 m_list.at(index)->setPinned(false);
156 return;
157 }
159 beginRemoveRows(QModelIndex(), index, index);
160 m_list.takeAt(index)->deleteLater();
161 endRemoveRows();
163 storeAppList();
166void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
168 int index = findApplication(appId);
169 if (index < 0) {
170 return;
171 }
173 LauncherItem *item = m_list.at(index);
174 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
175 if (model) {
176 QString actionId = model->get(actionIndex).actionId();
178 // Check if this is one of the launcher actions we handle ourselves
179 if (actionId == "pin_item") {
180 if (item->pinned()) {
181 requestRemove(appId);
182 } else {
183 pin(appId);
184 }
186 // Nope, we don't know this action, let the backend forward it to the application
187 } else {
188 m_backend->triggerQuickListAction(appId, actionId);
189 }
190 }
193void LauncherModel::setUser(const QString &username)
195 m_backend->setUser(username);
198void LauncherModel::refreshStoredApplications()
200 // First remove any existing ones
201 QList<int> storedAppIndices;
202 for (int i = 0; i < m_list.count(); ++i) {
203 if (!m_list.at(i)->recent()) {
204 storedAppIndices << i;
205 }
206 }
207 int run = 0;
208 while (storedAppIndices.count() > 0) {
209 beginRemoveRows(QModelIndex(), storedAppIndices.first() - run, storedAppIndices.first() - run);
210 m_list.takeAt(storedAppIndices.first() - run)->deleteLater();
211 endRemoveRows();
212 storedAppIndices.takeFirst();
213 ++run;
214 }
216 // Now insert all stored apps at beginning of list
217 QStringList storedApplications = m_backend->storedApplications();
218 beginInsertRows(QModelIndex(), 0, storedApplications.size() - 1);
219 run = 0;
220 Q_FOREACH (const QString &entry, storedApplications) {
221 LauncherItem *item = new LauncherItem(entry,
222 m_backend->displayName(entry),
223 m_backend->icon(entry),
224 !m_greeterMode,
225 this);
226 item->setPinned(true);
227 m_list.insert(run++, item);
228 }
229 endInsertRows();
232ApplicationManagerInterface *LauncherModel::applicationManager() const
234 return m_appManager;
237void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
239 // Is there already another appmanager set?
240 if (m_appManager) {
241 // Disconnect any signals
242 disconnect(this, SLOT(applicationAdded(QModelIndex,int)));
243 disconnect(this, SLOT(applicationRemoved(QModelIndex,int)));
244 disconnect(this, SLOT(focusedAppIdChanged()));
246 // remove any recent/running apps from the launcher
247 QList<int> recentAppIndices;
248 for (int i = 0; i < m_list.count(); ++i) {
249 if (m_list.at(i)->recent()) {
250 recentAppIndices << i;
251 }
252 }
253 int run = 0;
254 while (recentAppIndices.count() > 0) {
255 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
256 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
257 endRemoveRows();
258 recentAppIndices.takeFirst();
259 ++run;
260 }
261 }
263 m_appManager = appManager;
264 connect(m_appManager, SIGNAL(rowsInserted(QModelIndex, int, int)), SLOT(applicationAdded(QModelIndex,int)));
265 connect(m_appManager, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), SLOT(applicationRemoved(QModelIndex,int)));
266 connect(m_appManager, SIGNAL(focusedApplicationIdChanged()), SLOT(focusedAppIdChanged()));
268 Q_EMIT applicationManagerChanged();
270 for (int i = 0; i < appManager->count(); ++i) {
271 applicationAdded(QModelIndex(), i);
272 }
276void LauncherModel::storeAppList()
278 QStringList appIds;
279 Q_FOREACH(LauncherItem *item, m_list) {
280 if (item->pinned()) {
281 appIds << item->appId();
282 }
283 }
284 m_backend->setStoredApplications(appIds);
287int LauncherModel::findApplication(const QString &appId)
289 for (int i = 0; i < m_list.count(); ++i) {
290 LauncherItem *item = m_list.at(i);
291 if (item->appId() == appId) {
292 return i;
293 }
294 }
295 return -1;
298void LauncherModel::progressChanged(const QString &appId, int progress)
300 int idx = findApplication(appId);
301 if (idx >= 0) {
302 LauncherItem *item = m_list.at(idx);
303 item->setProgress(progress);
304 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleProgress);
305 }
309void LauncherModel::countChanged(const QString &appId, int count)
311 int idx = findApplication(appId);
312 if (idx >= 0) {
313 LauncherItem *item = m_list.at(idx);
314 item->setCount(count);
315 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleCount);
316 }
319void LauncherModel::applicationAdded(const QModelIndex &parent, int row)
321 Q_UNUSED(parent);
323 ApplicationInfoInterface *app = m_appManager->get(row);
325 bool found = false;
326 Q_FOREACH(LauncherItem *item, m_list) {
327 if (app->appId() == item->appId()) {
328 found = true;
329 break;
330 }
331 }
332 if (found) {
333 // Shall we paint some running/recent app highlight? If yes, do it here.
334 } else {
335 LauncherItem *item = new LauncherItem(app->appId(), app->name(), app->icon().toString(), !m_greeterMode);
336 item->setRecent(true);
337 item->setFocused(app->focused());
339 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
340 m_list.append(item);
341 endInsertRows();
342 }
345void LauncherModel::applicationRemoved(const QModelIndex &parent, int row)
347 Q_UNUSED(parent)
349 int appIndex = -1;
350 for (int i = 0; i < m_list.count(); ++i) {
351 if (m_list.at(i)->appId() == m_appManager->get(row)->appId()) {
352 appIndex = i;
353 break;
354 }
355 }
357 if (appIndex > -1 && !m_list.at(appIndex)->pinned()) {
358 beginRemoveRows(QModelIndex(), appIndex, appIndex);
359 m_list.takeAt(appIndex)->deleteLater();
360 endRemoveRows();
361 }
364void LauncherModel::focusedAppIdChanged()
366 QString appId = m_appManager->focusedApplicationId();
367 for (int i = 0; i < m_list.count(); ++i) {
368 LauncherItem *item = m_list.at(i);
369 if (!item->focused() && item->appId() == appId) {
370 item->setFocused(true);
371 Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);
372 } else if (item->focused() && item->appId() != appId) {
373 item->setFocused(false);
374 Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);
375 }
376 }