-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainForm.cs
225 lines (181 loc) · 5.72 KB
/
MainForm.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace Quickchan
{
public partial class MainForm : Form
{
readonly Konachaner k = new Konachaner();
readonly Dictionary<int, List<KonImage>> konImages = new Dictionary<int, List<KonImage>>();
readonly HashSet<string> viewed = new HashSet<string>();
const string AppName = "Quickchan";
static readonly string ViewedFile = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"LonamiWebs\\Quickchan\\viewed.qc");
string SaveTo;
int _RemainingDownloads;
int RemainingDownloads
{
get { return _RemainingDownloads; }
set
{
_RemainingDownloads = value;
downloadingL.Text = value + " downloading";
}
}
readonly bool Loaded;
public MainForm()
{
InitializeComponent();
Width = Settings.GetValue<int>("width");
Height = Settings.GetValue<int>("height");
sizeTB.Value = Settings.GetValue<int>("size");
k.OnKonImg += k_OnKonImg;
k.OnGetPageCount += RefreshTitle;
k.NoResults += k_NoResults;
if (File.Exists(ViewedFile))
foreach (var line in File.ReadAllLines(ViewedFile))
viewed.Add(line);
else
File.WriteAllText(ViewedFile, "", Encoding.UTF8);
CheckSettings();
Loaded = true;
}
public void CheckSettings()
{
var safeMode = Settings.GetValue<bool>("safeMode");
k.SafeMode = safeMode;
konImagesFLP.Controls.Clear();
konImages.Clear();
k.GetPreviews();
var saveTo = Settings.GetValue<string>("defaultFolder");
if (!Directory.Exists(saveTo))
saveTo = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
SaveTo = Settings.GetValue<bool>("dynamic") ? "" : saveTo;
}
void k_OnKonImg(KonImage konImg)
{
konImagesFLP.BackColor = Color.White;
if (!konImages.ContainsKey(konImg.PageNumber))
konImages.Add(konImg.PageNumber, new List<KonImage>());
konImages[konImg.PageNumber].Add(konImg);
if (konImg.PageNumber == k.Page)
AddKonImg(konImg);
}
void k_NoResults()
{
Text = AppName + " - No results found";
konImagesFLP.BackColor = Color.Salmon;
}
void PrevPageBClick(object sender, EventArgs e) { ChangePage(-1); }
void NextPageBClick(object sender, EventArgs e) { ChangePage(1); }
void ChangePage(int amount)
{
k.Page += amount;
if (!konImages.ContainsKey(k.Page))
{
konImages.Add(k.Page, new List<KonImage>());
k.GetPreviews();
}
RefreshPage();
RefreshTitle();
}
void RefreshPage()
{
pageTB.Text = k.Page.ToString();
konImagesFLP.SuspendLayout();
konImagesFLP.Controls.Clear();
if (konImages.ContainsKey(k.Page))
foreach (var konImg in konImages[k.Page])
AddKonImg(konImg);
konImagesFLP.ResumeLayout();
}
void RefreshTitle() { Text = AppName + " - " + k.Page + "/" + k.PageCount; }
void AddKonImg(KonImage konImg)
{
var kic = new KonImageControl(
konImg,
Viewed(konImg.ID) ? (int)sizeTB.Value / 4 : (int)sizeTB.Value,
SaveTo);
kic.DownloadStarted += () => RemainingDownloads++;
kic.DownloadCompleted += () => RemainingDownloads--;
kic.MouseEnter += (sender, e) => Text = kic.Title;
kic.MouseLeave += (sender, e) => RefreshTitle();
konImagesFLP.Controls.Add(kic);
viewed.Add(konImg.ID);
File.WriteAllLines(ViewedFile, viewed);
}
void SettingsBClick(object sender, EventArgs e)
{
var f = Application.OpenForms["SettingsForm"];
if (f == null)
new SettingsForm().Show();
else
f.Activate();
}
void FilterTBKeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.Handled = e.SuppressKeyPress = true;
k.ClearFilters();
foreach (var f in filterTB.Text.Split(' '))
k.AddFilter(f);
konImages.Clear();
konImagesFLP.Controls.Clear();
k.GetPreviews();
}
}
void MainFormFormClosing(object sender, FormClosingEventArgs e)
{
if (RemainingDownloads > 0 && e.CloseReason == CloseReason.UserClosing)
{
e.Cancel =
MessageBox.Show("There are some pending downloads. Do you want to exit and cancel them?",
"Pending downloads", MessageBoxButtons.YesNo, MessageBoxIcon.Information)
== DialogResult.No;
}
}
void PageTBKeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.Handled = e.SuppressKeyPress = true;
int increment = 0;
int.TryParse(pageTB.Text, out increment);
increment = increment - k.Page;
ChangePage(increment);
}
}
void KonImagesFLPMouseEnter(object sender, EventArgs e)
{ konImagesFLP.Focus(); }
void PageTBMouseEnter(object sender, EventArgs e)
{ pageTB.Focus(); pageTB.SelectAll(); }
void FilterTBMouseEnter(object sender, EventArgs e)
{ filterTB.Focus(); }
void SizeTBScroll(object sender, EventArgs e)
{
konImagesFLP.SuspendLayout();
foreach (KonImageControl kic in konImagesFLP.Controls)
kic.Height = (int)sizeTB.Value;
konImagesFLP.ResumeLayout();
if (Loaded)
Settings.SetValue<int>("size", (int)sizeTB.Value);
}
void MainFormResizeEnd(object sender, EventArgs e)
{
if (Loaded)
{
Settings.SetValue<int>("width", Width);
Settings.SetValue<int>("height", Height);
Settings.SetValue<bool>("maximized", WindowState == FormWindowState.Maximized);
}
}
bool Viewed(string id)
{ return Settings.GetValue<bool>("viewedSmaller") && viewed.Contains(id); }
}
}