forked from topharley/SCREENS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSettings.cs
131 lines (122 loc) · 4.13 KB
/
Settings.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
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
using Microsoft.Win32;
using Screens.Uploaders;
namespace Screens
{
public class Settings
{
public UploaderType UploaderType { get; set; }
public Boolean AutoStart { get; set; }
public Boolean UseHK { get; set; }
public Boolean ShortenUrl { get; set; }
public Boolean OpenInBrowser { get; set; }
public Boolean CopyInClipboard { get; set; }
public Boolean SaveLog { get; set; }
public String SaveLogFile { get; set; }
public Boolean SaveLastScreenshot { get; set; }
public String SaveLastScreenshotFile { get; set; }
public string WaterMark { get; set; }
public bool DrawWaterMark { get { return !String.IsNullOrWhiteSpace(WaterMark); } }
private static readonly string _settingsFile = Path.Combine(Application.StartupPath, "settings.xml");
private static readonly XmlSerializer _serializer = new XmlSerializer(typeof(Settings));
private static HotkeyBinder _hotKeyBinder;
private static Settings _current;
public static Settings Current
{
get { return _current ?? (_current = Load()); }
}
public Settings()
{
//defaults
CopyInClipboard = true;
OpenInBrowser = true;
}
public static void Save()
{
try
{
using (var writer = XmlWriter.Create(_settingsFile))
_serializer.Serialize(writer, _current);
}
catch
{
MessageBox.Show("Failed to save settings.");
}
}
private static Settings Load()
{
try
{
using (var stream = new FileStream(_settingsFile, FileMode.Open))
return _serializer.Deserialize(stream) as Settings;
}
catch
{
return new Settings();
}
}
public void Apply()
{
EnsureAutoStart();
BindHotKey();
}
private void BindHotKey()
{
if (Program.StartMode == StartMode.Tray)
{
if (UseHK)
{
if (_hotKeyBinder == null)
{
_hotKeyBinder = new HotkeyBinder();
_hotKeyBinder.Keys = Keys.PrintScreen;
_hotKeyBinder.ModKey = 0;
_hotKeyBinder.WindowHandle = Tray.TrayHandle;
_hotKeyBinder.HotKey += Clipper.TakeSnapshot;
_hotKeyBinder.Bind();
}
}
else
{
if (_hotKeyBinder != null)
{
_hotKeyBinder.Unbind();
_hotKeyBinder = null;
}
}
}
}
private void EnsureAutoStart()
{
#if DEBUG
//if (Process.GetProcessesByName("Screens.vshost").Length > 0)
// return;
#endif
if (Program.StartMode == StartMode.Capture)
{
try
{
RegistryKey runKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
if (AutoStart)
{
runKey.SetValue("Screens", "\"" + Application.ExecutablePath + "\" /tray");
var procs = Process.GetProcessesByName("Screens");
if (procs.Length == 1 && !procs[0].StartInfo.Arguments.Contains("/tray"))
Process.Start(Application.ExecutablePath, "/tray");
}
else
{
runKey.DeleteValue("Screens", false);
}
}
catch (Exception ex) { Console.WriteLine(ex.ToString()); }
}
}
}
}