on af9bb15 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(nullptr)
41 connect(m_dbusIface, &DBusInterface::countChanged, this, &LauncherModel::countChanged);
42 connect(m_dbusIface, &DBusInterface::countVisibleChanged, this, &LauncherModel::countVisibleChanged);
43 connect(m_dbusIface, &DBusInterface::runningChanged, this, &LauncherModel::runningChanged);
44 connect(m_dbusIface, &DBusInterface::refreshCalled, this, &LauncherModel::refresh);
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 RoleRunning:
87 return item->running();
88 default:
89 qWarning() << Q_FUNC_INFO << "missing role, implement me";
90 return QVariant();
91 }
93 return QVariant();
96unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
98 if (index < 0 || index >= m_list.count()) {
99 return 0;
100 }
101 return m_list.at(index);
104void LauncherModel::move(int oldIndex, int newIndex)
106 // Make sure its not moved outside the lists
107 if (newIndex < 0) {
108 newIndex = 0;
109 }
110 if (newIndex >= m_list.count()) {
111 newIndex = m_list.count()-1;
112 }
114 // Nothing to do?
115 if (oldIndex == newIndex) {
116 return;
117 }
119 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
120 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
121 // adjust the model's index by +1 in case we're moving upwards
122 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
124 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
125 m_list.move(oldIndex, newIndex);
126 endMoveRows();
128 if (!m_list.at(newIndex)->pinned()) {
129 pin(m_list.at(newIndex)->appId());
130 } else {
131 storeAppList();
132 }
135void LauncherModel::pin(const QString &appId, int index)
137 int currentIndex = findApplication(appId);
139 if (currentIndex >= 0) {
140 if (index == -1 || index == currentIndex) {
141 m_list.at(currentIndex)->setPinned(true);
142 QModelIndex modelIndex = this->index(currentIndex);
143 Q_EMIT dataChanged(modelIndex, modelIndex, {RolePinned});
144 } else {
145 move(currentIndex, index);
146 // move() will store the list to the backend itself, so just exit at this point.
147 return;
148 }
149 } else {
150 if (index == -1) {
151 index = m_list.count();
152 }
154 DesktopFileHandler desktopFile(appId);
155 if (!desktopFile.isValid()) {
156 qWarning() << "Can't pin this application, there is no .desktop file available.";
157 return;
158 }
160 beginInsertRows(QModelIndex(), index, index);
161 LauncherItem *item = new LauncherItem(appId,
162 desktopFile.displayName(),
163 desktopFile.icon(),
164 this);
165 item->setPinned(true);
166 m_list.insert(index, item);
167 endInsertRows();
168 }
170 storeAppList();
173void LauncherModel::requestRemove(const QString &appId)
175 unpin(appId);
176 storeAppList();
179void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
181 const int index = findApplication(appId);
182 if (index < 0) {
183 return;
184 }
186 LauncherItem *item = m_list.at(index);
187 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
188 if (model) {
189 const QString actionId = model->get(actionIndex).actionId();
191 // Check if this is one of the launcher actions we handle ourselves
192 if (actionId == "pin_item") {
193 if (item->pinned()) {
194 requestRemove(appId);
195 } else {
196 pin(appId);
197 }
198 } else if (actionId == "launch_item") {
199 QDesktopServices::openUrl(getUrlForAppId(appId));
200 } else if (actionId == "stop_item") { // Quit
201 m_appManager->stopApplication(appId);
202 // Nope, we don't know this action, let the backend forward it to the application
203 } else {
204 // TODO: forward quicklist action to app, possibly via m_dbusIface
205 }
206 }
209void LauncherModel::setUser(const QString &username)
211 Q_UNUSED(username)
212 qWarning() << "This backend doesn't support multiple users";
215QString LauncherModel::getUrlForAppId(const QString &appId) const
217 // appId is either an appId or a legacy app name. Let's find out which
218 if (appId.isEmpty()) {
219 return QString();
220 }
222 if (!appId.contains("_")) {
223 return "application:///" + appId + ".desktop";
224 }
226 QStringList parts = appId.split('_');
227 QString package = parts.value(0);
228 QString app = parts.value(1, "first-listed-app");
229 return "appid://" + package + "/" + app + "/current-user-version";
232ApplicationManagerInterface *LauncherModel::applicationManager() const
234 return m_appManager;
237void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
239 // Is there already another appmanager set?
240 if (m_appManager) {
241 // Disconnect any signals
242 disconnect(this, SLOT(applicationAdded(QModelIndex,int)));
243 disconnect(this, SLOT(applicationRemoved(QModelIndex,int)));
244 disconnect(this, SLOT(focusedAppIdChanged()));
246 // remove any recent/running apps from the launcher
247 QList<int> recentAppIndices;
248 for (int i = 0; i < m_list.count(); ++i) {
249 if (m_list.at(i)->recent()) {
250 recentAppIndices << i;
251 }
252 }
253 int run = 0;
254 while (recentAppIndices.count() > 0) {
255 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
256 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
257 endRemoveRows();
258 recentAppIndices.takeFirst();
259 ++run;
260 }
261 }
263 m_appManager = appManager;
264 connect(m_appManager, SIGNAL(rowsInserted(QModelIndex, int, int)), SLOT(applicationAdded(QModelIndex,int)));
265 connect(m_appManager, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), SLOT(applicationRemoved(QModelIndex,int)));
266 connect(m_appManager, SIGNAL(focusedApplicationIdChanged()), SLOT(focusedAppIdChanged()));
268 Q_EMIT applicationManagerChanged();
270 for (int i = 0; i < appManager->count(); ++i) {
271 applicationAdded(QModelIndex(), i);
272 }
275bool LauncherModel::onlyPinned() const
277 return false;
280void LauncherModel::setOnlyPinned(bool onlyPinned) {
281 Q_UNUSED(onlyPinned);
282 qWarning() << "This launcher implementation does not support showing only pinned apps";
285void LauncherModel::storeAppList()
287 QStringList appIds;
288 Q_FOREACH(LauncherItem *item, m_list) {
289 if (item->pinned()) {
290 appIds << item->appId();
291 }
292 }
293 m_settings->setStoredApplications(appIds);
294 m_asAdapter->syncItems(m_list);
297void LauncherModel::unpin(const QString &appId)
299 const int index = findApplication(appId);
300 if (index < 0) {
301 return;
302 }
304 if (m_appManager->findApplication(appId)) {
305 if (m_list.at(index)->pinned()) {
306 m_list.at(index)->setPinned(false);
307 QModelIndex modelIndex = this->index(index);
308 Q_EMIT dataChanged(modelIndex, modelIndex, {RolePinned});
309 }
310 } else {
311 beginRemoveRows(QModelIndex(), index, index);
312 m_list.takeAt(index)->deleteLater();
313 endRemoveRows();
314 }
317int LauncherModel::findApplication(const QString &appId)
319 for (int i = 0; i < m_list.count(); ++i) {
320 LauncherItem *item = m_list.at(i);
321 if (item->appId() == appId) {
322 return i;
323 }
324 }
325 return -1;
328void LauncherModel::progressChanged(const QString &appId, int progress)
330 const int idx = findApplication(appId);
331 if (idx >= 0) {
332 LauncherItem *item = m_list.at(idx);
333 item->setProgress(progress);
334 Q_EMIT dataChanged(index(idx), index(idx), {RoleProgress});
335 }
338void LauncherModel::runningChanged(const QString &appId, bool running)
340 const int idx = findApplication(appId);
341 if (idx >= 0) {
342 LauncherItem *item = m_list.at(idx);
343 item->setRunning(running);
344 m_asAdapter->syncItems(m_list);
345 Q_EMIT dataChanged(index(idx), index(idx), {RoleRunning});
346 }
349void LauncherModel::countChanged(const QString &appId, int count)
351 const int idx = findApplication(appId);
352 if (idx >= 0) {
353 LauncherItem *item = m_list.at(idx);
354 item->setCount(count);
355 m_asAdapter->syncItems(m_list);
356 Q_EMIT dataChanged(index(idx), index(idx), {RoleCount});
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 Q_EMIT dataChanged(index(idx), index(idx), {RoleCountVisible});
368 // If countVisible goes to false, and the item is neither pinned nor recent we can drop it
369 if (!countVisible && !item->pinned() && !item->recent()) {
370 beginRemoveRows(QModelIndex(), idx, idx);
371 m_list.takeAt(idx)->deleteLater();
372 endRemoveRows();
373 }
374 } else {
375 // Need to create a new LauncherItem and show the highlight
376 DesktopFileHandler desktopFile(appId);
377 if (countVisible && desktopFile.isValid()) {
378 LauncherItem *item = new LauncherItem(appId,
379 desktopFile.displayName(),
380 desktopFile.icon());
381 item->setCountVisible(true);
382 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
383 m_list.append(item);
384 endInsertRows();
385 Q_EMIT hint();
386 }
387 }
388 m_asAdapter->syncItems(m_list);
391void LauncherModel::refresh()
393 // First walk through all the existing items and see if we need to remove something
394 QList<LauncherItem*> toBeRemoved;
395 Q_FOREACH (LauncherItem* item, m_list) {
396 DesktopFileHandler desktopFile(item->appId());
397 if (!desktopFile.isValid()) {
398 // Desktop file not available for this app => drop it!
399 toBeRemoved << item;
400 } else if (!m_settings->storedApplications().contains(item->appId())) {
401 // Item not in settings any more => drop it!
402 toBeRemoved << item;
403 } else {
404 int idx = m_list.indexOf(item);
405 item->setName(desktopFile.displayName());
406 item->setIcon(desktopFile.icon());
407 item->setPinned(item->pinned()); // update pinned text if needed
408 item->setRunning(item->running());
409 Q_EMIT dataChanged(index(idx), index(idx), {RoleName, RoleIcon, RoleRunning});
410 }
411 }
413 Q_FOREACH (LauncherItem* item, toBeRemoved) {
414 unpin(item->appId());
415 }
417 bool changed = toBeRemoved.count() > 0;
419 // This brings the Launcher into sync with the settings backend again. There's an issue though:
420 // If we can't find a .desktop file for an entry we need to skip it. That makes our settingsIndex
421 // go out of sync with the actual index of items. So let's also use an addedIndex which reflects
422 // the settingsIndex minus the skipped items.
423 int addedIndex = 0;
425 // Now walk through settings and see if we need to add something
426 for (int settingsIndex = 0; settingsIndex < m_settings->storedApplications().count(); ++settingsIndex) {
427 const QString entry = m_settings->storedApplications().at(settingsIndex);
428 int itemIndex = -1;
429 for (int i = 0; i < m_list.count(); ++i) {
430 if (m_list.at(i)->appId() == entry) {
431 itemIndex = i;
432 break;
433 }
434 }
436 if (itemIndex == -1) {
437 // Need to add it. Just add it into the addedIndex to keep same ordering as the list
438 // in the settings.
439 DesktopFileHandler desktopFile(entry);
440 if (!desktopFile.isValid()) {
441 qWarning() << "Couldn't find a .desktop file for" << entry << ". Skipping...";
442 continue;
443 }
445 LauncherItem *item = new LauncherItem(entry,
446 desktopFile.displayName(),
447 desktopFile.icon(),
448 this);
449 item->setPinned(true);
450 beginInsertRows(QModelIndex(), addedIndex, addedIndex);
451 m_list.insert(addedIndex, item);
452 endInsertRows();
453 changed = true;
454 } else if (itemIndex != addedIndex) {
455 // The item is already there, but it is in a different place than in the settings.
456 // Move it to the addedIndex
457 beginMoveRows(QModelIndex(), itemIndex, itemIndex, QModelIndex(), addedIndex);
458 m_list.move(itemIndex, addedIndex);
459 endMoveRows();
460 changed = true;
461 }
463 // Just like settingsIndex, this will increase with every item, except the ones we
464 // skipped with the "continue" call above.
465 addedIndex++;
466 }
468 if (changed) {
469 Q_EMIT hint();
470 }
472 m_asAdapter->syncItems(m_list);
475void LauncherModel::applicationAdded(const QModelIndex &parent, int row)
477 Q_UNUSED(parent);
479 ApplicationInfoInterface *app = m_appManager->get(row);
480 if (!app) {
481 qWarning() << "LauncherModel received an applicationAdded signal, but there's no such application!";
482 return;
483 }
485 if (app->appId() == "unity8-dash") {
486 // Not adding the dash app
487 return;
488 }
490 const int itemIndex = findApplication(app->appId());
491 if (itemIndex != -1) {
492 LauncherItem *item = m_list.at(itemIndex);
493 if (!item->recent()) {
494 item->setRecent(true);
495 item->setRunning(true);
496 m_asAdapter->syncItems(m_list);
497 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), {RoleRecent});
498 }
499 // TODO Shall we paint some running/recent app highlight? If yes, do it here.
500 } else {
501 LauncherItem *item = new LauncherItem(app->appId(), app->name(), app->icon().toString(), this);
502 item->setRecent(true);
503 item->setRunning(true);
504 item->setFocused(app->focused());
506 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
507 m_list.append(item);
508 endInsertRows();
509 m_asAdapter->syncItems(m_list);
510 }
511 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), {RoleRunning});
514void LauncherModel::applicationRemoved(const QModelIndex &parent, int row)
516 Q_UNUSED(parent)
518 int appIndex = -1;
519 for (int i = 0; i < m_list.count(); ++i) {
520 if (m_list.at(i)->appId() == m_appManager->get(row)->appId()) {
521 appIndex = i;
522 break;
523 }
524 }
526 if (appIndex < 0) {
527 qWarning() << Q_FUNC_INFO << "appIndex not found";
528 return;
529 }
531 LauncherItem * item = m_list.at(appIndex);
532 item->setRunning(false);
534 if (!item->pinned()) {
535 beginRemoveRows(QModelIndex(), appIndex, appIndex);
536 m_list.takeAt(appIndex)->deleteLater();
537 endRemoveRows();
538 m_asAdapter->syncItems(m_list);
539 Q_EMIT dataChanged(index(appIndex), index(appIndex), {RolePinned});
540 }
541 Q_EMIT dataChanged(index(appIndex), index(appIndex), {RoleRunning});
544void LauncherModel::focusedAppIdChanged()
546 const 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), {RoleFocused});
552 } else if (item->focused() && item->appId() != appId) {
553 item->setFocused(false);
554 Q_EMIT dataChanged(index(i), index(i), {RoleFocused});
555 }
556 }