2009-06-16 12:20:12 -04:00
|
|
|
/* -*- mode: js2; js2-basic-offset: 4; indent-tabs-mode: nil -*- */
|
|
|
|
|
|
|
|
const Clutter = imports.gi.Clutter;
|
|
|
|
const Gio = imports.gi.Gio;
|
|
|
|
const Gtk = imports.gi.Gtk;
|
|
|
|
const Shell = imports.gi.Shell;
|
|
|
|
|
|
|
|
const Main = imports.ui.main;
|
|
|
|
|
|
|
|
function AppInfo(appId) {
|
|
|
|
this._init(appId);
|
|
|
|
}
|
|
|
|
|
|
|
|
AppInfo.prototype = {
|
|
|
|
_init : function(appId) {
|
|
|
|
this.appId = appId;
|
|
|
|
this._gAppInfo = Gio.DesktopAppInfo.new(appId);
|
2009-06-30 16:35:39 -04:00
|
|
|
if (!this._gAppInfo) {
|
2009-06-16 12:20:12 -04:00
|
|
|
throw new Error('Unknown appId ' + appId);
|
2009-06-30 16:35:39 -04:00
|
|
|
}
|
2009-06-16 12:20:12 -04:00
|
|
|
|
2009-06-17 12:37:54 -04:00
|
|
|
this.id = this._gAppInfo.get_id();
|
2009-06-16 12:20:12 -04:00
|
|
|
this.name = this._gAppInfo.get_name();
|
|
|
|
this.description = this._gAppInfo.get_description();
|
2009-06-17 12:37:54 -04:00
|
|
|
this.executable = this._gAppInfo.get_executable();
|
2009-06-16 12:20:12 -04:00
|
|
|
|
|
|
|
this._gicon = this._gAppInfo.get_icon();
|
|
|
|
},
|
|
|
|
|
|
|
|
getIcon : function(size) {
|
|
|
|
if (this._gicon)
|
|
|
|
return Shell.TextureCache.get_default().load_gicon(this._gicon, size);
|
|
|
|
else
|
|
|
|
return new Clutter.Texture({ width: size, height: size });
|
|
|
|
},
|
|
|
|
|
|
|
|
getIconPath : function(size) {
|
|
|
|
if (this._gicon) {
|
|
|
|
let iconTheme = Gtk.IconTheme.get_default();
|
|
|
|
let previewIconInfo = iconTheme.lookup_by_gicon(this._gicon, size, 0);
|
|
|
|
if (previewIconInfo)
|
|
|
|
return previewIconInfo.get_filename();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
|
|
|
launch : function() {
|
|
|
|
this._gAppInfo.launch([], Main.createAppLaunchContext());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var _infos = {};
|
|
|
|
|
|
|
|
// getAppInfo:
|
|
|
|
// @appId: an appId
|
|
|
|
//
|
|
|
|
// Gets an #AppInfo for @appId. This is preferable to calling
|
|
|
|
// new AppInfo() directly, because it caches #AppInfos.
|
|
|
|
//
|
|
|
|
// Return value: the new or cached #AppInfo, or %null if @appId
|
|
|
|
// doesn't point to a valid .desktop file
|
|
|
|
function getAppInfo(appId) {
|
|
|
|
let info = _infos[appId];
|
|
|
|
if (info === undefined) {
|
|
|
|
try {
|
|
|
|
info = _infos[appId] = new AppInfo(appId);
|
|
|
|
} catch (e) {
|
|
|
|
info = _infos[appId] = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2009-06-30 16:35:39 -04:00
|
|
|
// getTopApps:
|
2009-06-16 12:20:12 -04:00
|
|
|
// @count: maximum number of apps to retrieve
|
|
|
|
//
|
|
|
|
// Gets a list of #AppInfos for the @count most-frequently-used
|
2009-06-30 16:35:39 -04:00
|
|
|
// applications, with explicitly-chosen favorites first.
|
2009-06-16 12:20:12 -04:00
|
|
|
//
|
|
|
|
// Return value: the list of #AppInfo
|
2009-06-30 16:35:39 -04:00
|
|
|
function getTopApps(count) {
|
2009-06-18 12:27:19 -04:00
|
|
|
let appMonitor = Shell.AppMonitor.get_default();
|
2009-06-16 12:20:12 -04:00
|
|
|
|
2009-06-30 16:35:39 -04:00
|
|
|
let matches = [], alreadyAdded = {};
|
|
|
|
|
|
|
|
let favs = getFavorites();
|
|
|
|
for (let i = 0; i < favs.length && favs.length <= count; i++) {
|
|
|
|
let appId = favs[i].appId;
|
|
|
|
|
|
|
|
if (alreadyAdded[appId])
|
|
|
|
continue;
|
|
|
|
alreadyAdded[appId] = true;
|
|
|
|
|
|
|
|
matches.push(favs[i]);
|
|
|
|
}
|
|
|
|
|
2009-06-16 12:20:12 -04:00
|
|
|
// Ask for more apps than we need, since the list of recently used
|
|
|
|
// apps might contain an app we don't have a desktop file for
|
|
|
|
let apps = appMonitor.get_most_used_apps (0, Math.round(count * 1.5));
|
|
|
|
for (let i = 0; i < apps.length && matches.length <= count; i++) {
|
|
|
|
let appId = apps[i] + ".desktop";
|
2009-06-30 16:35:39 -04:00
|
|
|
if (alreadyAdded[appId])
|
|
|
|
continue;
|
|
|
|
alreadyAdded[appId] = true;
|
2009-06-16 12:20:12 -04:00
|
|
|
let appInfo = getAppInfo(appId);
|
|
|
|
if (appInfo) {
|
|
|
|
matches.push(appInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-30 16:35:39 -04:00
|
|
|
return matches;
|
|
|
|
}
|
2009-06-16 12:20:12 -04:00
|
|
|
|
2009-06-30 16:35:39 -04:00
|
|
|
function _idListToInfos(ids) {
|
|
|
|
let infos = [];
|
|
|
|
for (let i = 0; i < ids.length; i++) {
|
|
|
|
let display = getAppInfo(ids[i]);
|
|
|
|
if (display == null)
|
|
|
|
continue;
|
|
|
|
infos.push(display);
|
2009-06-16 12:20:12 -04:00
|
|
|
}
|
2009-06-30 16:35:39 -04:00
|
|
|
return infos;
|
|
|
|
}
|
2009-06-16 12:20:12 -04:00
|
|
|
|
2009-06-30 16:35:39 -04:00
|
|
|
function getFavorites() {
|
|
|
|
let system = Shell.AppSystem.get_default();
|
|
|
|
|
|
|
|
return _idListToInfos(system.get_favorites());
|
|
|
|
}
|
|
|
|
|
|
|
|
function getRunning() {
|
|
|
|
let monitor = Shell.AppMonitor.get_default();
|
|
|
|
return _idListToInfos(monitor.get_running_app_ids());
|
2009-06-16 12:20:12 -04:00
|
|
|
}
|