on 911a2fb help sign in
unity8/plugins/Unity/Launcher/launchermodel.cpp
377 lines 11 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 "backend/launcherbackend.h"
24#include <unity/shell/application/ApplicationInfoInterface.h>
26#include <QDebug>
28using namespace unity::shell::application;
30LauncherModel::LauncherModel(QObject *parent):
31 LauncherModelInterface(parent),
32 m_backend(new LauncherBackend(this)),
33 m_appManager(0)
35 connect(m_backend, SIGNAL(countChanged(QString,int)), SLOT(countChanged(QString,int)));
36 connect(m_backend, SIGNAL(progressChanged(QString,int)), SLOT(progressChanged(QString,int)));
38 Q_FOREACH (const QString &entry, m_backend->storedApplications()) {
39 LauncherItem *item = new LauncherItem(entry,
40 m_backend->displayName(entry),
41 m_backend->icon(entry),
42 this);
43 item->setPinned(true);
44 m_list.append(item);
45 }
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 RoleProgress:
76 return item->progress();
77 case RoleFocused:
78 return item->focused();
79 }
81 return QVariant();
84unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
86 if (index < 0 || index >= m_list.count()) {
87 return 0;
88 }
89 return m_list.at(index);
92void LauncherModel::move(int oldIndex, int newIndex)
94 // Make sure its not moved outside the lists
95 if (newIndex < 0) {
96 newIndex = 0;
97 }
98 if (newIndex >= m_list.count()) {
99 newIndex = m_list.count()-1;
100 }
102 // Nothing to do?
103 if (oldIndex == newIndex) {
104 return;
105 }
107 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
108 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
109 // adjust the model's index by +1 in case we're moving upwards
110 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
112 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
113 m_list.move(oldIndex, newIndex);
114 endMoveRows();
116 if (!m_list.at(newIndex)->pinned()) {
117 pin(m_list.at(newIndex)->appId());
118 } else {
119 storeAppList();
120 }
123void LauncherModel::pin(const QString &appId, int index)
125 int currentIndex = findApplication(appId);
127 if (currentIndex >= 0) {
128 if (index == -1 || index == currentIndex) {
129 m_list.at(currentIndex)->setPinned(true);
130 QModelIndex modelIndex = this->index(currentIndex);
131 Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RolePinned);
132 } else {
133 move(currentIndex, index);
134 // move() will store the list to the backend itself, so just exit at this point.
135 return;
136 }
137 } else {
138 if (index == -1) {
139 index = m_list.count();
140 }
141 beginInsertRows(QModelIndex(), index, index);
142 LauncherItem *item = new LauncherItem(appId,
143 m_backend->displayName(appId),
144 m_backend->icon(appId),
145 this);
146 item->setPinned(true);
147 m_list.insert(index, item);
148 endInsertRows();
149 }
151 storeAppList();
154void LauncherModel::requestRemove(const QString &appId)
156 int index = findApplication(appId);
157 if (index < 0) {
158 return;
159 }
161 if (m_appManager->findApplication(appId)) {
162 m_list.at(index)->setPinned(false);
163 QModelIndex modelIndex = this->index(index);
164 Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RolePinned);
165 return;
166 }
168 beginRemoveRows(QModelIndex(), index, index);
169 m_list.takeAt(index)->deleteLater();
170 endRemoveRows();
172 storeAppList();
175void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
177 int index = findApplication(appId);
178 if (index < 0) {
179 return;
180 }
182 LauncherItem *item = m_list.at(index);
183 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
184 if (model) {
185 QString actionId = model->get(actionIndex).actionId();
187 // Check if this is one of the launcher actions we handle ourselves
188 if (actionId == "pin_item") {
189 if (item->pinned()) {
190 requestRemove(appId);
191 } else {
192 pin(appId);
193 }
195 // Nope, we don't know this action, let the backend forward it to the application
196 } else {
197 m_backend->triggerQuickListAction(appId, actionId);
198 }
199 }
202void LauncherModel::setUser(const QString &username)
204 m_backend->setUser(username);
207QString LauncherModel::getUrlForAppId(const QString &appId) const
209 // appId is either an appId or a legacy app name. Let's find out which
210 if (appId.isEmpty())
211 return QString();
213 QString df = m_backend->desktopFile(appId + ".desktop");
214 if (!df.isEmpty())
215 return "application:///" + appId + ".desktop";
217 QStringList parts = appId.split('_');
218 QString package = parts.value(0);
219 QString app = parts.value(1, "first-listed-app");
220 return "appid://" + package + "/" + app + "/current-user-version";
223ApplicationManagerInterface *LauncherModel::applicationManager() const
225 return m_appManager;
228void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
230 // Is there already another appmanager set?
231 if (m_appManager) {
232 // Disconnect any signals
233 disconnect(this, SLOT(applicationAdded(QModelIndex,int)));
234 disconnect(this, SLOT(applicationRemoved(QModelIndex,int)));
235 disconnect(this, SLOT(focusedAppIdChanged()));
237 // remove any recent/running apps from the launcher
238 QList<int> recentAppIndices;
239 for (int i = 0; i < m_list.count(); ++i) {
240 if (m_list.at(i)->recent()) {
241 recentAppIndices << i;
242 }
243 }
244 int run = 0;
245 while (recentAppIndices.count() > 0) {
246 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
247 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
248 endRemoveRows();
249 recentAppIndices.takeFirst();
250 ++run;
251 }
252 }
254 m_appManager = appManager;
255 connect(m_appManager, SIGNAL(rowsInserted(QModelIndex, int, int)), SLOT(applicationAdded(QModelIndex,int)));
256 connect(m_appManager, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), SLOT(applicationRemoved(QModelIndex,int)));
257 connect(m_appManager, SIGNAL(focusedApplicationIdChanged()), SLOT(focusedAppIdChanged()));
259 Q_EMIT applicationManagerChanged();
261 for (int i = 0; i < appManager->count(); ++i) {
262 applicationAdded(QModelIndex(), i);
263 }
267void LauncherModel::storeAppList()
269 QStringList appIds;
270 Q_FOREACH(LauncherItem *item, m_list) {
271 if (item->pinned()) {
272 appIds << item->appId();
273 }
274 }
275 m_backend->setStoredApplications(appIds);
278int LauncherModel::findApplication(const QString &appId)
280 for (int i = 0; i < m_list.count(); ++i) {
281 LauncherItem *item = m_list.at(i);
282 if (item->appId() == appId) {
283 return i;
284 }
285 }
286 return -1;
289void LauncherModel::progressChanged(const QString &appId, int progress)
291 int idx = findApplication(appId);
292 if (idx >= 0) {
293 LauncherItem *item = m_list.at(idx);
294 item->setProgress(progress);
295 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleProgress);
296 }
300void LauncherModel::countChanged(const QString &appId, int count)
302 int idx = findApplication(appId);
303 if (idx >= 0) {
304 LauncherItem *item = m_list.at(idx);
305 item->setCount(count);
306 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleCount);
307 }
310void LauncherModel::applicationAdded(const QModelIndex &parent, int row)
312 Q_UNUSED(parent);
314 ApplicationInfoInterface *app = m_appManager->get(row);
315 if (!app) {
316 qWarning() << "LauncherModel received an applicationAdded signal, but there's no such application!";
317 return;
318 }
320 if (app->appId() == "unity8-dash") {
321 // Not adding the dash app
322 return;
323 }
325 bool found = false;
326 Q_FOREACH(LauncherItem *item, m_list) {
327 if (app->appId() == item->appId()) {
328 found = true;
329 break;
330 }
331 }
332 if (found) {
333 // Shall we paint some running/recent app highlight? If yes, do it here.
334 } else {
335 LauncherItem *item = new LauncherItem(app->appId(), app->name(), app->icon().toString(), this);
336 item->setRecent(true);
337 item->setFocused(app->focused());
339 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
340 m_list.append(item);
341 endInsertRows();
342 }
345void LauncherModel::applicationRemoved(const QModelIndex &parent, int row)
347 Q_UNUSED(parent)
349 int appIndex = -1;
350 for (int i = 0; i < m_list.count(); ++i) {
351 if (m_list.at(i)->appId() == m_appManager->get(row)->appId()) {
352 appIndex = i;
353 break;
354 }
355 }
357 if (appIndex > -1 && !m_list.at(appIndex)->pinned()) {
358 beginRemoveRows(QModelIndex(), appIndex, appIndex);
359 m_list.takeAt(appIndex)->deleteLater();
360 endRemoveRows();
361 }
364void LauncherModel::focusedAppIdChanged()
366 QString appId = m_appManager->focusedApplicationId();
367 for (int i = 0; i < m_list.count(); ++i) {
368 LauncherItem *item = m_list.at(i);
369 if (!item->focused() && item->appId() == appId) {
370 item->setFocused(true);
371 Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);
372 } else if (item->focused() && item->appId() != appId) {
373 item->setFocused(false);
374 Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);
375 }
376 }