17 #include "launchermodel.h" 18 #include "launcheritem.h" 19 #include "gsettings.h" 20 #include "dbusinterface.h" 21 #include "asadapter.h" 22 #include "ualwrapper.h" 24 #include <unity/shell/application/ApplicationInfoInterface.h> 25 #include <unity/shell/application/MirSurfaceListInterface.h> 27 #include <QDesktopServices> 32 LauncherModel::LauncherModel(QObject *parent):
33 LauncherModelInterface(parent),
34 m_settings(new GSettings(this)),
35 m_dbusIface(new DBusInterface(this)),
36 m_asAdapter(new ASAdapter()),
39 connect(m_dbusIface, &DBusInterface::countChanged,
this, &LauncherModel::countChanged);
40 connect(m_dbusIface, &DBusInterface::countVisibleChanged,
this, &LauncherModel::countVisibleChanged);
41 connect(m_dbusIface, &DBusInterface::progressChanged,
this, &LauncherModel::progressChanged);
42 connect(m_dbusIface, &DBusInterface::refreshCalled,
this, &LauncherModel::refresh);
43 connect(m_dbusIface, &DBusInterface::alertCalled,
this, &LauncherModel::alert);
45 connect(m_settings, &GSettings::changed,
this, &LauncherModel::refresh);
50 LauncherModel::~LauncherModel()
52 while (!m_list.empty()) {
53 m_list.takeFirst()->deleteLater();
59 int LauncherModel::rowCount(
const QModelIndex &parent)
const 62 return m_list.count();
65 QVariant LauncherModel::data(const QModelIndex &index,
int role)
const 67 LauncherItem *item = m_list.at(index.row());
76 return item->pinned();
79 case RoleCountVisible:
80 return item->countVisible();
82 return item->progress();
84 return item->focused();
86 return item->alerting();
88 return item->running();
89 case RoleSurfaceCount:
90 return item->surfaceCount();
92 qWarning() << Q_FUNC_INFO <<
"missing role, implement me";
99 unity::shell::launcher::LauncherItemInterface *LauncherModel::get(
int index)
const 101 if (index < 0 || index >= m_list.count()) {
104 return m_list.at(index);
107 void LauncherModel::move(
int oldIndex,
int newIndex)
113 if (newIndex >= m_list.count()) {
114 newIndex = m_list.count()-1;
118 if (oldIndex == newIndex) {
125 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
127 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
128 m_list.move(oldIndex, newIndex);
131 if (!m_list.at(newIndex)->pinned()) {
132 pin(m_list.at(newIndex)->appId());
138 void LauncherModel::pin(
const QString &appId,
int index)
140 int currentIndex = findApplication(appId);
142 if (currentIndex >= 0) {
143 if (index == -1 || index == currentIndex) {
144 m_list.at(currentIndex)->setPinned(
true);
145 QModelIndex modelIndex = this->index(currentIndex);
146 Q_EMIT dataChanged(modelIndex, modelIndex, {RolePinned});
148 move(currentIndex, index);
154 index = m_list.count();
157 UalWrapper::AppInfo appInfo = UalWrapper::getApplicationInfo(appId);
158 if (!appInfo.valid) {
159 qWarning() <<
"Can't pin application, appId not found:" << appId;
163 beginInsertRows(QModelIndex(), index, index);
164 LauncherItem *item =
new LauncherItem(appId,
168 item->setPinned(
true);
169 m_list.insert(index, item);
176 void LauncherModel::requestRemove(
const QString &appId)
182 void LauncherModel::quickListActionInvoked(
const QString &appId,
int actionIndex)
184 const int index = findApplication(appId);
189 LauncherItem *item = m_list.at(index);
190 QuickListModel *model = qobject_cast<QuickListModel*>(item->quickList());
192 const QString actionId = model->get(actionIndex).actionId();
195 if (actionId == QLatin1String(
"pin_item")) {
196 if (item->pinned()) {
197 requestRemove(appId);
201 }
else if (actionId == QLatin1String(
"launch_item")) {
202 QDesktopServices::openUrl(getUrlForAppId(appId));
203 }
else if (actionId == QLatin1String(
"stop_item")) {
205 m_appManager->stopApplication(appId);
214 void LauncherModel::setUser(
const QString &username)
217 qWarning() << "This backend doesn't support multiple users";
220 QString LauncherModel::getUrlForAppId(const QString &appId)
const 223 if (appId.isEmpty()) {
227 if (!appId.contains(
'_')) {
228 return "application:///" + appId +
".desktop";
231 QStringList parts = appId.split(
'_');
232 QString
package = parts.value(0);
233 QString app = parts.value(1, QStringLiteral(
"first-listed-app"));
234 return "appid://" +
package + "/" + app + "/current-user-version";
237 ApplicationManagerInterface *LauncherModel::applicationManager()
const 242 void LauncherModel::setApplicationManager(unity::shell::application::ApplicationManagerInterface *appManager)
247 disconnect(
this, &LauncherModel::applicationAdded, 0,
nullptr);
248 disconnect(
this, &LauncherModel::applicationRemoved, 0,
nullptr);
249 disconnect(
this, &LauncherModel::focusedAppIdChanged, 0,
nullptr);
252 QList<int> recentAppIndices;
253 for (
int i = 0; i < m_list.count(); ++i) {
254 if (m_list.at(i)->recent()) {
255 recentAppIndices << i;
259 while (recentAppIndices.count() > 0) {
260 beginRemoveRows(QModelIndex(), recentAppIndices.first() - run, recentAppIndices.first() - run);
261 m_list.takeAt(recentAppIndices.first() - run)->deleteLater();
263 recentAppIndices.takeFirst();
268 m_appManager = appManager;
269 connect(m_appManager, &ApplicationManagerInterface::rowsInserted,
this, &LauncherModel::applicationAdded);
270 connect(m_appManager, &ApplicationManagerInterface::rowsAboutToBeRemoved,
this, &LauncherModel::applicationRemoved);
271 connect(m_appManager, &ApplicationManagerInterface::focusedApplicationIdChanged,
this, &LauncherModel::focusedAppIdChanged);
273 Q_EMIT applicationManagerChanged();
275 for (
int i = 0; i < appManager->count(); ++i) {
276 applicationAdded(QModelIndex(), i);
280 bool LauncherModel::onlyPinned()
const 285 void LauncherModel::setOnlyPinned(
bool onlyPinned) {
286 Q_UNUSED(onlyPinned);
287 qWarning() <<
"This launcher implementation does not support showing only pinned apps";
290 void LauncherModel::storeAppList()
293 Q_FOREACH(LauncherItem *item, m_list) {
294 if (item->pinned()) {
295 appIds << item->appId();
298 m_settings->setStoredApplications(appIds);
299 m_asAdapter->syncItems(m_list);
302 void LauncherModel::unpin(
const QString &appId)
304 const int index = findApplication(appId);
309 if (m_appManager->findApplication(appId)) {
310 if (m_list.at(index)->pinned()) {
311 m_list.at(index)->setPinned(
false);
312 QModelIndex modelIndex = this->index(index);
313 Q_EMIT dataChanged(modelIndex, modelIndex, {RolePinned});
316 beginRemoveRows(QModelIndex(), index, index);
317 m_list.takeAt(index)->deleteLater();
322 int LauncherModel::findApplication(
const QString &appId)
324 for (
int i = 0; i < m_list.count(); ++i) {
325 LauncherItem *item = m_list.at(i);
326 if (item->appId() == appId) {
333 void LauncherModel::progressChanged(
const QString &appId,
int progress)
335 const int idx = findApplication(appId);
337 LauncherItem *item = m_list.at(idx);
338 item->setProgress(progress);
339 Q_EMIT dataChanged(index(idx), index(idx), {RoleProgress});
343 void LauncherModel::countChanged(
const QString &appId,
int count)
345 const int idx = findApplication(appId);
347 LauncherItem *item = m_list.at(idx);
348 item->setCount(count);
349 QVector<int> changedRoles = {RoleCount};
350 if (item->countVisible() && !item->alerting() && !item->focused()) {
351 changedRoles << RoleAlerting;
352 item->setAlerting(
true);
354 m_asAdapter->syncItems(m_list);
355 Q_EMIT dataChanged(index(idx), index(idx), changedRoles);
359 void LauncherModel::countVisibleChanged(
const QString &appId,
bool countVisible)
361 int idx = findApplication(appId);
363 LauncherItem *item = m_list.at(idx);
364 item->setCountVisible(countVisible);
365 QVector<int> changedRoles = {RoleCountVisible};
366 if (countVisible && !item->alerting() && !item->focused()) {
367 changedRoles << RoleAlerting;
368 item->setAlerting(
true);
370 Q_EMIT dataChanged(index(idx), index(idx), changedRoles);
373 if (!countVisible && !item->pinned() && !item->recent()) {
374 beginRemoveRows(QModelIndex(), idx, idx);
375 m_list.takeAt(idx)->deleteLater();
380 UalWrapper::AppInfo appInfo = UalWrapper::getApplicationInfo(appId);
381 if (countVisible && appInfo.valid) {
382 LauncherItem *item =
new LauncherItem(appId,
386 item->setCountVisible(
true);
387 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
392 m_asAdapter->syncItems(m_list);
395 void LauncherModel::refresh()
398 QList<LauncherItem*> toBeRemoved;
399 Q_FOREACH (LauncherItem* item, m_list) {
400 UalWrapper::AppInfo appInfo = UalWrapper::getApplicationInfo(item->appId());
401 if (!appInfo.valid) {
404 }
else if (!m_settings->storedApplications().contains(item->appId())) {
408 int idx = m_list.indexOf(item);
409 item->setName(appInfo.name);
410 item->setPinned(item->pinned());
411 item->setRunning(item->running());
412 Q_EMIT dataChanged(index(idx), index(idx), {RoleName, RoleRunning});
414 const QString oldIcon = item->icon();
415 if (oldIcon == appInfo.icon) {
416 item->setIcon(QString());
417 Q_EMIT dataChanged(index(idx), index(idx), {RoleIcon});
421 item->setIcon(appInfo.icon);
422 Q_EMIT dataChanged(index(idx), index(idx), {RoleIcon});
426 Q_FOREACH (LauncherItem* item, toBeRemoved) {
427 unpin(item->appId());
430 bool changed = toBeRemoved.count() > 0;
439 for (
int settingsIndex = 0; settingsIndex < m_settings->storedApplications().count(); ++settingsIndex) {
440 const QString entry = m_settings->storedApplications().at(settingsIndex);
442 for (
int i = 0; i < m_list.count(); ++i) {
443 if (m_list.at(i)->appId() == entry) {
449 if (itemIndex == -1) {
452 UalWrapper::AppInfo appInfo = UalWrapper::getApplicationInfo(entry);
453 if (!appInfo.valid) {
457 LauncherItem *item =
new LauncherItem(entry,
461 item->setPinned(
true);
462 beginInsertRows(QModelIndex(), addedIndex, addedIndex);
463 m_list.insert(addedIndex, item);
466 }
else if (itemIndex != addedIndex) {
469 beginMoveRows(QModelIndex(), itemIndex, itemIndex, QModelIndex(), addedIndex);
470 m_list.move(itemIndex, addedIndex);
484 m_asAdapter->syncItems(m_list);
487 void LauncherModel::alert(
const QString &appId)
489 int idx = findApplication(appId);
491 LauncherItem *item = m_list.at(idx);
492 if (!item->focused() && !item->alerting()) {
493 item->setAlerting(
true);
494 Q_EMIT dataChanged(index(idx), index(idx), {RoleAlerting});
499 void LauncherModel::applicationAdded(
const QModelIndex &parent,
int row)
503 ApplicationInfoInterface *app = m_appManager->get(row);
505 qWarning() <<
"LauncherModel received an applicationAdded signal, but there's no such application!";
509 if (app->appId() == QLatin1String(
"unity8-dash")) {
514 const int itemIndex = findApplication(app->appId());
515 if (itemIndex != -1) {
516 LauncherItem *item = m_list.at(itemIndex);
517 if (!item->recent()) {
518 item->setRecent(
true);
519 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), {RoleRecent});
521 if (item->surfaceCount() != app->surfaceCount()) {
522 item->setSurfaceCount(app->surfaceCount());
523 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), {RoleSurfaceCount});
526 item->setRunning(
true);
528 LauncherItem *item =
new LauncherItem(app->appId(), app->name(), app->icon().toString(),
this);
529 item->setRecent(
true);
530 item->setRunning(
true);
531 item->setFocused(app->focused());
532 item->setSurfaceCount(app->surfaceCount());
533 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
537 connect(app, &ApplicationInfoInterface::surfaceCountChanged,
this, &LauncherModel::applicationSurfaceCountChanged);
538 m_asAdapter->syncItems(m_list);
539 Q_EMIT dataChanged(index(itemIndex), index(itemIndex), {RoleRunning});
542 void LauncherModel::applicationSurfaceCountChanged(
int count)
544 ApplicationInfoInterface *app =
static_cast<ApplicationInfoInterface*
>(sender());
545 int idx = findApplication(app->appId());
547 qWarning() <<
"Received a surface count changed event from an app that's not in the Launcher model";
550 LauncherItem *item = m_list.at(idx);
551 if (item->surfaceCount() != count) {
552 item->setSurfaceCount(count);
553 Q_EMIT dataChanged(index(idx), index(idx), {RoleSurfaceCount});
557 void LauncherModel::applicationRemoved(
const QModelIndex &parent,
int row)
561 ApplicationInfoInterface *app = m_appManager->get(row);
563 for (
int i = 0; i < m_list.count(); ++i) {
564 if (m_list.at(i)->appId() == app->appId()) {
571 qWarning() << Q_FUNC_INFO <<
"appIndex not found";
575 disconnect(app, &ApplicationInfoInterface::surfaceCountChanged,
this, &LauncherModel::applicationSurfaceCountChanged);
577 LauncherItem * item = m_list.at(appIndex);
579 if (!item->pinned()) {
580 beginRemoveRows(QModelIndex(), appIndex, appIndex);
581 m_list.takeAt(appIndex)->deleteLater();
583 m_asAdapter->syncItems(m_list);
585 QVector<int> changedRoles = {RoleRunning};
586 item->setRunning(
false);
587 if (item->focused()) {
588 changedRoles << RoleFocused;
589 item->setFocused(
false);
591 Q_EMIT dataChanged(index(appIndex), index(appIndex), changedRoles);
595 void LauncherModel::focusedAppIdChanged()
597 const QString appId = m_appManager->focusedApplicationId();
598 for (
int i = 0; i < m_list.count(); ++i) {
599 LauncherItem *item = m_list.at(i);
600 if (!item->focused() && item->appId() == appId) {
601 QVector<int> changedRoles;
602 changedRoles << RoleFocused;
603 item->setFocused(
true);
604 if (item->alerting()) {
605 changedRoles << RoleAlerting;
606 item->setAlerting(
false);
608 Q_EMIT dataChanged(index(i), index(i), changedRoles);
609 }
else if (item->focused() && item->appId() != appId) {
610 item->setFocused(
false);
611 Q_EMIT dataChanged(index(i), index(i), {RoleFocused});