-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathPreferencesPage.cs
167 lines (138 loc) · 5.79 KB
/
PreferencesPage.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Outlook = Microsoft.Office.Interop.Outlook;
using System.Text.RegularExpressions;
// Copyright (c) 2016-2017, 2019 Ben Spiller.
namespace BetterReminders
{
[ComVisible(true)]
public partial class PreferencesPage : UserControl, Outlook.PropertyPage
{
private Logger logger = Logger.GetLogger();
private Outlook.PropertyPageSite propertyPageSite;
public PreferencesPage()
{
InitializeComponent();
}
bool isDirty = false;
void Outlook.PropertyPage.Apply()
{
string meetingregex = meetingUrlRegex.Text;
// normalize use of default regex to improve upgradeability
if (meetingregex == UpcomingMeeting.DefaultMeetingUrlRegex)
meetingregex = "";
if (meetingregex != "")
try
{
Regex re = new Regex(meetingregex);
if (!re.GetGroupNames().Contains("url"))
throw new Exception("The meeting regex must include a regex group named 'url' e.g. '"+UpcomingMeeting.DefaultMeetingUrlRegex+"'");
} catch (Exception e)
{
string msg = "Invalid meeting URL regex: "+e.Message;
MessageBox.Show(msg, "Invalid Meeting URL Regex", MessageBoxButtons.OK, MessageBoxIcon.Error);
throw new Exception(msg, e); // stops isDirty being changed
}
string subjectexcluderegex = subjectExcludeRegex.Text;
if (subjectexcluderegex != "")
try
{
Regex re = new Regex(subjectexcluderegex);
}
catch (Exception e)
{
string msg = "Invalid subject exclude regex: " + e.Message;
MessageBox.Show(msg, "Invalid Subject Exclude Regex", MessageBoxButtons.OK, MessageBoxIcon.Error);
throw new Exception(msg, e); // stops isDirty being changed
}
// first, validation
string reminderSound = (reminderSoundPath.Text == "(none)") ? "" : reminderSoundPath.Text;
if (reminderSound != "" && reminderSound != "(default)" &&
!System.IO.File.Exists(reminderSound))
{
MessageBox.Show("Reminder .wav path does not exist. Provide a valid .wav path, empty string or (default).", "Invalid BetterReminders settings", MessageBoxButtons.OK, MessageBoxIcon.Error);
throw new Exception("BetterReminders got invalid input"); // stops isDirty being changed
}
Properties.Settings.Default.defaultReminderSecs = Decimal.ToInt32(defaultReminderTimeSecs.Value);
Properties.Settings.Default.searchFrequencySecs = Decimal.ToInt32(searchFrequencyMins.Value)*60;
Properties.Settings.Default.playSoundOnReminder = reminderSound;
Properties.Settings.Default.meetingUrlRegex = meetingregex;
Properties.Settings.Default.subjectExcludeRegex = subjectexcluderegex;
Properties.Settings.Default.Save();
isDirty = false;
}
bool Outlook.PropertyPage.Dirty
{
get
{
return isDirty;
}
}
void Outlook.PropertyPage.GetPageInfo(ref string helpFile, ref int helpContext)
{
// nothing to do here
}
private void valueChanged(object sender, EventArgs e)
{
if (propertyPageSite == null) return; // still loading
if (isDirty) return; //already called
isDirty = true;
propertyPageSite.OnStatusChange();
}
Outlook.PropertyPageSite GetPropertyPageSite()
{
// this is what MS's documentation recommends, but doesn't seem to work as Parent is null
if (Parent is Outlook.PropertyPageSite) return (Outlook.PropertyPageSite)Parent;
// nb: I can't believe this hack is really required, but since Parent=null
// I can't find any better way to do it
Type type = typeof(System.Object);
string assembly = type.Assembly.CodeBase.Replace("mscorlib.dll", "System.Windows.Forms.dll");
assembly = assembly.Replace("file:///", "");
string assemblyName = System.Reflection.AssemblyName.GetAssemblyName(assembly).FullName;
Type unsafeNativeMethods = Type.GetType(System.Reflection.Assembly.CreateQualifiedName(assemblyName, "System.Windows.Forms.UnsafeNativeMethods"));
Type oleObj = unsafeNativeMethods.GetNestedType("IOleObject");
System.Reflection.MethodInfo methodInfo = oleObj.GetMethod("GetClientSite");
object propertyPageSite = methodInfo.Invoke(this, null);
return (Outlook.PropertyPageSite)propertyPageSite;
}
private void PreferencesPage_Load(object sender, EventArgs e)
{
try
{
defaultReminderTimeSecs.Value = Properties.Settings.Default.defaultReminderSecs;
searchFrequencyMins.Value = Math.Max(1, Math.Min(Properties.Settings.Default.searchFrequencySecs/60, searchFrequencyMins.Maximum));
reminderSoundPath.Text = Properties.Settings.Default.playSoundOnReminder;
// provide default in case user forgets
meetingUrlRegex.Items.Add(UpcomingMeeting.DefaultMeetingUrlRegex);
meetingUrlRegex.Text = Properties.Settings.Default.meetingUrlRegex;
if (string.IsNullOrWhiteSpace(meetingUrlRegex.Text))
meetingUrlRegex.Text = UpcomingMeeting.DefaultMeetingUrlRegex;
subjectExcludeRegex.Text = Properties.Settings.Default.subjectExcludeRegex;
propertyPageSite = GetPropertyPageSite();
logger.Info("Successfully loaded preferences page");
}
catch (Exception ex)
{
logger.Error("Error loading preferences page: ", ex);
throw;
}
}
private void reminderSoundBrowse_Click(object sender, EventArgs e)
{
if (reminderSoundPath.Text.StartsWith("("))
reminderSoundBrowseDialog.FileName = "";
else
reminderSoundBrowseDialog.FileName = reminderSoundPath.Text;
if (reminderSoundBrowseDialog.ShowDialog(ParentForm) == DialogResult.OK)
reminderSoundPath.Text = reminderSoundBrowseDialog.FileName;
}
}
}