2009-10-15 19:28:29 -04:00
|
|
|
/* -*- mode: js2; js2-basic-offset: 4; indent-tabs-mode: nil -*- */
|
|
|
|
|
|
|
|
const Shell = imports.gi.Shell;
|
|
|
|
const Lang = imports.lang;
|
|
|
|
const Signals = imports.signals;
|
2010-02-08 17:50:50 -05:00
|
|
|
const Gettext = imports.gettext.domain('gnome-shell');
|
|
|
|
const _ = Gettext.gettext;
|
2009-10-15 19:28:29 -04:00
|
|
|
|
2010-02-08 17:50:50 -05:00
|
|
|
const Main = imports.ui.main;
|
2009-10-15 19:28:29 -04:00
|
|
|
|
|
|
|
function AppFavorites() {
|
|
|
|
this._init();
|
|
|
|
}
|
|
|
|
|
|
|
|
AppFavorites.prototype = {
|
2010-05-05 17:05:42 -04:00
|
|
|
FAVORITE_APPS_KEY: 'favorite-apps',
|
2009-10-15 19:28:29 -04:00
|
|
|
|
|
|
|
_init: function() {
|
|
|
|
this._favorites = {};
|
2010-05-05 17:05:42 -04:00
|
|
|
global.settings.connect('changed::' + this.FAVORITE_APPS_KEY, Lang.bind(this, this._onFavsChanged));
|
2009-10-15 19:28:29 -04:00
|
|
|
this._reload();
|
|
|
|
},
|
|
|
|
|
|
|
|
_onFavsChanged: function() {
|
|
|
|
this._reload();
|
|
|
|
this.emit('changed');
|
|
|
|
},
|
|
|
|
|
|
|
|
_reload: function() {
|
2010-05-05 17:05:42 -04:00
|
|
|
let ids = global.settings.get_strv(this.FAVORITE_APPS_KEY);
|
2009-10-15 19:28:29 -04:00
|
|
|
let appSys = Shell.AppSystem.get_default();
|
|
|
|
let apps = ids.map(function (id) {
|
|
|
|
return appSys.get_app(id);
|
|
|
|
}).filter(function (app) {
|
|
|
|
return app != null;
|
|
|
|
});
|
|
|
|
this._favorites = {};
|
|
|
|
for (let i = 0; i < apps.length; i++) {
|
|
|
|
let app = apps[i];
|
|
|
|
this._favorites[app.get_id()] = app;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_getIds: function() {
|
|
|
|
let ret = [];
|
|
|
|
for (let id in this._favorites)
|
|
|
|
ret.push(id);
|
|
|
|
return ret;
|
|
|
|
},
|
|
|
|
|
|
|
|
getFavoriteMap: function() {
|
|
|
|
return this._favorites;
|
|
|
|
},
|
|
|
|
|
|
|
|
getFavorites: function() {
|
|
|
|
let ret = [];
|
|
|
|
for (let id in this._favorites)
|
|
|
|
ret.push(this._favorites[id]);
|
|
|
|
return ret;
|
|
|
|
},
|
|
|
|
|
|
|
|
isFavorite: function(appId) {
|
|
|
|
return appId in this._favorites;
|
|
|
|
},
|
|
|
|
|
2010-02-08 17:50:50 -05:00
|
|
|
_addFavorite: function(appId) {
|
2009-10-15 19:28:29 -04:00
|
|
|
if (appId in this._favorites)
|
2010-02-08 17:50:50 -05:00
|
|
|
return false;
|
|
|
|
|
2009-10-15 19:28:29 -04:00
|
|
|
let app = Shell.AppSystem.get_default().get_app(appId);
|
2010-02-08 17:50:50 -05:00
|
|
|
|
2009-10-15 19:28:29 -04:00
|
|
|
if (!app)
|
2010-02-08 17:50:50 -05:00
|
|
|
return false;
|
|
|
|
|
2009-10-15 19:28:29 -04:00
|
|
|
let ids = this._getIds();
|
|
|
|
ids.push(appId);
|
2010-05-05 17:05:42 -04:00
|
|
|
global.settings.set_strv(this.FAVORITE_APPS_KEY, ids);
|
2009-10-15 19:28:29 -04:00
|
|
|
this._favorites[appId] = app;
|
2010-02-08 17:50:50 -05:00
|
|
|
return true;
|
2009-10-15 19:28:29 -04:00
|
|
|
},
|
|
|
|
|
2010-02-08 17:50:50 -05:00
|
|
|
addFavorite: function(appId) {
|
|
|
|
if (!this._addFavorite(appId))
|
2009-10-15 19:28:29 -04:00
|
|
|
return;
|
2010-02-08 17:50:50 -05:00
|
|
|
|
|
|
|
let app = Shell.AppSystem.get_default().get_app(appId);
|
|
|
|
|
2010-02-15 08:33:35 -05:00
|
|
|
Main.overview.infoBar.setMessage(_("%s has been added to your favorites.").format(app.get_name()), Lang.bind(this, function () {
|
2010-02-08 17:50:50 -05:00
|
|
|
this._removeFavorite(appId);
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
|
|
|
|
_removeFavorite: function(appId) {
|
|
|
|
if (!appId in this._favorites)
|
|
|
|
return false;
|
|
|
|
|
2009-10-15 19:28:29 -04:00
|
|
|
let ids = this._getIds().filter(function (id) { return id != appId; });
|
2010-05-05 17:05:42 -04:00
|
|
|
global.settings.set_strv(this.FAVORITE_APPS_KEY, ids);
|
2010-02-08 17:50:50 -05:00
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
removeFavorite: function(appId) {
|
|
|
|
if (!this._removeFavorite(appId))
|
|
|
|
return;
|
|
|
|
|
2010-02-15 08:33:35 -05:00
|
|
|
Main.overview.infoBar.setMessage(_("%s has been removed from your favorites.").format(this._favorites[appId].get_name()),
|
2010-02-08 17:50:50 -05:00
|
|
|
Lang.bind(this, function () {
|
|
|
|
this._addFavorite(appId);
|
|
|
|
}));
|
2009-10-15 19:28:29 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
Signals.addSignalMethods(AppFavorites.prototype);
|
|
|
|
|
|
|
|
var appFavoritesInstance = null;
|
|
|
|
function getAppFavorites() {
|
|
|
|
if (appFavoritesInstance == null)
|
|
|
|
appFavoritesInstance = new AppFavorites();
|
|
|
|
return appFavoritesInstance;
|
|
|
|
}
|