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