on bcbf76a help sign in
unity8/plugins/Unity/Launcher/launchermodel.cpp
576 lines 19 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::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 case RoleRunning:
90 return item->running();
91 default:
92 qWarning() << Q_FUNC_INFO << "missing role, implement me";
93 return QVariant();
94 }
96 return QVariant();
99void LauncherModel::setAlerting(const QString &appId, bool alerting) {
100 int index = findApplication(appId);
101 if (index >= 0) {
102 QModelIndex modelIndex = this->index(index);
103 LauncherItem *item = m_list.at(index);
104 if (!item->focused()) {
105 item->setAlerting(alerting);
106 Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RoleAlerting);
107 }
108 }
111unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
113 if (index < 0 || index >= m_list.count()) {
114 return 0;
115 }
116 return m_list.at(index);
119void LauncherModel::move(int oldIndex, int newIndex)
121 // Make sure its not moved outside the lists
122 if (newIndex < 0) {
123 newIndex = 0;
124 }
125 if (newIndex >= m_list.count()) {
126 newIndex = m_list.count()-1;
127 }
129 // Nothing to do?
130 if (oldIndex == newIndex) {
131 return;
132 }
134 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
135 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
136 // adjust the model's index by +1 in case we're moving upwards
137 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
139 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
140 m_list.move(oldIndex, newIndex);
141 endMoveRows();
143 if (!m_list.at(newIndex)->pinned()) {
144 pin(m_list.at(newIndex)->appId());
145 } else {
146 storeAppList();
147 }
150void LauncherModel::pin(const QString &appId, int index)
152 int currentIndex = findApplication(appId);
154 if (currentIndex >= 0) {
155 if (index == -1 || index == currentIndex) {
156 m_list.at(currentIndex)->setPinned(true);
157 QModelIndex modelIndex = this->index(currentIndex);
158 Q_EMIT dataChanged(modelIndex, modelIndex, {RolePinned});
159 } else {
160 move(currentIndex, index);
161 // move() will store the list to the backend itself, so just exit at this point.
162 return;
163 }
164 } else {
165 if (index == -1) {
166 index = m_list.count();
167 }
169 DesktopFileHandler desktopFile(appId);
170 if (!desktopFile.isValid()) {
171 qWarning() << "Can't pin this application, there is no .desktop file available.";
172 return;
173 }
175 beginInsertRows(QModelIndex(), index, index);
176 LauncherItem *item = new LauncherItem(appId,
177 desktopFile.displayName(),
178 desktopFile.icon(),
179 this);
180 item->setPinned(true);
181 m_list.insert(index, item);
182 endInsertRows();
183 }
185 storeAppList();
188void LauncherModel::requestRemove(const QString &appId)
190 unpin(appId);
191 storeAppList();
194void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
196 const int index = findApplication(appId);
197 if (index < 0) {
198 return;
199 }
201 LauncherItem *item = m_list.at(index);
202 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
203 if (model) {
204 const QString actionId = model->get(actionIndex).actionId();
206 // Check if this is one of the launcher actions we handle ourselves
207 if (actionId == QLatin1String("pin_item")) {
208 if (item->pinned()) {
209 requestRemove(appId);
210 } else {
211 pin(appId);
212 }
213 } else if (actionId == QLatin1String("launch_item")) {
214 QDesktopServices::openUrl(getUrlForAppId(appId));
215 } else if (actionId == QLatin1String("stop_item")) { // Quit
216 if (m_appManager) {
217 m_appManager->stopApplication(appId);
218 }
219 // Nope, we don't know this action, let the backend forward it to the application
220 } else {
221 // TODO: forward quicklist action to app, possibly via m_dbusIface
222 }
223 }
226void LauncherModel::setUser(const QString &username)
228 Q_UNUSED(username)
229 qWarning() << "This backend doesn't support multiple users";
232QString LauncherModel::getUrlForAppId(const QString &appId) const
234 // appId is either an appId or a legacy app name. Let's find out which
235 if (appId.isEmpty()) {
236 return QString();
237 }
239 if (!appId.contains('_')) {
240 return "application:///" + appId + ".desktop";
241 }
243 QStringList parts = appId.split('_');
244 QString package = parts.value(0);
245 QString app = parts.value(1, QStringLiteral("first-listed-app"));
246 return "appid://" + package + "/" + app + "/current-user-version";
249ApplicationManagerInterface *LauncherModel::applicationManager() const
251 return m_appManager;
254void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
256 // Is there already another appmanager set?
257 if (m_appManager) {
258 // Disconnect any signals
259 disconnect(this, &LauncherModel::applicationAdded, 0, nullptr);
260 disconnect(this, &LauncherModel::applicationRemoved, 0, nullptr);
261 disconnect(this, &LauncherModel::focusedAppIdChanged, 0, nullptr);
263 // remove any recent/running apps from the launcher
264 QList<int> recentAppIndices;
265 for (int i = 0; i < m_list.count(); ++i) {
266 if (m_list.at(i)->recent()) {
267 recentAppIndices << i;
268 }
269 }
270 int run = 0;
271 while (recentAppIndices.count() > 0) {
272 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
273 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
274 endRemoveRows();
275 recentAppIndices.takeFirst();
276 ++run;
277 }
278 }
280 m_appManager = appManager;
281 connect(m_appManager, &ApplicationManagerInterface::rowsInserted, this, &LauncherModel::applicationAdded);
282 connect(m_appManager, &ApplicationManagerInterface::rowsAboutToBeRemoved, this, &LauncherModel::applicationRemoved);
283 connect(m_appManager, &ApplicationManagerInterface::focusedApplicationIdChanged, this, &LauncherModel::focusedAppIdChanged);
285 Q_EMIT applicationManagerChanged();
287 for (int i = 0; i < appManager->count(); ++i) {
288 applicationAdded(QModelIndex(), i);
289 }
292bool LauncherModel::onlyPinned() const
294 return false;
297void LauncherModel::setOnlyPinned(bool onlyPinned) {
298 Q_UNUSED(onlyPinned);
299 qWarning() << "This launcher implementation does not support showing only pinned apps";
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_settings->setStoredApplications(appIds);
311 m_asAdapter->syncItems(m_list);
314void LauncherModel::unpin(const QString &appId)
316 const int index = findApplication(appId);
317 if (index < 0) {
318 return;
319 }
321 if (m_appManager->findApplication(appId)) {
322 if (m_list.at(index)->pinned()) {
323 m_list.at(index)->setPinned(false);
324 QModelIndex modelIndex = this->index(index);
325 Q_EMIT dataChanged(modelIndex, modelIndex, {RolePinned});
326 }
327 } else {
328 beginRemoveRows(QModelIndex(), index, index);
329 m_list.takeAt(index)->deleteLater();
330 endRemoveRows();
331 }
334int LauncherModel::findApplication(const QString &appId)
336 for (int i = 0; i < m_list.count(); ++i) {
337 LauncherItem *item = m_list.at(i);
338 if (item->appId() == appId) {
339 return i;
340 }
341 }
342 return -1;
345void LauncherModel::progressChanged(const QString &appId, int progress)
347 const int idx = findApplication(appId);
348 if (idx >= 0) {
349 LauncherItem *item = m_list.at(idx);
350 item->setProgress(progress);
351 Q_EMIT dataChanged(index(idx), index(idx), {RoleProgress});
352 }
355void LauncherModel::countChanged(const QString &appId, int count)
357 const int idx = findApplication(appId);
358 if (idx >= 0) {
359 LauncherItem *item = m_list.at(idx);
360 item->setCount(count);
361 if (item->countVisible()) {
362 setAlerting(item->appId(), true);
363 }
364 m_asAdapter->syncItems(m_list);
365 Q_EMIT dataChanged(index(idx), index(idx), {RoleCount});
366 }
369void LauncherModel::countVisibleChanged(const QString &appId, bool countVisible)
371 int idx = findApplication(appId);
372 if (idx >= 0) {
373 LauncherItem *item = m_list.at(idx);
374 item->setCountVisible(countVisible);
375 if (countVisible) {
376 setAlerting(item->appId(), true);
377 }
378 Q_EMIT dataChanged(index(idx), index(idx), {RoleCountVisible});
380 // If countVisible goes to false, and the item is neither pinned nor recent we can drop it
381 if (!countVisible && !item->pinned() && !item->recent()) {
382 beginRemoveRows(QModelIndex(), idx, idx);
383 m_list.takeAt(idx)->deleteLater();
384 endRemoveRows();
385 }
386 } else {
387 // Need to create a new LauncherItem and show the highlight
388 DesktopFileHandler desktopFile(appId);
389 if (countVisible && desktopFile.isValid()) {
390 LauncherItem *item = new LauncherItem(appId,
391 desktopFile.displayName(),
392 desktopFile.icon());
393 item->setCountVisible(true);
394 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
395 m_list.append(item);
396 endInsertRows();
397 }
398 }
399 m_asAdapter->syncItems(m_list);
402void LauncherModel::refresh()
404 // First walk through all the existing items and see if we need to remove something
405 QList<LauncherItem*> toBeRemoved;
406 Q_FOREACH (LauncherItem* item, m_list) {
407 DesktopFileHandler desktopFile(item->appId());
408 if (!desktopFile.isValid()) {
409 // Desktop file not available for this app => drop it!
410 toBeRemoved << item;
411 } else if (!m_settings->storedApplications().contains(item->appId())) {
412 // Item not in settings any more => drop it!
413 toBeRemoved << item;
414 } else {
415 int idx = m_list.indexOf(item);
416 item->setName(desktopFile.displayName());
417 item->setIcon(desktopFile.icon());
418 item->setPinned(item->pinned()); // update pinned text if needed
419 item->setRunning(item->running());
420 Q_EMIT dataChanged(index(idx), index(idx), {RoleName, RoleIcon, RoleRunning});
421 }
422 }
424 Q_FOREACH (LauncherItem* item, toBeRemoved) {
425 unpin(item->appId());
426 }
428 bool changed = toBeRemoved.count() > 0;
430 // This brings the Launcher into sync with the settings backend again. There's an issue though:
431 // If we can't find a .desktop file for an entry we need to skip it. That makes our settingsIndex
432 // go out of sync with the actual index of items. So let's also use an addedIndex which reflects
433 // the settingsIndex minus the skipped items.
434 int addedIndex = 0;
436 // Now walk through settings and see if we need to add something
437 for (int settingsIndex = 0; settingsIndex < m_settings->storedApplications().count(); ++settingsIndex) {
438 const QString entry = m_settings->storedApplications().at(settingsIndex);
439 int itemIndex = -1;
440 for (int i = 0; i < m_list.count(); ++i) {
441 if (m_list.at(i)->appId() == entry) {
442 itemIndex = i;
443 break;
444 }
445 }
447 if (itemIndex == -1) {
448 // Need to add it. Just add it into the addedIndex to keep same ordering as the list
449 // in the settings.
450 DesktopFileHandler desktopFile(entry);
451 if (!desktopFile.isValid()) {
452 qWarning() << "Couldn't find a .desktop file for" << entry << ". Skipping...";
453 continue;
454 }
456 LauncherItem *item = new LauncherItem(entry,
457 desktopFile.displayName(),
458 desktopFile.icon(),
459 this);
460 item->setPinned(true);
461 beginInsertRows(QModelIndex(), addedIndex, addedIndex);
462 m_list.insert(addedIndex, item);
463 endInsertRows();
464 changed = true;
465 } else if (itemIndex != addedIndex) {
466 // The item is already there, but it is in a different place than in the settings.
467 // Move it to the addedIndex
468 beginMoveRows(QModelIndex(), itemIndex, itemIndex, QModelIndex(), addedIndex);
469 m_list.move(itemIndex, addedIndex);
470 endMoveRows();
471 changed = true;
472 }
474 // Just like settingsIndex, this will increase with every item, except the ones we
475 // skipped with the "continue" call above.
476 addedIndex++;
477 }
479 if (changed) {
480 Q_EMIT hint();
481 }
483 m_asAdapter->syncItems(m_list);
486void LauncherModel::alert(const QString &appId)
488 int idx = findApplication(appId);
489 if (idx >= 0) {
490 LauncherItem *item = m_list.at(idx);
491 setAlerting(item->appId(), true);
492 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleAlerting);
493 }
496void LauncherModel::applicationAdded(const QModelIndex &parent, int row)
498 Q_UNUSED(parent);
500 ApplicationInfoInterface *app = m_appManager->get(row);
501 if (!app) {
502 qWarning() << "LauncherModel received an applicationAdded signal, but there's no such application!";
503 return;
504 }
506 if (app->appId() == QLatin1String("unity8-dash")) {
507 // Not adding the dash app
508 return;
509 }
511 const int itemIndex = findApplication(app->appId());
512 if (itemIndex != -1) {
513 LauncherItem *item = m_list.at(itemIndex);
514 if (!item->recent()) {
515 item->setRecent(true);
516 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), {RoleRecent});
517 }
518 item->setRunning(true);
519 } else {
520 LauncherItem *item = new LauncherItem(app->appId(), app->name(), app->icon().toString(), this);
521 item->setRecent(true);
522 item->setRunning(true);
523 item->setFocused(app->focused());
525 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
526 m_list.append(item);
527 endInsertRows();
528 }
529 m_asAdapter->syncItems(m_list);
530 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), {RoleRunning});
533void LauncherModel::applicationRemoved(const QModelIndex &parent, int row)
535 Q_UNUSED(parent)
537 int appIndex = -1;
538 for (int i = 0; i < m_list.count(); ++i) {
539 if (m_list.at(i)->appId() == m_appManager->get(row)->appId()) {
540 appIndex = i;
541 break;
542 }
543 }
545 if (appIndex < 0) {
546 qWarning() << Q_FUNC_INFO << "appIndex not found";
547 return;
548 }
550 LauncherItem * item = m_list.at(appIndex);
551 item->setRunning(false);
553 if (!item->pinned()) {
554 beginRemoveRows(QModelIndex(), appIndex, appIndex);
555 m_list.takeAt(appIndex)->deleteLater();
556 endRemoveRows();
557 m_asAdapter->syncItems(m_list);
558 Q_EMIT dataChanged(index(appIndex), index(appIndex), {RolePinned});
559 }
560 Q_EMIT dataChanged(index(appIndex), index(appIndex), {RoleRunning});
563void LauncherModel::focusedAppIdChanged()
565 const QString appId = m_appManager->focusedApplicationId();
566 for (int i = 0; i < m_list.count(); ++i) {
567 LauncherItem *item = m_list.at(i);
568 if (!item->focused() && item->appId() == appId) {
569 item->setFocused(true);
570 Q_EMIT dataChanged(index(i), index(i), {RoleFocused});
571 } else if (item->focused() && item->appId() != appId) {
572 item->setFocused(false);
573 Q_EMIT dataChanged(index(i), index(i), {RoleFocused});
574 }
575 }