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