on 1663e5c help sign in
unity8/tests/mocks/Unity/Launcher/MockLauncherModel.cpp
194 lines 6.4 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("phone-app", "/usr/share/applications/phone-app.desktop", "Phone", "phone-app", this);
26 item->setProgress(0);
27 m_list.append(item);
28 item->setFocused(true);
29 item = new MockLauncherItem("camera-app", "/usr/share/applications/camera-app.desktop", "Camera", "camera", this);
30 item->setProgress(10);
31 m_list.append(item);
32 item = new MockLauncherItem("gallery-app", "/usr/share/applications/gallery-app.desktop", "Gallery", "gallery", this);
33 item->setProgress(50);
34 m_list.append(item);
35 item = new MockLauncherItem("facebook-webapp", "/usr/share/applications/facebook-webapp.desktop", "Facebook", "facebook", this);
36 item->setProgress(150);
37 m_list.append(item);
38 item = new MockLauncherItem("webbrowser-app", "/usr/share/applications/webbrowser-app.desktop", "Browser", "browser", this);
39 item->setCount(1);
40 m_list.append(item);
41 item = new MockLauncherItem("twitter-webapp", "/usr/share/applications/twitter-webapp.desktop", "Twitter", "twitter", this);
42 item->setCount(12);
43 m_list.append(item);
44 item = new MockLauncherItem("gmail-webapp", "/usr/share/applications/gmail-webapp.desktop", "GMail", "gmail", this);
45 item->setCount(123);
46 m_list.append(item);
47 item = new MockLauncherItem("ubuntu-weather-app", "/usr/share/applications/ubuntu-weather-app.desktop", "Weather", "weather", this);
48 item->setCount(1234567890);
49 m_list.append(item);
50 item = new MockLauncherItem("notes-app", "/usr/share/applications/notes-app.desktop", "Notepad", "notepad", this);
51 item->setProgress(50);
52 item->setCount(5);
53 item->setFocused(true);
54 m_list.append(item);
55 item = new MockLauncherItem("calendar-app", "/usr/share/applications/calendar-app.desktop","Calendar", "calendar", this);
56 m_list.append(item);
59MockLauncherModel::~MockLauncherModel()
63int MockLauncherModel::rowCount(const QModelIndex& parent) const
65 Q_UNUSED(parent)
66 return m_list.count();
69QVariant MockLauncherModel::data(const QModelIndex& index, int role) const
71 LauncherItemInterface *item = m_list.at(index.row());
72 switch(role)
73 {
74 case RoleName:
75 return item->name();
76 case RoleIcon:
77 return item->icon();
78 case RolePinned:
79 return item->pinned();
80 case RoleRunning:
81 return item->running();
82 case RoleRecent:
83 return item->recent();
84 case RoleProgress:
85 return item->progress();
86 case RoleCount:
87 return item->count();
88 case RoleFocused:
89 return item->focused();
90 }
92 return QVariant();
95unity::shell::launcher::LauncherItemInterface *MockLauncherModel::get(int index) const
97 if (index < 0 || index >= m_list.count())
98 {
99 return 0;
100 }
101 return m_list.at(index);
104void MockLauncherModel::move(int oldIndex, int newIndex)
106 // Make sure its not moved outside the lists
107 if (newIndex < 0) {
108 newIndex = 0;
109 }
110 if (newIndex >= m_list.count()) {
111 newIndex = m_list.count()-1;
112 }
114 // Nothing to do?
115 if (oldIndex == newIndex) {
116 return;
117 }
119 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
120 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
121 // adjust the model's index by +1 in case we're moving upwards
122 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
124 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
125 m_list.move(oldIndex, newIndex);
126 endMoveRows();
128 pin(m_list.at(newIndex)->appId());
131void MockLauncherModel::pin(const QString &appId, int index)
133 int currentIndex = findApp(appId);
135 if (currentIndex >= 0) {
136 if (index == -1 || index == currentIndex) {
137 m_list.at(currentIndex)->setPinned(true);
138 QModelIndex modelIndex = this->index(currentIndex);
139 Q_EMIT dataChanged(modelIndex, modelIndex);
140 } else {
141 move(currentIndex, index);
142 }
143 } else {
144 beginInsertRows(QModelIndex(), index, index);
145 m_list.insert(index, new MockLauncherItem(appId,
146 appId + ".desktop",
147 appId,
148 appId + ".png"));
149 m_list.at(index)->setPinned(true);
150 endInsertRows();
151 }
154void MockLauncherModel::requestRemove(const QString &appId)
156 int index = findApp(appId);
157 if (index >= 0) {
158 beginRemoveRows(QModelIndex(), index, 0);
159 m_list.takeAt(index)->deleteLater();
160 endRemoveRows();
161 }
164void MockLauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
166 Q_EMIT quickListTriggered(appId, actionIndex);
169int MockLauncherModel::findApp(const QString &appId)
171 for (int i = 0; i < m_list.count(); ++i) {
172 MockLauncherItem *item = m_list.at(i);
173 if (item->appId() == appId) {
174 return i;
175 }
176 }
177 return -1;
180void MockLauncherModel::setUser(const QString &username)
182 Q_UNUSED(username)
183 // TODO: implement this...
186void MockLauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *applicationManager)
188 Q_UNUSED(applicationManager)
191unity::shell::application::ApplicationManagerInterface *MockLauncherModel::applicationManager() const
193 return nullptr;