-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainForm.cs
552 lines (463 loc) · 18.5 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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AllegroFinder.Data;
using AllegroFinder.Searcher;
using System.Globalization;
using System.Diagnostics;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
namespace AllegroFinder
{
public partial class MainForm : Form
{
#region private fields
private LoginManager lm = null;
private AuctionsContainer container = new AuctionsContainer();
private Shortlist shortList = new Shortlist();
#endregion
#region public fields
public bool IsInitialized { get; set; }
#endregion
#region private methods
#region background worker methods
private void BackgroundWorkerSearchSelected(bool clearBeforeSearch, bool showBaloonAfterSearch)
{
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += new DoWorkEventHandler(SearchSelectedAsync);
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_SearchSelectedCompleted);
bw.RunWorkerAsync(new SearchCriteria() { ClearBeforeSearch = clearBeforeSearch, ShowBaloon = showBaloonAfterSearch });
}
private void BackgroundWorkerLogin()
{
BackgroundWorker bgWorker = new BackgroundWorker();
bgWorker.DoWork += new DoWorkEventHandler(Login);
bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgWorker_LoginCompleted);
bgWorker.RunWorkerAsync();
}
private void BackgroundWorkerSearchSelected(string title)
{
BackgroundWorker bgWorker = new BackgroundWorker();
bgWorker.DoWork += new DoWorkEventHandler(SearchAsync);
bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgWorker_SearchCompleted);
bgWorker.RunWorkerAsync(title);
}
#endregion
#region login methods
private void Login(object sender, DoWorkEventArgs e)
{
e.Result = lm.Login();
}
private void Login()
{
if (lm.UnsuccessfulLoginCounter == Consts.MaxConnectTries)
{
uxErrorLabel.Text =
string.Format("Błąd komunikacji z serwerem Allegro. Nieudanych prób: {0}. Uruchom program ponownie za jakiś czas.", lm.UnsuccessfulLoginCounter);
}
else
{
EnableLoader();
SetAllButtonsState(false);
uxErrorLabel.Text = string.Format("Łączenie do serwera Allegro... Próba nr {0}", lm.UnsuccessfulLoginCounter + 1);
BackgroundWorkerLogin();
}
}
#endregion
#region loader
private void EnableLoader()
{
uxLoader.Visible = true;
}
private void DisableLoader()
{
uxLoader.Visible = false;
}
#endregion
#region buttons helpers
private void SetAllButtonsState(bool enabled)
{
uxSearchNowBtn.Enabled = enabled;
uxSearchSelectedBtn.Enabled = enabled;
uxWaitForNewBtn.Enabled = enabled;
}
#endregion
#region shortlist helpers
private void ShortlistDeleteSelected()
{
if (uxGamesList.SelectedIndex != -1)
{
string selected = uxGamesList.Items[uxGamesList.SelectedIndex] as string;
shortList.Delete(selected);
BindShortList();
}
}
private void BindShortList()
{
uxGamesList.Items.Clear();
foreach (GameTitle title in shortList.Games)
{
uxGamesList.Items.Add(title.Title, title.Checked);
}
}
#endregion
#region event handlers
private void SearchAsync(object sender, DoWorkEventArgs e)
{
container.Clear();
SearchCriteria criteria = new SearchCriteria() { ClearBeforeSearch = true, ShowBaloon = false };
AuctionSearcher searcher = new AuctionSearcher(new AllegroServiceProvider(), lm.SessionKey);
searcher.Title = e.Argument as string;
searcher.OnlyNew = false;
List<Auction> auctionsFound = null;
MethodResult methodResult = new MethodResult();
methodResult = searcher.DoSearch(out auctionsFound);
SearchResult result = new SearchResult() { Auctions = auctionsFound, ShowBaloon = criteria.ShowBaloon, AllegroMethodResult = methodResult };
e.Result = result;
}
private void SearchSelectedAsync(object sender, DoWorkEventArgs e)
{
SearchCriteria criteria = e.Argument as SearchCriteria;
bool clearBeforeSearch = criteria.ClearBeforeSearch;
if (clearBeforeSearch)
{
container.Clear();
}
List<Auction> result = new List<Auction>();
AuctionSearcher searcher = new AuctionSearcher(new AllegroServiceProvider(), lm.SessionKey);
MethodResult methodResult = new MethodResult();
foreach (GameTitle title in shortList.Games)
{
if (title.Checked)
{
searcher.Title = title.Title;
searcher.OnlyNew = false;
List<Auction> auctionsFound = new List<Auction>();
methodResult = searcher.DoSearch(out auctionsFound);
result.AddRange(auctionsFound);
}
}
container.Add(result);
SearchResult searchResult = new SearchResult() { Auctions = container.Auctions, ShowBaloon = criteria.ShowBaloon, AllegroMethodResult = methodResult };
e.Result = searchResult;
}
private void searcher_OnSearchFinished(object sender, AuctionEventArgs e)
{
List<Auction> auctions = e.Auctions;
foreach (Auction a in auctions)
{
container.Add(a);
}
FillView(container.Auctions);
}
private void bw_SearchSelectedCompleted(object sender, RunWorkerCompletedEventArgs e)
{
SearchResult result = e.Result as SearchResult;
if (!result.AllegroMethodResult.Success)
{
uxErrorLabel.Text = result.AllegroMethodResult.ErrorMessage;
if (result.AllegroMethodResult.ErrorCode == "ERR_NO_SESSION" ||
result.AllegroMethodResult.ErrorCode == "ERR_SESSION_EXPIRED")
{
Login();
}
DisableLoader();
return;
}
List<Auction> auctions = result.Auctions;
FillView(auctions);
uxSearchSelectedBtn.Enabled = true;
DisableLoader();
SetAllButtonsState(true);
if (result.ShowBaloon)
{
List<Auction> newAuctions = container.GetNewAuctions();
if (newAuctions.Count > 0)
{
StringBuilder sb = new StringBuilder();
foreach (Auction auction in newAuctions)
{
sb.AppendLine(auction.Name);
sb.AppendFormat("Cena: {0}", auction.Price.ToString("0.00", CultureInfo.InvariantCulture));
if (auction.BuyNowPrice.HasValue)
{
sb.AppendFormat(", Kup teraz: {0}", auction.BuyNowPrice.Value.ToString("0.00", CultureInfo.InvariantCulture));
}
sb.AppendLine(string.Empty);
sb.AppendLine(string.Empty);
}
uxAreNewBtn.Visible = true;
uxNotifyIcon.ShowBalloonTip(5000, "Nowe aukcje spełniające kryteria", sb.ToString(), ToolTipIcon.Info);
System.Media.SystemSounds.Asterisk.Play();
}
}
}
private void bgWorker_LoginCompleted(object sender, RunWorkerCompletedEventArgs e)
{
MethodResult result = (MethodResult)e.Result;
bool logged = result.Success;
if (logged)
{
uxErrorLabel.Text = "Połączenie OK";
}
else
{
uxErrorLabel.Text = "Połączenie z Allegro nieudane. " + result.ErrorMessage;
}
SetAllButtonsState(logged);
DisableLoader();
}
private void container_OnAuctionAdded(object sender, AuctionEventArgs e)
{
if (InvokeRequired)
{
EventHandler<AuctionEventArgs> handler = new EventHandler<AuctionEventArgs>(container_OnAuctionAdded);
this.Invoke(handler, new object[] { sender, e });
}
else
{
StringBuilder sb = new StringBuilder();
foreach (Auction auction in e.Auctions)
{
sb.AppendLine(auction.Name);
sb.AppendFormat("Cena: {0}", auction.Price.ToString("0.00", CultureInfo.InvariantCulture));
if (auction.BuyNowPrice.HasValue)
{
sb.AppendFormat(", Kup teraz: {0}", auction.BuyNowPrice.Value.ToString("0.00", CultureInfo.InvariantCulture));
}
sb.AppendLine(string.Empty);
sb.AppendLine(string.Empty);
}
uxAreNewBtn.Visible = true;
uxNotifyIcon.ShowBalloonTip(Consts.BalloonTipTime, "Nowe aukcje spełniające kryteria", sb.ToString(), ToolTipIcon.Info);
System.Media.SystemSounds.Asterisk.Play();
}
}
private void bgWorker_SearchCompleted(object sender, RunWorkerCompletedEventArgs e)
{
SearchResult result = e.Result as SearchResult;
if (!result.AllegroMethodResult.Success)
{
uxErrorLabel.Text = result.AllegroMethodResult.ErrorMessage;
if (result.AllegroMethodResult.ErrorCode == "ERR_NO_SESSION" ||
result.AllegroMethodResult.ErrorCode == "ERR_SESSION_EXPIRED")
{
Login();
}
DisableLoader();
return;
}
FillView(result.Auctions);
uxSearchNowBtn.Enabled = true;
DisableLoader();
SetAllButtonsState(true);
}
private void searchTimer_Tick(object sender, EventArgs e)
{
SearchSelected(false, true);
}
private void uxSearchNowBtn_Click(object sender, EventArgs e)
{
if (uxSearchNowBtn.Enabled)
{
RunWorkerForTitle(uxTitleTB.Text);
uxTitleTB.SelectAll();
}
}
private void uxAuctionsView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == 4 && e.Value != null)
{
e.FormattingApplied = true;
TimeSpan val = (TimeSpan)e.Value;
e.Value = Auction.GetTimeLeftAsString(val);
}
if ((e.ColumnIndex == 2 || e.ColumnIndex == 3) && e.Value != null)
{
decimal val = (decimal)e.Value;
e.Value = val.ToString("0.00");
e.FormattingApplied = true;
}
}
private void uxAuctionsView_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 6 && e.RowIndex != -1)
{
System.Diagnostics.Process.Start(uxAuctionsView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
}
}
private void uxTitleTB_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
uxSearchNowBtn_Click(uxSearchNowBtn, new EventArgs());
e.SuppressKeyPress = true;
}
}
private void uxAddBtn_Click(object sender, EventArgs e)
{
shortList.Add(uxTitleTB.Text);
BindShortList();
}
private void uxGamesList_ItemCheck(object sender, ItemCheckEventArgs e)
{
string gameTitle = uxGamesList.Items[e.Index] as string;
if (e.NewValue == CheckState.Checked)
{
shortList.SetChecked(gameTitle);
}
else
{
shortList.SetUnchecked(gameTitle);
}
}
private void uxSearchSelectedBtn_Click(object sender, EventArgs e)
{
SearchSelected(true, false);
uxSearchSelectedBtn.Enabled = false;
uxAreNewBtn.Visible = false;
}
private void uxWaitForNewBtn_Click(object sender, EventArgs e)
{
uxAreNewBtn.Visible = false;
SearchSelected(true, false);
Hide();
searchTimer.Start();
}
private void uxNotifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
Show();
searchTimer.Stop();
}
private void uxNotifyIcon_BalloonTipClicked(object sender, EventArgs e)
{
Show();
searchTimer.Stop();
FillView(container.GetNewAuctions());
}
private void uxGamesList_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
ShortlistDeleteSelected();
e.SuppressKeyPress = true;
}
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
XmlWriter writer = XmlWriter.Create(new FileStream(Shortlist.FileName, FileMode.Create, FileAccess.Write));
XmlSerializer serializer = new XmlSerializer(typeof(Shortlist));
serializer.Serialize(writer, shortList);
writer.Close();
}
private void uxAreNewBtn_Click(object sender, EventArgs e)
{
FillView(container.GetNewAuctions());
}
private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
{
ShortlistDeleteSelected();
}
private void searchToolStripMenuItem_Click(object sender, EventArgs e)
{
if (uxSearchNowBtn.Enabled)
{
if (uxGamesList.SelectedIndex != -1)
{
string selected = uxGamesList.Items[uxGamesList.SelectedIndex] as string;
RunWorkerForTitle(selected);
}
}
}
private void uxGamesList_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
for (int i = 0; i < uxGamesList.Items.Count; i++)
{
Rectangle rect = uxGamesList.GetItemRectangle(i);
if ((e.Y >= rect.Y && e.Y < (rect.Y + rect.Height)))
{
uxGamesList.SelectedItem = uxGamesList.Items[i];
return;
}
}
}
}
#endregion
private void SearchSelected(bool clearBeforeSearch, bool showBaloonAfterSearch)
{
BackgroundWorkerSearchSelected(clearBeforeSearch, showBaloonAfterSearch);
EnableLoader();
SetAllButtonsState(false);
}
private void RunWorkerForTitle(string title)
{
BackgroundWorkerSearchSelected(title);
uxSearchNowBtn.Enabled = false;
EnableLoader();
SetAllButtonsState(false);
}
private void FillView(List<Auction> list)
{
uxAuctionsView.Rows.Clear();
list.Sort(new TimeLeftComparer());
foreach (Auction auction in list)
{
DataGridViewRow row = new DataGridViewRow();
row.CreateCells(uxAuctionsView);
row.SetValues(new object[] { auction.Id,
auction.Name,
auction.Price,
(auction.BuyNowPrice.HasValue) ? auction.BuyNowPrice.Value : default(decimal),
auction.TimeLeft,
auction.Offers,
auction.AllegroLink});
if (auction.TimeLeft <= TimeSpan.FromHours(5))
{
foreach (DataGridViewCell cell in row.Cells)
{
cell.Style.BackColor = Color.Pink;
}
}
uxAuctionsView.Rows.Add(row);
}
}
#endregion
#region constructors
public MainForm()
{
InitializeComponent();
try
{
lm = new LoginManager(new AllegroServiceProvider(),
AppSettings.Default.ApiKey, AppSettings.Default.Login, AppSettings.Default.Password);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Configuration error", MessageBoxButtons.OK, MessageBoxIcon.Error);
IsInitialized = false;
return;
}
Login();
if (File.Exists(Shortlist.FileName))
{
using(Stream stream = new FileStream(Shortlist.FileName, FileMode.Open))
{
XmlSerializer serializer = new XmlSerializer(typeof(Shortlist));
shortList = serializer.Deserialize(stream) as Shortlist;
BindShortList();
}
}
searchTimer.Interval = 1000 * 60 * AppSettings.Default.CheckInterval;
IsInitialized = true;
}
#endregion
}
}