17#include "launchermodel.h"
18#include "launcheritem.h"
20#include "dbusinterface.h"
22#include "ualwrapper.h"
24#include <lomiri/shell/application/ApplicationInfoInterface.h>
25#include <lomiri/shell/application/MirSurfaceListInterface.h>
26#include <lomiri/shell/application/MirSurfaceInterface.h>
28#include <QDesktopServices>
31using namespace lomiri::shell::application;
33LauncherModel::LauncherModel(QObject *parent):
34 LauncherModelInterface(parent),
35 m_settings(new GSettings(this)),
36 m_dbusIface(new DBusInterface(this)),
37 m_asAdapter(new ASAdapter()),
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);
46 connect(m_settings, &GSettings::changed,
this, &LauncherModel::refresh);
51LauncherModel::~LauncherModel()
53 while (!m_list.empty()) {
54 m_list.takeFirst()->deleteLater();
60int LauncherModel::rowCount(
const QModelIndex &parent)
const
63 return m_list.count();
66QVariant LauncherModel::data(
const QModelIndex &index,
int role)
const
68 LauncherItem *item = m_list.at(index.row());
77 return item->pinned();
80 case RoleCountVisible:
81 return item->countVisible();
83 return item->progress();
85 return item->focused();
87 return item->alerting();
89 return item->running();
90 case RoleSurfaceCount:
91 return item->surfaceCount();
93 qWarning() << Q_FUNC_INFO <<
"missing role, implement me";
100lomiri::shell::launcher::LauncherItemInterface *LauncherModel::get(
int index)
const
102 if (index < 0 || index >= m_list.count()) {
105 return m_list.at(index);
108void LauncherModel::move(
int oldIndex,
int newIndex)
114 if (newIndex >= m_list.count()) {
115 newIndex = m_list.count()-1;
119 if (oldIndex == newIndex) {
126 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
128 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
129 m_list.move(oldIndex, newIndex);
132 if (!m_list.at(newIndex)->pinned()) {
133 pin(m_list.at(newIndex)->appId());
139void LauncherModel::pin(
const QString &appId,
int index)
141 int currentIndex = findApplication(appId);
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});
149 move(currentIndex, index);
155 index = m_list.count();
158 UalWrapper::AppInfo appInfo = UalWrapper::getApplicationInfo(appId);
159 if (!appInfo.valid) {
160 qWarning() <<
"Can't pin application, appId not found:" << appId;
164 beginInsertRows(QModelIndex(), index, index);
165 LauncherItem *item =
new LauncherItem(appId,
169 item->setPinned(
true);
170 item->setPopularity(appInfo.popularity);
171 m_list.insert(index, item);
178void LauncherModel::requestRemove(
const QString &appId)
184void LauncherModel::quickListActionInvoked(
const QString &appId,
int actionIndex)
186 const int index = findApplication(appId);
191 LauncherItem *item = m_list.at(index);
192 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
194 const QString actionId = model->get(actionIndex).actionId();
197 if (actionId == QLatin1String(
"pin_item")) {
198 if (item->pinned()) {
199 requestRemove(appId);
203 }
else if (actionId == QStringLiteral(
"launch_item")) {
204 QDesktopServices::openUrl(getUrlForAppId(appId));
205 }
else if (actionId == QStringLiteral(
"stop_item")) {
207 m_appManager->stopApplication(appId);
209 }
else if (actionId.startsWith(QStringLiteral(
"surface_"))){
210 ApplicationInfoInterface *appInfo = m_appManager->findApplication(appId);
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()) {
221 qWarning() <<
"App for" << appId <<
"not found in launcher. Cannot invoke quicklist action";
230void LauncherModel::setUser(
const QString &username)
235QString LauncherModel::getUrlForAppId(
const QString &appId)
const
238 if (appId.isEmpty()) {
242 if (!appId.contains(
'_')) {
243 return "application:///" + appId +
".desktop";
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";
252ApplicationManagerInterface *LauncherModel::applicationManager()
const
257void LauncherModel::setApplicationManager(lomiri::shell::application::ApplicationManagerInterface *appManager)
262 disconnect(
this, &LauncherModel::applicationAdded, 0,
nullptr);
263 disconnect(
this, &LauncherModel::applicationRemoved, 0,
nullptr);
264 disconnect(
this, &LauncherModel::focusedAppIdChanged, 0,
nullptr);
267 QList<int> recentAppIndices;
268 for (
int i = 0; i < m_list.count(); ++i) {
269 if (m_list.at(i)->recent()) {
270 recentAppIndices << i;
274 while (recentAppIndices.count() > 0) {
275 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
276 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
278 recentAppIndices.takeFirst();
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);
288 Q_EMIT applicationManagerChanged();
290 for (
int i = 0; i < appManager->count(); ++i) {
291 applicationAdded(QModelIndex(), i);
295bool LauncherModel::onlyPinned()
const
300void LauncherModel::setOnlyPinned(
bool onlyPinned) {
301 Q_UNUSED(onlyPinned);
302 qWarning() <<
"This launcher implementation does not support showing only pinned apps";
305void LauncherModel::storeAppList()
308 Q_FOREACH(LauncherItem *item, m_list) {
309 if (item->pinned()) {
310 appIds << item->appId();
313 m_settings->setStoredApplications(appIds);
314 m_asAdapter->syncItems(m_list);
317void LauncherModel::unpin(
const QString &appId)
319 const int index = findApplication(appId);
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});
331 beginRemoveRows(QModelIndex(), index, index);
332 m_list.takeAt(index)->deleteLater();
337int LauncherModel::findApplication(
const QString &appId)
339 for (
int i = 0; i < m_list.count(); ++i) {
340 LauncherItem *item = m_list.at(i);
341 if (item->appId() == appId) {
348void LauncherModel::progressChanged(
const QString &appId,
int progress)
350 const int idx = findApplication(appId);
352 LauncherItem *item = m_list.at(idx);
353 item->setProgress(progress);
354 Q_EMIT dataChanged(index(idx), index(idx), {RoleProgress});
358void LauncherModel::countChanged(
const QString &appId,
int count)
360 const int idx = findApplication(appId);
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);
369 m_asAdapter->syncItems(m_list);
370 Q_EMIT dataChanged(index(idx), index(idx), changedRoles);
374void LauncherModel::countVisibleChanged(
const QString &appId,
bool countVisible)
376 int idx = findApplication(appId);
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);
385 Q_EMIT dataChanged(index(idx), index(idx), changedRoles);
388 if (!countVisible && !item->pinned() && !item->recent()) {
389 beginRemoveRows(QModelIndex(), idx, idx);
390 m_list.takeAt(idx)->deleteLater();
395 UalWrapper::AppInfo appInfo = UalWrapper::getApplicationInfo(appId);
396 if (countVisible && appInfo.valid) {
397 LauncherItem *item =
new LauncherItem(appId,
401 item->setCountVisible(
true);
402 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
407 m_asAdapter->syncItems(m_list);
410void LauncherModel::refresh()
413 QList<LauncherItem*> toBeRemoved;
414 Q_FOREACH (LauncherItem* item, m_list) {
415 UalWrapper::AppInfo appInfo = UalWrapper::getApplicationInfo(item->appId());
416 if (!appInfo.valid) {
419 }
else if (!m_settings->storedApplications().contains(item->appId())) {
423 int idx = m_list.indexOf(item);
424 item->setName(appInfo.name);
425 item->setPinned(item->pinned());
426 item->setRunning(item->running());
427 Q_EMIT dataChanged(index(idx), index(idx), {RoleName, RoleRunning});
429 const QString oldIcon = item->icon();
430 if (oldIcon == appInfo.icon) {
431 item->setIcon(QString());
432 Q_EMIT dataChanged(index(idx), index(idx), {RoleIcon});
436 item->setIcon(appInfo.icon);
437 Q_EMIT dataChanged(index(idx), index(idx), {RoleIcon});
441 Q_FOREACH (LauncherItem* item, toBeRemoved) {
442 unpin(item->appId());
445 bool changed = toBeRemoved.count() > 0;
454 for (
int settingsIndex = 0; settingsIndex < m_settings->storedApplications().count(); ++settingsIndex) {
455 const QString entry = m_settings->storedApplications().at(settingsIndex);
457 for (
int i = 0; i < m_list.count(); ++i) {
458 if (m_list.at(i)->appId() == entry) {
464 if (itemIndex == -1) {
467 UalWrapper::AppInfo appInfo = UalWrapper::getApplicationInfo(entry);
468 if (!appInfo.valid) {
472 LauncherItem *item =
new LauncherItem(entry,
476 item->setPinned(
true);
477 beginInsertRows(QModelIndex(), addedIndex, addedIndex);
478 m_list.insert(addedIndex, item);
481 }
else if (itemIndex != addedIndex) {
484 beginMoveRows(QModelIndex(), itemIndex, itemIndex, QModelIndex(), addedIndex);
485 m_list.move(itemIndex, addedIndex);
499 m_asAdapter->syncItems(m_list);
502void LauncherModel::alert(
const QString &appId)
504 int idx = findApplication(appId);
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});
514void LauncherModel::applicationAdded(
const QModelIndex &parent,
int row)
518 ApplicationInfoInterface *app = m_appManager->get(row);
520 qWarning() <<
"LauncherModel received an applicationAdded signal, but there's no such application!";
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});
531 item->setRunning(
true);
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());
541 connect(app, &ApplicationInfoInterface::surfaceCountChanged,
this, &LauncherModel::updateSurfaceList);
542 m_asAdapter->syncItems(m_list);
543 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), {RoleRunning});
546void LauncherModel::updateSurfaceList()
548 ApplicationInfoInterface *app =
static_cast<ApplicationInfoInterface*
>(sender());
549 updateSurfaceListForApp(app);
552void LauncherModel::updateSurfaceListForSurface()
554 MirSurfaceInterface *iface =
static_cast<MirSurfaceInterface*
>(sender());
555 ApplicationInfoInterface* app = m_appManager->findApplication(iface->appId());
559 updateSurfaceListForApp(app);
562void LauncherModel::updateSurfaceListForApp(ApplicationInfoInterface* app)
564 int idx = findApplication(app->appId());
566 qWarning() <<
"Received a surface count changed event from an app that's not in the Launcher model";
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) {
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()) {
581 surfaces.append({iface->persistentId(), name});
584 item->setSurfaces(surfaces);
585 Q_EMIT dataChanged(index(idx), index(idx), {RoleSurfaceCount});
588void LauncherModel::applicationRemoved(
const QModelIndex &parent,
int row)
592 ApplicationInfoInterface *app = m_appManager->get(row);
594 for (
int i = 0; i < m_list.count(); ++i) {
595 if (m_list.at(i)->appId() == app->appId()) {
602 qWarning() << Q_FUNC_INFO <<
"appIndex not found";
606 disconnect(app, &ApplicationInfoInterface::surfaceCountChanged,
this, &LauncherModel::updateSurfaceList);
608 LauncherItem * item = m_list.at(appIndex);
610 if (!item->pinned()) {
611 beginRemoveRows(QModelIndex(), appIndex, appIndex);
612 m_list.takeAt(appIndex)->deleteLater();
614 m_asAdapter->syncItems(m_list);
616 QVector<int> changedRoles = {RoleRunning};
617 item->setRunning(
false);
618 if (item->focused()) {
619 changedRoles << RoleFocused;
620 item->setFocused(
false);
622 Q_EMIT dataChanged(index(appIndex), index(appIndex), changedRoles);
626void LauncherModel::focusedAppIdChanged()
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);
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});