on 00a2ef4 help sign in
unity8/tests/mocks/Unity/Launcher/MockLauncherModel.cpp
288 lines 9.2 kB history raw
1/*
2 * Copyright 2013,2015 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 "MockLauncherModel.h"
21#include "MockLauncherItem.h"
23MockLauncherModel::MockLauncherModel(QObject* parent): LauncherModelInterface(parent)
25 MockLauncherItem *item = new MockLauncherItem("dialer-app", "/usr/share/applications/dialer-app.desktop", "Dialer", "dialer-app", this);
26 item->setProgress(0);
27 item->setPinned(true);
28 item->setFocused(true);
29 m_list.append(item);
30 item = new MockLauncherItem("camera-app", "/usr/share/applications/camera-app.desktop", "Camera", "camera", this);
31 item->setProgress(10);
32 item->setPinned(true);
33 m_list.append(item);
34 item = new MockLauncherItem("gallery-app", "/usr/share/applications/gallery-app.desktop", "Gallery", "gallery", this);
35 item->setProgress(50);
36 item->setCountVisible(true);
37 item->setAlerting(false);
38 m_list.append(item);
39 item = new MockLauncherItem("music-app", "/usr/share/applications/music-app.desktop", "Music", "soundcloud", this);
40 m_list.append(item);
41 item = new MockLauncherItem("facebook-webapp", "/usr/share/applications/facebook-webapp.desktop", "Facebook", "facebook", this);
42 item->setProgress(150);
43 m_list.append(item);
44 item = new MockLauncherItem("webbrowser-app", "/usr/share/applications/webbrowser-app.desktop", "Browser", "browser", this);
45 item->setCount(1);
46 item->setCountVisible(true);
47 item->setRunning(true);
48 item->setAlerting(false);
49 m_list.append(item);
50 item = new MockLauncherItem("twitter-webapp", "/usr/share/applications/twitter-webapp.desktop", "Twitter", "twitter", this);
51 item->setCount(12);
52 item->setCountVisible(true);
53 item->setAlerting(false);
54 item->setPinned(true);
55 m_list.append(item);
56 item = new MockLauncherItem("gmail-webapp", "/usr/share/applications/gmail-webapp.desktop", "GMail", "gmail", this);
57 item->setCount(123);
58 item->setCountVisible(true);
59 item->setAlerting(false);
60 m_list.append(item);
61 item = new MockLauncherItem("ubuntu-weather-app", "/usr/share/applications/ubuntu-weather-app.desktop", "Weather", "weather", this);
62 item->setCount(1234567890);
63 item->setCountVisible(true);
64 item->setAlerting(false);
65 item->setPinned(true);
66 m_list.append(item);
67 item = new MockLauncherItem("notes-app", "/usr/share/applications/notes-app.desktop", "Notepad", "notepad", this);
68 item->setProgress(50);
69 item->setCount(5);
70 item->setCountVisible(true);
71 item->setAlerting(false);
72 item->setFocused(true);
73 item->setPinned(true);
74 m_list.append(item);
75 item = new MockLauncherItem("calendar-app", "/usr/share/applications/calendar-app.desktop","Calendar", "calendar", this);
76 item->setPinned(true);
77 m_list.append(item);
78 item = new MockLauncherItem("libreoffice", "/usr/share/applications/libreoffice.desktop","Libre Office", "libreoffice", this);
79 item->setPinned(true);
80 m_list.append(item);
83MockLauncherModel::~MockLauncherModel()
87int MockLauncherModel::rowCount(const QModelIndex& parent) const
89 Q_UNUSED(parent)
90 return m_list.count();
93QVariant MockLauncherModel::data(const QModelIndex& index, int role) const
95 LauncherItemInterface *item = m_list.at(index.row());
96 switch(role)
97 {
98 case RoleAppId:
99 return item->appId();
100 case RoleName:
101 return item->name();
102 case RoleIcon:
103 return item->icon();
104 case RolePinned:
105 return item->pinned();
106 case RoleRunning:
107 return item->running();
108 case RoleRecent:
109 return item->recent();
110 case RoleProgress:
111 return item->progress();
112 case RoleCount:
113 return item->count();
114 case RoleCountVisible:
115 return item->countVisible();
116 case RoleFocused:
117 return item->focused();
118 case RoleAlerting:
119 return item->alerting();
120 }
122 return QVariant();
125unity::shell::launcher::LauncherItemInterface *MockLauncherModel::get(int index) const
127 if (index < 0 || index >= m_list.count())
128 {
129 return 0;
130 }
131 return m_list.at(index);
134void MockLauncherModel::setAlerting(const QString &appId, bool alerting) {
135 int index = findApp(appId);
136 if (index >= 0) {
137 QModelIndex modelIndex = this->index(index);
138 MockLauncherItem *item = m_list.at(index);
139 if (!item->focused()) {
140 item->setAlerting(alerting);
141 Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RoleAlerting);
142 }
143 }
146void MockLauncherModel::move(int oldIndex, int newIndex)
148 // Make sure its not moved outside the lists
149 if (newIndex < 0) {
150 newIndex = 0;
151 }
152 if (newIndex >= m_list.count()) {
153 newIndex = m_list.count()-1;
154 }
156 // Nothing to do?
157 if (oldIndex == newIndex) {
158 return;
159 }
161 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
162 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
163 // adjust the model's index by +1 in case we're moving upwards
164 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
166 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
167 m_list.move(oldIndex, newIndex);
168 endMoveRows();
170 pin(m_list.at(newIndex)->appId());
173void MockLauncherModel::pin(const QString &appId, int index)
175 int currentIndex = findApp(appId);
177 if (currentIndex >= 0) {
178 if (index == -1 || index == currentIndex) {
179 m_list.at(currentIndex)->setPinned(true);
180 QModelIndex modelIndex = this->index(currentIndex);
181 Q_EMIT dataChanged(modelIndex, modelIndex);
182 } else {
183 move(currentIndex, index);
184 }
185 } else {
186 beginInsertRows(QModelIndex(), index, index);
187 m_list.insert(index, new MockLauncherItem(appId,
188 appId + ".desktop",
189 appId,
190 appId + ".png"));
191 m_list.at(index)->setPinned(true);
192 endInsertRows();
193 }
196void MockLauncherModel::requestRemove(const QString &appId)
198 int index = findApp(appId);
199 if (index >= 0) {
200 beginRemoveRows(QModelIndex(), index, 0);
201 MockLauncherItem * item = m_list.takeAt(index);
202 item->setRunning(false);
203 item->deleteLater();
204 endRemoveRows();
205 }
208void MockLauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
210 Q_EMIT quickListTriggered(appId, actionIndex);
213int MockLauncherModel::findApp(const QString &appId)
215 for (int i = 0; i < m_list.count(); ++i) {
216 MockLauncherItem *item = m_list.at(i);
217 if (item->appId() == appId) {
218 return i;
219 }
220 }
221 return -1;
224void MockLauncherModel::setProgress(const QString &appId, int progress)
226 int index = findApp(appId);
227 if (index >= 0) {
228 m_list.at(index)->setProgress(progress);
229 QModelIndex modelIndex = this->index(index);
230 Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RoleProgress);
231 }
234void MockLauncherModel::setUser(const QString &username)
236 Q_UNUSED(username)
237 // TODO: implement this...
240QString MockLauncherModel::getUrlForAppId(const QString &appId) const
242 return "application:///" + appId + ".desktop";
245void MockLauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *applicationManager)
247 Q_UNUSED(applicationManager)
250bool MockLauncherModel::onlyPinned() const
252 return false;
255void MockLauncherModel::setOnlyPinned(bool onlyPinned)
257 Q_UNUSED(onlyPinned)
260void MockLauncherModel::emitHint()
262 Q_EMIT hint();
265void MockLauncherModel::setCount(const QString &appId, int count)
267 int index = findApp(appId);
268 if (index >= 0) {
269 m_list.at(index)->setCount(count);
270 QModelIndex modelIndex = this->index(index);
271 Q_EMIT dataChanged(modelIndex, modelIndex);
272 }
275void MockLauncherModel::setCountVisible(const QString &appId, bool countVisible)
277 int index = findApp(appId);
278 if (index >= 0) {
279 m_list.at(index)->setCountVisible(countVisible);
280 QModelIndex modelIndex = this->index(index);
281 Q_EMIT dataChanged(modelIndex, modelIndex);
282 }
285unity::shell::application::ApplicationManagerInterface *MockLauncherModel::applicationManager() const
287 return nullptr;