on 4853336 help sign in
unity8/tests/mocks/Unity/Launcher/MockLauncherModel.cpp
244 lines 7.7 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"
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("facebook-webapp", "/usr/share/applications/facebook-webapp.desktop", "Facebook", "facebook", this);
39 item->setProgress(150);
40 m_list.append(item);
41 item = new MockLauncherItem("webbrowser-app", "/usr/share/applications/webbrowser-app.desktop", "Browser", "browser", this);
42 item->setCount(1);
43 item->setCountVisible(true);
44 m_list.append(item);
45 item = new MockLauncherItem("twitter-webapp", "/usr/share/applications/twitter-webapp.desktop", "Twitter", "twitter", this);
46 item->setCount(12);
47 item->setCountVisible(true);
48 item->setPinned(true);
49 m_list.append(item);
50 item = new MockLauncherItem("gmail-webapp", "/usr/share/applications/gmail-webapp.desktop", "GMail", "gmail", this);
51 item->setCount(123);
52 item->setCountVisible(true);
53 m_list.append(item);
54 item = new MockLauncherItem("ubuntu-weather-app", "/usr/share/applications/ubuntu-weather-app.desktop", "Weather", "weather", this);
55 item->setCount(1234567890);
56 item->setCountVisible(true);
57 item->setPinned(true);
58 m_list.append(item);
59 item = new MockLauncherItem("notes-app", "/usr/share/applications/notes-app.desktop", "Notepad", "notepad", this);
60 item->setProgress(50);
61 item->setCount(5);
62 item->setCountVisible(true);
63 item->setFocused(true);
64 item->setPinned(true);
65 m_list.append(item);
66 item = new MockLauncherItem("calendar-app", "/usr/share/applications/calendar-app.desktop","Calendar", "calendar", this);
67 item->setPinned(true);
68 m_list.append(item);
71MockLauncherModel::~MockLauncherModel()
75int MockLauncherModel::rowCount(const QModelIndex& parent) const
77 Q_UNUSED(parent)
78 return m_list.count();
81QVariant MockLauncherModel::data(const QModelIndex& index, int role) const
83 LauncherItemInterface *item = m_list.at(index.row());
84 switch(role)
85 {
86 case RoleAppId:
87 return item->appId();
88 case RoleName:
89 return item->name();
90 case RoleIcon:
91 return item->icon();
92 case RolePinned:
93 return item->pinned();
94 case RoleRunning:
95 return item->running();
96 case RoleRecent:
97 return item->recent();
98 case RoleProgress:
99 return item->progress();
100 case RoleCount:
101 return item->count();
102 case RoleCountVisible:
103 return item->countVisible();
104 case RoleFocused:
105 return item->focused();
106 case RoleAlerting:
107 return item->alerting();
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::alert(const QString &appId, bool alerting) {
123 int index = findApp(appId);
124 if (index >= 0) {
125 QModelIndex modelIndex = this->index(index);
126 MockLauncherItem *item = m_list.at(index);
127 if (!item->focused()) {
128 item->setAlerting(alerting);
129 Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RoleAlerting);
130 }
131 }
134void MockLauncherModel::move(int oldIndex, int newIndex)
136 // Make sure its not moved outside the lists
137 if (newIndex < 0) {
138 newIndex = 0;
139 }
140 if (newIndex >= m_list.count()) {
141 newIndex = m_list.count()-1;
142 }
144 // Nothing to do?
145 if (oldIndex == newIndex) {
146 return;
147 }
149 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
150 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
151 // adjust the model's index by +1 in case we're moving upwards
152 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
154 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
155 m_list.move(oldIndex, newIndex);
156 endMoveRows();
158 pin(m_list.at(newIndex)->appId());
161void MockLauncherModel::pin(const QString &appId, int index)
163 int currentIndex = findApp(appId);
165 if (currentIndex >= 0) {
166 if (index == -1 || index == currentIndex) {
167 m_list.at(currentIndex)->setPinned(true);
168 QModelIndex modelIndex = this->index(currentIndex);
169 Q_EMIT dataChanged(modelIndex, modelIndex);
170 } else {
171 move(currentIndex, index);
172 }
173 } else {
174 beginInsertRows(QModelIndex(), index, index);
175 m_list.insert(index, new MockLauncherItem(appId,
176 appId + ".desktop",
177 appId,
178 appId + ".png"));
179 m_list.at(index)->setPinned(true);
180 endInsertRows();
181 }
184void MockLauncherModel::requestRemove(const QString &appId)
186 int index = findApp(appId);
187 if (index >= 0) {
188 beginRemoveRows(QModelIndex(), index, 0);
189 m_list.takeAt(index)->deleteLater();
190 endRemoveRows();
191 }
194void MockLauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
196 Q_EMIT quickListTriggered(appId, actionIndex);
199int MockLauncherModel::findApp(const QString &appId)
201 for (int i = 0; i < m_list.count(); ++i) {
202 MockLauncherItem *item = m_list.at(i);
203 if (item->appId() == appId) {
204 return i;
205 }
206 }
207 return -1;
210void MockLauncherModel::setUser(const QString &username)
212 Q_UNUSED(username)
213 // TODO: implement this...
216QString MockLauncherModel::getUrlForAppId(const QString &appId) const
218 return "application:///" + appId + ".desktop";
221void MockLauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *applicationManager)
223 Q_UNUSED(applicationManager)
226bool MockLauncherModel::onlyPinned() const
228 return false;
231void MockLauncherModel::setOnlyPinned(bool onlyPinned)
233 Q_UNUSED(onlyPinned)
236void MockLauncherModel::emitHint()
238 Q_EMIT hint();
241unity::shell::application::ApplicationManagerInterface *MockLauncherModel::applicationManager() const
243 return nullptr;