41
40
static ApplicationItem ()
43
42
Instances = new Dictionary<string, ApplicationItem> ();
45
// Populate desktop environment flag, for ShouldShow
46
string desktopSession = Environment.GetEnvironmentVariable ("XDG_CURRENT_DESKTOP");
47
if (desktopSession == null) {
49
// If KDE_FULL_SESSION is true, assume kde.
51
if (Environment.GetEnvironmentVariable ("KDE_FULL_SESSION") == "true") {
52
desktopSession = "KDE";
54
desktopSession = "GNOME";
57
DesktopAppInfo.DesktopEnv = desktopSession;
46
60
public static ApplicationItem MaybeCreateFromDesktopItem (string path)
55
69
if (Instances.ContainsKey (key)) {
56
70
appItem = Instances [key];
58
DesktopItem item = null;
72
DesktopAppInfo item = null;
60
item = DesktopItem.NewFromFile (path, 0);
74
item = DesktopAppInfo.NewFromFilename(path);
61
75
appItem = new ApplicationItem (item);
62
76
} catch (Exception e) {
64
78
try { item.Dispose (); } catch { }
65
Log.Error ("Could not load desktop item: {0}", e.Message);
66
Log.Debug (e.StackTrace);
79
Do.Platform.Log.Error ("Could not load desktop item: {0}", e.Message);
80
Do.Platform.Log.Debug (e.StackTrace);
69
83
if (appItem != null)
123
137
return bestMatch;
126
protected DesktopItem item;
140
protected GLib.DesktopAppInfo item;
127
141
string name, description, icon;
128
142
IEnumerable<string> categories;
134
148
/// A <see cref="System.String"/> containing the absolute path of
135
149
/// a desktop (.desktop) file.
137
protected ApplicationItem (DesktopItem item)
151
protected ApplicationItem (GLib.DesktopAppInfo item)
139
153
this.item = item;
140
if (item.Exists ()) {
141
name = item.GetLocalestring ("Name");
142
description = item.GetLocalestring ("Comment");
143
icon = item.GetString ("Icon") ?? DefaultApplicationIcon;
145
if (item.AttrExists ("Categories"))
146
categories = item.GetString ("Categories").Split (';');
148
categories = Enumerable.Empty<string> ();
150
name = Path.GetFileName (item.Location);
152
Catalog.GetString ("This application could not be indexed.");
153
icon = DefaultApplicationIcon;
154
categories = Enumerable.Empty<string> ();
156
description = item.Description;
157
icon = item.Icon.ToString() ?? DefaultApplicationIcon;
159
// TODO: Populate categories once GIO# exposes them
160
categories = Enumerable.Empty<string> ();
158
163
public override string Name {
174
179
public bool NoDisplay {
176
return item.AttrExists ("NoDisplay") && item.GetBoolean ("NoDisplay");
181
return !item.ShouldShow;
180
185
public string Exec {
181
get { return item.GetString ("Exec"); }
186
get { return item.Commandline; }
184
189
protected string Location {
185
get { return item.Location; }
190
get { return item.Executable; }
188
193
public bool Hidden {
189
get { return item.GetBoolean ("NoDisplay"); }
194
get { return item.IsHidden; }
192
197
public bool IsUserCustomItem {
193
get { return item.Location.StartsWith ("file:///home"); }
198
get { return item.Executable.StartsWith ("file:///home"); }
196
public bool IsAppropriateForCurrentDesktop {
201
public bool ShouldShow {
198
string onlyShowIn = item.GetString ("OnlyShowIn");
199
string notShowIn = item.GetString ("NotShowIn");
200
string desktopSession = Environment.GetEnvironmentVariable ("XDG_CURRENT_DESKTOP");
202
if (desktopSession == null) {
204
// If KDE_FULL_SESSION is true, assume kde.
205
// Else, assume GNOME
206
if (Environment.GetEnvironmentVariable ("KDE_FULL_SESSION") == "true") {
207
desktopSession = "KDE";
209
desktopSession = "GNOME";
213
// It doesn't make sense for a DE to appear in both OnlyShowIn and
214
// NotShowIn. We choose to prefer OnlyShowIn in this case as it makes
215
// the following checks easier.
216
if (onlyShowIn != null) {
217
foreach (string environment in onlyShowIn.Split (';')) {
218
if (desktopSession.Equals (environment, StringComparison.InvariantCultureIgnoreCase)) {
222
// There's an OnlyShowIn attribute, and the current environment doesn't match.
226
if (notShowIn != null) {
227
foreach (string environment in notShowIn.Split (';')) {
228
if (desktopSession.Equals (environment, StringComparison.InvariantCultureIgnoreCase)) {
203
return item.ShouldShow;
241
210
public void Run ()
243
item.Launch (null, DesktopItemLaunchFlags.OnlyOne);
212
item.Launch (null, null);
246
215
public void LaunchWithFiles (IEnumerable<IFileItem> files)
248
217
string [] uris = files.Select (file => file.Uri).ToArray ();
249
218
GLib.List glist = new GLib.List (uris as object[], typeof (string), false, true);
250
item.Launch (glist, DesktopItemLaunchFlags.OnlyOne);
219
item.Launch (glist, null);