on de9fe6d help sign in
unity8/plugins/Unity/Launcher/launchermodel.cpp
403 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>
25#include <upstart-app-launch.h>
27using namespace unity::shell::application;
29LauncherModel::LauncherModel(QObject *parent):
30 LauncherModelInterface(parent),
31 m_greeterMode(qgetenv("XDG_SESSION_CLASS") == "greeter"),
32 m_backend(new LauncherBackend(m_greeterMode, this)),
33 m_appManager(0)
35 connect(m_backend, SIGNAL(countChanged(QString,int)), SLOT(countChanged(QString,int)));
36 connect(m_backend, SIGNAL(progressChanged(QString,int)), SLOT(progressChanged(QString,int)));
37 connect(m_backend, SIGNAL(refreshApplications()), SLOT(refreshStoredApplications()));
39 refreshStoredApplications();
42LauncherModel::~LauncherModel()
44 while (!m_list.empty()) {
45 m_list.takeFirst()->deleteLater();
46 }
49int LauncherModel::rowCount(const QModelIndex &parent) const
51 Q_UNUSED(parent)
52 return m_list.count();
55QVariant LauncherModel::data(const QModelIndex &index, int role) const
57 LauncherItem *item = m_list.at(index.row());
58 switch(role) {
59 case RoleAppId:
60 return item->appId();
61 case RoleName:
62 return item->name();
63 case RoleIcon:
64 return item->icon();
65 case RolePinned:
66 return item->pinned();
67 case RoleCount:
68 return item->count();
69 case RoleProgress:
70 return item->progress();
71 case RoleFocused:
72 return item->focused();
73 }
75 return QVariant();
78unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
80 if (index < 0 || index >= m_list.count()) {
81 return 0;
82 }
83 return m_list.at(index);
86void LauncherModel::move(int oldIndex, int newIndex)
88 // Make sure its not moved outside the lists
89 if (newIndex < 0) {
90 newIndex = 0;
91 }
92 if (newIndex >= m_list.count()) {
93 newIndex = m_list.count()-1;
94 }
96 // Nothing to do?
97 if (oldIndex == newIndex) {
98 return;
99 }
101 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
102 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
103 // adjust the model's index by +1 in case we're moving upwards
104 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
106 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
107 m_list.move(oldIndex, newIndex);
108 endMoveRows();
110 if (!m_list.at(newIndex)->pinned()) {
111 pin(m_list.at(newIndex)->appId());
112 } else {
113 storeAppList();
114 }
117void LauncherModel::pin(const QString &appId, int index)
119 int currentIndex = findApplication(appId);
121 if (currentIndex >= 0) {
122 if (index == -1 || index == currentIndex) {
123 m_list.at(currentIndex)->setPinned(true);
124 QModelIndex modelIndex = this->index(currentIndex);
125 Q_EMIT dataChanged(modelIndex, modelIndex);
126 } else {
127 move(currentIndex, index);
128 // move() will store the list to the backend itself, so just exit at this point.
129 return;
130 }
131 } else {
132 if (index == -1) {
133 index = m_list.count();
134 }
135 beginInsertRows(QModelIndex(), index, index);
136 LauncherItem *item = new LauncherItem(appId,
137 m_backend->displayName(appId),
138 m_backend->icon(appId),
139 !m_greeterMode);
140 item->setPinned(true);
141 m_list.insert(index, item);
142 endInsertRows();
143 }
145 storeAppList();
148void LauncherModel::requestRemove(const QString &appId)
150 int index = findApplication(appId);
151 if (index < 0) {
152 return;
153 }
155 if (m_appManager->findApplication(appId)) {
156 m_list.at(index)->setPinned(false);
157 return;
158 }
160 beginRemoveRows(QModelIndex(), index, index);
161 m_list.takeAt(index)->deleteLater();
162 endRemoveRows();
164 storeAppList();
167void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
169 int index = findApplication(appId);
170 if (index < 0) {
171 return;
172 }
174 LauncherItem *item = m_list.at(index);
175 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
176 if (model) {
177 QString actionId = model->get(actionIndex).actionId();
179 // Check if this is one of the launcher actions we handle ourselves
180 if (actionId == "pin_item") {
181 if (item->pinned()) {
182 requestRemove(appId);
183 } else {
184 pin(appId);
185 }
187 // Nope, we don't know this action, let the backend forward it to the application
188 } else {
189 m_backend->triggerQuickListAction(appId, actionId);
190 }
191 }
194void LauncherModel::setUser(const QString &username)
196 m_backend->setUser(username);
199QString LauncherModel::getUrlForAppId(const QString &appId) const
201 // appId is either an appId or a legacy app name. Let's find out which
202 QString url;
203 gchar *package, *app, *version, *longAppId;
205 // assume legacy app until proven otherwise
206 url = "application:///" + appId + ".desktop";
208 if (upstart_app_launch_app_id_parse(appId.toLatin1().constData(),
209 &package, &app, &version)) {
210 // Turn it back into an appId to test if it's a click app or not
211 longAppId = upstart_app_launch_triplet_to_app_id(package, app, version);
212 if (longAppId != nullptr) { // it is a click app!
213 url = QString("appid://%1/%2/%3").arg(package, app, version);
214 g_free(longAppId);
215 }
216 g_free(package);
217 g_free(app);
218 g_free(version);
219 }
221 return url;
224void LauncherModel::refreshStoredApplications()
226 // First remove any existing ones
227 QList<int> storedAppIndices;
228 for (int i = 0; i < m_list.count(); ++i) {
229 if (!m_list.at(i)->recent()) {
230 storedAppIndices << i;
231 }
232 }
233 int run = 0;
234 while (storedAppIndices.count() > 0) {
235 beginRemoveRows(QModelIndex(), storedAppIndices.first() - run, storedAppIndices.first() - run);
236 m_list.takeAt(storedAppIndices.first() - run)->deleteLater();
237 endRemoveRows();
238 storedAppIndices.takeFirst();
239 ++run;
240 }
242 // Now insert all stored apps at beginning of list
243 QStringList storedApplications = m_backend->storedApplications();
244 beginInsertRows(QModelIndex(), 0, storedApplications.size() - 1);
245 run = 0;
246 Q_FOREACH (const QString &entry, storedApplications) {
247 LauncherItem *item = new LauncherItem(entry,
248 m_backend->displayName(entry),
249 m_backend->icon(entry),
250 !m_greeterMode,
251 this);
252 item->setPinned(true);
253 m_list.insert(run++, item);
254 }
255 endInsertRows();
258ApplicationManagerInterface *LauncherModel::applicationManager() const
260 return m_appManager;
263void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
265 // Is there already another appmanager set?
266 if (m_appManager) {
267 // Disconnect any signals
268 disconnect(this, SLOT(applicationAdded(QModelIndex,int)));
269 disconnect(this, SLOT(applicationRemoved(QModelIndex,int)));
270 disconnect(this, SLOT(focusedAppIdChanged()));
272 // remove any recent/running apps from the launcher
273 QList<int> recentAppIndices;
274 for (int i = 0; i < m_list.count(); ++i) {
275 if (m_list.at(i)->recent()) {
276 recentAppIndices << i;
277 }
278 }
279 int run = 0;
280 while (recentAppIndices.count() > 0) {
281 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
282 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
283 endRemoveRows();
284 recentAppIndices.takeFirst();
285 ++run;
286 }
287 }
289 m_appManager = appManager;
290 connect(m_appManager, SIGNAL(rowsInserted(QModelIndex, int, int)), SLOT(applicationAdded(QModelIndex,int)));
291 connect(m_appManager, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), SLOT(applicationRemoved(QModelIndex,int)));
292 connect(m_appManager, SIGNAL(focusedApplicationIdChanged()), SLOT(focusedAppIdChanged()));
294 Q_EMIT applicationManagerChanged();
296 for (int i = 0; i < appManager->count(); ++i) {
297 applicationAdded(QModelIndex(), i);
298 }
302void LauncherModel::storeAppList()
304 QStringList appIds;
305 Q_FOREACH(LauncherItem *item, m_list) {
306 if (item->pinned()) {
307 appIds << item->appId();
308 }
309 }
310 m_backend->setStoredApplications(appIds);
313int LauncherModel::findApplication(const QString &appId)
315 for (int i = 0; i < m_list.count(); ++i) {
316 LauncherItem *item = m_list.at(i);
317 if (item->appId() == appId) {
318 return i;
319 }
320 }
321 return -1;
324void LauncherModel::progressChanged(const QString &appId, int progress)
326 int idx = findApplication(appId);
327 if (idx >= 0) {
328 LauncherItem *item = m_list.at(idx);
329 item->setProgress(progress);
330 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleProgress);
331 }
335void LauncherModel::countChanged(const QString &appId, int count)
337 int idx = findApplication(appId);
338 if (idx >= 0) {
339 LauncherItem *item = m_list.at(idx);
340 item->setCount(count);
341 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleCount);
342 }
345void LauncherModel::applicationAdded(const QModelIndex &parent, int row)
347 Q_UNUSED(parent);
349 ApplicationInfoInterface *app = m_appManager->get(row);
351 bool found = false;
352 Q_FOREACH(LauncherItem *item, m_list) {
353 if (app->appId() == item->appId()) {
354 found = true;
355 break;
356 }
357 }
358 if (found) {
359 // Shall we paint some running/recent app highlight? If yes, do it here.
360 } else {
361 LauncherItem *item = new LauncherItem(app->appId(), app->name(), app->icon().toString(), !m_greeterMode);
362 item->setRecent(true);
363 item->setFocused(app->focused());
365 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
366 m_list.append(item);
367 endInsertRows();
368 }
371void LauncherModel::applicationRemoved(const QModelIndex &parent, int row)
373 Q_UNUSED(parent)
375 int appIndex = -1;
376 for (int i = 0; i < m_list.count(); ++i) {
377 if (m_list.at(i)->appId() == m_appManager->get(row)->appId()) {
378 appIndex = i;
379 break;
380 }
381 }
383 if (appIndex > -1 && !m_list.at(appIndex)->pinned()) {
384 beginRemoveRows(QModelIndex(), appIndex, appIndex);
385 m_list.takeAt(appIndex)->deleteLater();
386 endRemoveRows();
387 }
390void LauncherModel::focusedAppIdChanged()
392 QString appId = m_appManager->focusedApplicationId();
393 for (int i = 0; i < m_list.count(); ++i) {
394 LauncherItem *item = m_list.at(i);
395 if (!item->focused() && item->appId() == appId) {
396 item->setFocused(true);
397 Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);
398 } else if (item->focused() && item->appId() != appId) {
399 item->setFocused(false);
400 Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);
401 }
402 }