74
77
/// where .desktop files can be found.
76
static IEnumerable<ApplicationItem> LoadDesktopFiles (string dir)
79
List<ApplicationItem> apps;
81
if (!Directory.Exists (dir))
82
return Enumerable.Empty<ApplicationItem> ();
84
apps = new List<ApplicationItem> ();
85
queue = new Queue<string> ();
88
while (queue.Any ()) {
89
dir = queue.Dequeue ();
91
// Do not index screensavers.
92
if (dir.Contains ("screensavers"))
95
foreach (string subdir in Directory.GetDirectories (dir))
96
queue.Enqueue (subdir);
99
Directory.GetFiles (dir, "*.desktop")
100
.Select (f => ApplicationItem.MaybeCreateFromDesktopItem (f))
102
app != null && app.IsAppropriateForCurrentDesktop &&
103
(show_hidden || !app.NoDisplay))
79
IEnumerable<ApplicationItem> LoadDesktopFiles (string dir)
81
return GetDesktopFiles ()
82
.Where (ShouldUseDesktopFile)
83
.Select (f => ApplicationItem.MaybeCreateFromDesktopItem (f)).Where (a => a != null)
84
.Where (ShouldUseApplicationItem);
87
IEnumerable<string> GetDesktopFiles ()
89
return desktop_file_directories
90
.Where (d => Directory.Exists (d))
91
.SelectMany (d => GetDesktopFiles (d));
94
IEnumerable<string> GetDesktopFiles (string parent)
96
IEnumerable<string> baseFiles = Directory.GetFiles (parent, "*.desktop");
97
IEnumerable<string> recursiveFiles = Directory.GetDirectories (parent).SelectMany (d => GetDesktopFiles (d));
98
return baseFiles.Concat (recursiveFiles);
101
IEnumerable<CategoryItem> LoadCategoryItems (ApplicationItem appItem)
103
return appItem.Categories
104
.Where (c => !CategoryItem.ContainsCategory (c))
105
.Select (c => CategoryItem.GetCategoryItem (c));
108
bool ShouldUseDesktopFile (string path)
110
return !path.Contains ("screensavers");
113
bool ShouldUseApplicationItem (ApplicationItem app)
115
return app.IsAppropriateForCurrentDesktop && (show_hidden || !app.NoDisplay);
109
118
public override void UpdateItems ()
111
app_items = desktop_file_directories
112
.Select (dir => dir.Replace ("~", Environment.GetFolderPath (Environment.SpecialFolder.Personal)))
113
.SelectMany (dir => LoadDesktopFiles (dir))
120
IEnumerable<ApplicationItem> appItems = desktop_file_directories
121
.SelectMany (dir => LoadDesktopFiles (dir));
123
IEnumerable<CategoryItem> categoryItems = appItems
124
.SelectMany (a => LoadCategoryItems (a));
128
.Concat (categoryItems.Cast<Item> ())
119
134
get { return app_items; }
122
static IEnumerable<string> ReadXdgDataDirs ()
124
string home, envPath;
126
const string appDirSuffix = "applications";
127
string [] xdgVars = { "XDG_DATA_HOME", "XDG_DATA_DIRS" };
129
home = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
131
foreach (string xdgVar in xdgVars) {
132
envPath = Environment.GetEnvironmentVariable (xdgVar);
134
if (string.IsNullOrEmpty (envPath)) {
135
if (xdgVar == "XDG_DATA_HOME") {
137
new [] { home, ".local/share", appDirSuffix }.Aggregate (Path.Combine);
138
} else if (xdgVar == "XDG_DATA_DIRS") {
139
yield return Path.Combine ("/usr/local/share/", appDirSuffix);
140
yield return Path.Combine ("/usr/share/", appDirSuffix);
143
foreach (string dir in envPath.Split (':')) {
144
yield return Path.Combine (dir, appDirSuffix);
137
public override IEnumerable<Item> ChildrenOfItem (Item item)
139
if (item is CategoryItem) {
140
CategoryItem catItem = item as CategoryItem;
142
.Where (a => a is ApplicationItem)
143
.Where (a => (a as ApplicationItem).Categories.Contains (catItem.Category));
145
return Enumerable.Empty<Item> ();
150
/// Return list of directories to scan for .desktop files.
153
/// Returns absolute paths.
154
/// Implements XDG data directory specification.
157
/// A <see cref="IEnumerable"/>
159
static IEnumerable<string> GetDesktopFileDirectories ()
162
// These are XDG variables...
165
}.SelectMany (v => GetXdgEnvironmentPaths (v));
168
static IEnumerable<string> GetXdgEnvironmentPaths (string xdgVar)
170
string envPath = Environment.GetEnvironmentVariable (xdgVar);
172
if (string.IsNullOrEmpty (envPath)) {
174
case "XDG_DATA_HOME":
175
yield return Path.Combine (
176
Environment.GetFolderPath (Environment.SpecialFolder.Personal),
177
".local/share/applications"
180
case "XDG_DATA_DIRS":
181
yield return "/usr/local/share/applications";
182
yield return "/usr/share/applications";
186
foreach (string dir in envPath.Split (':')) {
187
yield return Path.Combine (dir, "applications");