| 1 |
/* |
| 2 |
* Copyright 2013, 2015 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 <[email protected]> |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "MockLauncherModel.h" |
| 21 |
#include "MockLauncherItem.h" |
| 22 |
|
| 23 |
MockLauncherModel::MockLauncherModel(QObject* parent): LauncherModelInterface(parent) |
| 24 |
{ |
| 25 |
MockLauncherItem *item = new MockLauncherItem("lomiri-dialer-app", "/usr/share/applications/lomiri-dialer-app.desktop", "Dialer", "lomiri-dialer-app", this); |
| 26 |
item->setProgress(0); |
| 27 |
item->setPinned(true); |
| 28 |
item->setSurfaceCount(1); |
| 29 |
item->setRunning(true); |
| 30 |
item->setFocused(true); |
| 31 |
m_list.append(item); |
| 32 |
item = new MockLauncherItem("camera-app", "/usr/share/applications/camera-app.desktop", "Camera", "camera", this); |
| 33 |
item->setProgress(10); |
| 34 |
item->setPinned(true); |
| 35 |
m_list.append(item); |
| 36 |
item = new MockLauncherItem("camera-app2", "/usr/share/applications/camera-app2.desktop", "Camera2", "camera", this); |
| 37 |
item->setPinned(true); |
| 38 |
m_list.append(item); |
| 39 |
item = new MockLauncherItem("camera-app3", "/usr/share/applications/camera-app2.desktop", "Camera2", "camera", this); |
| 40 |
item->setPinned(true); |
| 41 |
m_list.append(item); |
| 42 |
item = new MockLauncherItem("gallery-app", "/usr/share/applications/gallery-app.desktop", "Gallery", "gallery", this); |
| 43 |
item->setProgress(50); |
| 44 |
item->setCountVisible(true); |
| 45 |
item->setRunning(true); |
| 46 |
item->setSurfaceCount(2); |
| 47 |
item->setAlerting(false); |
| 48 |
m_list.append(item); |
| 49 |
item = new MockLauncherItem("music-app", "/usr/share/applications/music-app.desktop", "Music", "soundcloud", this); |
| 50 |
m_list.append(item); |
| 51 |
item = new MockLauncherItem("facebook-webapp", "/usr/share/applications/facebook-webapp.desktop", "Facebook", "facebook", this); |
| 52 |
item->setProgress(150); |
| 53 |
m_list.append(item); |
| 54 |
item = new MockLauncherItem("morph-browser", "/usr/share/applications/morph-browser.desktop", "Browser", "browser", this); |
| 55 |
item->setSurfaceCount(5); |
| 56 |
item->setCount(1); |
| 57 |
item->setCountVisible(true); |
| 58 |
item->setRunning(true); |
| 59 |
item->setAlerting(false); |
| 60 |
m_list.append(item); |
| 61 |
item = new MockLauncherItem("twitter-webapp", "/usr/share/applications/twitter-webapp.desktop", "Twitter", "twitter", this); |
| 62 |
item->setCount(12); |
| 63 |
item->setCountVisible(true); |
| 64 |
item->setAlerting(false); |
| 65 |
item->setPinned(true); |
| 66 |
m_list.append(item); |
| 67 |
item = new MockLauncherItem("gmail-webapp", "/usr/share/applications/gmail-webapp.desktop", "GMail", "gmail", this); |
| 68 |
item->setCount(123); |
| 69 |
item->setCountVisible(true); |
| 70 |
item->setAlerting(false); |
| 71 |
m_list.append(item); |
| 72 |
item = new MockLauncherItem("lomiri-weather-app", "/usr/share/applications/lomiri-weather-app.desktop", "Weather", "weather", this); |
| 73 |
item->setCount(1234567890); |
| 74 |
item->setCountVisible(true); |
| 75 |
item->setAlerting(false); |
| 76 |
item->setPinned(true); |
| 77 |
m_list.append(item); |
| 78 |
item = new MockLauncherItem("notes-app", "/usr/share/applications/notes-app.desktop", "Notepad", "notepad", this); |
| 79 |
item->setProgress(50); |
| 80 |
item->setCount(5); |
| 81 |
item->setCountVisible(true); |
| 82 |
item->setAlerting(false); |
| 83 |
item->setFocused(true); |
| 84 |
item->setPinned(true); |
| 85 |
m_list.append(item); |
| 86 |
item = new MockLauncherItem("calendar-app", "/usr/share/applications/calendar-app.desktop","Calendar", "calendar", this); |
| 87 |
item->setPinned(true); |
| 88 |
m_list.append(item); |
| 89 |
item = new MockLauncherItem("libreoffice", "/usr/share/applications/libreoffice.desktop","Libre Office", "libreoffice", this); |
| 90 |
item->setPinned(true); |
| 91 |
m_list.append(item); |
| 92 |
} |
| 93 |
|
| 94 |
MockLauncherModel::~MockLauncherModel() |
| 95 |
{ |
| 96 |
} |
| 97 |
|
| 98 |
int MockLauncherModel::rowCount(const QModelIndex& parent) const |
| 99 |
{ |
| 100 |
Q_UNUSED(parent) |
| 101 |
return m_list.count(); |
| 102 |
} |
| 103 |
|
| 104 |
QVariant MockLauncherModel::data(const QModelIndex& index, int role) const |
| 105 |
{ |
| 106 |
LauncherItemInterface *item = m_list.at(index.row()); |
| 107 |
switch(role) |
| 108 |
{ |
| 109 |
case RoleAppId: |
| 110 |
return item->appId(); |
| 111 |
case RoleName: |
| 112 |
return item->name(); |
| 113 |
case RoleIcon: |
| 114 |
return item->icon(); |
| 115 |
case RolePinned: |
| 116 |
return item->pinned(); |
| 117 |
case RoleRunning: |
| 118 |
return item->running(); |
| 119 |
case RoleRecent: |
| 120 |
return item->recent(); |
| 121 |
case RoleProgress: |
| 122 |
return item->progress(); |
| 123 |
case RoleCount: |
| 124 |
return item->count(); |
| 125 |
case RoleCountVisible: |
| 126 |
return item->countVisible(); |
| 127 |
case RoleFocused: |
| 128 |
return item->focused(); |
| 129 |
case RoleAlerting: |
| 130 |
return item->alerting(); |
| 131 |
case RoleSurfaceCount: |
| 132 |
return item->surfaceCount(); |
| 133 |
} |
| 134 |
|
| 135 |
return QVariant(); |
| 136 |
} |
| 137 |
|
| 138 |
lomiri::shell::launcher::LauncherItemInterface *MockLauncherModel::get(int index) const |
| 139 |
{ |
| 140 |
if (index < 0 || index >= m_list.count()) |
| 141 |
{ |
| 142 |
return 0; |
| 143 |
} |
| 144 |
return m_list.at(index); |
| 145 |
} |
| 146 |
|
| 147 |
void MockLauncherModel::setAlerting(const QString &appId, bool alerting) { |
| 148 |
int index = findApp(appId); |
| 149 |
if (index >= 0) { |
| 150 |
QModelIndex modelIndex = this->index(index); |
| 151 |
MockLauncherItem *item = m_list.at(index); |
| 152 |
if (!item->focused()) { |
| 153 |
item->setAlerting(alerting); |
| 154 |
Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RoleAlerting); |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
void MockLauncherModel::move(int oldIndex, int newIndex) |
| 160 |
{ |
| 161 |
// Make sure its not moved outside the lists |
| 162 |
if (newIndex < 0) { |
| 163 |
newIndex = 0; |
| 164 |
} |
| 165 |
if (newIndex >= m_list.count()) { |
| 166 |
newIndex = m_list.count()-1; |
| 167 |
} |
| 168 |
|
| 169 |
// Nothing to do? |
| 170 |
if (oldIndex == newIndex) { |
| 171 |
return; |
| 172 |
} |
| 173 |
|
| 174 |
// QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/ |
| 175 |
// While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list |
| 176 |
// adjust the model's index by +1 in case we're moving upwards |
| 177 |
int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex; |
| 178 |
|
| 179 |
beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex); |
| 180 |
m_list.move(oldIndex, newIndex); |
| 181 |
endMoveRows(); |
| 182 |
|
| 183 |
pin(m_list.at(newIndex)->appId()); |
| 184 |
} |
| 185 |
|
| 186 |
void MockLauncherModel::pin(const QString &appId, int index) |
| 187 |
{ |
| 188 |
int currentIndex = findApp(appId); |
| 189 |
|
| 190 |
if (currentIndex >= 0) { |
| 191 |
if (index == -1 || index == currentIndex) { |
| 192 |
m_list.at(currentIndex)->setPinned(true); |
| 193 |
QModelIndex modelIndex = this->index(currentIndex); |
| 194 |
Q_EMIT dataChanged(modelIndex, modelIndex); |
| 195 |
} else { |
| 196 |
move(currentIndex, index); |
| 197 |
} |
| 198 |
} else { |
| 199 |
beginInsertRows(QModelIndex(), index, index); |
| 200 |
m_list.insert(index, new MockLauncherItem(appId, |
| 201 |
appId + ".desktop", |
| 202 |
appId, |
| 203 |
appId + ".png")); |
| 204 |
m_list.at(index)->setPinned(true); |
| 205 |
endInsertRows(); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
void MockLauncherModel::requestRemove(const QString &appId) |
| 210 |
{ |
| 211 |
int index = findApp(appId); |
| 212 |
if (index >= 0) { |
| 213 |
beginRemoveRows(QModelIndex(), index, 0); |
| 214 |
MockLauncherItem * item = m_list.takeAt(index); |
| 215 |
item->setRunning(false); |
| 216 |
item->deleteLater(); |
| 217 |
endRemoveRows(); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
void MockLauncherModel::quickListActionInvoked(const QString &appId, int actionIndex) |
| 222 |
{ |
| 223 |
Q_EMIT quickListTriggered(appId, actionIndex); |
| 224 |
} |
| 225 |
|
| 226 |
int MockLauncherModel::findApp(const QString &appId) |
| 227 |
{ |
| 228 |
for (int i = 0; i < m_list.count(); ++i) { |
| 229 |
MockLauncherItem *item = m_list.at(i); |
| 230 |
if (item->appId() == appId) { |
| 231 |
return i; |
| 232 |
} |
| 233 |
} |
| 234 |
return -1; |
| 235 |
} |
| 236 |
|
| 237 |
void MockLauncherModel::setProgress(const QString &appId, int progress) |
| 238 |
{ |
| 239 |
int index = findApp(appId); |
| 240 |
if (index >= 0) { |
| 241 |
m_list.at(index)->setProgress(progress); |
| 242 |
QModelIndex modelIndex = this->index(index); |
| 243 |
Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RoleProgress); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
void MockLauncherModel::setUser(const QString &username) |
| 248 |
{ |
| 249 |
Q_UNUSED(username) |
| 250 |
// TODO: implement this... |
| 251 |
} |
| 252 |
|
| 253 |
QString MockLauncherModel::getUrlForAppId(const QString &appId) const |
| 254 |
{ |
| 255 |
return "application:///" + appId + ".desktop"; |
| 256 |
} |
| 257 |
|
| 258 |
void MockLauncherModel::setApplicationManager(lomiri::shell::application::ApplicationManagerInterface *applicationManager) |
| 259 |
{ |
| 260 |
Q_UNUSED(applicationManager) |
| 261 |
} |
| 262 |
|
| 263 |
bool MockLauncherModel::onlyPinned() const |
| 264 |
{ |
| 265 |
return false; |
| 266 |
} |
| 267 |
|
| 268 |
void MockLauncherModel::setOnlyPinned(bool onlyPinned) |
| 269 |
{ |
| 270 |
Q_UNUSED(onlyPinned) |
| 271 |
} |
| 272 |
|
| 273 |
void MockLauncherModel::emitHint() |
| 274 |
{ |
| 275 |
Q_EMIT hint(); |
| 276 |
} |
| 277 |
|
| 278 |
void MockLauncherModel::setCount(const QString &appId, int count) |
| 279 |
{ |
| 280 |
int index = findApp(appId); |
| 281 |
if (index >= 0) { |
| 282 |
m_list.at(index)->setCount(count); |
| 283 |
QModelIndex modelIndex = this->index(index); |
| 284 |
Q_EMIT dataChanged(modelIndex, modelIndex); |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
void MockLauncherModel::setCountVisible(const QString &appId, bool countVisible) |
| 289 |
{ |
| 290 |
int index = findApp(appId); |
| 291 |
if (index >= 0) { |
| 292 |
m_list.at(index)->setCountVisible(countVisible); |
| 293 |
QModelIndex modelIndex = this->index(index); |
| 294 |
Q_EMIT dataChanged(modelIndex, modelIndex); |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
lomiri::shell::application::ApplicationManagerInterface *MockLauncherModel::applicationManager() const |
| 299 |
{ |
| 300 |
return nullptr; |
| 301 |
} |