appFavorites: Use AppStream for renamed apps if possible

The map for renamed .desktop IDs introduced in commit cceac0d8fb
has been growing ever bigger. In the meantime, a similar mapping was
added to AppStream, as well as the ability for apps to provide that
data themselves. It seems silly to duplicate those mappings, and
AppStreams provide-mechanism is clearly more flexible, so start
using it with the goal of dropping our own hard-coded mapping at
one point.

https://bugzilla.gnome.org/show_bug.cgi?id=784588
This commit is contained in:
Florian Müllner 2017-07-05 23:50:05 +02:00
parent d9a1434ae9
commit 901a089b45

View File

@ -1,5 +1,6 @@
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*- // -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
const AppStream = imports.gi.AppStreamGlib;
const Shell = imports.gi.Shell; const Shell = imports.gi.Shell;
const Lang = imports.lang; const Lang = imports.lang;
const Signals = imports.signals; const Signals = imports.signals;
@ -52,6 +53,13 @@ var AppFavorites = new Lang.Class({
FAVORITE_APPS_KEY: 'favorite-apps', FAVORITE_APPS_KEY: 'favorite-apps',
_init() { _init() {
let loadFlags = AppStream.StoreLoadFlags.APP_INFO_SYSTEM |
AppStream.StoreLoadFlags.APP_INFO_USER |
AppStream.StoreLoadFlags.APPDATA |
AppStream.StoreLoadFlags.DESKTOP;
this._appDataStore = new AppStream.Store();
this._appDataStore.load(loadFlags, null);
this._favorites = {}; this._favorites = {};
global.settings.connect('changed::' + this.FAVORITE_APPS_KEY, this._onFavsChanged.bind(this)); global.settings.connect('changed::' + this.FAVORITE_APPS_KEY, this._onFavsChanged.bind(this));
this.reload(); this.reload();
@ -69,12 +77,23 @@ var AppFavorites = new Lang.Class({
// Map old desktop file names to the current ones // Map old desktop file names to the current ones
let updated = false; let updated = false;
ids = ids.map(id => { ids = ids.map(id => {
let newId = RENAMED_DESKTOP_IDS[id]; let appData = this._appDataStore.get_app_by_id_with_fallbacks(id) ||
this._appDataStore.get_app_by_provide(AppStream.ProvideKind.ID, id);
let newId = appData != null ? appData.get_id() : undefined;
if (newId !== undefined &&
newId !== id &&
appSys.lookup_app(newId) != null) {
updated = true;
return newId;
}
newId = RENAMED_DESKTOP_IDS[id];
if (newId !== undefined && if (newId !== undefined &&
appSys.lookup_app(newId) != null) { appSys.lookup_app(newId) != null) {
updated = true; updated = true;
return newId; return newId;
} }
return id; return id;
}); });
// ... and write back the updated desktop file names // ... and write back the updated desktop file names