@@ -1,5 +1,5 @@
11/*
2− * Copyright 2013-2014 Canonical Ltd.
2+ * Copyright 2013-2016 Canonical Ltd.
33 *
44 * This program is free software; you can redistribute it and/or modify
55 * it under the terms of the GNU Lesser General Public License as published by
@@ -12,24 +12,25 @@
1212 *
1313 * You should have received a copy of the GNU Lesser General Public License
1414 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15− *
16− * Authors:
17− * Michael Zanetti <michael.zanetti@canonical.com>
1815 */
1916
2017#include "launchermodel.h"
2118#include "launcheritem.h"
2219#include "gsettings.h"
23−#include "desktopfilehandler.h"
2420#include "dbusinterface.h"
2521#include "asadapter.h"
2622
23+#include <ubuntu-app-launch/appid.h>
24+#include <ubuntu-app-launch/application.h>
25+#include <ubuntu-app-launch/registry.h>
2726#include <unity/shell/application/ApplicationInfoInterface.h>
2827#include <unity/shell/application/MirSurfaceListInterface.h>
2928
3029#include <QDesktopServices>
3130#include <QDebug>
3231
32+namespace ual = ubuntu::app_launch;
33+
3334using namespace unity::shell::application;
3435
3536LauncherModel::LauncherModel(QObject *parent):
@@ -37,7 +38,8 @@ LauncherModel::LauncherModel(QObject *parent):
3738 m_settings(new GSettings(this)),
3839 m_dbusIface(new DBusInterface(this)),
3940 m_asAdapter(new ASAdapter()),
40− m_appManager(nullptr)
41+ m_appManager(nullptr),
42+ m_ualRegistry(std::make_shared<ual::Registry>())
4143{
4244 connect(m_dbusIface, &DBusInterface::countChanged, this, &LauncherModel::countChanged);
4345 connect(m_dbusIface, &DBusInterface::countVisibleChanged, this, &LauncherModel::countVisibleChanged);
@@ -157,16 +159,16 @@ void LauncherModel::pin(const QString &appId, int index)
157159 index = m_list.count();
158160 }
159161
160− DesktopFileHandler desktopFile(appId);
161− if (!desktopFile.isValid()) {
162− qWarning() << "Can't pin this application, there is no .desktop file available.";
162+ auto appInfo = getApplicationInfo(appId);
163+ if (!appInfo.valid) {
164+ qWarning() << "Can't pin application, appId not found:" << appId;
163165 return;
164166 }
165167
166168 beginInsertRows(QModelIndex(), index, index);
167169 LauncherItem *item = new LauncherItem(appId,
168− desktopFile.displayName(),
169− desktopFile.icon(),
170+ appInfo.name,
171+ appInfo.icon,
170172 this);
171173 item->setPinned(true);
172174 m_list.insert(index, item);
@@ -333,6 +335,32 @@ int LauncherModel::findApplication(const QString &appId)
333335 return -1;
334336}
335337
338+LauncherModel::AppInfo LauncherModel::getApplicationInfo(const QString &appId)
339+{
340+ AppInfo info;
341+
342+ ual::AppID ualAppId = ual::AppID::find(m_ualRegistry, appId.toStdString());
343+ if (ualAppId.empty()) {
344+ return info;
345+ }
346+
347+ std::shared_ptr<ual::Application> ualApp;
348+ try
349+ {
350+ ualApp = ual::Application::create(ualAppId, m_ualRegistry);
351+ }
352+ catch (std::runtime_error &e)
353+ {
354+ qWarning() << "Couldn't find application info for" << appId << "-" << e.what();
355+ return info;
356+ }
357+
358+ info.valid = true;
359+ info.name = QString::fromStdString(ualApp->info()->name());
360+ info.icon = QString::fromStdString(ualApp->info()->iconPath());
361+ return info;
362+}
363+
336364void LauncherModel::progressChanged(const QString &appId, int progress)
337365{
338366 const int idx = findApplication(appId);
@@ -380,11 +408,11 @@ void LauncherModel::countVisibleChanged(const QString &appId, bool countVisible)
380408 }
381409 } else {
382410 // Need to create a new LauncherItem and show the highlight
383− DesktopFileHandler desktopFile(appId);
384− if (countVisible && desktopFile.isValid()) {
411+ auto appInfo = getApplicationInfo(appId);
412+ if (countVisible && appInfo.valid) {
385413 LauncherItem *item = new LauncherItem(appId,
386− desktopFile.displayName(),
387− desktopFile.icon(),
414+ appInfo.name,
415+ appInfo.icon,
388416 this);
389417 item->setCountVisible(true);
390418 beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
@@ -400,28 +428,28 @@ void LauncherModel::refresh()
400428 // First walk through all the existing items and see if we need to remove something
401429 QList<LauncherItem*> toBeRemoved;
402430 Q_FOREACH (LauncherItem* item, m_list) {
403− DesktopFileHandler desktopFile(item->appId());
404− if (!desktopFile.isValid()) {
405− // Desktop file not available for this app => drop it!
431+ auto appInfo = getApplicationInfo(item->appId());
432+ if (!appInfo.valid) {
433+ // Application no longer available => drop it!
406434 toBeRemoved << item;
407435 } else if (!m_settings->storedApplications().contains(item->appId())) {
408436 // Item not in settings any more => drop it!
409437 toBeRemoved << item;
410438 } else {
411439 int idx = m_list.indexOf(item);
412− item->setName(desktopFile.displayName());
440+ item->setName(appInfo.name);
413441 item->setPinned(item->pinned()); // update pinned text if needed
414442 item->setRunning(item->running());
415443 Q_EMIT dataChanged(index(idx), index(idx), {RoleName, RoleRunning});
416444
417445 const QString oldIcon = item->icon();
418− if (oldIcon == desktopFile.icon()) { // same icon file, perhaps different contents, simulate changing the icon name to force reload
446+ if (oldIcon == appInfo.icon) { // same icon file, perhaps different contents, simulate changing the icon name to force reload
419447 item->setIcon(QString());
420448 Q_EMIT dataChanged(index(idx), index(idx), {RoleIcon});
421449 }
422450
423451 // now set the icon for real
424− item->setIcon(desktopFile.icon());
452+ item->setIcon(appInfo.icon);
425453 Q_EMIT dataChanged(index(idx), index(idx), {RoleIcon});
426454 }
427455 }
@@ -452,15 +480,14 @@ void LauncherModel::refresh()
452480 if (itemIndex == -1) {
453481 // Need to add it. Just add it into the addedIndex to keep same ordering as the list
454482 // in the settings.
455− DesktopFileHandler desktopFile(entry);
456− if (!desktopFile.isValid()) {
457− qWarning() << "Couldn't find a .desktop file for" << entry << ". Skipping...";
483+ auto appInfo = getApplicationInfo(entry);
484+ if (!appInfo.valid) {
458485 continue;
459486 }
460487
461488 LauncherItem *item = new LauncherItem(entry,
462− desktopFile.displayName(),
463− desktopFile.icon(),
489+ appInfo.name,
490+ appInfo.icon,
464491 this);
465492 item->setPinned(true);
466493 beginInsertRows(QModelIndex(), addedIndex, addedIndex);