~ubuntu-branches/ubuntu/utopic/gnome-do/utopic-proposed

« back to all changes in this revision

Viewing changes to Do.Platform.Linux/src/Do.Universe/ApplicationItem.cs

  • Committer: Package Import Robot
  • Author(s): Christopher James Halse Rogers, Iain Lane, Christopher James Halse Rogers
  • Date: 2014-07-11 16:20:37 UTC
  • mfrom: (0.1.14)
  • Revision ID: package-import@ubuntu.com-20140711162037-08ncoy0ur0046i8e
Tags: 0.95.1-1
[ Iain Lane ]
* Build-depend directly on nunit instead of relying on indirect
* Build-depend on dbus-sharp instead of obsolete ndesk-dbus

[ Christopher James Halse Rogers ]
* Import new upstream 0.95 release (Closes: 713094, 708038)
* Transition to DBus# 2.0
* Simplify gnome-do launcher script (Closes: 650368)
* Version build-dependency on mono-cario; uses methods new in 3.2
* Add new GIO# and GKeyfile# build-depends
* Bump Standards-Version to 3.9.5, updating debian/copyright for
  machine-readable v1.0
* Import new upstream 0.95.1 release
* Drop versioned depends on Mono.Cairo; 0.95.1 builds against Mono < 3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
using System.IO;
22
22
using System.Linq;
23
23
using System.Collections.Generic;
24
 
using System.Runtime.InteropServices;
25
24
using System.Text.RegularExpressions;
26
25
 
27
 
using Gnome;
28
26
using Mono.Unix;
29
27
 
30
28
using Do.Universe;
31
29
using Do.Platform;
 
30
using GLib;
32
31
 
33
32
namespace Do.Universe.Linux {
34
33
 
41
40
                static ApplicationItem ()
42
41
                {
43
42
                        Instances = new Dictionary<string, ApplicationItem> ();
 
43
 
 
44
                        
 
45
                        // Populate desktop environment flag, for ShouldShow
 
46
                        string desktopSession = Environment.GetEnvironmentVariable ("XDG_CURRENT_DESKTOP");
 
47
                        if (desktopSession == null) {
 
48
                                // Legacy fallbacks:
 
49
                                // If KDE_FULL_SESSION is true, assume kde.
 
50
                                // Else, assume GNOME
 
51
                                if (Environment.GetEnvironmentVariable ("KDE_FULL_SESSION") == "true") {
 
52
                                        desktopSession = "KDE";
 
53
                                } else {
 
54
                                        desktopSession = "GNOME";
 
55
                                }
 
56
                        }
 
57
                        DesktopAppInfo.DesktopEnv = desktopSession;
44
58
                }
45
 
                
 
59
 
46
60
                public static ApplicationItem MaybeCreateFromDesktopItem (string path)
47
61
                {
48
62
                        string key = path;
55
69
                                if (Instances.ContainsKey (key)) {
56
70
                                                appItem = Instances [key];
57
71
                                } else {
58
 
                                        DesktopItem item = null;
 
72
                                        DesktopAppInfo item = null;
59
73
                                        try {
60
 
                                                item = DesktopItem.NewFromFile (path, 0);
 
74
                                                item = DesktopAppInfo.NewFromFilename(path);
61
75
                                                appItem = new ApplicationItem (item);
62
76
                                        } catch (Exception e) {
63
77
                                                appItem = null;
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);
67
81
                                        }
68
82
 
69
83
                                        if (appItem != null)
113
127
                                                bestMatch = item;
114
128
                                                continue;
115
129
                                        }
116
 
                                        if (item.IsAppropriateForCurrentDesktop) {
117
 
                                                if (!bestMatch.IsAppropriateForCurrentDesktop || item.Exec.Length < bestMatch.Exec.Length)
 
130
                                        if (item.ShouldShow) {
 
131
                                                if (!bestMatch.ShouldShow || item.Exec.Length < bestMatch.Exec.Length)
118
132
                                                        bestMatch = item;
119
133
                                        }
120
134
                                }
123
137
                        return bestMatch;
124
138
                }
125
139
 
126
 
                protected DesktopItem item;
 
140
                protected GLib.DesktopAppInfo item;
127
141
                string name, description, icon;
128
142
                IEnumerable<string> categories;
129
143
 
