on 68ddbaf help sign in
unity8/tests/mocks/Unity/Launcher/MockLauncherModel.cpp
232 lines 7.3 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 m_list.append(item);
38 item = new MockLauncherItem("music-app", "/usr/share/applications/music-app.desktop", "Music", "soundcloud", this);
39 m_list.append(item);
40 item = new MockLauncherItem("facebook-webapp", "/usr/share/applications/facebook-webapp.desktop", "Facebook", "facebook", this);
41 item->setProgress(150);
42 m_list.append(item);
43 item = new MockLauncherItem("webbrowser-app", "/usr/share/applications/webbrowser-app.desktop", "Browser", "browser", this);
44 item->setCount(1);
45 item->setCountVisible(true);
46 m_list.append(item);
47 item = new MockLauncherItem("twitter-webapp", "/usr/share/applications/twitter-webapp.desktop", "Twitter", "twitter", this);
48 item->setCount(12);
49 item->setCountVisible(true);
50 item->setPinned(true);
51 m_list.append(item);
52 item = new MockLauncherItem("gmail-webapp", "/usr/share/applications/gmail-webapp.desktop", "GMail", "gmail", this);
53 item->setCount(123);
54 item->setCountVisible(true);
55 m_list.append(item);
56 item = new MockLauncherItem("ubuntu-weather-app", "/usr/share/applications/ubuntu-weather-app.desktop", "Weather", "weather", this);
57 item->setCount(1234567890);
58 item->setCountVisible(true);
59 item->setPinned(true);
60 m_list.append(item);
61 item = new MockLauncherItem("notes-app", "/usr/share/applications/notes-app.desktop", "Notepad", "notepad", this);
62 item->setProgress(50);
63 item->setCount(5);
64 item->setCountVisible(true);
65 item->setFocused(true);
66 item->setPinned(true);
67 m_list.append(item);
68 item = new MockLauncherItem("calendar-app", "/usr/share/applications/calendar-app.desktop","Calendar", "calendar", this);
69 item->setPinned(true);
70 m_list.append(item);
73MockLauncherModel::~MockLauncherModel()
77int MockLauncherModel::rowCount(const QModelIndex& parent) const
79 Q_UNUSED(parent)
80 return m_list.count();
83QVariant MockLauncherModel::data(const QModelIndex& index, int role) const
85 LauncherItemInterface *item = m_list.at(index.row());
86 switch(role)
87 {
88 case RoleAppId:
89 return item->appId();
90 case RoleName:
91 return item->name();
92 case RoleIcon:
93 return item->icon();
94 case RolePinned:
95 return item->pinned();
96 case RoleRunning:
97 return item->running();
98 case RoleRecent:
99 return item->recent();
100 case RoleProgress:
101 return item->progress();
102 case RoleCount:
103 return item->count();
104 case RoleCountVisible:
105 return item->countVisible();
106 case RoleFocused:
107 return item->focused();
108 }
110 return QVariant();
113unity::shell::launcher::LauncherItemInterface *MockLauncherModel::get(int index) const
115 if (index < 0 || index >= m_list.count())
116 {
117 return 0;
118 }
119 return m_list.at(index);
122void MockLauncherModel::move(int oldIndex, int newIndex)
124 // Make sure its not moved outside the lists
125 if (newIndex < 0) {
126 newIndex = 0;
127 }
128 if (newIndex >= m_list.count()) {
129 newIndex = m_list.count()-1;
130 }
132 // Nothing to do?
133 if (oldIndex == newIndex) {
134 return;
135 }
137 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
138 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
139 // adjust the model's index by +1 in case we're moving upwards
140 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
142 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
143 m_list.move(oldIndex, newIndex);
144 endMoveRows();
146 pin(m_list.at(newIndex)->appId());
149void MockLauncherModel::pin(const QString &appId, int index)
151 int currentIndex = findApp(appId);
153 if (currentIndex >= 0) {
154 if (index == -1 || index == currentIndex) {
155 m_list.at(currentIndex)->setPinned(true);
156 QModelIndex modelIndex = this->index(currentIndex);
157 Q_EMIT dataChanged(modelIndex, modelIndex);
158 } else {
159 move(currentIndex, index);
160 }
161 } else {
162 beginInsertRows(QModelIndex(), index, index);
163 m_list.insert(index, new MockLauncherItem(appId,
164 appId + ".desktop",
165 appId,
166 appId + ".png"));
167 m_list.at(index)->setPinned(true);
168 endInsertRows();
169 }
172void MockLauncherModel::requestRemove(const QString &appId)
174 int index = findApp(appId);
175 if (index >= 0) {
176 beginRemoveRows(QModelIndex(), index, 0);
177 m_list.takeAt(index)->deleteLater();
178 endRemoveRows();
179 }
182void MockLauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
184 Q_EMIT quickListTriggered(appId, actionIndex);
187int MockLauncherModel::findApp(const QString &appId)
189 for (int i = 0; i < m_list.count(); ++i) {
190 MockLauncherItem *item = m_list.at(i);
191 if (item->appId() == appId) {
192 return i;
193 }
194 }
195 return -1;
198void MockLauncherModel::setUser(const QString &username)
200 Q_UNUSED(username)
201 // TODO: implement this...
204QString MockLauncherModel::getUrlForAppId(const QString &appId) const
206 return "application:///" + appId + ".desktop";
209void MockLauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *applicationManager)
211 Q_UNUSED(applicationManager)
214bool MockLauncherModel::onlyPinned() const
216 return false;
219void MockLauncherModel::setOnlyPinned(bool onlyPinned)
221 Q_UNUSED(onlyPinned)
224void MockLauncherModel::emitHint()
226 Q_EMIT hint();
229unity::shell::application::ApplicationManagerInterface *MockLauncherModel::applicationManager() const
231 return nullptr;