17 #include "launchermodel.h" 18 #include "launcheritem.h" 19 #include "gsettings.h" 20 #include "dbusinterface.h" 21 #include "asadapter.h" 22 #include "ualwrapper.h" 24 #include <unity/shell/application/ApplicationInfoInterface.h> 25 #include <unity/shell/application/MirSurfaceListInterface.h> 26 #include <unity/shell/application/MirSurfaceInterface.h> 28 #include <QDesktopServices> 33 LauncherModel::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);
51 LauncherModel::~LauncherModel()
53 while (!m_list.empty()) {
54 m_list.takeFirst()->deleteLater();
60 int LauncherModel::rowCount(
const QModelIndex &parent)
const 63 return m_list.count();
66 QVariant 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";
100 unity::shell::launcher::LauncherItemInterface *LauncherModel::get(
int index)
const 102 if (index < 0 || index >= m_list.count()) {
105 return m_list.at(index);
108 void 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());
139 void 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 m_list.insert(index, item);
177 void LauncherModel::requestRemove(
const QString &appId)
183 void LauncherModel::quickListActionInvoked(
const QString &appId,
int actionIndex)
185 const int index = findApplication(appId);
190 LauncherItem *item = m_list.at(index);
191 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
193 const QString actionId = model->get(actionIndex).actionId();
196 if (actionId == QLatin1String(
"pin_item")) {
197 if (item->pinned()) {
198 requestRemove(appId);
202 }
else if (actionId == QStringLiteral(
"launch_item")) {
203 QDesktopServices::openUrl(getUrlForAppId(appId));
204 }
else if (actionId == QStringLiteral(
"stop_item")) {
206 m_appManager->stopApplication(appId);
208 }
else if (actionId.startsWith(QStringLiteral(
"surface_"))){
209 ApplicationInfoInterface *appInfo = m_appManager->findApplication(appId);
211 for (
int i = 0; i < appInfo->surfaceList()->count(); ++i) {
212 MirSurfaceInterface *iface = appInfo->surfaceList()->get(i);
213 QString
id = actionId;
214 id.remove(QRegExp(
"^surface_"));
215 if (
id == iface->persistentId()) {
220 qWarning() <<
"App for" << appId <<
"not found in launcher. Cannot invoke quicklist action";
229 void LauncherModel::setUser(
const QString &username)
234 QString LauncherModel::getUrlForAppId(
const QString &appId)
const 237 if (appId.isEmpty()) {
241 if (!appId.contains(
'_')) {
242 return "application:///" + appId +
".desktop";
245 QStringList parts = appId.split(
'_');
246 QString
package = parts.value(0);
247 QString app = parts.value(1, QStringLiteral(
"first-listed-app"));
248 return "appid://" +
package + "/" + app + "/current-user-version";
251 ApplicationManagerInterface *LauncherModel::applicationManager()
const 256 void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
261 disconnect(
this, &LauncherModel::applicationAdded, 0,
nullptr);
262 disconnect(
this, &LauncherModel::applicationRemoved, 0,
nullptr);
263 disconnect(
this, &LauncherModel::focusedAppIdChanged, 0,
nullptr);
266 QList<int> recentAppIndices;
267 for (
int i = 0; i < m_list.count(); ++i) {
268 if (m_list.at(i)->recent()) {
269 recentAppIndices << i;
273 while (recentAppIndices.count() > 0) {
274 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
275 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
277 recentAppIndices.takeFirst();
282 m_appManager = appManager;
283 connect(m_appManager, &ApplicationManagerInterface::rowsInserted,
this, &LauncherModel::applicationAdded);
284 connect(m_appManager, &ApplicationManagerInterface::rowsAboutToBeRemoved,
this, &LauncherModel::applicationRemoved);
285 connect(m_appManager, &ApplicationManagerInterface::focusedApplicationIdChanged,
this, &LauncherModel::focusedAppIdChanged);
287 Q_EMIT applicationManagerChanged();
289 for (
int i = 0; i < appManager->count(); ++i) {
290 applicationAdded(QModelIndex(), i);
294 bool LauncherModel::onlyPinned()
const 299 void LauncherModel::setOnlyPinned(
bool onlyPinned) {
300 Q_UNUSED(onlyPinned);
301 qWarning() <<
"This launcher implementation does not support showing only pinned apps";
304 void LauncherModel::storeAppList()
307 Q_FOREACH(LauncherItem *item, m_list) {
308 if (item->pinned()) {
309 appIds << item->appId();
312 m_settings->setStoredApplications(appIds);
313 m_asAdapter->syncItems(m_list);
316 void LauncherModel::unpin(
const QString &appId)
318 const int index = findApplication(appId);
323 if (m_appManager->findApplication(appId)) {
324 if (m_list.at(index)->pinned()) {
325 m_list.at(index)->setPinned(
false);
326 QModelIndex modelIndex = this->index(index);
327 Q_EMIT dataChanged(modelIndex, modelIndex, {RolePinned});
330 beginRemoveRows(QModelIndex(), index, index);
331 m_list.takeAt(index)->deleteLater();
336 int LauncherModel::findApplication(
const QString &appId)
338 for (
int i = 0; i < m_list.count(); ++i) {
339 LauncherItem *item = m_list.at(i);
340 if (item->appId() == appId) {
347 void LauncherModel::progressChanged(
const QString &appId,
int progress)
349 const int idx = findApplication(appId);
351 LauncherItem *item = m_list.at(idx);
352 item->setProgress(progress);
353 Q_EMIT dataChanged(index(idx), index(idx), {RoleProgress});
357 void LauncherModel::countChanged(
const QString &appId,
int count)
359 const int idx = findApplication(appId);
361 LauncherItem *item = m_list.at(idx);
362 item->setCount(count);
363 QVector<int> changedRoles = {RoleCount};
364 if (item->countVisible() && !item->alerting() && !item->focused()) {
365 changedRoles << RoleAlerting;
366 item->setAlerting(
true);
368 m_asAdapter->syncItems(m_list);
369 Q_EMIT dataChanged(index(idx), index(idx), changedRoles);
373 void LauncherModel::countVisibleChanged(
const QString &appId,
bool countVisible)
375 int idx = findApplication(appId);
377 LauncherItem *item = m_list.at(idx);
378 item->setCountVisible(countVisible);
379 QVector<int> changedRoles = {RoleCountVisible};
380 if (countVisible && !item->alerting() && !item->focused()) {
381 changedRoles << RoleAlerting;
382 item->setAlerting(
true);
384 Q_EMIT dataChanged(index(idx), index(idx), changedRoles);
387 if (!countVisible && !item->pinned() && !item->recent()) {
388 beginRemoveRows(QModelIndex(), idx, idx);
389 m_list.takeAt(idx)->deleteLater();
394 UalWrapper::AppInfo appInfo = UalWrapper::getApplicationInfo(appId);
395 if (countVisible && appInfo.valid) {
396 LauncherItem *item =
new LauncherItem(appId,
400 item->setCountVisible(
true);
401 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
406 m_asAdapter->syncItems(m_list);
409 void LauncherModel::refresh()
412 QList<LauncherItem*> toBeRemoved;
413 Q_FOREACH (LauncherItem* item, m_list) {
414 UalWrapper::AppInfo appInfo = UalWrapper::getApplicationInfo(item->appId());
415 if (!appInfo.valid) {
418 }
else if (!m_settings->storedApplications().contains(item->appId())) {
422 int idx = m_list.indexOf(item);
423 item->setName(appInfo.name);
424 item->setPinned(item->pinned());
425 item->setRunning(item->running());
426 Q_EMIT dataChanged(index(idx), index(idx), {RoleName, RoleRunning});
428 const QString oldIcon = item->icon();
429 if (oldIcon == appInfo.icon) {
430 item->setIcon(QString());
431 Q_EMIT dataChanged(index(idx), index(idx), {RoleIcon});
435 item->setIcon(appInfo.icon);
436 Q_EMIT dataChanged(index(idx), index(idx), {RoleIcon});
440 Q_FOREACH (LauncherItem* item, toBeRemoved) {
441 unpin(item->appId());
444 bool changed = toBeRemoved.count() > 0;
453 for (
int settingsIndex = 0; settingsIndex < m_settings->storedApplications().count(); ++settingsIndex) {
454 const QString entry = m_settings->storedApplications().at(settingsIndex);
456 for (
int i = 0; i < m_list.count(); ++i) {
457 if (m_list.at(i)->appId() == entry) {
463 if (itemIndex == -1) {
466 UalWrapper::AppInfo appInfo = UalWrapper::getApplicationInfo(entry);
467 if (!appInfo.valid) {
471 LauncherItem *item =
new LauncherItem(entry,
475 item->setPinned(
true);
476 beginInsertRows(QModelIndex(), addedIndex, addedIndex);
477 m_list.insert(addedIndex, item);
480 }
else if (itemIndex != addedIndex) {
483 beginMoveRows(QModelIndex(), itemIndex, itemIndex, QModelIndex(), addedIndex);
484 m_list.move(itemIndex, addedIndex);
498 m_asAdapter->syncItems(m_list);
501 void LauncherModel::alert(
const QString &appId)
503 int idx = findApplication(appId);
505 LauncherItem *item = m_list.at(idx);
506 if (!item->focused() && !item->alerting()) {
507 item->setAlerting(
true);
508 Q_EMIT dataChanged(index(idx), index(idx), {RoleAlerting});
513 void LauncherModel::applicationAdded(
const QModelIndex &parent,
int row)
517 ApplicationInfoInterface *app = m_appManager->get(row);
519 qWarning() <<
"LauncherModel received an applicationAdded signal, but there's no such application!";
523 if (app->appId() == QLatin1String(
"unity8-dash")) {
528 const int itemIndex = findApplication(app->appId());
529 if (itemIndex != -1) {
530 LauncherItem *item = m_list.at(itemIndex);
531 if (!item->recent()) {
532 item->setRecent(
true);
533 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), {RoleRecent});
535 item->setRunning(
true);
537 LauncherItem *item =
new LauncherItem(app->appId(), app->name(), app->icon().toString(),
this);
538 item->setRecent(
true);
539 item->setRunning(
true);
540 item->setFocused(app->focused());
541 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
545 connect(app, &ApplicationInfoInterface::surfaceCountChanged,
this, &LauncherModel::updateSurfaceList);
546 m_asAdapter->syncItems(m_list);
547 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), {RoleRunning});
550 void LauncherModel::updateSurfaceList()
552 ApplicationInfoInterface *app =
static_cast<ApplicationInfoInterface*
>(sender());
553 updateSurfaceListForApp(app);
556 void LauncherModel::updateSurfaceListForSurface()
558 MirSurfaceInterface *iface =
static_cast<MirSurfaceInterface*
>(sender());
559 ApplicationInfoInterface* app = m_appManager->findApplication(iface->appId());
563 updateSurfaceListForApp(app);
566 void LauncherModel::updateSurfaceListForApp(ApplicationInfoInterface* app)
568 int idx = findApplication(app->appId());
570 qWarning() <<
"Received a surface count changed event from an app that's not in the Launcher model";
573 LauncherItem *item = m_list.at(idx);
574 QList<QPair<QString, QString> > surfaces;
575 for (
int i = 0; i < app->surfaceList()->count(); ++i) {
576 MirSurfaceInterface* iface = app->surfaceList()->get(i);
577 if (iface->type() == Mir::NormalType || iface->type() == Mir::DialogType) {
579 disconnect(iface, &MirSurfaceInterface::nameChanged,
this, &LauncherModel::updateSurfaceListForSurface);
580 connect(iface, &MirSurfaceInterface::nameChanged,
this, &LauncherModel::updateSurfaceListForSurface);
581 QString name = iface->name();
582 if (name.isEmpty()) {
585 surfaces.append({iface->persistentId(), name});
588 item->setSurfaces(surfaces);
589 Q_EMIT dataChanged(index(idx), index(idx), {RoleSurfaceCount});
592 void LauncherModel::applicationRemoved(
const QModelIndex &parent,
int row)
596 ApplicationInfoInterface *app = m_appManager->get(row);
598 for (
int i = 0; i < m_list.count(); ++i) {
599 if (m_list.at(i)->appId() == app->appId()) {
606 qWarning() << Q_FUNC_INFO <<
"appIndex not found";
610 disconnect(app, &ApplicationInfoInterface::surfaceCountChanged,
this, &LauncherModel::updateSurfaceList);
612 LauncherItem * item = m_list.at(appIndex);
614 if (!item->pinned()) {
615 beginRemoveRows(QModelIndex(), appIndex, appIndex);
616 m_list.takeAt(appIndex)->deleteLater();
618 m_asAdapter->syncItems(m_list);
620 QVector<int> changedRoles = {RoleRunning};
621 item->setRunning(
false);
622 if (item->focused()) {
623 changedRoles << RoleFocused;
624 item->setFocused(
false);
626 Q_EMIT dataChanged(index(appIndex), index(appIndex), changedRoles);
630 void LauncherModel::focusedAppIdChanged()
632 const QString appId = m_appManager->focusedApplicationId();
633 for (
int i = 0; i < m_list.count(); ++i) {
634 LauncherItem *item = m_list.at(i);
635 if (!item->focused() && item->appId() == appId) {
636 QVector<int> changedRoles;
637 changedRoles << RoleFocused;
638 item->setFocused(
true);
639 if (item->alerting()) {
640 changedRoles << RoleAlerting;
641 item->setAlerting(
false);
643 Q_EMIT dataChanged(index(i), index(i), changedRoles);
644 }
else if (item->focused() && item->appId() != appId) {
645 item->setFocused(
false);
646 Q_EMIT dataChanged(index(i), index(i), {RoleFocused});