-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConfigurationHelper.cs
208 lines (175 loc) · 8.97 KB
/
ConfigurationHelper.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
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 a previous version. "
+ "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 "
+ "ICherry Music Manager?\n\nIf this is a fresh installation of ICherry Music Manager, 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.Description = "Please choose the installation folder of your pre ICherry Music Manager 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 = new DeviceConfiguration();
foreach (SyncPattern sp in oldConfig.SyncPatterns)
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
{
File.Create(MainForm.DATA_PATH + "\\.ita-convert");
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;
}
/// <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()
{
string original = Application.UserAppDataPath;
return original.Substring(0, original.LastIndexOf("\\"));
}
}
}