134
148
                /// A <see cref="System.String"/> containing the absolute path of
135
149
                /// a desktop (.desktop) file.
136
150
                /// </param>
137
 
                protected ApplicationItem (DesktopItem item)
 
151
                protected ApplicationItem (GLib.DesktopAppInfo item)
138
152
                {
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;
144
 
                                
145
 
                                if (item.AttrExists ("Categories"))
146
 
                                        categories = item.GetString ("Categories").Split (';');
147
 
                                else
148
 
                                        categories = Enumerable.Empty<string> ();
149
 
                        } else {
150
 
                                name = Path.GetFileName (item.Location);
151
 
                                description =
152
 
                                        Catalog.GetString ("This application could not be indexed.");
153
 
                                icon = DefaultApplicationIcon;
154
 
                                categories = Enumerable.Empty<string> ();
155
 
                        }
 
154
 
 
155
                        name = item.Name;
 
156
                        description = item.Description;
 
157
                        icon = item.Icon.ToString() ?? DefaultApplicationIcon;
 
158
 
 
159
                        // TODO: Populate categories once GIO# exposes them
 
160
                        categories = Enumerable.Empty<string> ();
156
161
                }
157
162
                
158
163
                public override string Name {
173
178
 
174
179
                public bool NoDisplay {
175
180
                        get {
176
 
                                return item.AttrExists ("NoDisplay") && item.GetBoolean ("NoDisplay");
 
181
                                return !item.ShouldShow;
177
182
                        }
178
183
                }
179
184
                
180
185
                public string Exec {
181
 
                        get { return item.GetString ("Exec"); }
 
186
                        get { return item.Commandline; }
182
187
                }
183
188
                
184
189
                protected string Location {
185
 
                        get { return item.Location; }
 
190
                        get { return item.Executable; }
186
191
                }
187
192
 
188
193
                public bool Hidden {
189
 
                        get { return item.GetBoolean ("NoDisplay"); }
 
194
                        get { return item.IsHidden; }
190
195
                }
191
196
                
192
197
                public bool IsUserCustomItem {
193
 
                        get { return item.Location.StartsWith ("file:///home"); }
 
198
                        get { return item.Executable.StartsWith ("file:///home"); }
194
199
                }
195
200
 
196
 
                public bool IsAppropriateForCurrentDesktop {
 
201
                public bool ShouldShow {
197
202
                        get {
198
 
                                string onlyShowIn = item.GetString ("OnlyShowIn");
199
 
                                string notShowIn = item.GetString ("NotShowIn");
200
 
                                string desktopSession = Environment.GetEnvironmentVariable ("XDG_CURRENT_DESKTOP");
201
 
 
202
 
                                if (desktopSession == null) {
203
 
                                        // Legacy fallbacks:
204
 
                                        // If KDE_FULL_SESSION is true, assume kde.
205
 
                                        // Else, assume GNOME
206
 
                                        if (Environment.GetEnvironmentVariable ("KDE_FULL_SESSION") == "true") {
207
 
                                                desktopSession = "KDE";
208
 
                                        } else {
209
 
                                                desktopSession = "GNOME";
210
 
                                        }
211
 
                                }
212
 
 
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)) {
219
 
                                                        return true;
220
 
                                                }
221
 
                                        }
222
 
                                        // There's an OnlyShowIn attribute, and the current environment doesn't match.
223
 
                                        return false;
224
 
                                }
225
 
 
226
 
                                if (notShowIn != null) {
227
 
                                        foreach (string environment in notShowIn.Split (';')) {
228
 
                                                if (desktopSession.Equals (environment, StringComparison.InvariantCultureIgnoreCase)) {
229
 
                                                        return false;
230
 
                                                }
231
 
                                        }
232
 
                                }
233
 
 
234
 
                                return true;
 
203
                                return item.ShouldShow;
235
204
                        }
236
205
                }
237
206
                
240
209
                /// </summary>
241
210
                public void Run ()
242
211
                {
243
 
                        item.Launch (null, DesktopItemLaunchFlags.OnlyOne);
 
212
                        item.Launch (null, null);
244
213
                }
245
214
 
246
215
                public void LaunchWithFiles (IEnumerable<IFileItem> files)
247
216
                {
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);
251
220
                }
252
221
        }
253
222
}