on daa5708 help sign in
unity8/tests/mocks/Unity/Launcher/MockLauncherModel.cpp
230 lines 7.2 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 }
108 return QVariant();
111unity::shell::launcher::LauncherItemInterface *MockLauncherModel::get(int index) const
113 if (index < 0 || index >= m_list.count())
114 {
115 return 0;
116 }
117 return m_list.at(index);
120void MockLauncherModel::move(int oldIndex, int newIndex)
122 // Make sure its not moved outside the lists
123 if (newIndex < 0) {
124 newIndex = 0;
125 }
126 if (newIndex >= m_list.count()) {
127 newIndex = m_list.count()-1;
128 }
130 // Nothing to do?
131 if (oldIndex == newIndex) {
132 return;
133 }
135 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
136 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
137 // adjust the model's index by +1 in case we're moving upwards
138 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
140 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
141 m_list.move(oldIndex, newIndex);
142 endMoveRows();
144 pin(m_list.at(newIndex)->appId());
147void MockLauncherModel::pin(const QString &appId, int index)
149 int currentIndex = findApp(appId);
151 if (currentIndex >= 0) {
152 if (index == -1 || index == currentIndex) {
153 m_list.at(currentIndex)->setPinned(true);
154 QModelIndex modelIndex = this->index(currentIndex);
155 Q_EMIT dataChanged(modelIndex, modelIndex);
156 } else {
157 move(currentIndex, index);
158 }
159 } else {
160 beginInsertRows(QModelIndex(), index, index);
161 m_list.insert(index, new MockLauncherItem(appId,
162 appId + ".desktop",
163 appId,
164 appId + ".png"));
165 m_list.at(index)->setPinned(true);
166 endInsertRows();
167 }
170void MockLauncherModel::requestRemove(const QString &appId)
172 int index = findApp(appId);
173 if (index >= 0) {
174 beginRemoveRows(QModelIndex(), index, 0);
175 m_list.takeAt(index)->deleteLater();
176 endRemoveRows();
177 }
180void MockLauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
182 Q_EMIT quickListTriggered(appId, actionIndex);
185int MockLauncherModel::findApp(const QString &appId)
187 for (int i = 0; i < m_list.count(); ++i) {
188 MockLauncherItem *item = m_list.at(i);
189 if (item->appId() == appId) {
190 return i;
191 }
192 }
193 return -1;
196void MockLauncherModel::setUser(const QString &username)
198 Q_UNUSED(username)
199 // TODO: implement this...
202QString MockLauncherModel::getUrlForAppId(const QString &appId) const
204 return "application:///" + appId + ".desktop";
207void MockLauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *applicationManager)
209 Q_UNUSED(applicationManager)
212bool MockLauncherModel::onlyPinned() const
214 return false;
217void MockLauncherModel::setOnlyPinned(bool onlyPinned)
219 Q_UNUSED(onlyPinned)
222void MockLauncherModel::emitHint()
224 Q_EMIT hint();
227unity::shell::application::ApplicationManagerInterface *MockLauncherModel::applicationManager() const
229 return nullptr;