on 0bc28ff help sign in
unity8/plugins/Unity/Launcher/launchermodel.cpp
556 lines 18 kB history raw
1/*
2 * Copyright 2013-2014 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 "gsettings.h"
23#include "desktopfilehandler.h"
24#include "dbusinterface.h"
25#include "asadapter.h"
27#include <unity/shell/application/ApplicationInfoInterface.h>
29#include <QDesktopServices>
30#include <QDebug>
32using namespace unity::shell::application;
34LauncherModel::LauncherModel(QObject *parent):
35 LauncherModelInterface(parent),
36 m_settings(new GSettings(this)),
37 m_dbusIface(new DBusInterface(this)),
38 m_asAdapter(new ASAdapter()),
39 m_appManager(0)
41 connect(m_dbusIface, &DBusInterface::countChanged, this, &LauncherModel::countChanged);
42 connect(m_dbusIface, &DBusInterface::countVisibleChanged, this, &LauncherModel::countVisibleChanged);
43 connect(m_dbusIface, &DBusInterface::refreshCalled, this, &LauncherModel::refresh);
44 connect(m_dbusIface, &DBusInterface::alertCalled, this, &LauncherModel::alert);
46 connect(m_settings, &GSettings::changed, this, &LauncherModel::refresh);
48 refresh();
51LauncherModel::~LauncherModel()
53 while (!m_list.empty()) {
54 m_list.takeFirst()->deleteLater();
55 }
57 delete m_asAdapter;
60int LauncherModel::rowCount(const QModelIndex &parent) const
62 Q_UNUSED(parent)
63 return m_list.count();
66QVariant LauncherModel::data(const QModelIndex &index, int role) const
68 LauncherItem *item = m_list.at(index.row());
69 switch(role) {
70 case RoleAppId:
71 return item->appId();
72 case RoleName:
73 return item->name();
74 case RoleIcon:
75 return item->icon();
76 case RolePinned:
77 return item->pinned();
78 case RoleCount:
79 return item->count();
80 case RoleCountVisible:
81 return item->countVisible();
82 case RoleProgress:
83 return item->progress();
84 case RoleFocused:
85 return item->focused();
86 case RoleAlerting:
87 return item->alerting();
88 }
90 return QVariant();
93void LauncherModel::setAlerting(const QString &appId, bool alerting) {
94 int index = findApplication(appId);
95 if (index >= 0) {
96 QModelIndex modelIndex = this->index(index);
97 LauncherItem *item = m_list.at(index);
98 if (!item->focused()) {
99 item->setAlerting(alerting);
100 Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RoleAlerting);
101 }
102 }
105unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
107 if (index < 0 || index >= m_list.count()) {
108 return 0;
109 }
110 return m_list.at(index);
113void LauncherModel::move(int oldIndex, int newIndex)
115 // Make sure its not moved outside the lists
116 if (newIndex < 0) {
117 newIndex = 0;
118 }
119 if (newIndex >= m_list.count()) {
120 newIndex = m_list.count()-1;
121 }
123 // Nothing to do?
124 if (oldIndex == newIndex) {
125 return;
126 }
128 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
129 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
130 // adjust the model's index by +1 in case we're moving upwards
131 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
133 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
134 m_list.move(oldIndex, newIndex);
135 endMoveRows();
137 if (!m_list.at(newIndex)->pinned()) {
138 pin(m_list.at(newIndex)->appId());
139 } else {
140 storeAppList();
141 }
144void LauncherModel::pin(const QString &appId, int index)
146 int currentIndex = findApplication(appId);
148 if (currentIndex >= 0) {
149 if (index == -1 || index == currentIndex) {
150 m_list.at(currentIndex)->setPinned(true);
151 QModelIndex modelIndex = this->index(currentIndex);
152 Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RolePinned);
153 } else {
154 move(currentIndex, index);
155 // move() will store the list to the backend itself, so just exit at this point.
156 return;
157 }
158 } else {
159 if (index == -1) {
160 index = m_list.count();
161 }
163 DesktopFileHandler desktopFile(appId);
164 if (!desktopFile.isValid()) {
165 qWarning() << "Can't pin this application, there is no .destkop file available.";
166 return;
167 }
169 beginInsertRows(QModelIndex(), index, index);
170 LauncherItem *item = new LauncherItem(appId,
171 desktopFile.displayName(),
172 desktopFile.icon(),
173 this);
174 item->setPinned(true);
175 m_list.insert(index, item);
176 endInsertRows();
177 }
179 storeAppList();
182void LauncherModel::requestRemove(const QString &appId)
184 unpin(appId);
185 storeAppList();
188void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
190 int index = findApplication(appId);
191 if (index < 0) {
192 return;
193 }
195 LauncherItem *item = m_list.at(index);
196 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
197 if (model) {
198 QString actionId = model->get(actionIndex).actionId();
200 // Check if this is one of the launcher actions we handle ourselves
201 if (actionId == "pin_item") {
202 if (item->pinned()) {
203 requestRemove(appId);
204 } else {
205 pin(appId);
206 }
207 } else if (actionId == "launch_item") {
208 QDesktopServices::openUrl(getUrlForAppId(appId));
210 // Nope, we don't know this action, let the backend forward it to the application
211 } else {
212 // TODO: forward quicklist action to app, possibly via m_dbusIface
213 }
214 }
217void LauncherModel::setUser(const QString &username)
219 Q_UNUSED(username)
220 qWarning() << "This backend doesn't support multiple users";
223QString LauncherModel::getUrlForAppId(const QString &appId) const
225 // appId is either an appId or a legacy app name. Let's find out which
226 if (appId.isEmpty()) {
227 return QString();
228 }
230 if (!appId.contains("_")) {
231 return "application:///" + appId + ".desktop";
232 }
234 QStringList parts = appId.split('_');
235 QString package = parts.value(0);
236 QString app = parts.value(1, "first-listed-app");
237 return "appid://" + package + "/" + app + "/current-user-version";
240ApplicationManagerInterface *LauncherModel::applicationManager() const
242 return m_appManager;
245void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
247 // Is there already another appmanager set?
248 if (m_appManager) {
249 // Disconnect any signals
250 disconnect(this, SLOT(applicationAdded(QModelIndex,int)));
251 disconnect(this, SLOT(applicationRemoved(QModelIndex,int)));
252 disconnect(this, SLOT(focusedAppIdChanged()));
254 // remove any recent/running apps from the launcher
255 QList<int> recentAppIndices;
256 for (int i = 0; i < m_list.count(); ++i) {
257 if (m_list.at(i)->recent()) {
258 recentAppIndices << i;
259 }
260 }
261 int run = 0;
262 while (recentAppIndices.count() > 0) {
263 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
264 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
265 endRemoveRows();
266 recentAppIndices.takeFirst();
267 ++run;
268 }
269 }
271 m_appManager = appManager;
272 connect(m_appManager, SIGNAL(rowsInserted(QModelIndex, int, int)), SLOT(applicationAdded(QModelIndex,int)));
273 connect(m_appManager, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), SLOT(applicationRemoved(QModelIndex,int)));
274 connect(m_appManager, SIGNAL(focusedApplicationIdChanged()), SLOT(focusedAppIdChanged()));
276 Q_EMIT applicationManagerChanged();
278 for (int i = 0; i < appManager->count(); ++i) {
279 applicationAdded(QModelIndex(), i);
280 }
283bool LauncherModel::onlyPinned() const
285 return false;
288void LauncherModel::setOnlyPinned(bool onlyPinned) {
289 Q_UNUSED(onlyPinned);
290 qWarning() << "This launcher implementation does not support showing only pinned apps";
293void LauncherModel::storeAppList()
295 QStringList appIds;
296 Q_FOREACH(LauncherItem *item, m_list) {
297 if (item->pinned()) {
298 appIds << item->appId();
299 }
300 }
301 m_settings->setStoredApplications(appIds);
302 m_asAdapter->syncItems(m_list);
305void LauncherModel::unpin(const QString &appId)
307 int index = findApplication(appId);
308 if (index < 0) {
309 return;
310 }
312 if (m_appManager->findApplication(appId)) {
313 if (m_list.at(index)->pinned()) {
314 m_list.at(index)->setPinned(false);
315 QModelIndex modelIndex = this->index(index);
316 Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RolePinned);
317 }
318 } else {
319 beginRemoveRows(QModelIndex(), index, index);
320 m_list.takeAt(index)->deleteLater();
321 endRemoveRows();
322 }
325int LauncherModel::findApplication(const QString &appId)
327 for (int i = 0; i < m_list.count(); ++i) {
328 LauncherItem *item = m_list.at(i);
329 if (item->appId() == appId) {
330 return i;
331 }
332 }
333 return -1;
336void LauncherModel::progressChanged(const QString &appId, int progress)
338 int idx = findApplication(appId);
339 if (idx >= 0) {
340 LauncherItem *item = m_list.at(idx);
341 item->setProgress(progress);
342 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleProgress);
343 }
346void LauncherModel::countChanged(const QString &appId, int count)
348 int idx = findApplication(appId);
349 if (idx >= 0) {
350 LauncherItem *item = m_list.at(idx);
351 item->setCount(count);
352 if (item->countVisible()) {
353 setAlerting(item->appId(), true);
354 }
355 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleCount);
356 m_asAdapter->syncItems(m_list);
357 }
360void LauncherModel::countVisibleChanged(const QString &appId, int countVisible)
362 int idx = findApplication(appId);
363 if (idx >= 0) {
364 LauncherItem *item = m_list.at(idx);
365 item->setCountVisible(countVisible);
366 if (countVisible) {
367 setAlerting(item->appId(), true);
368 }
369 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleCountVisible);
371 // If countVisible goes to false, and the item is neither pinned nor recent we can drop it
372 if (!countVisible && !item->pinned() && !item->recent()) {
373 beginRemoveRows(QModelIndex(), idx, idx);
374 m_list.takeAt(idx)->deleteLater();
375 endRemoveRows();
376 }
377 } else {
378 // Need to create a new LauncherItem and show the highlight
379 DesktopFileHandler desktopFile(appId);
380 if (countVisible && desktopFile.isValid()) {
381 LauncherItem *item = new LauncherItem(appId,
382 desktopFile.displayName(),
383 desktopFile.icon());
384 item->setCountVisible(true);
385 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
386 m_list.append(item);
387 endInsertRows();
388 Q_EMIT hint();
389 }
390 }
391 m_asAdapter->syncItems(m_list);
394void LauncherModel::refresh()
396 // First walk through all the existing items and see if we need to remove something
397 QList<LauncherItem*> toBeRemoved;
398 Q_FOREACH (LauncherItem* item, m_list) {
399 DesktopFileHandler desktopFile(item->appId());
400 if (!desktopFile.isValid()) {
401 // Desktop file not available for this app => drop it!
402 toBeRemoved << item;
403 } else if (!m_settings->storedApplications().contains(item->appId())) {
404 // Item not in settings any more => drop it!
405 toBeRemoved << item;
406 } else {
407 int idx = m_list.indexOf(item);
408 item->setName(desktopFile.displayName());
409 item->setIcon(desktopFile.icon());
410 item->setPinned(item->pinned()); // update pinned text if needed
411 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleName << RoleIcon);
412 }
413 }
415 Q_FOREACH (LauncherItem* item, toBeRemoved) {
416 unpin(item->appId());
417 }
419 bool changed = toBeRemoved.count() > 0;
421 // This brings the Launcher into sync with the settings backend again. There's an issue though:
422 // If we can't find a .desktop file for an entry we need to skip it. That makes our settingsIndex
423 // go out of sync with the actual index of items. So let's also use an addedIndex which reflects
424 // the settingsIndex minus the skipped items.
425 int addedIndex = 0;
427 // Now walk through settings and see if we need to add something
428 for (int settingsIndex = 0; settingsIndex < m_settings->storedApplications().count(); ++settingsIndex) {
429 QString entry = m_settings->storedApplications().at(settingsIndex);
430 int itemIndex = -1;
431 for (int i = 0; i < m_list.count(); ++i) {
432 if (m_list.at(i)->appId() == entry) {
433 itemIndex = i;
434 break;
435 }
436 }
438 if (itemIndex == -1) {
439 // Need to add it. Just add it into the addedIndex to keep same ordering as the list
440 // in the settings.
441 DesktopFileHandler desktopFile(entry);
442 if (!desktopFile.isValid()) {
443 qWarning() << "Couldn't find a .desktop file for" << entry << ". Skipping...";
444 continue;
445 }
447 LauncherItem *item = new LauncherItem(entry,
448 desktopFile.displayName(),
449 desktopFile.icon(),
450 this);
451 item->setPinned(true);
452 beginInsertRows(QModelIndex(), addedIndex, addedIndex);
453 m_list.insert(addedIndex, item);
454 endInsertRows();
455 changed = true;
456 } else if (itemIndex != addedIndex) {
457 // The item is already there, but it is in a different place than in the settings.
458 // Move it to the addedIndex
459 beginMoveRows(QModelIndex(), itemIndex, itemIndex, QModelIndex(), addedIndex);
460 m_list.move(itemIndex, addedIndex);
461 endMoveRows();
462 changed = true;
463 }
465 // Just like settingsIndex, this will increase with every item, except the ones we
466 // skipped with the "continue" call above.
467 addedIndex++;
468 }
470 if (changed) {
471 Q_EMIT hint();
472 }
474 m_asAdapter->syncItems(m_list);
477void LauncherModel::alert(const QString &appId)
479 int idx = findApplication(appId);
480 if (idx >= 0) {
481 LauncherItem *item = m_list.at(idx);
482 setAlerting(item->appId(), true);
483 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleAlerting);
484 }
487void LauncherModel::applicationAdded(const QModelIndex &parent, int row)
489 Q_UNUSED(parent);
491 ApplicationInfoInterface *app = m_appManager->get(row);
492 if (!app) {
493 qWarning() << "LauncherModel received an applicationAdded signal, but there's no such application!";
494 return;
495 }
497 if (app->appId() == "unity8-dash") {
498 // Not adding the dash app
499 return;
500 }
502 int itemIndex = findApplication(app->appId());
503 if (itemIndex != -1) {
504 LauncherItem *item = m_list.at(itemIndex);
505 if (!item->recent()) {
506 item->setRecent(true);
507 m_asAdapter->syncItems(m_list);
508 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), QVector<int>() << RoleRecent);
509 }
510 // Shall we paint some running/recent app highlight? If yes, do it here.
511 } else {
512 LauncherItem *item = new LauncherItem(app->appId(), app->name(), app->icon().toString(), this);
513 item->setRecent(true);
514 item->setFocused(app->focused());
516 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
517 m_list.append(item);
518 endInsertRows();
519 m_asAdapter->syncItems(m_list);
520 }
523void LauncherModel::applicationRemoved(const QModelIndex &parent, int row)
525 Q_UNUSED(parent)
527 int appIndex = -1;
528 for (int i = 0; i < m_list.count(); ++i) {
529 if (m_list.at(i)->appId() == m_appManager->get(row)->appId()) {
530 appIndex = i;
531 break;
532 }
533 }
535 if (appIndex > -1 && !m_list.at(appIndex)->pinned()) {
536 beginRemoveRows(QModelIndex(), appIndex, appIndex);
537 m_list.takeAt(appIndex)->deleteLater();
538 endRemoveRows();
539 m_asAdapter->syncItems(m_list);
540 }
543void LauncherModel::focusedAppIdChanged()
545 QString appId = m_appManager->focusedApplicationId();
546 for (int i = 0; i < m_list.count(); ++i) {
547 LauncherItem *item = m_list.at(i);
548 if (!item->focused() && item->appId() == appId) {
549 item->setFocused(true);
550 Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);
551 } else if (item->focused() && item->appId() != appId) {
552 item->setFocused(false);
553 Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);
554 }
555 }