17#include "launchermodelas.h"
18#include "launcheritem.h"
19#include "AccountsServiceDBusAdaptor.h"
20#include <lomiri/shell/application/ApplicationInfoInterface.h>
22#include <QDesktopServices>
24#include <QDBusArgument>
26using namespace lomiri::shell::application;
28LauncherModel::LauncherModel(QObject *parent):
29 LauncherModelInterface(parent),
30 m_accounts(new AccountsServiceDBusAdaptor(this)),
33 connect(m_accounts, &AccountsServiceDBusAdaptor::propertiesChanged,
this, &LauncherModel::propertiesChanged);
37LauncherModel::~LauncherModel()
39 while (!m_list.empty()) {
40 m_list.takeFirst()->deleteLater();
44int LauncherModel::rowCount(
const QModelIndex &parent)
const
47 return m_list.count();
50QVariant LauncherModel::data(
const QModelIndex &index,
int role)
const
52 LauncherItem *item = m_list.at(index.row());
61 return item->pinned();
64 case RoleCountVisible:
65 return item->countVisible();
67 return item->progress();
69 return item->focused();
71 return item->running();
72 case RoleSurfaceCount:
73 return item->surfaceCount();
79lomiri::shell::launcher::LauncherItemInterface *LauncherModel::get(
int index)
const
81 if (index < 0 || index >= m_list.count()) {
84 return m_list.at(index);
87void LauncherModel::move(
int oldIndex,
int newIndex)
91 qWarning() <<
"This is a read only implementation. Cannot move items.";
94void LauncherModel::pin(
const QString &appId,
int index)
98 qWarning() <<
"This is a read only implementation. Cannot pin items";
101void LauncherModel::requestRemove(
const QString &appId)
104 qWarning() <<
"This is a read only implementation. Cannot remove items";
107void LauncherModel::quickListActionInvoked(
const QString &appId,
int actionIndex)
109 int index = findApplication(appId);
114 LauncherItem *item = m_list.at(index);
115 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
117 QString actionId = model->get(actionIndex).actionId();
120 if (actionId == QLatin1String(
"launch_item")) {
121 QDesktopServices::openUrl(getUrlForAppId(appId));
130void LauncherModel::setUser(
const QString &username)
132 if (m_user != username) {
138QString LauncherModel::getUrlForAppId(
const QString &appId)
const
141 if (appId.isEmpty()) {
145 if (!appId.contains(
'_')) {
146 return "application:///" + appId +
".desktop";
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";
155ApplicationManagerInterface *LauncherModel::applicationManager()
const
160void LauncherModel::setApplicationManager(lomiri::shell::application::ApplicationManagerInterface *appManager)
163 qWarning() <<
"This plugin syncs all its states from AccountsService. Not using ApplicationManager.";
167bool LauncherModel::onlyPinned()
const
172void LauncherModel::setOnlyPinned(
bool onlyPinned)
174 if (m_onlyPinned != onlyPinned) {
175 m_onlyPinned = onlyPinned;
176 Q_EMIT onlyPinnedChanged();
181int LauncherModel::findApplication(
const QString &appId)
183 for (
int i = 0; i < m_list.count(); ++i) {
184 LauncherItem *item = m_list.at(i);
185 if (item->appId() == appId) {
192void LauncherModel::refresh()
194 if (!m_accounts || m_user.isEmpty()) {
195 refreshWithItems(QList<QVariantMap>());
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) {
202 QDBusPendingReply<QVariant> reply = *watcher;
203 watcher->deleteLater();
204 if (reply.isError()) {
205 qWarning() <<
"Failed to refresh LauncherItems" << reply.error().message();
209 refreshWithItems(qdbus_cast<QList<QVariantMap>>(reply.value().value<QDBusArgument>()));
214void LauncherModel::refreshWithItems(
const QList<QVariantMap> &items)
217 QList<LauncherItem*> toBeRemoved;
219 Q_FOREACH (LauncherItem* item, m_list) {
221 Q_FOREACH(
const QVariant &asItem, items) {
222 QVariantMap cachedMap = asItem.toMap();
223 if (cachedMap.value(QStringLiteral(
"id")).toString() == item->appId()) {
225 if (!m_onlyPinned || cachedMap.value(QStringLiteral(
"pinned")).toBool()) {
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());
234 int idx = m_list.indexOf(item);
235 Q_EMIT dataChanged(index(idx), index(idx), {RoleName, RoleIcon, RoleCount, RoleCountVisible, RoleRunning, RoleProgress});
241 toBeRemoved.append(item);
245 Q_FOREACH (LauncherItem* item, toBeRemoved) {
246 int idx = m_list.indexOf(item);
247 beginRemoveRows(QModelIndex(), idx, idx);
248 m_list.takeAt(idx)->deleteLater();
254 for (
int asIndex = 0; asIndex < items.count(); ++asIndex) {
255 QVariant entry = items.at(asIndex);
257 if (m_onlyPinned && !entry.toMap().value(QStringLiteral(
"pinned")).toBool()) {
262 int newPosition = asIndex - skipped;
265 for (
int i = 0; i < m_list.count(); ++i) {
266 if (m_list.at(i)->appId() == entry.toMap().value(QStringLiteral(
"id")).toString()) {
272 if (itemIndex == -1) {
273 QVariantMap cachedMap = entry.toMap();
275 LauncherItem *item =
new LauncherItem(cachedMap.value(QStringLiteral(
"id")).toString(),
276 cachedMap.value(QStringLiteral(
"name")).toString(),
277 cachedMap.value(QStringLiteral(
"icon")).toString(),
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);
287 }
else if (itemIndex != newPosition) {
290 beginMoveRows(QModelIndex(), itemIndex, itemIndex, QModelIndex(), newPosition);
291 m_list.move(itemIndex, newPosition);
297void LauncherModel::propertiesChanged(
const QString &user,
const QString &interface,
const QStringList &changed)
299 if (user != m_user || interface != QLatin1String(
"com.lomiri.shell.AccountsService") || !changed.contains(QStringLiteral(
"LauncherItems"))) {