on d2aa4eb help sign in
unity8/tests/mocks/Unity/Launcher/MockLauncherModel.cpp
285 lines 9.0 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);
80MockLauncherModel::~MockLauncherModel()
84int MockLauncherModel::rowCount(const QModelIndex& parent) const
86 Q_UNUSED(parent)
87 return m_list.count();
90QVariant MockLauncherModel::data(const QModelIndex& index, int role) const
92 LauncherItemInterface *item = m_list.at(index.row());
93 switch(role)
94 {
95 case RoleAppId:
96 return item->appId();
97 case RoleName:
98 return item->name();
99 case RoleIcon:
100 return item->icon();
101 case RolePinned:
102 return item->pinned();
103 case RoleRunning:
104 return item->running();
105 case RoleRecent:
106 return item->recent();
107 case RoleProgress:
108 return item->progress();
109 case RoleCount:
110 return item->count();
111 case RoleCountVisible:
112 return item->countVisible();
113 case RoleFocused:
114 return item->focused();
115 case RoleAlerting:
116 return item->alerting();
117 }
119 return QVariant();
122unity::shell::launcher::LauncherItemInterface *MockLauncherModel::get(int index) const
124 if (index < 0 || index >= m_list.count())
125 {
126 return 0;
127 }
128 return m_list.at(index);
131void MockLauncherModel::setAlerting(const QString &appId, bool alerting) {
132 int index = findApp(appId);
133 if (index >= 0) {
134 QModelIndex modelIndex = this->index(index);
135 MockLauncherItem *item = m_list.at(index);
136 if (!item->focused()) {
137 item->setAlerting(alerting);
138 Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RoleAlerting);
139 }
140 }
143void MockLauncherModel::move(int oldIndex, int newIndex)
145 // Make sure its not moved outside the lists
146 if (newIndex < 0) {
147 newIndex = 0;
148 }
149 if (newIndex >= m_list.count()) {
150 newIndex = m_list.count()-1;
151 }
153 // Nothing to do?
154 if (oldIndex == newIndex) {
155 return;
156 }
158 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
159 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
160 // adjust the model's index by +1 in case we're moving upwards
161 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
163 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
164 m_list.move(oldIndex, newIndex);
165 endMoveRows();
167 pin(m_list.at(newIndex)->appId());
170void MockLauncherModel::pin(const QString &appId, int index)
172 int currentIndex = findApp(appId);
174 if (currentIndex >= 0) {
175 if (index == -1 || index == currentIndex) {
176 m_list.at(currentIndex)->setPinned(true);
177 QModelIndex modelIndex = this->index(currentIndex);
178 Q_EMIT dataChanged(modelIndex, modelIndex);
179 } else {
180 move(currentIndex, index);
181 }
182 } else {
183 beginInsertRows(QModelIndex(), index, index);
184 m_list.insert(index, new MockLauncherItem(appId,
185 appId + ".desktop",
186 appId,
187 appId + ".png"));
188 m_list.at(index)->setPinned(true);
189 endInsertRows();
190 }
193void MockLauncherModel::requestRemove(const QString &appId)
195 int index = findApp(appId);
196 if (index >= 0) {
197 beginRemoveRows(QModelIndex(), index, 0);
198 MockLauncherItem * item = m_list.takeAt(index);
199 item->setRunning(false);
200 item->deleteLater();
201 endRemoveRows();
202 }
205void MockLauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
207 Q_EMIT quickListTriggered(appId, actionIndex);
210int MockLauncherModel::findApp(const QString &appId)
212 for (int i = 0; i < m_list.count(); ++i) {
213 MockLauncherItem *item = m_list.at(i);
214 if (item->appId() == appId) {
215 return i;
216 }
217 }
218 return -1;
221void MockLauncherModel::setProgress(const QString &appId, int progress)
223 int index = findApp(appId);
224 if (index >= 0) {
225 m_list.at(index)->setProgress(progress);
226 QModelIndex modelIndex = this->index(index);
227 Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RoleProgress);
228 }
231void MockLauncherModel::setUser(const QString &username)
233 Q_UNUSED(username)
234 // TODO: implement this...
237QString MockLauncherModel::getUrlForAppId(const QString &appId) const
239 return "application:///" + appId + ".desktop";
242void MockLauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *applicationManager)
244 Q_UNUSED(applicationManager)
247bool MockLauncherModel::onlyPinned() const
249 return false;
252void MockLauncherModel::setOnlyPinned(bool onlyPinned)
254 Q_UNUSED(onlyPinned)
257void MockLauncherModel::emitHint()
259 Q_EMIT hint();
262void MockLauncherModel::setCount(const QString &appId, int count)
264 int index = findApp(appId);
265 if (index >= 0) {
266 m_list.at(index)->setCount(count);
267 QModelIndex modelIndex = this->index(index);
268 Q_EMIT dataChanged(modelIndex, modelIndex);
269 }
272void MockLauncherModel::setCountVisible(const QString &appId, bool countVisible)
274 int index = findApp(appId);
275 if (index >= 0) {
276 m_list.at(index)->setCountVisible(countVisible);
277 QModelIndex modelIndex = this->index(index);
278 Q_EMIT dataChanged(modelIndex, modelIndex);
279 }
282unity::shell::application::ApplicationManagerInterface *MockLauncherModel::applicationManager() const
284 return nullptr;