on bef70da help sign in
unity8/plugins/Unity/Launcher/launchermodel.cpp
443 lines 14 kB history raw
1/*
2 * Copyright 2013 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 <QDebug>
30using namespace unity::shell::application;
32LauncherModel::LauncherModel(QObject *parent):
33 LauncherModelInterface(parent),
34 m_settings(new GSettings(this)),
35 m_dbusIface(new DBusInterface(this)),
36 m_appManager(0)
38 connect(m_dbusIface, &DBusInterface::countChanged, this, &LauncherModel::countChanged);
39 connect(m_dbusIface, &DBusInterface::countVisibleChanged, this, &LauncherModel::countVisibleChanged);
40 connect(m_dbusIface, &DBusInterface::refreshCalled, this, &LauncherModel::refresh);
42 Q_FOREACH (const QString &entry, m_settings->storedApplications()) {
43 if (DesktopFileHandler::findDesktopFile(entry).isEmpty()) {
44 qWarning() << "Couldn't find a .desktop file for" << entry << ". Skipping...";
45 continue;
46 }
48 LauncherItem *item = new LauncherItem(entry,
49 DesktopFileHandler::displayName(entry),
50 DesktopFileHandler::icon(entry),
51 this);
52 item->setPinned(true);
53 m_list.append(item);
54 }
57LauncherModel::~LauncherModel()
59 while (!m_list.empty()) {
60 m_list.takeFirst()->deleteLater();
61 }
64int LauncherModel::rowCount(const QModelIndex &parent) const
66 Q_UNUSED(parent)
67 return m_list.count();
70QVariant LauncherModel::data(const QModelIndex &index, int role) const
72 LauncherItem *item = m_list.at(index.row());
73 switch(role) {
74 case RoleAppId:
75 return item->appId();
76 case RoleName:
77 return item->name();
78 case RoleIcon:
79 return item->icon();
80 case RolePinned:
81 return item->pinned();
82 case RoleCount:
83 return item->count();
84 case RoleCountVisible:
85 return item->countVisible();
86 case RoleProgress:
87 return item->progress();
88 case RoleFocused:
89 return item->focused();
90 }
92 return QVariant();
95unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
97 if (index < 0 || index >= m_list.count()) {
98 return 0;
99 }
100 return m_list.at(index);
103void LauncherModel::move(int oldIndex, int newIndex)
105 // Make sure its not moved outside the lists
106 if (newIndex < 0) {
107 newIndex = 0;
108 }
109 if (newIndex >= m_list.count()) {
110 newIndex = m_list.count()-1;
111 }
113 // Nothing to do?
114 if (oldIndex == newIndex) {
115 return;
116 }
118 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
119 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
120 // adjust the model's index by +1 in case we're moving upwards
121 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
123 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
124 m_list.move(oldIndex, newIndex);
125 endMoveRows();
127 if (!m_list.at(newIndex)->pinned()) {
128 pin(m_list.at(newIndex)->appId());
129 } else {
130 storeAppList();
131 }
134void LauncherModel::pin(const QString &appId, int index)
136 int currentIndex = findApplication(appId);
138 if (currentIndex >= 0) {
139 if (index == -1 || index == currentIndex) {
140 m_list.at(currentIndex)->setPinned(true);
141 QModelIndex modelIndex = this->index(currentIndex);
142 Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RolePinned);
143 } else {
144 move(currentIndex, index);
145 // move() will store the list to the backend itself, so just exit at this point.
146 return;
147 }
148 } else {
149 if (index == -1) {
150 index = m_list.count();
151 }
153 if (DesktopFileHandler::findDesktopFile(appId).isEmpty()) {
154 qWarning() << "Can't pin this application, there is no .destkop file available.";
155 return;
156 }
158 beginInsertRows(QModelIndex(), index, index);
159 LauncherItem *item = new LauncherItem(appId,
160 DesktopFileHandler::displayName(appId),
161 DesktopFileHandler::icon(appId),
162 this);
163 item->setPinned(true);
164 m_list.insert(index, item);
165 endInsertRows();
166 }
168 storeAppList();
171void LauncherModel::requestRemove(const QString &appId)
173 int index = findApplication(appId);
174 if (index < 0) {
175 return;
176 }
178 if (m_appManager->findApplication(appId)) {
179 m_list.at(index)->setPinned(false);
180 QModelIndex modelIndex = this->index(index);
181 Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RolePinned);
182 return;
183 }
185 beginRemoveRows(QModelIndex(), index, index);
186 m_list.takeAt(index)->deleteLater();
187 endRemoveRows();
189 storeAppList();
192void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
194 int index = findApplication(appId);
195 if (index < 0) {
196 return;
197 }
199 LauncherItem *item = m_list.at(index);
200 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
201 if (model) {
202 QString actionId = model->get(actionIndex).actionId();
204 // Check if this is one of the launcher actions we handle ourselves
205 if (actionId == "pin_item") {
206 if (item->pinned()) {
207 requestRemove(appId);
208 } else {
209 pin(appId);
210 }
212 // Nope, we don't know this action, let the backend forward it to the application
213 } else {
214 // TODO: forward quicklist action to app, possibly via m_dbusIface
215 }
216 }
219void LauncherModel::setUser(const QString &username)
221 Q_UNUSED(username)
222 qWarning() << "This backend doesn't support multiple users";
225QString LauncherModel::getUrlForAppId(const QString &appId) const
227 // appId is either an appId or a legacy app name. Let's find out which
228 if (appId.isEmpty()) {
229 return QString();
230 }
232 if (!appId.contains("_")) {
233 return "application:///" + appId + ".desktop";
234 }
236 QStringList parts = appId.split('_');
237 QString package = parts.value(0);
238 QString app = parts.value(1, "first-listed-app");
239 return "appid://" + package + "/" + app + "/current-user-version";
242ApplicationManagerInterface *LauncherModel::applicationManager() const
244 return m_appManager;
247void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
249 // Is there already another appmanager set?
250 if (m_appManager) {
251 // Disconnect any signals
252 disconnect(this, SLOT(applicationAdded(QModelIndex,int)));
253 disconnect(this, SLOT(applicationRemoved(QModelIndex,int)));
254 disconnect(this, SLOT(focusedAppIdChanged()));
256 // remove any recent/running apps from the launcher
257 QList<int> recentAppIndices;
258 for (int i = 0; i < m_list.count(); ++i) {
259 if (m_list.at(i)->recent()) {
260 recentAppIndices << i;
261 }
262 }
263 int run = 0;
264 while (recentAppIndices.count() > 0) {
265 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
266 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
267 endRemoveRows();
268 recentAppIndices.takeFirst();
269 ++run;
270 }
271 }
273 m_appManager = appManager;
274 connect(m_appManager, SIGNAL(rowsInserted(QModelIndex, int, int)), SLOT(applicationAdded(QModelIndex,int)));
275 connect(m_appManager, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), SLOT(applicationRemoved(QModelIndex,int)));
276 connect(m_appManager, SIGNAL(focusedApplicationIdChanged()), SLOT(focusedAppIdChanged()));
278 Q_EMIT applicationManagerChanged();
280 for (int i = 0; i < appManager->count(); ++i) {
281 applicationAdded(QModelIndex(), i);
282 }
286void LauncherModel::storeAppList()
288 QStringList appIds;
289 Q_FOREACH(LauncherItem *item, m_list) {
290 if (item->pinned()) {
291 appIds << item->appId();
292 }
293 }
294 m_settings->setStoredApplications(appIds);
297int LauncherModel::findApplication(const QString &appId)
299 for (int i = 0; i < m_list.count(); ++i) {
300 LauncherItem *item = m_list.at(i);
301 if (item->appId() == appId) {
302 return i;
303 }
304 }
305 return -1;
308void LauncherModel::progressChanged(const QString &appId, int progress)
310 int idx = findApplication(appId);
311 if (idx >= 0) {
312 LauncherItem *item = m_list.at(idx);
313 item->setProgress(progress);
314 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleProgress);
315 }
319void LauncherModel::countChanged(const QString &appId, int count)
321 int idx = findApplication(appId);
322 if (idx >= 0) {
323 LauncherItem *item = m_list.at(idx);
324 item->setCount(count);
325 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleCount);
326 }
329void LauncherModel::countVisibleChanged(const QString &appId, int countVisible)
331 int idx = findApplication(appId);
332 if (idx >= 0) {
333 LauncherItem *item = m_list.at(idx);
334 item->setCountVisible(countVisible);
335 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleCountVisible);
337 // If countVisible goes to false, and the item is neither pinned nor recent we can drop it
338 if (!countVisible && !item->pinned() && !item->recent()) {
339 beginRemoveRows(QModelIndex(), idx, idx);
340 m_list.takeAt(idx)->deleteLater();
341 endRemoveRows();
342 }
343 } else {
344 // Need to create a new LauncherItem and show the highlight
345 if (countVisible && !DesktopFileHandler::findDesktopFile(appId).isEmpty()) {
346 LauncherItem *item = new LauncherItem(appId,
347 DesktopFileHandler::displayName(appId),
348 DesktopFileHandler::icon(appId));
349 item->setCountVisible(true);
350 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
351 m_list.append(item);
352 endInsertRows();
353 }
354 }
357void LauncherModel::refresh()
359 QList<LauncherItem*> toBeRemoved;
360 Q_FOREACH (LauncherItem* item, m_list) {
361 if (DesktopFileHandler::findDesktopFile(item->appId()).isEmpty()) {
362 toBeRemoved << item;
363 } else {
364 int idx = m_list.indexOf(item);
365 item->setName(DesktopFileHandler::displayName(item->appId()));
366 item->setIcon(DesktopFileHandler::icon(item->appId()));
367 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleName << RoleIcon);
368 }
369 }
371 Q_FOREACH (LauncherItem* item, toBeRemoved) {
372 requestRemove(item->appId());
373 }
376void LauncherModel::applicationAdded(const QModelIndex &parent, int row)
378 Q_UNUSED(parent);
380 ApplicationInfoInterface *app = m_appManager->get(row);
381 if (!app) {
382 qWarning() << "LauncherModel received an applicationAdded signal, but there's no such application!";
383 return;
384 }
386 if (app->appId() == "unity8-dash") {
387 // Not adding the dash app
388 return;
389 }
391 bool found = false;
392 Q_FOREACH(LauncherItem *item, m_list) {
393 if (app->appId() == item->appId()) {
394 found = true;
395 break;
396 }
397 }
398 if (found) {
399 // Shall we paint some running/recent app highlight? If yes, do it here.
400 } else {
401 LauncherItem *item = new LauncherItem(app->appId(), app->name(), app->icon().toString(), this);
402 item->setRecent(true);
403 item->setFocused(app->focused());
405 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
406 m_list.append(item);
407 endInsertRows();
408 }
411void LauncherModel::applicationRemoved(const QModelIndex &parent, int row)
413 Q_UNUSED(parent)
415 int appIndex = -1;
416 for (int i = 0; i < m_list.count(); ++i) {
417 if (m_list.at(i)->appId() == m_appManager->get(row)->appId()) {
418 appIndex = i;
419 break;
420 }
421 }
423 if (appIndex > -1 && !m_list.at(appIndex)->pinned()) {
424 beginRemoveRows(QModelIndex(), appIndex, appIndex);
425 m_list.takeAt(appIndex)->deleteLater();
426 endRemoveRows();
427 }
430void LauncherModel::focusedAppIdChanged()
432 QString appId = m_appManager->focusedApplicationId();
433 for (int i = 0; i < m_list.count(); ++i) {
434 LauncherItem *item = m_list.at(i);
435 if (!item->focused() && item->appId() == appId) {
436 item->setFocused(true);
437 Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);
438 } else if (item->focused() && item->appId() != appId) {
439 item->setFocused(false);
440 Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);
441 }
442 }