2 * Copyright 2014 Canonical Ltd.
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.
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.
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/>.
17 * Michael Zanetti <michael.zanetti@canonical.com>
20#include "launchermodelas.h"
21#include "launcheritem.h"
22#include "AccountsServiceDBusAdaptor.h"
23#include <unity/shell/application/ApplicationInfoInterface.h>
25#include <QDesktopServices>
27#include <QDBusArgument>
29using namespace unity::shell::application;
31LauncherModel::LauncherModel(QObject *parent):
32 LauncherModelInterface(parent),
33 m_accounts(new AccountsServiceDBusAdaptor(this)),
36 connect(m_accounts, &AccountsServiceDBusAdaptor::propertiesChanged, this, &LauncherModel::propertiesChanged);
40LauncherModel::~LauncherModel()
42 while (!m_list.empty()) {
43 m_list.takeFirst()->deleteLater();
47int LauncherModel::rowCount(const QModelIndex &parent) const
50 return m_list.count();
53QVariant LauncherModel::data(const QModelIndex &index, int role) const
55 LauncherItem *item = m_list.at(index.row());
64 return item->pinned();
67 case RoleCountVisible:
68 return item->countVisible();
70 return item->progress();
72 return item->focused();
78unity::shell::launcher::LauncherItemInterface *LauncherModel::get(int index) const
80 if (index < 0 || index >= m_list.count()) {
83 return m_list.at(index);
86void LauncherModel::move(int oldIndex, int newIndex)
90 qWarning() << "This is a read only implementation. Cannot move items.";
93void LauncherModel::pin(const QString &appId, int index)
97 qWarning() << "This is a read only implementation. Cannot pin items";
100void LauncherModel::requestRemove(const QString &appId)
103 qWarning() << "This is a read only implementation. Cannot remove items";
106void LauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
108 int index = findApplication(appId);
113 LauncherItem *item = m_list.at(index);
114 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
116 QString actionId = model->get(actionIndex).actionId();
118 // Check if this is one of the launcher actions we handle ourselves
119 if (actionId == "launch_item") {
120 QDesktopServices::openUrl(getUrlForAppId(appId));
122 // Nope, we don't know this action, let the backend forward it to the application
124 // TODO: forward quicklist action to app, possibly via m_dbusIface
129void LauncherModel::setUser(const QString &username)
131 if (m_user != username) {
137QString LauncherModel::getUrlForAppId(const QString &appId) const
139 // appId is either an appId or a legacy app name. Let's find out which
140 if (appId.isEmpty()) {
144 if (!appId.contains("_")) {
145 return "application:///" + appId + ".desktop";
148 QStringList parts = appId.split('_');
149 QString package = parts.value(0);
150 QString app = parts.value(1, "first-listed-app");
151 return "appid://" + package + "/" + app + "/current-user-version";
154ApplicationManagerInterface *LauncherModel::applicationManager() const
159void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
162 qWarning() << "This plugin syncs all its states from AccountsService. Not using ApplicationManager.";
166bool LauncherModel::onlyPinned() const
171void LauncherModel::setOnlyPinned(bool onlyPinned)
173 if (m_onlyPinned != onlyPinned) {
174 m_onlyPinned = onlyPinned;
175 Q_EMIT onlyPinnedChanged();
180int LauncherModel::findApplication(const QString &appId)
182 for (int i = 0; i < m_list.count(); ++i) {
183 LauncherItem *item = m_list.at(i);
184 if (item->appId() == appId) {
191typedef QList<QVariantMap> VariantMapList;
192void LauncherModel::refresh()
194 QList<QVariantMap> items;
196 if (m_accounts && !m_user.isEmpty()) {
197 items = m_accounts->getUserProperty<VariantMapList>(m_user, "com.canonical.unity.AccountsService", "LauncherItems");
200 // First walk through all the existing items and see if we need to remove something
201 QList<LauncherItem*> toBeRemoved;
203 Q_FOREACH (LauncherItem* item, m_list) {
205 Q_FOREACH(const QVariant &asItem, items) {
206 if (asItem.toMap().value("id").toString() == item->appId()) {
207 // Only keep and update it if we either show unpinned or it is pinned
208 if (!m_onlyPinned || asItem.toMap().value("pinned").toBool()) {
210 item->setName(asItem.toMap().value("name").toString());
211 item->setIcon(asItem.toMap().value("icon").toString());
212 item->setCount(asItem.toMap().value("count").toInt());
213 item->setCountVisible(asItem.toMap().value("countVisible").toBool());
214 int idx = m_list.indexOf(item);
215 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleName << RoleIcon);
221 toBeRemoved.append(item);
225 Q_FOREACH (LauncherItem* item, toBeRemoved) {
226 int idx = m_list.indexOf(item);
227 beginRemoveRows(QModelIndex(), idx, idx);
228 m_list.takeAt(idx)->deleteLater();
232 // Now walk through list and see if we need to add something
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
242 int newPosition = asIndex - skipped;
245 for (int i = 0; i < m_list.count(); ++i) {
246 if (m_list.at(i)->appId() == entry.toMap().value("id").toString()) {
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(),
258 item->setPinned(true);
259 item->setCount(entry.toMap().value("count").toInt());
260 item->setCountVisible(entry.toMap().value("countVisible").toBool());
261 beginInsertRows(QModelIndex(), newPosition, newPosition);
262 m_list.insert(newPosition, item);
264 } else if (itemIndex != newPosition) {
265 // The item is already there, but it is in a different place than in the settings.
266 // Move it to the addedIndex
267 beginMoveRows(QModelIndex(), itemIndex, itemIndex, QModelIndex(), newPosition);
268 m_list.move(itemIndex, newPosition);
274void LauncherModel::propertiesChanged(const QString &user, const QString &interface, const QStringList &changed)
276 if (user != m_user || interface != "com.canonical.unity.AccountsService" || !changed.contains("LauncherItems")) {
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list