20 #include "launchermodel.h"
21 #include "launcheritem.h"
22 #include "gsettings.h"
23 #include "desktopfilehandler.h"
24 #include "dbusinterface.h"
25 #include "asadapter.h"
27 #include <unity/shell/application/ApplicationInfoInterface.h>
29 #include <QDesktopServices>
34 LauncherModel::LauncherModel(QObject *parent):
35 LauncherModelInterface(parent),
36 m_settings(new GSettings(this)),
37 m_dbusIface(new DBusInterface(this)),
38 m_asAdapter(new ASAdapter()),
41 connect(m_dbusIface, &DBusInterface::countChanged,
this, &LauncherModel::countChanged);
42 connect(m_dbusIface, &DBusInterface::countVisibleChanged,
this, &LauncherModel::countVisibleChanged);
43 connect(m_dbusIface, &DBusInterface::progressChanged,
this, &LauncherModel::progressChanged);
44 connect(m_dbusIface, &DBusInterface::refreshCalled,
this, &LauncherModel::refresh);
45 connect(m_dbusIface, &DBusInterface::alertCalled,
this, &LauncherModel::alert);
47 connect(m_settings, &GSettings::changed,
this, &LauncherModel::refresh);
52 LauncherModel::~LauncherModel()
54 while (!m_list.empty()) {
55 m_list.takeFirst()->deleteLater();
61 int LauncherModel::rowCount(
const QModelIndex &parent)
const
64 return m_list.count();
67 QVariant LauncherModel::data(const QModelIndex &index,
int role)
const
69 LauncherItem *item = m_list.at(index.row());
78 return item->pinned();
81 case RoleCountVisible:
82 return item->countVisible();
84 return item->progress();
86 return item->focused();
88 return item->alerting();
90 return item->running();
92 qWarning() << Q_FUNC_INFO <<
"missing role, implement me";
99 void LauncherModel::setAlerting(
const QString &appId,
bool alerting) {
100 int index = findApplication(appId);
102 QModelIndex modelIndex = this->index(index);
103 LauncherItem *item = m_list.at(index);
104 if (!item->focused()) {
105 item->setAlerting(alerting);
106 Q_EMIT dataChanged(modelIndex, modelIndex, QVector<int>() << RoleAlerting);
111 unity::shell::launcher::LauncherItemInterface *LauncherModel::get(
int index)
const
113 if (index < 0 || index >= m_list.count()) {
116 return m_list.at(index);
119 void LauncherModel::move(
int oldIndex,
int newIndex)
125 if (newIndex >= m_list.count()) {
126 newIndex = m_list.count()-1;
130 if (oldIndex == newIndex) {
137 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
139 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
140 m_list.move(oldIndex, newIndex);
143 if (!m_list.at(newIndex)->pinned()) {
144 pin(m_list.at(newIndex)->appId());
150 void LauncherModel::pin(
const QString &appId,
int index)
152 int currentIndex = findApplication(appId);
154 if (currentIndex >= 0) {
155 if (index == -1 || index == currentIndex) {
156 m_list.at(currentIndex)->setPinned(
true);
157 QModelIndex modelIndex = this->index(currentIndex);
158 Q_EMIT dataChanged(modelIndex, modelIndex, {RolePinned});
160 move(currentIndex, index);
166 index = m_list.count();
170 if (!desktopFile.isValid()) {
171 qWarning() <<
"Can't pin this application, there is no .desktop file available.";
175 beginInsertRows(QModelIndex(), index, index);
176 LauncherItem *item =
new LauncherItem(appId,
177 desktopFile.displayName(),
180 item->setPinned(
true);
181 m_list.insert(index, item);
188 void LauncherModel::requestRemove(
const QString &appId)
194 void LauncherModel::quickListActionInvoked(
const QString &appId,
int actionIndex)
196 const int index = findApplication(appId);
201 LauncherItem *item = m_list.at(index);
202 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
204 const QString actionId = model->get(actionIndex).actionId();
207 if (actionId == QLatin1String(
"pin_item")) {
208 if (item->pinned()) {
209 requestRemove(appId);
213 }
else if (actionId == QLatin1String(
"launch_item")) {
214 QDesktopServices::openUrl(getUrlForAppId(appId));
215 }
else if (actionId == QLatin1String(
"stop_item")) {
217 m_appManager->stopApplication(appId);
226 void LauncherModel::setUser(
const QString &username)
229 qWarning() << "This backend doesn't support multiple users";
232 QString LauncherModel::getUrlForAppId(const QString &appId)
const
235 if (appId.isEmpty()) {
239 if (!appId.contains(
'_')) {
240 return "application:///" + appId +
".desktop";
243 QStringList parts = appId.split(
'_');
244 QString
package = parts.value(0);
245 QString app = parts.value(1, QStringLiteral(
"first-listed-app"));
246 return "appid://" +
package + "/" + app + "/current-user-version";
249 ApplicationManagerInterface *LauncherModel::applicationManager()
const
254 void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
259 disconnect(
this, &LauncherModel::applicationAdded, 0,
nullptr);
260 disconnect(
this, &LauncherModel::applicationRemoved, 0,
nullptr);
261 disconnect(
this, &LauncherModel::focusedAppIdChanged, 0,
nullptr);
264 QList<int> recentAppIndices;
265 for (
int i = 0; i < m_list.count(); ++i) {
266 if (m_list.at(i)->recent()) {
267 recentAppIndices << i;
271 while (recentAppIndices.count() > 0) {
272 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
273 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
275 recentAppIndices.takeFirst();
280 m_appManager = appManager;
281 connect(m_appManager, &ApplicationManagerInterface::rowsInserted,
this, &LauncherModel::applicationAdded);
282 connect(m_appManager, &ApplicationManagerInterface::rowsAboutToBeRemoved,
this, &LauncherModel::applicationRemoved);
283 connect(m_appManager, &ApplicationManagerInterface::focusedApplicationIdChanged,
this, &LauncherModel::focusedAppIdChanged);
285 Q_EMIT applicationManagerChanged();
287 for (
int i = 0; i < appManager->count(); ++i) {
288 applicationAdded(QModelIndex(), i);
292 bool LauncherModel::onlyPinned()
const
297 void LauncherModel::setOnlyPinned(
bool onlyPinned) {
298 Q_UNUSED(onlyPinned);
299 qWarning() <<
"This launcher implementation does not support showing only pinned apps";
302 void LauncherModel::storeAppList()
305 Q_FOREACH(LauncherItem *item, m_list) {
306 if (item->pinned()) {
307 appIds << item->appId();
310 m_settings->setStoredApplications(appIds);
311 m_asAdapter->syncItems(m_list);
314 void LauncherModel::unpin(
const QString &appId)
316 const int index = findApplication(appId);
321 if (m_appManager->findApplication(appId)) {
322 if (m_list.at(index)->pinned()) {
323 m_list.at(index)->setPinned(
false);
324 QModelIndex modelIndex = this->index(index);
325 Q_EMIT dataChanged(modelIndex, modelIndex, {RolePinned});
328 beginRemoveRows(QModelIndex(), index, index);
329 m_list.takeAt(index)->deleteLater();
334 int LauncherModel::findApplication(
const QString &appId)
336 for (
int i = 0; i < m_list.count(); ++i) {
337 LauncherItem *item = m_list.at(i);
338 if (item->appId() == appId) {
345 void LauncherModel::progressChanged(
const QString &appId,
int progress)
347 const int idx = findApplication(appId);
349 LauncherItem *item = m_list.at(idx);
350 item->setProgress(progress);
351 Q_EMIT dataChanged(index(idx), index(idx), {RoleProgress});
355 void LauncherModel::countChanged(
const QString &appId,
int count)
357 const int idx = findApplication(appId);
359 LauncherItem *item = m_list.at(idx);
360 item->setCount(count);
361 if (item->countVisible()) {
362 setAlerting(item->appId(),
true);
364 m_asAdapter->syncItems(m_list);
365 Q_EMIT dataChanged(index(idx), index(idx), {RoleCount});
369 void LauncherModel::countVisibleChanged(
const QString &appId,
bool countVisible)
371 int idx = findApplication(appId);
373 LauncherItem *item = m_list.at(idx);
374 item->setCountVisible(countVisible);
376 setAlerting(item->appId(),
true);
378 Q_EMIT dataChanged(index(idx), index(idx), {RoleCountVisible});
381 if (!countVisible && !item->pinned() && !item->recent()) {
382 beginRemoveRows(QModelIndex(), idx, idx);
383 m_list.takeAt(idx)->deleteLater();
389 if (countVisible && desktopFile.isValid()) {
390 LauncherItem *item =
new LauncherItem(appId,
391 desktopFile.displayName(),
393 item->setCountVisible(
true);
394 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
399 m_asAdapter->syncItems(m_list);
402 void LauncherModel::refresh()
405 QList<LauncherItem*> toBeRemoved;
406 Q_FOREACH (LauncherItem* item, m_list) {
408 if (!desktopFile.isValid()) {
411 }
else if (!m_settings->storedApplications().contains(item->appId())) {
415 int idx = m_list.indexOf(item);
416 item->setName(desktopFile.displayName());
417 item->setIcon(desktopFile.icon());
418 item->setPinned(item->pinned());
419 item->setRunning(item->running());
420 Q_EMIT dataChanged(index(idx), index(idx), {RoleName, RoleIcon, RoleRunning});
424 Q_FOREACH (LauncherItem* item, toBeRemoved) {
425 unpin(item->appId());
428 bool changed = toBeRemoved.count() > 0;
437 for (
int settingsIndex = 0; settingsIndex < m_settings->storedApplications().count(); ++settingsIndex) {
438 const QString entry = m_settings->storedApplications().at(settingsIndex);
440 for (
int i = 0; i < m_list.count(); ++i) {
441 if (m_list.at(i)->appId() == entry) {
447 if (itemIndex == -1) {
451 if (!desktopFile.isValid()) {
452 qWarning() <<
"Couldn't find a .desktop file for" << entry <<
". Skipping...";
456 LauncherItem *item =
new LauncherItem(entry,
457 desktopFile.displayName(),
460 item->setPinned(
true);
461 beginInsertRows(QModelIndex(), addedIndex, addedIndex);
462 m_list.insert(addedIndex, item);
465 }
else if (itemIndex != addedIndex) {
468 beginMoveRows(QModelIndex(), itemIndex, itemIndex, QModelIndex(), addedIndex);
469 m_list.move(itemIndex, addedIndex);
483 m_asAdapter->syncItems(m_list);
486 void LauncherModel::alert(
const QString &appId)
488 int idx = findApplication(appId);
490 LauncherItem *item = m_list.at(idx);
491 setAlerting(item->appId(),
true);
492 Q_EMIT dataChanged(index(idx), index(idx), QVector<int>() << RoleAlerting);
496 void LauncherModel::applicationAdded(
const QModelIndex &parent,
int row)
500 ApplicationInfoInterface *app = m_appManager->get(row);
502 qWarning() <<
"LauncherModel received an applicationAdded signal, but there's no such application!";
506 if (app->appId() == QLatin1String(
"unity8-dash")) {
511 const int itemIndex = findApplication(app->appId());
512 if (itemIndex != -1) {
513 LauncherItem *item = m_list.at(itemIndex);
514 if (!item->recent()) {
515 item->setRecent(
true);
516 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), {RoleRecent});
518 item->setRunning(
true);
520 LauncherItem *item =
new LauncherItem(app->appId(), app->name(), app->icon().toString(),
this);
521 item->setRecent(
true);
522 item->setRunning(
true);
523 item->setFocused(app->focused());
525 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
529 m_asAdapter->syncItems(m_list);
530 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), {RoleRunning});
533 void LauncherModel::applicationRemoved(
const QModelIndex &parent,
int row)
538 for (
int i = 0; i < m_list.count(); ++i) {
539 if (m_list.at(i)->appId() == m_appManager->get(row)->appId()) {
546 qWarning() << Q_FUNC_INFO <<
"appIndex not found";
550 LauncherItem * item = m_list.at(appIndex);
551 item->setRunning(
false);
553 if (!item->pinned()) {
554 beginRemoveRows(QModelIndex(), appIndex, appIndex);
555 m_list.takeAt(appIndex)->deleteLater();
557 m_asAdapter->syncItems(m_list);
558 Q_EMIT dataChanged(index(appIndex), index(appIndex), {RolePinned});
560 Q_EMIT dataChanged(index(appIndex), index(appIndex), {RoleRunning});
563 void LauncherModel::focusedAppIdChanged()
565 const QString appId = m_appManager->focusedApplicationId();
566 for (
int i = 0; i < m_list.count(); ++i) {
567 LauncherItem *item = m_list.at(i);
568 if (!item->focused() && item->appId() == appId) {
569 item->setFocused(
true);
570 Q_EMIT dataChanged(index(i), index(i), {RoleFocused});
571 }
else if (item->focused() && item->appId() != appId) {
572 item->setFocused(
false);
573 Q_EMIT dataChanged(index(i), index(i), {RoleFocused});