Unity 8
 All Classes Functions Properties
launchermodel.cpp
1 /*
2  * Copyright 2013 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 "backend/launcherbackend.h"
23 
24 #include <unity/shell/application/ApplicationInfoInterface.h>
25 
26 using namespace unity::shell::application;
27 
28 LauncherModel::LauncherModel(QObject *parent):
29  LauncherModelInterface(parent),
30  m_backend(new LauncherBackend(this)),
31  m_appManager(0)
32 {
33  connect(m_backend, SIGNAL(countChanged(QString,int)), SLOT(countChanged(QString,int)));
34  connect(m_backend, SIGNAL(progressChanged(QString,int)), SLOT(progressChanged(QString,int)));
35 
36  Q_FOREACH (const QString &entry, m_backend->storedApplications()) {
37  LauncherItem *item = new LauncherItem(entry,
38  m_backend->displayName(entry),
39  m_backend->icon(entry),
40  this);
41  item->setPinned(true);
42  m_list.append(item);
43  }
44 }
45 
46 LauncherModel::~LauncherModel()
47 {
48  while (!m_list.empty()) {
49  m_list.takeFirst()->deleteLater();
50  }
51 }
52 
53 int LauncherModel::rowCount(const QModelIndex &parent) const
54 {
55  Q_UNUSED(parent)
56  return m_list.count();
57 }
58 
59 QVariant LauncherModel::data(const QModelIndex &index, int role) const
60 {
61  LauncherItem *item = m_list.at(index.row());
62  switch(role) {
63  case RoleAppId:
64  return item->appId();
65  case RoleName:
66  return item->name();
67  case RoleIcon:
68  return item->icon();
69  case RolePinned:
70  return item->pinned();
71  case RoleCount:
72  return item->count();
73  case RoleProgress:
74  return item->progress();
75  case RoleFocused:
76  return item->focused();
77  }
78 
79  return QVariant();
80 }
81 
82 unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
83 {
84  if (index < 0 || index >= m_list.count()) {
85  return 0;
86  }
87  return m_list.at(index);
88 }
89 
90 void LauncherModel::move(int oldIndex, int newIndex)
91 {
92  // Make sure its not moved outside the lists
93  if (newIndex < 0) {
94  newIndex = 0;
95  }
96  if (newIndex >= m_list.count()) {
97  newIndex = m_list.count()-1;
98  }
99 
100  // Nothing to do?
101  if (oldIndex == newIndex) {
102  return;
103  }
104 
105  // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
106  // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
107  // adjust the model's index by +1 in case we're moving upwards
108  int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
109 
110  beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
111  m_list.move(oldIndex, newIndex);
112  endMoveRows();
113 
114  if (!m_list.at(newIndex)->pinned()) {
115  pin(m_list.at(newIndex)->appId());
116  } else {
117  storeAppList();
118  }
119 }
120 
121 void LauncherModel::pin(const QString &appId, int index)
122 {
123  int currentIndex = findApplication(appId);
124 
125  if (currentIndex >= 0) {
126  if (index == -1 || index == currentIndex) {
127  m_list.at(currentIndex)->setPinned(true);
128  QModelIndex modelIndex = this->index(currentIndex);
129  Q_EMIT dataChanged(modelIndex, modelIndex);
130  } else {
131  move(currentIndex, index);
132  // move() will store the list to the backend itself, so just exit at this point.
133  return;
134  }
135  } else {
136  if (index == -1) {
137  index = m_list.count();
138  }
139  beginInsertRows(QModelIndex(), index, index);
140  LauncherItem *item = new LauncherItem(appId,
141  m_backend->displayName(appId),
142  m_backend->icon(appId));
143  item->setPinned(true);
144  m_list.insert(index, item);
145  endInsertRows();
146  }
147 
148  storeAppList();
149 }
150 
151 void LauncherModel::requestRemove(const QString &appId)
152 {
153  int index = findApplication(appId);
154  if (index < 0) {
155  return;
156  }
157 
158  if (m_appManager->findApplication(appId)) {
159  m_list.at(index)->setPinned(false);
160  return;
161  }
162 
163  beginRemoveRows(QModelIndex(), index, index);
164  m_list.takeAt(index)->deleteLater();
165  endRemoveRows();
166 
167  storeAppList();
168 }
169 
170 void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
171 {
172  int index = findApplication(appId);
173  if (index < 0) {
174  return;
175  }
176 
177  LauncherItem *item = m_list.at(index);
178  QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
179  if (model) {
180  QString actionId = model->get(actionIndex).actionId();
181 
182  // Check if this is one of the launcher actions we handle ourselves
183  if (actionId == "pin_item") {
184  if (item->pinned()) {
185  requestRemove(appId);
186  } else {
187  pin(appId);
188  }
189 
190  // Nope, we don't know this action, let the backend forward it to the application
191  } else {
192  m_backend->triggerQuickListAction(appId, actionId);
193  }
194  }
195 }
196 
197 void LauncherModel::setUser(const QString &username)
198 {
199  m_backend->setUser(username);
200 }
201 
202 ApplicationManagerInterface *LauncherModel::applicationManager() const
203 {
204  return m_appManager;
205 }
206 
207 void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
208 {
209  // Is there already another appmanager set?
210  if (m_appManager) {
211  // Disconnect any signals
212  disconnect(this, SLOT(applicationAdded(QModelIndex,int)));
213  disconnect(this, SLOT(applicationRemoved(QModelIndex,int)));
214  disconnect(this, SLOT(focusedAppIdChanged()));
215 
216  // remove any recent/running apps from the launcher
217  QList<int> recentAppIndices;
218  for (int i = 0; i < m_list.count(); ++i) {
219  if (m_list.at(i)->recent()) {
220  recentAppIndices << i;
221  }
222  }
223  int run = 0;
224  while (recentAppIndices.count() > 0) {
225  beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
226  m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
227  endRemoveRows();
228  recentAppIndices.takeFirst();
229  ++run;
230  }
231  }
232 
233  m_appManager = appManager;
234  connect(m_appManager, SIGNAL(rowsInserted(QModelIndex, int, int)), SLOT(applicationAdded(QModelIndex,int)));
235  connect(m_appManager, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), SLOT(applicationRemoved(QModelIndex,int)));
236  connect(m_appManager, SIGNAL(focusedApplicationIdChanged()), SLOT(focusedAppIdChanged()));
237 
238  Q_EMIT applicationManagerChanged();
239 
240  for (int i = 0; i < appManager->count(); ++i) {
241  applicationAdded(QModelIndex(), i);
242  }
243 }
244 
245 
246 void LauncherModel::storeAppList()
247 {
248  QStringList appIds;
249  Q_FOREACH(LauncherItem *item, m_list) {
250  if (item->pinned()) {
251  appIds << item->appId();
252  }
253  }
254  m_backend->setStoredApplications(appIds);
255 }
256 
257 int LauncherModel::findApplication(const QString &appId)
258 {
259  for (int i = 0; i < m_list.count(); ++i) {
260  LauncherItem *item = m_list.at(i);
261  if (item->appId() == appId) {
262  return i;
263  }
264  }
265  return -1;
266 }
267 
268 void LauncherModel::progressChanged(const QString &appId, int progress)
269 {
270  int idx = findApplication(appId);
271  if (idx >= 0) {
272  LauncherItem *item = m_list.at(idx);
273  item->setProgress(progress);
274  Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleProgress);
275  }
276 }
277 
278 
279 void LauncherModel::countChanged(const QString &appId, int count)
280 {
281  int idx = findApplication(appId);
282  if (idx >= 0) {
283  LauncherItem *item = m_list.at(idx);
284  item->setCount(count);
285  Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleCount);
286  }
287 }
288 
289 void LauncherModel::applicationAdded(const QModelIndex &parent, int row)
290 {
291  Q_UNUSED(parent);
292 
293  ApplicationInfoInterface *app = m_appManager->get(row);
294 
295  bool found = false;
296  Q_FOREACH(LauncherItem *item, m_list) {
297  if (app->appId() == item->appId()) {
298  found = true;
299  break;
300  }
301  }
302  if (found) {
303  // Shall we paint some running/recent app highlight? If yes, do it here.
304  } else {
305  LauncherItem *item = new LauncherItem(app->appId(), app->name(), app->icon().toString());
306  item->setRecent(true);
307  item->setFocused(app->focused());
308 
309  beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
310  m_list.append(item);
311  endInsertRows();
312  }
313 }
314 
315 void LauncherModel::applicationRemoved(const QModelIndex &parent, int row)
316 {
317  Q_UNUSED(parent)
318 
319  int appIndex = -1;
320  for (int i = 0; i < m_list.count(); ++i) {
321  if (m_list.at(i)->appId() == m_appManager->get(row)->appId()) {
322  appIndex = i;
323  break;
324  }
325  }
326 
327  if (appIndex > -1 && !m_list.at(appIndex)->pinned()) {
328  beginRemoveRows(QModelIndex(), appIndex, appIndex);
329  m_list.takeAt(appIndex)->deleteLater();
330  endRemoveRows();
331  }
332 }
333 
334 void LauncherModel::focusedAppIdChanged()
335 {
336  QString appId = m_appManager->focusedApplicationId();
337  for (int i = 0; i < m_list.count(); ++i) {
338  LauncherItem *item = m_list.at(i);
339  if (!item->focused() && item->appId() == appId) {
340  item->setFocused(true);
341  Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);
342  } else if (item->focused() && item->appId() != appId) {
343  item->setFocused(false);
344  Q_EMIT dataChanged(index(i), index(i), QVector<int>() << RoleFocused);
345  }
346  }
347 }