on 0b48d4e help sign in
unity8/tests/mocks/Unity/Launcher/MockLauncherModel.cpp
152 lines 5.1 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");
26 m_list.append(item);
27 item = new MockLauncherItem("camera-app", "/usr/share/applications/camera-app.desktop", "Camera", "camera");
28 m_list.append(item);
29 item = new MockLauncherItem("gallery-app", "/usr/share/applications/gallery-app.desktop", "Gallery", "gallery");
30 m_list.append(item);
31 item = new MockLauncherItem("facebook-webapp", "/usr/share/applications/facebook-webapp.desktop", "Facebook", "facebook");
32 m_list.append(item);
33 item = new MockLauncherItem("webbrowser-app", "/usr/share/applications/webbrowser-app.desktop", "Browser", "browser");
34 m_list.append(item);
35 item = new MockLauncherItem("twitter-webapp", "/usr/share/applications/twitter-webapp.desktop", "Twitter", "twitter");
36 m_list.append(item);
37 item = new MockLauncherItem("gmail-webapp", "/usr/share/applications/gmail-webapp.desktop", "GMail", "gmail");
38 m_list.append(item);
39 item = new MockLauncherItem("ubuntu-weather-app", "/usr/share/applications/ubuntu-weather-app.desktop", "Weather", "weather");
40 m_list.append(item);
41 item = new MockLauncherItem("notes-app", "/usr/share/applications/notes-app.desktop", "Notepad", "notepad");
42 m_list.append(item);
43 item = new MockLauncherItem("calendar-app", "/usr/share/applications/calendar-app.desktop","Calendar", "calendar");
44 m_list.append(item);
47MockLauncherModel::~MockLauncherModel()
49 while (!m_list.empty())
50 {
51 m_list.takeFirst()->deleteLater();
52 }
55int MockLauncherModel::rowCount(const QModelIndex& parent) const
57 Q_UNUSED(parent)
58 return m_list.count();
61QVariant MockLauncherModel::data(const QModelIndex& index, int role) const
63 LauncherItemInterface *item = m_list.at(index.row());
64 switch(role)
65 {
66 case RoleDesktopFile:
67 return item->desktopFile();
68 case RoleName:
69 return item->name();
70 case RoleIcon:
71 return item->icon();
72 case RolePinned:
73 return item->pinned();
74 case RoleRunning:
75 return item->running();
76 case RoleRecent:
77 return item->recent();
78 case RoleProgress:
79 return item->progress();
80 case RoleCount:
81 return item->count();
82 }
84 return QVariant();
87unity::shell::launcher::LauncherItemInterface *MockLauncherModel::get(int index) const
89 if (index < 0 || index >= m_list.count())
90 {
91 return 0;
92 }
93 return m_list.at(index);
96void MockLauncherModel::move(int oldIndex, int newIndex)
98 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newIndex);
99 m_list.move(oldIndex, newIndex);
100 endMoveRows();
103void MockLauncherModel::pin(const QString &appId, int index)
105 int currentIndex = findApp(appId);
107 if (currentIndex >= 0) {
108 if (index == -1 || index == currentIndex) {
109 m_list.at(currentIndex)->setPinned(true);
110 QModelIndex modelIndex = this->index(currentIndex);
111 Q_EMIT dataChanged(modelIndex, modelIndex);
112 } else {
113 move(currentIndex, index);
114 }
115 } else {
116 beginInsertRows(QModelIndex(), index, index);
117 m_list.insert(index, new MockLauncherItem(appId,
118 appId + ".desktop",
119 appId,
120 appId + ".png"));
121 m_list.at(index)->setPinned(true);
122 endInsertRows();
123 }
126void MockLauncherModel::requestRemove(const QString &appId)
128 int index = findApp(appId);
129 if (index >= 0) {
130 beginRemoveRows(QModelIndex(), index, 0);
131 m_list.takeAt(index)->deleteLater();
132 endRemoveRows();
133 }
136void MockLauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
138 Q_UNUSED(appId)
139 Q_UNUSED(actionIndex)
140 // Nothing to mock yet...
143int MockLauncherModel::findApp(const QString &appId)
145 for (int i = 0; i < m_list.count(); ++i) {
146 MockLauncherItem *item = m_list.at(i);
147 if (item->appId() == appId) {
148 return i;
149 }
150 }
151 return -1;