on cc4e572 help sign in
unity8/plugins/Unity/Launcher/launchermodel.cpp
235 lines 6.9 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(true, 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 m_backend->setPinned(appId, true);
127 QModelIndex modelIndex = this->index(currentIndex);
128 Q_EMIT dataChanged(modelIndex, modelIndex);
129 } else {
130 move(currentIndex, index);
131 }
132 } else {
133 if (index == -1) {
134 index = m_list.count();
135 }
136 beginInsertRows(QModelIndex(), index, index);
137 LauncherItem *item = new LauncherItem(appId,
138 m_backend->desktopFile(appId),
139 m_backend->displayName(appId),
140 m_backend->icon(appId));
141 item->setPinned(true);
142 m_backend->setPinned(appId, true);
143 m_list.insert(index, item);
144 endInsertRows();
145 }
148void LauncherModel::requestRemove(const QString &appId)
150 int index = findApplication(appId);
151 if (index < 0) {
152 return;
153 }
155 beginRemoveRows(QModelIndex(), index, index);
156 m_list.takeAt(index)->deleteLater();
157 endRemoveRows();
159 storeAppList();
162void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
164 int index = findApplication(appId);
165 if (index < 0) {
166 return;
167 }
169 LauncherItem *item = m_list.at(index);
170 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
171 if (model) {
172 QString actionId = model->get(actionIndex).actionId();
174 // Check if this is one of the launcher actions we handle ourselves
175 if (actionId == "pin_item") {
176 if (item->pinned()) {
177 requestRemove(appId);
178 } else {
179 pin(appId);
180 }
182 // Nope, we don't know this action, let the backend forward it to the application
183 } else {
184 m_backend->triggerQuickListAction(appId, actionId);
185 }
186 }
189void LauncherModel::setUser(const QString &username)
191 m_backend->setUser(username);
194void LauncherModel::storeAppList()
196 QStringList appIds;
197 Q_FOREACH(LauncherItem *item, m_list) {
198 if (item->pinned() || item->recent()) {
199 appIds << item->appId();
200 }
201 }
202 m_backend->setStoredApplications(appIds);
205int LauncherModel::findApplication(const QString &appId)
207 for (int i = 0; i < m_list.count(); ++i) {
208 LauncherItem *item = m_list.at(i);
209 if (item->appId() == appId) {
210 return i;
211 }
212 }
213 return -1;
216void LauncherModel::progressChanged(const QString &appId, int progress)
218 int idx = findApplication(appId);
219 if (idx >= 0) {
220 LauncherItem *item = m_list.at(idx);
221 item->setProgress(progress);
222 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleProgress);
223 }
227void LauncherModel::countChanged(const QString &appId, int count)
229 int idx = findApplication(appId);
230 if (idx >= 0) {
231 LauncherItem *item = m_list.at(idx);
232 item->setCount(count);
233 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleCount);
234 }