on 5aef858 help sign in
unity8/plugins/Unity/Launcher/launchermodel.cpp
518 lines 17 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 qDebug() << "Loading dconf based launcher model";
42 connect(m_dbusIface, &DBusInterface::countChanged, this, &LauncherModel::countChanged);
43 connect(m_dbusIface, &DBusInterface::countVisibleChanged, this, &LauncherModel::countVisibleChanged);
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 }
88 return QVariant();
91unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
93 if (index < 0 || index >= m_list.count()) {
94 return 0;
95 }
96 return m_list.at(index);
99void LauncherModel::move(int oldIndex, int newIndex)
101 // Make sure its not moved outside the lists
102 if (newIndex < 0) {
103 newIndex = 0;
104 }
105 if (newIndex >= m_list.count()) {
106 newIndex = m_list.count()-1;
107 }
109 // Nothing to do?
110 if (oldIndex == newIndex) {
111 return;
112 }
114 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
115 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
116 // adjust the model's index by +1 in case we're moving upwards
117 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
119 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
120 m_list.move(oldIndex, newIndex);
121 endMoveRows();
123 if (!m_list.at(newIndex)->pinned()) {
124 pin(m_list.at(newIndex)->appId());
125 } else {
126 storeAppList();
127 }
130void LauncherModel::pin(const QString &appId, int index)
132 int currentIndex = findApplication(appId);
134 if (currentIndex >= 0) {
135 if (index == -1 || index == currentIndex) {
136 m_list.at(currentIndex)->setPinned(true);
137 QModelIndex modelIndex = this->index(currentIndex);
138 Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RolePinned);
139 } else {
140 move(currentIndex, index);
141 // move() will store the list to the backend itself, so just exit at this point.
142 return;
143 }
144 } else {
145 if (index == -1) {
146 index = m_list.count();
147 }
149 DesktopFileHandler desktopFile(appId);
150 if (!desktopFile.isValid()) {
151 qWarning() << "Can't pin this application, there is no .destkop file available.";
152 return;
153 }
155 beginInsertRows(QModelIndex(), index, index);
156 LauncherItem *item = new LauncherItem(appId,
157 desktopFile.displayName(),
158 desktopFile.icon(),
159 this);
160 item->setPinned(true);
161 m_list.insert(index, item);
162 endInsertRows();
163 }
165 storeAppList();
168void LauncherModel::requestRemove(const QString &appId)
170 unpin(appId);
171 storeAppList();
174void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
176 int index = findApplication(appId);
177 if (index < 0) {
178 return;
179 }
181 LauncherItem *item = m_list.at(index);
182 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
183 if (model) {
184 QString actionId = model->get(actionIndex).actionId();
186 // Check if this is one of the launcher actions we handle ourselves
187 if (actionId == "pin_item") {
188 if (item->pinned()) {
189 requestRemove(appId);
190 } else {
191 pin(appId);
192 }
193 } else if (actionId == "launch_item") {
194 QDesktopServices::openUrl(getUrlForAppId(appId));
196 // Nope, we don't know this action, let the backend forward it to the application
197 } else {
198 // TODO: forward quicklist action to app, possibly via m_dbusIface
199 }
200 }
203void LauncherModel::setUser(const QString &username)
205 Q_UNUSED(username)
206 qWarning() << "This backend doesn't support multiple users";
209QString LauncherModel::getUrlForAppId(const QString &appId) const
211 // appId is either an appId or a legacy app name. Let's find out which
212 if (appId.isEmpty()) {
213 return QString();
214 }
216 if (!appId.contains("_")) {
217 return "application:///" + appId + ".desktop";
218 }
220 QStringList parts = appId.split('_');
221 QString package = parts.value(0);
222 QString app = parts.value(1, "first-listed-app");
223 return "appid://" + package + "/" + app + "/current-user-version";
226ApplicationManagerInterface *LauncherModel::applicationManager() const
228 return m_appManager;
231void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
233 // Is there already another appmanager set?
234 if (m_appManager) {
235 // Disconnect any signals
236 disconnect(this, SLOT(applicationAdded(QModelIndex,int)));
237 disconnect(this, SLOT(applicationRemoved(QModelIndex,int)));
238 disconnect(this, SLOT(focusedAppIdChanged()));
240 // remove any recent/running apps from the launcher
241 QList<int> recentAppIndices;
242 for (int i = 0; i < m_list.count(); ++i) {
243 if (m_list.at(i)->recent()) {
244 recentAppIndices << i;
245 }
246 }
247 int run = 0;
248 while (recentAppIndices.count() > 0) {
249 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
250 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
251 endRemoveRows();
252 recentAppIndices.takeFirst();
253 ++run;
254 }
255 }
257 m_appManager = appManager;
258 connect(m_appManager, SIGNAL(rowsInserted(QModelIndex, int, int)), SLOT(applicationAdded(QModelIndex,int)));
259 connect(m_appManager, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), SLOT(applicationRemoved(QModelIndex,int)));
260 connect(m_appManager, SIGNAL(focusedApplicationIdChanged()), SLOT(focusedAppIdChanged()));
262 Q_EMIT applicationManagerChanged();
264 for (int i = 0; i < appManager->count(); ++i) {
265 applicationAdded(QModelIndex(), i);
266 }
270void LauncherModel::storeAppList()
272 QStringList appIds;
273 Q_FOREACH(LauncherItem *item, m_list) {
274 if (item->pinned()) {
275 appIds << item->appId();
276 }
277 }
278 m_settings->setStoredApplications(appIds);
279 m_asAdapter->syncItems(m_list);
282void LauncherModel::unpin(const QString &appId)
284 int index = findApplication(appId);
285 if (index < 0) {
286 return;
287 }
289 if (m_appManager->findApplication(appId)) {
290 if (m_list.at(index)->pinned()) {
291 m_list.at(index)->setPinned(false);
292 QModelIndex modelIndex = this->index(index);
293 Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RolePinned);
294 }
295 } else {
296 beginRemoveRows(QModelIndex(), index, index);
297 m_list.takeAt(index)->deleteLater();
298 endRemoveRows();
299 }
302int LauncherModel::findApplication(const QString &appId)
304 for (int i = 0; i < m_list.count(); ++i) {
305 LauncherItem *item = m_list.at(i);
306 if (item->appId() == appId) {
307 return i;
308 }
309 }
310 return -1;
313void LauncherModel::progressChanged(const QString &appId, int progress)
315 int idx = findApplication(appId);
316 if (idx >= 0) {
317 LauncherItem *item = m_list.at(idx);
318 item->setProgress(progress);
319 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleProgress);
320 }
323void LauncherModel::countChanged(const QString &appId, int count)
325 int idx = findApplication(appId);
326 if (idx >= 0) {
327 LauncherItem *item = m_list.at(idx);
328 item->setCount(count);
329 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleCount);
330 m_asAdapter->syncItems(m_list);
331 }
334void LauncherModel::countVisibleChanged(const QString &appId, int countVisible)
336 int idx = findApplication(appId);
337 if (idx >= 0) {
338 LauncherItem *item = m_list.at(idx);
339 item->setCountVisible(countVisible);
340 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleCountVisible);
342 // If countVisible goes to false, and the item is neither pinned nor recent we can drop it
343 if (!countVisible && !item->pinned() && !item->recent()) {
344 beginRemoveRows(QModelIndex(), idx, idx);
345 m_list.takeAt(idx)->deleteLater();
346 endRemoveRows();
347 }
348 } else {
349 // Need to create a new LauncherItem and show the highlight
350 DesktopFileHandler desktopFile(appId);
351 if (countVisible && desktopFile.isValid()) {
352 LauncherItem *item = new LauncherItem(appId,
353 desktopFile.displayName(),
354 desktopFile.icon());
355 item->setCountVisible(true);
356 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
357 m_list.append(item);
358 endInsertRows();
359 Q_EMIT hint();
360 }
361 }
362 m_asAdapter->syncItems(m_list);
365void LauncherModel::refresh()
367 qDebug() << "refreshing";
368 // First walk through all the existing items and see if we need to remove something
369 QList<LauncherItem*> toBeRemoved;
370 Q_FOREACH (LauncherItem* item, m_list) {
371 DesktopFileHandler desktopFile(item->appId());
372 if (!desktopFile.isValid()) {
373 // Desktop file not available for this app => drop it!
374 toBeRemoved << item;
375 } else if (!m_settings->storedApplications().contains(item->appId())) {
376 // Item not in settings any more => drop it!
377 toBeRemoved << item;
378 } else {
379 int idx = m_list.indexOf(item);
380 item->setName(desktopFile.displayName());
381 item->setIcon(desktopFile.icon());
382 item->setPinned(item->pinned()); // update pinned text if needed
383 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleName << RoleIcon);
384 }
385 }
387 Q_FOREACH (LauncherItem* item, toBeRemoved) {
388 unpin(item->appId());
389 }
391 bool changed = toBeRemoved.count() > 0;
393 // This brings the Launcher into sync with the settings backend again. There's an issue though:
394 // If we can't find a .desktop file for an entry we need to skip it. That makes our settingsIndex
395 // go out of sync with the actual index of items. So let's also use an addedIndex which reflects
396 // the settingsIndex minus the skipped items.
397 int addedIndex = 0;
399 // Now walk through settings and see if we need to add something
400 for (int settingsIndex = 0; settingsIndex < m_settings->storedApplications().count(); ++settingsIndex) {
401 QString entry = m_settings->storedApplications().at(settingsIndex);
402 int itemIndex = -1;
403 for (int i = 0; i < m_list.count(); ++i) {
404 if (m_list.at(i)->appId() == entry) {
405 itemIndex = i;
406 break;
407 }
408 }
410 if (itemIndex == -1) {
411 // Need to add it. Just add it into the addedIndex to keep same ordering as the list
412 // in the settings.
413 DesktopFileHandler desktopFile(entry);
414 if (!desktopFile.isValid()) {
415 qWarning() << "Couldn't find a .desktop file for" << entry << ". Skipping...";
416 continue;
417 }
419 LauncherItem *item = new LauncherItem(entry,
420 desktopFile.displayName(),
421 desktopFile.icon(),
422 this);
423 item->setPinned(true);
424 beginInsertRows(QModelIndex(), addedIndex, addedIndex);
425 m_list.insert(addedIndex, item);
426 endInsertRows();
427 changed = true;
428 } else if (itemIndex != addedIndex) {
429 // The item is already there, but it is in a different place than in the settings.
430 // Move it to the addedIndex
431 beginMoveRows(QModelIndex(), itemIndex, itemIndex, QModelIndex(), addedIndex);
432 m_list.move(itemIndex, addedIndex);
433 endMoveRows();
434 changed = true;
435 }
437 // Just like settingsIndex, this will increase with every item, except the ones we
438 // skipped with the "continue" call above.
439 addedIndex++;
440 }
442 if (changed) {
443 Q_EMIT hint();
444 }
446 m_asAdapter->syncItems(m_list);
449void LauncherModel::applicationAdded(const QModelIndex &parent, int row)
451 Q_UNUSED(parent);
453 ApplicationInfoInterface *app = m_appManager->get(row);
454 if (!app) {
455 qWarning() << "LauncherModel received an applicationAdded signal, but there's no such application!";
456 return;
457 }
459 if (app->appId() == "unity8-dash") {
460 // Not adding the dash app
461 return;
462 }
464 bool found = false;
465 Q_FOREACH(LauncherItem *item, m_list) {
466 if (app->appId() == item->appId()) {
467 found = true;
468 break;
469 }
470 }
471 if (found) {
472 // Shall we paint some running/recent app highlight? If yes, do it here.
473 } else {
474 LauncherItem *item = new LauncherItem(app->appId(), app->name(), app->icon().toString(), this);
475 item->setRecent(true);
476 item->setFocused(app->focused());
478 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
479 m_list.append(item);
480 endInsertRows();
481 m_asAdapter->syncItems(m_list);
482 }
485void LauncherModel::applicationRemoved(const QModelIndex &parent, int row)
487 Q_UNUSED(parent)
489 int appIndex = -1;
490 for (int i = 0; i < m_list.count(); ++i) {
491 if (m_list.at(i)->appId() == m_appManager->get(row)->appId()) {
492 appIndex = i;
493 break;
494 }
495 }
497 if (appIndex > -1 && !m_list.at(appIndex)->pinned()) {
498 beginRemoveRows(QModelIndex(), appIndex, appIndex);
499 m_list.takeAt(appIndex)->deleteLater();
500 endRemoveRows();
501 m_asAdapter->syncItems(m_list);
502 }
505void LauncherModel::focusedAppIdChanged()
507 QString appId = m_appManager->focusedApplicationId();
508 for (int i = 0; i < m_list.count(); ++i) {
509 LauncherItem *item = m_list.at(i);
510 if (!item->focused() && item->appId() == appId) {
511 item->setFocused(true);
512 Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);
513 } else if (item->focused() && item->appId() != appId) {
514 item->setFocused(false);
515 Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);
516 }
517 }