Repository navigation
Expand file tree
/
Copy pathConfigurationHelper.cs
More file actions
237 lines (199 loc) · 10.1 KB
/
Copy pathConfigurationHelper.cs
File metadata and controls
237 lines (199 loc) · 10.1 KB
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
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using Notpod.Properties;
using Notpod.Configuration12;
using log4net;
namespace Notpod
{
/// <summary>
/// Contains methods for managing configuration.
/// </summary>
public class ConfigurationHelper
{
private static ILog l = LogManager.GetLogger(typeof(ConfigurationHelper));
/// <summary>
/// Move configuration from iTA to Notpod
/// </summary>
/// <returns></returns>
public static bool MovePreNotpodConfiguration()
{
MessageBox.Show("It seems that you have recently installed me, or upgraded me from iTunes Agent - my previous name. "
+ "I need to configure myself for this new version.\n\nIf you "
+ "have existing configuration from an earlier version of the application, then I will help "
+ "you copy this. Please click 'OK' to continue.",
"Configuration changes required", MessageBoxButtons.OK,
MessageBoxIcon.Information);
try
{
WriteNewConfiguration();
}
catch (Exception ex)
{
string message = "I failed to write default configuration to "
+ MainForm.DATA_PATH + "\\notpod-config.xml. I can "
+ "not continue without this configuration.";
l.Error(message, ex);
MessageBox.Show(message, "Configuation failure",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
try
{
WriteNewDeviceConfiguration();
}
catch (Exception ex)
{
string message = "I failed to write default device configuration to "
+ MainForm.DATA_PATH + "\\device-config.xml.\n\nI can "
+ "not continue without this configuration.";
l.Error(message, ex);
MessageBox.Show(message, "Configuation failure",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
if (MessageBox.Show("Do you want me to import old device configuration from a previous version of "
+ "Notpod, or iTunes Agent?\n\nIf this is a fresh installation of Notpod, not "
+ "an upgrade, you can safely choose 'No'.", "Upgrade device configuration?",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
FolderBrowserDialog folders = new FolderBrowserDialog();
folders.ShowNewFolderButton = false;
folders.SelectedPath = GetLikelyPreNotpodPath();
folders.Description = "Please choose the installation folder of your pre Notpod installation";
if (folders.ShowDialog() == DialogResult.OK)
{
string appPath = folders.SelectedPath;
// Existing configuration
try
{
XmlSerializer deserializer = new XmlSerializer(typeof(Configuration));
Configuration oldConfig = (Configuration)deserializer.Deserialize(
new XmlTextReader(new StreamReader(appPath + "\\ita-config.xml")));
Configuration newConfig = new Configuration();
newConfig.ShowNotificationPopups = oldConfig.ShowNotificationPopups;
newConfig.UseListFolder = oldConfig.UseListFolder;
XmlSerializer serializer = new XmlSerializer(typeof(Configuration));
XmlTextWriter xtw = new XmlTextWriter(new StreamWriter(MainForm.DATA_PATH
+ "\\notpod-config.xml"));
serializer.Serialize(xtw, newConfig);
xtw.Close();
}
catch (Exception ex)
{
string message = "I was unable to read the old configuration file in " + appPath
+ ", or write existing configuration to the new location " + MainForm.DATA_PATH
+ "\\notpod-config.xml. I will now exit. You may start me again and try another "
+ "time, or simply continue using the new default configration.\n\nError was: "
+ ex.Message;
l.Error(message, ex);
MessageBox.Show(message,
"Failed to locate configuration", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
// Existing device configuration
try
{
XmlSerializer deserializer = new XmlSerializer(typeof(DeviceConfiguration));
DeviceConfiguration oldConfig = (DeviceConfiguration)deserializer.Deserialize(
new XmlTextReader(new StreamReader(appPath + "\\device-config.xml")));
DeviceConfiguration newConfig = (DeviceConfiguration)deserializer.Deserialize(
new XmlTextReader(new StringReader(Resources.device_config)));
//
// foreach (SyncPattern sp in oldConfig.SyncPatterns) {
//
// if(!newConfig.ContainsSyncPattern(sp)) {
// newConfig.AddSyncPattern(sp);
// }
// }
foreach (Device d in oldConfig.Devices)
newConfig.AddDevice(d);
XmlSerializer serializer = new XmlSerializer(typeof(DeviceConfiguration));
XmlTextWriter xtw = new XmlTextWriter(new StreamWriter(MainForm.DATA_PATH
+ "\\device-config.xml"));
serializer.Serialize(xtw, newConfig);
xtw.Close();
}
catch (Exception ex)
{
string message = "I was unable to read the old configuration file in " + appPath
+ ", or write existing configuration to the new location " + MainForm.DATA_PATH
+ "\\notpod-config.xml. I will now exit. You may start me again and try another "
+ "time, or simply continue using the new default configration.\n\nError was: "
+ ex.Message;
l.Error(message, ex);
MessageBox.Show(message,
"Failed to locate configuration", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
}
// Done. Write file indicating that the upgrade was successful.
try
{
FileStream convertDoneFile = File.Create(MainForm.DATA_PATH + "\\.ita-convert");
convertDoneFile.Close();
MessageBox.Show("I have successfully configured myself and we're ready to go. Enjoy!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
l.Error(ex);
MessageBox.Show("I failed to move configuration for the new version. Please try restarting the application.", "Fatal", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false ;
}
return true;
}
static string GetLikelyPreNotpodPath()
{
string dataPath = ConfigurationHelper.GetAppDataPath();
l.Info("Determining likely pre Notpod data path from \"" + dataPath + "\".");
int corpIndex = dataPath.IndexOf("Jaran Nilsen");
if(corpIndex == -1) {
return dataPath;
}
string pathBeforeCorp = dataPath.Substring(0, corpIndex);
pathBeforeCorp += "Jaran Nilsen\\iTunes Agent";
return pathBeforeCorp;
}
/// <summary>
/// Write new device configuration. Since 1.2.
/// </summary>
protected static void WriteNewDeviceConfiguration()
{
StreamWriter fw = new StreamWriter(MainForm.DATA_PATH + "\\device-config.xml");
fw.Write(Resources.device_config);
fw.Flush();
fw.Close();
}
/// <summary>
/// Write new default configuration. Since 1.2.
/// </summary>
///
protected static void WriteNewConfiguration()
{
StreamWriter fw = new StreamWriter(MainForm.DATA_PATH + "\\notpod-config.xml");
fw.Write(Resources.ita_config);
fw.Flush();
fw.Close();
}
/// <summary>
/// Get the data path to use by the application. The path returned by the runtime
/// also contains the version number, but the path should be used without any version
/// number.
/// </summary>
/// <returns></returns>
public static String GetAppDataPath()
{
#if DEBUG
string original = Application.StartupPath + "\\";
#else
string original = Application.UserAppDataPath;
#endif
return original.Substring(0, original.LastIndexOf("\\"));
}
}
}