on 5dbd13d help sign in
unity8/plugins/Unity/Launcher/launchermodel.cpp
446 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_desktopFileHandler(new DesktopFileHandler(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 Q_FOREACH (const QString &entry, m_settings->storedApplications()) {
44 qDebug() << "got stored item:" << entry;
45 if (m_desktopFileHandler->findDesktopFile(entry).isEmpty()) {
46 qWarning() << "Couldn't find a .desktop file for" << entry << ". Skipping...";
47 continue;
48 }
50 LauncherItem *item = new LauncherItem(entry,
51 m_desktopFileHandler->displayName(entry),
52 m_desktopFileHandler->icon(entry),
53 this);
54 item->setPinned(true);
55 m_list.append(item);
56 qDebug() << "Loaded item" << item->name() << item->icon();
57 }
60LauncherModel::~LauncherModel()
62 while (!m_list.empty()) {
63 m_list.takeFirst()->deleteLater();
64 }
67int LauncherModel::rowCount(const QModelIndex &parent) const
69 Q_UNUSED(parent)
70 return m_list.count();
73QVariant LauncherModel::data(const QModelIndex &index, int role) const
75 LauncherItem *item = m_list.at(index.row());
76 switch(role) {
77 case RoleAppId:
78 return item->appId();
79 case RoleName:
80 return item->name();
81 case RoleIcon:
82 return item->icon();
83 case RolePinned:
84 return item->pinned();
85 case RoleCount:
86 return item->count();
87 case RoleCountVisible:
88 return item->countVisible();
89 case RoleProgress:
90 return item->progress();
91 case RoleFocused:
92 return item->focused();
93 }
95 return QVariant();
98unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
100 if (index < 0 || index >= m_list.count()) {
101 return 0;
102 }
103 return m_list.at(index);
106void LauncherModel::move(int oldIndex, int newIndex)
108 // Make sure its not moved outside the lists
109 if (newIndex < 0) {
110 newIndex = 0;
111 }
112 if (newIndex >= m_list.count()) {
113 newIndex = m_list.count()-1;
114 }
116 // Nothing to do?
117 if (oldIndex == newIndex) {
118 return;
119 }
121 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
122 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
123 // adjust the model's index by +1 in case we're moving upwards
124 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
126 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
127 m_list.move(oldIndex, newIndex);
128 endMoveRows();
130 if (!m_list.at(newIndex)->pinned()) {
131 pin(m_list.at(newIndex)->appId());
132 } else {
133 storeAppList();
134 }
137void LauncherModel::pin(const QString &appId, int index)
139 int currentIndex = findApplication(appId);
141 if (currentIndex >= 0) {
142 if (index == -1 || index == currentIndex) {
143 m_list.at(currentIndex)->setPinned(true);
144 QModelIndex modelIndex = this->index(currentIndex);
145 Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RolePinned);
146 } else {
147 move(currentIndex, index);
148 // move() will store the list to the backend itself, so just exit at this point.
149 return;
150 }
151 } else {
152 if (index == -1) {
153 index = m_list.count();
154 }
156 if (m_desktopFileHandler->findDesktopFile(appId).isEmpty()) {
157 qWarning() << "Can't pin this application, there is no .destkop file available.";
158 return;
159 }
161 beginInsertRows(QModelIndex(), index, index);
162 LauncherItem *item = new LauncherItem(appId,
163 m_desktopFileHandler->displayName(appId),
164 m_desktopFileHandler->icon(appId),
165 this);
166 item->setPinned(true);
167 m_list.insert(index, item);
168 endInsertRows();
169 }
171 storeAppList();
174void LauncherModel::requestRemove(const QString &appId)
176 int index = findApplication(appId);
177 if (index < 0) {
178 return;
179 }
181 if (m_appManager->findApplication(appId)) {
182 m_list.at(index)->setPinned(false);
183 QModelIndex modelIndex = this->index(index);
184 Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RolePinned);
185 return;
186 }
188 beginRemoveRows(QModelIndex(), index, index);
189 m_list.takeAt(index)->deleteLater();
190 endRemoveRows();
192 storeAppList();
195void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
197 int index = findApplication(appId);
198 if (index < 0) {
199 return;
200 }
202 LauncherItem *item = m_list.at(index);
203 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
204 if (model) {
205 QString actionId = model->get(actionIndex).actionId();
207 // Check if this is one of the launcher actions we handle ourselves
208 if (actionId == "pin_item") {
209 if (item->pinned()) {
210 requestRemove(appId);
211 } else {
212 pin(appId);
213 }
215 // Nope, we don't know this action, let the backend forward it to the application
216 } else {
217 // TODO: forward quicklist action to app, possibly via m_dbusIface
218 }
219 }
222void LauncherModel::setUser(const QString &username)
224 Q_UNUSED(username)
225 qWarning() << "This backend doesn't support multiple users";
228QString LauncherModel::getUrlForAppId(const QString &appId) const
230 // appId is either an appId or a legacy app name. Let's find out which
231 if (appId.isEmpty()) {
232 return QString();
233 }
235 if (!appId.contains("_")) {
236 return "application:///" + appId + ".desktop";
237 }
239 QStringList parts = appId.split('_');
240 QString package = parts.value(0);
241 QString app = parts.value(1, "first-listed-app");
242 return "appid://" + package + "/" + app + "/current-user-version";
245ApplicationManagerInterface *LauncherModel::applicationManager() const
247 return m_appManager;
250void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
252 // Is there already another appmanager set?
253 if (m_appManager) {
254 // Disconnect any signals
255 disconnect(this, SLOT(applicationAdded(QModelIndex,int)));
256 disconnect(this, SLOT(applicationRemoved(QModelIndex,int)));
257 disconnect(this, SLOT(focusedAppIdChanged()));
259 // remove any recent/running apps from the launcher
260 QList<int> recentAppIndices;
261 for (int i = 0; i < m_list.count(); ++i) {
262 if (m_list.at(i)->recent()) {
263 recentAppIndices << i;
264 }
265 }
266 int run = 0;
267 while (recentAppIndices.count() > 0) {
268 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
269 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
270 endRemoveRows();
271 recentAppIndices.takeFirst();
272 ++run;
273 }
274 }
276 m_appManager = appManager;
277 connect(m_appManager, SIGNAL(rowsInserted(QModelIndex, int, int)), SLOT(applicationAdded(QModelIndex,int)));
278 connect(m_appManager, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), SLOT(applicationRemoved(QModelIndex,int)));
279 connect(m_appManager, SIGNAL(focusedApplicationIdChanged()), SLOT(focusedAppIdChanged()));
281 Q_EMIT applicationManagerChanged();
283 for (int i = 0; i < appManager->count(); ++i) {
284 applicationAdded(QModelIndex(), i);
285 }
289void LauncherModel::storeAppList()
291 QStringList appIds;
292 Q_FOREACH(LauncherItem *item, m_list) {
293 if (item->pinned()) {
294 appIds << item->appId();
295 }
296 }
297 m_settings->setStoredApplications(appIds);
300int LauncherModel::findApplication(const QString &appId)
302 for (int i = 0; i < m_list.count(); ++i) {
303 LauncherItem *item = m_list.at(i);
304 if (item->appId() == appId) {
305 return i;
306 }
307 }
308 return -1;
311void LauncherModel::progressChanged(const QString &appId, int progress)
313 int idx = findApplication(appId);
314 if (idx >= 0) {
315 LauncherItem *item = m_list.at(idx);
316 item->setProgress(progress);
317 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleProgress);
318 }
322void LauncherModel::countChanged(const QString &appId, int count)
324 int idx = findApplication(appId);
325 if (idx >= 0) {
326 LauncherItem *item = m_list.at(idx);
327 item->setCount(count);
328 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleCount);
329 }
332void LauncherModel::countVisibleChanged(const QString &appId, int countVisible)
334 int idx = findApplication(appId);
335 if (idx >= 0) {
336 LauncherItem *item = m_list.at(idx);
337 item->setCountVisible(countVisible);
338 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleCountVisible);
340 // If countVisible goes to false, and the item is neither pinned nor recent we can drop it
341 if (!countVisible && !item->pinned() && !item->recent()) {
342 beginRemoveRows(QModelIndex(), idx, idx);
343 m_list.takeAt(idx)->deleteLater();
344 endRemoveRows();
345 }
346 } else {
347 // Need to create a new LauncherItem and show the highlight
348 if (countVisible && !m_desktopFileHandler->findDesktopFile(appId).isEmpty()) {
349 LauncherItem *item = new LauncherItem(appId,
350 m_desktopFileHandler->displayName(appId),
351 m_desktopFileHandler->icon(appId));
352 item->setCountVisible(true);
353 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
354 m_list.append(item);
355 endInsertRows();
356 }
357 }
360void LauncherModel::refresh()
362 QList<LauncherItem*> toBeRemoved;
363 Q_FOREACH (LauncherItem* item, m_list) {
364 if (m_desktopFileHandler->findDesktopFile(item->appId()).isEmpty()) {
365 toBeRemoved << item;
366 } else {
367 int idx = m_list.indexOf(item);
368 item->setName(m_desktopFileHandler->displayName(item->appId()));
369 item->setIcon(m_desktopFileHandler->icon(item->appId()));
370 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleName << RoleIcon);
371 }
372 }
374 Q_FOREACH (LauncherItem* item, toBeRemoved) {
375 requestRemove(item->appId());
376 }
379void LauncherModel::applicationAdded(const QModelIndex &parent, int row)
381 Q_UNUSED(parent);
383 ApplicationInfoInterface *app = m_appManager->get(row);
384 if (!app) {
385 qWarning() << "LauncherModel received an applicationAdded signal, but there's no such application!";
386 return;
387 }
389 if (app->appId() == "unity8-dash") {
390 // Not adding the dash app
391 return;
392 }
394 bool found = false;
395 Q_FOREACH(LauncherItem *item, m_list) {
396 if (app->appId() == item->appId()) {
397 found = true;
398 break;
399 }
400 }
401 if (found) {
402 // Shall we paint some running/recent app highlight? If yes, do it here.
403 } else {
404 LauncherItem *item = new LauncherItem(app->appId(), app->name(), app->icon().toString(), this);
405 item->setRecent(true);
406 item->setFocused(app->focused());
408 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
409 m_list.append(item);
410 endInsertRows();
411 }
414void LauncherModel::applicationRemoved(const QModelIndex &parent, int row)
416 Q_UNUSED(parent)
418 int appIndex = -1;
419 for (int i = 0; i < m_list.count(); ++i) {
420 if (m_list.at(i)->appId() == m_appManager->get(row)->appId()) {
421 appIndex = i;
422 break;
423 }
424 }
426 if (appIndex > -1 && !m_list.at(appIndex)->pinned()) {
427 beginRemoveRows(QModelIndex(), appIndex, appIndex);
428 m_list.takeAt(appIndex)->deleteLater();
429 endRemoveRows();
430 }
433void LauncherModel::focusedAppIdChanged()
435 QString appId = m_appManager->focusedApplicationId();
436 for (int i = 0; i < m_list.count(); ++i) {
437 LauncherItem *item = m_list.at(i);
438 if (!item->focused() && item->appId() == appId) {
439 item->setFocused(true);
440 Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);
441 } else if (item->focused() && item->appId() != appId) {
442 item->setFocused(false);
443 Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);
444 }
445 }