-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrmMain.cs
187 lines (164 loc) · 5.76 KB
/
frmMain.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
using System;
using System.Threading;
using System.Windows.Forms;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Net;
using System.Xml.Serialization;
using System.IO;
using HtmlAgilityPack;
using System.Drawing;
using HtmlDocument = HtmlAgilityPack.HtmlDocument;
namespace _4chget
{
public partial class frmMain : Form
{
Downloader worker;
Settings settings;
Language la;
public frmMain(bool fromurl=false, string url="")
{
Environment.CurrentDirectory = Path.GetDirectoryName(Application.ExecutablePath);
InitializeComponent();
InitSettings();
lblVisit.Text = la.VisitMyWebsite;
lblLicense.Text = la.LicensedUnder + " MIT License";
cbOpen.Text = la.OpenFolderAfterDownload;
btnDownload.Text = la.Download;
btnPaste.Text = la.Paste;
btnSettings.Text = la.Settings;
tslStatus.Text = la.StsReady;
btnODLF.Text = la.OpenDownloadFolder;
if (fromurl)
{
txtUrl.Text = url;
btnDownload_Click(this, EventArgs.Empty);
}
}
private bool InitSettings(uint try_=1)
{
try
{
XmlSerializer xs = new XmlSerializer(typeof(Settings));
FileStream fs = new FileStream("settings.xml", FileMode.Open, FileAccess.Read);
settings = (Settings)xs.Deserialize(fs);
la = LanguageManager.LoadLanguage(settings.Language);
fs.Close();
return true;
}
catch (Exception e)
{
DialogResult dr = MessageBox.Show("ERROR: \r\n" + e.Message, "4chget", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
if (dr == DialogResult.Retry)
{
bool s = InitSettings(try_ + 1);
if (!InitSettings())
{
MessageBox.Show("Try #" + (try_ + 1) + " failed!", "4chget", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return false;
} else
{
return true;
}
} else
{
return false;
}
}
}
private void btnPaste_Click(object sender, EventArgs e)
{
txtUrl.Text = Clipboard.GetText();
if (_validateurl(txtUrl.Text))
{
pbOK.BackColor = Color.Green;
} else
{
pbOK.BackColor = Color.Red;
}
}
private void btnDownload_Click(object sender, EventArgs e)
{
try
{
tslStatus.Text = la.StsDownloading;
btnDownload.Enabled = false;
worker = new Downloader(settings);
worker.DownloadComplete += Worker_DownloadComplete;
worker.ErrorOccurred += Worker_ErrorOccurred;
string vurl = txtUrl.Text;
Regex regex = new Regex(@"(https|http):\/\/boards\.4chan\.org\/[a-zA-Z0-9]*\/thread\/[0-9]+[a-zA-Z0-9\/\-]*");
if (regex.IsMatch(vurl))
{
worker.Download(vurl);
} else
{
throw new Exception(la.ExcURLDoesntMatchRegex);
}
}
catch (Exception ex)
{
frmError error = new frmError(ex, la);
error.ShowDialog();
}
finally
{
btnDownload.Enabled = true;
}
}
private void Worker_ErrorOccurred(object sender, ErrorEventArgs e)
{
frmError err = new frmError(e.GetException(), la);
err.ShowDialog();
}
private void Worker_DownloadComplete(object sender, EventArgs e)
{
tslStatus.Text = la.StsReady;
MessageBox.Show(la.MsgFinishedDownloading, "4chget", MessageBoxButtons.OK, MessageBoxIcon.Information);
btnDownload.Enabled = true;
}
private void txtUrl_TextChanged(object sender, EventArgs e)
{
if (_validateurl(txtUrl.Text))
{
pbOK.BackColor = Color.Green;
}
else
{
pbOK.BackColor = Color.Red;
}
}
private bool _validateurl(string t)
{
Regex regex = new Regex(@"(https|http):\/\/boards\.4chan\.org\/[a-zA-Z0-9]*\/thread\/[0-9]+[a-zA-Z0-9\/\-]*");
if (regex.IsMatch(t))
{
return true;
}
else
{
return false;
}
}
private void btnSettings_Click(object sender, EventArgs e)
{
frmSettings settings = new frmSettings();
settings.Init();
settings.ShowDialog();
settings.Dispose();
}
private void lblVisit_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Process.Start("http://felipekaisersoft.hol.es/");
}
private void btnODLF_Click(object sender, EventArgs e)
{
Process.Start(settings.SavePath);
}
private void btnManager_Click(object sender, EventArgs e)
{
frmManager man = new frmManager(settings, la, Location);
man.Show();
}
}
}