1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Drawing;
|
---|
4 | using System.IO;
|
---|
5 | using System.Linq;
|
---|
6 | using System.Threading;
|
---|
7 | using System.Windows.Forms;
|
---|
8 | using HeuristicLab.Clients.Hive;
|
---|
9 | using HeuristicLab.Common;
|
---|
10 | using HeuristicLab.Core;
|
---|
11 | using HeuristicLab.MainForm;
|
---|
12 |
|
---|
13 | namespace HeuristicLab.HiveJobDownloader {
|
---|
14 | public partial class HiveJobDownloaderForm : Form {
|
---|
15 |
|
---|
16 | public HiveJobDownloaderForm() {
|
---|
17 | InitializeComponent();
|
---|
18 | ContentManager.Initialize(new PersistenceContentManager());
|
---|
19 | statusIcons.Images.Add(Properties.Resources.Downloading);
|
---|
20 | statusIcons.Images.Add(Properties.Resources.Finished);
|
---|
21 | statusIcons.Images.Add(Properties.Resources._new);
|
---|
22 | }
|
---|
23 |
|
---|
24 | private void btnBrowse_Click(object sender, EventArgs e) {
|
---|
25 | if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) {
|
---|
26 | this.tbxTargetDirectory.Text = folderBrowserDialog1.SelectedPath;
|
---|
27 | }
|
---|
28 | }
|
---|
29 |
|
---|
30 | private void refreshButton_Click(object sender, EventArgs e) {
|
---|
31 | HiveClient.Instance.Refresh();
|
---|
32 | try {
|
---|
33 | jobListView.Items.Clear();
|
---|
34 | int i = 0;
|
---|
35 | foreach (RefreshableJob refJob in HiveClient.Instance.Jobs) {
|
---|
36 | var cj = new HiveJob { Job = refJob, JobStatus = HiveJob.Status.New, Path = tbxTargetDirectory.Text };
|
---|
37 | ListViewItem jobList = new ListViewItem();
|
---|
38 | jobList.SubItems.Add(cj.Name);
|
---|
39 | jobList.Tag = cj;
|
---|
40 | jobList.ImageIndex = 2;
|
---|
41 | jobListView.Items.Add(jobList);
|
---|
42 | i++;
|
---|
43 | }
|
---|
44 | }
|
---|
45 | catch (Exception E) {
|
---|
46 | lblState.Text = E.Message;
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | private void HiveJobDownloaderForm_Load(object sender, EventArgs e) {
|
---|
51 | tbxTargetDirectory.Text = @"C:\Users\Lokal\Documents";
|
---|
52 | prbStatus.Visible = false;
|
---|
53 | HiveClient.Instance.Refresh();
|
---|
54 | try {
|
---|
55 | jobListView.Items.Clear();
|
---|
56 | jobListView.Columns.Add("Status", 50);
|
---|
57 | jobListView.Columns.Add("Job Name", 150);
|
---|
58 | int i = 0;
|
---|
59 | foreach (RefreshableJob refJob in HiveClient.Instance.Jobs) {
|
---|
60 | var cj = new HiveJob { Job = refJob, JobStatus = HiveJob.Status.New, Path = tbxTargetDirectory.Text };
|
---|
61 | ListViewItem jobList = new ListViewItem();
|
---|
62 | jobList.SubItems.Add(cj.Name);
|
---|
63 | jobList.Tag = cj;
|
---|
64 | jobList.ImageIndex = 2;
|
---|
65 | jobListView.Items.Add(jobList);
|
---|
66 | i++;
|
---|
67 | }
|
---|
68 | statusIcons.ImageSize = new Size(16, 16);
|
---|
69 | }
|
---|
70 | catch (Exception E) {
|
---|
71 | lblState.Text = E.Message;
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | private void btnDownload_Click(object sender, EventArgs e) {
|
---|
76 | lblStatus.Text = "Downloading ..";
|
---|
77 | SelectedList().ImageIndex = 0;
|
---|
78 | Thread loader = new Thread(DownloadJob);
|
---|
79 | loader.Start();
|
---|
80 | }
|
---|
81 |
|
---|
82 | private HiveJob SelectedJob() {
|
---|
83 | HiveJob selectedJob = new HiveJob();
|
---|
84 | ListViewItem lvItem = new ListViewItem();
|
---|
85 | foreach (ListViewItem list in jobListView.SelectedItems) {
|
---|
86 | selectedJob = (HiveJob)list.Tag;
|
---|
87 | }
|
---|
88 | return selectedJob;
|
---|
89 | }
|
---|
90 |
|
---|
91 | private ListViewItem SelectedList() {
|
---|
92 | ListViewItem lvItem = new ListViewItem();
|
---|
93 | foreach (ListViewItem list in jobListView.SelectedItems) {
|
---|
94 | lvItem = list;
|
---|
95 | }
|
---|
96 | return lvItem;
|
---|
97 | }
|
---|
98 |
|
---|
99 | private void DownloadJob() {
|
---|
100 | try {
|
---|
101 | if (InvokeRequired) Invoke(new Action(DownloadJob));
|
---|
102 | else {
|
---|
103 | if (SelectedJob().Job.IsDownloadable) {
|
---|
104 | prbStatus.Enabled = true;
|
---|
105 | SelectedJob().Job.Loaded += Job_Loaded;
|
---|
106 | SelectedJob().Job.IsProgressingChanged += Job_IsProgressingChanged;
|
---|
107 | HiveClient.LoadJob(SelectedJob().Job);
|
---|
108 | } else {
|
---|
109 | lblStatus.Text = "Not Downloadable";
|
---|
110 | }
|
---|
111 | }
|
---|
112 | }
|
---|
113 | catch (Exception E) {
|
---|
114 | lblState.Text = E.Message;
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | void Job_IsProgressingChanged(object sender, EventArgs e) {
|
---|
119 | var rj = (RefreshableJob)sender;
|
---|
120 | rj.Progress.ProgressValueChanged += Progress_ProgressValueChanged;
|
---|
121 | }
|
---|
122 |
|
---|
123 |
|
---|
124 | void Progress_ProgressValueChanged(object sender, EventArgs e) {
|
---|
125 | if (InvokeRequired) Invoke((Action<object, EventArgs>)Progress_ProgressValueChanged);
|
---|
126 | else {
|
---|
127 | double progressValue = ((Progress)sender).ProgressValue;
|
---|
128 | prbStatus.Visible = true;
|
---|
129 | prbStatus.Minimum = 0;
|
---|
130 | prbStatus.Maximum = 100;
|
---|
131 | prbStatus.Value = (int)Math.Round(prbStatus.Minimum + progressValue * (prbStatus.Maximum - prbStatus.Minimum));
|
---|
132 | prbStatus.Step = 1;
|
---|
133 | Thread update = new Thread(UpdateLblStatus);
|
---|
134 | update.Start();
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | private void UpdateLblStatus() {
|
---|
139 | if (InvokeRequired) {
|
---|
140 | MethodInvoker AssignMethodToControl = new MethodInvoker(UpdateLblStatus);
|
---|
141 | lblStatus.Invoke(AssignMethodToControl);
|
---|
142 | } else {
|
---|
143 | if (prbStatus.Value < 100) {
|
---|
144 | lblStatus.Text = "Downloading ..";
|
---|
145 | SelectedList().ImageIndex = 0;
|
---|
146 | } else if (prbStatus.Value == 100) {
|
---|
147 | lblStatus.Text = "Finished";
|
---|
148 | SelectedList().ImageIndex = 1;
|
---|
149 | prbStatus.Visible = false;
|
---|
150 | }
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | void Job_Loaded(object sender, EventArgs e) {
|
---|
155 | if (InvokeRequired) Invoke((Action<object, EventArgs>)Job_Loaded);
|
---|
156 | else {
|
---|
157 | RefreshableJob cJob = (RefreshableJob)sender;
|
---|
158 | HiveJob chJob = null;
|
---|
159 |
|
---|
160 | foreach (ListViewItem list in jobListView.Items) {
|
---|
161 | var hj = (HiveJob)list.Tag;
|
---|
162 | if (hj.Job == cJob) {
|
---|
163 | chJob = hj;
|
---|
164 | break;
|
---|
165 | }
|
---|
166 | }
|
---|
167 |
|
---|
168 | List<HiveTask> parentTasks = new List<HiveTask>();
|
---|
169 | foreach (var task in cJob.HiveTasks) {
|
---|
170 | if (task.ChildHiveTasks.Count() > 0) {
|
---|
171 | parentTasks.Add(task);
|
---|
172 | }
|
---|
173 | }
|
---|
174 | if (parentTasks.Count == 0) {
|
---|
175 | parentTasks.AddRange(cJob.HiveTasks);
|
---|
176 | }
|
---|
177 | int i = 0;
|
---|
178 | foreach (var task in parentTasks) {
|
---|
179 | string dir = Path.Combine(chJob.Path, cJob.Job.Name);
|
---|
180 | ContentManager.Save(task.ItemTask.Item as IStorableContent, dir + i.ToString() + ".hl", true);
|
---|
181 | chJob.JobStatus = HiveJob.Status.Downloading;
|
---|
182 | chJob.Path = tbxTargetDirectory.Text;
|
---|
183 | lblStatus.Text = "Downloading..";
|
---|
184 | i++;
|
---|
185 | }
|
---|
186 | SelectedList().ImageIndex = 1;
|
---|
187 | chJob.JobStatus = HiveJob.Status.Finished;
|
---|
188 | prbStatus.Visible = false;
|
---|
189 | }
|
---|
190 | }
|
---|
191 |
|
---|
192 |
|
---|
193 | private void jobListView_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
194 | if (SelectedJob().JobStatus == HiveJob.Status.New) {
|
---|
195 | SelectedList().ImageIndex = 2;
|
---|
196 | lblStatus.Text = SelectedJob().JobStatus.ToString();
|
---|
197 | prbStatus.Value = 0;
|
---|
198 | prbStatus.Visible = false;
|
---|
199 | } else if (SelectedJob().JobStatus == HiveJob.Status.Downloading) {
|
---|
200 | SelectedList().ImageIndex = 0;
|
---|
201 | lblStatus.Text = "Downloading!";
|
---|
202 | prbStatus.Visible = true;
|
---|
203 | } else {
|
---|
204 | SelectedList().ImageIndex = 1;
|
---|
205 | lblStatus.Text = SelectedJob().JobStatus.ToString();
|
---|
206 | tbxTargetDirectory.Text = SelectedJob().Path;
|
---|
207 | prbStatus.Visible = false;
|
---|
208 | }
|
---|
209 | }
|
---|
210 |
|
---|
211 |
|
---|
212 | }
|
---|
213 | }
|
---|