on af18ef9 help sign in
unity8/tests/mocks/Unity/Launcher/MockLauncherModel.cpp
245 lines 7.6 kB history raw
1/*
2 * Copyright 2013 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"
23#include <iostream>
25MockLauncherModel::MockLauncherModel(QObject* parent): LauncherModelInterface(parent)
27 MockLauncherItem *item = new MockLauncherItem("dialer-app", "/usr/share/applications/dialer-app.desktop", "Dialer", "dialer-app", this);
28 item->setProgress(0);
29 item->setPinned(true);
30 item->setFocused(true);
31 item->setAlerting(true);
32 m_list.append(item);
33 item = new MockLauncherItem("camera-app", "/usr/share/applications/camera-app.desktop", "Camera", "camera", this);
34 item->setProgress(10);
35 item->setPinned(true);
36 item->setAlerting(true);
37 m_list.append(item);
38 item = new MockLauncherItem("gallery-app", "/usr/share/applications/gallery-app.desktop", "Gallery", "gallery", this);
39 item->setProgress(50);
40 item->setCountVisible(true);
41 m_list.append(item);
42 item = new MockLauncherItem("facebook-webapp", "/usr/share/applications/facebook-webapp.desktop", "Facebook", "facebook", this);
43 item->setProgress(150);
44 m_list.append(item);
45 item = new MockLauncherItem("webbrowser-app", "/usr/share/applications/webbrowser-app.desktop", "Browser", "browser", this);
46 item->setCount(1);
47 item->setCountVisible(true);
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->setPinned(true);
53 m_list.append(item);
54 item = new MockLauncherItem("gmail-webapp", "/usr/share/applications/gmail-webapp.desktop", "GMail", "gmail", this);
55 item->setCount(123);
56 item->setCountVisible(true);
57 m_list.append(item);
58 item = new MockLauncherItem("ubuntu-weather-app", "/usr/share/applications/ubuntu-weather-app.desktop", "Weather", "weather", this);
59 item->setCount(1234567890);
60 item->setCountVisible(true);
61 item->setPinned(true);
62 m_list.append(item);
63 item = new MockLauncherItem("notes-app", "/usr/share/applications/notes-app.desktop", "Notepad", "notepad", this);
64 item->setProgress(50);
65 item->setCount(5);
66 item->setCountVisible(true);
67 item->setFocused(true);
68 item->setPinned(true);
69 m_list.append(item);
70 item = new MockLauncherItem("calendar-app", "/usr/share/applications/calendar-app.desktop","Calendar", "calendar", this);
71 item->setPinned(true);
72 m_list.append(item);
75MockLauncherModel::~MockLauncherModel()
79int MockLauncherModel::rowCount(const QModelIndex& parent) const
81 Q_UNUSED(parent)
82 return m_list.count();
85QVariant MockLauncherModel::data(const QModelIndex& index, int role) const
87 LauncherItemInterface *item = m_list.at(index.row());
88 switch(role)
89 {
90 case RoleAppId:
91 return item->appId();
92 case RoleName:
93 return item->name();
94 case RoleIcon:
95 return item->icon();
96 case RolePinned:
97 return item->pinned();
98 case RoleRunning:
99 return item->running();
100 case RoleRecent:
101 return item->recent();
102 case RoleProgress:
103 return item->progress();
104 case RoleCount:
105 return item->count();
106 case RoleCountVisible:
107 return item->countVisible();
108 case RoleFocused:
109 return item->focused();
110 case RoleAlerting:
111 return item->alerting();
112 }
114 return QVariant();
117unity::shell::launcher::LauncherItemInterface *MockLauncherModel::get(int index) const
119 if (index < 0 || index >= m_list.count())
120 {
121 return 0;
122 }
123 return m_list.at(index);
126void MockLauncherModel::alert(const QString &appId, bool alerting) {
127 int index = findApp(appId);
128 if (index >= 0) {
129 std::cout << "MockLauncherModel::alert()" << std::endl;
130 MockLauncherItem *item = m_list.at(index);
131 item->setAlerting(alerting);
132 }
135void MockLauncherModel::move(int oldIndex, int newIndex)
137 // Make sure its not moved outside the lists
138 if (newIndex < 0) {
139 newIndex = 0;
140 }
141 if (newIndex >= m_list.count()) {
142 newIndex = m_list.count()-1;
143 }
145 // Nothing to do?
146 if (oldIndex == newIndex) {
147 return;
148 }
150 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
151 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
152 // adjust the model's index by +1 in case we're moving upwards
153 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
155 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
156 m_list.move(oldIndex, newIndex);
157 endMoveRows();
159 pin(m_list.at(newIndex)->appId());
162void MockLauncherModel::pin(const QString &appId, int index)
164 int currentIndex = findApp(appId);
166 if (currentIndex >= 0) {
167 if (index == -1 || index == currentIndex) {
168 m_list.at(currentIndex)->setPinned(true);
169 QModelIndex modelIndex = this->index(currentIndex);
170 Q_EMIT dataChanged(modelIndex, modelIndex);
171 } else {
172 move(currentIndex, index);
173 }
174 } else {
175 beginInsertRows(QModelIndex(), index, index);
176 m_list.insert(index, new MockLauncherItem(appId,
177 appId + ".desktop",
178 appId,
179 appId + ".png"));
180 m_list.at(index)->setPinned(true);
181 endInsertRows();
182 }
185void MockLauncherModel::requestRemove(const QString &appId)
187 int index = findApp(appId);
188 if (index >= 0) {
189 beginRemoveRows(QModelIndex(), index, 0);
190 m_list.takeAt(index)->deleteLater();
191 endRemoveRows();
192 }
195void MockLauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
197 Q_EMIT quickListTriggered(appId, actionIndex);
200int MockLauncherModel::findApp(const QString &appId)
202 for (int i = 0; i < m_list.count(); ++i) {
203 MockLauncherItem *item = m_list.at(i);
204 if (item->appId() == appId) {
205 return i;
206 }
207 }
208 return -1;
211void MockLauncherModel::setUser(const QString &username)
213 Q_UNUSED(username)
214 // TODO: implement this...
217QString MockLauncherModel::getUrlForAppId(const QString &appId) const
219 return "application:///" + appId + ".desktop";
222void MockLauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *applicationManager)
224 Q_UNUSED(applicationManager)
227bool MockLauncherModel::onlyPinned() const
229 return false;
232void MockLauncherModel::setOnlyPinned(bool onlyPinned)
234 Q_UNUSED(onlyPinned)
237void MockLauncherModel::emitHint()
239 Q_EMIT hint();
242unity::shell::application::ApplicationManagerInterface *MockLauncherModel::applicationManager() const
244 return nullptr;