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
=== modified file 'Do.Platform.Linux/src/Do.Universe/ApplicationItemSource.cs'
--- Do.Platform.Linux/src/Do.Universe/ApplicationItemSource.cs 2009-01-15 19:25:06 +0000
+++ Do.Platform.Linux/src/Do.Universe/ApplicationItemSource.cs 2009-03-12 21:04:48 +0000
@@ -33,26 +33,13 @@
33 public class ApplicationItemSource : ItemSource {33 public class ApplicationItemSource : ItemSource {
3434
35 const bool show_hidden = false;35 const bool show_hidden = false;
3636 static IEnumerable<string> desktop_file_directories;
37 private IEnumerable<Item> app_items;37
3838 IEnumerable<Item> app_items;
39 /// <summary>39
40 /// Locations to search for .desktop files.40 static ApplicationItemSource ()
41 /// </summary>41 {
42 static IEnumerable<String> DesktopFilesDirectories {42 desktop_file_directories = ReadXdgDataDirs ();
43 get {
44 return new [] {
45 Environment.GetFolderPath (Environment.SpecialFolder.Desktop),
46 "~/.local/share/applications",
47 "~/.local/share/applications/wine",
48 "~/.local/share/applications/wine/Programs",
49 "/usr/share/applications",
50 "/usr/share/applications/kde",
51 "/usr/share/applications/kde4",
52 "/usr/share/gdm/applications",
53 "/usr/local/share/applications",
54 };
55 }
56 }43 }
57 44
58 public ApplicationItemSource ()45 public ApplicationItemSource ()
@@ -87,21 +74,36 @@
87 /// directory74 /// directory
88 /// where .desktop files can be found.75 /// where .desktop files can be found.
89 /// </param>76 /// </param>
90 private static IEnumerable<ApplicationItem> LoadDesktopFiles (string dir)77 static IEnumerable<ApplicationItem> LoadDesktopFiles (string dir)
91 {78 {
79 Queue<string> queue;
80 List<ApplicationItem> apps;
81
92 if (!Directory.Exists (dir))82 if (!Directory.Exists (dir))
93 return Enumerable.Empty<ApplicationItem> ();83 return Enumerable.Empty<ApplicationItem> ();
9484
95 return Directory.GetFiles (dir, "*.desktop")85 queue = new Queue<string> ();
96 .Select (file => ApplicationItem.MaybeCreateFromDesktopItem (file))86 queue.Enqueue (dir);
97 .Where (app => app != null &&87
98 app.IsAppropriateForCurrentDesktop &&88 apps = new List<ApplicationItem> ();
99 (show_hidden || !app.NoDisplay));89
90 while (queue.Count > 0) {
91 dir = queue.Dequeue ();
92 foreach (string d in Directory.GetDirectories (dir))
93 queue.Enqueue (d);
94
95 apps.AddRange (Directory.GetFiles (dir, "*.desktop")
96 .Select (file => ApplicationItem.MaybeCreateFromDesktopItem (file))
97 .Where (app => app != null && app.IsAppropriateForCurrentDesktop && (show_hidden || !app.NoDisplay))
98 );
99 }
100
101 return apps;
100 }102 }
101103
102 public override void UpdateItems ()104 public override void UpdateItems ()
103 {105 {
104 app_items = DesktopFilesDirectories106 app_items = desktop_file_directories
105 .Select (dir => dir.Replace ("~", Environment.GetFolderPath (Environment.SpecialFolder.Personal)))107 .Select (dir => dir.Replace ("~", Environment.GetFolderPath (Environment.SpecialFolder.Personal)))
106 .SelectMany (dir => LoadDesktopFiles (dir))108 .SelectMany (dir => LoadDesktopFiles (dir))
107 .Cast<Item> ()109 .Cast<Item> ()
@@ -111,6 +113,31 @@
111 public override IEnumerable<Item> Items {113 public override IEnumerable<Item> Items {
112 get { return app_items; }114 get { return app_items; }
113 }115 }
114116
117 static IEnumerable<string> ReadXdgDataDirs ()
118 {
119 string home, envPath;
120
121 const string appDirSuffix = "applications";
122 string [] xdgVars = new [] {"XDG_DATA_HOME", "XDG_DATA_DIRS"};
123
124 home = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
125
126 foreach (string xdgVar in xdgVars) {
127 envPath = Environment.GetEnvironmentVariable (xdgVar);
128
129 if (string.IsNullOrEmpty (envPath)) {
130 if (xdgVar == "XDG_DATA_HOME") {
131 yield return (new [] {home, ".local/share", appDirSuffix}.Aggregate (Path.Combine));
132 } else if (xdgVar == "XDG_DATA_DIRS") {
133 yield return Path.Combine ("/usr/local/share/", appDirSuffix);
134 yield return Path.Combine ("/usr/share/applications", appDirSuffix);
135 }
136 } else {
137 foreach (string dir in envPath.Split (':'))
138 yield return Path.Combine (dir, appDirSuffix);
139 }
140 }
141 }
115 }142 }
116}143}