on 0df89fa help sign in
unity8/plugins/Unity/Launcher/launchermodel.cpp
399 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>
26#include <QDebug>
28using namespace unity::shell::application;
30LauncherModel::LauncherModel(QObject *parent):
31 LauncherModelInterface(parent),
32 m_greeterMode(qgetenv("XDG_SESSION_CLASS") == "greeter"),
33 m_backend(new LauncherBackend(m_greeterMode, this)),
34 m_appManager(0)
36 connect(m_backend, SIGNAL(countChanged(QString,int)), SLOT(countChanged(QString,int)));
37 connect(m_backend, SIGNAL(progressChanged(QString,int)), SLOT(progressChanged(QString,int)));
38 connect(m_backend, SIGNAL(refreshApplications()), SLOT(refreshStoredApplications()));
40 refreshStoredApplications();
43LauncherModel::~LauncherModel()
45 while (!m_list.empty()) {
46 m_list.takeFirst()->deleteLater();
47 }
50int LauncherModel::rowCount(const QModelIndex &parent) const
52 Q_UNUSED(parent)
53 return m_list.count();
56QVariant LauncherModel::data(const QModelIndex &index, int role) const
58 LauncherItem *item = m_list.at(index.row());
59 switch(role) {
60 case RoleAppId:
61 return item->appId();
62 case RoleName:
63 return item->name();
64 case RoleIcon:
65 return item->icon();
66 case RolePinned:
67 return item->pinned();
68 case RoleCount:
69 return item->count();
70 case RoleProgress:
71 return item->progress();
72 case RoleFocused:
73 return item->focused();
74 }
76 return QVariant();
79unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
81 if (index < 0 || index >= m_list.count()) {
82 return 0;
83 }
84 return m_list.at(index);
87void LauncherModel::move(int oldIndex, int newIndex)
89 // Make sure its not moved outside the lists
90 if (newIndex < 0) {
91 newIndex = 0;
92 }
93 if (newIndex >= m_list.count()) {
94 newIndex = m_list.count()-1;
95 }
97 // Nothing to do?
98 if (oldIndex == newIndex) {
99 return;
100 }
102 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
103 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
104 // adjust the model's index by +1 in case we're moving upwards
105 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
107 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
108 m_list.move(oldIndex, newIndex);
109 endMoveRows();
111 if (!m_list.at(newIndex)->pinned()) {
112 pin(m_list.at(newIndex)->appId());
113 } else {
114 storeAppList();
115 }
118void LauncherModel::pin(const QString &appId, int index)
120 int currentIndex = findApplication(appId);
122 if (currentIndex >= 0) {
123 if (index == -1 || index == currentIndex) {
124 m_list.at(currentIndex)->setPinned(true);
125 QModelIndex modelIndex = this->index(currentIndex);
126 Q_EMIT dataChanged(modelIndex, modelIndex);
127 } else {
128 move(currentIndex, index);
129 // move() will store the list to the backend itself, so just exit at this point.
130 return;
131 }
132 } else {
133 if (index == -1) {
134 index = m_list.count();
135 }
136 beginInsertRows(QModelIndex(), index, index);
137 LauncherItem *item = new LauncherItem(appId,
138 m_backend->displayName(appId),
139 m_backend->icon(appId),
140 !m_greeterMode);
141 item->setPinned(true);
142 m_list.insert(index, item);
143 endInsertRows();
144 }
146 storeAppList();
149void LauncherModel::requestRemove(const QString &appId)
151 int index = findApplication(appId);
152 if (index < 0) {
153 return;
154 }
156 if (m_appManager->findApplication(appId)) {
157 m_list.at(index)->setPinned(false);
158 return;
159 }
161 beginRemoveRows(QModelIndex(), index, index);
162 m_list.takeAt(index)->deleteLater();
163 endRemoveRows();
165 storeAppList();
168void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
170 int index = findApplication(appId);
171 if (index < 0) {
172 return;
173 }
175 LauncherItem *item = m_list.at(index);
176 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
177 if (model) {
178 QString actionId = model->get(actionIndex).actionId();
180 // Check if this is one of the launcher actions we handle ourselves
181 if (actionId == "pin_item") {
182 if (item->pinned()) {
183 requestRemove(appId);
184 } else {
185 pin(appId);
186 }
188 // Nope, we don't know this action, let the backend forward it to the application
189 } else {
190 m_backend->triggerQuickListAction(appId, actionId);
191 }
192 }
195void LauncherModel::setUser(const QString &username)
197 m_backend->setUser(username);
200QString LauncherModel::getUrlForAppId(const QString &appId) const
202 // appId is either an appId or a legacy app name. Let's find out which
203 if (appId.isEmpty())
204 return QString();
206 QString df = m_backend->desktopFile(appId + ".desktop");
207 if (!df.isEmpty())
208 return "application:///" + appId + ".desktop";
210 QStringList parts = appId.split('_');
211 QString package = parts.value(0);
212 QString app = parts.value(1, "first-listed-app");
213 return "appid://" + package + "/" + app + "/current-user-version";
216void LauncherModel::refreshStoredApplications()
218 // First remove any existing ones
219 QList<int> storedAppIndices;
220 for (int i = 0; i < m_list.count(); ++i) {
221 if (!m_list.at(i)->recent()) {
222 storedAppIndices << i;
223 }
224 }
225 int run = 0;
226 while (storedAppIndices.count() > 0) {
227 beginRemoveRows(QModelIndex(), storedAppIndices.first() - run, storedAppIndices.first() - run);
228 m_list.takeAt(storedAppIndices.first() - run)->deleteLater();
229 endRemoveRows();
230 storedAppIndices.takeFirst();
231 ++run;
232 }
234 // Now insert all stored apps at beginning of list
235 QStringList storedApplications = m_backend->storedApplications();
236 beginInsertRows(QModelIndex(), 0, storedApplications.size() - 1);
237 run = 0;
238 Q_FOREACH (const QString &entry, storedApplications) {
239 LauncherItem *item = new LauncherItem(entry,
240 m_backend->displayName(entry),
241 m_backend->icon(entry),
242 !m_greeterMode,
243 this);
244 item->setPinned(true);
245 m_list.insert(run++, item);
246 }
247 endInsertRows();
250ApplicationManagerInterface *LauncherModel::applicationManager() const
252 return m_appManager;
255void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
257 // Is there already another appmanager set?
258 if (m_appManager) {
259 // Disconnect any signals
260 disconnect(this, SLOT(applicationAdded(QModelIndex,int)));
261 disconnect(this, SLOT(applicationRemoved(QModelIndex,int)));
262 disconnect(this, SLOT(focusedAppIdChanged()));
264 // remove any recent/running apps from the launcher
265 QList<int> recentAppIndices;
266 for (int i = 0; i < m_list.count(); ++i) {
267 if (m_list.at(i)->recent()) {
268 recentAppIndices << i;
269 }
270 }
271 int run = 0;
272 while (recentAppIndices.count() > 0) {
273 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
274 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
275 endRemoveRows();
276 recentAppIndices.takeFirst();
277 ++run;
278 }
279 }
281 m_appManager = appManager;
282 connect(m_appManager, SIGNAL(rowsInserted(QModelIndex, int, int)), SLOT(applicationAdded(QModelIndex,int)));
283 connect(m_appManager, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), SLOT(applicationRemoved(QModelIndex,int)));
284 connect(m_appManager, SIGNAL(focusedApplicationIdChanged()), SLOT(focusedAppIdChanged()));
286 Q_EMIT applicationManagerChanged();
288 for (int i = 0; i < appManager->count(); ++i) {
289 applicationAdded(QModelIndex(), i);
290 }
294void LauncherModel::storeAppList()
296 QStringList appIds;
297 Q_FOREACH(LauncherItem *item, m_list) {
298 if (item->pinned()) {
299 appIds << item->appId();
300 }
301 }
302 m_backend->setStoredApplications(appIds);
305int LauncherModel::findApplication(const QString &appId)
307 for (int i = 0; i < m_list.count(); ++i) {
308 LauncherItem *item = m_list.at(i);
309 if (item->appId() == appId) {
310 return i;
311 }
312 }
313 return -1;
316void LauncherModel::progressChanged(const QString &appId, int progress)
318 int idx = findApplication(appId);
319 if (idx >= 0) {
320 LauncherItem *item = m_list.at(idx);
321 item->setProgress(progress);
322 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleProgress);
323 }
327void LauncherModel::countChanged(const QString &appId, int count)
329 int idx = findApplication(appId);
330 if (idx >= 0) {
331 LauncherItem *item = m_list.at(idx);
332 item->setCount(count);
333 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleCount);
334 }
337void LauncherModel::applicationAdded(const QModelIndex &parent, int row)
339 Q_UNUSED(parent);
341 ApplicationInfoInterface *app = m_appManager->get(row);
342 if (!app) {
343 qWarning() << "LauncherModel received an applicationAdded signal, but there's no such application!";
344 return;
345 }
347 bool found = false;
348 Q_FOREACH(LauncherItem *item, m_list) {
349 if (app->appId() == item->appId()) {
350 found = true;
351 break;
352 }
353 }
354 if (found) {
355 // Shall we paint some running/recent app highlight? If yes, do it here.
356 } else {
357 LauncherItem *item = new LauncherItem(app->appId(), app->name(), app->icon().toString(), !m_greeterMode);
358 item->setRecent(true);
359 item->setFocused(app->focused());
361 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
362 m_list.append(item);
363 endInsertRows();
364 }
367void LauncherModel::applicationRemoved(const QModelIndex &parent, int row)
369 Q_UNUSED(parent)
371 int appIndex = -1;
372 for (int i = 0; i < m_list.count(); ++i) {
373 if (m_list.at(i)->appId() == m_appManager->get(row)->appId()) {
374 appIndex = i;
375 break;
376 }
377 }
379 if (appIndex > -1 && !m_list.at(appIndex)->pinned()) {
380 beginRemoveRows(QModelIndex(), appIndex, appIndex);
381 m_list.takeAt(appIndex)->deleteLater();
382 endRemoveRows();
383 }
386void LauncherModel::focusedAppIdChanged()
388 QString appId = m_appManager->focusedApplicationId();
389 for (int i = 0; i < m_list.count(); ++i) {
390 LauncherItem *item = m_list.at(i);
391 if (!item->focused() && item->appId() == appId) {
392 item->setFocused(true);
393 Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);
394 } else if (item->focused() && item->appId() != appId) {
395 item->setFocused(false);
396 Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);
397 }
398 }