Lomiri
Loading...
Searching...
No Matches
launchermodel.cpp
1/*
2 * Copyright 2013-2016 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
17#include "launchermodel.h"
18#include "launcheritem.h"
19#include "gsettings.h"
20#include "dbusinterface.h"
21#include "asadapter.h"
22#include "ualwrapper.h"
23
24#include <lomiri/shell/application/ApplicationInfoInterface.h>
25#include <lomiri/shell/application/MirSurfaceListInterface.h>
26#include <lomiri/shell/application/MirSurfaceInterface.h>
27
28#include <QDesktopServices>
29#include <QDebug>
30
31using namespace lomiri::shell::application;
32
33LauncherModel::LauncherModel(QObject *parent):
34 LauncherModelInterface(parent),
35 m_settings(new GSettings(this)),
36 m_dbusIface(new DBusInterface(this)),
37 m_asAdapter(new ASAdapter()),
38 m_appManager(nullptr)
39{
40 connect(m_dbusIface, &DBusInterface::countChanged, this, &LauncherModel::countChanged);
41 connect(m_dbusIface, &DBusInterface::countVisibleChanged, this, &LauncherModel::countVisibleChanged);
42 connect(m_dbusIface, &DBusInterface::progressChanged, this, &LauncherModel::progressChanged);
43 connect(m_dbusIface, &DBusInterface::refreshCalled, this, &LauncherModel::refresh);
44 connect(m_dbusIface, &DBusInterface::alertCalled, this, &LauncherModel::alert);
45
46 connect(m_settings, &GSettings::changed, this, &LauncherModel::refresh);
47
48 refresh();
49}
50
51LauncherModel::~LauncherModel()
52{
53 while (!m_list.empty()) {
54 m_list.takeFirst()->deleteLater();
55 }
56
57 delete m_asAdapter;
58}
59
60int LauncherModel::rowCount(const QModelIndex &parent) const
61{
62 Q_UNUSED(parent)
63 return m_list.count();
64}
65
66QVariant LauncherModel::data(const QModelIndex &index, int role) const
67{
68 LauncherItem *item = m_list.at(index.row());
69 switch(role) {
70 case RoleAppId:
71 return item->appId();
72 case RoleName:
73 return item->name();
74 case RoleIcon:
75 return item->icon();
76 case RolePinned:
77 return item->pinned();
78 case RoleCount:
79 return item->count();
80 case RoleCountVisible:
81 return item->countVisible();
82 case RoleProgress:
83 return item->progress();
84 case RoleFocused:
85 return item->focused();
86 case RoleAlerting:
87 return item->alerting();
88 case RoleRunning:
89 return item->running();
90 case RoleSurfaceCount:
91 return item->surfaceCount();
92 default:
93 qWarning() << Q_FUNC_INFO << "missing role, implement me";
94 return QVariant();
95 }
96
97 return QVariant();
98}
99
100lomiri::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
101{
102 if (index < 0 || index >= m_list.count()) {
103 return 0;
104 }
105 return m_list.at(index);
106}
107
108void LauncherModel::move(int oldIndex, int newIndex)
109{
110 // Make sure its not moved outside the lists
111 if (newIndex < 0) {
112 newIndex = 0;
113 }
114 if (newIndex >= m_list.count()) {
115 newIndex = m_list.count()-1;
116 }
117
118 // Nothing to do?
119 if (oldIndex == newIndex) {
120 return;
121 }
122
123 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
124 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
125 // adjust the model's index by +1 in case we're moving upwards
126 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
127
128 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
129 m_list.move(oldIndex, newIndex);
130 endMoveRows();
131
132 if (!m_list.at(newIndex)->pinned()) {
133 pin(m_list.at(newIndex)->appId());
134 } else {
135 storeAppList();
136 }
137}
138
139void LauncherModel::pin(const QString &appId, int index)
140{
141 int currentIndex = findApplication(appId);
142
143 if (currentIndex >= 0) {
144 if (index == -1 || index == currentIndex) {
145 m_list.at(currentIndex)->setPinned(true);
146 QModelIndex modelIndex = this->index(currentIndex);
147 Q_EMIT dataChanged(modelIndex, modelIndex, {RolePinned});
148 } else {
149 move(currentIndex, index);
150 // move() will store the list to the backend itself, so just exit at this point.
151 return;
152 }
153 } else {
154 if (index == -1) {
155 index = m_list.count();
156 }
157
158 UalWrapper::AppInfo appInfo = UalWrapper::getApplicationInfo(appId);
159 if (!appInfo.valid) {
160 qWarning() << "Can't pin application, appId not found:" << appId;
161 return;
162 }
163
164 beginInsertRows(QModelIndex(), index, index);
165 LauncherItem *item = new LauncherItem(appId,
166 appInfo.name,
167 appInfo.icon,
168 this);
169 item->setPinned(true);
170 item->setPopularity(appInfo.popularity);
171 m_list.insert(index, item);
172 endInsertRows();
173 }
174
175 storeAppList();
176}
177
178void LauncherModel::requestRemove(const QString &appId)
179{
180 unpin(appId);
181 storeAppList();
182}
183
184void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
185{
186 const int index = findApplication(appId);
187 if (index < 0) {
188 return;
189 }
190
191 LauncherItem *item = m_list.at(index);
192 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
193 if (model) {
194 const QString actionId = model->get(actionIndex).actionId();
195
196 // Check if this is one of the launcher actions we handle ourselves
197 if (actionId == QLatin1String("pin_item")) {
198 if (item->pinned()) {
199 requestRemove(appId);
200 } else {
201 pin(appId);
202 }
203 } else if (actionId == QStringLiteral("launch_item")) {
204 QDesktopServices::openUrl(getUrlForAppId(appId));
205 } else if (actionId == QStringLiteral("stop_item")) { // Quit
206 if (m_appManager) {
207 m_appManager->stopApplication(appId);
208 }
209 } else if (actionId.startsWith(QStringLiteral("surface_"))){
210 ApplicationInfoInterface *appInfo = m_appManager->findApplication(appId);
211 if (appInfo) {
212 for (int i = 0; i < appInfo->surfaceList()->count(); ++i) {
213 MirSurfaceInterface *iface = appInfo->surfaceList()->get(i);
214 QString id = actionId;
215 id.remove(QRegExp("^surface_"));
216 if (id == iface->persistentId()) {
217 iface->activate();
218 }
219 }
220 } else {
221 qWarning() << "App for" << appId << "not found in launcher. Cannot invoke quicklist action";
222 }
223 // Nope, we don't know this action, let the backend forward it to the application
224 } else {
225 // TODO: forward quicklist action to app, possibly via m_dbusIface
226 }
227 }
228}
229
230void LauncherModel::setUser(const QString &username)
231{
232 Q_UNUSED(username)
233}
234
235QString LauncherModel::getUrlForAppId(const QString &appId) const
236{
237 // appId is either an appId or a legacy app name. Let's find out which
238 if (appId.isEmpty()) {
239 return QString();
240 }
241
242 if (!appId.contains('_')) {
243 return "application:///" + appId + ".desktop";
244 }
245
246 QStringList parts = appId.split('_');
247 QString package = parts.value(0);
248 QString app = parts.value(1, QStringLiteral("first-listed-app"));
249 return "appid://" + package + "/" + app + "/current-user-version";
250}
251
252ApplicationManagerInterface *LauncherModel::applicationManager() const
253{
254 return m_appManager;
255}
256
257void LauncherModel::setApplicationManager(lomiri::shell::application::ApplicationManagerInterface *appManager)
258{
259 // Is there already another appmanager set?
260 if (m_appManager) {
261 // Disconnect any signals
262 disconnect(this, &LauncherModel::applicationAdded, 0, nullptr);
263 disconnect(this, &LauncherModel::applicationRemoved, 0, nullptr);
264 disconnect(this, &LauncherModel::focusedAppIdChanged, 0, nullptr);
265
266 // remove any recent/running apps from the launcher
267 QList<int> recentAppIndices;
268 for (int i = 0; i < m_list.count(); ++i) {
269 if (m_list.at(i)->recent()) {
270 recentAppIndices << i;
271 }
272 }
273 int run = 0;
274 while (recentAppIndices.count() > 0) {
275 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
276 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
277 endRemoveRows();
278 recentAppIndices.takeFirst();
279 ++run;
280 }
281 }
282
283 m_appManager = appManager;
284 connect(m_appManager, &ApplicationManagerInterface::rowsInserted, this, &LauncherModel::applicationAdded);
285 connect(m_appManager, &ApplicationManagerInterface::rowsAboutToBeRemoved, this, &LauncherModel::applicationRemoved);
286 connect(m_appManager, &ApplicationManagerInterface::focusedApplicationIdChanged, this, &LauncherModel::focusedAppIdChanged);
287
288 Q_EMIT applicationManagerChanged();
289
290 for (int i = 0; i < appManager->count(); ++i) {
291 applicationAdded(QModelIndex(), i);
292 }
293}
294
295bool LauncherModel::onlyPinned() const
296{
297 return false;
298}
299
300void LauncherModel::setOnlyPinned(bool onlyPinned) {
301 Q_UNUSED(onlyPinned);
302 qWarning() << "This launcher implementation does not support showing only pinned apps";
303}
304
305void LauncherModel::storeAppList()
306{
307 QStringList appIds;
308 Q_FOREACH(LauncherItem *item, m_list) {
309 if (item->pinned()) {
310 appIds << item->appId();
311 }
312 }
313 m_settings->setStoredApplications(appIds);
314 m_asAdapter->syncItems(m_list);
315}
316
317void LauncherModel::unpin(const QString &appId)
318{
319 const int index = findApplication(appId);
320 if (index < 0) {
321 return;
322 }
323
324 if (m_appManager->findApplication(appId)) {
325 if (m_list.at(index)->pinned()) {
326 m_list.at(index)->setPinned(false);
327 QModelIndex modelIndex = this->index(index);
328 Q_EMIT dataChanged(modelIndex, modelIndex, {RolePinned});
329 }
330 } else {
331 beginRemoveRows(QModelIndex(), index, index);
332 m_list.takeAt(index)->deleteLater();
333 endRemoveRows();
334 }
335}
336
337int LauncherModel::findApplication(const QString &appId)
338{
339 for (int i = 0; i < m_list.count(); ++i) {
340 LauncherItem *item = m_list.at(i);
341 if (item->appId() == appId) {
342 return i;
343 }
344 }
345 return -1;
346}
347
348void LauncherModel::progressChanged(const QString &appId, int progress)
349{
350 const int idx = findApplication(appId);
351 if (idx >= 0) {
352 LauncherItem *item = m_list.at(idx);
353 item->setProgress(progress);
354 Q_EMIT dataChanged(index(idx), index(idx), {RoleProgress});
355 }
356}
357
358void LauncherModel::countChanged(const QString &appId, int count)
359{
360 const int idx = findApplication(appId);
361 if (idx >= 0) {
362 LauncherItem *item = m_list.at(idx);
363 item->setCount(count);
364 QVector<int> changedRoles = {RoleCount};
365 if (item->countVisible() && !item->alerting() && !item->focused()) {
366 changedRoles << RoleAlerting;
367 item->setAlerting(true);
368 }
369 m_asAdapter->syncItems(m_list);
370 Q_EMIT dataChanged(index(idx), index(idx), changedRoles);
371 }
372}
373
374void LauncherModel::countVisibleChanged(const QString &appId, bool countVisible)
375{
376 int idx = findApplication(appId);
377 if (idx >= 0) {
378 LauncherItem *item = m_list.at(idx);
379 item->setCountVisible(countVisible);
380 QVector<int> changedRoles = {RoleCountVisible};
381 if (countVisible && !item->alerting() && !item->focused()) {
382 changedRoles << RoleAlerting;
383 item->setAlerting(true);
384 }
385 Q_EMIT dataChanged(index(idx), index(idx), changedRoles);
386
387 // If countVisible goes to false, and the item is neither pinned nor recent we can drop it
388 if (!countVisible && !item->pinned() && !item->recent()) {
389 beginRemoveRows(QModelIndex(), idx, idx);
390 m_list.takeAt(idx)->deleteLater();
391 endRemoveRows();
392 }
393 } else {
394 // Need to create a new LauncherItem and show the highlight
395 UalWrapper::AppInfo appInfo = UalWrapper::getApplicationInfo(appId);
396 if (countVisible && appInfo.valid) {
397 LauncherItem *item = new LauncherItem(appId,
398 appInfo.name,
399 appInfo.icon,
400 this);
401 item->setCountVisible(true);
402 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
403 m_list.append(item);
404 endInsertRows();
405 }
406 }
407 m_asAdapter->syncItems(m_list);
408}
409
410void LauncherModel::refresh()
411{
412 // First walk through all the existing items and see if we need to remove something
413 QList<LauncherItem*> toBeRemoved;
414 Q_FOREACH (LauncherItem* item, m_list) {
415 UalWrapper::AppInfo appInfo = UalWrapper::getApplicationInfo(item->appId());
416 if (!appInfo.valid) {
417 // Application no longer available => drop it!
418 toBeRemoved << item;
419 } else if (!m_settings->storedApplications().contains(item->appId())) {
420 // Item not in settings any more => drop it!
421 toBeRemoved << item;
422 } else {
423 int idx = m_list.indexOf(item);
424 item->setName(appInfo.name);
425 item->setPinned(item->pinned()); // update pinned text if needed
426 item->setRunning(item->running());
427 Q_EMIT dataChanged(index(idx), index(idx), {RoleName, RoleRunning});
428
429 const QString oldIcon = item->icon();
430 if (oldIcon == appInfo.icon) { // same icon file, perhaps different contents, simulate changing the icon name to force reload
431 item->setIcon(QString());
432 Q_EMIT dataChanged(index(idx), index(idx), {RoleIcon});
433 }
434
435 // now set the icon for real
436 item->setIcon(appInfo.icon);
437 Q_EMIT dataChanged(index(idx), index(idx), {RoleIcon});
438 }
439 }
440
441 Q_FOREACH (LauncherItem* item, toBeRemoved) {
442 unpin(item->appId());
443 }
444
445 bool changed = toBeRemoved.count() > 0;
446
447 // This brings the Launcher into sync with the settings backend again. There's an issue though:
448 // If we can't find a .desktop file for an entry we need to skip it. That makes our settingsIndex
449 // go out of sync with the actual index of items. So let's also use an addedIndex which reflects
450 // the settingsIndex minus the skipped items.
451 int addedIndex = 0;
452
453 // Now walk through settings and see if we need to add something
454 for (int settingsIndex = 0; settingsIndex < m_settings->storedApplications().count(); ++settingsIndex) {
455 const QString entry = m_settings->storedApplications().at(settingsIndex);
456 int itemIndex = -1;
457 for (int i = 0; i < m_list.count(); ++i) {
458 if (m_list.at(i)->appId() == entry) {
459 itemIndex = i;
460 break;
461 }
462 }
463
464 if (itemIndex == -1) {
465 // Need to add it. Just add it into the addedIndex to keep same ordering as the list
466 // in the settings.
467 UalWrapper::AppInfo appInfo = UalWrapper::getApplicationInfo(entry);
468 if (!appInfo.valid) {
469 continue;
470 }
471
472 LauncherItem *item = new LauncherItem(entry,
473 appInfo.name,
474 appInfo.icon,
475 this);
476 item->setPinned(true);
477 beginInsertRows(QModelIndex(), addedIndex, addedIndex);
478 m_list.insert(addedIndex, item);
479 endInsertRows();
480 changed = true;
481 } else if (itemIndex != addedIndex) {
482 // The item is already there, but it is in a different place than in the settings.
483 // Move it to the addedIndex
484 beginMoveRows(QModelIndex(), itemIndex, itemIndex, QModelIndex(), addedIndex);
485 m_list.move(itemIndex, addedIndex);
486 endMoveRows();
487 changed = true;
488 }
489
490 // Just like settingsIndex, this will increase with every item, except the ones we
491 // skipped with the "continue" call above.
492 addedIndex++;
493 }
494
495 if (changed) {
496 Q_EMIT hint();
497 }
498
499 m_asAdapter->syncItems(m_list);
500}
501
502void LauncherModel::alert(const QString &appId)
503{
504 int idx = findApplication(appId);
505 if (idx >= 0) {
506 LauncherItem *item = m_list.at(idx);
507 if (!item->focused() && !item->alerting()) {
508 item->setAlerting(true);
509 Q_EMIT dataChanged(index(idx), index(idx), {RoleAlerting});
510 }
511 }
512}
513
514void LauncherModel::applicationAdded(const QModelIndex &parent, int row)
515{
516 Q_UNUSED(parent);
517
518 ApplicationInfoInterface *app = m_appManager->get(row);
519 if (!app) {
520 qWarning() << "LauncherModel received an applicationAdded signal, but there's no such application!";
521 return;
522 }
523
524 const int itemIndex = findApplication(app->appId());
525 if (itemIndex != -1) {
526 LauncherItem *item = m_list.at(itemIndex);
527 if (!item->recent()) {
528 item->setRecent(true);
529 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), {RoleRecent});
530 }
531 item->setRunning(true);
532 } else {
533 LauncherItem *item = new LauncherItem(app->appId(), app->name(), app->icon().toString(), this);
534 item->setRecent(true);
535 item->setRunning(true);
536 item->setFocused(app->focused());
537 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
538 m_list.append(item);
539 endInsertRows();
540 }
541 connect(app, &ApplicationInfoInterface::surfaceCountChanged, this, &LauncherModel::updateSurfaceList);
542 m_asAdapter->syncItems(m_list);
543 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), {RoleRunning});
544}
545
546void LauncherModel::updateSurfaceList()
547{
548 ApplicationInfoInterface *app = static_cast<ApplicationInfoInterface*>(sender());
549 updateSurfaceListForApp(app);
550}
551
552void LauncherModel::updateSurfaceListForSurface()
553{
554 MirSurfaceInterface *iface = static_cast<MirSurfaceInterface*>(sender());
555 ApplicationInfoInterface* app = m_appManager->findApplication(iface->appId());
556 if (!app) {
557 return;
558 }
559 updateSurfaceListForApp(app);
560}
561
562void LauncherModel::updateSurfaceListForApp(ApplicationInfoInterface* app)
563{
564 int idx = findApplication(app->appId());
565 if (idx < 0) {
566 qWarning() << "Received a surface count changed event from an app that's not in the Launcher model";
567 return;
568 }
569 LauncherItem *item = m_list.at(idx);
570 QList<QPair<QString, QString> > surfaces;
571 for (int i = 0; i < app->surfaceList()->count(); ++i) {
572 MirSurfaceInterface* iface = app->surfaceList()->get(i);
573 if (iface->type() == Mir::NormalType || iface->type() == Mir::DialogType) {
574 // Avoid duplicate connections, so let's just disconnect first to be sure
575 disconnect(iface, &MirSurfaceInterface::nameChanged, this, &LauncherModel::updateSurfaceListForSurface);
576 connect(iface, &MirSurfaceInterface::nameChanged, this, &LauncherModel::updateSurfaceListForSurface);
577 QString name = iface->name();
578 if (name.isEmpty()) {
579 name = app->name();
580 }
581 surfaces.append({iface->persistentId(), name});
582 }
583 }
584 item->setSurfaces(surfaces);
585 Q_EMIT dataChanged(index(idx), index(idx), {RoleSurfaceCount});
586}
587
588void LauncherModel::applicationRemoved(const QModelIndex &parent, int row)
589{
590 Q_UNUSED(parent)
591
592 ApplicationInfoInterface *app = m_appManager->get(row);
593 int appIndex = -1;
594 for (int i = 0; i < m_list.count(); ++i) {
595 if (m_list.at(i)->appId() == app->appId()) {
596 appIndex = i;
597 break;
598 }
599 }
600
601 if (appIndex < 0) {
602 qWarning() << Q_FUNC_INFO << "appIndex not found";
603 return;
604 }
605
606 disconnect(app, &ApplicationInfoInterface::surfaceCountChanged, this, &LauncherModel::updateSurfaceList);
607
608 LauncherItem * item = m_list.at(appIndex);
609
610 if (!item->pinned()) {
611 beginRemoveRows(QModelIndex(), appIndex, appIndex);
612 m_list.takeAt(appIndex)->deleteLater();
613 endRemoveRows();
614 m_asAdapter->syncItems(m_list);
615 } else {
616 QVector<int> changedRoles = {RoleRunning};
617 item->setRunning(false);
618 if (item->focused()) {
619 changedRoles << RoleFocused;
620 item->setFocused(false);
621 }
622 Q_EMIT dataChanged(index(appIndex), index(appIndex), changedRoles);
623 }
624}
625
626void LauncherModel::focusedAppIdChanged()
627{
628 const QString appId = m_appManager->focusedApplicationId();
629 for (int i = 0; i < m_list.count(); ++i) {
630 LauncherItem *item = m_list.at(i);
631 if (!item->focused() && item->appId() == appId) {
632 QVector<int> changedRoles;
633 changedRoles << RoleFocused;
634 item->setFocused(true);
635 if (item->alerting()) {
636 changedRoles << RoleAlerting;
637 item->setAlerting(false);
638 }
639 Q_EMIT dataChanged(index(i), index(i), changedRoles);
640 } else if (item->focused() && item->appId() != appId) {
641 item->setFocused(false);
642 Q_EMIT dataChanged(index(i), index(i), {RoleFocused});
643 }
644 }
645}