~do-win/do/test-paths

« back to all changes in this revision

Viewing changes to Do.Platform.Windows/src/Do.Universe/ApplicationItem.cs

  • Committer: Hardeep S
  • Date: 2009-06-22 03:02:36 UTC
  • Revision ID: ootz0rz@gmail.com-20090622030236-n3dzhli1lsesqxo2
fixed formatting in ApplicationItem
added the gtk-gui folder from the linux platform, and modified namespaces to work with windows platform
added gui.stetic as a resource to the Do.Platform.Windows project

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
using Do.Platform;
29
29
 
30
30
using IWshRuntimeLibrary;
31
 
using System.Diagnostics; 
 
31
using System.Diagnostics;
32
32
 
33
33
namespace Do.Universe.Windows {
34
34
 
35
 
    internal class ApplicationItem : Item, IApplicationItem {
36
 
        private static WshShell shell = new WshShell ();
37
 
 
38
 
        const string DefaultApplicationIcon = "applications-other";
39
 
 
40
 
        static IDictionary<string, ApplicationItem> Instances { get; set; }
41
 
 
42
 
        static ApplicationItem ()
43
 
        {
44
 
            Instances = new Dictionary<string, ApplicationItem> ();
45
 
        }
46
 
 
47
 
        public static ApplicationItem MaybeCreateFromDesktopItem (string path)
48
 
        {
49
 
            ApplicationItem appItem;
50
 
 
51
 
            if (path == null) throw new ArgumentNullException ("path");
52
 
 
53
 
            if (Instances.ContainsKey (path)) {
54
 
                appItem = Instances [path];
55
 
            } else {
56
 
                IWshShortcut item = null;
57
 
                try {
58
 
                    item = (IWshShortcut) shell.CreateShortcut (path); 
59
 
                    appItem = new ApplicationItem (item);
60
 
                }
61
 
                catch (Exception e) {
62
 
                    appItem = null;
63
 
                    item = null;
64
 
                    Log.Error ("Could not load desktop item: {0}", e.Message);
65
 
                    Log.Debug (e.StackTrace);
66
 
                }
67
 
 
68
 
                if (appItem != null)
69
 
                    Instances [path] = appItem;
70
 
            }
71
 
            return appItem;
72
 
        }
73
 
 
74
 
        public static ApplicationItem MaybeCreateFromCmd (string cmd)
75
 
        {
76
 
            if (string.IsNullOrEmpty (cmd))
77
 
                return null;
78
 
 
79
 
            List<ApplicationItem> appItems = new List<ApplicationItem> ();
80
 
 
81
 
            cmd = Regex.Escape (cmd);
82
 
            Regex regex = new Regex (string.Format ("(^| ){0}( |)", cmd));
83
 
            foreach (ApplicationItem item in Instances.Values) {
84
 
                string path = item.Location;
85
 
                try {
86
 
                    if (path.StartsWith ("file://"))
87
 
                        path = path.Substring ("file://".Length);
88
 
 
89
 
                    path = Path.GetFileName (path);
90
 
                }
91
 
                catch { continue; }
92
 
 
93
 
                try {
94
 
                    if (!string.IsNullOrEmpty (path) && !string.IsNullOrEmpty (item.Exec) &&
95
 
                        (regex.IsMatch (path) || regex.IsMatch (item.Exec))) {
96
 
                        appItems.Add (item);
97
 
                    }
98
 
                }
99
 
                catch {
100
 
                    // it failed, probably a null somewhere, we dont care really
101
 
                }
102
 
            }
103
 
            
104
 
            return appItems[0];
105
 
        }
106
 
 
107
 
        protected IWshShortcut item;
108
 
        string name, description, icon, mimetype;
109
 
        IEnumerable<string> categories;
110
 
 
111
 
        /// <summary>
112
 
        /// Create an application item from a desktop file.
113
 
        /// </summary>
114
 
        /// <param name="desktopFile">
115
 
