~do-core/do/trunk

« back to all changes in this revision

Viewing changes to Do.Platform.Linux/src/Plugins/Applications/ApplicationItemSource.cs

  • Committer: David Siegel
  • Date: 2008-11-23 21:51:31 UTC
  • Revision ID: david@dell-desktop-20081123215131-kfohdq0ntipk782p
Restructured codebase to introduce a new layer of organization, and to begin mass refactorization of Do.Addins (and core) into separate assemblies.

        * Do.Platform: platform-neutral codebase to be used by non-interface plugins and core. Plugins that only use code from this assembly are cross-platform.
                * Do.Platform.UniverseFactory: create abstract items, like Applications (IApplicationItem) and Files (IFileItem).
                * Do.Platform.*: Platform-neutral logging, preferences, other utilities.

        * Do.Platform.Linux: Linux-specific implementations to be used by classes in Do.Platform. Plugins that use code from this assembly are Linux-dependent.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ApplicationItemSource.cs
 
2
 *
 
3
 * GNOME Do is the legal property of its developers. Please refer to the
 
4
 * COPYRIGHT file distributed with this
 
5
 * source distribution.
 
6
 *
 
7
 * This program is free software: you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation, either version 3 of the License, or
 
10
 * (at your option) any later version.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
 */
 
20
 
 
21
using System;
 
22
using System.IO;
 
23
using System.Linq;
 
24
using System.Collections.Generic;
 
25
 
 
26
using Do.Platform;
 
27
using Do.Universe;
 
28
 
 
29
using Mono.Unix;
 
30
 
 
31
namespace Do.Platform.Linux {
 
32
 
 
33
        public class ApplicationItemSource : IItemSource {
 
34
 
 
35
                const bool show_hidden = false;
 
36
 
 
37
                private IEnumerable<IItem> app_items;
 
38
 
 
39
                /// <summary>
 
40
                /// Locations to search for .desktop files.
 
41
                /// </summary>
 
42
                static IEnumerable<String> DesktopFilesDirectories {
 
43
                        get {
 
44
                                return new string[] {
 
45
                                        "~/.local/share/applications/wine",
 
46
                                        "~/.local/share/applications",
 
47
                                        "/usr/share/applications",
 
48
                                        "/usr/share/applications/kde",
 
49
                                        "/usr/share/applications/kde4",
 
50
                                        "/usr/share/gdm/applications",
 
51
                                        "/usr/local/share/applications",
 
52
                                        Paths.UserDesktop,
 
53
                                };
 
54
                        }
 
55
                }
 
56
                
 
57
                public ApplicationItemSource ()
 
58
                {
 
59
                        app_items = Enumerable.Empty<IItem> ();
 
60
                }
 
61
 
 
62
                public IEnumerable<Type> SupportedItemTypes {
 
63
                        get { yield return typeof (ApplicationItem); }
 
64
                }
 
65
 
 
66
                public string Name {
 
67
                        get { return Catalog.GetString ("Applications"); }
 
68
                }
 
69
 
 
70
                public string Description {
 
71
                        get { return Catalog.GetString ("Finds applications in many locations."); }
 
72
                }
 
73
 
 
74
                public string Icon {
 
75
                        get { return "gnome-applications"; }
 
76
                }
 
77
 
 
78
                /// <summary>
 
79
                /// Given an absolute path to a directory, scan that directory for
 
80
                /// .desktop files, creating an ApplicationItem for each desktop file
 
81
                /// found and adding the ApplicationItem to the list of
 
82
                /// ApplicationItems.
 
83
                /// </summary>
 
84
                /// <param name="dir">
 
85
                /// A <see cref="System.String"/> containing an absolute path to a
 
86
                /// directory
 
87
                /// where .desktop files can be found.
 
88
                /// </param>
 
89
                private static IEnumerable<ApplicationItem> LoadDesktopFiles (string dir)
 
90
                {
 
91
                        if (!Directory.Exists (dir))
 
92
                                return Enumerable.Empty<ApplicationItem> ();
 
93
 
 
94
                        return Directory.GetFiles (dir, "*.desktop")
 
95
                                .Select (file => new ApplicationItem (file))
 
96
                                .Where (app => !string.IsNullOrEmpty (app.Name) &&
 
97
                                                                                         app.IsAppropriateForCurrentDesktop &&
 
98
                                                                                         (show_hidden || !app.NoDisplay));
 
99
                }
 
100
 
 
101
                public void UpdateItems ()
 
102
                {
 
103
                        // Updating is turned off because it uses a ridiculous amount of memory.
 
104
                        if (app_items.Any ()) return;
 
105
                        
 
106
                        app_items = DesktopFilesDirectories
 
107
                                .Select (dir => dir.Replace ("~", Paths.UserHome))
 
108
                                .Select (dir => LoadDesktopFiles (dir))
 
109
                                .Aggregate ((a, b) => Enumerable.Concat (a, b))
 
110
                                .Cast<IItem> ()
 
111
                                .ToList ();
 
112
                }
 
113
 
 
114
                public IEnumerable<IItem> Items {
 
115
                        get { return app_items; }
 
116
                }
 
117
 
 
118
                public IEnumerable<IItem> ChildrenOfItem (IItem item)
 
119
                {
 
120
                        return null;
 
121
                }
 
122
 
 
123
        }
 
124
}