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