~cszikszoy/do/do-fix-keybindings

« back to all changes in this revision

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

  • Committer: Chris S.
  • Date: 2009-06-18 06:18:12 UTC
  • mfrom: (1107.2.124 trunk)
  • Revision ID: chris@szikszoy.com-20090618061812-8ynmedlxpmvwkwe9
merge trunk & make properties public

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
                
39
39
                static ApplicationItemSource ()
40
40
                {
41
 
                        desktop_file_directories = ReadXdgDataDirs ();
 
41
                        desktop_file_directories = GetDesktopFileDirectories ();
42
42
                }
43
43
                
44
44
                public ApplicationItemSource ()
47
47
                }
48
48
 
49
49
                public override IEnumerable<Type> SupportedItemTypes {
50
 
                        get { yield return typeof (ApplicationItem); }
 
50
                        get { 
 
51
                                yield return typeof (ApplicationItem); 
 
52
                                yield return typeof (CategoryItem);
 
53
                        }
51
54
                }
52
55
 
53
56
                public override string Name {
73
76
                /// directory
74
77
                /// where .desktop files can be found.
75
78
                /// </param>
76
 
                static IEnumerable<ApplicationItem> LoadDesktopFiles (string dir)
77
 
                {
78
 
                        Queue<string> queue;
79
 
                        List<ApplicationItem> apps;
80
 
                        
81
 
                        if (!Directory.Exists (dir))
82
 
                                return Enumerable.Empty<ApplicationItem> ();
83
 
                        
84
 
                        apps = new List<ApplicationItem> ();
85
 
                        queue = new Queue<string> ();
86
 
                        queue.Enqueue (dir);
87
 
                                
88
 
                        while (queue.Any ()) {
89
 
                                dir = queue.Dequeue ();
90
 
 
91
 
                                // Do not index screensavers.
92
 
                                if (dir.Contains ("screensavers"))
93
 
                                        continue;
94
 
 
95
 
                                foreach (string subdir in Directory.GetDirectories (dir))
96
 
                                        queue.Enqueue (subdir);
97
 
                                
98
 
                                apps.AddRange (
99
 
                                        Directory.GetFiles (dir, "*.desktop")
100
 
                                                .Select (f => ApplicationItem.MaybeCreateFromDesktopItem (f))
101
 
                                                .Where (app =>
102
 
                                                        app != null && app.IsAppropriateForCurrentDesktop &&
103
 
                                                        (show_hidden || !app.NoDisplay))
104
 
                                );
105
 
                        }
106
 
                        return apps;
 
79
                IEnumerable<ApplicationItem> LoadDesktopFiles (string dir)
 
80
                {
 
81
                        return GetDesktopFiles ()
 
82
                                .Where (ShouldUseDesktopFile)
 
83
                                .Select (f => ApplicationItem.MaybeCreateFromDesktopItem (f)).Where (a => a != null)
 
84
                                .Where (ShouldUseApplicationItem);
 
85
                }
 
86
                
 
87
                IEnumerable<string> GetDesktopFiles ()
 
88
                {
 
89
                        return desktop_file_directories
 
90
                                .Where (d => Directory.Exists (d))
 
91
                                .SelectMany (d => GetDesktopFiles (d));
 
92
                }
 
93
                
 
94
                IEnumerable<string> GetDesktopFiles (string parent)
 
95
                {
 
96
                        IEnumerable<string> baseFiles      = Directory.GetFiles (parent, "*.desktop");
 
97
                        IEnumerable<string> recursiveFiles = Directory.GetDirectories (parent).SelectMany (d => GetDesktopFiles (d));
 
98
                        return baseFiles.Concat (recursiveFiles);
 
99
                }
 
100
                
 
101
                IEnumerable<CategoryItem> LoadCategoryItems (ApplicationItem appItem)
 
102
                {
 
103
                        return appItem.Categories
 
104
                                .Where (c => !CategoryItem.ContainsCategory (c))
 
105
                                .Select (c => CategoryItem.GetCategoryItem (c));
 
106
                }
 
107
                
 
108
                bool ShouldUseDesktopFile (string path)
 
109
                {
 
110
                        return !path.Contains ("screensavers");
 
111
                }
 
112
                
 
113
                bool ShouldUseApplicationItem (ApplicationItem app)
 
114
                {
 
115
                        return app.IsAppropriateForCurrentDesktop && (show_hidden || !app.NoDisplay);
107
116
                }
108
117
                
109
118
                public override void UpdateItems ()
110
119
                {
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));
 
122
                        
 
123
                        IEnumerable<CategoryItem> categoryItems = appItems
 
124
                                .SelectMany (a => LoadCategoryItems (a));
 
125
 
 
126
                        app_items = appItems
114
127
                                .Cast<Item> ()
 
128
                                .Concat (categoryItems.Cast<Item> ())
 
129
                                .Distinct ()
115
130
                                .ToArray ();
116
131
                }
