-
Notifications
You must be signed in to change notification settings - Fork 9
/
PreferencesWindow.cs
179 lines (151 loc) · 6.43 KB
/
PreferencesWindow.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
using FreenetTray.Browsers;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using NLog;
using NLog.Config;
namespace FreenetTray
{
public partial class PreferencesWindow : Form
{
private const int StartIconIndex = 0;
private const int StartFreenetIndex = 1;
private const string RegistryStartupName = "Freenet";
private const string StartupKeyLocation = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
private readonly string _initialCustomLocation;
public PreferencesWindow(IEnumerable<string> availableBrowsers)
{
InitializeComponent();
StartupCheckboxList.SetItemChecked(StartIconIndex,
Properties.Settings.Default.StartIcon);
StartupCheckboxList.SetItemChecked(StartFreenetIndex,
Properties.Settings.Default.StartFreenet);
// TODO: Localize?
BrowserChoice.Items.Add(BrowserUtil.Auto);
foreach (var browser in availableBrowsers)
{
BrowserChoice.Items.Add(browser);
}
BrowserChoice.Text = Properties.Settings.Default.UseBrowser;
if (!BrowserChoice.Items.Contains(BrowserChoice.Text))
{
// TODO: User's preference not found. Worthy of a message box?
BrowserChoice.Text = BrowserUtil.Auto;
}
SlowStartOption.Checked = Properties.Settings.Default.ShowSlowOpenTip;
LogLevelChoice.Text = Properties.Settings.Default.LogLevel;
CustomLocationDisplay.Text = Properties.Settings.Default.CustomLocation;
_initialCustomLocation = Properties.Settings.Default.CustomLocation;
}
private void Apply_Click(object sender, EventArgs e)
{
Properties.Settings.Default.StartIcon = StartupCheckboxList.GetItemChecked(StartIconIndex);
Properties.Settings.Default.StartFreenet = StartupCheckboxList.GetItemChecked(StartFreenetIndex);
Properties.Settings.Default.UseBrowser = (string)BrowserChoice.SelectedItem;
Properties.Settings.Default.ShowSlowOpenTip = SlowStartOption.Checked;
Properties.Settings.Default.LogLevel = LogLevelChoice.Text;
Properties.Settings.Default.CustomLocation = CustomLocationDisplay.Text;
Properties.Settings.Default.Save();
foreach (var rule in LogManager.Configuration.LoggingRules)
{
ChangeRuleMinLevel(rule, LogLevel.FromString(LogLevelChoice.Text));
}
if (Properties.Settings.Default.StartIcon)
{
// Start Freenet or just the icon.
SetStartupArguments(Properties.Settings.Default.StartFreenet ? "-start" : "");
}
else if (Properties.Settings.Default.StartFreenet)
{
// Just start Freenet.
SetStartupArguments("-start -hide");
}
else
{
// Do not start.
SetStartupArguments(null);
}
if (_initialCustomLocation != CustomLocationDisplay.Text)
{
// TODO: Reload config without restart? What would be the difference?
Application.Restart();
}
Close();
}
private void Cancel_Click(object sender, EventArgs e)
{
Close();
}
// Set the tray arguments on Windows startup, or remove if from startup if arguments is null.
private static void SetStartupArguments(string arguments)
{
using (var currentUser32 = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry32))
using (var key = currentUser32.OpenSubKey(StartupKeyLocation, true))
{
// TODO: Assuming startup registry location exists. Is this viable?
key.DeleteValue(RegistryStartupName, false);
if (arguments != null)
{
/*
* Double quotes are required around the executable path to get multiple(?) command
* line arguments.
*/
key.SetValue(RegistryStartupName, '"' + Application.ExecutablePath + "\" " + arguments);
}
}
}
private static void ChangeRuleMinLevel(LoggingRule rule, LogLevel minLevel)
{
/*
* Based on how the LoggingLevel initializes its logging levels when given a minLevel,
* but because LogLevel.MinLevel and LogLevel.MaxLevel are not publically accessible,
* their current values are hardcoded. TODO: This is fragile!
*/
for (var i = LogLevel.Trace.Ordinal; i < minLevel.Ordinal; i++)
{
rule.DisableLoggingForLevel(LogLevel.FromOrdinal(i));
}
for (var i = minLevel.Ordinal; i <= LogLevel.Fatal.Ordinal; i++)
{
rule.EnableLoggingForLevel(LogLevel.FromOrdinal(i));
}
LogManager.ReconfigExistingLoggers();
}
private void customLocationClear_Click(object sender, EventArgs e)
{
CustomLocationDisplay.Text = String.Empty;
}
private void CustomLocationBrowse_Click(object sender, EventArgs e)
{
try
{
CustomLocationDisplay.Text = SelectCustomLocation(this);
}
catch (OperationCanceledException)
{
/* User does not want to change the directory. */
}
}
private static string SelectCustomLocation(IWin32Window owner)
{
using (var dialog = new FolderBrowserDialog
{
Description = strings.ChooseCustomLocation,
ShowNewFolderButton = false,
})
{
if (dialog.ShowDialog(owner) == DialogResult.Cancel)
{
throw new OperationCanceledException();
}
return dialog.SelectedPath;
}
}
public static void PromptCustomLocation(IWin32Window owner)
{
Properties.Settings.Default.CustomLocation = SelectCustomLocation(owner);
Properties.Settings.Default.Save();
}
}
}