        /// A <see cref="System.String"/> containing the absolute path of
116
 
        /// a desktop (.desktop) file.
117
 
        /// </param>
118
 
        protected ApplicationItem (IWshShortcut item)
119
 
        {
120
 
            this.item = item;
121
 
 
122
 
            name = Path.GetFileNameWithoutExtension(item.FullName);
123
 
            description = item.Description;
124
 
            categories = Enumerable.Empty<string> ();
125
 
 
126
 
            if (item.IconLocation.StartsWith (","))
127
 
                icon = item.TargetPath + item.IconLocation;
128
 
            else
129
 
                icon = item.IconLocation;
130
 
        }
131
 
 
132
 
        public override string Name
133
 
        {
134
 
            get { return name; }
135
 
        }
136
 
 
137
 
        public override string Description
138
 
        {
139
 
            get { return description; }
140
 
        }
141
 
 
142
 
        public override string Icon
143
 
        {
144
 
            get { return icon; }
145
 
        }
146
 
 
147
 
        public IEnumerable<string> Categories
148
 
        {
149
 
            get { return categories; }
150
 
        }
151
 
 
152
 
        public bool NoDisplay
153
 
        {
154
 
            get
155
 
            {
156
 
                return false;
157
 
            }
158
 
        }
159
 
 
160
 
        public string Exec
161
 
        {
162
 
            get { return item.TargetPath; }
163
 
        }
164
 
 
165
 
        protected string Location
166
 
        {
167
 
            get { return item.TargetPath; }
168
 
        }
169
 
 
170
 
        public bool Hidden
171
 
        {
172
 
            get { return false; }
173
 
        }
174
 
 
175
 
        public bool IsUserCustomItem
176
 
        {
177
 
            get { return false; }
178
 
        }
179
 
 
180
 
        /// <summary>
181
 
        /// Executes the application.
182
 
        /// </summary>
183
 
        public void Run ()
184
 
        {
185
 
            Process.Start (item.TargetPath, item.Arguments);
186
 
        }
187
 
 
188
 
        public void LaunchWithFiles (IEnumerable<IFileItem> files)
189
 
