on 34be00a help sign in
unity8/tests/mocks/Unity/Launcher/MockLauncherModel.cpp
298 lines 9.6 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->setSurfaceCount(1);
29 item->setRunning(true);
30 item->setFocused(true);
31 m_list.append(item);
32 item = new MockLauncherItem("camera-app", "/usr/share/applications/camera-app.desktop", "Camera", "camera", this);
33 item->setProgress(10);
34 item->setPinned(true);
35 m_list.append(item);
36 item = new MockLauncherItem("camera-app2", "/usr/share/applications/camera-app2.desktop", "Camera2", "camera", this);
37 item->setPinned(true);
38 m_list.append(item);
39 item = new MockLauncherItem("gallery-app", "/usr/share/applications/gallery-app.desktop", "Gallery", "gallery", this);
40 item->setProgress(50);
41 item->setCountVisible(true);
42 item->setRunning(true);
43 item->setSurfaceCount(2);
44 item->setAlerting(false);
45 m_list.append(item);
46 item = new MockLauncherItem("music-app", "/usr/share/applications/music-app.desktop", "Music", "soundcloud", this);
47 m_list.append(item);
48 item = new MockLauncherItem("facebook-webapp", "/usr/share/applications/facebook-webapp.desktop", "Facebook", "facebook", this);
49 item->setProgress(150);
50 m_list.append(item);
51 item = new MockLauncherItem("webbrowser-app", "/usr/share/applications/webbrowser-app.desktop", "Browser", "browser", this);
52 item->setSurfaceCount(5);
53 item->setCount(1);
54 item->setCountVisible(true);
55 item->setRunning(true);
56 item->setAlerting(false);
57 m_list.append(item);
58 item = new MockLauncherItem("twitter-webapp", "/usr/share/applications/twitter-webapp.desktop", "Twitter", "twitter", this);
59 item->setCount(12);
60 item->setCountVisible(true);
61 item->setAlerting(false);
62 item->setPinned(true);
63 m_list.append(item);
64 item = new MockLauncherItem("gmail-webapp", "/usr/share/applications/gmail-webapp.desktop", "GMail", "gmail", this);
65 item->setCount(123);
66 item->setCountVisible(true);
67 item->setAlerting(false);
68 m_list.append(item);
69 item = new MockLauncherItem("ubuntu-weather-app", "/usr/share/applications/ubuntu-weather-app.desktop", "Weather", "weather", this);
70 item->setCount(1234567890);
71 item->setCountVisible(true);
72 item->setAlerting(false);
73 item->setPinned(true);
74 m_list.append(item);
75 item = new MockLauncherItem("notes-app", "/usr/share/applications/notes-app.desktop", "Notepad", "notepad", this);
76 item->setProgress(50);
77 item->setCount(5);
78 item->setCountVisible(true);
79 item->setAlerting(false);
80 item->setFocused(true);
81 item->setPinned(true);
82 m_list.append(item);
83 item = new MockLauncherItem("calendar-app", "/usr/share/applications/calendar-app.desktop","Calendar", "calendar", this);
84 item->setPinned(true);
85 m_list.append(item);
86 item = new MockLauncherItem("libreoffice", "/usr/share/applications/libreoffice.desktop","Libre Office", "libreoffice", this);
87 item->setPinned(true);
88 m_list.append(item);
91MockLauncherModel::~MockLauncherModel()
95int MockLauncherModel::rowCount(const QModelIndex& parent) const
97 Q_UNUSED(parent)
98 return m_list.count();
101QVariant MockLauncherModel::data(const QModelIndex& index, int role) const
103 LauncherItemInterface *item = m_list.at(index.row());
104 switch(role)
105 {
106 case RoleAppId:
107 return item->appId();
108 case RoleName:
109 return item->name();
110 case RoleIcon:
111 return item->icon();
112 case RolePinned:
113 return item->pinned();
114 case RoleRunning:
115 return item->running();
116 case RoleRecent:
117 return item->recent();
118 case RoleProgress:
119 return item->progress();
120 case RoleCount:
121 return item->count();
122 case RoleCountVisible:
123 return item->countVisible();
124 case RoleFocused:
125 return item->focused();
126 case RoleAlerting:
127 return item->alerting();
128 case RoleSurfaceCount:
129 return item->surfaceCount();
130 }
132 return QVariant();
135unity::shell::launcher::LauncherItemInterface *MockLauncherModel::get(int index) const
137 if (index < 0 || index >= m_list.count())
138 {
139 return 0;
140 }
141 return m_list.at(index);
144void MockLauncherModel::setAlerting(const QString &appId, bool alerting) {
145 int index = findApp(appId);
146 if (index >= 0) {
147 QModelIndex modelIndex = this->index(index);
148 MockLauncherItem *item = m_list.at(index);
149 if (!item->focused()) {
150 item->setAlerting(alerting);
151 Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RoleAlerting);
152 }
153 }
156void MockLauncherModel::move(int oldIndex, int newIndex)
158 // Make sure its not moved outside the lists
159 if (newIndex < 0) {
160 newIndex = 0;
161 }
162 if (newIndex >= m_list.count()) {
163 newIndex = m_list.count()-1;
164 }
166 // Nothing to do?
167 if (oldIndex == newIndex) {
168 return;
169 }
171 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
172 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
173 // adjust the model's index by +1 in case we're moving upwards
174 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
176 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
177 m_list.move(oldIndex, newIndex);
178 endMoveRows();
180 pin(m_list.at(newIndex)->appId());
183void MockLauncherModel::pin(const QString &appId, int index)
185 int currentIndex = findApp(appId);
187 if (currentIndex >= 0) {
188 if (index == -1 || index == currentIndex) {
189 m_list.at(currentIndex)->setPinned(true);
190 QModelIndex modelIndex = this->index(currentIndex);
191 Q_EMIT dataChanged(modelIndex, modelIndex);
192 } else {
193 move(currentIndex, index);
194 }
195 } else {
196 beginInsertRows(QModelIndex(), index, index);
197 m_list.insert(index, new MockLauncherItem(appId,
198 appId + ".desktop",
199 appId,
200 appId + ".png"));
201 m_list.at(index)->setPinned(true);
202 endInsertRows();
203 }
206void MockLauncherModel::requestRemove(const QString &appId)
208 int index = findApp(appId);
209 if (index >= 0) {
210 beginRemoveRows(QModelIndex(), index, 0);
211 MockLauncherItem * item = m_list.takeAt(index);
212 item->setRunning(false);
213 item->deleteLater();
214 endRemoveRows();
215 }
218void MockLauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
220 Q_EMIT quickListTriggered(appId, actionIndex);
223int MockLauncherModel::findApp(const QString &appId)
225 for (int i = 0; i < m_list.count(); ++i) {
226 MockLauncherItem *item = m_list.at(i);
227 if (item->appId() == appId) {
228 return i;
229 }
230 }
231 return -1;
234void MockLauncherModel::setProgress(const QString &appId, int progress)
236 int index = findApp(appId);
237 if (index >= 0) {
238 m_list.at(index)->setProgress(progress);
239 QModelIndex modelIndex = this->index(index);
240 Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RoleProgress);
241 }
244void MockLauncherModel::setUser(const QString &username)
246 Q_UNUSED(username)
247 // TODO: implement this...
250QString MockLauncherModel::getUrlForAppId(const QString &appId) const
252 return "application:///" + appId + ".desktop";
255void MockLauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *applicationManager)
257 Q_UNUSED(applicationManager)
260bool MockLauncherModel::onlyPinned() const
262 return false;
265void MockLauncherModel::setOnlyPinned(bool onlyPinned)
267 Q_UNUSED(onlyPinned)
270void MockLauncherModel::emitHint()
272 Q_EMIT hint();
275void MockLauncherModel::setCount(const QString &appId, int count)
277 int index = findApp(appId);
278 if (index >= 0) {
279 m_list.at(index)->setCount(count);
280 QModelIndex modelIndex = this->index(index);
281 Q_EMIT dataChanged(modelIndex, modelIndex);
282 }
285void MockLauncherModel::setCountVisible(const QString &appId, bool countVisible)
287 int index = findApp(appId);
288 if (index >= 0) {
289 m_list.at(index)->setCountVisible(countVisible);
290 QModelIndex modelIndex = this->index(index);
291 Q_EMIT dataChanged(modelIndex, modelIndex);
292 }
295unity::shell::application::ApplicationManagerInterface *MockLauncherModel::applicationManager() const
297 return nullptr;