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