Subversion Repositories spk

Rev

Rev 224 | 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;
197 cycrow 42
			_lAddress = new Utils::WStringList;
1 cycrow 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
		{
171 cycrow 140
			ListViewItem ^item = gcnew ListViewItem(_US(p->name(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");
203 cycrow 144
			item->SubItems->Add(_US(p->getNameValidFile().removeChar(' ') + L"_" + p->author().remove(L' ') + L".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
 
170 cycrow 158
			if ( p->icon() )
1 cycrow 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 )
224 cycrow 176
					m_pDownloadPackage = m_pPackages->findAvailablePackage(_WS(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 )
224 cycrow 214
					m_pDownloadPackage = m_pPackages->findAvailablePackage(_WS(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 )
162 cycrow 275
				_lAddress->pushBack(m_pDownloadPackage->sFilename);
1 cycrow 276
			else if ( m_pPackage )
277
			{
162 cycrow 278
				_lAddress->pushBack(m_pPackage->webAddress());
273 cycrow 279
				for (size_t i = 0; i < m_pPackage->getMaxWebMirrors(); i++ )
162 cycrow 280
					_lAddress->pushBack(m_pPackage->getWebMirror(i));
1 cycrow 281
			}
282
			else
283
				return;
284
 
285
			// start the check
286
			this->backgroundWorker1->RunWorkerAsync();
287
		}
288
 
162 cycrow 289
		System::String^ GetDestiantion()
290
		{
291
			return Environment::GetFolderPath(Environment::SpecialFolder::Personal) + "\\Egosoft\\PluginManager";
292
		}
293
 
1 cycrow 294
		void StartUpdate()
295
		{
296
			// create downloads directory
162 cycrow 297
			System::String^ dir = GetDestiantion();
1 cycrow 298
			if ( !IO::Directory::Exists(dir + "\\Downloads") )
299
			{
300
				try {
301
					IO::Directory::CreateDirectory(dir + "\\Downloads");
302
				}
303
				catch (System::UnauthorizedAccessException ^) {
304
					MessageBox::Show(this, "Unable to create directory for downloads\nAccess Denied\n(" + dir + "\\Downloads)", "Access Denied", MessageBoxButtons::OK, MessageBoxIcon::Error);
305
					this->Close();					
306
				}
307
			}
308
 
309
			// start the update
310
			this->CheckButton();
311
			this->button1->Text = "Cancel";
312
			this->button1->DialogResult = Windows::Forms::DialogResult::None;
313
			this->progressBar1->Style = Windows::Forms::ProgressBarStyle::Marquee;
314
 
315
			this->listView1->UseWaitCursor = true;
316
			m_pCurrentItem->Selected = true;
317
			m_pCurrentItem->SubItems[3]->Text = "Downloading";
318
			this->UpdateList();
319
 
320
			// start the update
321
			this->backgroundWorker2->RunWorkerAsync();
322
		}
323
 
324
		void UpdateList()
325
		{
326
			this->listView1->AutoResizeColumns(ColumnHeaderAutoResizeStyle::HeaderSize);
327
		}
328
 
329
		bool ReadData(String ^url)
330
		{
331
			bool ret = false;
332
			m_sData = "";
333
			try 
334
			{
335
				System::Net::WebClient ^Client = gcnew System::Net::WebClient();
336
				Client->Headers->Add( "user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)" );
337
				System::IO::Stream ^strm = Client->OpenRead(url);
338
				System::IO::StreamReader ^sr = gcnew System::IO::StreamReader(strm);
339
				m_sData = sr->ReadToEnd();
340
 
341
				strm->Close();
342
				sr->Close();
343
 
344
				ret = true;
345
			}
346
			catch (System::Net::WebException ^)
347
			{
348
				return false;
349
			}
350
 
351
			if ( m_sData->Length )
352
				return ret;
353
			return false;
354
		}
355
		int CheckFile(String ^url)
356
		{
357
			int ret = CONNECTERROR_UNKNOWN;
358
			try 
359
			{
360
				// check for the file
361
				Net::WebRequest ^check = (Net::HttpWebRequest ^)Net::WebRequest::Create(url);
362
				check->Credentials = Net::CredentialCache::DefaultCredentials;
363
				Net::WebResponse ^response = (Net::HttpWebResponse ^)check->GetResponse();
364
				// check the file size
365
				__int64 fileSize = response->ContentLength;
366
				if ( fileSize )
367
					ret = CONNECTERROR_NONE;
368
 
369
				response->Close();
370
 
371
			}
372
			catch (System::Net::WebException ^ex)
373
			{
374
				if ( ex->Status == System::Net::WebExceptionStatus::ConnectFailure )
375
					ret = CONNECTERROR_FAILED;
376
				else
377
				{
378
					switch ( cli::safe_cast<Net::HttpWebResponse ^>(ex->Response)->StatusCode )
379
					{
380
						case Net::HttpStatusCode::NotFound:
381
							ret = CONNECTERROR_NOFILE;
382
							break;
383
						case Net::HttpStatusCode::RequestTimeout:
384
							ret = CONNECTERROR_TIMEOUT;
385
							break;
386
						default:
387
							ret = CONNECTERROR_UNKNOWN;
388
					}
389
				}
390
			}
391
 
392
			return ret;
393
		}
394
 
395
	protected:
396
		ImageList		^m_pImageList;
397
		ArrayList		^m_lInstall;
398
		String			^m_sData;
399
		String			^m_sStatus;
400
		String			^m_sBestServer;
197 cycrow 401
		Utils::WStringList	*_lAddress;
1 cycrow 402
		ListViewItem	^m_pCurrentItem;
403
		CPackages		*m_pPackages;
404
		CBaseFile		*m_pPackage;
405
		int				 m_iStatus;
406
		bool			 m_bDownloader;
161 cycrow 407
		const SAvailablePackage *m_pDownloadPackage;
1 cycrow 408
		int				 m_iErrorState;			
409
private: System::Windows::Forms::ColumnHeader^  columnHeader4;
410
private: System::ComponentModel::BackgroundWorker^  backgroundWorker2;
411
private: System::Windows::Forms::Button^  button2;
412
protected: 
413
 
414
		/// <summary>
415
		/// Clean up any resources being used.
416
		/// </summary>
417
		~CheckUpdate()
418
		{
419
			if (components)
420
			{
421
				delete components;
422
			}
162 cycrow 423
			delete _lAddress;
1 cycrow 424
		}
425
 
426
	private: System::Windows::Forms::ProgressBar^  progressBar1;
427
	private: System::ComponentModel::BackgroundWorker^  backgroundWorker1;
428
	private: System::Windows::Forms::ListView^  listView1;
429
	private: System::Windows::Forms::ColumnHeader^  columnHeader1;
430
	private: System::Windows::Forms::ColumnHeader^  columnHeader2;
431
	private: System::Windows::Forms::ColumnHeader^  columnHeader3;
432
	private: System::Windows::Forms::Panel^  panel4;
433
	private: System::Windows::Forms::Button^  button1;
434
	protected: 
435
 
436
	private:
437
		/// <summary>
438
		/// Required designer variable.
439
		/// </summary>
440
		System::ComponentModel::Container ^components;
441
 
442
#pragma region Windows Form Designer generated code
443
		/// <summary>
444
		/// Required method for Designer support - do not modify
445
		/// the contents of this method with the code editor.
446
		/// </summary>
447
		void InitializeComponent(void)
448
		{
449
			this->progressBar1 = (gcnew System::Windows::Forms::ProgressBar());
450
			this->backgroundWorker1 = (gcnew System::ComponentModel::BackgroundWorker());
451
			this->listView1 = (gcnew System::Windows::Forms::ListView());
452
			this->columnHeader1 = (gcnew System::Windows::Forms::ColumnHeader());
453
			this->columnHeader2 = (gcnew System::Windows::Forms::ColumnHeader());
454
			this->columnHeader3 = (gcnew System::Windows::Forms::ColumnHeader());
455
			this->columnHeader4 = (gcnew System::Windows::Forms::ColumnHeader());
456
			this->panel4 = (gcnew System::Windows::Forms::Panel());
457
			this->button2 = (gcnew System::Windows::Forms::Button());
458
			this->button1 = (gcnew System::Windows::Forms::Button());
459
			this->backgroundWorker2 = (gcnew System::ComponentModel::BackgroundWorker());
460
			this->panel4->SuspendLayout();
461
			this->SuspendLayout();
462
			// 
463
			// progressBar1
464
			// 
465
			this->progressBar1->Dock = System::Windows::Forms::DockStyle::Bottom;
466
			this->progressBar1->Location = System::Drawing::Point(20, 192);
467
			this->progressBar1->MarqueeAnimationSpeed = 10;
468
			this->progressBar1->Name = L"progressBar1";
469
			this->progressBar1->Size = System::Drawing::Size(593, 31);
470
			this->progressBar1->Step = 1;
471
			this->progressBar1->Style = System::Windows::Forms::ProgressBarStyle::Continuous;
472
			this->progressBar1->TabIndex = 3;
473
			// 
474
			// backgroundWorker1
475
			// 
476
			this->backgroundWorker1->WorkerReportsProgress = true;
477
			this->backgroundWorker1->WorkerSupportsCancellation = true;
478
			this->backgroundWorker1->DoWork += gcnew System::ComponentModel::DoWorkEventHandler(this, &CheckUpdate::backgroundWorker1_DoWork);
479
			this->backgroundWorker1->RunWorkerCompleted += gcnew System::ComponentModel::RunWorkerCompletedEventHandler(this, &CheckUpdate::backgroundWorker1_RunWorkerCompleted);
480
			// 
481
			// listView1
482
			// 
483
			this->listView1->CheckBoxes = true;
484
			this->listView1->Columns->AddRange(gcnew cli::array< System::Windows::Forms::ColumnHeader^  >(4) {this->columnHeader1, this->columnHeader2, 
485
				this->columnHeader3, this->columnHeader4});
486
			this->listView1->Dock = System::Windows::Forms::DockStyle::Fill;
487
			this->listView1->FullRowSelect = true;
488
			this->listView1->HideSelection = false;
489
			this->listView1->Location = System::Drawing::Point(20, 20);
490
			this->listView1->MultiSelect = false;
491
			this->listView1->Name = L"listView1";
492
			this->listView1->ShowItemToolTips = true;
493
			this->listView1->Size = System::Drawing::Size(593, 172);
494
			this->listView1->Sorting = System::Windows::Forms::SortOrder::Ascending;
495
			this->listView1->TabIndex = 7;
496
			this->listView1->UseCompatibleStateImageBehavior = false;
497
			this->listView1->UseWaitCursor = true;
498
			this->listView1->View = System::Windows::Forms::View::Details;
499
			this->listView1->Click += gcnew System::EventHandler(this, &CheckUpdate::listView1_Click);
500
			// 
501
			// columnHeader1
502
			// 
503
			this->columnHeader1->Text = L"Package";
504
			this->columnHeader1->Width = 200;
505
			// 
506
			// columnHeader2
507
			// 
508
			this->columnHeader2->Text = L"Author";
509
			this->columnHeader2->Width = 50;
510
			// 
511
			// columnHeader3
512
			// 
513
			this->columnHeader3->Text = L"Version";
514
			// 
515
			// columnHeader4
516
			// 
517
			this->columnHeader4->Text = L"Status";
518
			// 
519
			// panel4
520
			// 
521
			this->panel4->Controls->Add(this->button2);
522
			this->panel4->Controls->Add(this->button1);
523
			this->panel4->Dock = System::Windows::Forms::DockStyle::Bottom;
524
			this->panel4->Location = System::Drawing::Point(20, 223);
525
			this->panel4->Name = L"panel4";
526
			this->panel4->Padding = System::Windows::Forms::Padding(10);
527
			this->panel4->Size = System::Drawing::Size(593, 55);
528
			this->panel4->TabIndex = 8;
529
			// 
530
			// button2
531
			// 
532
			this->button2->Dock = System::Windows::Forms::DockStyle::Left;
533
			this->button2->Location = System::Drawing::Point(10, 10);
534
			this->button2->Name = L"button2";
535
			this->button2->Size = System::Drawing::Size(132, 35);
536
			this->button2->TabIndex = 1;
537
			this->button2->Text = L"Start Check";
538
			this->button2->UseVisualStyleBackColor = true;
539
			this->button2->Click += gcnew System::EventHandler(this, &CheckUpdate::button2_Click);
540
			// 
541
			// button1
542
			// 
543
			this->button1->DialogResult = System::Windows::Forms::DialogResult::Cancel;
544
			this->button1->Dock = System::Windows::Forms::DockStyle::Right;
545
			this->button1->Location = System::Drawing::Point(486, 10);
546
			this->button1->Name = L"button1";
547
			this->button1->Size = System::Drawing::Size(97, 35);
548
			this->button1->TabIndex = 0;
549
			this->button1->Text = L"Cancel";
550
			this->button1->UseVisualStyleBackColor = true;
551
			this->button1->Click += gcnew System::EventHandler(this, &CheckUpdate::button1_Click);
552
			// 
553
			// backgroundWorker2
554
			// 
555
			this->backgroundWorker2->WorkerReportsProgress = true;
556
			this->backgroundWorker2->WorkerSupportsCancellation = true;
557
			this->backgroundWorker2->DoWork += gcnew System::ComponentModel::DoWorkEventHandler(this, &CheckUpdate::backgroundWorker2_DoWork);
558
			this->backgroundWorker2->RunWorkerCompleted += gcnew System::ComponentModel::RunWorkerCompletedEventHandler(this, &CheckUpdate::backgroundWorker2_RunWorkerCompleted);
559
			this->backgroundWorker2->ProgressChanged += gcnew System::ComponentModel::ProgressChangedEventHandler(this, &CheckUpdate::backgroundWorker2_ProgressChanged);
560
			// 
561
			// CheckUpdate
562
			// 
563
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
564
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
565
			this->ClientSize = System::Drawing::Size(633, 298);
566
			this->ControlBox = false;
567
			this->Controls->Add(this->listView1);
568
			this->Controls->Add(this->progressBar1);
569
			this->Controls->Add(this->panel4);
570
			this->Name = L"CheckUpdate";
571
			this->Padding = System::Windows::Forms::Padding(20);
572
			this->ShowInTaskbar = false;
573
			this->StartPosition = System::Windows::Forms::FormStartPosition::CenterParent;
574
			this->Text = L"Checking Updates";
575
			this->Load += gcnew System::EventHandler(this, &CheckUpdate::CheckUpdate_Load);
576
			this->panel4->ResumeLayout(false);
577
			this->ResumeLayout(false);
578
 
579
		}
580
#pragma endregion
581
	private: System::Void CheckUpdate_Load(System::Object^  sender, System::EventArgs^  e) {
582
				 this->CheckButton();
583
				 if ( m_pCurrentItem )
584
				 {
585
					if ( m_pPackage )
586
						this->StartCheck();
587
					else if ( m_bDownloader && m_pDownloadPackage )
588
						this->StartCheck();
589
				 }
590
			 }
591
private: System::Void backgroundWorker1_DoWork(System::Object^  sender, System::ComponentModel::DoWorkEventArgs^  e) {
592
			 // for each server
593
			 String ^bestServer = "";
224 cycrow 594
			 Utils::WString bestVersion = L"0.00";
1 cycrow 595
			 String ^bestFile = "";
596
 
162 cycrow 597
			 for(auto itr = _lAddress->begin(); itr != _lAddress->end(); itr++)
1 cycrow 598
			 {
599
				 if ( backgroundWorker1->CancellationPending )
600
					 break;
162 cycrow 601
				 String ^address = _US((*itr)->str);
1 cycrow 602
				 m_sData = "";
603
				 String ^url = address;
604
 
605
				 if ( !m_bDownloader )
606
					url += "/" + m_pCurrentItem->SubItems[4]->Text;
607
 
608
				 //int error = this->CheckFile(url);
609
				 int error = CheckWebFileExists(url);
610
 
611
				 if ( backgroundWorker1->CancellationPending )
612
					 break;
613
 
614
				 if ( m_bDownloader )
615
				 {
616
					 if ( error == CONNECTERROR_NONE )
617
					 {
618
						 m_sBestServer = url;
619
						 m_sStatus = "Ready";
620
						 return;
621
					 }
622
					 switch(error)
623
					 {
624
						case CONNECTERROR_NOFILE:
625
							m_sStatus = "File Not Found";
626
							break;
627
						case CONNECTERROR_TIMEOUT:
628
							m_sStatus = "Connection Timedout";
629
							break;
630
						case CONNECTERROR_FAILED:
631
							m_sStatus = "Failed";
632
							break;
633
					 }
634
				 }
635
 
636
				 if ( error == CONNECTERROR_NONE )
637
				 {
638
					 // file exists, lets download and examine it
639
					 if ( this->ReadData(url) )
640
					 {
641
						 if ( backgroundWorker1->CancellationPending )
642
							 break;
643
 
644
						// we have the file, parse data
645
						cli::array<String ^> ^lines = m_sData->Split('\n');
646
						if ( lines )
647
						{
224 cycrow 648
							 Utils::WString version;
649
							 Utils::WString file;
1 cycrow 650
							 for ( int i = 0; i < lines->Length; i++ )
651
							 {
224 cycrow 652
								 Utils::WString l = _WS(lines[i]).remove(9).remove('\r');
1 cycrow 653
								 //if ( !l.IsIn(":") ) continue;
224 cycrow 654
								 Utils::WString first = l.token(L":", 1);
655
								 if ( first.Compare(L"Version") )
656
									 version = l.tokens(L":", 2).removeFirstSpace();
657
								 else if ( first.Compare(L"File") )
658
									 file = l.tokens(L":", 2).removeFirstSpace();
1 cycrow 659
							 }
660
 
162 cycrow 661
							 if ( !version.empty() && !file.empty() )
1 cycrow 662
							 {
162 cycrow 663
								 int error = this->CheckFile(address + "/" + _US(file));
1 cycrow 664
								 if ( error == CONNECTERROR_NONE )
665
								 {
162 cycrow 666
									 if ( bestVersion.compareVersion(version) == COMPARE_NEWER )
1 cycrow 667
									 {
162 cycrow 668
										 bestServer = address + "/" + _US(file);
1 cycrow 669
										 bestVersion = version;
670
									 }
671
								 }
672
							 }
673
						 }
674
					}
675
				 }
676
			 }
677
 
678
			 if ( m_bDownloader )
679
				 return;
680
 
681
			 // now check if we have found an update
682
			 if ( bestServer->Length )
683
			 {
162 cycrow 684
				 int v = m_pPackage->version().compareVersion(bestVersion);
1 cycrow 685
				 switch ( v )
686
				 {
687
					case COMPARE_OLDER:
688
						m_sStatus = "Older";
689
						break;
690
					case COMPARE_SAME:
691
						m_sStatus = "Same";
692
						break;
693
					case COMPARE_NEWER:
162 cycrow 694
						m_sStatus = "Newer (" + _US(bestVersion) + ")";
1 cycrow 695
						m_sBestServer = bestServer;
696
						break;
697
				 }
698
			 }
699
			 else
700
			 {
701
				 if ( this->backgroundWorker1->CancellationPending )
702
					m_sStatus = "Cancelled";
703
				 else
704
					m_sStatus = "No Update";
705
			 }
706
		 }
707
private: System::Void backgroundWorker1_RunWorkerCompleted(System::Object^  sender, System::ComponentModel::RunWorkerCompletedEventArgs^  e) {
708
			 // update the status display
709
			 m_pCurrentItem->SubItems[3]->Text = m_sStatus;
710
			 if ( m_sBestServer && m_sBestServer->Length )
711
				 m_pCurrentItem->SubItems[5]->Text = m_sBestServer;
712
			 this->UpdateList();
713
 
714
			 // next package
715
			 this->CheckNextPackage();
716
		 }
717
private: System::Void listView1_Click(System::Object^  sender, System::EventArgs^  e) {
718
			 this->CheckButton();
719
		 }
720
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
721
			 if ( m_iStatus == 0 ) // start check
722
			 {
723
				 // remove all items that are not checked
724
				 for ( int i = this->listView1->Items->Count - 1; i >= 0; i-- )
725
				 {
726
					 if ( this->listView1->Items[i]->Checked )
727
						 continue;
728
					 this->listView1->Items->RemoveAt(i);
729
				 }
730
 
731
				 this->CheckNextPackage();
732
			 }
733
			 else if ( m_iStatus == 1 ) // start downloading
734
			 {
735
				 for ( int i = this->listView1->Items->Count - 1; i >= 0; i-- )
736
				 {
737
					 if ( this->listView1->Items[i]->SubItems[5]->Text->Length && this->listView1->Items[i]->Checked )
738
						 continue;
739
					 this->listView1->Items->RemoveAt(i);
740
				 }
741
 
742
				 this->UpdateNextPackage();
743
			 }
744
			 else if ( m_iStatus == 2 ) // start installing
745
			 {
746
				 //this->button2 
747
			 }
748
		 }
749
private: System::Void backgroundWorker2_DoWork(System::Object^  sender, System::ComponentModel::DoWorkEventArgs^  e) {
750
 
751
				m_iErrorState = ERROR_NONE;
752
 
753
				Threading::Thread::Sleep(1000);
754
				System::Net::WebResponse ^res = nullptr;
755
				System::Net::WebRequest ^req = nullptr;
756
				try {
757
					req = System::Net::WebRequest::Create(m_pCurrentItem->SubItems[5]->Text);
758
					req->Timeout = 25000;
759
					res = req->GetResponse();
760
				}
761
				catch (System::Net::WebException ^) {
762
					return;
763
				}
764
 
765
				if ( backgroundWorker2->CancellationPending )
766
					return;
767
 
162 cycrow 768
				System::String ^dir = GetDestiantion();
1 cycrow 769
				dir += "\\Downloads";
224 cycrow 770
				String ^file = dir + "\\" + _US(CFileIO(_WS(m_pCurrentItem->SubItems[5]->Text)).filename().remove('\r').remove(9));
1 cycrow 771
 
772
				__int64 maxSize = res->ContentLength;
773
				__int64 curSize = 0;
774
 
775
				System::IO::FileStream ^writeStream = nullptr;
776
				try {
777
					writeStream = gcnew System::IO::FileStream(file, System::IO::FileMode::OpenOrCreate);
778
				}
779
				catch (System::UnauthorizedAccessException ^) {
780
					m_iErrorState = ERROR_ACCESSDENIED;
781
					return;
782
				}
783
				catch (System::IO::IOException ^)
784
				{
785
					return;
786
				}
787
				catch (System::Exception ^e)
788
				{
789
					MessageBox::Show("Error: " + e->ToString(), "Error", MessageBoxButtons::OK, MessageBoxIcon::Error);
790
					return;
791
				}
792
 
793
				do
794
				{
795
					if ( backgroundWorker2->CancellationPending )
796
						break;
797
 
798
					Threading::Thread::Sleep(20);
799
 
800
					array<unsigned char> ^readBytes = gcnew array<unsigned char>(4096);
801
					int read = res->GetResponseStream()->Read(readBytes, 0, 4096);
802
 
803
					curSize += (__int64)read;
804
 
805
					int percent = (int)((curSize * 100) / maxSize);
806
					backgroundWorker2->ReportProgress(percent);
807
 
808
					if ( read <= 0 )
809
						break;
810
 
811
					writeStream->Write(readBytes, 0, read);
812
				}
813
				while(1);
814
 
815
				res->GetResponseStream()->Close();
816
				writeStream->Close();
817
 
818
				Threading::Thread::Sleep(1000);
819
 
820
				if ( backgroundWorker2->CancellationPending )
821
				{
822
					try {
823
						System::IO::File::Delete(file);
824
					}
825
					catch (System::IO::IOException ^)
826
					{
827
					}
828
					catch (System::Exception ^)
829
					{
830
					}
831
				}
832
		 }
833
private: System::Void backgroundWorker2_ProgressChanged(System::Object^  sender, System::ComponentModel::ProgressChangedEventArgs^  e) {
834
			this->progressBar1->Style = Windows::Forms::ProgressBarStyle::Continuous;
835
 			this->progressBar1->Value = e->ProgressPercentage;
836
		 }
837
private: System::Void backgroundWorker2_RunWorkerCompleted(System::Object^  sender, System::ComponentModel::RunWorkerCompletedEventArgs^  e) {
838
			if ( this->backgroundWorker2->CancellationPending )
839
			{
840
				 m_pCurrentItem->SubItems[3]->Text = "Cancelled";
841
				 m_pCurrentItem->SubItems[5]->Text = "";
842
			}
843
			else
844
			{
162 cycrow 845
				System::String ^dir = GetDestiantion();
1 cycrow 846
				dir += "\\Downloads";
224 cycrow 847
				String ^file = dir + "\\" + _US(CFileIO(_WS(m_pCurrentItem->SubItems[5]->Text)).filename().remove('\r').remove(9));
1 cycrow 848
 
849
				 if ( IO::File::Exists(file) )
850
				 {
851
					 m_pCurrentItem->SubItems[3]->Text = "Ready to Install";
852
					 m_pCurrentItem->SubItems[5]->Text = file;
853
					 m_lInstall->Add(file);
854
				 }
855
				 else
856
				 {
857
					 switch ( m_iErrorState ) {
858
						 case ERROR_ACCESSDENIED:
859
							 m_pCurrentItem->SubItems[3]->Text = "Access Denied";
860
							 break;
861
						 default:
862
							 m_pCurrentItem->SubItems[3]->Text = "Failed";
863
					 }
864
					 m_pCurrentItem->SubItems[5]->Text = "";
865
				 }
866
				 this->UpdateList();
867
				 this->UpdateNextPackage();
868
			}
869
		 }
870
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
871
			 this->backgroundWorker1->CancelAsync();
872
			 this->backgroundWorker2->CancelAsync();
873
		 }
874
};
875
}