on de035fa help sign in
unity8/plugins/Unity/Launcher/launchermodel.cpp
393 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);
198QString LauncherModel::getUrlForAppId(const QString &appId) const
200 // appId is either an appId or a legacy app name. Let's find out which
201 if (appId.isEmpty())
202 return QString();
204 QString df = m_backend->desktopFile(appId + ".desktop");
205 if (!df.isEmpty())
206 return "application:///" + appId + ".desktop";
208 QStringList parts = appId.split('_');
209 QString package = parts.value(0);
210 QString app = parts.value(1, "first-listed-app");
211 return "appid://" + package + "/" + app + "/current-user-version";
214void LauncherModel::refreshStoredApplications()
216 // First remove any existing ones
217 QList<int> storedAppIndices;
218 for (int i = 0; i < m_list.count(); ++i) {
219 if (!m_list.at(i)->recent()) {
220 storedAppIndices << i;
221 }
222 }
223 int run = 0;
224 while (storedAppIndices.count() > 0) {
225 beginRemoveRows(QModelIndex(), storedAppIndices.first() - run, storedAppIndices.first() - run);
226 m_list.takeAt(storedAppIndices.first() - run)->deleteLater();
227 endRemoveRows();
228 storedAppIndices.takeFirst();
229 ++run;
230 }
232 // Now insert all stored apps at beginning of list
233 QStringList storedApplications = m_backend->storedApplications();
234 beginInsertRows(QModelIndex(), 0, storedApplications.size() - 1);
235 run = 0;
236 Q_FOREACH (const QString &entry, storedApplications) {
237 LauncherItem *item = new LauncherItem(entry,
238 m_backend->displayName(entry),
239 m_backend->icon(entry),
240 !m_greeterMode,
241 this);
242 item->setPinned(true);
243 m_list.insert(run++, item);
244 }
245 endInsertRows();
248ApplicationManagerInterface *LauncherModel::applicationManager() const
250 return m_appManager;
253void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
255 // Is there already another appmanager set?
256 if (m_appManager) {
257 // Disconnect any signals
258 disconnect(this, SLOT(applicationAdded(QModelIndex,int)));
259 disconnect(this, SLOT(applicationRemoved(QModelIndex,int)));
260 disconnect(this, SLOT(focusedAppIdChanged()));
262 // remove any recent/running apps from the launcher
263 QList<int> recentAppIndices;
264 for (int i = 0; i < m_list.count(); ++i) {
265 if (m_list.at(i)->recent()) {
266 recentAppIndices << i;
267 }
268 }
269 int run = 0;
270 while (recentAppIndices.count() > 0) {
271 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
272 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
273 endRemoveRows();
274 recentAppIndices.takeFirst();
275 ++run;
276 }
277 }
279 m_appManager = appManager;
280 connect(m_appManager, SIGNAL(rowsInserted(QModelIndex, int, int)), SLOT(applicationAdded(QModelIndex,int)));
281 connect(m_appManager, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), SLOT(applicationRemoved(QModelIndex,int)));
282 connect(m_appManager, SIGNAL(focusedApplicationIdChanged()), SLOT(focusedAppIdChanged()));
284 Q_EMIT applicationManagerChanged();
286 for (int i = 0; i < appManager->count(); ++i) {
287 applicationAdded(QModelIndex(), i);
288 }
292void LauncherModel::storeAppList()
294 QStringList appIds;
295 Q_FOREACH(LauncherItem *item, m_list) {
296 if (item->pinned()) {
297 appIds << item->appId();
298 }
299 }
300 m_backend->setStoredApplications(appIds);
303int LauncherModel::findApplication(const QString &appId)
305 for (int i = 0; i < m_list.count(); ++i) {
306 LauncherItem *item = m_list.at(i);
307 if (item->appId() == appId) {
308 return i;
309 }
310 }
311 return -1;
314void LauncherModel::progressChanged(const QString &appId, int progress)
316 int idx = findApplication(appId);
317 if (idx >= 0) {
318 LauncherItem *item = m_list.at(idx);
319 item->setProgress(progress);
320 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleProgress);
321 }
325void LauncherModel::countChanged(const QString &appId, int count)
327 int idx = findApplication(appId);
328 if (idx >= 0) {
329 LauncherItem *item = m_list.at(idx);
330 item->setCount(count);
331 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleCount);
332 }
335void LauncherModel::applicationAdded(const QModelIndex &parent, int row)
337 Q_UNUSED(parent);
339 ApplicationInfoInterface *app = m_appManager->get(row);
341 bool found = false;
342 Q_FOREACH(LauncherItem *item, m_list) {
343 if (app->appId() == item->appId()) {
344 found = true;
345 break;
346 }
347 }
348 if (found) {
349 // Shall we paint some running/recent app highlight? If yes, do it here.
350 } else {
351 LauncherItem *item = new LauncherItem(app->appId(), app->name(), app->icon().toString(), !m_greeterMode);
352 item->setRecent(true);
353 item->setFocused(app->focused());
355 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
356 m_list.append(item);
357 endInsertRows();
358 }
361void LauncherModel::applicationRemoved(const QModelIndex &parent, int row)
363 Q_UNUSED(parent)
365 int appIndex = -1;
366 for (int i = 0; i < m_list.count(); ++i) {
367 if (m_list.at(i)->appId() == m_appManager->get(row)->appId()) {
368 appIndex = i;
369 break;
370 }
371 }
373 if (appIndex > -1 && !m_list.at(appIndex)->pinned()) {
374 beginRemoveRows(QModelIndex(), appIndex, appIndex);
375 m_list.takeAt(appIndex)->deleteLater();
376 endRemoveRows();
377 }
380void LauncherModel::focusedAppIdChanged()
382 QString appId = m_appManager->focusedApplicationId();
383 for (int i = 0; i < m_list.count(); ++i) {
384 LauncherItem *item = m_list.at(i);
385 if (!item->focused() && item->appId() == appId) {
386 item->setFocused(true);
387 Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);
388 } else if (item->focused() && item->appId() != appId) {
389 item->setFocused(false);
390 Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);
391 }
392 }