on 129ee8b help sign in
unity8/tests/mocks/Unity/Launcher/MockLauncherModel.cpp
204 lines 6.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"
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 m_list.append(item);
37 item = new MockLauncherItem("facebook-webapp", "/usr/share/applications/facebook-webapp.desktop", "Facebook", "facebook", this);
38 item->setProgress(150);
39 m_list.append(item);
40 item = new MockLauncherItem("webbrowser-app", "/usr/share/applications/webbrowser-app.desktop", "Browser", "browser", this);
41 item->setCount(1);
42 m_list.append(item);
43 item = new MockLauncherItem("twitter-webapp", "/usr/share/applications/twitter-webapp.desktop", "Twitter", "twitter", this);
44 item->setCount(12);
45 item->setPinned(true);
46 m_list.append(item);
47 item = new MockLauncherItem("gmail-webapp", "/usr/share/applications/gmail-webapp.desktop", "GMail", "gmail", this);
48 item->setCount(123);
49 m_list.append(item);
50 item = new MockLauncherItem("ubuntu-weather-app", "/usr/share/applications/ubuntu-weather-app.desktop", "Weather", "weather", this);
51 item->setCount(1234567890);
52 m_list.append(item);
53 item = new MockLauncherItem("notes-app", "/usr/share/applications/notes-app.desktop", "Notepad", "notepad", this);
54 item->setProgress(50);
55 item->setCount(5);
56 item->setFocused(true);
57 item->setPinned(true);
58 m_list.append(item);
59 item = new MockLauncherItem("calendar-app", "/usr/share/applications/calendar-app.desktop","Calendar", "calendar", this);
60 item->setPinned(true);
61 m_list.append(item);
64MockLauncherModel::~MockLauncherModel()
68int MockLauncherModel::rowCount(const QModelIndex& parent) const
70 Q_UNUSED(parent)
71 return m_list.count();
74QVariant MockLauncherModel::data(const QModelIndex& index, int role) const
76 LauncherItemInterface *item = m_list.at(index.row());
77 switch(role)
78 {
79 case RoleName:
80 return item->name();
81 case RoleIcon:
82 return item->icon();
83 case RolePinned:
84 return item->pinned();
85 case RoleRunning:
86 return item->running();
87 case RoleRecent:
88 return item->recent();
89 case RoleProgress:
90 return item->progress();
91 case RoleCount:
92 return item->count();
93 case RoleFocused:
94 return item->focused();
95 }
97 return QVariant();
100unity::shell::launcher::LauncherItemInterface *MockLauncherModel::get(int index) const
102 if (index < 0 || index >= m_list.count())
103 {
104 return 0;
105 }
106 return m_list.at(index);
109void MockLauncherModel::move(int oldIndex, int newIndex)
111 // Make sure its not moved outside the lists
112 if (newIndex < 0) {
113 newIndex = 0;
114 }
115 if (newIndex >= m_list.count()) {
116 newIndex = m_list.count()-1;
117 }
119 // Nothing to do?
120 if (oldIndex == newIndex) {
121 return;
122 }
124 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
125 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
126 // adjust the model's index by +1 in case we're moving upwards
127 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
129 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
130 m_list.move(oldIndex, newIndex);
131 endMoveRows();
133 pin(m_list.at(newIndex)->appId());
136void MockLauncherModel::pin(const QString &appId, int index)
138 int currentIndex = findApp(appId);
140 if (currentIndex >= 0) {
141 if (index == -1 || index == currentIndex) {
142 m_list.at(currentIndex)->setPinned(true);
143 QModelIndex modelIndex = this->index(currentIndex);
144 Q_EMIT dataChanged(modelIndex, modelIndex);
145 } else {
146 move(currentIndex, index);
147 }
148 } else {
149 beginInsertRows(QModelIndex(), index, index);
150 m_list.insert(index, new MockLauncherItem(appId,
151 appId + ".desktop",
152 appId,
153 appId + ".png"));
154 m_list.at(index)->setPinned(true);
155 endInsertRows();
156 }
159void MockLauncherModel::requestRemove(const QString &appId)
161 int index = findApp(appId);
162 if (index >= 0) {
163 beginRemoveRows(QModelIndex(), index, 0);
164 m_list.takeAt(index)->deleteLater();
165 endRemoveRows();
166 }
169void MockLauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
171 Q_EMIT quickListTriggered(appId, actionIndex);
174int MockLauncherModel::findApp(const QString &appId)
176 for (int i = 0; i < m_list.count(); ++i) {
177 MockLauncherItem *item = m_list.at(i);
178 if (item->appId() == appId) {
179 return i;
180 }
181 }
182 return -1;
185void MockLauncherModel::setUser(const QString &username)
187 Q_UNUSED(username)
188 // TODO: implement this...
191QString MockLauncherModel::getUrlForAppId(const QString &appId) const
193 return "application:///" + appId + ".desktop";
196void MockLauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *applicationManager)
198 Q_UNUSED(applicationManager)
201unity::shell::application::ApplicationManagerInterface *MockLauncherModel::applicationManager() const
203 return nullptr;