-
-
Notifications
You must be signed in to change notification settings - Fork 577
/
SparkleConfig.cs
executable file
·351 lines (257 loc) · 10.6 KB
/
SparkleConfig.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// SparkleShare, a collaboration and sharing tool.
// Copyright (C) 2010 Hylke Bons <hylkebons@gmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.IO;
using System.Collections.Generic;
using System.Xml;
namespace SparkleLib {
public class SparkleConfig : XmlDocument {
public static SparkleConfig DefaultConfig;
public static bool DebugMode = true;
public string FullPath;
public string TmpPath;
public string LogFilePath;
public string HomePath {
get {
if (SparkleBackend.Platform == PlatformID.Win32NT)
return Environment.GetFolderPath (Environment.SpecialFolder.UserProfile);
else
return Environment.GetFolderPath (Environment.SpecialFolder.Personal);
}
}
public string FoldersPath {
get {
if (GetConfigOption ("folders_path") != null)
return GetConfigOption ("folders_path");
else
return Path.Combine (HomePath, "SparkleShare");
}
}
public SparkleConfig (string config_path, string config_file_name)
{
FullPath = Path.Combine (config_path, config_file_name);
LogFilePath = Path.Combine (config_path, "debug_log.txt");
if (File.Exists (LogFilePath)) {
try {
File.Delete (LogFilePath);
} catch (Exception) {
// Don't delete the debug.log if, for example, 'tail' is reading it
}
}
if (!Directory.Exists (config_path))
Directory.CreateDirectory (config_path);
try {
Load (FullPath);
} catch (TypeInitializationException) {
CreateInitialConfig ();
} catch (FileNotFoundException) {
CreateInitialConfig ();
} catch (XmlException) {
FileInfo file = new FileInfo (FullPath);
if (file.Length == 0) {
File.Delete (FullPath);
CreateInitialConfig ();
} else {
throw new XmlException (FullPath + " does not contain a valid config XML structure.");
}
} finally {
Load (FullPath);
TmpPath = Path.Combine (FoldersPath, ".tmp");
Directory.CreateDirectory (TmpPath);
}
}
private void CreateInitialConfig ()
{
string user_name = "Unknown";
if (SparkleBackend.Platform == PlatformID.Unix ||
SparkleBackend.Platform == PlatformID.MacOSX) {
user_name = Environment.UserName;
if (string.IsNullOrEmpty (user_name))
user_name = "";
else
user_name = user_name.TrimEnd (",".ToCharArray ());
} else {
user_name = Environment.UserName;
}
if (string.IsNullOrEmpty (user_name))
user_name = "Unknown";
string n = Environment.NewLine;
File.WriteAllText (FullPath,
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" + n +
"<sparkleshare>" + n +
" <user>" + n +
" <name>" + user_name + "</name>" + n +
" <email>Unknown</email>" + n +
" </user>" + n +
"</sparkleshare>");
}
public SparkleUser User {
get {
XmlNode name_node = SelectSingleNode ("/sparkleshare/user/name/text()");
XmlNode email_node = SelectSingleNode ("/sparkleshare/user/email/text()");
string user_name = name_node.Value;
string user_email = email_node.Value;
SparkleUser user = new SparkleUser (user_name, user_email);
string [] pubkey_file_paths = Directory.GetFiles (Path.GetDirectoryName (FullPath), "*.pub");
if (pubkey_file_paths.Length > 0)
user.PublicKey = File.ReadAllText (pubkey_file_paths [0]);
return user;
}
set {
SparkleUser user = (SparkleUser) value;
XmlNode name_node = SelectSingleNode ("/sparkleshare/user/name/text()");
XmlNode email_node = SelectSingleNode ("/sparkleshare/user/email/text()");
name_node.InnerText = user.Name;
email_node.InnerText = user.Email;
Save ();
}
}
public List<string> Folders {
get {
List<string> folders = new List<string> ();
foreach (XmlNode node_folder in SelectNodes ("/sparkleshare/folder"))
folders.Add (node_folder ["name"].InnerText);
folders.Sort ();
return folders;
}
}
public void AddFolder (string name, string identifier, string url, string backend)
{
XmlNode node_name = CreateElement ("name");
XmlNode node_identifier = CreateElement ("identifier");
XmlNode node_url = CreateElement ("url");
XmlNode node_backend = CreateElement ("backend");
node_name.InnerText = name;
node_identifier.InnerText = identifier;
node_url.InnerText = url;
node_backend.InnerText = backend;
XmlNode node_folder = CreateNode (XmlNodeType.Element, "folder", null);
node_folder.AppendChild (node_name);
node_folder.AppendChild (node_identifier);
node_folder.AppendChild (node_url);
node_folder.AppendChild (node_backend);
XmlNode node_root = SelectSingleNode ("/sparkleshare");
node_root.AppendChild (node_folder);
Save ();
}
public void RemoveFolder (string name)
{
foreach (XmlNode node_folder in SelectNodes ("/sparkleshare/folder")) {
if (node_folder ["name"].InnerText.Equals (name))
SelectSingleNode ("/sparkleshare").RemoveChild (node_folder);
}
Save ();
}
public void RenameFolder (string identifier, string name)
{
XmlNode node_folder = SelectSingleNode (
string.Format ("/sparkleshare/folder[identifier=\"{0}\"]", identifier));
node_folder ["name"].InnerText = name;
Save ();
}
public string GetBackendForFolder (string name)
{
return GetFolderValue (name, "backend");
}
public string GetIdentifierForFolder (string name)
{
return GetFolderValue (name, "identifier");
}
public string GetUrlForFolder (string name)
{
return GetFolderValue (name, "url");
}
public bool IdentifierExists (string identifier)
{
if (identifier == null)
throw new ArgumentNullException ();
foreach (XmlNode node_folder in SelectNodes ("/sparkleshare/folder")) {
XmlElement folder_id = node_folder ["identifier"];
if (folder_id != null && identifier.Equals (folder_id.InnerText))
return true;
}
return false;
}
public bool SetFolderOptionalAttribute (string folder_name, string key, string value)
{
XmlNode folder = GetFolder (folder_name);
if (folder == null)
return false;
if (folder [key] != null) {
folder [key].InnerText = value;
} else {
XmlNode new_node = CreateElement (key);
new_node.InnerText = value;
folder.AppendChild (new_node);
}
Save ();
return true;
}
public string GetFolderOptionalAttribute (string folder_name, string key)
{
XmlNode folder = GetFolder (folder_name);
if (folder != null) {
if (folder [key] != null)
return folder [key].InnerText;
else
return null;
} else {
return null;
}
}
public string GetConfigOption (string name)
{
XmlNode node = SelectSingleNode ("/sparkleshare/" + name);
if (node != null)
return node.InnerText;
else
return null;
}
public void SetConfigOption (string name, string content)
{
XmlNode node = SelectSingleNode ("/sparkleshare/" + name);
if (node != null) {
node.InnerText = content;
} else {
node = CreateElement (name);
node.InnerText = content;
XmlNode node_root = SelectSingleNode ("/sparkleshare");
node_root.AppendChild (node);
}
Save ();
SparkleLogger.LogInfo ("Config", "Updated option " + name + ":" + content);
}
private XmlNode GetFolder (string name)
{
return SelectSingleNode (string.Format ("/sparkleshare/folder[name=\"{0}\"]", name));
}
private string GetFolderValue (string name, string key)
{
XmlNode folder = GetFolder(name);
if ((folder != null) && (folder [key] != null))
return folder [key].InnerText;
else
return null;
}
private void Save ()
{
if (!File.Exists (FullPath))
throw new FileNotFoundException (FullPath + " does not exist");
Save (FullPath);
SparkleLogger.LogInfo ("Config", "Wrote to '" + FullPath + "'");
}
}
}