-
Notifications
You must be signed in to change notification settings - Fork 0
/
FTPManager.cs
1924 lines (1641 loc) · 81 KB
/
FTPManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;
using System.Collections;
using System.Runtime.InteropServices;
using System.IO;
namespace FTPManager
{
public partial class FTPManager : UserControl
{
/***************************************************** Variables *****************************************************/
#region Variables
private FtpAPI _FtpAPI; // FTP API
private Directory _Root; // Root directory.
private Directory _CurrentDirectory; // Current directory.
private Region _ConnectedRegion; // Connected region.
private List<File> _FilesToDownload; // List of files to download.
private string _LocalFilePath; // Local path (where download the file)
private List<Region> _RegionsList; // List of regions (fetched from the db).
private ToolTip _TargetPathTooltip; // Tooltpip.
private BackgroundWorker _Worker; // Background worker.
private ToolTip _SynchronizeButtonTooltip; // Another tooltip.
#endregion
/**************************************************** Constructor ****************************************************/
#region Constructor
public FTPManager()
{
InitializeComponent();
_FtpAPI = new FtpAPI();
_Root = new Directory();
_CurrentDirectory = new Directory();
_FilesToDownload = new List<File>();
_ConnectedRegion = new Region();
_RegionsList = new List<Region>();
_TargetPathTooltip = new ToolTip();
_TargetPathTooltip.SetToolTip(TargetDirectoryButton, "");
AddButton.Click += AddButton_Click;
EditButton.Click += EditButton_Click;
GetRegions();
FillFTPTreeView(); //FillFtpListDataGridView();
ResetSummaryTextboxesAndLabels();
_Worker = new BackgroundWorker();
_Worker.WorkerReportsProgress = true;
_Worker.WorkerSupportsCancellation = true;
_Worker.DoWork += Worker_DoWork;
_Worker.RunWorkerCompleted += Worker_RunWorkerCompleted;
_Worker.ProgressChanged += Worker_ProgressChanged;
_SynchronizeButtonTooltip = new ToolTip();
_SynchronizeButtonTooltip.SetToolTip(SynchronizeLocalFileButton, "Download all files in the FTP which are more recent than the more recent file in your selected region's local folder.");
}
#endregion
/******************************** * FTP Creation/Edition/Suppression Methods *****************************************/
#region CreationMethods
/*******************************************\
* Start editing by make Textboxes visible *
* - Case of FTP creation *
* - Case of FTP edition *
\*******************************************/
private void AddButton_Click(object sender, EventArgs e)
{
BeginCreateFTP();
}
private void EditButton_Click(object sender, EventArgs e)
{
BeginEditFTP();
}
private void BeginCreateFTP()
{
ServerAddressLabel2.Visible = false;
ServerAddressTextBox.Visible = true;
RegionNameLabel2.Visible = false;
RegionNameTextBox.Visible = true;
ProviderLabel2.Visible = false;
ProviderTextBox.Visible = true;
LoginLabel2.Visible = false;
LoginTextBox.Visible = true;
PasswordLabel2.Visible = false;
PasswordTextBox.Visible = true;
_TargetPathTooltip.SetToolTip(TargetDirectoryButton, "");
TargetDirectoryButton.Tag = true;
TargetDirectoryButton.Visible = true;
TargetDirectoryLabel2.Visible = false;
FileMaskLabel2.Visible = false;
FileMaskTextBox.Visible = true;
RecoveryFrequencyLabel2.Visible = false;
RecoveryFrequencyComboBox.Visible = true;
RecoveryFrequencyComboBox.SelectedIndex = -1;
RecoveryDayLabel2.Visible = false;
RecoveryDayComboBox.Visible = true;
RecoveryDayComboBox.SelectedIndex = -1;
OkPictureBox.Visible = true;
OkPictureBox.Tag = "";
BackButton.Visible = true;
AddButton.Enabled = ComponentFactory.Krypton.Toolkit.ButtonEnabled.False;
DeleteButton.Enabled = ComponentFactory.Krypton.Toolkit.ButtonEnabled.False;
EditButton.Enabled = ComponentFactory.Krypton.Toolkit.ButtonEnabled.False;
ConnectButton.Enabled = ComponentFactory.Krypton.Toolkit.ButtonEnabled.False;
DLLastUpdateButton.Enabled = false;
SynchronizeLocalFileButton.Enabled = false;
FTPTreeView.SelectedNode = null;//FTPListDataGridView.ClearSelection();
}
private void BeginEditFTP()
{
//if (FTPListDataGridView.CurrentCell.RowIndex != -1)
//{
/* Retrieve region information */
Region regionToUpdate = new Region();
foreach (Region element in _RegionsList)
{
if (element.Get_RegionName().Equals(FTPTreeView.SelectedNode.Text.ToString())) //if (element.Get_RegionName().Equals(FTPListDataGridView.CurrentCell.Value.ToString()))
{
regionToUpdate = element;
break;
}
}
/* Show/Hide controls */
ServerAddressLabel2.Visible = false;
ServerAddressTextBox.Visible = true;
ServerAddressTextBox.Text = regionToUpdate.Get_FtpHost();
RegionNameLabel2.Visible = false;
RegionNameTextBox.Visible = true;
RegionNameTextBox.Text = regionToUpdate.Get_RegionName();
ProviderLabel2.Visible = false;
ProviderTextBox.Visible = true;
ProviderTextBox.Text = regionToUpdate.Get_Provider();
LoginLabel2.Visible = false;
LoginTextBox.Visible = true;
LoginTextBox.Text = regionToUpdate.Get_Login();
PasswordLabel2.Visible = false;
PasswordTextBox.Visible = true;
PasswordTextBox.Text = regionToUpdate.Get_Password();
_TargetPathTooltip.SetToolTip(TargetDirectoryButton, regionToUpdate.Get_TargetDirectory());
TargetDirectoryButton.Tag = true;
TargetDirectoryButton.Visible = true;
TargetDirectoryLabel2.Visible = false;
FileMaskLabel2.Visible = false;
FileMaskTextBox.Visible = true;
FileMaskTextBox.Text = regionToUpdate.Get_FileMask();
RecoveryFrequencyLabel2.Visible = false;
RecoveryFrequencyComboBox.Visible = true;
RecoveryFrequencyComboBox.Text = regionToUpdate.Get_RecoveryFrequency();
RecoveryDayLabel2.Visible = false;
RecoveryDayComboBox.Visible = true;
RecoveryDayComboBox.Text = regionToUpdate.Get_RecoveryDay();
OkPictureBox.Visible = true;
OkPictureBox.Tag = regionToUpdate.Get_RegionName();
BackButton.Visible = true;
AddButton.Enabled = ComponentFactory.Krypton.Toolkit.ButtonEnabled.False;
DeleteButton.Enabled = ComponentFactory.Krypton.Toolkit.ButtonEnabled.False;
EditButton.Enabled = ComponentFactory.Krypton.Toolkit.ButtonEnabled.False;
ConnectButton.Enabled = ComponentFactory.Krypton.Toolkit.ButtonEnabled.False;
DLLastUpdateButton.Enabled = false;
SynchronizeLocalFileButton.Enabled = false;
FTPTreeView.SelectedNode = null; //FTPListDataGridView.ClearSelection();
//}
}
/************************************\
* Back button click (in edit mode) *
\************************************/
private void BackButton_Click(object sender, EventArgs e)
{
EndEdit();
}
private void EndEdit()
{
ResetSummaryTextboxesAndLabels();
TargetDirectoryButton.Tag = false;
ServerAddressLabel2.Visible = true;
ServerAddressTextBox.Visible = false;
TargetDirectoryButton.Visible = false;
TargetDirectoryLabel2.Visible = true;
RegionNameLabel2.Visible = true;
RegionNameTextBox.Visible = false;
ProviderLabel2.Visible = true;
ProviderTextBox.Visible = false;
LoginLabel2.Visible = true;
LoginTextBox.Visible = false;
PasswordLabel2.Visible = true;
PasswordTextBox.Visible = false;
FileMaskLabel2.Visible = true;
FileMaskTextBox.Visible = false;
RecoveryFrequencyLabel2.Visible = true;
RecoveryFrequencyComboBox.Visible = false;
RecoveryDayLabel2.Visible = true;
RecoveryDayComboBox.Visible = false;
OkPictureBox.Visible = false;
BackButton.Visible = false;
AddButton.Enabled = ComponentFactory.Krypton.Toolkit.ButtonEnabled.True;
DLLastUpdateButton.Enabled = false;
SynchronizeLocalFileButton.Enabled = false;
FTPTreeView.SelectedNode = null; //FTPListDataGridView.ClearSelection();
}
/********************************************************\
* Create FTP (OK button click) *
* - Create/Edit FTP (depending on the context) *
* - CheckFields : check if mandatory fields are filled *
\********************************************************/
private void OkPictureBox_Click(object sender, EventArgs e)
{
/* Check if fields are OK */
if (CheckFields())
{
/* Create service */
FTPWebService.ftpSoapClient service = new FTPWebService.ftpSoapClient();
/* CASE 1 : CREATION */
if (((string)OkPictureBox.Tag).Equals(""))
{
/* Check if server exists (Provider|Server Adress|Login|Password) */
int serverID = service.Get_server_id(ProviderTextBox.Text, ServerAddressTextBox.Text, LoginTextBox.Text, PasswordTextBox.Text);
if (serverID == 0)
{
/* Create server if not exists then get id_server */
service.Add_server(ProviderTextBox.Text, ServerAddressTextBox.Text, LoginTextBox.Text, PasswordTextBox.Text);
serverID = service.Get_server_id(ProviderTextBox.Text, ServerAddressTextBox.Text, LoginTextBox.Text, PasswordTextBox.Text);
}
/* Set idFrequency and idDay */
int idFrequency;
int idDay;
string frequency;
string day;
switch (RecoveryFrequencyComboBox.Text)
{
case "None": idFrequency = 5; frequency = "None"; break;
case "Daily": idFrequency = 1; frequency = "Daily"; break;
case "Weekly": idFrequency = 2; frequency = "Weekly"; break;
case "Monthly": idFrequency = 3; frequency = "Monthly"; break;
case "Yearly": idFrequency = 4; frequency = "Yearly"; break;
default: idFrequency = 5; frequency = "None"; break;
}
switch (RecoveryDayComboBox.Text)
{
case "None": idDay = 8; day = "None"; break;
case "Monday": idDay = 1; day = "Monday"; break;
case "Tuesday": idDay = 2; day = "Tuesday"; break;
case "Wednesday": idDay = 3; day = "Wednesday"; break;
case "Thursday": idDay = 4; day = "Thursday"; break;
case "Friday": idDay = 5; day = "Friday"; break;
case "Saturday": idDay = 6; day = "Saturday"; break;
case "Sunday": idDay = 7; day = "Sunday"; break;
default: idDay = 8; day = "None"; break;
}
/* Insert region */
service.Add_region(serverID, RegionNameTextBox.Text, FileMaskTextBox.Text, _TargetPathTooltip.GetToolTip(TargetDirectoryButton), idFrequency, idDay);
/* Add new region to the list */
_RegionsList.Add(new Region(ProviderTextBox.Text, ServerAddressTextBox.Text, LoginTextBox.Text, PasswordTextBox.Text, RegionNameTextBox.Text, _TargetPathTooltip.GetToolTip(TargetDirectoryButton), FileMaskTextBox.Text, frequency, day));
/* Refresh List of FTP */
//FTPListDataGridView.DataSource = null;
FillFTPTreeView();//FillFtpListDataGridView();
/* Display result */
KryptonMessageBox.Show("FTP Created !", "Saved",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
/* CASE 2 : EDIT */
else if (!((string)OkPictureBox.Tag).Equals(""))
{
/* Check if server exists (Provider|Server Adress|Login|Password) */
int serverID = service.Get_server_id(ProviderTextBox.Text, ServerAddressTextBox.Text, LoginTextBox.Text, PasswordTextBox.Text);
if (serverID == 0)
{
/* Create server if not exists then get id_server */
service.Add_server(ProviderTextBox.Text, ServerAddressTextBox.Text, LoginTextBox.Text, PasswordTextBox.Text);
serverID = service.Get_server_id(ProviderTextBox.Text, ServerAddressTextBox.Text, LoginTextBox.Text, PasswordTextBox.Text);
}
/* Set idFrequency and idDay */
int idFrequency;
int idDay;
switch (RecoveryFrequencyComboBox.Text)
{
case "None": idFrequency = 5; break;
case "Daily": idFrequency = 1; break;
case "Weekly": idFrequency = 2; break;
case "Monthly": idFrequency = 3; break;
case "Yearly": idFrequency = 4; break;
default: idFrequency = 5; break;
}
switch (RecoveryDayComboBox.Text)
{
case "None": idDay = 8; break;
case "Monday": idDay = 1; break;
case "Tuesday": idDay = 2; break;
case "Wednesday": idDay = 3; break;
case "Thursday": idDay = 4; break;
case "Friday": idDay = 5; break;
case "Saturday": idDay = 6; break;
case "Sunday": idDay = 7; break;
default: idDay = 8; break;
}
/* Update region */
service.Update_region((string)OkPictureBox.Tag, serverID, RegionNameTextBox.Text, FileMaskTextBox.Text, _TargetPathTooltip.GetToolTip(TargetDirectoryButton), idFrequency, idDay);
/* Remove old region from the list */
foreach (Region element in _RegionsList)
{
if (element.Get_RegionName().Equals((string)OkPictureBox.Tag))
{
_RegionsList.Remove(element);
break;
}
}
/* Add new one */
_RegionsList.Add(new Region(ProviderTextBox.Text, ServerAddressTextBox.Text, LoginTextBox.Text, PasswordTextBox.Text, RegionNameTextBox.Text, _TargetPathTooltip.GetToolTip(TargetDirectoryButton), FileMaskTextBox.Text, RecoveryFrequencyComboBox.Text, RecoveryDayComboBox.Text));
/* Refresh List of FTP */
//FTPListDataGridView.DataSource = null;
FillFTPTreeView();//FillFtpListDataGridView();
/* Display result */
KryptonMessageBox.Show("FTP updated !", "Updated",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
/* Finally, End Edit */
EndEdit();
/* Close service */
service.Close();
}
}
private Boolean CheckFields()
{
/* Check server address field validity */
if (!ServerAddressTextBox.Text.StartsWith("ftp://"))
{
KryptonMessageBox.Show("Server address must start with \"ftp://\"", "Server address incorrect",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
return false;
}
/* Check empty fields */
if (ServerAddressTextBox.Text.Equals("") || RegionNameTextBox.Text.Equals("") || LoginTextBox.Text.Equals("") || PasswordTextBox.Text.Equals(""))
{
KryptonMessageBox.Show("Please fill at least the server address, the region name, the login and the password.", "Please fill fields.",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
return false;
}
/* Check region name (if it doesn't already exists) */
foreach (Region element in _RegionsList)
{
if ((RegionNameTextBox.Text.Equals(element.Get_RegionName()) && ((string)OkPictureBox.Tag).Equals("")) || ((RegionNameTextBox.Text.Equals(element.Get_RegionName()) && !((string)OkPictureBox.Tag).Equals(RegionNameTextBox.Text))))
{
KryptonMessageBox.Show("Region name already exists !!!", "Wrong region name",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
return false;
}
}
return true;
}
/****************************************\
* Delete FTP button click event *
* - Delete selected region from DB. *
\****************************************/
private void DeleteButton_Click(object sender, EventArgs e)
{
/* Create service */
FTPWebService.ftpSoapClient service = new FTPWebService.ftpSoapClient();
/* Delete region from DB */
service.Delete_region(FTPTreeView.SelectedNode.Text.ToString()); //service.Delete_region(FTPListDataGridView.CurrentCell.Value.ToString());
/* Delete region in list of region */
foreach (Region element in _RegionsList)
{
if (element.Get_RegionName().Equals(FTPTreeView.SelectedNode.Text.ToString())) //if (element.Get_RegionName().Equals(FTPListDataGridView.CurrentCell.Value.ToString()))
{
_RegionsList.Remove(element);
break;
}
}
/* Refresh DataGridView */
//FTPListDataGridView.DataSource = null;
FillFTPTreeView();//FillFtpListDataGridView();
FTPTreeView.SelectedNode = null;//FTPListDataGridView.ClearSelection();
/* CLose service */
service.Close();
/* Display result */
KryptonMessageBox.Show("Region Deleted !", "Deleted",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
/****************************************\
* Duplicate contextMenuItemClick event *
* - Duplicate selected region *
\****************************************/
private void DuplicateToolStripMenuItem_Click(object sender, EventArgs e)
{
if (FTPTreeView.SelectedNode != null && FTPTreeView.SelectedNode.Nodes.Count == 0)
{
Region duplicatedRegion = new Region();
/* Create service */
FTPWebService.ftpSoapClient service = new FTPWebService.ftpSoapClient();
/* Get region to duplicate */
Region regionToDuplicate = new Region();
foreach (Region element in _RegionsList)
{
if (element.Get_RegionName().Equals(FTPTreeView.SelectedNode.Text.ToString())) //if (element.Get_RegionName().Equals(FTPListDataGridView.CurrentCell.Value))
{
regionToDuplicate = element;
break;
}
}
/* Clone it */
duplicatedRegion.Clone(regionToDuplicate);
duplicatedRegion.Set_RegionNameForClone();
/* Add it to the list of regions */
_RegionsList.Add(duplicatedRegion);
/* Add it in DB */
int serverID = service.Get_server_id(duplicatedRegion.Get_Provider(), duplicatedRegion.Get_FtpHost(), duplicatedRegion.Get_Login(), duplicatedRegion.Get_Password());
int idFrequency;
int idDay;
switch (duplicatedRegion.Get_RecoveryFrequency())
{
case "None": idFrequency = 5; break;
case "Daily": idFrequency = 1; break;
case "Weekly": idFrequency = 2; break;
case "Monthly": idFrequency = 3; break;
case "Yearly": idFrequency = 4; break;
default: idFrequency = 5; break;
}
switch (duplicatedRegion.Get_RecoveryDay())
{
case "None": idDay = 8; break;
case "Monday": idDay = 1; break;
case "Tuesday": idDay = 2; break;
case "Wednesday": idDay = 3; break;
case "Thursday": idDay = 4; break;
case "Friday": idDay = 5; break;
case "Saturday": idDay = 6; break;
case "Sunday": idDay = 7; break;
default: idDay = 8; break;
}
service.Add_region(serverID, duplicatedRegion.Get_RegionName(), duplicatedRegion.Get_FileMask(), duplicatedRegion.Get_TargetDirectory(), idFrequency, idDay);
/* Refresh List of FTP */
//FTPListDataGridView.DataSource = null;
FillFTPTreeView(); //FillFtpListDataGridView();
FTPTreeView.SelectedNode = null; //FTPListDataGridView.ClearSelection();
/* Display result */
KryptonMessageBox.Show("Region Duplicated !", "Cloned",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
/* Close service */
service.Close();
}
}
/************************************\
* Target Path button click event : *
* - Define target path *
\************************************/
private void TargetDirectoryButton_Click(object sender, EventArgs e)
{
if ((Boolean)TargetDirectoryButton.Tag == true)
{
FolderBrowserDialog openFolderDialog = new FolderBrowserDialog();
openFolderDialog.SelectedPath = @"V:\DONNEES\INTERNATIONAL\DATA";
DialogResult result = openFolderDialog.ShowDialog();
if (result == DialogResult.OK)
{
_TargetPathTooltip.SetToolTip(TargetDirectoryButton, openFolderDialog.SelectedPath);
var result2 = KryptonMessageBox.Show("Path defined at \n" + _TargetPathTooltip.GetToolTip(TargetDirectoryButton), "Path Defined",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
}
}
/**************************************\
* Reset summary textboxes and labels *
\**************************************/
private void ResetSummaryTextboxesAndLabels()
{
PasswordLabel2.Text = "";
LoginLabel2.Text = "";
ProviderLabel2.Text = "";
RecoveryDayLabel2.Text = "";
RecoveryFrequencyLabel2.Text = "";
RegionNameLabel2.Text = "";
ServerAddressLabel2.Text = "";
FileMaskLabel2.Text = "";
PasswordTextBox.Text = "";
LoginTextBox.Text = "";
ProviderTextBox.Text = "";
RecoveryFrequencyComboBox.Text = "";
RecoveryDayComboBox.Text = "";
RecoveryDayComboBox.SelectedIndex = -1;
RecoveryFrequencyComboBox.SelectedIndex = -1;
RegionNameTextBox.Text = "";
ServerAddressTextBox.Text = "";
FileMaskTextBox.Text = "";
_TargetPathTooltip.SetToolTip(TargetDirectoryButton, "");
TargetDirectoryButton.Tag = false;
TargetDirectoryButton.Visible = false;
TargetDirectoryLabel2.Text = "";
OkPictureBox.Tag = false;
}
#endregion
/******************************************* Methods driving the FTP API ***********************************************/
#region UsingFtpAPIMethods
/*************************************************************\
* "Connect" to FTP *
* -> FTP Query that get files/folders of indicated path. *
* -> Fill TreeView. *
\*************************************************************/
private void ConnectButton_Click(object sender, EventArgs e)
{
Connect();
}
private void Connect()
{
ConnectButton.Enabled = ComponentFactory.Krypton.Toolkit.ButtonEnabled.False;
StopButton.Enabled = ButtonEnabled.True;
DLLastUpdateButton.Enabled = false;
SynchronizeLocalFileButton.Enabled = false;
FileExplorerListView.SetSortIcon(0, SortOrder.None);
FileExplorerListView.ListViewItemSorter = new ListViewItemComparer(0, SortOrder.Ascending);
DirFileCounterLabel.Text = "";
/* Reset current directory */
_Root = new Directory();
/* Get region to connect */
Region regionToConnect = new Region();
foreach (Region element in _RegionsList)
{
if (element.Get_RegionName().Equals(FTPTreeView.SelectedNode.Text.ToString()))//if (element.Get_RegionName().Equals(FTPListDataGridView.CurrentCell.Value))
{
regionToConnect = element;
break;
}
}
_ConnectedRegion = regionToConnect;
/* Set credentials & current folder */
_FtpAPI.Set_Host(regionToConnect.Get_FtpHost());
_FtpAPI.Set_Root(regionToConnect.Get_FtpHost());
_FtpAPI.Set_User(regionToConnect.Get_Login());
_FtpAPI.Set_Password(regionToConnect.Get_Password());
_Root.Set_IsRoot(true);
_Root.Set_Name("Root");
_Root.Set_Path(regionToConnect.Get_FtpHost());
/* Start backgroundworker to get files*/
FTPBrowserHeaderGroup.ValuesPrimary.Heading = "FTP Browser (" + regionToConnect.Get_RegionName() + ") :";
LogcatRichTextBox.AppendText("Connecting to " + regionToConnect.Get_RegionName() + " : " + regionToConnect.Get_FtpHost() + "... ");
FileExplorerListView.Items.Clear();
List<object> arguments = new List<object>();
arguments.Add("GetDir");
_Worker.RunWorkerAsync(arguments);
}
/*******************************************************\
* Change directory *
* -> FTP Query that get files/folders of new path. *
* -> Fill TreeView. *
\*******************************************************/
private void ChangeDirectory(Directory dirToGo)
{
StopButton.Enabled = ButtonEnabled.True;
/* Set current directory */
_CurrentDirectory = dirToGo;
/* Modify credential (host) */
_FtpAPI.Set_Host(_CurrentDirectory.Get_Path());
/* Start backgroundworker to get files*/
LogcatRichTextBox.AppendText("Changing directory to : " + _CurrentDirectory.Get_Path() + "... ");
FileExplorerListView.Items.Clear();
List<object> arguments = new List<object>();
arguments.Add("ChDir");
_Worker.RunWorkerAsync(arguments);
}
/**************************************\
* Download selected item in ListView *
\**************************************/
private void DownloadButton_Click(object sender, EventArgs e)
{
DownloadButton.Enabled = ButtonEnabled.False;
DLLastUpdateButton.Enabled = false;
SynchronizeLocalFileButton.Enabled = false;
/* Clear list of files to dl */
_FilesToDownload.Clear();
/* Get item to download */
foreach (File file in _CurrentDirectory.Get_FilesList())
{
if (FileExplorerListView.FocusedItem.Text.Equals(file.Get_Name()) || FileExplorerListView.FocusedItem.Text.Equals(file.Get_Name().Replace("%20"," ")))
{
_FilesToDownload.Add(file);
_FtpAPI.Set_Total_Size(file.Get_Size());
break;
}
}
/* If the target directory has been specified, directly download the file */
if (!String.IsNullOrEmpty(_ConnectedRegion.Get_TargetDirectory()) && System.IO.Directory.Exists(_ConnectedRegion.Get_TargetDirectory()))
{
StopButton.Enabled = ButtonEnabled.True;
//_LocalFilePath = _ConnectedRegion.Get_TargetDirectory() + "\\" + _FileToDownload.Get_Name();
_LocalFilePath = _ConnectedRegion.Get_TargetDirectory() + "\\" + _FilesToDownload[0].Get_Name().Replace("%20", " ");
/* Start Backgroundworker to download the file */
LogcatRichTextBox.AppendText("Downloading " + _FilesToDownload[0].Get_Name().Replace("%20", " ") + "... ");
List<object> arguments = new List<object>();
arguments.Add("Download");
_Worker.RunWorkerAsync(arguments);
}
/* If the target directory is not specified, ask the user where he wants to download the file */
else
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.AddExtension = true;
saveFileDialog.InitialDirectory = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop);
//saveFileDialog.FileName = _FileToDownload.Get_Name().Replace("%20", " ");
saveFileDialog.FileName = _FilesToDownload[0].Get_Name().Replace("%20", " ");
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
StopButton.Enabled = ButtonEnabled.True;
_LocalFilePath = System.IO.Path.GetFullPath(saveFileDialog.FileName);
/* Start Backgroundworker to download the file */
//LogcatRichTextBox.AppendText("Downloading " + _FileToDownload.Get_Name() + "... ");
LogcatRichTextBox.AppendText("Downloading " + _FilesToDownload[0].Get_Name() + "... ");
List<object> arguments = new List<object>();
arguments.Add("Download");
_Worker.RunWorkerAsync(arguments);
}
}
}
/*******************************************************************************\
* Download last update of selected region. 2 methods : *
* - The first one is when the user click on the button to dl last updates. *
* - The second one is part of the Analytics batch *
\*******************************************************************************/
private void DLLastUpdateButton_Click(object sender, EventArgs e)
{
if (!_Worker.IsBusy)
{
StopButton.Enabled = ButtonEnabled.True;
/* Clear interface */
ProgressBar.Value = 0;
ProgressLabel.Text = "-";
DirFileCounterLabel.Text = "";
FTPBrowserHeaderGroup.ValuesPrimary.Heading = "FTP Browser :";
/* Clear list of files to dl */
_FilesToDownload.Clear();
/* Reset root */
_Root = new Directory();
/* Get region to connect */
Region regionToConnect = new Region();
foreach (Region element in _RegionsList)
{
if (element.Get_RegionName().Equals(FTPTreeView.SelectedNode.Text.ToString()))//if (element.Get_RegionName().Equals(FTPListDataGridView.CurrentCell.Value))
{
regionToConnect = element;
break;
}
}
/* Set credentials & current folder */
_ConnectedRegion = regionToConnect;
_FtpAPI.Set_Host(regionToConnect.Get_FtpHost());
_FtpAPI.Set_Root(regionToConnect.Get_FtpHost());
_FtpAPI.Set_User(regionToConnect.Get_Login());
_FtpAPI.Set_Password(regionToConnect.Get_Password());
_Root.Set_IsRoot(true);
_Root.Set_Name("Root");
_Root.Set_Path(regionToConnect.Get_FtpHost());
/* Start backgroundworker to get files */
LogcatRichTextBox.AppendText("Connecting to " + regionToConnect.Get_RegionName() + " : " + regionToConnect.Get_FtpHost() + "... ");
List<object> arguments = new List<object>();
arguments.Add("DlLastUp");
_Worker.RunWorkerAsync(arguments);
}
}
public void DLLastUpdate(Region regionToConnect)
{
if (!_Worker.IsBusy)
{
StopButton.Enabled = ButtonEnabled.True;
/* Clear list of files to dl */
_FilesToDownload.Clear();
/* Reset root */
_Root = new Directory();
/* Set credentials & current folder */
_ConnectedRegion = regionToConnect;
_FtpAPI.Set_Host(regionToConnect.Get_FtpHost());
_FtpAPI.Set_Root(regionToConnect.Get_FtpHost());
_FtpAPI.Set_User(regionToConnect.Get_Login());
_FtpAPI.Set_Password(regionToConnect.Get_Password());
_Root.Set_IsRoot(true);
_Root.Set_Name("Root");
_Root.Set_Path(regionToConnect.Get_FtpHost());
/* Start backgroundworker to get files */
//LogcatRichTextBox.AppendText("Connecting to " + regionToConnect.Get_RegionName() + " : " + regionToConnect.Get_FtpHost() + "... ");
//List<object> arguments = new List<object>();
//arguments.Add("DlLastUp");
//_Worker.RunWorkerAsync(arguments);
}
}
/**********************************************************************\
* Synchronize local file with FTP server *
* - Get the newest file in local and Download all files in server *
* which are relatively more recent. *
\**********************************************************************/
private void SynchronizeLocalFileButton_Click(object sender, EventArgs e)
{
if (!_Worker.IsBusy)
{
/* Clear files to dl */
_FtpAPI.Reset_DownloadedFilesTarget();
_FilesToDownload.Clear();
/* Get region from list and check if its target directory has been defined */
Region regionToConnect = new Region();
foreach (Region element in _RegionsList)
{
if (element.Get_RegionName().Equals(FTPTreeView.SelectedNode.Text.ToString()))//if (element.Get_RegionName().Equals(FTPListDataGridView.CurrentCell.Value))
{
regionToConnect = element;
break;
}
}
if (String.IsNullOrEmpty(regionToConnect.Get_TargetDirectory()) || !System.IO.Directory.Exists(regionToConnect.Get_TargetDirectory()))
{
KryptonMessageBox.Show("Target directory not defined, or directory doesn't exist !", "Error while checking the target directory",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
else
{
/* Reset root */
_Root = new Directory();
/* Set credentials & current folder */
_ConnectedRegion = regionToConnect;
_FtpAPI.Set_Host(regionToConnect.Get_FtpHost());
_FtpAPI.Set_Root(regionToConnect.Get_FtpHost());
_FtpAPI.Set_User(regionToConnect.Get_Login());
_FtpAPI.Set_Password(regionToConnect.Get_Password());
_Root.Set_IsRoot(true);
_Root.Set_Name("Root");
_Root.Set_Path(regionToConnect.Get_FtpHost());
/* Start backgroundworker to get files*/
LogcatRichTextBox.AppendText("Connecting to " + regionToConnect.Get_RegionName() + " : " + regionToConnect.Get_FtpHost() + "... ");
List<object> arguments = new List<object>();
arguments.Add("SyncLF");
_Worker.RunWorkerAsync(arguments);
}
}
}
/*****************\
* Cancel Action *
\*****************/
private void StopButton_Click(object sender, EventArgs e)
{
StopButton.Enabled = ButtonEnabled.False;
/* Stop backgroundworker */
_Worker.CancelAsync();
}
#endregion
/*************************************************** Misc Methods *******************************************************/
#region MiscMethods
/*************************************\
* Get all regions from DB *
* - Query for getting all regions *
* - instanciate region objects *
\*************************************/
private void GetRegions()
{
try
{
FTPWebService.ftpSoapClient service = new FTPWebService.ftpSoapClient();
_RegionsList.Clear();
DataSet dataSet = service.Get_all_regions();
service.Close();
foreach (DataRow row in dataSet.Tables[0].Rows)
{
_RegionsList.Add(new Region(row["provider"].ToString(),
row["server_address"].ToString(),
row["login"].ToString(),
row["password"].ToString(),
row["name"].ToString(),
row["target_directory"].ToString(),
row["file_mask"].ToString(),
row["type"].ToString(),
row["day"].ToString()));
}
}
catch (Exception ex) { KryptonMessageBox.Show(ex.ToString()); }
}
/*******************************\
* Add download historic on DB *
\*******************************/
public void AddDownloadLog()
{
int regionID = 0;
int fileID = 0;
try
{
/* Create service */
FTPWebService.ftpSoapClient service = new FTPWebService.ftpSoapClient();
/* Create Timestamp of download date */
System.DateTime dateOfDownload = System.DateTime.Now;
string dateOfDownloadTreated = dateOfDownload.ToString("yyyy-MM-dd HH':'mm':'ss");
/* Get region_id of downloaded file */
regionID = service.Get_region_id(_ConnectedRegion.Get_RegionName());
if (regionID == 0)
throw new Exception("Region doesn't exist !");
foreach (File downloadedFile in _FilesToDownload)
{
/* Check if file (with it's associated region) already exists, if not, create one and get its ID. */
fileID = service.Get_file_id(downloadedFile.Get_Name(), regionID);
if (fileID == 0)
{
service.Add_file(regionID, downloadedFile.Get_Name());
fileID = service.Get_file_id(downloadedFile.Get_Name(), regionID);
}
/* Add historic in DB */
service.Add_download_per_region(regionID, fileID, dateOfDownloadTreated);
}
/* Update FileExplorer ListView */
FileExplorerListView.Items.Clear();
FillFileExplorerListView();
/* Close service */
service.Close();
/* Reset targets paths & set labels*/
_FtpAPI.Reset_DownloadedFilesTarget();
DirFileCounterLabel.Text = _CurrentDirectory.Get_FoldersList().Count + " dir / " + _CurrentDirectory.Get_FilesList().Count + " files";
FTPBrowserHeaderGroup.ValuesPrimary.Heading = "FTP Browser (" + _ConnectedRegion.Get_RegionName() + ") :";
}
catch (Exception ex) { KryptonMessageBox.Show(ex.ToString()); }
}
/*************************************\
* Fill FillFTPTreeView with regions *
\*************************************/
private void FillFTPTreeView()
{
List<string> regions = new List<string>();
List<string> group = new List<string>();
string groupName = String.Empty;
string[] split;
FTPTreeView.Nodes.Clear();
/* Fill list of region names */
foreach (Region element in _RegionsList)
regions.Add(element.Get_RegionName());
regions.Sort();
/* Construct TreeView */
foreach (string region in regions)
{
/* The goal is to group by the first word of the region name */
split = region.Split(' ');
/* Primer */
if (String.IsNullOrEmpty(groupName))
{