forked from canneverbe/Ketarin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SettingsExporter.cs
182 lines (157 loc) · 7.2 KB
/
SettingsExporter.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
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
namespace Ketarin
{
/// <summary>
/// Exports and imports Ketarin settings to files.
/// </summary>
internal static class SettingsExporter
{
public static void ExportToFile(string filename)
{
XmlWriterSettings settings = new XmlWriterSettings {Indent = true};
XmlWriter writer = XmlWriter.Create(filename, settings);
writer.WriteStartElement("Ketarin");
writer.WriteStartElement("Settings");
// Export normal settings
XmlSerializer serializer = new XmlSerializer(typeof(SerializableDictionary<string, string>));
serializer.Serialize(writer, DbManager.GetSettings());
writer.WriteEndElement();
// Export global variables (stored in "variables" table)
writer.WriteStartElement("GlobalVariables");
foreach (UrlVariable var in UrlVariable.GlobalVariables.Values)
{
writer.WriteStartElement("Variable");
writer.WriteAttributeString("Name", var.Name);
writer.WriteAttributeString("Content", var.CachedContent);
writer.WriteEndElement();
}
writer.WriteEndElement();
// Export code snippets (stored in "snippets" table)
writer.WriteStartElement("CodeSnippets");
foreach (Snippet snippet in DbManager.GetSnippets())
{
writer.WriteStartElement("Snippet");
writer.WriteAttributeString("Guid", DbManager.FormatGuid(snippet.Guid));
writer.WriteAttributeString("Name", snippet.Name);
writer.WriteAttributeString("Type", ((int)snippet.Type).ToString());
writer.WriteString(snippet.Text);
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.WriteStartElement("SetupLists");
// Save setup lists
foreach (ApplicationList list in DbManager.GetSetupLists(DbManager.GetJobs()))
{
if (!list.IsPredefined)
{
writer.WriteStartElement("List");
writer.WriteAttributeString("Name", list.Name);
writer.WriteAttributeString("Guid", DbManager.FormatGuid(list.Guid));
writer.WriteStartElement("Applications");
foreach (ApplicationJob app in list.Applications)
{
writer.WriteStartElement("Application");
writer.WriteAttributeString("Guid", DbManager.FormatGuid(app.Guid));
writer.WriteAttributeString("Name", app.Name);
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.WriteEndElement();
}
}
writer.WriteEndElement();
writer.Close();
}
public static void ImportFromFile(string filename)
{
XmlDocument doc = new XmlDocument();
doc.Load(filename);
// Import settings from file as dictionary
XmlElement settingsElem = doc.SelectSingleNode("//Settings/dictionary") as XmlElement ??
doc.SelectSingleNode("//dictionary") as XmlElement;
if (settingsElem != null)
{
XmlSerializer serializer = new XmlSerializer(typeof(SerializableDictionary<string, string>));
using (StringReader textReader = new StringReader(settingsElem.OuterXml))
{
DbManager.SetSettings(serializer.Deserialize(textReader) as Dictionary<string, string>);
}
}
// Import global variables
XmlElement varNodes = doc.SelectSingleNode("//GlobalVariables") as XmlElement;
if (varNodes != null)
{
UrlVariable.GlobalVariables.Clear();
foreach (XmlElement varElem in doc.SelectNodes("//GlobalVariables/Variable"))
{
UrlVariable newVar = new UrlVariable
{
Name = varElem.GetAttribute("Name"),
CachedContent = varElem.GetAttribute("Content")
};
UrlVariable.GlobalVariables[newVar.Name] = newVar;
}
UrlVariable.GlobalVariables.Save();
}
// Import code snippets
XmlElement snippetNodes = doc.SelectSingleNode("//CodeSnippets") as XmlElement;
if (snippetNodes != null)
{
using (SQLiteCommand comm = DbManager.Connection.CreateCommand())
{
comm.CommandText = "DELETE FROM snippets";
comm.ExecuteNonQuery();
}
foreach (XmlElement snippetElem in doc.SelectNodes("//CodeSnippets/Snippet"))
{
Snippet snippet = new Snippet
{
Guid = new Guid(snippetElem.GetAttribute("Guid")),
Name = snippetElem.GetAttribute("Name"),
Type = (ScriptType) Convert.ToInt32(snippetElem.GetAttribute("Type")),
Text = snippetElem.InnerText
};
snippet.Save();
}
}
XmlElement setupNodes = doc.SelectSingleNode("//SetupLists") as XmlElement;
if (setupNodes != null)
{
using (IDbCommand command = DbManager.Connection.CreateCommand())
{
command.CommandText = @"DELETE FROM setuplists_applications";
command.ExecuteNonQuery();
}
using (IDbCommand command = DbManager.Connection.CreateCommand())
{
command.CommandText = @"DELETE FROM setuplists";
command.ExecuteNonQuery();
}
foreach (XmlElement listElem in doc.SelectNodes("//SetupLists/List"))
{
ApplicationList list = new ApplicationList
{
Name = listElem.GetAttribute("Name"),
Guid = new Guid(listElem.GetAttribute("Guid"))
};
foreach (XmlElement appListElem in listElem.SelectNodes("Applications/Application"))
{
Guid guid = new Guid(appListElem.GetAttribute("Guid"));
ApplicationJob job = DbManager.GetJob(guid);
if (job != null)
{
list.Applications.Add(job);
}
}
list.Save();
}
}
}
}
}