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