on f38cc38 help sign in
unity8/plugins/Unity/Launcher/launchermodel.cpp
539 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);
45 connect(m_settings, &GSettings::changed, this, &LauncherModel::refresh);
47 refresh();
50LauncherModel::~LauncherModel()
52 while (!m_list.empty()) {
53 m_list.takeFirst()->deleteLater();
54 }
56 delete m_asAdapter;
59int LauncherModel::rowCount(const QModelIndex &parent) const
61 Q_UNUSED(parent)
62 return m_list.count();
65QVariant LauncherModel::data(const QModelIndex &index, int role) const
67 LauncherItem *item = m_list.at(index.row());
68 switch(role) {
69 case RoleAppId:
70 return item->appId();
71 case RoleName:
72 return item->name();
73 case RoleIcon:
74 return item->icon();
75 case RolePinned:
76 return item->pinned();
77 case RoleCount:
78 return item->count();
79 case RoleCountVisible:
80 return item->countVisible();
81 case RoleProgress:
82 return item->progress();
83 case RoleFocused:
84 return item->focused();
85 case RoleAlerting:
86 return item->alerting();
87 }
89 return QVariant();
92void LauncherModel::alert(const QString &appId, bool alerting) {
93 int index = findApplication(appId);
94 if (index >= 0) {
95 QModelIndex modelIndex = this->index(index);
96 LauncherItem *item = m_list.at(index);
97 if (!item->focused()) {
98 item->setAlerting(alerting);
99 Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RoleAlerting);
100 }
101 }
104unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
106 if (index < 0 || index >= m_list.count()) {
107 return 0;
108 }
109 return m_list.at(index);
112void LauncherModel::move(int oldIndex, int newIndex)
114 // Make sure its not moved outside the lists
115 if (newIndex < 0) {
116 newIndex = 0;
117 }
118 if (newIndex >= m_list.count()) {
119 newIndex = m_list.count()-1;
120 }
122 // Nothing to do?
123 if (oldIndex == newIndex) {
124 return;
125 }
127 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
128 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
129 // adjust the model's index by +1 in case we're moving upwards
130 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
132 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
133 m_list.move(oldIndex, newIndex);
134 endMoveRows();
136 if (!m_list.at(newIndex)->pinned()) {
137 pin(m_list.at(newIndex)->appId());
138 } else {
139 storeAppList();
140 }
143void LauncherModel::pin(const QString &appId, int index)
145 int currentIndex = findApplication(appId);
147 if (currentIndex >= 0) {
148 if (index == -1 || index == currentIndex) {
149 m_list.at(currentIndex)->setPinned(true);
150 QModelIndex modelIndex = this->index(currentIndex);
151 Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RolePinned);
152 } else {
153 move(currentIndex, index);
154 // move() will store the list to the backend itself, so just exit at this point.
155 return;
156 }
157 } else {
158 if (index == -1) {
159 index = m_list.count();
160 }
162 DesktopFileHandler desktopFile(appId);
163 if (!desktopFile.isValid()) {
164 qWarning() << "Can't pin this application, there is no .destkop file available.";
165 return;
166 }
168 beginInsertRows(QModelIndex(), index, index);
169 LauncherItem *item = new LauncherItem(appId,
170 desktopFile.displayName(),
171 desktopFile.icon(),
172 this);
173 item->setPinned(true);
174 m_list.insert(index, item);
175 endInsertRows();
176 }
178 storeAppList();
181void LauncherModel::requestRemove(const QString &appId)
183 unpin(appId);
184 storeAppList();
187void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
189 int index = findApplication(appId);
190 if (index < 0) {
191 return;
192 }
194 LauncherItem *item = m_list.at(index);
195 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
196 if (model) {
197 QString actionId = model->get(actionIndex).actionId();
199 // Check if this is one of the launcher actions we handle ourselves
200 if (actionId == "pin_item") {
201 if (item->pinned()) {
202 requestRemove(appId);
203 } else {
204 pin(appId);
205 }
206 } else if (actionId == "launch_item") {
207 QDesktopServices::openUrl(getUrlForAppId(appId));
209 // Nope, we don't know this action, let the backend forward it to the application
210 } else {
211 // TODO: forward quicklist action to app, possibly via m_dbusIface
212 }
213 }
216void LauncherModel::setUser(const QString &username)
218 Q_UNUSED(username)
219 qWarning() << "This backend doesn't support multiple users";
222QString LauncherModel::getUrlForAppId(const QString &appId) const
224 // appId is either an appId or a legacy app name. Let's find out which
225 if (appId.isEmpty()) {
226 return QString();
227 }
229 if (!appId.contains("_")) {
230 return "application:///" + appId + ".desktop";
231 }
233 QStringList parts = appId.split('_');
234 QString package = parts.value(0);
235 QString app = parts.value(1, "first-listed-app");
236 return "appid://" + package + "/" + app + "/current-user-version";
239ApplicationManagerInterface *LauncherModel::applicationManager() const
241 return m_appManager;
244void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
246 // Is there already another appmanager set?
247 if (m_appManager) {
248 // Disconnect any signals
249 disconnect(this, SLOT(applicationAdded(QModelIndex,int)));
250 disconnect(this, SLOT(applicationRemoved(QModelIndex,int)));
251 disconnect(this, SLOT(focusedAppIdChanged()));
253 // remove any recent/running apps from the launcher
254 QList<int> recentAppIndices;
255 for (int i = 0; i < m_list.count(); ++i) {
256 if (m_list.at(i)->recent()) {
257 recentAppIndices << i;
258 }
259 }
260 int run = 0;
261 while (recentAppIndices.count() > 0) {
262 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
263 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
264 endRemoveRows();
265 recentAppIndices.takeFirst();
266 ++run;
267 }
268 }
270 m_appManager = appManager;
271 connect(m_appManager, SIGNAL(rowsInserted(QModelIndex, int, int)), SLOT(applicationAdded(QModelIndex,int)));
272 connect(m_appManager, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), SLOT(applicationRemoved(QModelIndex,int)));
273 connect(m_appManager, SIGNAL(focusedApplicationIdChanged()), SLOT(focusedAppIdChanged()));
275 Q_EMIT applicationManagerChanged();
277 for (int i = 0; i < appManager->count(); ++i) {
278 applicationAdded(QModelIndex(), i);
279 }
282bool LauncherModel::onlyPinned() const
284 return false;
287void LauncherModel::setOnlyPinned(bool onlyPinned) {
288 Q_UNUSED(onlyPinned);
289 qWarning() << "This launcher implementation does not support showing only pinned apps";
292void LauncherModel::storeAppList()
294 QStringList appIds;
295 Q_FOREACH(LauncherItem *item, m_list) {
296 if (item->pinned()) {
297 appIds << item->appId();
298 }
299 }
300 m_settings->setStoredApplications(appIds);
301 m_asAdapter->syncItems(m_list);
304void LauncherModel::unpin(const QString &appId)
306 int index = findApplication(appId);
307 if (index < 0) {
308 return;
309 }
311 if (m_appManager->findApplication(appId)) {
312 if (m_list.at(index)->pinned()) {
313 m_list.at(index)->setPinned(false);
314 QModelIndex modelIndex = this->index(index);
315 Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RolePinned);
316 }
317 } else {
318 beginRemoveRows(QModelIndex(), index, index);
319 m_list.takeAt(index)->deleteLater();
320 endRemoveRows();
321 }
324int LauncherModel::findApplication(const QString &appId)
326 for (int i = 0; i < m_list.count(); ++i) {
327 LauncherItem *item = m_list.at(i);
328 if (item->appId() == appId) {
329 return i;
330 }
331 }
332 return -1;
335void LauncherModel::progressChanged(const QString &appId, int progress)
337 int idx = findApplication(appId);
338 if (idx >= 0) {
339 LauncherItem *item = m_list.at(idx);
340 item->setProgress(progress);
341 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleProgress);
342 }
345void LauncherModel::countChanged(const QString &appId, int count)
347 int idx = findApplication(appId);
348 if (idx >= 0) {
349 LauncherItem *item = m_list.at(idx);
350 item->setCount(count);
351 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleCount);
352 m_asAdapter->syncItems(m_list);
353 }
356void LauncherModel::countVisibleChanged(const QString &appId, int countVisible)
358 int idx = findApplication(appId);
359 if (idx >= 0) {
360 LauncherItem *item = m_list.at(idx);
361 item->setCountVisible(countVisible);
362 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleCountVisible);
364 // If countVisible goes to false, and the item is neither pinned nor recent we can drop it
365 if (!countVisible && !item->pinned() && !item->recent()) {
366 beginRemoveRows(QModelIndex(), idx, idx);
367 m_list.takeAt(idx)->deleteLater();
368 endRemoveRows();
369 }
370 } else {
371 // Need to create a new LauncherItem and show the highlight
372 DesktopFileHandler desktopFile(appId);
373 if (countVisible && desktopFile.isValid()) {
374 LauncherItem *item = new LauncherItem(appId,
375 desktopFile.displayName(),
376 desktopFile.icon());
377 item->setCountVisible(true);
378 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
379 m_list.append(item);
380 endInsertRows();
381 Q_EMIT hint();
382 }
383 }
384 m_asAdapter->syncItems(m_list);
387void LauncherModel::refresh()
389 // First walk through all the existing items and see if we need to remove something
390 QList<LauncherItem*> toBeRemoved;
391 Q_FOREACH (LauncherItem* item, m_list) {
392 DesktopFileHandler desktopFile(item->appId());
393 if (!desktopFile.isValid()) {
394 // Desktop file not available for this app => drop it!
395 toBeRemoved << item;
396 } else if (!m_settings->storedApplications().contains(item->appId())) {
397 // Item not in settings any more => drop it!
398 toBeRemoved << item;
399 } else {
400 int idx = m_list.indexOf(item);
401 item->setName(desktopFile.displayName());
402 item->setIcon(desktopFile.icon());
403 item->setPinned(item->pinned()); // update pinned text if needed
404 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleName << RoleIcon);
405 }
406 }
408 Q_FOREACH (LauncherItem* item, toBeRemoved) {
409 unpin(item->appId());
410 }
412 bool changed = toBeRemoved.count() > 0;
414 // This brings the Launcher into sync with the settings backend again. There's an issue though:
415 // If we can't find a .desktop file for an entry we need to skip it. That makes our settingsIndex
416 // go out of sync with the actual index of items. So let's also use an addedIndex which reflects
417 // the settingsIndex minus the skipped items.
418 int addedIndex = 0;
420 // Now walk through settings and see if we need to add something
421 for (int settingsIndex = 0; settingsIndex < m_settings->storedApplications().count(); ++settingsIndex) {
422 QString entry = m_settings->storedApplications().at(settingsIndex);
423 int itemIndex = -1;
424 for (int i = 0; i < m_list.count(); ++i) {
425 if (m_list.at(i)->appId() == entry) {
426 itemIndex = i;
427 break;
428 }
429 }
431 if (itemIndex == -1) {
432 // Need to add it. Just add it into the addedIndex to keep same ordering as the list
433 // in the settings.
434 DesktopFileHandler desktopFile(entry);
435 if (!desktopFile.isValid()) {
436 qWarning() << "Couldn't find a .desktop file for" << entry << ". Skipping...";
437 continue;
438 }
440 LauncherItem *item = new LauncherItem(entry,
441 desktopFile.displayName(),
442 desktopFile.icon(),
443 this);
444 item->setPinned(true);
445 beginInsertRows(QModelIndex(), addedIndex, addedIndex);
446 m_list.insert(addedIndex, item);
447 endInsertRows();
448 changed = true;
449 } else if (itemIndex != addedIndex) {
450 // The item is already there, but it is in a different place than in the settings.
451 // Move it to the addedIndex
452 beginMoveRows(QModelIndex(), itemIndex, itemIndex, QModelIndex(), addedIndex);
453 m_list.move(itemIndex, addedIndex);
454 endMoveRows();
455 changed = true;
456 }
458 // Just like settingsIndex, this will increase with every item, except the ones we
459 // skipped with the "continue" call above.
460 addedIndex++;
461 }
463 if (changed) {
464 Q_EMIT hint();
465 }
467 m_asAdapter->syncItems(m_list);
470void LauncherModel::applicationAdded(const QModelIndex &parent, int row)
472 Q_UNUSED(parent);
474 ApplicationInfoInterface *app = m_appManager->get(row);
475 if (!app) {
476 qWarning() << "LauncherModel received an applicationAdded signal, but there's no such application!";
477 return;
478 }
480 if (app->appId() == "unity8-dash") {
481 // Not adding the dash app
482 return;
483 }
485 int itemIndex = findApplication(app->appId());
486 if (itemIndex != -1) {
487 LauncherItem *item = m_list.at(itemIndex);
488 if (!item->recent()) {
489 item->setRecent(true);
490 m_asAdapter->syncItems(m_list);
491 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), QVector<int>() << RoleRecent);
492 }
493 // Shall we paint some running/recent app highlight? If yes, do it here.
494 } else {
495 LauncherItem *item = new LauncherItem(app->appId(), app->name(), app->icon().toString(), this);
496 item->setRecent(true);
497 item->setFocused(app->focused());
499 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
500 m_list.append(item);
501 endInsertRows();
502 m_asAdapter->syncItems(m_list);
503 }
506void LauncherModel::applicationRemoved(const QModelIndex &parent, int row)
508 Q_UNUSED(parent)
510 int appIndex = -1;
511 for (int i = 0; i < m_list.count(); ++i) {
512 if (m_list.at(i)->appId() == m_appManager->get(row)->appId()) {
513 appIndex = i;
514 break;
515 }
516 }
518 if (appIndex > -1 && !m_list.at(appIndex)->pinned()) {
519 beginRemoveRows(QModelIndex(), appIndex, appIndex);
520 m_list.takeAt(appIndex)->deleteLater();
521 endRemoveRows();
522 m_asAdapter->syncItems(m_list);
523 }
526void LauncherModel::focusedAppIdChanged()
528 QString appId = m_appManager->focusedApplicationId();
529 for (int i = 0; i < m_list.count(); ++i) {
530 LauncherItem *item = m_list.at(i);
531 if (!item->focused() && item->appId() == appId) {
532 item->setFocused(true);
533 Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);
534 } else if (item->focused() && item->appId() != appId) {
535 item->setFocused(false);
536 Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);
537 }
538 }