-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathConfiguration.cs
More file actions
330 lines (279 loc) · 12.5 KB
/
Configuration.cs
File metadata and controls
330 lines (279 loc) · 12.5 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Security.Principal;
using System.Threading.Tasks;
using System.Windows.Forms;
using ProSnap.ActionItems;
using ProSnap.Uploading;
namespace ProSnap
{
public static class Configuration
{
public static string LocalPath
{
get
{
string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "factormystic.net", Application.ProductName, Application.ProductVersion);
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
return path;
}
}
public static string ConfigFilePath
{
get
{
return Path.Combine(LocalPath, "config.json");
}
}
public static string ApplicationFilePath
{
get
{
return Assembly.GetEntryAssembly().Location;
}
}
public static string RUninstallerFilePath
{
get
{
return Path.Combine(Directory.GetParent(LocalPath).FullName, "FMUtils.TaskRUninstaller.exe");
}
}
public static List<ShortcutItem> Shortcuts;
public static List<IUploadService> UploadServices;
public static bool IgnoreAllKeyHooks
{
get
{
return _ignoreallkeyhooks;
}
set
{
_ignoreallkeyhooks = value;
}
}
static bool _ignoreallkeyhooks = false;
public static bool isElevated
{
get
{
return new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator);
}
}
public static int PreviewDelayTime { get; set; }
public static FMUtils.WinApi.Helper.WindowLocation PreviewLocation { get; set; }
public static DockStyle ButtonPanelLocation { get; set; }
public static bool ShowPreviewWithoutActivation { get; set; }
public static bool UpdateRestartRequired { get; set; }
public static string DefaultSaveDirectory { get; set; }
public static string DefaultFileName { get; set; }
public static string FileDialogFilter { get; set; }
public static int DefaultFilterIndex { get; set; }
static Configuration()
{
MigrateFromPriorVersion();
Read();
}
private static void MigrateFromPriorVersion()
{
Version CurrentVersion = Version.Parse(Application.ProductVersion);
var ParentDirectory = System.IO.Directory.GetParent(Configuration.LocalPath);
try
{
//Try and get content out of any prior version data directory
foreach (var dir in ParentDirectory.EnumerateDirectories())
{
Version v;
if (Version.TryParse(dir.Name, out v))
{
if (CurrentVersion.CompareTo(v) > 0)
{
foreach (var ss in dir.EnumerateDirectories("screenshots", SearchOption.TopDirectoryOnly))
{
var From = ss.FullName;
var To = Path.Combine(Configuration.LocalPath, "screenshots");
if (From != To)
Directory.Move(From, To);
}
var TempProductPath = Path.Combine(Environment.ExpandEnvironmentVariables("%temp%"), Application.ProductName);
//Directory.Move requires the parent of the last directory part exist, which in this case is the product name directory
if (!Directory.Exists(TempProductPath))
Directory.CreateDirectory(TempProductPath);
var Destination = Path.Combine(Environment.ExpandEnvironmentVariables("%temp%"), Application.ProductName, dir.Name);
if (!Directory.Exists(Destination))
Directory.Move(dir.FullName, Destination);
}
}
}
}
catch (Exception ex)
{
//Really, swallow everything here, but log it
Trace.WriteLine(string.Format("Failed: '{0}'", ex.GetBaseException().Message), string.Format("Configuration.MigrateFromPriorVersion (screenshots) [{0}]", System.Threading.Thread.CurrentThread.Name));
}
try
{
//Copy FMUtils.TaskRUninstaller.exe and its dependency to somewhere that will exist across versions and when the ClickOnce app is uninstalled
var From = Path.Combine(Directory.GetParent(Configuration.ApplicationFilePath).FullName, "FMUtils.TaskRUninstaller.exe");
var To = Path.Combine(ParentDirectory.FullName, "FMUtils.TaskRUninstaller.exe");
File.Copy(From, To, true);
var FromDep = Path.Combine(Directory.GetParent(Configuration.ApplicationFilePath).FullName, "Microsoft.Win32.TaskScheduler.dll");
var ToDep = Path.Combine(ParentDirectory.FullName, "Microsoft.Win32.TaskScheduler.dll");
File.Copy(FromDep, ToDep, true);
}
catch (Exception ex)
{
//Really, swallow everything here, but log it
Trace.WriteLine(string.Format("Failed: '{0}'", ex.GetBaseException().Message), string.Format("Configuration.MigrateFromPriorVersion (FMUtils.TaskRUninstaller) [{0}]", System.Threading.Thread.CurrentThread.Name));
}
}
internal static void Read()
{
if (File.Exists(ConfigFilePath))
{
//todo: read
}
else
{
LoadDefaults();
}
}
internal static void Write()
{
//todo: write
}
public static void LoadDefaults()
{
LoadDefaultProperties();
LoadDefaultShortcuts();
LoadDefaultUploadServices();
}
private static void LoadDefaultProperties()
{
PreviewDelayTime = 5;
PreviewLocation = FMUtils.WinApi.Helper.WindowLocation.LowerRight;
ButtonPanelLocation = DockStyle.Bottom;
ShowPreviewWithoutActivation = false;
DefaultSaveDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "ProSnap Screenshots");
DefaultFileName = @":yyyy:MM:dd_:hh:mm:ss:tt :w.png";
FileDialogFilter = @"PNG File (*.png)|*.png|Bitmap (*.bmp)|*.bmp|JPEG File (*.jpg)|*.jpg; *.jpeg|GIF File (*.gif)|*.gif|All files (*.*)|*.*";
DefaultFilterIndex = 0;
}
private static void LoadDefaultShortcuts()
{
Shortcuts = new List<ShortcutItem>();
Shortcuts.Add(new ShortcutItem(true, false, new KeyCombo(Keys.PrintScreen), new ProgramActionChain("Create Foreground Window Screenshot")
{
ActionItems = {
ActionTypes.TakeForegroundScreenshot.ToInstance(),
ActionTypes.ApplyEdits.ToInstance(),
ActionTypes.ShowPreview.ToInstance(),
}
}));
Shortcuts.Add(new ShortcutItem(true, false, new KeyCombo(Keys.PrintScreen, alt: true), new ProgramActionChain("Create Region Screenshot")
{
ActionItems = {
ActionTypes.TakeRegionScreenshot.ToInstance(),
ActionTypes.ShowPreview.ToInstance(),
}
}));
var OpenInBrowser = ActionTypes.Run.ToInstance() as RunAction;
OpenInBrowser.ApplicationPath = "cmd.exe";
OpenInBrowser.Parameters = "/c start \"\" \":url?DELETE_URL=:delete\"";
var CopyImageUrl = ActionTypes.Clipboard.ToInstance() as ClipboardAction;
CopyImageUrl.Content = ":url";
Shortcuts.Add(new ShortcutItem(true, false, new KeyCombo(Keys.PrintScreen, ctrl: true, shift: true), new ProgramActionChain("Create & Upload Screenshot")
{
ActionItems = {
ActionTypes.TakeForegroundScreenshot.ToInstance(),
ActionTypes.ApplyEdits.ToInstance(),
ActionTypes.Upload.ToInstance(),
CopyImageUrl,
OpenInBrowser,
}
}));
Shortcuts.Add(new ShortcutItem(true, false, new KeyCombo(Keys.Scroll), new ProgramActionChain("Begin Scrolling Screenshot")
{
ActionItems = {
ActionTypes.BeginScrollingScreenshot.ToInstance()
}
}));
Shortcuts.Add(new ShortcutItem(true, false, new KeyCombo(Keys.PageDown), new ProgramActionChain("Continue Scrolling Screenshot")
{
ActionItems = {
ActionTypes.ContinueScrollingScreenshot.ToInstance()
}
}));
Shortcuts.Add(new ShortcutItem(true, false, new KeyCombo(Keys.End), new ProgramActionChain("End Scrolling Screenshot")
{
ActionItems = {
ActionTypes.EndScrollingScreenshot.ToInstance(),
ActionTypes.ShowPreview.ToInstance()
}
}));
Shortcuts.Add(new ShortcutItem(true, true, new KeyCombo(Keys.Enter), new ProgramActionChain("(Default) Toggle Heart")
{
ActionItems = { ActionTypes.Heart.ToInstance() }
}));
Shortcuts.Add(new ShortcutItem(true, true, new KeyCombo(Keys.S, ctrl: true), new ProgramActionChain("(Default) Prompt to Save")
{
ActionItems = {
new SaveAction() {
FilePath = Configuration.DefaultFileName,
Prompt = true
} }
}));
Shortcuts.Add(new ShortcutItem(true, true, new KeyCombo(Keys.Up, ctrl: true), new ProgramActionChain("(Default) Upload")
{
ActionItems = { ActionTypes.Upload.ToInstance() }
}));
Shortcuts.Add(new ShortcutItem(true, true, new KeyCombo(Keys.Down, ctrl: true), new ProgramActionChain("(Default) Edit in default editor")
{
ActionItems = {
new RunAction() {
Mode = RunAction.Modes.ShellVerb,
ShellVerb = "edit",
Parameters = "\":file\""
}
}
}));
Shortcuts.Add(new ShortcutItem(true, true, new KeyCombo(Keys.Delete), new ProgramActionChain("(Default) Delete")
{
ActionItems = { ActionTypes.Delete.ToInstance() }
}));
Shortcuts.Add(new ShortcutItem(true, true, new KeyCombo(Keys.Escape), new ProgramActionChain("(Default) Hide Preview")
{
ActionItems = { ActionTypes.HidePreview.ToInstance() }
}));
}
private static void LoadDefaultUploadServices()
{
UploadServices = new List<IUploadService>();
UploadServices.Add(new MultipartFormDataUploadService("Imgur Public")
{
isActive = true,
EndpointUrl = "http://imgur.com/api/upload.xml",
UploadValues = new NameValueCollection
{
{"key", "21925b62ac028b00051243ea38965eb9"},
{"image", "%i"}
},
ImageLinkXPath = "rsp/original_image",
DeleteLinkXPath = "rsp/delete_page",
});
}
internal static Task<bool> ToggleStartupTask()
{
return Task.Factory.StartNew<bool>(() => FMUtils.TaskRUninstaller.Helper.Toggle(Configuration.RUninstallerFilePath, "ProSnap", Configuration.ApplicationFilePath));
}
internal static Task<bool> GetStartupTaskStatus()
{
return Task.Factory.StartNew<bool>(() => FMUtils.TaskRUninstaller.Helper.IsTaskInstalled("ProSnap"));
}
}
}