on 0bbce16 help sign in
unity8/tests/mocks/Unity/Launcher/MockLauncherModel.cpp
214 lines 7.0 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 m_list.append(item);
58 item = new MockLauncherItem("notes-app", "/usr/share/applications/notes-app.desktop", "Notepad", "notepad", this);
59 item->setProgress(50);
60 item->setCount(5);
61 item->setCountVisible(true);
62 item->setFocused(true);
63 item->setPinned(true);
64 m_list.append(item);
65 item = new MockLauncherItem("calendar-app", "/usr/share/applications/calendar-app.desktop","Calendar", "calendar", this);
66 item->setPinned(true);
67 m_list.append(item);
70MockLauncherModel::~MockLauncherModel()
74int MockLauncherModel::rowCount(const QModelIndex& parent) const
76 Q_UNUSED(parent)
77 return m_list.count();
80QVariant MockLauncherModel::data(const QModelIndex& index, int role) const
82 LauncherItemInterface *item = m_list.at(index.row());
83 switch(role)
84 {
85 case RoleAppId:
86 return item->appId();
87 case RoleName:
88 return item->name();
89 case RoleIcon:
90 return item->icon();
91 case RolePinned:
92 return item->pinned();
93 case RoleRunning:
94 return item->running();
95 case RoleRecent:
96 return item->recent();
97 case RoleProgress:
98 return item->progress();
99 case RoleCount:
100 return item->count();
101 case RoleCountVisible:
102 return item->countVisible();
103 case RoleFocused:
104 return item->focused();
105 }
107 return QVariant();
110unity::shell::launcher::LauncherItemInterface *MockLauncherModel::get(int index) const
112 if (index < 0 || index >= m_list.count())
113 {
114 return 0;
115 }
116 return m_list.at(index);
119void MockLauncherModel::move(int oldIndex, int newIndex)
121 // Make sure its not moved outside the lists
122 if (newIndex < 0) {
123 newIndex = 0;
124 }
125 if (newIndex >= m_list.count()) {
126 newIndex = m_list.count()-1;
127 }
129 // Nothing to do?
130 if (oldIndex == newIndex) {
131 return;
132 }
134 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
135 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
136 // adjust the model's index by +1 in case we're moving upwards
137 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
139 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
140 m_list.move(oldIndex, newIndex);
141 endMoveRows();
143 pin(m_list.at(newIndex)->appId());
146void MockLauncherModel::pin(const QString &appId, int index)
148 int currentIndex = findApp(appId);
150 if (currentIndex >= 0) {
151 if (index == -1 || index == currentIndex) {
152 m_list.at(currentIndex)->setPinned(true);
153 QModelIndex modelIndex = this->index(currentIndex);
154 Q_EMIT dataChanged(modelIndex, modelIndex);
155 } else {
156 move(currentIndex, index);
157 }
158 } else {
159 beginInsertRows(QModelIndex(), index, index);
160 m_list.insert(index, new MockLauncherItem(appId,
161 appId + ".desktop",
162 appId,
163 appId + ".png"));
164 m_list.at(index)->setPinned(true);
165 endInsertRows();
166 }
169void MockLauncherModel::requestRemove(const QString &appId)
171 int index = findApp(appId);
172 if (index >= 0) {
173 beginRemoveRows(QModelIndex(), index, 0);
174 m_list.takeAt(index)->deleteLater();
175 endRemoveRows();
176 }
179void MockLauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
181 Q_EMIT quickListTriggered(appId, actionIndex);
184int MockLauncherModel::findApp(const QString &appId)
186 for (int i = 0; i < m_list.count(); ++i) {
187 MockLauncherItem *item = m_list.at(i);
188 if (item->appId() == appId) {
189 return i;
190 }
191 }
192 return -1;
195void MockLauncherModel::setUser(const QString &username)
197 Q_UNUSED(username)
198 // TODO: implement this...
201QString MockLauncherModel::getUrlForAppId(const QString &appId) const
203 return "application:///" + appId + ".desktop";
206void MockLauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *applicationManager)
208 Q_UNUSED(applicationManager)
211unity::shell::application::ApplicationManagerInterface *MockLauncherModel::applicationManager() const
213 return nullptr;