forked from easimon/Caffeinated
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SettingsForm.cs
72 lines (64 loc) · 2.48 KB
/
SettingsForm.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
using System;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using Caffeinated.Properties;
namespace Caffeinated {
public partial class SettingsForm : BaseForm {
BindingList<Duration> Durations;
public SettingsForm() : base() {
InitializeComponent();
var durations = from i in Settings.Default.RealDurations
select new Duration { Minutes = i };
this.Durations = new BindingList<Duration>(durations.ToList());
var defaultItem = this.Durations.Where(
d => d.Minutes == Settings.Default.DefaultDuration
).First();
DefaultDurationBox.DataSource = this.Durations;
DefaultDurationBox.DisplayMember = "Description";
DefaultDurationBox.ValueMember = "Minutes";
DefaultDurationBox.SelectedItem = defaultItem;
var shortcut = GetShortcutPath();
StartupChkBox.Checked = File.Exists(shortcut);
}
private void okBtn_Click(object sender, EventArgs e) {
Settings.Default.Save();
this.Close();
}
private void cancelBtn_Click(object sender, EventArgs e) {
this.Close();
}
private void DefaultDurationBox_SelectedIndexChanged(
object sender,
EventArgs e
) {
var item = DefaultDurationBox.SelectedItem as Duration;
if (item != null) {
Settings.Default.DefaultDuration = item.Minutes;
}
}
static string GetShortcutPath(ReflectedShell shell = null) {
if (shell == null) {
shell = new ReflectedShell();
}
var startup = shell.GetSpecialFolder("Startup");
return Path.Combine(startup, "Caffeinated.lnk");
}
private void StartupChkBox_CheckedChanged(object sender, EventArgs e) {
var shell = new ReflectedShell();
var shortcut = GetShortcutPath(shell);
var executable = Assembly.GetExecutingAssembly()
.GetName().CodeBase;
if (StartupChkBox.Checked) {
// create shortcut in startup items folder in start menu
shell.CreateShortcut(shortcut, executable);
}
else {
// remove shortcut if it exists
File.Delete(shortcut);
}
}
}
}