on d07a47e help sign in
unity8/tests/mocks/Unity/Launcher/MockLauncherModel.cpp
146 lines 5.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("phone-app", "/usr/share/applications/phone-app.desktop", "Phone", "phone-app", this);
26 m_list.append(item);
27 item = new MockLauncherItem("camera-app", "/usr/share/applications/camera-app.desktop", "Camera", "camera", this);
28 m_list.append(item);
29 item = new MockLauncherItem("gallery-app", "/usr/share/applications/gallery-app.desktop", "Gallery", "gallery", this);
30 m_list.append(item);
31 item = new MockLauncherItem("facebook-webapp", "/usr/share/applications/facebook-webapp.desktop", "Facebook", "facebook", this);
32 m_list.append(item);
33 item = new MockLauncherItem("webbrowser-app", "/usr/share/applications/webbrowser-app.desktop", "Browser", "browser", this);
34 m_list.append(item);
35 item = new MockLauncherItem("twitter-webapp", "/usr/share/applications/twitter-webapp.desktop", "Twitter", "twitter", this);
36 m_list.append(item);
37 item = new MockLauncherItem("gmail-webapp", "/usr/share/applications/gmail-webapp.desktop", "GMail", "gmail", this);
38 m_list.append(item);
39 item = new MockLauncherItem("ubuntu-weather-app", "/usr/share/applications/ubuntu-weather-app.desktop", "Weather", "weather", this);
40 m_list.append(item);
41 item = new MockLauncherItem("notes-app", "/usr/share/applications/notes-app.desktop", "Notepad", "notepad", this);
42 m_list.append(item);
43 item = new MockLauncherItem("calendar-app", "/usr/share/applications/calendar-app.desktop","Calendar", "calendar", this);
44 m_list.append(item);
47MockLauncherModel::~MockLauncherModel()
51int MockLauncherModel::rowCount(const QModelIndex& parent) const
53 Q_UNUSED(parent)
54 return m_list.count();
57QVariant MockLauncherModel::data(const QModelIndex& index, int role) const
59 LauncherItemInterface *item = m_list.at(index.row());
60 switch(role)
61 {
62 case RoleDesktopFile:
63 return item->desktopFile();
64 case RoleName:
65 return item->name();
66 case RoleIcon:
67 return item->icon();
68 case RolePinned:
69 return item->pinned();
70 case RoleRunning:
71 return item->running();
72 case RoleRecent:
73 return item->recent();
74 case RoleProgress:
75 return item->progress();
76 case RoleCount:
77 return item->count();
78 }
80 return QVariant();
83unity::shell::launcher::LauncherItemInterface *MockLauncherModel::get(int index) const
85 if (index < 0 || index >= m_list.count())
86 {
87 return 0;
88 }
89 return m_list.at(index);
92void MockLauncherModel::move(int oldIndex, int newIndex)
94 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newIndex);
95 m_list.move(oldIndex, newIndex);
96 endMoveRows();
99void MockLauncherModel::pin(const QString &appId, int index)
101 int currentIndex = findApp(appId);
103 if (currentIndex >= 0) {
104 if (index == -1 || index == currentIndex) {
105 m_list.at(currentIndex)->setPinned(true);
106 QModelIndex modelIndex = this->index(currentIndex);
107 Q_EMIT dataChanged(modelIndex, modelIndex);
108 } else {
109 move(currentIndex, index);
110 }
111 } else {
112 beginInsertRows(QModelIndex(), index, index);
113 m_list.insert(index, new MockLauncherItem(appId,
114 appId + ".desktop",
115 appId,
116 appId + ".png"));
117 m_list.at(index)->setPinned(true);
118 endInsertRows();
119 }
122void MockLauncherModel::requestRemove(const QString &appId)
124 int index = findApp(appId);
125 if (index >= 0) {
126 beginRemoveRows(QModelIndex(), index, 0);
127 m_list.takeAt(index)->deleteLater();
128 endRemoveRows();
129 }
132void MockLauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
134 Q_EMIT quickListTriggered(appId, actionIndex);
137int MockLauncherModel::findApp(const QString &appId)
139 for (int i = 0; i < m_list.count(); ++i) {
140 MockLauncherItem *item = m_list.at(i);
141 if (item->appId() == appId) {
142 return i;
143 }
144 }
145 return -1;