on fd4e062 help sign in
unity8/plugins/Unity/Launcher/launchermodel.cpp
234 lines 6.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"
24LauncherModel::LauncherModel(QObject *parent):
25 LauncherModelInterface(parent),
26 m_backend(new LauncherBackend(this))
28 connect(m_backend, SIGNAL(countChanged(QString,int)), SLOT(countChanged(QString,int)));
29 connect(m_backend, SIGNAL(progressChanged(QString,int)), SLOT(progressChanged(QString,int)));
31 Q_FOREACH (const QString &entry, m_backend->storedApplications()) {
32 LauncherItem *item = new LauncherItem(entry,
33 m_backend->desktopFile(entry),
34 m_backend->displayName(entry),
35 m_backend->icon(entry),
36 this);
37 if (m_backend->isPinned(entry)) {
38 item->setPinned(true);
39 } else {
40 item->setRecent(true);
41 }
42 m_list.append(item);
43 }
46LauncherModel::~LauncherModel()
48 while (!m_list.empty()) {
49 m_list.takeFirst()->deleteLater();
50 }
53int LauncherModel::rowCount(const QModelIndex &parent) const
55 Q_UNUSED(parent)
56 return m_list.count();
59QVariant LauncherModel::data(const QModelIndex &index, int role) const
61 LauncherItem *item = m_list.at(index.row());
62 switch(role) {
63 case RoleAppId:
64 return item->desktopFile();
65 case RoleDesktopFile:
66 return item->desktopFile();
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 }
79 return QVariant();
82unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
84 if (index < 0 || index >= m_list.count()) {
85 return 0;
86 }
87 return m_list.at(index);
90void LauncherModel::move(int oldIndex, int newIndex)
92 // Make sure its not moved outside the lists
93 if (newIndex < 0) {
94 newIndex = 0;
95 }
96 if (newIndex >= m_list.count()) {
97 newIndex = m_list.count()-1;
98 }
100 // Nothing to do?
101 if (oldIndex == newIndex) {
102 return;
103 }
105 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
106 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
107 // adjust the model's index by +1 in case we're moving upwards
108 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
110 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
111 m_list.move(oldIndex, newIndex);
112 endMoveRows();
114 storeAppList();
116 pin(m_list.at(newIndex)->appId());
119void LauncherModel::pin(const QString &appId, int index)
121 int currentIndex = findApplication(appId);
123 if (currentIndex >= 0) {
124 if (index == -1 || index == currentIndex) {
125 m_list.at(currentIndex)->setPinned(true);
126 QModelIndex modelIndex = this->index(currentIndex);
127 Q_EMIT dataChanged(modelIndex, modelIndex);
128 } else {
129 move(currentIndex, index);
130 }
131 } else {
132 if (index == -1) {
133 index = m_list.count();
134 }
135 beginInsertRows(QModelIndex(), index, index);
136 LauncherItem *item = new LauncherItem(appId,
137 m_backend->desktopFile(appId),
138 m_backend->displayName(appId),
139 m_backend->icon(appId));
140 item->setPinned(true);
141 m_list.insert(index, item);
142 endInsertRows();
143 }
146void LauncherModel::requestRemove(const QString &appId)
148 int index = findApplication(appId);
149 if (index < 0) {
150 return;
151 }
153 beginRemoveRows(QModelIndex(), index, index);
154 m_list.takeAt(index)->deleteLater();
155 endRemoveRows();
157 storeAppList();
160void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
162 int index = findApplication(appId);
163 if (index < 0) {
164 return;
165 }
167 LauncherItem *item = m_list.at(index);
168 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
169 if (model) {
170 QString actionId = model->get(actionIndex).actionId();
172 // Check if this is one of the launcher actions we handle ourselves
173 if (actionId == "pin_item") {
174 if (item->pinned()) {
175 requestRemove(appId);
176 } else {
177 pin(appId);
178 }
180 // Nope, we don't know this action, let the backend forward it to the application
181 } else {
182 m_backend->triggerQuickListAction(appId, actionId);
183 }
184 }
187void LauncherModel::setUser(const QString &username)
189 Q_UNUSED(username)
190 // TODO: Implement this...
193void LauncherModel::storeAppList()
195 QStringList appIds;
196 Q_FOREACH(LauncherItem *item, m_list) {
197 if (item->pinned() || item->recent()) {
198 appIds << item->appId();
199 }
200 }
201 m_backend->setStoredApplications(appIds);
204int LauncherModel::findApplication(const QString &appId)
206 for (int i = 0; i < m_list.count(); ++i) {
207 LauncherItem *item = m_list.at(i);
208 if (item->appId() == appId) {
209 return i;
210 }
211 }
212 return -1;
215void LauncherModel::progressChanged(const QString &appId, int progress)
217 int idx = findApplication(appId);
218 if (idx >= 0) {
219 LauncherItem *item = m_list.at(idx);
220 item->setProgress(progress);
221 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleProgress);
222 }
226void LauncherModel::countChanged(const QString &appId, int count)
228 int idx = findApplication(appId);
229 if (idx >= 0) {
230 LauncherItem *item = m_list.at(idx);
231 item->setCount(count);
232 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleCount);
233 }