117
132
 
119
134
                        get { return app_items; }
120
135
                }
121
136
                
122
 
                static IEnumerable<string> ReadXdgDataDirs ()
123
 
                {
124
 
                        string home, envPath;
125
 
                        
126
 
                        const string appDirSuffix = "applications";
127
 
                        string [] xdgVars = { "XDG_DATA_HOME", "XDG_DATA_DIRS" };
128
 
                        
129
 
                        home = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
130
 
                                
131
 
                        foreach (string xdgVar in xdgVars) {
132
 
                                envPath = Environment.GetEnvironmentVariable (xdgVar);
133
 
                                                
134
 
                                if (string.IsNullOrEmpty (envPath)) {
135
 
                                        if (xdgVar == "XDG_DATA_HOME") {
136
 
                                                yield return
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);
141
 
                                        }
142
 
                                } else {
143
 
                                        foreach (string dir in envPath.Split (':')) {
144
 
                                                yield return Path.Combine (dir, appDirSuffix);
145
 
                                        }
 
137
                public override IEnumerable<Item> ChildrenOfItem (Item item)
 
138
                {
 
139
                        if (item is CategoryItem) {
 
140
                                CategoryItem catItem = item as CategoryItem;
 
141
                                return app_items
 
142
                                        .Where (a => a is ApplicationItem)
 
143
                                        .Where (a => (a as ApplicationItem).Categories.Contains (catItem.Category));
 
144
                        } else {
 
145
                                return Enumerable.Empty<Item> ();
 
146
                        }
 
147
                }
 
148
                
 
149
                /// <summary>
 
150
                /// Return list of directories to scan for .desktop files.
 
151
                /// </summary>
 
152
                /// <comment>
 
153
                /// Returns absolute paths.
 
154
                /// Implements XDG data directory specification.
 
155
                /// </comment>
 
156
                /// <returns>
 
157
                /// A <see cref="IEnumerable"/>
 
158
                /// </returns>
 
159
                static IEnumerable<string> GetDesktopFileDirectories ()
 
160
                {
 
161
                        return new [] {
 
162
                                // These are XDG variables...
 
163
                                "XDG_DATA_HOME",
 
164
                                "XDG_DATA_DIRS"
 
165
                        }.SelectMany (v => GetXdgEnvironmentPaths (v));
 
166
                }
 
167
                
 
168
                static IEnumerable<string> GetXdgEnvironmentPaths (string xdgVar)
 
169
                {
 
170
                        string envPath = Environment.GetEnvironmentVariable (xdgVar);
 
171
                        
 
172
                        if (string.IsNullOrEmpty (envPath)) {
 
173
                                switch (xdgVar) {
 
174
                                case "XDG_DATA_HOME":
 
175
                                        yield return Path.Combine (
 
176
                                                Environment.GetFolderPath (Environment.SpecialFolder.Personal),
 
177
                                                ".local/share/applications"
 
178
                                        );
 
179
                                        break;
 
180
                                case "XDG_DATA_DIRS":
 
181
                                        yield return "/usr/local/share/applications";
 
182
                                        yield return "/usr/share/applications";
 
183
                                        break;
 
184
                                }
 
185
                        } else {
 
186
                                foreach (string dir in envPath.Split (':')) {
 
187
                                        yield return Path.Combine (dir, "applications");
146
188
                                }
147
189
                        }
148
190
                }