on e198a75 help sign in
unity8/tests/mocks/Unity/Launcher/MockLauncherModel.cpp
272 lines 8.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->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->setAlerting(false);
48 m_list.append(item);
49 item = new MockLauncherItem("twitter-webapp", "/usr/share/applications/twitter-webapp.desktop", "Twitter", "twitter", this);
50 item->setCount(12);
51 item->setCountVisible(true);
52 item->setAlerting(false);
53 item->setPinned(true);
54 m_list.append(item);
55 item = new MockLauncherItem("gmail-webapp", "/usr/share/applications/gmail-webapp.desktop", "GMail", "gmail", this);
56 item->setCount(123);
57 item->setCountVisible(true);
58 item->setAlerting(false);
59 m_list.append(item);
60 item = new MockLauncherItem("ubuntu-weather-app", "/usr/share/applications/ubuntu-weather-app.desktop", "Weather", "weather", this);
61 item->setCount(1234567890);
62 item->setCountVisible(true);
63 item->setAlerting(false);
64 item->setPinned(true);
65 m_list.append(item);
66 item = new MockLauncherItem("notes-app", "/usr/share/applications/notes-app.desktop", "Notepad", "notepad", this);
67 item->setProgress(50);
68 item->setCount(5);
69 item->setCountVisible(true);
70 item->setAlerting(false);
71 item->setFocused(true);
72 item->setPinned(true);
73 m_list.append(item);
74 item = new MockLauncherItem("calendar-app", "/usr/share/applications/calendar-app.desktop","Calendar", "calendar", this);
75 item->setPinned(true);
76 m_list.append(item);
79MockLauncherModel::~MockLauncherModel()
83int MockLauncherModel::rowCount(const QModelIndex& parent) const
85 Q_UNUSED(parent)
86 return m_list.count();
89QVariant MockLauncherModel::data(const QModelIndex& index, int role) const
91 LauncherItemInterface *item = m_list.at(index.row());
92 switch(role)
93 {
94 case RoleAppId:
95 return item->appId();
96 case RoleName:
97 return item->name();
98 case RoleIcon:
99 return item->icon();
100 case RolePinned:
101 return item->pinned();
102 case RoleRunning:
103 return item->running();
104 case RoleRecent:
105 return item->recent();
106 case RoleProgress:
107 return item->progress();
108 case RoleCount:
109 return item->count();
110 case RoleCountVisible:
111 return item->countVisible();
112 case RoleFocused:
113 return item->focused();
114 case RoleAlerting:
115 return item->alerting();
116 }
118 return QVariant();
121unity::shell::launcher::LauncherItemInterface *MockLauncherModel::get(int index) const
123 if (index < 0 || index >= m_list.count())
124 {
125 return 0;
126 }
127 return m_list.at(index);
130void MockLauncherModel::setAlerting(const QString &appId, bool alerting) {
131 int index = findApp(appId);
132 if (index >= 0) {
133 QModelIndex modelIndex = this->index(index);
134 MockLauncherItem *item = m_list.at(index);
135 if (!item->focused()) {
136 item->setAlerting(alerting);
137 Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RoleAlerting);
138 }
139 }
142void MockLauncherModel::move(int oldIndex, int newIndex)
144 // Make sure its not moved outside the lists
145 if (newIndex < 0) {
146 newIndex = 0;
147 }
148 if (newIndex >= m_list.count()) {
149 newIndex = m_list.count()-1;
150 }
152 // Nothing to do?
153 if (oldIndex == newIndex) {
154 return;
155 }
157 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
158 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
159 // adjust the model's index by +1 in case we're moving upwards
160 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
162 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
163 m_list.move(oldIndex, newIndex);
164 endMoveRows();
166 pin(m_list.at(newIndex)->appId());
169void MockLauncherModel::pin(const QString &appId, int index)
171 int currentIndex = findApp(appId);
173 if (currentIndex >= 0) {
174 if (index == -1 || index == currentIndex) {
175 m_list.at(currentIndex)->setPinned(true);
176 QModelIndex modelIndex = this->index(currentIndex);
177 Q_EMIT dataChanged(modelIndex, modelIndex);
178 } else {
179 move(currentIndex, index);
180 }
181 } else {
182 beginInsertRows(QModelIndex(), index, index);
183 m_list.insert(index, new MockLauncherItem(appId,
184 appId + ".desktop",
185 appId,
186 appId + ".png"));
187 m_list.at(index)->setPinned(true);
188 endInsertRows();
189 }
192void MockLauncherModel::requestRemove(const QString &appId)
194 int index = findApp(appId);
195 if (index >= 0) {
196 beginRemoveRows(QModelIndex(), index, 0);
197 m_list.takeAt(index)->deleteLater();
198 endRemoveRows();
199 }
202void MockLauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
204 Q_EMIT quickListTriggered(appId, actionIndex);
207int MockLauncherModel::findApp(const QString &appId)
209 for (int i = 0; i < m_list.count(); ++i) {
210 MockLauncherItem *item = m_list.at(i);
211 if (item->appId() == appId) {
212 return i;
213 }
214 }
215 return -1;
218void MockLauncherModel::setUser(const QString &username)
220 Q_UNUSED(username)
221 // TODO: implement this...
224QString MockLauncherModel::getUrlForAppId(const QString &appId) const
226 return "application:///" + appId + ".desktop";
229void MockLauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *applicationManager)
231 Q_UNUSED(applicationManager)
234bool MockLauncherModel::onlyPinned() const
236 return false;
239void MockLauncherModel::setOnlyPinned(bool onlyPinned)
241 Q_UNUSED(onlyPinned)
244void MockLauncherModel::emitHint()
246 Q_EMIT hint();
249void MockLauncherModel::setCount(const QString &appId, int count)
251 int index = findApp(appId);
252 if (index >= 0) {
253 m_list.at(index)->setCount(count);
254 QModelIndex modelIndex = this->index(index);
255 Q_EMIT dataChanged(modelIndex, modelIndex);
256 }
259void MockLauncherModel::setCountVisible(const QString &appId, bool countVisible)
261 int index = findApp(appId);
262 if (index >= 0) {
263 m_list.at(index)->setCountVisible(countVisible);
264 QModelIndex modelIndex = this->index(index);
265 Q_EMIT dataChanged(modelIndex, modelIndex);
266 }
269unity::shell::application::ApplicationManagerInterface *MockLauncherModel::applicationManager() const
271 return nullptr;