Do

Merge lp:~alexlauni/do/xdg-appitemsource into lp:do

Proposed by Alex Launi
Status: Merged
Approved by: Alex Launi
Approved revision: 1091
Merged at revision: not available
Proposed branch: lp:~alexlauni/do/xdg-appitemsource
Merge into: lp:do
Diff against target: None lines
To merge this branch: bzr merge lp:~alexlauni/do/xdg-appitemsource
Reviewer Review Type Date Requested Status
Jason Smith (community) Approve
Review via email: mp+4424@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Jason Smith (jassmith) wrote :

looks good

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'Do.Platform.Linux/src/Do.Universe/ApplicationItemSource.cs'
2--- Do.Platform.Linux/src/Do.Universe/ApplicationItemSource.cs 2009-01-15 19:25:06 +0000
3+++ Do.Platform.Linux/src/Do.Universe/ApplicationItemSource.cs 2009-03-12 21:04:48 +0000
4@@ -33,26 +33,13 @@
5 public class ApplicationItemSource : ItemSource {
6
7 const bool show_hidden = false;
8-
9- private IEnumerable<Item> app_items;
10-
11- /// <summary>
12- /// Locations to search for .desktop files.
13- /// </summary>
14- static IEnumerable<String> DesktopFilesDirectories {
15- get {
16- return new [] {
17- Environment.GetFolderPath (Environment.SpecialFolder.Desktop),
18- "~/.local/share/applications",
19- "~/.local/share/applications/wine",
20- "~/.local/share/applications/wine/Programs",
21- "/usr/share/applications",
22- "/usr/share/applications/kde",
23- "/usr/share/applications/kde4",
24- "/usr/share/gdm/applications",
25- "/usr/local/share/applications",
26- };
27- }
28+ static IEnumerable<string> desktop_file_directories;
29+
30+ IEnumerable<Item> app_items;
31+
32+ static ApplicationItemSource ()
33+ {
34+ desktop_file_directories = ReadXdgDataDirs ();
35 }
36
37 public ApplicationItemSource ()
38@@ -87,21 +74,36 @@
39 /// directory
40 /// where .desktop files can be found.
41 /// </param>
42- private static IEnumerable<ApplicationItem> LoadDesktopFiles (string dir)
43+ static IEnumerable<ApplicationItem> LoadDesktopFiles (string dir)
44 {
45+ Queue<string> queue;
46+ List<ApplicationItem> apps;
47+
48 if (!Directory.Exists (dir))
49 return Enumerable.Empty<ApplicationItem> ();
50-
51- return Directory.GetFiles (dir, "*.desktop")
52- .Select (file => ApplicationItem.MaybeCreateFromDesktopItem (file))
53- .Where (app => app != null &&
54- app.IsAppropriateForCurrentDesktop &&
55- (show_hidden || !app.NoDisplay));
56+
57+ queue = new Queue<string> ();
58+ queue.Enqueue (dir);
59+
60+ apps = new List<ApplicationItem> ();
61+
62+ while (queue.Count > 0) {
63+ dir = queue.Dequeue ();
64+ foreach (string d in Directory.GetDirectories (dir))
65+ queue.Enqueue (d);
66+
67+ apps.AddRange (Directory.GetFiles (dir, "*.desktop")
68+ .Select (file => ApplicationItem.MaybeCreateFromDesktopItem (file))
69+ .Where (app => app != null && app.IsAppropriateForCurrentDesktop && (show_hidden || !app.NoDisplay))
70+ );
71+ }
72+
73+ return apps;
74 }
75-
76+
77 public override void UpdateItems ()
78 {
79- app_items = DesktopFilesDirectories
80+ app_items = desktop_file_directories
81 .Select (dir => dir.Replace ("~", Environment.GetFolderPath (Environment.SpecialFolder.Personal)))
82 .SelectMany (dir => LoadDesktopFiles (dir))
83 .Cast<Item> ()
84@@ -111,6 +113,31 @@
85 public override IEnumerable<Item> Items {
86 get { return app_items; }
87 }
88-
89+
90+ static IEnumerable<string> ReadXdgDataDirs ()
91+ {
92+ string home, envPath;
93+
94+ const string appDirSuffix = "applications";
95+ string [] xdgVars = new [] {"XDG_DATA_HOME", "XDG_DATA_DIRS"};
96+
97+ home = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
98+
99+ foreach (string xdgVar in xdgVars) {
100+ envPath = Environment.GetEnvironmentVariable (xdgVar);
101+
102+ if (string.IsNullOrEmpty (envPath)) {
103+ if (xdgVar == "XDG_DATA_HOME") {
104+ yield return (new [] {home, ".local/share", appDirSuffix}.Aggregate (Path.Combine));
105+ } else if (xdgVar == "XDG_DATA_DIRS") {
106+ yield return Path.Combine ("/usr/local/share/", appDirSuffix);
107+ yield return Path.Combine ("/usr/share/applications", appDirSuffix);
108+ }
109+ } else {
110+ foreach (string dir in envPath.Split (':'))
111+ yield return Path.Combine (dir, appDirSuffix);
112+ }
113+ }
114+ }
115 }
116 }