on a63f337 help sign in
unity8/plugins/Greeter/Unity/Launcher/launchermodelas.cpp
294 lines 9.5 kB history raw
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 */
17#include "launchermodelas.h"
18#include "launcheritem.h"
19#include "AccountsServiceDBusAdaptor.h"
20#include <unity/shell/application/ApplicationInfoInterface.h>
22#include <QDesktopServices>
23#include <QDebug>
24#include <QDBusArgument>
26using namespace unity::shell::application;
28LauncherModel::LauncherModel(QObject *parent):
29 LauncherModelInterface(parent),
30 m_accounts(new AccountsServiceDBusAdaptor(this)),
31 m_onlyPinned(true)
33 connect(m_accounts, &AccountsServiceDBusAdaptor::propertiesChanged, this, &LauncherModel::propertiesChanged);
34 refresh();
37LauncherModel::~LauncherModel()
39 while (!m_list.empty()) {
40 m_list.takeFirst()->deleteLater();
41 }
44int LauncherModel::rowCount(const QModelIndex &parent) const
46 Q_UNUSED(parent)
47 return m_list.count();
50QVariant LauncherModel::data(const QModelIndex &index, int role) const
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 }
72 return QVariant();
75void LauncherModel::setAlerting(const QString &appId, bool alerting)
77 Q_UNUSED(appId)
78 Q_UNUSED(alerting)
79 qWarning() << "This is a read only implementation. Cannot set alert-state of items.";
82unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
84 if (index < 0 || index >= m_list.count()) {
85 return 0;
86 }
87 return m_list.at(index);
90void LauncherModel::move(int oldIndex, int newIndex)
92 Q_UNUSED(oldIndex)
93 Q_UNUSED(newIndex)
94 qWarning() << "This is a read only implementation. Cannot move items.";
97void LauncherModel::pin(const QString &appId, int index)
99 Q_UNUSED(appId)
100 Q_UNUSED(index)
101 qWarning() << "This is a read only implementation. Cannot pin items";
104void LauncherModel::requestRemove(const QString &appId)
106 Q_UNUSED(appId)
107 qWarning() << "This is a read only implementation. Cannot remove items";
110void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
112 int index = findApplication(appId);
113 if (index < 0) {
114 return;
115 }
117 LauncherItem *item = m_list.at(index);
118 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
119 if (model) {
120 QString actionId = model->get(actionIndex).actionId();
122 // Check if this is one of the launcher actions we handle ourselves
123 if (actionId == "launch_item") {
124 QDesktopServices::openUrl(getUrlForAppId(appId));
126 // Nope, we don't know this action, let the backend forward it to the application
127 } else {
128 // TODO: forward quicklist action to app, possibly via m_dbusIface
129 }
130 }
133void LauncherModel::setUser(const QString &username)
135 if (m_user != username) {
136 m_user = username;
137 refresh();
138 }
141QString LauncherModel::getUrlForAppId(const QString &appId) const
143 // appId is either an appId or a legacy app name. Let's find out which
144 if (appId.isEmpty()) {
145 return QString();
146 }
148 if (!appId.contains("_")) {
149 return "application:///" + appId + ".desktop";
150 }
152 QStringList parts = appId.split('_');
153 QString package = parts.value(0);
154 QString app = parts.value(1, "first-listed-app");
155 return "appid://" + package + "/" + app + "/current-user-version";
158ApplicationManagerInterface *LauncherModel::applicationManager() const
160 return nullptr;
163void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
165 Q_UNUSED(appManager)
166 qWarning() << "This plugin syncs all its states from AccountsService. Not using ApplicationManager.";
167 return;
170bool LauncherModel::onlyPinned() const
172 return m_onlyPinned;
175void LauncherModel::setOnlyPinned(bool onlyPinned)
177 if (m_onlyPinned != onlyPinned) {
178 m_onlyPinned = onlyPinned;
179 Q_EMIT onlyPinnedChanged();
180 refresh();
181 }
184int LauncherModel::findApplication(const QString &appId)
186 for (int i = 0; i < m_list.count(); ++i) {
187 LauncherItem *item = m_list.at(i);
188 if (item->appId() == appId) {
189 return i;
190 }
191 }
192 return -1;
195typedef QList<QVariantMap> VariantMapList;
196void LauncherModel::refresh()
198 QList<QVariantMap> items;
200 if (m_accounts && !m_user.isEmpty()) {
201 items = m_accounts->getUserProperty<VariantMapList>(m_user, "com.canonical.unity.AccountsService", "LauncherItems");
202 }
204 // First walk through all the existing items and see if we need to remove something
205 QList<LauncherItem*> toBeRemoved;
207 Q_FOREACH (LauncherItem* item, m_list) {
208 bool found = false;
209 Q_FOREACH(const QVariant &asItem, items) {
210 QVariantMap cachedMap = asItem.toMap();
211 if (cachedMap.value("id").toString() == item->appId()) {
212 // Only keep and update it if we either show unpinned or it is pinned
213 if (!m_onlyPinned || cachedMap.value("pinned").toBool()) {
214 found = true;
215 item->setName(cachedMap.value("name").toString());
216 item->setIcon(cachedMap.value("icon").toString());
217 item->setCount(cachedMap.value("count").toInt());
218 item->setCountVisible(cachedMap.value("countVisible").toBool());
219 item->setProgress(cachedMap.value("progress").toInt());
220 int idx = m_list.indexOf(item);
221 Q_EMIT dataChanged(index(idx),
222 index(idx),
223 {RoleName,
224 RoleIcon,
225 RoleCount,
226 RoleCountVisible,
227 RoleProgress});
228 }
229 break;
230 }
231 }
232 if (!found) {
233 toBeRemoved.append(item);
234 }
235 }
237 Q_FOREACH (LauncherItem* item, toBeRemoved) {
238 int idx = m_list.indexOf(item);
239 beginRemoveRows(QModelIndex(), idx, idx);
240 m_list.takeAt(idx)->deleteLater();
241 endRemoveRows();
242 }
244 // Now walk through list and see if we need to add something
245 int skipped = 0;
246 for (int asIndex = 0; asIndex < items.count(); ++asIndex) {
247 QVariant entry = items.at(asIndex);
249 if (m_onlyPinned && !entry.toMap().value("pinned").toBool()) {
250 // Skipping it as we only show pinned and it is not
251 skipped++;
252 continue;
253 }
254 int newPosition = asIndex - skipped;
256 int itemIndex = -1;
257 for (int i = 0; i < m_list.count(); ++i) {
258 if (m_list.at(i)->appId() == entry.toMap().value("id").toString()) {
259 itemIndex = i;
260 break;
261 }
262 }
264 if (itemIndex == -1) {
265 QVariantMap chachedMap = entry.toMap();
266 // Need to add it. Just add it into the addedIndex to keep same ordering as the list in AS.
267 LauncherItem *item = new LauncherItem(chachedMap.value("id").toString(),
268 chachedMap.value("name").toString(),
269 chachedMap.value("icon").toString(),
270 this);
271 item->setPinned(true);
272 item->setCount(chachedMap.value("count").toInt());
273 item->setCountVisible(chachedMap.value("countVisible").toBool());
274 item->setProgress(chachedMap.value("progress").toInt());
275 beginInsertRows(QModelIndex(), newPosition, newPosition);
276 m_list.insert(newPosition, item);
277 endInsertRows();
278 } else if (itemIndex != newPosition) {
279 // The item is already there, but it is in a different place than in the settings.
280 // Move it to the addedIndex
281 beginMoveRows(QModelIndex(), itemIndex, itemIndex, QModelIndex(), newPosition);
282 m_list.move(itemIndex, newPosition);
283 endMoveRows();
284 }
285 }
288void LauncherModel::propertiesChanged(const QString &user, const QString &interface, const QStringList &changed)
290 if (user != m_user || interface != "com.canonical.unity.AccountsService" || !changed.contains("LauncherItems")) {
291 return;
292 }
293 refresh();