-
Notifications
You must be signed in to change notification settings - Fork 3
/
Item.cs
230 lines (179 loc) · 7.35 KB
/
Item.cs
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Input;
namespace Flow.Launcher.Plugin.Favorites
{
class Item
{
public string Name { get; set; }
public string Value { get; set; }
string _iconPath;
public string IconPath {
get {
if (_iconPath == null)
{
string value = Value;
if (value != null && value.Contains("%"))
value = ExpandEnvVars(value);
if (value != null && value.StartsWith("http"))
_iconPath = Path.Combine(Favorites.AssemblyDirectory, "Icons\\Web.ico");
else if (value != null && value.StartsWith("shell:"))
_iconPath = @"C:\Windows\explorer.exe";
else if (value != null && value.Contains(".") && File.Exists(value))
{
string txtIconPath = Path.Combine(Favorites.AssemblyDirectory, "Icons\\txt.ico");
if (value.EndsWith(".txt") && File.Exists(txtIconPath))
_iconPath = txtIconPath;
else
_iconPath = value;
}
else if (Directory.Exists(value))
_iconPath = @"C:\Windows\explorer.exe";
else
_iconPath = Path.Combine(Favorites.AssemblyDirectory, "Icons\\CommandLine.ico");
}
return _iconPath;
}
}
public void Execute(bool asAdmin = false)
{
string value = Value;
if (string.IsNullOrEmpty(value))
return;
if (value.Contains("%"))
value = ExpandEnvVars(value);
bool isFolder = Directory.Exists(value);
if (value.Length > 3 && value[1..].StartsWith(":\\") &&
value.Contains(" ") && (File.Exists(value) || Directory.Exists(value)))
value = "\"" + value + "\"";
Match match = Regex.Match(value, "((?<file>[^\\s\"]+)|\"(?<file>.+?)\") *(?<args>[^\\f\\r]*)");
var info = new ProcessStartInfo();
bool showParent = (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)) &&
!(Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift));
if (showParent)
info.FileName = Path.GetDirectoryName(match.Groups["file"].Value);
else
{
if (isFolder)
{
info.FileName = Favorites.FolderAppPath;
info.Arguments = Favorites.FolderAppArguments.Replace("%1", match.Groups["file"].Value);
}
else
{
info.FileName = match.Groups["file"].Value;
info.Arguments = match.Groups["args"].Value;
}
}
info.UseShellExecute = true;
if (asAdmin)
info.Verb = "runas";
using Process p = new Process() { StartInfo = info };
try {
p.Start();
} catch { }
}
static string ExpandEnvVars(string value)
{
if (string.IsNullOrEmpty(value))
return "";
if (value.Contains("%"))
value = Environment.ExpandEnvironmentVariables(value);
return value;
}
public static List<Item> LoadFile(string path)
{
List<Item> ret = new List<Item>();
if (!File.Exists(path))
return ret;
foreach (string it in File.ReadAllLines(path))
{
string line = it.Trim();
if (line.StartsWith("#folder-app-path:"))
Favorites.FolderAppPath = line.Substring(line.IndexOf(":") + 1).Trim();
else if (line.StartsWith("#folder-app-args:"))
Favorites.FolderAppArguments = line.Substring(line.IndexOf(":") + 1).Trim();
if (line.StartsWith("#") || !line.Contains("="))
continue;
Item item = new Item() {
Name = line.Substring(0, line.IndexOf("=")).Trim(),
Value = line.Substring(line.IndexOf("=") + 1).Trim()
};
ret.Add(item);
}
return ret;
}
public static List<Item> Filter(List<Item> items, string value)
{
List<Item> ret = new List<Item>();
if (string.IsNullOrEmpty(value))
return ret;
string[] searches = value.Split(' ', StringSplitOptions.RemoveEmptyEntries);
string valueLower = value.ToLower();
if (value.Length == 1)
{
foreach (Item item in items)
{
if (item.Name.Contains("__"))
item.Name.Replace("__", "!dbl!");
int index = item.Name.IndexOf("_");
if (index > -1 && item.Name.Length > index + 1 && item.Name.ToLower()[index + 1] == value[0])
ret.Add(item);
if (item.Name.Contains("!dbl!"))
item.Name = item.Name.Replace("!dbl!", "_");
}
return ret;
}
// all searches
if (searches.Length > 1)
{
foreach (Item item in items)
{
bool notFound = false;
foreach (string search in searches)
{
if (!item.Name.ToLower().Contains(search.ToLower()) && !item.Value.ToLower().Contains(search.ToLower()))
{
notFound = true;
break;
}
}
if (notFound)
continue;
else
if (!ret.Contains(item))
ret.Add(item);
}
}
// upper chars
foreach (Item item in items)
{
if (ret.Contains(item))
continue;
string upperChars = "";
foreach (char ch in item.Name)
if (char.IsUpper(ch))
upperChars += ch;
if (upperChars.ToLower().Contains(valueLower) && !ret.Contains(item))
ret.Add(item);
}
// name starts with
foreach (Item item in items)
if (item.Name.ToLower().StartsWith(valueLower) && !ret.Contains(item))
ret.Add(item);
// name contains
foreach (Item item in items)
if (item.Name.ToLower().Contains(valueLower) && !ret.Contains(item))
ret.Add(item);
// value contains
foreach (Item item in items)
if (item.Value.ToLower().Contains(valueLower) && !ret.Contains(item))
ret.Add(item);
return ret;
}
}
}