on 787fcde help sign in
unity8/plugins/Unity/Launcher/launchermodel.cpp
264 lines 7.8 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 <QDebug>
26LauncherModel::LauncherModel(QObject *parent):
27 LauncherModelInterface(parent),
28 m_backend(new LauncherBackend(true, this))
30 connect(m_backend, SIGNAL(countChanged(QString,int)), SLOT(countChanged(QString,int)));
31 connect(m_backend, SIGNAL(progressChanged(QString,int)), SLOT(progressChanged(QString,int)));
33 Q_FOREACH (const QString &entry, m_backend->storedApplications()) {
34 LauncherItem *item = new LauncherItem(entry,
35 m_backend->desktopFile(entry),
36 m_backend->displayName(entry),
37 m_backend->icon(entry),
38 this);
39 if (m_backend->isPinned(entry)) {
40 item->setPinned(true);
41 } else {
42 item->setRecent(true);
43 }
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->desktopFile();
67 case RoleDesktopFile:
68 return item->desktopFile();
69 case RoleName:
70 return item->name();
71 case RoleIcon:
72 return item->icon();
73 case RolePinned:
74 return item->pinned();
75 case RoleCount:
76 return item->count();
77 case RoleProgress:
78 return item->progress();
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 storeAppList();
118 pin(m_list.at(newIndex)->appId());
121void LauncherModel::pin(const QString &appId, int index)
123 int currentIndex = findApplication(appId);
125 if (currentIndex >= 0) {
126 if (index == -1 || index == currentIndex) {
127 m_list.at(currentIndex)->setPinned(true);
128 m_backend->setPinned(appId, true);
129 QModelIndex modelIndex = this->index(currentIndex);
130 Q_EMIT dataChanged(modelIndex, modelIndex);
131 } else {
132 move(currentIndex, index);
133 }
134 } else {
135 if (index == -1) {
136 index = m_list.count();
137 }
138 beginInsertRows(QModelIndex(), index, index);
139 LauncherItem *item = new LauncherItem(appId,
140 m_backend->desktopFile(appId),
141 m_backend->displayName(appId),
142 m_backend->icon(appId));
143 item->setPinned(true);
144 m_backend->setPinned(appId, true);
145 m_list.insert(index, item);
146 endInsertRows();
147 }
150void LauncherModel::requestRemove(const QString &appId)
152 int index = findApplication(appId);
153 if (index < 0) {
154 return;
155 }
157 beginRemoveRows(QModelIndex(), index, index);
158 m_list.takeAt(index)->deleteLater();
159 endRemoveRows();
161 storeAppList();
164void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
166 int index = findApplication(appId);
167 if (index < 0) {
168 return;
169 }
171 LauncherItem *item = m_list.at(index);
172 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
173 if (model) {
174 QString actionId = model->get(actionIndex).actionId();
176 // Check if this is one of the launcher actions we handle ourselves
177 if (actionId == "pin_item") {
178 if (item->pinned()) {
179 requestRemove(appId);
180 } else {
181 pin(appId);
182 }
184 // Nope, we don't know this action, let the backend forward it to the application
185 } else {
186 m_backend->triggerQuickListAction(appId, actionId);
187 }
188 }
191void LauncherModel::setUser(const QString &username)
193 m_backend->setUser(username);
196void LauncherModel::applicationFocused(const QString &appId)
198 int index = findApplication(appId);
200 if (index >= 0) {
201 // TODO: Mark application as focued
202 } else {
203 // FIXME: The backend does not yet support this stuff
204 // and currently the appId still is the desktopFile
205 //QString desktopFile = m_backend->desktopFile(appId);
206 QString desktopFile = appId;
208 QString appName = m_backend->displayName(appId);
209 QString icon = m_backend->icon(appId);
211 LauncherItem *item = new LauncherItem(appId, desktopFile, appName, icon);
212 item->setRecent(true);
213 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
214 m_list.append(item);
215 endInsertRows();
216 storeAppList();
217 }
220void LauncherModel::storeAppList()
222 QStringList appIds;
223 Q_FOREACH(LauncherItem *item, m_list) {
224 qDebug() << "shouldI store app" << item->appId();
225 if (item->pinned() || item->recent()) {
226 qDebug() << "yes";
227 appIds << item->appId();
228 }
229 }
230 qDebug() << "storing appids" << appIds;
231 m_backend->setStoredApplications(appIds);
234int LauncherModel::findApplication(const QString &appId)
236 for (int i = 0; i < m_list.count(); ++i) {
237 LauncherItem *item = m_list.at(i);
238 if (item->appId() == appId) {
239 return i;
240 }
241 }
242 return -1;
245void LauncherModel::progressChanged(const QString &appId, int progress)
247 int idx = findApplication(appId);
248 if (idx >= 0) {
249 LauncherItem *item = m_list.at(idx);
250 item->setProgress(progress);
251 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleProgress);
252 }
256void LauncherModel::countChanged(const QString &appId, int count)
258 int idx = findApplication(appId);
259 if (idx >= 0) {
260 LauncherItem *item = m_list.at(idx);
261 item->setCount(count);
262 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleCount);
263 }