on 36f2dcd help sign in
unity8/plugins/Unity/Launcher/launchermodel.cpp
499 lines 16 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"
26#include <unity/shell/application/ApplicationInfoInterface.h>
28#include <QDesktopServices>
29#include <QDebug>
31using namespace unity::shell::application;
33LauncherModel::LauncherModel(QObject *parent):
34 LauncherModelInterface(parent),
35 m_settings(new GSettings(this)),
36 m_dbusIface(new DBusInterface(this)),
37 m_appManager(0)
39 connect(m_dbusIface, &DBusInterface::countChanged, this, &LauncherModel::countChanged);
40 connect(m_dbusIface, &DBusInterface::countVisibleChanged, this, &LauncherModel::countVisibleChanged);
41 connect(m_dbusIface, &DBusInterface::refreshCalled, this, &LauncherModel::refresh);
43 connect(m_settings, &GSettings::changed, this, &LauncherModel::refresh);
45 refresh();
48LauncherModel::~LauncherModel()
50 while (!m_list.empty()) {
51 m_list.takeFirst()->deleteLater();
52 }
55int LauncherModel::rowCount(const QModelIndex &parent) const
57 Q_UNUSED(parent)
58 return m_list.count();
61QVariant LauncherModel::data(const QModelIndex &index, int role) const
63 LauncherItem *item = m_list.at(index.row());
64 switch(role) {
65 case RoleAppId:
66 return item->appId();
67 case RoleName:
68 return item->name();
69 case RoleIcon:
70 return item->icon();
71 case RolePinned:
72 return item->pinned();
73 case RoleCount:
74 return item->count();
75 case RoleCountVisible:
76 return item->countVisible();
77 case RoleProgress:
78 return item->progress();
79 case RoleFocused:
80 return item->focused();
81 }
83 return QVariant();
86unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
88 if (index < 0 || index >= m_list.count()) {
89 return 0;
90 }
91 return m_list.at(index);
94void LauncherModel::move(int oldIndex, int newIndex)
96 // Make sure its not moved outside the lists
97 if (newIndex < 0) {
98 newIndex = 0;
99 }
100 if (newIndex >= m_list.count()) {
101 newIndex = m_list.count()-1;
102 }
104 // Nothing to do?
105 if (oldIndex == newIndex) {
106 return;
107 }
109 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
110 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
111 // adjust the model's index by +1 in case we're moving upwards
112 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
114 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
115 m_list.move(oldIndex, newIndex);
116 endMoveRows();
118 if (!m_list.at(newIndex)->pinned()) {
119 pin(m_list.at(newIndex)->appId());
120 } else {
121 storeAppList();
122 }
125void LauncherModel::pin(const QString &appId, int index)
127 int currentIndex = findApplication(appId);
129 if (currentIndex >= 0) {
130 if (index == -1 || index == currentIndex) {
131 m_list.at(currentIndex)->setPinned(true);
132 QModelIndex modelIndex = this->index(currentIndex);
133 Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RolePinned);
134 } else {
135 move(currentIndex, index);
136 // move() will store the list to the backend itself, so just exit at this point.
137 return;
138 }
139 } else {
140 if (index == -1) {
141 index = m_list.count();
142 }
144 DesktopFileHandler desktopFile(appId);
145 if (!desktopFile.isValid()) {
146 qWarning() << "Can't pin this application, there is no .destkop file available.";
147 return;
148 }
150 beginInsertRows(QModelIndex(), index, index);
151 LauncherItem *item = new LauncherItem(appId,
152 desktopFile.displayName(),
153 desktopFile.icon(),
154 this);
155 item->setPinned(true);
156 m_list.insert(index, item);
157 endInsertRows();
158 }
160 storeAppList();
163void LauncherModel::requestRemove(const QString &appId)
165 int index = findApplication(appId);
166 if (index < 0) {
167 return;
168 }
170 if (m_appManager->findApplication(appId)) {
171 m_list.at(index)->setPinned(false);
172 QModelIndex modelIndex = this->index(index);
173 Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RolePinned);
174 return;
175 }
177 beginRemoveRows(QModelIndex(), index, index);
178 m_list.takeAt(index)->deleteLater();
179 endRemoveRows();
181 storeAppList();
184void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
186 int index = findApplication(appId);
187 if (index < 0) {
188 return;
189 }
191 LauncherItem *item = m_list.at(index);
192 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
193 if (model) {
194 QString actionId = model->get(actionIndex).actionId();
196 // Check if this is one of the launcher actions we handle ourselves
197 if (actionId == "pin_item") {
198 if (item->pinned()) {
199 requestRemove(appId);
200 } else {
201 pin(appId);
202 }
203 } else if (actionId == "launch_item") {
204 QDesktopServices::openUrl(getUrlForAppId(appId));
206 // Nope, we don't know this action, let the backend forward it to the application
207 } else {
208 // TODO: forward quicklist action to app, possibly via m_dbusIface
209 }
210 }
213void LauncherModel::setUser(const QString &username)
215 Q_UNUSED(username)
216 qWarning() << "This backend doesn't support multiple users";
219QString LauncherModel::getUrlForAppId(const QString &appId) const
221 // appId is either an appId or a legacy app name. Let's find out which
222 if (appId.isEmpty()) {
223 return QString();
224 }
226 if (!appId.contains("_")) {
227 return "application:///" + appId + ".desktop";
228 }
230 QStringList parts = appId.split('_');
231 QString package = parts.value(0);
232 QString app = parts.value(1, "first-listed-app");
233 return "appid://" + package + "/" + app + "/current-user-version";
236ApplicationManagerInterface *LauncherModel::applicationManager() const
238 return m_appManager;
241void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
243 // Is there already another appmanager set?
244 if (m_appManager) {
245 // Disconnect any signals
246 disconnect(this, SLOT(applicationAdded(QModelIndex,int)));
247 disconnect(this, SLOT(applicationRemoved(QModelIndex,int)));
248 disconnect(this, SLOT(focusedAppIdChanged()));
250 // remove any recent/running apps from the launcher
251 QList<int> recentAppIndices;
252 for (int i = 0; i < m_list.count(); ++i) {
253 if (m_list.at(i)->recent()) {
254 recentAppIndices << i;
255 }
256 }
257 int run = 0;
258 while (recentAppIndices.count() > 0) {
259 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
260 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
261 endRemoveRows();
262 recentAppIndices.takeFirst();
263 ++run;
264 }
265 }
267 m_appManager = appManager;
268 connect(m_appManager, SIGNAL(rowsInserted(QModelIndex, int, int)), SLOT(applicationAdded(QModelIndex,int)));
269 connect(m_appManager, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), SLOT(applicationRemoved(QModelIndex,int)));
270 connect(m_appManager, SIGNAL(focusedApplicationIdChanged()), SLOT(focusedAppIdChanged()));
272 Q_EMIT applicationManagerChanged();
274 for (int i = 0; i < appManager->count(); ++i) {
275 applicationAdded(QModelIndex(), i);
276 }
280void LauncherModel::storeAppList()
282 QStringList appIds;
283 Q_FOREACH(LauncherItem *item, m_list) {
284 if (item->pinned()) {
285 appIds << item->appId();
286 }
287 }
288 m_settings->setStoredApplications(appIds);
291int LauncherModel::findApplication(const QString &appId)
293 for (int i = 0; i < m_list.count(); ++i) {
294 LauncherItem *item = m_list.at(i);
295 if (item->appId() == appId) {
296 return i;
297 }
298 }
299 return -1;
302void LauncherModel::progressChanged(const QString &appId, int progress)
304 int idx = findApplication(appId);
305 if (idx >= 0) {
306 LauncherItem *item = m_list.at(idx);
307 item->setProgress(progress);
308 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleProgress);
309 }
312void LauncherModel::countChanged(const QString &appId, int count)
314 int idx = findApplication(appId);
315 if (idx >= 0) {
316 LauncherItem *item = m_list.at(idx);
317 item->setCount(count);
318 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleCount);
319 }
322void LauncherModel::countVisibleChanged(const QString &appId, int countVisible)
324 int idx = findApplication(appId);
325 if (idx >= 0) {
326 LauncherItem *item = m_list.at(idx);
327 item->setCountVisible(countVisible);
328 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleCountVisible);
330 // If countVisible goes to false, and the item is neither pinned nor recent we can drop it
331 if (!countVisible && !item->pinned() && !item->recent()) {
332 beginRemoveRows(QModelIndex(), idx, idx);
333 m_list.takeAt(idx)->deleteLater();
334 endRemoveRows();
335 }
336 } else {
337 // Need to create a new LauncherItem and show the highlight
338 DesktopFileHandler desktopFile(appId);
339 if (countVisible && desktopFile.isValid()) {
340 LauncherItem *item = new LauncherItem(appId,
341 desktopFile.displayName(),
342 desktopFile.icon());
343 item->setCountVisible(true);
344 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
345 m_list.append(item);
346 endInsertRows();
347 Q_EMIT hintChanged();
348 }
349 }
352void LauncherModel::refresh()
354 // First walk through all the existing items and see if we need to remove something
355 QList<LauncherItem*> toBeRemoved;
356 Q_FOREACH (LauncherItem* item, m_list) {
357 DesktopFileHandler desktopFile(item->appId());
358 if (!desktopFile.isValid()) {
359 // Desktop file not available for this app => drop it!
360 toBeRemoved << item;
361 } else if (!m_settings->storedApplications().contains(item->appId())) {
362 // Item not in settings any more => drop it!
363 toBeRemoved << item;
364 } else {
365 int idx = m_list.indexOf(item);
366 item->setName(desktopFile.displayName());
367 item->setIcon(desktopFile.icon());
368 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleName << RoleIcon);
369 }
370 }
372 Q_FOREACH (LauncherItem* item, toBeRemoved) {
373 requestRemove(item->appId());
374 }
376 bool changed = toBeRemoved.count() > 0;
378 // This brings the Launcher into sync with the settings backend again. There's an issue though:
379 // If we can't find a .desktop file for an entry we need to skip it. That makes our settingsIndex
380 // go out of sync with the actual index of items. So let's also use an addedIndex which reflects
381 // the settingsIndex minus the skipped items.
382 int addedIndex = 0;
384 // Now walk through settings and see if we need to add something
385 for (int settingsIndex = 0; settingsIndex < m_settings->storedApplications().count(); ++settingsIndex) {
386 QString entry = m_settings->storedApplications().at(settingsIndex);
387 int itemIndex = -1;
388 for (int i = 0; i < m_list.count(); ++i) {
389 if (m_list.at(i)->appId() == entry) {
390 itemIndex = i;
391 break;
392 }
393 }
395 if (itemIndex == -1) {
396 // Need to add it. Just add it into the addedIndex to keep same ordering as the list
397 // in the settings.
398 DesktopFileHandler desktopFile(entry);
399 if (!desktopFile.isValid()) {
400 qWarning() << "Couldn't find a .desktop file for" << entry << ". Skipping...";
401 continue;
402 }
404 LauncherItem *item = new LauncherItem(entry,
405 desktopFile.displayName(),
406 desktopFile.icon(),
407 this);
408 item->setPinned(true);
409 beginInsertRows(QModelIndex(), addedIndex, addedIndex);
410 m_list.insert(addedIndex, item);
411 endInsertRows();
412 changed = true;
413 } else if (itemIndex != addedIndex) {
414 // The item is already there, but it is in a different place than in the settings.
415 // Move it to the addedIndex
416 beginMoveRows(QModelIndex(), itemIndex, itemIndex, QModelIndex(), addedIndex);
417 m_list.move(itemIndex, addedIndex);
418 endMoveRows();
419 changed = true;
420 }
422 // Just like settingsIndex, this will increase with every item, except the ones we
423 // skipped with the "continue" call above.
424 addedIndex++;
425 }
427 if (changed) {
428 Q_EMIT hintChanged();
429 }
432void LauncherModel::applicationAdded(const QModelIndex &parent, int row)
434 Q_UNUSED(parent);
436 ApplicationInfoInterface *app = m_appManager->get(row);
437 if (!app) {
438 qWarning() << "LauncherModel received an applicationAdded signal, but there's no such application!";
439 return;
440 }
442 if (app->appId() == "unity8-dash") {
443 // Not adding the dash app
444 return;
445 }
447 bool found = false;
448 Q_FOREACH(LauncherItem *item, m_list) {
449 if (app->appId() == item->appId()) {
450 found = true;
451 break;
452 }
453 }
454 if (found) {
455 // Shall we paint some running/recent app highlight? If yes, do it here.
456 } else {
457 LauncherItem *item = new LauncherItem(app->appId(), app->name(), app->icon().toString(), this);
458 item->setRecent(true);
459 item->setFocused(app->focused());
461 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
462 m_list.append(item);
463 endInsertRows();
464 }
467void LauncherModel::applicationRemoved(const QModelIndex &parent, int row)
469 Q_UNUSED(parent)
471 int appIndex = -1;
472 for (int i = 0; i < m_list.count(); ++i) {
473 if (m_list.at(i)->appId() == m_appManager->get(row)->appId()) {
474 appIndex = i;
475 break;
476 }
477 }
479 if (appIndex > -1 && !m_list.at(appIndex)->pinned()) {
480 beginRemoveRows(QModelIndex(), appIndex, appIndex);
481 m_list.takeAt(appIndex)->deleteLater();
482 endRemoveRows();
483 }
486void LauncherModel::focusedAppIdChanged()
488 QString appId = m_appManager->focusedApplicationId();
489 for (int i = 0; i < m_list.count(); ++i) {
490 LauncherItem *item = m_list.at(i);
491 if (!item->focused() && item->appId() == appId) {
492 item->setFocused(true);
493 Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);
494 } else if (item->focused() && item->appId() != appId) {
495 item->setFocused(false);
496 Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);
497 }
498 }