Subversion Repositories spk

Rev

Rev 134 | Rev 161 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 cycrow 1
#pragma once
2
 
3
using namespace System;
4
using namespace System::ComponentModel;
5
using namespace System::Collections;
6
using namespace System::Windows::Forms;
7
using namespace System::Data;
8
using namespace System::Drawing;
9
 
10
enum {ERROR_NONE, ERROR_ACCESSDENIED};
11
namespace PluginManager {
12
 
13
	/// <summary>
14
	/// Summary for CheckUpdate
15
	///
16
	/// WARNING: If you change the name of this class, you will need to change the
17
	///          'Resource File Name' property for the managed resource compiler tool
18
	///          associated with all .resx files this class depends on.  Otherwise,
19
	///          the designers will not be able to interact properly with localized
20
	///          resources associated with this form.
21
	/// </summary>
22
	public ref class CheckUpdate : public System::Windows::Forms::Form
23
	{
24
	public:
25
		CheckUpdate(CPackages *p, ImageList ^images)
26
		{
27
			m_iStatus = 0;
28
			InitializeComponent();
29
 
30
			m_lInstall = gcnew ArrayList();
31
			m_pPackages = p;
32
			m_pPackage = NULL;
33
			m_pImageList = images;
34
			this->listView1->SmallImageList = m_pImageList;
35
			this->listView1->LargeImageList = m_pImageList;
36
 
37
			this->listView1->Groups->Add(gcnew ListViewGroup("Packages", HorizontalAlignment::Left));
38
			this->listView1->Groups->Add(gcnew ListViewGroup("Ships", HorizontalAlignment::Left));
39
			this->listView1->Groups->Add(gcnew ListViewGroup("Mods", HorizontalAlignment::Left));
40
 
41
			m_pCurrentItem = nullptr;
42
			m_lAddress = new CyStringList;
43
 
49 cycrow 44
			for ( CBaseFile *package = p->PackageList()->First(); package; package = p->PackageList()->Next() ) {
45
				if ( !package->webAddress().empty() ) this->AddPackage(package);
1 cycrow 46
			}
47
 
48
			this->UpdateList();
49
		}
50
 
51
		void SetDownloader()
52
		{
53
			m_bDownloader = true;
54
			this->Text = "Download Packages";
55
 
56
			this->listView1->Items->Clear();
57
			for ( SAvailablePackage *package = m_pPackages->GetAvailablePackageList()->First(); package; package = m_pPackages->GetAvailablePackageList()->Next() )
58
				this->AddPackage(package);
59
 
60
			this->UpdateList();
61
		}
62
 
63
		ArrayList ^GetInstallList() { return m_lInstall; }
64
 
65
		void OnePackage(CBaseFile *p)
66
		{
67
			this->listView1->Items->Clear();
68
			this->listView1->Groups->Clear();
69
			this->listView1->CheckBoxes = false;
70
			m_pCurrentItem = this->AddPackage(p);
71
			this->UpdateList();
72
			this->button2->Visible = false;
73
			m_pPackage = p;
74
			this->Height = 250;
75
		}
76
 
77
		void OnePackage(SAvailablePackage *p)
78
		{
79
			m_bDownloader = true;
80
			this->Text = "Download Package";
81
			this->listView1->Items->Clear();
82
			this->listView1->Groups->Clear();
83
			this->listView1->CheckBoxes = false;
84
			m_pCurrentItem = this->AddPackage(p);
85
			this->UpdateList();
86
			this->button2->Visible = false;
87
			m_pDownloadPackage = p;
88
			this->Height = 250;
89
		}
90
 
91
		ListViewItem ^AddPackage(SAvailablePackage *p)
92
		{
93
			ListViewItem ^item = gcnew ListViewItem(SystemStringFromCyString(p->sName));
94
			item->SubItems->Add(SystemStringFromCyString(p->sAuthor));
95
			item->SubItems->Add(SystemStringFromCyString(p->sVersion));
96
			item->SubItems->Add("Waiting");
97
			item->SubItems->Add(SystemStringFromCyString(p->sFilename));
98
			item->Tag = SystemStringFromCyString(p->sFilename);
99
 
100
			AddPackage(item, p->iType);
101
 
102
			return item;
103
		}
104
 
105
		void AddPackage(ListViewItem ^item, int type)
106
		{
107
			item->SubItems->Add("");
108
			item->Checked = true;
109
			item->SubItems->Add("");
110
			this->listView1->Items->Add(item);
111
 
112
			if ( this->listView1->Groups->Count )
113
			{
114
				if ( type == PACKAGETYPE_SHIP )
115
					item->Group = this->listView1->Groups[1];
116
				else if ( type == PACKAGETYPE_MOD )
117
					item->Group = this->listView1->Groups[2];
118
				else
119
					item->Group = this->listView1->Groups[0];
120
			}
121
 
122
			item->ImageIndex = -1;
123
 
124
			if ( item->ImageIndex == -1 )
125
			{
126
				if ( type == PACKAGETYPE_SHIP )
127
					item->ImageKey = "ship.png";
128
				else if ( type == PACKAGETYPE_LIBRARY )
129
					item->ImageKey = "library.png";
130
				else if ( type == PACKAGETYPE_FAKEPATCH || type == PACKAGETYPE_MOD )
131
					item->ImageKey = "fake.png";
132
				else
133
					item->ImageKey = "package.png";
134
			}
135
		}
136
 
137
		ListViewItem ^AddPackage(CBaseFile *p)
138
		{
139
			ListViewItem ^item = gcnew ListViewItem(SystemStringFromCyString(p->GetLanguageName(m_pPackages->GetLanguage())));
50 cycrow 140
			item->SubItems->Add(_US(p->author()));
141
			item->SubItems->Add(_US(p->version()));
1 cycrow 142
			item->SubItems->Add("Waiting");
134 cycrow 143
			item->SubItems->Add(_US(p->getNameValidFile().removeChar(' ') + "_" + p->author().remove(' ') + ".dat"));
1 cycrow 144
			item->Tag = p->GetNum();
145
 
146
			if ( p->GetType() == TYPE_XSP )
147
				AddPackage(item, PACKAGETYPE_SHIP);
148
			else if ( p->IsMod() )
149
				AddPackage(item, PACKAGETYPE_MOD);
150
			else if ( p->GetType() == TYPE_SPK && ((CSpkFile *)p)->IsLibrary() )
151
				AddPackage(item, PACKAGETYPE_LIBRARY);
152
			else if ( p->IsFakePatch() )
153
				AddPackage(item, PACKAGETYPE_FAKEPATCH);
154
			else
155
				AddPackage(item, PACKAGETYPE_NORMAL);
156
 
157
			if ( p->GetIcon() )
158
				PluginManager::DisplayListIcon(p, this->listView1, item);
159
 
160
			return item;
161
		}
162
 
163
		void UpdateNextPackage()
164
		{
165
			m_pPackage = NULL;
166
			m_pDownloadPackage = NULL;
167
			int pos = (m_pCurrentItem) ? this->listView1->Items->IndexOf(m_pCurrentItem) : -1;
168
			m_pCurrentItem = nullptr;
169
 
170
			++pos;
171
			if ( pos < this->listView1->Items->Count )
172
			{
173
				m_pCurrentItem = this->listView1->Items[pos];
174
				if ( m_bDownloader )
126 cycrow 175
					m_pDownloadPackage = m_pPackages->FindAvailablePackage(_S(Convert::ToString(m_pCurrentItem->Tag)));
1 cycrow 176
				else
177
					m_pPackage = m_pPackages->GetPackageAt(Convert::ToInt32(m_pCurrentItem->Tag));
178
			}
179
 
180
			if ( m_pCurrentItem && (m_pPackage || m_pDownloadPackage) )
181
				this->StartUpdate();
182
			else // end of the list
183
			{
184
				this->progressBar1->Style = Windows::Forms::ProgressBarStyle::Continuous;
185
				this->listView1->UseWaitCursor = false;
186
				if ( m_bDownloader )
187
					this->button2->Text = "Install Now";
188
				else
189
					this->button2->Text = "Update Now";
190
				this->button2->DialogResult = Windows::Forms::DialogResult::OK;
191
				this->button1->Text = "Close";
192
				this->button1->DialogResult = Windows::Forms::DialogResult::Cancel;
193
				this->listView1->CheckBoxes = false;
194
 
195
				m_iStatus = 2;
196
				// check if theres any updates
197
				this->CheckButton();
198
			}
199
		}
200
 
201
		void CheckNextPackage()
202
		{
203
			m_pPackage = NULL;
204
			m_pDownloadPackage = NULL;
205
			int pos = (m_pCurrentItem) ? this->listView1->Items->IndexOf(m_pCurrentItem) : -1;
206
			m_pCurrentItem = nullptr;
207
 
208
			++pos;
209
			if ( pos < this->listView1->Items->Count )
210
			{
211
				m_pCurrentItem = this->listView1->Items[pos];
212
				if ( m_bDownloader )
126 cycrow 213
					m_pDownloadPackage = m_pPackages->FindAvailablePackage(_S(Convert::ToString(m_pCurrentItem->Tag)));
1 cycrow 214
				else
215
					m_pPackage = m_pPackages->GetPackageAt(Convert::ToInt32(m_pCurrentItem->Tag));
216
			}
217
 
218
			if ( m_pCurrentItem && (m_pPackage || m_pDownloadPackage) )
219
				this->StartCheck();
220
			else // end of the list
221
			{
222
				this->progressBar1->Style = Windows::Forms::ProgressBarStyle::Continuous;
223
				this->listView1->UseWaitCursor = false;
224
				if ( m_bDownloader )
225
					this->button2->Text = "Start Download";
226
				else
227
					this->button2->Text = "Start Update";
228
				this->button1->Text = "Close";
229
				this->button1->DialogResult = Windows::Forms::DialogResult::Cancel;
230
 
231
				m_iStatus = 1;
232
				// check if theres any updates
233
				this->CheckButton();
234
			}
235
		}
236
 
237
		void CheckButton()
238
		{
239
			this->button2->Visible = false;
240
 
241
			if ( m_pCurrentItem )
242
				return;
243
 
244
			for ( int i = 0; i < this->listView1->Items->Count; i++ )
245
			{
246
				if ( !this->listView1->CheckBoxes || this->listView1->Items[i]->Checked )
247
				{
248
					// if we are updating, check for a valid server
249
					if ( m_iStatus == 0 || this->listView1->Items[i]->SubItems[5]->Text->Length )
250
					{
251
						this->button2->Visible = true;
252
						break;
253
					}
254
				}
255
			}
256
		}
257
 
258
		void StartCheck()
259
		{
260
			this->CheckButton();
261
			this->button1->Text = "Cancel";
262
			this->button1->DialogResult = Windows::Forms::DialogResult::None;
263
			this->progressBar1->Style = Windows::Forms::ProgressBarStyle::Marquee;
264
			this->listView1->UseWaitCursor = true;
265
			m_sStatus = "";
266
			m_sBestServer = "";
267
			m_pCurrentItem->Selected = true;
268
			m_pCurrentItem->SubItems[3]->Text = "Checking";
269
 
270
			this->UpdateList();
271
 
272
			// add all address
273
			if ( m_pDownloadPackage )
126 cycrow 274
				m_lAddress->PushBack(CyString(m_pDownloadPackage->sFilename));
1 cycrow 275
			else if ( m_pPackage )
276
			{
49 cycrow 277
				m_lAddress->PushBack(CyString(m_pPackage->webAddress()));
1 cycrow 278
				for ( int i = 0; i < m_pPackage->GetMaxWebMirrors(); i++ )
279
					m_lAddress->PushBack(m_pPackage->GetWebMirror(i));
280
			}
281
			else
282
				return;
283
 
284
			// start the check
285
			this->backgroundWorker1->RunWorkerAsync();
286
		}
287
 
288
		void StartUpdate()
289
		{
290
			// create downloads directory
291
			System::String ^dir = System::IO::FileInfo(System::Windows::Forms::Application::ExecutablePath).DirectoryName;
292
			if ( !IO::Directory::Exists(dir + "\\Downloads") )
293
			{
294
				try {
295
					IO::Directory::CreateDirectory(dir + "\\Downloads");
296
				}
297
				catch (System::UnauthorizedAccessException ^) {
298
					MessageBox::Show(this, "Unable to create directory for downloads\nAccess Denied\n(" + dir + "\\Downloads)", "Access Denied", MessageBoxButtons::OK, MessageBoxIcon::Error);
299
					this->Close();					
300
				}
301
			}
302
 
303
			// start the update
304
			this->CheckButton();
305
			this->button1->Text = "Cancel";
306
			this->button1->DialogResult = Windows::Forms::DialogResult::None;
307
			this->progressBar1->Style = Windows::Forms::ProgressBarStyle::Marquee;
308
 
309
			this->listView1->UseWaitCursor = true;
310
			m_pCurrentItem->Selected = true;
311
			m_pCurrentItem->SubItems[3]->Text = "Downloading";
312
			this->UpdateList();
313
 
314
			// start the update
315
			this->backgroundWorker2->RunWorkerAsync();
316
		}
317
 
318
		void UpdateList()
319
		{
320
			this->listView1->AutoResizeColumns(ColumnHeaderAutoResizeStyle::HeaderSize);
321
		}
322
 
323
		bool ReadData(String ^url)
324
		{
325
			bool ret = false;
326
			m_sData = "";
327
			try 
328
			{
329
				System::Net::WebClient ^Client = gcnew System::Net::WebClient();
330
				Client->Headers->Add( "user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)" );
331
				System::IO::Stream ^strm = Client->OpenRead(url);
332
				System::IO::StreamReader ^sr = gcnew System::IO::StreamReader(strm);
333
				m_sData = sr->ReadToEnd();
334
 
335
				strm->Close();
336
				sr->Close();
337
 
338
				ret = true;
339
			}
340
			catch (System::Net::WebException ^)
341
			{
342
				return false;
343
			}
344
 
345
			if ( m_sData->Length )
346
				return ret;
347
			return false;
348
		}
349
		int CheckFile(String ^url)
350
		{
351
			int ret = CONNECTERROR_UNKNOWN;
352
			try 
353
			{
354
				// check for the file
355
				Net::WebRequest ^check = (Net::HttpWebRequest ^)Net::WebRequest::Create(url);
356
				check->Credentials = Net::CredentialCache::DefaultCredentials;
357
				Net::WebResponse ^response = (Net::HttpWebResponse ^)check->GetResponse();
358
				// check the file size
359
				__int64 fileSize = response->ContentLength;
360
				if ( fileSize )
361
					ret = CONNECTERROR_NONE;
362
 
363
				response->Close();
364
 
365
			}
366
			catch (System::Net::WebException ^ex)
367
			{
368
				if ( ex->Status == System::Net::WebExceptionStatus::ConnectFailure )
369
					ret = CONNECTERROR_FAILED;
370
				else
371
				{
372
					switch ( cli::safe_cast<Net::HttpWebResponse ^>(ex->Response)->StatusCode )
373
					{
374
						case Net::HttpStatusCode::NotFound:
375
							ret = CONNECTERROR_NOFILE;
376
							break;
377
						case Net::HttpStatusCode::RequestTimeout:
378
							ret = CONNECTERROR_TIMEOUT;
379
							break;
380
						default:
381
							ret = CONNECTERROR_UNKNOWN;
382
					}
383
				}
384
			}
385
 
386
			return ret;
387
		}
388
 
389
	protected:
390
		ImageList		^m_pImageList;
391
		ArrayList		^m_lInstall;
392
		String			^m_sData;
393
		String			^m_sStatus;
394
		String			^m_sBestServer;
395
		CyStringList	*m_lAddress;
396
		ListViewItem	^m_pCurrentItem;
397
		CPackages		*m_pPackages;
398
		CBaseFile		*m_pPackage;
399
		int				 m_iStatus;
400
		bool			 m_bDownloader;
401
		SAvailablePackage *m_pDownloadPackage;
402
		int				 m_iErrorState;			
403
private: System::Windows::Forms::ColumnHeader^  columnHeader4;
404
private: System::ComponentModel::BackgroundWorker^  backgroundWorker2;
405
private: System::Windows::Forms::Button^  button2;
406
protected: 
407
 
408
		/// <summary>
409
		/// Clean up any resources being used.
410
		/// </summary>
411
		~CheckUpdate()
412
		{
413
			if (components)
414
			{
415
				delete components;
416
			}
417
			delete m_lAddress;
418
		}
419
 
420
	private: System::Windows::Forms::ProgressBar^  progressBar1;
421
	private: System::ComponentModel::BackgroundWorker^  backgroundWorker1;
422
	private: System::Windows::Forms::ListView^  listView1;
423
	private: System::Windows::Forms::ColumnHeader^  columnHeader1;
424
	private: System::Windows::Forms::ColumnHeader^  columnHeader2;
425
	private: System::Windows::Forms::ColumnHeader^  columnHeader3;
426
	private: System::Windows::Forms::Panel^  panel4;
427
	private: System::Windows::Forms::Button^  button1;
428
	protected: 
429
 
430
	private:
431
		/// <summary>
432
		/// Required designer variable.
433
		/// </summary>
434
		System::ComponentModel::Container ^components;
435
 
436
#pragma region Windows Form Designer generated code
437
		/// <summary>
438
		/// Required method for Designer support - do not modify
439
		/// the contents of this method with the code editor.
440
		/// </summary>
441
		void InitializeComponent(void)
442
		{
443
			this->progressBar1 = (gcnew System::Windows::Forms::ProgressBar());
444
			this->backgroundWorker1 = (gcnew System::ComponentModel::BackgroundWorker());
445
			this->listView1 = (gcnew System::Windows::Forms::ListView());
446
			this->columnHeader1 = (gcnew System::Windows::Forms::ColumnHeader());
447
			this->columnHeader2 = (gcnew System::Windows::Forms::ColumnHeader());
448
			this->columnHeader3 = (gcnew System::Windows::Forms::ColumnHeader());
449
			this->columnHeader4 = (gcnew System::Windows::Forms::ColumnHeader());
450
			this->panel4 = (gcnew System::Windows::Forms::Panel());
451
			this->button2 = (gcnew System::Windows::Forms::Button());
452
			this->button1 = (gcnew System::Windows::Forms::Button());
453
			this->backgroundWorker2 = (gcnew System::ComponentModel::BackgroundWorker());
454
			this->panel4->SuspendLayout();
455
			this->SuspendLayout();
456
			// 
457
			// progressBar1
458
			// 
459
			this->progressBar1->Dock = System::Windows::Forms::DockStyle::Bottom;
460
			this->progressBar1->Location = System::Drawing::Point(20, 192);
461
			this->progressBar1->MarqueeAnimationSpeed = 10;
462
			this->progressBar1->Name = L"progressBar1";
463
			this->progressBar1->Size = System::Drawing::Size(593, 31);
464
			this->progressBar1->Step = 1;
465
			this->progressBar1->Style = System::Windows::Forms::ProgressBarStyle::Continuous;
466
			this->progressBar1->TabIndex = 3;
467
			// 
468
			// backgroundWorker1
469
			// 
470
			this->backgroundWorker1->WorkerReportsProgress = true;
471
			this->backgroundWorker1->WorkerSupportsCancellation = true;
472
			this->backgroundWorker1->DoWork += gcnew System::ComponentModel::DoWorkEventHandler(this, &CheckUpdate::backgroundWorker1_DoWork);
473
			this->backgroundWorker1->RunWorkerCompleted += gcnew System::ComponentModel::RunWorkerCompletedEventHandler(this, &CheckUpdate::backgroundWorker1_RunWorkerCompleted);
474
			// 
475
			// listView1
476
			// 
477
			this->listView1->CheckBoxes = true;
478
			this->listView1->Columns->AddRange(gcnew cli::array< System::Windows::Forms::ColumnHeader^  >(4) {this->columnHeader1, this->columnHeader2, 
479
				this->columnHeader3, this->columnHeader4});
480
			this->listView1->Dock = System::Windows::Forms::DockStyle::Fill;
481
			this->listView1->FullRowSelect = true;
482
			this->listView1->HideSelection = false;
483
			this->listView1->Location = System::Drawing::Point(20, 20);
484
			this->listView1->MultiSelect = false;
485
			this->listView1->Name = L"listView1";
486
			this->listView1->ShowItemToolTips = true;
487
			this->listView1->Size = System::Drawing::Size(593, 172);
488
			this->listView1->Sorting = System::Windows::Forms::SortOrder::Ascending;
489
			this->listView1->TabIndex = 7;
490
			this->listView1->UseCompatibleStateImageBehavior = false;
491
			this->listView1->UseWaitCursor = true;
492
			this->listView1->View = System::Windows::Forms::View::Details;
493
			this->listView1->Click += gcnew System::EventHandler(this, &CheckUpdate::listView1_Click);
494
			// 
495
			// columnHeader1
496
			// 
497
			this->columnHeader1->Text = L"Package";
498
			this->columnHeader1->Width = 200;
499
			// 
500
			// columnHeader2
501
			// 
502
			this->columnHeader2->Text = L"Author";
503
			this->columnHeader2->Width = 50;
504
			// 
505
			// columnHeader3
506
			// 
507
			this->columnHeader3->Text = L"Version";
508
			// 
509
			// columnHeader4
510
			// 
511
			this->columnHeader4->Text = L"Status";
512
			// 
513
			// panel4
514
			// 
515
			this->panel4->Controls->Add(this->button2);
516
			this->panel4->Controls->Add(this->button1);
517
			this->panel4->Dock = System::Windows::Forms::DockStyle::Bottom;
518
			this->panel4->Location = System::Drawing::Point(20, 223);
519
			this->panel4->Name = L"panel4";
520
			this->panel4->Padding = System::Windows::Forms::Padding(10);
521
			this->panel4->Size = System::Drawing::Size(593, 55);
522
			this->panel4->TabIndex = 8;
523
			// 
524
			// button2
525
			// 
526
			this->button2->Dock = System::Windows::Forms::DockStyle::Left;
527
			this->button2->Location = System::Drawing::Point(10, 10);
528
			this->button2->Name = L"button2";
529
			this->button2->Size = System::Drawing::Size(132, 35);
530
			this->button2->TabIndex = 1;
531
			this->button2->Text = L"Start Check";
532
			this->button2->UseVisualStyleBackColor = true;
533
			this->button2->Click += gcnew System::EventHandler(this, &CheckUpdate::button2_Click);
534
			// 
535
			// button1
536
			// 
537
			this->button1->DialogResult = System::Windows::Forms::DialogResult::Cancel;
538
			this->button1->Dock = System::Windows::Forms::DockStyle::Right;
539
			this->button1->Location = System::Drawing::Point(486, 10);
540
			this->button1->Name = L"button1";
541
			this->button1->Size = System::Drawing::Size(97, 35);
542
			this->button1->TabIndex = 0;
543
			this->button1->Text = L"Cancel";
544
			this->button1->UseVisualStyleBackColor = true;
545
			this->button1->Click += gcnew System::EventHandler(this, &CheckUpdate::button1_Click);
546
			// 
547
			// backgroundWorker2
548
			// 
549
			this->backgroundWorker2->WorkerReportsProgress = true;
550
			this->backgroundWorker2->WorkerSupportsCancellation = true;
551
			this->backgroundWorker2->DoWork += gcnew System::ComponentModel::DoWorkEventHandler(this, &CheckUpdate::backgroundWorker2_DoWork);
552
			this->backgroundWorker2->RunWorkerCompleted += gcnew System::ComponentModel::RunWorkerCompletedEventHandler(this, &CheckUpdate::backgroundWorker2_RunWorkerCompleted);
553
			this->backgroundWorker2->ProgressChanged += gcnew System::ComponentModel::ProgressChangedEventHandler(this, &CheckUpdate::backgroundWorker2_ProgressChanged);
554
			// 
555
			// CheckUpdate
556
			// 
557
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
558
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
559
			this->ClientSize = System::Drawing::Size(633, 298);
560
			this->ControlBox = false;
561
			this->Controls->Add(this->listView1);
562
			this->Controls->Add(this->progressBar1);
563
			this->Controls->Add(this->panel4);
564
			this->Name = L"CheckUpdate";
565
			this->Padding = System::Windows::Forms::Padding(20);
566
			this->ShowInTaskbar = false;
567
			this->StartPosition = System::Windows::Forms::FormStartPosition::CenterParent;
568
			this->Text = L"Checking Updates";
569
			this->Load += gcnew System::EventHandler(this, &CheckUpdate::CheckUpdate_Load);
570
			this->panel4->ResumeLayout(false);
571
			this->ResumeLayout(false);
572
 
573
		}
574
#pragma endregion
575
	private: System::Void CheckUpdate_Load(System::Object^  sender, System::EventArgs^  e) {
576
				 this->CheckButton();
577
				 if ( m_pCurrentItem )
578
				 {
579
					if ( m_pPackage )
580
						this->StartCheck();
581
					else if ( m_bDownloader && m_pDownloadPackage )
582
						this->StartCheck();
583
				 }
584
			 }
585
private: System::Void backgroundWorker1_DoWork(System::Object^  sender, System::ComponentModel::DoWorkEventArgs^  e) {
586
			 // for each server
587
			 String ^bestServer = "";
588
			 CyString bestVersion = "0.00";
589
			 String ^bestFile = "";
590
 
591
			 for ( SStringList *str = m_lAddress->Head(); str; str = str->next )
592
			 {
593
				 if ( backgroundWorker1->CancellationPending )
594
					 break;
595
				 String ^address = SystemStringFromCyString(str->str);
596
				 m_sData = "";
597
				 String ^url = address;
598
 
599
				 if ( !m_bDownloader )
600
					url += "/" + m_pCurrentItem->SubItems[4]->Text;
601
 
602
				 //int error = this->CheckFile(url);
603
				 int error = CheckWebFileExists(url);
604
 
605
				 if ( backgroundWorker1->CancellationPending )
606
					 break;
607
 
608
				 if ( m_bDownloader )
609
				 {
610
					 if ( error == CONNECTERROR_NONE )
611
					 {
612
						 m_sBestServer = url;
613
						 m_sStatus = "Ready";
614
						 return;
615
					 }
616
					 switch(error)
617
					 {
618
						case CONNECTERROR_NOFILE:
619
							m_sStatus = "File Not Found";
620
							break;
621
						case CONNECTERROR_TIMEOUT:
622
							m_sStatus = "Connection Timedout";
623
							break;
624
						case CONNECTERROR_FAILED:
625
							m_sStatus = "Failed";
626
							break;
627
					 }
628
				 }
629
 
630
				 if ( error == CONNECTERROR_NONE )
631
				 {
632
					 // file exists, lets download and examine it
633
					 if ( this->ReadData(url) )
634
					 {
635
						 if ( backgroundWorker1->CancellationPending )
636
							 break;
637
 
638
						// we have the file, parse data
639
						cli::array<String ^> ^lines = m_sData->Split('\n');
640
						if ( lines )
641
						{
642
							 CyString version;
643
							 CyString file;
644
							 for ( int i = 0; i < lines->Length; i++ )
645
							 {
646
								 CyString l = CyStringFromSystemString(lines[i]).Remove(9).Remove('\r');
647
								 //if ( !l.IsIn(":") ) continue;
648
								 CyString first = l.GetToken(":", 1, 1);
649
								 if ( first.Compare("Version") )
650
									 version = l.GetToken(":", 2).RemoveFirstSpace();
651
								 else if ( first.Compare("File") )
652
									 file = l.GetToken(":", 2).RemoveFirstSpace();
653
							 }
654
 
655
							 if ( !version.Empty() && !file.Empty() )
656
							 {
657
								 int error = this->CheckFile(address + "/" + SystemStringFromCyString(file));
658
								 if ( error == CONNECTERROR_NONE )
659
								 {
660
									 if ( bestVersion.CompareVersion(version) == COMPARE_NEWER )
661
									 {
662
										 bestServer = address + "/" + SystemStringFromCyString(file);
663
										 bestVersion = version;
664
									 }
665
								 }
666
							 }
667
						 }
668
					}
669
				 }
670
			 }
671
 
672
			 if ( m_bDownloader )
673
				 return;
674
 
675
			 // now check if we have found an update
676
			 if ( bestServer->Length )
677
			 {
50 cycrow 678
				 int v = m_pPackage->version().compareVersion(bestVersion.ToString());
1 cycrow 679
				 switch ( v )
680
				 {
681
					case COMPARE_OLDER:
682
						m_sStatus = "Older";
683
						break;
684
					case COMPARE_SAME:
685
						m_sStatus = "Same";
686
						break;
687
					case COMPARE_NEWER:
688
						m_sStatus = "Newer (" + SystemStringFromCyString(bestVersion) + ")";
689
						m_sBestServer = bestServer;
690
						break;
691
				 }
692
			 }
693
			 else
694
			 {
695
				 if ( this->backgroundWorker1->CancellationPending )
696
					m_sStatus = "Cancelled";
697
				 else
698
					m_sStatus = "No Update";
699
			 }
700
		 }
701
private: System::Void backgroundWorker1_RunWorkerCompleted(System::Object^  sender, System::ComponentModel::RunWorkerCompletedEventArgs^  e) {
702
			 // update the status display
703
			 m_pCurrentItem->SubItems[3]->Text = m_sStatus;
704
			 if ( m_sBestServer && m_sBestServer->Length )
705
				 m_pCurrentItem->SubItems[5]->Text = m_sBestServer;
706
			 this->UpdateList();
707
 
708
			 // next package
709
			 this->CheckNextPackage();
710
		 }
711
private: System::Void listView1_Click(System::Object^  sender, System::EventArgs^  e) {
712
			 this->CheckButton();
713
		 }
714
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
715
			 if ( m_iStatus == 0 ) // start check
716
			 {
717
				 // remove all items that are not checked
718
				 for ( int i = this->listView1->Items->Count - 1; i >= 0; i-- )
719
				 {
720
					 if ( this->listView1->Items[i]->Checked )
721
						 continue;
722
					 this->listView1->Items->RemoveAt(i);
723
				 }
724
 
725
				 this->CheckNextPackage();
726
			 }
727
			 else if ( m_iStatus == 1 ) // start downloading
728
			 {
729
				 for ( int i = this->listView1->Items->Count - 1; i >= 0; i-- )
730
				 {
731
					 if ( this->listView1->Items[i]->SubItems[5]->Text->Length && this->listView1->Items[i]->Checked )
732
						 continue;
733
					 this->listView1->Items->RemoveAt(i);
734
				 }
735
 
736
				 this->UpdateNextPackage();
737
			 }
738
			 else if ( m_iStatus == 2 ) // start installing
739
			 {
740
				 //this->button2 
741
			 }
742
		 }
743
private: System::Void backgroundWorker2_DoWork(System::Object^  sender, System::ComponentModel::DoWorkEventArgs^  e) {
744
 
745
				m_iErrorState = ERROR_NONE;
746
 
747
				Threading::Thread::Sleep(1000);
748
				System::Net::WebResponse ^res = nullptr;
749
				System::Net::WebRequest ^req = nullptr;
750
				try {
751
					req = System::Net::WebRequest::Create(m_pCurrentItem->SubItems[5]->Text);
752
					req->Timeout = 25000;
753
					res = req->GetResponse();
754
				}
755
				catch (System::Net::WebException ^) {
756
					return;
757
				}
758
 
759
				if ( backgroundWorker2->CancellationPending )
760
					return;
761
 
762
				System::String ^dir = System::IO::FileInfo(System::Windows::Forms::Application::ExecutablePath).DirectoryName;
763
				dir += "\\Downloads";
158 cycrow 764
				String ^file = dir + "\\" + _US(CFileIO(_S(m_pCurrentItem->SubItems[5]->Text)).filename().remove('\r').remove(9));
1 cycrow 765
 
766
				__int64 maxSize = res->ContentLength;
767
				__int64 curSize = 0;
768
 
769
				System::IO::FileStream ^writeStream = nullptr;
770
				try {
771
					writeStream = gcnew System::IO::FileStream(file, System::IO::FileMode::OpenOrCreate);
772
				}
773
				catch (System::UnauthorizedAccessException ^) {
774
					m_iErrorState = ERROR_ACCESSDENIED;
775
					return;
776
				}
777
				catch (System::IO::IOException ^)
778
				{
779
					return;
780
				}
781
				catch (System::Exception ^e)
782
				{
783
					MessageBox::Show("Error: " + e->ToString(), "Error", MessageBoxButtons::OK, MessageBoxIcon::Error);
784
					return;
785
				}
786
 
787
				do
788
				{
789
					if ( backgroundWorker2->CancellationPending )
790
						break;
791
 
792
					Threading::Thread::Sleep(20);
793
 
794
					array<unsigned char> ^readBytes = gcnew array<unsigned char>(4096);
795
					int read = res->GetResponseStream()->Read(readBytes, 0, 4096);
796
 
797
					curSize += (__int64)read;
798
 
799
					int percent = (int)((curSize * 100) / maxSize);
800
					backgroundWorker2->ReportProgress(percent);
801
 
802
					if ( read <= 0 )
803
						break;
804
 
805
					writeStream->Write(readBytes, 0, read);
806
				}
807
				while(1);
808
 
809
				res->GetResponseStream()->Close();
810
				writeStream->Close();
811
 
812
				Threading::Thread::Sleep(1000);
813
 
814
				if ( backgroundWorker2->CancellationPending )
815
				{
816
					try {
817
						System::IO::File::Delete(file);
818
					}
819
					catch (System::IO::IOException ^)
820
					{
821
					}
822
					catch (System::Exception ^)
823
					{
824
					}
825
				}
826
		 }
827
private: System::Void backgroundWorker2_ProgressChanged(System::Object^  sender, System::ComponentModel::ProgressChangedEventArgs^  e) {
828
			this->progressBar1->Style = Windows::Forms::ProgressBarStyle::Continuous;
829
 			this->progressBar1->Value = e->ProgressPercentage;
830
		 }
831
private: System::Void backgroundWorker2_RunWorkerCompleted(System::Object^  sender, System::ComponentModel::RunWorkerCompletedEventArgs^  e) {
832
			if ( this->backgroundWorker2->CancellationPending )
833
			{
834
				 m_pCurrentItem->SubItems[3]->Text = "Cancelled";
835
				 m_pCurrentItem->SubItems[5]->Text = "";
836
			}
837
			else
838
			{
839
				System::String ^dir = System::IO::FileInfo(System::Windows::Forms::Application::ExecutablePath).DirectoryName;
840
				dir += "\\Downloads";
158 cycrow 841
				String ^file = dir + "\\" + _US(CFileIO(_S(m_pCurrentItem->SubItems[5]->Text)).filename().remove('\r').remove(9));
1 cycrow 842
 
843
				 if ( IO::File::Exists(file) )
844
				 {
845
					 m_pCurrentItem->SubItems[3]->Text = "Ready to Install";
846
					 m_pCurrentItem->SubItems[5]->Text = file;
847
					 m_lInstall->Add(file);
848
				 }
849
				 else
850
				 {
851
					 switch ( m_iErrorState ) {
852
						 case ERROR_ACCESSDENIED:
853
							 m_pCurrentItem->SubItems[3]->Text = "Access Denied";
854
							 break;
855
						 default:
856
							 m_pCurrentItem->SubItems[3]->Text = "Failed";
857
					 }
858
					 m_pCurrentItem->SubItems[5]->Text = "";
859
				 }
860
				 this->UpdateList();
861
				 this->UpdateNextPackage();
862
			}
863
		 }
864
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
865
			 this->backgroundWorker1->CancelAsync();
866
			 this->backgroundWorker2->CancelAsync();
867
		 }
868
};
869
}