Unity 8
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 <unity/shell/application/ApplicationInfoInterface.h>
25 #include <unity/shell/application/MirSurfaceListInterface.h>
26 #include <unity/shell/application/MirSurfaceInterface.h>
27 
28 #include <QDesktopServices>
29 #include <QDebug>
30 
31 using namespace unity::shell::application;
32 
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()),
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 
51 LauncherModel::~LauncherModel()
52 {
53  while (!m_list.empty()) {
54  m_list.takeFirst()->deleteLater();
55  }
56 
57  delete m_asAdapter;
58 }
59 
60 int LauncherModel::rowCount(const QModelIndex &parent) const
61 {
62  Q_UNUSED(parent)
63  return m_list.count();
64 }
65 
66 QVariant 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 
100 unity::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 
108 void 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 
139 void 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  m_list.insert(index, item);
171  endInsertRows();
172  }
173 
174  storeAppList();
175 }
176 
177 void LauncherModel::requestRemove(const QString &appId)
178 {
179  unpin(appId);
180  storeAppList();
181 }
182 
183 void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
184 {
185  const int index = findApplication(appId);
186  if (index < 0) {
187  return;
188  }
189 
190  LauncherItem *item = m_list.at(index);
191  QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
192  if (model) {
193  const QString actionId = model->get(actionIndex).actionId();
194 
195  // Check if this is one of the launcher actions we handle ourselves
196  if (actionId == QLatin1String("pin_item")) {
197  if (item->pinned()) {
198  requestRemove(appId);
199  } else {
200  pin(appId);
201  }
202  } else if (actionId == QStringLiteral("launch_item")) {
203  QDesktopServices::openUrl(getUrlForAppId(appId));
204  } else if (actionId == QStringLiteral("stop_item")) { // Quit
205  if (m_appManager) {
206  m_appManager->stopApplication(appId);
207  }
208  } else if (actionId.startsWith(QStringLiteral("surface_"))){
209  ApplicationInfoInterface *appInfo = m_appManager->findApplication(appId);
210  if (appInfo) {
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()) {
216  iface->activate();
217  }
218  }
219  } else {
220  qWarning() << "App for" << appId << "not found in launcher. Cannot invoke quicklist action";
221  }
222  // Nope, we don't know this action, let the backend forward it to the application
223  } else {
224  // TODO: forward quicklist action to app, possibly via m_dbusIface
225  }
226  }
227 }
228 
229 void LauncherModel::setUser(const QString &username)
230 {
231  Q_UNUSED(username)
232 }
233 
234 QString LauncherModel::getUrlForAppId(const QString &appId) const
235 {
236  // appId is either an appId or a legacy app name. Let's find out which
237  if (appId.isEmpty()) {
238  return QString();
239  }
240 
241  if (!appId.contains('_')) {
242  return "application:///" + appId + ".desktop";
243  }
244 
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";
249 }
250 
251 ApplicationManagerInterface *LauncherModel::applicationManager() const
252 {
253  return m_appManager;
254 }
255 
256 void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
257 {
258  // Is there already another appmanager set?
259  if (m_appManager) {
260  // Disconnect any signals
261  disconnect(this, &LauncherModel::applicationAdded, 0, nullptr);
262  disconnect(this, &LauncherModel::applicationRemoved, 0, nullptr);
263  disconnect(this, &LauncherModel::focusedAppIdChanged, 0, nullptr);
264 
265  // remove any recent/running apps from the launcher
266  QList<int> recentAppIndices;
267  for (int i = 0; i < m_list.count(); ++i) {
268  if (m_list.at(i)->recent()) {
269  recentAppIndices << i;
270  }
271  }
272  int run = 0;
273  while (recentAppIndices.count() > 0) {
274  beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
275  m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
276  endRemoveRows();
277  recentAppIndices.takeFirst();
278  ++run;
279  }
280  }
281 
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);
286 
287  Q_EMIT applicationManagerChanged();
288 
289  for (int i = 0; i < appManager->count(); ++i) {
290  applicationAdded(QModelIndex(), i);
291  }
292 }
293 
294 bool LauncherModel::onlyPinned() const
295 {
296  return false;
297 }
298 
299 void LauncherModel::setOnlyPinned(bool onlyPinned) {
300  Q_UNUSED(onlyPinned);
301  qWarning() << "This launcher implementation does not support showing only pinned apps";
302 }
303 
304 void LauncherModel::storeAppList()
305 {
306  QStringList appIds;
307  Q_FOREACH(LauncherItem *item, m_list) {
308  if (item->pinned()) {
309  appIds << item->appId();
310  }
311  }
312  m_settings->setStoredApplications(appIds);
313  m_asAdapter->syncItems(m_list);
314 }
315 
316 void LauncherModel::unpin(const QString &appId)
317 {
318  const int index = findApplication(appId);
319  if (index < 0) {
320  return;
321  }
322 
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});
328  }
329  } else {
330  beginRemoveRows(QModelIndex(), index, index);
331  m_list.takeAt(index)->deleteLater();
332  endRemoveRows();
333  }
334 }
335 
336 int LauncherModel::findApplication(const QString &appId)
337 {
338  for (int i = 0; i < m_list.count(); ++i) {
339  LauncherItem *item = m_list.at(i);
340  if (item->appId() == appId) {
341  return i;
342  }
343  }
344  return -1;
345 }
346 
347 void LauncherModel::progressChanged(const QString &appId, int progress)
348 {
349  const int idx = findApplication(appId);
350  if (idx >= 0) {
351  LauncherItem *item = m_list.at(idx);
352  item->setProgress(progress);
353  Q_EMIT dataChanged(index(idx), index(idx), {RoleProgress});
354  }
355 }
356 
357 void LauncherModel::countChanged(const QString &appId, int count)
358 {
359  const int idx = findApplication(appId);
360  if (idx >= 0) {
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);
367  }
368  m_asAdapter->syncItems(m_list);
369  Q_EMIT dataChanged(index(idx), index(idx), changedRoles);
370  }
371 }
372 
373 void LauncherModel::countVisibleChanged(const QString &appId, bool countVisible)
374 {
375  int idx = findApplication(appId);
376  if (idx >= 0) {
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);
383  }
384  Q_EMIT dataChanged(index(idx), index(idx), changedRoles);
385 
386  // If countVisible goes to false, and the item is neither pinned nor recent we can drop it
387  if (!countVisible && !item->pinned() && !item->recent()) {
388  beginRemoveRows(QModelIndex(), idx, idx);
389  m_list.takeAt(idx)->deleteLater();
390  endRemoveRows();
391  }
392  } else {
393  // Need to create a new LauncherItem and show the highlight
394  UalWrapper::AppInfo appInfo = UalWrapper::getApplicationInfo(appId);
395  if (countVisible && appInfo.valid) {
396  LauncherItem *item = new LauncherItem(appId,
397  appInfo.name,
398  appInfo.icon,
399  this);
400  item->setCountVisible(true);
401  beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
402  m_list.append(item);
403  endInsertRows();
404  }
405  }
406  m_asAdapter->syncItems(m_list);
407 }
408 
409 void LauncherModel::refresh()
410 {
411  // First walk through all the existing items and see if we need to remove something
412  QList<LauncherItem*> toBeRemoved;
413  Q_FOREACH (LauncherItem* item, m_list) {
414  UalWrapper::AppInfo appInfo = UalWrapper::getApplicationInfo(item->appId());
415  if (!appInfo.valid) {
416  // Application no longer available => drop it!
417  toBeRemoved << item;
418  } else if (!m_settings->storedApplications().contains(item->appId())) {
419  // Item not in settings any more => drop it!
420  toBeRemoved << item;
421  } else {
422  int idx = m_list.indexOf(item);
423  item->setName(appInfo.name);
424  item->setPinned(item->pinned()); // update pinned text if needed
425  item->setRunning(item->running());
426  Q_EMIT dataChanged(index(idx), index(idx), {RoleName, RoleRunning});
427 
428  const QString oldIcon = item->icon();
429  if (oldIcon == appInfo.icon) { // same icon file, perhaps different contents, simulate changing the icon name to force reload
430  item->setIcon(QString());
431  Q_EMIT dataChanged(index(idx), index(idx), {RoleIcon});
432  }
433 
434  // now set the icon for real
435  item->setIcon(appInfo.icon);
436  Q_EMIT dataChanged(index(idx), index(idx), {RoleIcon});
437  }
438  }
439 
440  Q_FOREACH (LauncherItem* item, toBeRemoved) {
441  unpin(item->appId());
442  }
443 
444  bool changed = toBeRemoved.count() > 0;
445 
446  // This brings the Launcher into sync with the settings backend again. There's an issue though:
447  // If we can't find a .desktop file for an entry we need to skip it. That makes our settingsIndex
448  // go out of sync with the actual index of items. So let's also use an addedIndex which reflects
449  // the settingsIndex minus the skipped items.
450  int addedIndex = 0;
451 
452  // Now walk through settings and see if we need to add something
453  for (int settingsIndex = 0; settingsIndex < m_settings->storedApplications().count(); ++settingsIndex) {
454  const QString entry = m_settings->storedApplications().at(settingsIndex);
455  int itemIndex = -1;
456  for (int i = 0; i < m_list.count(); ++i) {
457  if (m_list.at(i)->appId() == entry) {
458  itemIndex = i;
459  break;
460  }
461  }
462 
463  if (itemIndex == -1) {
464  // Need to add it. Just add it into the addedIndex to keep same ordering as the list
465  // in the settings.
466  UalWrapper::AppInfo appInfo = UalWrapper::getApplicationInfo(entry);
467  if (!appInfo.valid) {
468  continue;
469  }
470 
471  LauncherItem *item = new LauncherItem(entry,
472  appInfo.name,
473  appInfo.icon,
474  this);
475  item->setPinned(true);
476  beginInsertRows(QModelIndex(), addedIndex, addedIndex);
477  m_list.insert(addedIndex, item);
478  endInsertRows();
479  changed = true;
480  } else if (itemIndex != addedIndex) {
481  // The item is already there, but it is in a different place than in the settings.
482  // Move it to the addedIndex
483  beginMoveRows(QModelIndex(), itemIndex, itemIndex, QModelIndex(), addedIndex);
484  m_list.move(itemIndex, addedIndex);
485  endMoveRows();
486  changed = true;
487  }
488 
489  // Just like settingsIndex, this will increase with every item, except the ones we
490  // skipped with the "continue" call above.
491  addedIndex++;
492  }
493 
494  if (changed) {
495  Q_EMIT hint();
496  }
497 
498  m_asAdapter->syncItems(m_list);
499 }
500 
501 void LauncherModel::alert(const QString &appId)
502 {
503  int idx = findApplication(appId);
504  if (idx >= 0) {
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});
509  }
510  }
511 }
512 
513 void LauncherModel::applicationAdded(const QModelIndex &parent, int row)
514 {
515  Q_UNUSED(parent);
516 
517  ApplicationInfoInterface *app = m_appManager->get(row);
518  if (!app) {
519  qWarning() << "LauncherModel received an applicationAdded signal, but there's no such application!";
520  return;
521  }
522 
523  if (app->appId() == QLatin1String("unity8-dash")) {
524  // Not adding the dash app
525  return;
526  }
527 
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});
534  }
535  item->setRunning(true);
536  } else {
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());
542  m_list.append(item);
543  endInsertRows();
544  }
545  connect(app, &ApplicationInfoInterface::surfaceCountChanged, this, &LauncherModel::updateSurfaceList);
546  m_asAdapter->syncItems(m_list);
547  Q_EMIT dataChanged(index(itemIndex), index(itemIndex), {RoleRunning});
548 }
549 
550 void LauncherModel::updateSurfaceList()
551 {
552  ApplicationInfoInterface *app = static_cast<ApplicationInfoInterface*>(sender());
553  updateSurfaceListForApp(app);
554 }
555 
556 void LauncherModel::updateSurfaceListForSurface()
557 {
558  MirSurfaceInterface *iface = static_cast<MirSurfaceInterface*>(sender());
559  ApplicationInfoInterface* app = m_appManager->findApplication(iface->appId());
560  if (!app) {
561  return;
562  }
563  updateSurfaceListForApp(app);
564 }
565 
566 void LauncherModel::updateSurfaceListForApp(ApplicationInfoInterface* app)
567 {
568  int idx = findApplication(app->appId());
569  if (idx < 0) {
570  qWarning() << "Received a surface count changed event from an app that's not in the Launcher model";
571  return;
572  }
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) {
578  // Avoid duplicate connections, so let's just disconnect first to be sure
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()) {
583  name = app->name();
584  }
585  surfaces.append({iface->persistentId(), name});
586  }
587  }
588  item->setSurfaces(surfaces);
589  Q_EMIT dataChanged(index(idx), index(idx), {RoleSurfaceCount});
590 }
591 
592 void LauncherModel::applicationRemoved(const QModelIndex &parent, int row)
593 {
594  Q_UNUSED(parent)
595 
596  ApplicationInfoInterface *app = m_appManager->get(row);
597  int appIndex = -1;
598  for (int i = 0; i < m_list.count(); ++i) {
599  if (m_list.at(i)->appId() == app->appId()) {
600  appIndex = i;
601  break;
602  }
603  }
604 
605  if (appIndex < 0) {
606  qWarning() << Q_FUNC_INFO << "appIndex not found";
607  return;
608  }
609 
610  disconnect(app, &ApplicationInfoInterface::surfaceCountChanged, this, &LauncherModel::updateSurfaceList);
611 
612  LauncherItem * item = m_list.at(appIndex);
613 
614  if (!item->pinned()) {
615  beginRemoveRows(QModelIndex(), appIndex, appIndex);
616  m_list.takeAt(appIndex)->deleteLater();
617  endRemoveRows();
618  m_asAdapter->syncItems(m_list);
619  } else {
620  QVector<int> changedRoles = {RoleRunning};
621  item->setRunning(false);
622  if (item->focused()) {
623  changedRoles << RoleFocused;
624  item->setFocused(false);
625  }
626  Q_EMIT dataChanged(index(appIndex), index(appIndex), changedRoles);
627  }
628 }
629 
630 void LauncherModel::focusedAppIdChanged()
631 {
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);
642  }
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});
647  }
648  }
649 }