on d1da2c0 help sign in
unity8/tests/mocks/Unity/Launcher/MockLauncherModel.cpp
191 lines 6.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("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 RoleDesktopFile:
75 return item->desktopFile();
76 case RoleName:
77 return item->name();
78 case RoleIcon:
79 return item->icon();
80 case RolePinned:
81 return item->pinned();
82 case RoleRunning:
83 return item->running();
84 case RoleRecent:
85 return item->recent();
86 case RoleProgress:
87 return item->progress();
88 case RoleCount:
89 return item->count();
90 case RoleFocused:
91 return item->focused();
92 }
94 return QVariant();
97unity::shell::launcher::LauncherItemInterface *MockLauncherModel::get(int index) const
99 if (index < 0 || index >= m_list.count())
100 {
101 return 0;
102 }
103 return m_list.at(index);
106void MockLauncherModel::move(int oldIndex, int newIndex)
108 // Make sure its not moved outside the lists
109 if (newIndex < 0) {
110 newIndex = 0;
111 }
112 if (newIndex >= m_list.count()) {
113 newIndex = m_list.count()-1;
114 }
116 // Nothing to do?
117 if (oldIndex == newIndex) {
118 return;
119 }
121 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
122 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
123 // adjust the model's index by +1 in case we're moving upwards
124 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
126 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
127 m_list.move(oldIndex, newIndex);
128 endMoveRows();
130 pin(m_list.at(newIndex)->appId());
133void MockLauncherModel::pin(const QString &appId, int index)
135 int currentIndex = findApp(appId);
137 if (currentIndex >= 0) {
138 if (index == -1 || index == currentIndex) {
139 m_list.at(currentIndex)->setPinned(true);
140 QModelIndex modelIndex = this->index(currentIndex);
141 Q_EMIT dataChanged(modelIndex, modelIndex);
142 } else {
143 move(currentIndex, index);
144 }
145 } else {
146 beginInsertRows(QModelIndex(), index, index);
147 m_list.insert(index, new MockLauncherItem(appId,
148 appId + ".desktop",
149 appId,
150 appId + ".png"));
151 m_list.at(index)->setPinned(true);
152 endInsertRows();
153 }
156void MockLauncherModel::requestRemove(const QString &appId)
158 int index = findApp(appId);
159 if (index >= 0) {
160 beginRemoveRows(QModelIndex(), index, 0);
161 m_list.takeAt(index)->deleteLater();
162 endRemoveRows();
163 }
166void MockLauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
168 Q_EMIT quickListTriggered(appId, actionIndex);
171int MockLauncherModel::findApp(const QString &appId)
173 for (int i = 0; i < m_list.count(); ++i) {
174 MockLauncherItem *item = m_list.at(i);
175 if (item->appId() == appId) {
176 return i;
177 }
178 }
179 return -1;
182void MockLauncherModel::setUser(const QString &username)
184 Q_UNUSED(username)
185 // TODO: implement this...
188void MockLauncherModel::applicationFocused(const QString &appId)
190 Q_UNUSED(appId)