Subversion Repositories spk

Rev

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