        {
190
 
            foreach (IFileItem file in files)
191
 
                Process.Start (file.Path);
192
 
        }
193
 
    }
 
35
        internal class ApplicationItem : Item, IApplicationItem {
 
36
                private static WshShell shell = new WshShell ();
 
37
 
 
38
                const string DefaultApplicationIcon = "applications-other";
 
39
 
 
40
                static IDictionary<string, ApplicationItem> Instances { get; set; }
 
41
 
 
42
                static ApplicationItem ()
 
43
                {
 
44
                        Instances = new Dictionary<string, ApplicationItem> ();
 
45
                }
 
46
 
 
47
                public static ApplicationItem MaybeCreateFromDesktopItem (string path)
 
48
                {
 
49
                        ApplicationItem appItem;
 
50
 
 
51
                        if (path == null) throw new ArgumentNullException ("path");
 
52
 
 
53
                        if (Instances.ContainsKey (path)) {
 
54
                                appItem = Instances [path];
 
55
                        } else {
 
56
                                IWshShortcut item = null;
 
57
                                try {
 
58
                                        item = (IWshShortcut) shell.CreateShortcut (path);
 
59
                                        appItem = new ApplicationItem (item);
 
60
                                } catch (Exception e) {
 
61
                                        appItem = null;
 
62
                                        item = null;
 
63
                                        Log.Error ("Could not load desktop item: {0}", e.Message);
 
64
                                        Log.Debug (e.StackTrace);
 
65
                                }
 
66
 
 
67
                                if (appItem != null)
 
68
                                        Instances [path] = appItem;
 
69
                        }
 
70
                        return appItem;
 
71
                }
 
72
 
 
73
                public static ApplicationItem MaybeCreateFromCmd (string cmd)
 
74
                {
 
75
                        if (string.IsNullOrEmpty (cmd))
 
76
                                return null;
 
77
 
 
78
                        List<ApplicationItem> appItems = new List<ApplicationItem> ();
 
79
 
 
80
                        cmd = Regex.Escape (cmd);
 
81
                        Regex regex = new Regex (string.Format ("(^| ){0}( |)", cmd));
 
82
                        foreach (ApplicationItem item in Instances.Values) {
 
83
                                string path = item.Location;
 
84
                                try {
 
85
                                        if (path.StartsWith ("file://"))
 
86
                                                path = path.Substring ("file://".Length);
 
87
 
 
88
                                        path = Path.GetFileName (path);
 
89
                                } catch {
 
90
                                        continue;
 
91
                                }
 
92
 
 
93
                                try {
 
94
                                        if (!string.IsNullOrEmpty (path) && !string.IsNullOrEmpty (item.Exec) &&
 
95
                                                (regex.IsMatch (path) || regex.IsMatch (item.Exec))) {
 
96
                                                appItems.Add (item);
 
97
                                        }
 
98
                                } catch {
 
99
                                        // it failed, probably a null somewhere, we dont care really
 
100
                                }
 
101
                        }
 
102
 
 
103
                        return appItems [0];
 
104
                }
 
105
 
 
106
                protected IWshShortcut item;
 
107
                string name, description, icon, mimetype;
 
108
                IEnumerable<string> categories;
 
109
 
 
110
                /// <summary>
 
111
                /// Create an application item from a desktop file.
 
112
                /// </summary>
 
113
                /// <param name="desktopFile">
 
114
                /// A <see cref="System.String"/> containing the absolute path of
 
115
                /// a desktop (.desktop) file.
 
116
                /// </param>
 
117
                protected ApplicationItem (IWshShortcut item)
 
118
                {
 
119
                        this.item = item;
 
120
 
 
121
                        name = Path.GetFileNameWithoutExtension (item.FullName);
 
122
                        description = item.Description;
 
123
                        categories = Enumerable.Empty<string> ();
 
124
 
 
125
                        if (item.IconLocation.StartsWith (","))
 
126
                                icon = item.TargetPath + item.IconLocation;
 
127
                        else
 
128
                                icon = item.IconLocation;
 
129
                }
 
130
 
 
131
                public override string Name
 
132
                {
 
133
                        get { return name; }
 
134
                }
 
135
 
 
136
                public override string Description
 
137
                {
 
138
                        get { return description; }
 
139
                }
 
140
 
 
141
                public override string Icon
 
142
                {
 
143
                        get { return icon; }
 
144
                }
 
145
 
 
146
                public IEnumerable<string> Categories
 
147
                {
 
148
                        get { return categories; }
 
149
                }
 
150
 
 
151
                public bool NoDisplay
 
152
                {
 
153
                        get
 
154
                        {
 
155
                                return false;
 
156
                        }
 
157
                }
 
158
 
 
159
                public string Exec
 
160
                {
 
161
                        get { return item.TargetPath; }
 
162
                }
 
163
 
 
164
                protected string Location
 
165
                {
 
166
                        get { return item.TargetPath; }
 
167
                }
 
168
 
 
169
                public bool Hidden
 
170
                {
 
171
                        get { return false; }
 
172
                }
 
173
 
 
174
                public bool IsUserCustomItem
 
175
                {
 
176
                        get { return false; }
 
177
                }
 
178
 
 
179
                /// <summary>
 
180
                /// Executes the application.
 
181
                /// </summary>
 
182
                public void Run ()
 
183
                {
 
184
                        Process.Start (item.TargetPath, item.Arguments);
 
185
                }
 
186
 
 
187
                public void LaunchWithFiles (IEnumerable<IFileItem> files)
 
188
                {
 
189
                        foreach (IFileItem file in files)
 
190
                                Process.Start (file.Path);
 
191
                }
 
192
        }
194
193
}
 
 
b'\\ No newline at end of file'