30
30
using IWshRuntimeLibrary;
31
using System.Diagnostics;
31
using System.Diagnostics;
33
33
namespace Do.Universe.Windows {
35
internal class ApplicationItem : Item, IApplicationItem {
36
private static WshShell shell = new WshShell ();
38
const string DefaultApplicationIcon = "applications-other";
40
static IDictionary<string, ApplicationItem> Instances { get; set; }
42
static ApplicationItem ()
44
Instances = new Dictionary<string, ApplicationItem> ();
47
public static ApplicationItem MaybeCreateFromDesktopItem (string path)
49
ApplicationItem appItem;
51
if (path == null) throw new ArgumentNullException ("path");
53
if (Instances.ContainsKey (path)) {
54
appItem = Instances [path];
56
IWshShortcut item = null;
58
item = (IWshShortcut) shell.CreateShortcut (path);
59
appItem = new ApplicationItem (item);
64
Log.Error ("Could not load desktop item: {0}", e.Message);
65
Log.Debug (e.StackTrace);
69
Instances [path] = appItem;
74
public static ApplicationItem MaybeCreateFromCmd (string cmd)
76
if (string.IsNullOrEmpty (cmd))
79
List<ApplicationItem> appItems = new List<ApplicationItem> ();
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;
86
if (path.StartsWith ("file://"))
87
path = path.Substring ("file://".Length);
89
path = Path.GetFileName (path);
94
if (!string.IsNullOrEmpty (path) && !string.IsNullOrEmpty (item.Exec) &&
95
(regex.IsMatch (path) || regex.IsMatch (item.Exec))) {
100
// it failed, probably a null somewhere, we dont care really
107
protected IWshShortcut item;
108
string name, description, icon, mimetype;
109
IEnumerable<string> categories;
112
/// Create an application item from a desktop file.
114
/// <param name="desktopFile">
115
/// A <see cref="System.String"/> containing the absolute path of
116
/// a desktop (.desktop) file.
118
protected ApplicationItem (IWshShortcut item)
122
name = Path.GetFileNameWithoutExtension(item.FullName);
123
description = item.Description;
124
categories = Enumerable.Empty<string> ();
126
if (item.IconLocation.StartsWith (","))
127
icon = item.TargetPath + item.IconLocation;
129
icon = item.IconLocation;
132
public override string Name
137
public override string Description
139
get { return description; }
142
public override string Icon
147
public IEnumerable<string> Categories
149
get { return categories; }
152
public bool NoDisplay
162
get { return item.TargetPath; }
165
protected string Location
167
get { return item.TargetPath; }
172
get { return false; }
175
public bool IsUserCustomItem
177
get { return false; }
181
/// Executes the application.
185
Process.Start (item.TargetPath, item.Arguments);
188
public void LaunchWithFiles (IEnumerable<IFileItem> files)
190
foreach (IFileItem file in files)
191
Process.Start (file.Path);
35
internal class ApplicationItem : Item, IApplicationItem {
36
private static WshShell shell = new WshShell ();
38
const string DefaultApplicationIcon = "applications-other";
40
static IDictionary<string, ApplicationItem> Instances { get; set; }
42
static ApplicationItem ()
44
Instances = new Dictionary<string, ApplicationItem> ();
47
public static ApplicationItem MaybeCreateFromDesktopItem (string path)
49
ApplicationItem appItem;
51
if (path == null) throw new ArgumentNullException ("path");
53
if (Instances.ContainsKey (path)) {
54
appItem = Instances [path];
56
IWshShortcut item = null;
58
item = (IWshShortcut) shell.CreateShortcut (path);
59
appItem = new ApplicationItem (item);
60
} catch (Exception e) {
63
Log.Error ("Could not load desktop item: {0}", e.Message);
64
Log.Debug (e.StackTrace);
68
Instances [path] = appItem;
73
public static ApplicationItem MaybeCreateFromCmd (string cmd)
75
if (string.IsNullOrEmpty (cmd))
78
List<ApplicationItem> appItems = new List<ApplicationItem> ();
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;
85
if (path.StartsWith ("file://"))
86
path = path.Substring ("file://".Length);
88
path = Path.GetFileName (path);
94
if (!string.IsNullOrEmpty (path) && !string.IsNullOrEmpty (item.Exec) &&
95
(regex.IsMatch (path) || regex.IsMatch (item.Exec))) {
99
// it failed, probably a null somewhere, we dont care really
106
protected IWshShortcut item;
107
string name, description, icon, mimetype;
108
IEnumerable<string> categories;
111
/// Create an application item from a desktop file.
113
/// <param name="desktopFile">
114
/// A <see cref="System.String"/> containing the absolute path of
115
/// a desktop (.desktop) file.
117
protected ApplicationItem (IWshShortcut item)
121
name = Path.GetFileNameWithoutExtension (item.FullName);
122
description = item.Description;
123
categories = Enumerable.Empty<string> ();
125
if (item.IconLocation.StartsWith (","))
126
icon = item.TargetPath + item.IconLocation;
128
icon = item.IconLocation;
131
public override string Name
136
public override string Description
138
get { return description; }
141
public override string Icon
146
public IEnumerable<string> Categories
148
get { return categories; }
151
public bool NoDisplay
161
get { return item.TargetPath; }
164
protected string Location
166
get { return item.TargetPath; }
171
get { return false; }
174
public bool IsUserCustomItem
176
get { return false; }
180
/// Executes the application.
184
Process.Start (item.TargetPath, item.Arguments);
187
public void LaunchWithFiles (IEnumerable<IFileItem> files)
189
foreach (IFileItem file in files)
190
Process.Start (file.Path);
b'\\ No newline at end of file'