-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathXmlPreferencesClient.cs
263 lines (220 loc) · 5.37 KB
/
XmlPreferencesClient.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
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
using System;
using System.IO;
using System.Collections.Generic;
using System.Xml;
namespace Tomboy
{
public class XmlPreferencesClient : IPreferencesClient
{
#region Private Members
private string fileName;
private XmlDocument prefsDoc;
private Dictionary<string, NotifyEventHandler> events;
#endregion
#region Constructor
public XmlPreferencesClient ()
{
string confDir = Services.NativeApplication.ConfigurationDirectory;
fileName = Path.Combine (confDir, "prefs.xml");
prefsDoc = new XmlDocument ();
// Migration from old location
// NOTE: Assumes this class is instantiated before
// NoteManager performs its migration
if (!Directory.Exists (Services.NativeApplication.DataDirectory) &&
!File.Exists (fileName) &&
File.Exists (Path.Combine (Services.NativeApplication.PreOneDotZeroNoteDirectory, "prefs.xml"))) {
if (!Directory.Exists (confDir))
Directory.CreateDirectory (confDir);
File.Copy (Path.Combine (Services.NativeApplication.PreOneDotZeroNoteDirectory, "prefs.xml"),
fileName);
}
if (File.Exists (fileName))
prefsDoc.Load (fileName);
events = new Dictionary<string, NotifyEventHandler> ();
}
#endregion
#region IPreferencesClient implementation
public void Set (string key, object value)
{
try {
CreatePath (key);
prefsDoc.SelectSingleNode (key).InnerText = System.Convert.ToString(value);
prefsDoc.Save(fileName);
foreach (string nkey in events.Keys) {
NotifyEventHandler handler = events [nkey] as NotifyEventHandler;
if (handler != null && key.StartsWith (nkey))
{
NotifyEventArgs args = new NotifyEventArgs (key, value);
handler (this, args);
}
}
}
catch {}
}
public object Get (string key)
{
try {
XmlElement element = prefsDoc.SelectSingleNode(key) as XmlElement;
if (element != null) {
int intVal;
double doubleVal;
bool boolVal;
string innerText = element.InnerText;
if (bool.TryParse (innerText, out boolVal))
return boolVal;
else if (int.TryParse (innerText, out intVal))
return intVal;
else if (double.TryParse (innerText, out doubleVal))
return doubleVal;
else
return innerText;
}
// TODO: Ugly, fix
throw new System.Exception();
} catch {
throw new NoSuchKeyException (key);
}
}
public void AddNotify (string dir, NotifyEventHandler notify)
{
lock (events) {
if (!events.ContainsKey (dir))
events [dir] = notify;
else
events [dir] += notify;
}
}
public void RemoveNotify (string dir, NotifyEventHandler notify)
{
lock (events) {
if (events.ContainsKey (dir))
events [dir] -= notify;
}
}
public void SuggestSync ()
{
// TODO: Sync with file?
}
#endregion
#region Private Methods
private void CreatePath(string path)
{
if (path.Length == 0)
return;
if (path [0] == '/')
path = path.Substring (1);
if (path.Length == 0)
return;
string [] parts = path.Split ('/');
XmlNode node = prefsDoc;
for (int i = 0; i < parts.Length; ++i)
{
if (node [parts [i]] == null)
{
node.AppendChild (prefsDoc.CreateElement (parts [i]));
}
node = node [parts [i]];
}
}
#endregion
}
public class PropertyEditor : IPropertyEditor
{
private string key;
public string Key
{
get { return key; }
}
public PropertyEditor (string key)
{
this.key = key;
}
public virtual void Setup ()
{
}
protected object Get ()
{
return Services.Preferences.Get (Key);
}
protected void Set (object value)
{
Services.Preferences.Set (Key, value);
}
}
public class PropertyEditorBool : PropertyEditor, IPropertyEditorBool
{
protected List<Gtk.Widget> guards = new List<Gtk.Widget> ();
public PropertyEditorBool (string key) : base (key) { }
public virtual void AddGuard (Gtk.Widget widget)
{
guards.Add (widget);
}
protected void Set (bool value)
{
base.Set (value);
UpdateGuards (value);
}
protected void UpdateGuards ()
{
bool val = false;
try {
val = Get ();
} catch (NoSuchKeyException) { }
UpdateGuards (val);
}
private void UpdateGuards (bool value)
{
foreach (Gtk.Widget widget in guards)
widget.Sensitive = value;
}
protected new bool Get ()
{
return (bool) base.Get ();
}
}
public class PropertyEditorToggleButton : PropertyEditorBool
{
Gtk.CheckButton button;
public PropertyEditorToggleButton (string key, Gtk.CheckButton sourceButton) :
base (key)
{
button = sourceButton;
}
public override void Setup ()
{
bool active = false;
try {
active = Get ();
} catch (NoSuchKeyException) { }
button.Active = active;
button.Clicked += new System.EventHandler (OnChanged);
UpdateGuards ();
}
private void OnChanged (object sender, System.EventArgs args)
{
Set (button.Active);
}
}
public class PropertyEditorEntry : PropertyEditor
{
Gtk.Entry entry;
public PropertyEditorEntry (string key, Gtk.Entry sourceEntry) :
base (key)
{
entry = sourceEntry;
}
public override void Setup ()
{
string val = string.Empty;
try {
val = System.Convert.ToString (base.Get ());
} catch (NoSuchKeyException) { }
entry.Text = val;
entry.Changed += OnChanged;
}
private void OnChanged (object sender, System.EventArgs args)
{
Set (entry.Text);
}
}
}