on 3caee59 help sign in
unity8/tests/mocks/Unity/Launcher/MockLauncherModel.cpp
290 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->setRunning(true);
29 item->setFocused(true);
30 m_list.append(item);
31 item = new MockLauncherItem("camera-app", "/usr/share/applications/camera-app.desktop", "Camera", "camera", this);
32 item->setProgress(10);
33 item->setPinned(true);
34 m_list.append(item);
35 item = new MockLauncherItem("gallery-app", "/usr/share/applications/gallery-app.desktop", "Gallery", "gallery", this);
36 item->setProgress(50);
37 item->setCountVisible(true);
38 item->setRunning(true);
39 item->setAlerting(false);
40 m_list.append(item);
41 item = new MockLauncherItem("music-app", "/usr/share/applications/music-app.desktop", "Music", "soundcloud", this);
42 m_list.append(item);
43 item = new MockLauncherItem("facebook-webapp", "/usr/share/applications/facebook-webapp.desktop", "Facebook", "facebook", this);
44 item->setProgress(150);
45 m_list.append(item);
46 item = new MockLauncherItem("webbrowser-app", "/usr/share/applications/webbrowser-app.desktop", "Browser", "browser", this);
47 item->setCount(1);
48 item->setCountVisible(true);
49 item->setRunning(true);
50 item->setAlerting(false);
51 m_list.append(item);
52 item = new MockLauncherItem("twitter-webapp", "/usr/share/applications/twitter-webapp.desktop", "Twitter", "twitter", this);
53 item->setCount(12);
54 item->setCountVisible(true);
55 item->setAlerting(false);
56 item->setPinned(true);
57 m_list.append(item);
58 item = new MockLauncherItem("gmail-webapp", "/usr/share/applications/gmail-webapp.desktop", "GMail", "gmail", this);
59 item->setCount(123);
60 item->setCountVisible(true);
61 item->setAlerting(false);
62 m_list.append(item);
63 item = new MockLauncherItem("ubuntu-weather-app", "/usr/share/applications/ubuntu-weather-app.desktop", "Weather", "weather", this);
64 item->setCount(1234567890);
65 item->setCountVisible(true);
66 item->setAlerting(false);
67 item->setPinned(true);
68 m_list.append(item);
69 item = new MockLauncherItem("notes-app", "/usr/share/applications/notes-app.desktop", "Notepad", "notepad", this);
70 item->setProgress(50);
71 item->setCount(5);
72 item->setCountVisible(true);
73 item->setAlerting(false);
74 item->setFocused(true);
75 item->setPinned(true);
76 m_list.append(item);
77 item = new MockLauncherItem("calendar-app", "/usr/share/applications/calendar-app.desktop","Calendar", "calendar", this);
78 item->setPinned(true);
79 m_list.append(item);
80 item = new MockLauncherItem("libreoffice", "/usr/share/applications/libreoffice.desktop","Libre Office", "libreoffice", this);
81 item->setPinned(true);
82 m_list.append(item);
85MockLauncherModel::~MockLauncherModel()
89int MockLauncherModel::rowCount(const QModelIndex& parent) const
91 Q_UNUSED(parent)
92 return m_list.count();
95QVariant MockLauncherModel::data(const QModelIndex& index, int role) const
97 LauncherItemInterface *item = m_list.at(index.row());
98 switch(role)
99 {
100 case RoleAppId:
101 return item->appId();
102 case RoleName:
103 return item->name();
104 case RoleIcon:
105 return item->icon();
106 case RolePinned:
107 return item->pinned();
108 case RoleRunning:
109 return item->running();
110 case RoleRecent:
111 return item->recent();
112 case RoleProgress:
113 return item->progress();
114 case RoleCount:
115 return item->count();
116 case RoleCountVisible:
117 return item->countVisible();
118 case RoleFocused:
119 return item->focused();
120 case RoleAlerting:
121 return item->alerting();
122 }
124 return QVariant();
127unity::shell::launcher::LauncherItemInterface *MockLauncherModel::get(int index) const
129 if (index < 0 || index >= m_list.count())
130 {
131 return 0;
132 }
133 return m_list.at(index);
136void MockLauncherModel::setAlerting(const QString &appId, bool alerting) {
137 int index = findApp(appId);
138 if (index >= 0) {
139 QModelIndex modelIndex = this->index(index);
140 MockLauncherItem *item = m_list.at(index);
141 if (!item->focused()) {
142 item->setAlerting(alerting);
143 Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RoleAlerting);
144 }
145 }
148void MockLauncherModel::move(int oldIndex, int newIndex)
150 // Make sure its not moved outside the lists
151 if (newIndex < 0) {
152 newIndex = 0;
153 }
154 if (newIndex >= m_list.count()) {
155 newIndex = m_list.count()-1;
156 }
158 // Nothing to do?
159 if (oldIndex == newIndex) {
160 return;
161 }
163 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
164 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
165 // adjust the model's index by +1 in case we're moving upwards
166 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
168 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
169 m_list.move(oldIndex, newIndex);
170 endMoveRows();
172 pin(m_list.at(newIndex)->appId());
175void MockLauncherModel::pin(const QString &appId, int index)
177 int currentIndex = findApp(appId);
179 if (currentIndex >= 0) {
180 if (index == -1 || index == currentIndex) {
181 m_list.at(currentIndex)->setPinned(true);
182 QModelIndex modelIndex = this->index(currentIndex);
183 Q_EMIT dataChanged(modelIndex, modelIndex);
184 } else {
185 move(currentIndex, index);
186 }
187 } else {
188 beginInsertRows(QModelIndex(), index, index);
189 m_list.insert(index, new MockLauncherItem(appId,
190 appId + ".desktop",
191 appId,
192 appId + ".png"));
193 m_list.at(index)->setPinned(true);
194 endInsertRows();
195 }
198void MockLauncherModel::requestRemove(const QString &appId)
200 int index = findApp(appId);
201 if (index >= 0) {
202 beginRemoveRows(QModelIndex(), index, 0);
203 MockLauncherItem * item = m_list.takeAt(index);
204 item->setRunning(false);
205 item->deleteLater();
206 endRemoveRows();
207 }
210void MockLauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
212 Q_EMIT quickListTriggered(appId, actionIndex);
215int MockLauncherModel::findApp(const QString &appId)
217 for (int i = 0; i < m_list.count(); ++i) {
218 MockLauncherItem *item = m_list.at(i);
219 if (item->appId() == appId) {
220 return i;
221 }
222 }
223 return -1;
226void MockLauncherModel::setProgress(const QString &appId, int progress)
228 int index = findApp(appId);
229 if (index >= 0) {
230 m_list.at(index)->setProgress(progress);
231 QModelIndex modelIndex = this->index(index);
232 Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RoleProgress);
233 }
236void MockLauncherModel::setUser(const QString &username)
238 Q_UNUSED(username)
239 // TODO: implement this...
242QString MockLauncherModel::getUrlForAppId(const QString &appId) const
244 return "application:///" + appId + ".desktop";
247void MockLauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *applicationManager)
249 Q_UNUSED(applicationManager)
252bool MockLauncherModel::onlyPinned() const
254 return false;
257void MockLauncherModel::setOnlyPinned(bool onlyPinned)
259 Q_UNUSED(onlyPinned)
262void MockLauncherModel::emitHint()
264 Q_EMIT hint();
267void MockLauncherModel::setCount(const QString &appId, int count)
269 int index = findApp(appId);
270 if (index >= 0) {
271 m_list.at(index)->setCount(count);
272 QModelIndex modelIndex = this->index(index);
273 Q_EMIT dataChanged(modelIndex, modelIndex);
274 }
277void MockLauncherModel::setCountVisible(const QString &appId, bool countVisible)
279 int index = findApp(appId);
280 if (index >= 0) {
281 m_list.at(index)->setCountVisible(countVisible);
282 QModelIndex modelIndex = this->index(index);
283 Q_EMIT dataChanged(modelIndex, modelIndex);
284 }
287unity::shell::application::ApplicationManagerInterface *MockLauncherModel::applicationManager() const
289 return nullptr;