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