on a63f337 help sign in
unity8/plugins/Unity/Launcher/launchermodel.cpp
557 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::progressChanged, this, &LauncherModel::progressChanged);
44 connect(m_dbusIface, &DBusInterface::refreshCalled, this, &LauncherModel::refresh);
45 connect(m_dbusIface, &DBusInterface::alertCalled, this, &LauncherModel::alert);
47 connect(m_settings, &GSettings::changed, this, &LauncherModel::refresh);
49 refresh();
52LauncherModel::~LauncherModel()
54 while (!m_list.empty()) {
55 m_list.takeFirst()->deleteLater();
56 }
58 delete m_asAdapter;
61int LauncherModel::rowCount(const QModelIndex &parent) const
63 Q_UNUSED(parent)
64 return m_list.count();
67QVariant LauncherModel::data(const QModelIndex &index, int role) const
69 LauncherItem *item = m_list.at(index.row());
70 switch(role) {
71 case RoleAppId:
72 return item->appId();
73 case RoleName:
74 return item->name();
75 case RoleIcon:
76 return item->icon();
77 case RolePinned:
78 return item->pinned();
79 case RoleCount:
80 return item->count();
81 case RoleCountVisible:
82 return item->countVisible();
83 case RoleProgress:
84 return item->progress();
85 case RoleFocused:
86 return item->focused();
87 case RoleAlerting:
88 return item->alerting();
89 }
91 return QVariant();
94void LauncherModel::setAlerting(const QString &appId, bool alerting) {
95 int index = findApplication(appId);
96 if (index >= 0) {
97 QModelIndex modelIndex = this->index(index);
98 LauncherItem *item = m_list.at(index);
99 if (!item->focused()) {
100 item->setAlerting(alerting);
101 Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RoleAlerting);
102 }
103 }
106unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
108 if (index < 0 || index >= m_list.count()) {
109 return 0;
110 }
111 return m_list.at(index);
114void LauncherModel::move(int oldIndex, int newIndex)
116 // Make sure its not moved outside the lists
117 if (newIndex < 0) {
118 newIndex = 0;
119 }
120 if (newIndex >= m_list.count()) {
121 newIndex = m_list.count()-1;
122 }
124 // Nothing to do?
125 if (oldIndex == newIndex) {
126 return;
127 }
129 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
130 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
131 // adjust the model's index by +1 in case we're moving upwards
132 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
134 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
135 m_list.move(oldIndex, newIndex);
136 endMoveRows();
138 if (!m_list.at(newIndex)->pinned()) {
139 pin(m_list.at(newIndex)->appId());
140 } else {
141 storeAppList();
142 }
145void LauncherModel::pin(const QString &appId, int index)
147 int currentIndex = findApplication(appId);
149 if (currentIndex >= 0) {
150 if (index == -1 || index == currentIndex) {
151 m_list.at(currentIndex)->setPinned(true);
152 QModelIndex modelIndex = this->index(currentIndex);
153 Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RolePinned);
154 } else {
155 move(currentIndex, index);
156 // move() will store the list to the backend itself, so just exit at this point.
157 return;
158 }
159 } else {
160 if (index == -1) {
161 index = m_list.count();
162 }
164 DesktopFileHandler desktopFile(appId);
165 if (!desktopFile.isValid()) {
166 qWarning() << "Can't pin this application, there is no .destkop file available.";
167 return;
168 }
170 beginInsertRows(QModelIndex(), index, index);
171 LauncherItem *item = new LauncherItem(appId,
172 desktopFile.displayName(),
173 desktopFile.icon(),
174 this);
175 item->setPinned(true);
176 m_list.insert(index, item);
177 endInsertRows();
178 }
180 storeAppList();
183void LauncherModel::requestRemove(const QString &appId)
185 unpin(appId);
186 storeAppList();
189void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
191 int index = findApplication(appId);
192 if (index < 0) {
193 return;
194 }
196 LauncherItem *item = m_list.at(index);
197 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
198 if (model) {
199 QString actionId = model->get(actionIndex).actionId();
201 // Check if this is one of the launcher actions we handle ourselves
202 if (actionId == "pin_item") {
203 if (item->pinned()) {
204 requestRemove(appId);
205 } else {
206 pin(appId);
207 }
208 } else if (actionId == "launch_item") {
209 QDesktopServices::openUrl(getUrlForAppId(appId));
211 // Nope, we don't know this action, let the backend forward it to the application
212 } else {
213 // TODO: forward quicklist action to app, possibly via m_dbusIface
214 }
215 }
218void LauncherModel::setUser(const QString &username)
220 Q_UNUSED(username)
221 qWarning() << "This backend doesn't support multiple users";
224QString LauncherModel::getUrlForAppId(const QString &appId) const
226 // appId is either an appId or a legacy app name. Let's find out which
227 if (appId.isEmpty()) {
228 return QString();
229 }
231 if (!appId.contains("_")) {
232 return "application:///" + appId + ".desktop";
233 }
235 QStringList parts = appId.split('_');
236 QString package = parts.value(0);
237 QString app = parts.value(1, "first-listed-app");
238 return "appid://" + package + "/" + app + "/current-user-version";
241ApplicationManagerInterface *LauncherModel::applicationManager() const
243 return m_appManager;
246void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
248 // Is there already another appmanager set?
249 if (m_appManager) {
250 // Disconnect any signals
251 disconnect(this, SLOT(applicationAdded(QModelIndex,int)));
252 disconnect(this, SLOT(applicationRemoved(QModelIndex,int)));
253 disconnect(this, SLOT(focusedAppIdChanged()));
255 // remove any recent/running apps from the launcher
256 QList<int> recentAppIndices;
257 for (int i = 0; i < m_list.count(); ++i) {
258 if (m_list.at(i)->recent()) {
259 recentAppIndices << i;
260 }
261 }
262 int run = 0;
263 while (recentAppIndices.count() > 0) {
264 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
265 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
266 endRemoveRows();
267 recentAppIndices.takeFirst();
268 ++run;
269 }
270 }
272 m_appManager = appManager;
273 connect(m_appManager, SIGNAL(rowsInserted(QModelIndex, int, int)), SLOT(applicationAdded(QModelIndex,int)));
274 connect(m_appManager, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), SLOT(applicationRemoved(QModelIndex,int)));
275 connect(m_appManager, SIGNAL(focusedApplicationIdChanged()), SLOT(focusedAppIdChanged()));
277 Q_EMIT applicationManagerChanged();
279 for (int i = 0; i < appManager->count(); ++i) {
280 applicationAdded(QModelIndex(), i);
281 }
284bool LauncherModel::onlyPinned() const
286 return false;
289void LauncherModel::setOnlyPinned(bool onlyPinned) {
290 Q_UNUSED(onlyPinned);
291 qWarning() << "This launcher implementation does not support showing only pinned apps";
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_settings->setStoredApplications(appIds);
303 m_asAdapter->syncItems(m_list);
306void LauncherModel::unpin(const QString &appId)
308 int index = findApplication(appId);
309 if (index < 0) {
310 return;
311 }
313 if (m_appManager->findApplication(appId)) {
314 if (m_list.at(index)->pinned()) {
315 m_list.at(index)->setPinned(false);
316 QModelIndex modelIndex = this->index(index);
317 Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RolePinned);
318 }
319 } else {
320 beginRemoveRows(QModelIndex(), index, index);
321 m_list.takeAt(index)->deleteLater();
322 endRemoveRows();
323 }
326int LauncherModel::findApplication(const QString &appId)
328 for (int i = 0; i < m_list.count(); ++i) {
329 LauncherItem *item = m_list.at(i);
330 if (item->appId() == appId) {
331 return i;
332 }
333 }
334 return -1;
337void LauncherModel::progressChanged(const QString &appId, int progress)
339 int idx = findApplication(appId);
340 if (idx >= 0) {
341 LauncherItem *item = m_list.at(idx);
342 item->setProgress(progress);
343 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleProgress);
344 }
347void LauncherModel::countChanged(const QString &appId, int count)
349 int idx = findApplication(appId);
350 if (idx >= 0) {
351 LauncherItem *item = m_list.at(idx);
352 item->setCount(count);
353 if (item->countVisible()) {
354 setAlerting(item->appId(), true);
355 }
356 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleCount);
357 m_asAdapter->syncItems(m_list);
358 }
361void LauncherModel::countVisibleChanged(const QString &appId, int countVisible)
363 int idx = findApplication(appId);
364 if (idx >= 0) {
365 LauncherItem *item = m_list.at(idx);
366 item->setCountVisible(countVisible);
367 if (countVisible) {
368 setAlerting(item->appId(), true);
369 }
370 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleCountVisible);
372 // If countVisible goes to false, and the item is neither pinned nor recent we can drop it
373 if (!countVisible && !item->pinned() && !item->recent()) {
374 beginRemoveRows(QModelIndex(), idx, idx);
375 m_list.takeAt(idx)->deleteLater();
376 endRemoveRows();
377 }
378 } else {
379 // Need to create a new LauncherItem and show the highlight
380 DesktopFileHandler desktopFile(appId);
381 if (countVisible && desktopFile.isValid()) {
382 LauncherItem *item = new LauncherItem(appId,
383 desktopFile.displayName(),
384 desktopFile.icon());
385 item->setCountVisible(true);
386 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
387 m_list.append(item);
388 endInsertRows();
389 Q_EMIT hint();
390 }
391 }
392 m_asAdapter->syncItems(m_list);
395void LauncherModel::refresh()
397 // First walk through all the existing items and see if we need to remove something
398 QList<LauncherItem*> toBeRemoved;
399 Q_FOREACH (LauncherItem* item, m_list) {
400 DesktopFileHandler desktopFile(item->appId());
401 if (!desktopFile.isValid()) {
402 // Desktop file not available for this app => drop it!
403 toBeRemoved << item;
404 } else if (!m_settings->storedApplications().contains(item->appId())) {
405 // Item not in settings any more => drop it!
406 toBeRemoved << item;
407 } else {
408 int idx = m_list.indexOf(item);
409 item->setName(desktopFile.displayName());
410 item->setIcon(desktopFile.icon());
411 item->setPinned(item->pinned()); // update pinned text if needed
412 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleName << RoleIcon);
413 }
414 }
416 Q_FOREACH (LauncherItem* item, toBeRemoved) {
417 unpin(item->appId());
418 }
420 bool changed = toBeRemoved.count() > 0;
422 // This brings the Launcher into sync with the settings backend again. There's an issue though:
423 // If we can't find a .desktop file for an entry we need to skip it. That makes our settingsIndex
424 // go out of sync with the actual index of items. So let's also use an addedIndex which reflects
425 // the settingsIndex minus the skipped items.
426 int addedIndex = 0;
428 // Now walk through settings and see if we need to add something
429 for (int settingsIndex = 0; settingsIndex < m_settings->storedApplications().count(); ++settingsIndex) {
430 QString entry = m_settings->storedApplications().at(settingsIndex);
431 int itemIndex = -1;
432 for (int i = 0; i < m_list.count(); ++i) {
433 if (m_list.at(i)->appId() == entry) {
434 itemIndex = i;
435 break;
436 }
437 }
439 if (itemIndex == -1) {
440 // Need to add it. Just add it into the addedIndex to keep same ordering as the list
441 // in the settings.
442 DesktopFileHandler desktopFile(entry);
443 if (!desktopFile.isValid()) {
444 qWarning() << "Couldn't find a .desktop file for" << entry << ". Skipping...";
445 continue;
446 }
448 LauncherItem *item = new LauncherItem(entry,
449 desktopFile.displayName(),
450 desktopFile.icon(),
451 this);
452 item->setPinned(true);
453 beginInsertRows(QModelIndex(), addedIndex, addedIndex);
454 m_list.insert(addedIndex, item);
455 endInsertRows();
456 changed = true;
457 } else if (itemIndex != addedIndex) {
458 // The item is already there, but it is in a different place than in the settings.
459 // Move it to the addedIndex
460 beginMoveRows(QModelIndex(), itemIndex, itemIndex, QModelIndex(), addedIndex);
461 m_list.move(itemIndex, addedIndex);
462 endMoveRows();
463 changed = true;
464 }
466 // Just like settingsIndex, this will increase with every item, except the ones we
467 // skipped with the "continue" call above.
468 addedIndex++;
469 }
471 if (changed) {
472 Q_EMIT hint();
473 }
475 m_asAdapter->syncItems(m_list);
478void LauncherModel::alert(const QString &appId)
480 int idx = findApplication(appId);
481 if (idx >= 0) {
482 LauncherItem *item = m_list.at(idx);
483 setAlerting(item->appId(), true);
484 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleAlerting);
485 }
488void LauncherModel::applicationAdded(const QModelIndex &parent, int row)
490 Q_UNUSED(parent);
492 ApplicationInfoInterface *app = m_appManager->get(row);
493 if (!app) {
494 qWarning() << "LauncherModel received an applicationAdded signal, but there's no such application!";
495 return;
496 }
498 if (app->appId() == "unity8-dash") {
499 // Not adding the dash app
500 return;
501 }
503 int itemIndex = findApplication(app->appId());
504 if (itemIndex != -1) {
505 LauncherItem *item = m_list.at(itemIndex);
506 if (!item->recent()) {
507 item->setRecent(true);
508 m_asAdapter->syncItems(m_list);
509 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), QVector<int>() << RoleRecent);
510 }
511 // Shall we paint some running/recent app highlight? If yes, do it here.
512 } else {
513 LauncherItem *item = new LauncherItem(app->appId(), app->name(), app->icon().toString(), this);
514 item->setRecent(true);
515 item->setFocused(app->focused());
517 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
518 m_list.append(item);
519 endInsertRows();
520 m_asAdapter->syncItems(m_list);
521 }
524void LauncherModel::applicationRemoved(const QModelIndex &parent, int row)
526 Q_UNUSED(parent)
528 int appIndex = -1;
529 for (int i = 0; i < m_list.count(); ++i) {
530 if (m_list.at(i)->appId() == m_appManager->get(row)->appId()) {
531 appIndex = i;
532 break;
533 }
534 }
536 if (appIndex > -1 && !m_list.at(appIndex)->pinned()) {
537 beginRemoveRows(QModelIndex(), appIndex, appIndex);
538 m_list.takeAt(appIndex)->deleteLater();
539 endRemoveRows();
540 m_asAdapter->syncItems(m_list);
541 }
544void LauncherModel::focusedAppIdChanged()
546 QString appId = m_appManager->focusedApplicationId();
547 for (int i = 0; i < m_list.count(); ++i) {
548 LauncherItem *item = m_list.at(i);
549 if (!item->focused() && item->appId() == appId) {
550 item->setFocused(true);
551 Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);
552 } else if (item->focused() && item->appId() != appId) {
553 item->setFocused(false);
554 Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);
555 }
556 }