Unity 8
launchermodelas.cpp
1 /*
2  * Copyright 2014-2015 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 "launchermodelas.h"
18 #include "launcheritem.h"
19 #include "AccountsServiceDBusAdaptor.h"
20 #include <unity/shell/application/ApplicationInfoInterface.h>
21 
22 #include <QDesktopServices>
23 #include <QDebug>
24 #include <QDBusArgument>
25 
26 using namespace unity::shell::application;
27 
28 LauncherModel::LauncherModel(QObject *parent):
29  LauncherModelInterface(parent),
30  m_accounts(new AccountsServiceDBusAdaptor(this)),
31  m_onlyPinned(true)
32 {
33  connect(m_accounts, &AccountsServiceDBusAdaptor::propertiesChanged, this, &LauncherModel::propertiesChanged);
34  refresh();
35 }
36 
37 LauncherModel::~LauncherModel()
38 {
39  while (!m_list.empty()) {
40  m_list.takeFirst()->deleteLater();
41  }
42 }
43 
44 int LauncherModel::rowCount(const QModelIndex &parent) const
45 {
46  Q_UNUSED(parent)
47  return m_list.count();
48 }
49 
50 QVariant LauncherModel::data(const QModelIndex &index, int role) const
51 {
52  LauncherItem *item = m_list.at(index.row());
53  switch(role) {
54  case RoleAppId:
55  return item->appId();
56  case RoleName:
57  return item->name();
58  case RoleIcon:
59  return item->icon();
60  case RolePinned:
61  return item->pinned();
62  case RoleCount:
63  return item->count();
64  case RoleCountVisible:
65  return item->countVisible();
66  case RoleProgress:
67  return item->progress();
68  case RoleFocused:
69  return item->focused();
70  case RoleRunning:
71  return item->running();
72  }
73 
74  return QVariant();
75 }
76 
77 void LauncherModel::setAlerting(const QString &appId, bool alerting)
78 {
79  Q_UNUSED(appId)
80  Q_UNUSED(alerting)
81  qWarning() << "This is a read only implementation. Cannot set alert-state of items.";
82 }
83 
84 unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
85 {
86  if (index < 0 || index >= m_list.count()) {
87  return 0;
88  }
89  return m_list.at(index);
90 }
91 
92 void LauncherModel::move(int oldIndex, int newIndex)
93 {
94  Q_UNUSED(oldIndex)
95  Q_UNUSED(newIndex)
96  qWarning() << "This is a read only implementation. Cannot move items.";
97 }
98 
99 void LauncherModel::pin(const QString &appId, int index)
100 {
101  Q_UNUSED(appId)
102  Q_UNUSED(index)
103  qWarning() << "This is a read only implementation. Cannot pin items";
104 }
105 
106 void LauncherModel::requestRemove(const QString &appId)
107 {
108  Q_UNUSED(appId)
109  qWarning() << "This is a read only implementation. Cannot remove items";
110 }
111 
112 void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
113 {
114  int index = findApplication(appId);
115  if (index < 0) {
116  return;
117  }
118 
119  LauncherItem *item = m_list.at(index);
120  QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
121  if (model) {
122  QString actionId = model->get(actionIndex).actionId();
123 
124  // Check if this is one of the launcher actions we handle ourselves
125  if (actionId == QLatin1String("launch_item")) {
126  QDesktopServices::openUrl(getUrlForAppId(appId));
127 
128  // Nope, we don't know this action, let the backend forward it to the application
129  } else {
130  // TODO: forward quicklist action to app, possibly via m_dbusIface
131  }
132  }
133 }
134 
135 void LauncherModel::setUser(const QString &username)
136 {
137  if (m_user != username) {
138  m_user = username;
139  refresh();
140  }
141 }
142 
143 QString LauncherModel::getUrlForAppId(const QString &appId) const
144 {
145  // appId is either an appId or a legacy app name. Let's find out which
146  if (appId.isEmpty()) {
147  return QString();
148  }
149 
150  if (!appId.contains('_')) {
151  return "application:///" + appId + ".desktop";
152  }
153 
154  QStringList parts = appId.split('_');
155  QString package = parts.value(0);
156  QString app = parts.value(1, QStringLiteral("first-listed-app"));
157  return "appid://" + package + "/" + app + "/current-user-version";
158 }
159 
160 ApplicationManagerInterface *LauncherModel::applicationManager() const
161 {
162  return nullptr;
163 }
164 
165 void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
166 {
167  Q_UNUSED(appManager)
168  qWarning() << "This plugin syncs all its states from AccountsService. Not using ApplicationManager.";
169  return;
170 }
171 
172 bool LauncherModel::onlyPinned() const
173 {
174  return m_onlyPinned;
175 }
176 
177 void LauncherModel::setOnlyPinned(bool onlyPinned)
178 {
179  if (m_onlyPinned != onlyPinned) {
180  m_onlyPinned = onlyPinned;
181  Q_EMIT onlyPinnedChanged();
182  refresh();
183  }
184 }
185 
186 int LauncherModel::findApplication(const QString &appId)
187 {
188  for (int i = 0; i < m_list.count(); ++i) {
189  LauncherItem *item = m_list.at(i);
190  if (item->appId() == appId) {
191  return i;
192  }
193  }
194  return -1;
195 }
196 
197 typedef QList<QVariantMap> VariantMapList;
198 void LauncherModel::refresh()
199 {
200  QList<QVariantMap> items;
201 
202  if (m_accounts && !m_user.isEmpty()) {
203  items = m_accounts->getUserProperty<VariantMapList>(m_user, QStringLiteral("com.canonical.unity.AccountsService"), QStringLiteral("LauncherItems"));
204  }
205 
206  // First walk through all the existing items and see if we need to remove something
207  QList<LauncherItem*> toBeRemoved;
208 
209  Q_FOREACH (LauncherItem* item, m_list) {
210  bool found = false;
211  Q_FOREACH(const QVariant &asItem, items) {
212  QVariantMap cachedMap = asItem.toMap();
213  if (cachedMap.value(QStringLiteral("id")).toString() == item->appId()) {
214  // Only keep and update it if we either show unpinned or it is pinned
215  if (!m_onlyPinned || cachedMap.value(QStringLiteral("pinned")).toBool()) {
216  found = true;
217  item->setName(cachedMap.value(QStringLiteral("name")).toString());
218  item->setIcon(cachedMap.value(QStringLiteral("icon")).toString());
219  item->setCount(cachedMap.value(QStringLiteral("count")).toInt());
220  item->setCountVisible(cachedMap.value(QStringLiteral("countVisible")).toBool());
221  item->setProgress(cachedMap.value(QStringLiteral("progress")).toInt());
222  item->setRunning(cachedMap.value(QStringLiteral("running")).toBool());
223 
224  int idx = m_list.indexOf(item);
225  Q_EMIT dataChanged(index(idx), index(idx), {RoleName, RoleIcon, RoleCount, RoleCountVisible, RoleRunning, RoleProgress});
226  }
227  break;
228  }
229  }
230  if (!found) {
231  toBeRemoved.append(item);
232  }
233  }
234 
235  Q_FOREACH (LauncherItem* item, toBeRemoved) {
236  int idx = m_list.indexOf(item);
237  beginRemoveRows(QModelIndex(), idx, idx);
238  m_list.takeAt(idx)->deleteLater();
239  endRemoveRows();
240  }
241 
242  // Now walk through list and see if we need to add something
243  int skipped = 0;
244  for (int asIndex = 0; asIndex < items.count(); ++asIndex) {
245  QVariant entry = items.at(asIndex);
246 
247  if (m_onlyPinned && !entry.toMap().value(QStringLiteral("pinned")).toBool()) {
248  // Skipping it as we only show pinned and it is not
249  skipped++;
250  continue;
251  }
252  int newPosition = asIndex - skipped;
253 
254  int itemIndex = -1;
255  for (int i = 0; i < m_list.count(); ++i) {
256  if (m_list.at(i)->appId() == entry.toMap().value(QStringLiteral("id")).toString()) {
257  itemIndex = i;
258  break;
259  }
260  }
261 
262  if (itemIndex == -1) {
263  QVariantMap cachedMap = entry.toMap();
264  // Need to add it. Just add it into the addedIndex to keep same ordering as the list in AS.
265  LauncherItem *item = new LauncherItem(cachedMap.value(QStringLiteral("id")).toString(),
266  cachedMap.value(QStringLiteral("name")).toString(),
267  cachedMap.value(QStringLiteral("icon")).toString(),
268  this);
269  item->setPinned(true);
270  item->setCount(cachedMap.value(QStringLiteral("count")).toInt());
271  item->setCountVisible(cachedMap.value(QStringLiteral("countVisible")).toBool());
272  item->setProgress(cachedMap.value(QStringLiteral("progress")).toInt());
273  item->setRunning(cachedMap.value(QStringLiteral("running")).toBool());
274  beginInsertRows(QModelIndex(), newPosition, newPosition);
275  m_list.insert(newPosition, item);
276  endInsertRows();
277  } else if (itemIndex != newPosition) {
278  // The item is already there, but it is in a different place than in the settings.
279  // Move it to the addedIndex
280  beginMoveRows(QModelIndex(), itemIndex, itemIndex, QModelIndex(), newPosition);
281  m_list.move(itemIndex, newPosition);
282  endMoveRows();
283  }
284  }
285 }
286 
287 void LauncherModel::propertiesChanged(const QString &user, const QString &interface, const QStringList &changed)
288 {
289  if (user != m_user || interface != QLatin1String("com.canonical.unity.AccountsService") || !changed.contains(QStringLiteral("LauncherItems"))) {
290  return;
291  }
292  refresh();
293 }