on ac987a2 help sign in
unity8/plugins/Unity/Launcher/launchermodel.cpp
378 lines 12 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)));
37 connect(m_backend, SIGNAL(appUninstalled(QString)), SLOT(requestRemove(QString)));
39 Q_FOREACH (const QString &entry, m_backend->storedApplications()) {
40 LauncherItem *item = new LauncherItem(entry,
41 m_backend->displayName(entry),
42 m_backend->icon(entry),
43 this);
44 item->setPinned(true);
45 m_list.append(item);
46 }
49LauncherModel::~LauncherModel()
51 while (!m_list.empty()) {
52 m_list.takeFirst()->deleteLater();
53 }
56int LauncherModel::rowCount(const QModelIndex &parent) const
58 Q_UNUSED(parent)
59 return m_list.count();
62QVariant LauncherModel::data(const QModelIndex &index, int role) const
64 LauncherItem *item = m_list.at(index.row());
65 switch(role) {
66 case RoleAppId:
67 return item->appId();
68 case RoleName:
69 return item->name();
70 case RoleIcon:
71 return item->icon();
72 case RolePinned:
73 return item->pinned();
74 case RoleCount:
75 return item->count();
76 case RoleProgress:
77 return item->progress();
78 case RoleFocused:
79 return item->focused();
80 }
82 return QVariant();
85unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
87 if (index < 0 || index >= m_list.count()) {
88 return 0;
89 }
90 return m_list.at(index);
93void LauncherModel::move(int oldIndex, int newIndex)
95 // Make sure its not moved outside the lists
96 if (newIndex < 0) {
97 newIndex = 0;
98 }
99 if (newIndex >= m_list.count()) {
100 newIndex = m_list.count()-1;
101 }
103 // Nothing to do?
104 if (oldIndex == newIndex) {
105 return;
106 }
108 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
109 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
110 // adjust the model's index by +1 in case we're moving upwards
111 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
113 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
114 m_list.move(oldIndex, newIndex);
115 endMoveRows();
117 if (!m_list.at(newIndex)->pinned()) {
118 pin(m_list.at(newIndex)->appId());
119 } else {
120 storeAppList();
121 }
124void LauncherModel::pin(const QString &appId, int index)
126 int currentIndex = findApplication(appId);
128 if (currentIndex >= 0) {
129 if (index == -1 || index == currentIndex) {
130 m_list.at(currentIndex)->setPinned(true);
131 QModelIndex modelIndex = this->index(currentIndex);
132 Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RolePinned);
133 } else {
134 move(currentIndex, index);
135 // move() will store the list to the backend itself, so just exit at this point.
136 return;
137 }
138 } else {
139 if (index == -1) {
140 index = m_list.count();
141 }
142 beginInsertRows(QModelIndex(), index, index);
143 LauncherItem *item = new LauncherItem(appId,
144 m_backend->displayName(appId),
145 m_backend->icon(appId),
146 this);
147 item->setPinned(true);
148 m_list.insert(index, item);
149 endInsertRows();
150 }
152 storeAppList();
155void LauncherModel::requestRemove(const QString &appId)
157 int index = findApplication(appId);
158 if (index < 0) {
159 return;
160 }
162 if (m_appManager->findApplication(appId)) {
163 m_list.at(index)->setPinned(false);
164 QModelIndex modelIndex = this->index(index);
165 Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RolePinned);
166 return;
167 }
169 beginRemoveRows(QModelIndex(), index, index);
170 m_list.takeAt(index)->deleteLater();
171 endRemoveRows();
173 storeAppList();
176void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
178 int index = findApplication(appId);
179 if (index < 0) {
180 return;
181 }
183 LauncherItem *item = m_list.at(index);
184 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
185 if (model) {
186 QString actionId = model->get(actionIndex).actionId();
188 // Check if this is one of the launcher actions we handle ourselves
189 if (actionId == "pin_item") {
190 if (item->pinned()) {
191 requestRemove(appId);
192 } else {
193 pin(appId);
194 }
196 // Nope, we don't know this action, let the backend forward it to the application
197 } else {
198 m_backend->triggerQuickListAction(appId, actionId);
199 }
200 }
203void LauncherModel::setUser(const QString &username)
205 m_backend->setUser(username);
208QString LauncherModel::getUrlForAppId(const QString &appId) const
210 // appId is either an appId or a legacy app name. Let's find out which
211 if (appId.isEmpty())
212 return QString();
214 QString df = m_backend->desktopFile(appId + ".desktop");
215 if (!df.isEmpty())
216 return "application:///" + appId + ".desktop";
218 QStringList parts = appId.split('_');
219 QString package = parts.value(0);
220 QString app = parts.value(1, "first-listed-app");
221 return "appid://" + package + "/" + app + "/current-user-version";
224ApplicationManagerInterface *LauncherModel::applicationManager() const
226 return m_appManager;
229void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
231 // Is there already another appmanager set?
232 if (m_appManager) {
233 // Disconnect any signals
234 disconnect(this, SLOT(applicationAdded(QModelIndex,int)));
235 disconnect(this, SLOT(applicationRemoved(QModelIndex,int)));
236 disconnect(this, SLOT(focusedAppIdChanged()));
238 // remove any recent/running apps from the launcher
239 QList<int> recentAppIndices;
240 for (int i = 0; i < m_list.count(); ++i) {
241 if (m_list.at(i)->recent()) {
242 recentAppIndices << i;
243 }
244 }
245 int run = 0;
246 while (recentAppIndices.count() > 0) {
247 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
248 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
249 endRemoveRows();
250 recentAppIndices.takeFirst();
251 ++run;
252 }
253 }
255 m_appManager = appManager;
256 connect(m_appManager, SIGNAL(rowsInserted(QModelIndex, int, int)), SLOT(applicationAdded(QModelIndex,int)));
257 connect(m_appManager, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), SLOT(applicationRemoved(QModelIndex,int)));
258 connect(m_appManager, SIGNAL(focusedApplicationIdChanged()), SLOT(focusedAppIdChanged()));
260 Q_EMIT applicationManagerChanged();
262 for (int i = 0; i < appManager->count(); ++i) {
263 applicationAdded(QModelIndex(), i);
264 }
268void LauncherModel::storeAppList()
270 QStringList appIds;
271 Q_FOREACH(LauncherItem *item, m_list) {
272 if (item->pinned()) {
273 appIds << item->appId();
274 }
275 }
276 m_backend->setStoredApplications(appIds);
279int LauncherModel::findApplication(const QString &appId)
281 for (int i = 0; i < m_list.count(); ++i) {
282 LauncherItem *item = m_list.at(i);
283 if (item->appId() == appId) {
284 return i;
285 }
286 }
287 return -1;
290void LauncherModel::progressChanged(const QString &appId, int progress)
292 int idx = findApplication(appId);
293 if (idx >= 0) {
294 LauncherItem *item = m_list.at(idx);
295 item->setProgress(progress);
296 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleProgress);
297 }
301void LauncherModel::countChanged(const QString &appId, int count)
303 int idx = findApplication(appId);
304 if (idx >= 0) {
305 LauncherItem *item = m_list.at(idx);
306 item->setCount(count);
307 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleCount);
308 }
311void LauncherModel::applicationAdded(const QModelIndex &parent, int row)
313 Q_UNUSED(parent);
315 ApplicationInfoInterface *app = m_appManager->get(row);
316 if (!app) {
317 qWarning() << "LauncherModel received an applicationAdded signal, but there's no such application!";
318 return;
319 }
321 if (app->appId() == "unity8-dash") {
322 // Not adding the dash app
323 return;
324 }
326 bool found = false;
327 Q_FOREACH(LauncherItem *item, m_list) {
328 if (app->appId() == item->appId()) {
329 found = true;
330 break;
331 }
332 }
333 if (found) {
334 // Shall we paint some running/recent app highlight? If yes, do it here.
335 } else {
336 LauncherItem *item = new LauncherItem(app->appId(), app->name(), app->icon().toString(), this);
337 item->setRecent(true);
338 item->setFocused(app->focused());
340 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
341 m_list.append(item);
342 endInsertRows();
343 }
346void LauncherModel::applicationRemoved(const QModelIndex &parent, int row)
348 Q_UNUSED(parent)
350 int appIndex = -1;
351 for (int i = 0; i < m_list.count(); ++i) {
352 if (m_list.at(i)->appId() == m_appManager->get(row)->appId()) {
353 appIndex = i;
354 break;
355 }
356 }
358 if (appIndex > -1 && !m_list.at(appIndex)->pinned()) {
359 beginRemoveRows(QModelIndex(), appIndex, appIndex);
360 m_list.takeAt(appIndex)->deleteLater();
361 endRemoveRows();
362 }
365void LauncherModel::focusedAppIdChanged()
367 QString appId = m_appManager->focusedApplicationId();
368 for (int i = 0; i < m_list.count(); ++i) {
369 LauncherItem *item = m_list.at(i);
370 if (!item->focused() && item->appId() == appId) {
371 item->setFocused(true);
372 Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);
373 } else if (item->focused() && item->appId() != appId) {
374 item->setFocused(false);
375 Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);
376 }
377 }