-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
300 lines (258 loc) · 12 KB
/
Form1.cs
File metadata and controls
300 lines (258 loc) · 12 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
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
using System;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using HtmlAgilityPack;
using Windows.ApplicationModel;
using Windows.Management.Deployment;
using xtrance.Properties;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace xtrance
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
/*string configPath = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath;
if (Properties.Settings.Default.UpdateSettings)
{
Properties.Settings.Default.Upgrade();
Properties.Settings.Default.Reload();
Properties.Settings.Default.UpdateSettings = false;
Properties.Settings.Default.Save();
}*/
Text = Text + " - " + Application.ProductVersion;
textBoxUser.Text = Properties.Settings.Default.username;
textBoxUser.TextChanged += TextBox_TextChanged;
textBoxPassword.Text = Properties.Settings.Default.password;
textBoxPassword.TextChanged += TextBox_TextChanged;
textBoxServer.Text = Properties.Settings.Default.serverid;
textBoxServer.TextChanged += TextBox_TextChanged;
textBoxFrom.Text = Properties.Settings.Default.from;
textBoxFrom.TextChanged += TextBox_TextChanged;
textBoxTo.Text = Properties.Settings.Default.to;
textBoxTo.TextChanged += TextBox_TextChanged;
textBoxDirectory.Text = Properties.Settings.Default.FilesPath;
textBoxDirectory.TextChanged += TextBox_TextChanged;
buttonOK.Enabled = true; buttonCancel.Enabled = false;
new Thread(() =>
{
try
{
PackageManager packageManager = new PackageManager();
Package currentPackage = packageManager.FindPackageForUser(string.Empty, Package.Current.Id.FullName);
while (true)
{
labelUpdate.Invoke((Action)(() => labelUpdate.Text = "Checking status..."));
PackageUpdateAvailabilityResult status = currentPackage.CheckUpdateAvailabilityAsync().GetAwaiter().GetResult();
if (status.Availability == PackageUpdateAvailability.Unknown)
{
Thread.Sleep(TimeSpan.FromSeconds(5));
}
else
if (status.Availability == PackageUpdateAvailability.Error)
{
Thread.Sleep(TimeSpan.FromSeconds(1));
}
else
if (status.Availability == PackageUpdateAvailability.NoUpdates)
{
labelUpdate.Invoke((Action)(() => labelUpdate.Text = status.Availability.ToString()));
Thread.Sleep(TimeSpan.FromMinutes(5));
} else
if (status.Availability == PackageUpdateAvailability.Required || status.Availability == PackageUpdateAvailability.Available)
{
labelUpdate.Invoke((Action)(() => labelUpdate.Text = status.Availability.ToString()));
buttonUpdate.Invoke((Action)(() => buttonUpdate.Visible = true));
break;
}
}
}
catch (Exception ex)
{
labelUpdate.Invoke((Action)(() => labelUpdate.Text = $"error {ex.Message}"));
}
})
{ IsBackground = true }.Start();
}
private string RootPath => @"\\?\"+textBoxDirectory.Text+ @"\";
private void TextBox_TextChanged(object sender, EventArgs e)
{
Properties.Settings.Default.username = textBoxUser.Text;
Properties.Settings.Default.password = textBoxPassword.Text;
Properties.Settings.Default.serverid = textBoxServer.Text;
Properties.Settings.Default.from = textBoxFrom.Text;
Properties.Settings.Default.to = textBoxTo.Text;
Properties.Settings.Default.FilesPath = textBoxDirectory.Text;
Properties.Settings.Default.Save();
}
private void Log(string str)
{
textBoxLog.Invoke((Action)(() => textBoxLog.AppendText(str + "\r\n")));
}
private Task _task = null;
CancellationTokenSource _cancellationToken = new CancellationTokenSource();
private void buttonOK_Click(object sender, EventArgs e)
{
TimeSpan sleeptime = new TimeSpan(0, 0, 0, Int32.Parse(textBoxSleep.Text));
string user = textBoxUser.Text;
string pass = textBoxPassword.Text;
int serverId = Int32.Parse(textBoxServer.Text);
buttonOK.Enabled = false; buttonCancel.Enabled = true;
_task = Task.Run(async () =>
{
try
{
XtranceAccessor xtrance = new XtranceAccessor(textBoxURL.Text, Log, serverId, _cancellationToken.Token);
await xtrance.LogingAndGetCookie(user, pass);
MetaBooks metaBooks = new MetaBooks();
Books books = await xtrance.GetLatestBooks(Int32.Parse(textBoxFrom.Text), Int32.Parse(textBoxTo.Text));
await Parallel.ForEachAsync(books.Values, new ParallelOptions()
{
MaxDegreeOfParallelism = 1
}, async (book, ct) =>
{
_cancellationToken.Token.ThrowIfCancellationRequested();
//Thread.Sleep(sleeptime);
await xtrance.FillBookInfo(metaBooks, book);
});
/*await Parallel.ForEachAsync(books.Values, async (book, ct) => {
_cancellationToken.Token.ThrowIfCancellationRequested();
//Thread.Sleep(sleeptime);
await xtrance.FillBookInfo(metaBooks, book);
});*/
Log("Collected " + metaBooks.Count + " metabooks...");
await Parallel.ForEachAsync(metaBooks.Values, new ParallelOptions()
{
MaxDegreeOfParallelism = 5
}, async (metaBook, ct) =>
{
_cancellationToken.Token.ThrowIfCancellationRequested();
//Thread.Sleep(sleeptime);
await xtrance.FillBookMeta(metaBook);
});
/*foreach (MetaBook metaBook in metaBooks.Values)
{
_cancellationToken.Token.ThrowIfCancellationRequested();
Thread.Sleep(sleeptime);
await xtrance.FillBookMeta(metaBook);
}*/
foreach (MetaBook metaBook in metaBooks.Values)
{
SaveMetaBook(metaBook);
}
foreach (Book book in books.Values)
{
SaveBook(book);
}
}
catch (Exception ex)
{
Log(ex.Message);
}
}, _cancellationToken.Token).ContinueWith(_ => {
Log("done");
buttonOK.Invoke((Action)(() => buttonOK.Enabled = true));
buttonCancel.Invoke((Action)(() => buttonCancel.Enabled = false));
});
}
private void SaveBook(Book book)
{
Log("Saving book : " + book.ToString());
string path = Sanitize(book.Parent.Author) + @"\" + Sanitize(book.Parent.Name);
Directory.CreateDirectory(RootPath + path);
Stream stream = File.Create(RootPath + path + @"\" + Sanitize(book.Revision) + " - " + Sanitize(book.Format) + ".url");
StreamWriter sw = new StreamWriter(stream);
sw.WriteLine("[DEFAULT]");
sw.WriteLine("[InternetShortcut]");
foreach (string downloadLink in book.DownloadLinks)
{
sw.WriteLine("URL=" + downloadLink);
}
sw.Close();
stream.Close();
}
private void SaveMetaBook(MetaBook metaBook)
{
Log("Saving metabook : " + metaBook.ToString());
string path = Sanitize(metaBook.Author) + @"\" + Sanitize(metaBook.Name);
Directory.CreateDirectory(RootPath + path);
if (metaBook.Data != null)
{
Stream stream = File.Create(RootPath + path + @"\" + "metadata.zip");
stream.Write(metaBook.Data, 0, metaBook.Data.Length);
stream.Close();
}
}
private void buttonCancel_Click(object sender, EventArgs e)
{
_cancellationToken.Cancel();
}
private void labelFrom_Click(object sender, EventArgs e)
{
}
private string Sanitize(string input)
{
if (string.IsNullOrEmpty(input))
{
input = "No_Author";
}
string s = String.Join("_", input.Split(Path.GetInvalidFileNameChars())).TrimEnd('.').Trim();
if (s.Length > 250)
{
s = s.Substring(0, 250);
}
return s;
}
private void buttonUpdate_Click(object sender, EventArgs e)
{
buttonOK.Enabled = false;
buttonCancel.Enabled = false;
labelUpdate.Text = $"Updating... Application will automatically restart to the new version.";
new Thread(() =>
{
try
{
RelaunchHelper.RegisterApplicationRestart(null, RelaunchHelper.RestartFlags.NONE);
PackageManager pm = new PackageManager();
Package currentPackage = pm.FindPackageForUser(string.Empty, Package.Current.Id.FullName);
var installTask = pm.AddPackageByAppInstallerFileAsync(
currentPackage.GetAppInstallerInfo().Uri,
AddPackageByAppInstallerOptions.ForceTargetAppShutdown,
pm.GetDefaultPackageVolume());
installTask.Progress = (installResult, progress) => labelUpdate.BeginInvoke(() =>
{
labelUpdate.Invoke((Action)(() => labelUpdate.Text = $"Progress: {progress.percentage} {progress.state}"));
});
var res = installTask.GetAwaiter().GetResult();
if (res.IsRegistered == true)
{
labelUpdate.Invoke((Action)(() => labelUpdate.Text = "Status ok. Application should automatically restart..."));
}
else
{
labelUpdate.Invoke((Action)(() => labelUpdate.Text = $"Error {res.ErrorText}"));
}
}
catch (Exception ex)
{
labelUpdate.Invoke((Action)(() => labelUpdate.Text = $"Error {ex.Message}"));
}
})
{ IsBackground = true }.Start();
}
private void buttonDirectory_Click(object sender, EventArgs e)
{
FolderBrowserDialog dlg = new FolderBrowserDialog();
dlg.InitialDirectory = textBoxDirectory.Text;
if (dlg.ShowDialog() == DialogResult.OK)
{
textBoxDirectory.Text = dlg.SelectedPath;
}
}
}
}