1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
// ApplicationItemSource.cs
//
// GNOME Do is the legal property of its developers. Please refer to the
// COPYRIGHT file distributed with this source distribution.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Do.Platform;
using Do.Universe;
using Mono.Unix;
namespace Do.Universe.Windows {
public class ApplicationItemSource : ItemSource {
const bool show_hidden = false;
static IEnumerable<string> desktop_file_directories;
IEnumerable<Item> app_items;
static ApplicationItemSource ()
{
desktop_file_directories = GetDesktopFileDirectories ();
}
public ApplicationItemSource ()
{
app_items = Enumerable.Empty<Item> ();
}
public override IEnumerable<Type> SupportedItemTypes
{
get
{
yield return typeof (ApplicationItem);
yield return typeof (CategoryItem);
}
}
public override string Name
{
get { return Catalog.GetString ("Applications"); }
}
public override string Description
{
get { return Catalog.GetString ("Finds applications in many locations."); }
}
public override string Icon
{
get { return "gnome-applications"; }
}
/// <summary>
/// Given an absolute path to a directory, scan that directory for
/// .desktop files, creating an ApplicationItem for each desktop file
/// found and adding the ApplicationItem to the list of
/// ApplicationItems.
/// </summary>
/// <param name="dir">
/// A <see cref="System.String"/> containing an absolute path to a
/// directory
/// where .desktop files can be found.
/// </param>
IEnumerable<ApplicationItem> LoadDesktopFiles (string dir)
{
return GetDesktopFiles ()
.Where (ShouldUseDesktopFile)
.Select (f => ApplicationItem.MaybeCreateFromDesktopItem (f)).Where (a => a != null)
.Where (ShouldUseApplicationItem);
}
IEnumerable<string> GetDesktopFiles ()
{
return desktop_file_directories
.Where (d => Directory.Exists (d))
.SelectMany (d => GetDesktopFiles (d));
}
IEnumerable<string> GetDesktopFiles (string parent)
{
IEnumerable<string> baseFiles = Directory.GetFiles (parent, "*.lnk");
IEnumerable<string> recursiveFiles = Directory.GetDirectories (parent).SelectMany (d => GetDesktopFiles (d));
return baseFiles.Concat (recursiveFiles);
}
IEnumerable<CategoryItem> LoadCategoryItems (ApplicationItem appItem)
{
return appItem.Categories
.Where (c => !CategoryItem.ContainsCategory (c))
.Select (c => CategoryItem.GetCategoryItem (c));
}
bool ShouldUseDesktopFile (string path)
{
path = path.ToLower ();
return !(path.Contains ("uninstall") || path.Contains ("install") || path.Contains ("remove"));
}
bool ShouldUseApplicationItem (ApplicationItem app)
{
return show_hidden || !app.NoDisplay;
}
public override void UpdateItems ()
{
IEnumerable<ApplicationItem> appItems = desktop_file_directories
.SelectMany (dir => LoadDesktopFiles (dir));
IEnumerable<CategoryItem> categoryItems = appItems
.SelectMany (a => LoadCategoryItems (a));
app_items = appItems
.Cast<Item> ()
.Concat (categoryItems.Cast<Item> ())
.Distinct ()
.ToArray ();
}
public override IEnumerable<Item> Items
{
get { return app_items; }
}
public override IEnumerable<Item> ChildrenOfItem (Item item)
{
if (item is CategoryItem) {
CategoryItem catItem = item as CategoryItem;
return app_items
.Where (a => a is ApplicationItem)
.Where (a => (a as ApplicationItem).Categories.Contains (catItem.Category));
} else {
return Enumerable.Empty<Item> ();
}
}
/// <summary>
/// Return list of directories to scan for application shortcuts.
/// </summary>
/// <comment>
/// Returns absolute paths.
/// </comment>
/// <returns>
/// A <see cref="IEnumerable"/>
/// </returns>
static IEnumerable<string> GetDesktopFileDirectories ()
{
// get the path to the "all users" start menu ** THIS IS FOR VISTA + WIN7
string commonStartMenu = Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.CommonApplicationData), "Microsoft");
commonStartMenu = Path.Combine (commonStartMenu, "Windows");
commonStartMenu = Path.Combine (commonStartMenu, "Start Menu");
/*
if (!File.Exists (commonStartMenu))
Console.WriteLine ("Common start menu: {0}", commonStartMenu);
Console.WriteLine (Environment.GetFolderPath (Environment.SpecialFolder.StartMenu));
*/
//Console.WriteLine (Environment.GetWin
return new [] {
Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
Environment.GetFolderPath(Environment.SpecialFolder.StartMenu),
Environment.GetFolderPath(Environment.SpecialFolder.Programs),
commonStartMenu,
};
}
}
}
|