OSDN Git Service

AppliStation,ハッシュが非整合の時に尋ねるようにする部分の改良
[applistation/AppliStation.git] / AppliStation / AppliStation.Util / ExecutionProgressViewer.cs
1 using System;\r
2 using System.Drawing;\r
3 using System.Windows.Forms;\r
4 using System.Threading;\r
5 using NaGet.SubCommands;\r
6 using NaGet.Net;\r
7 \r
8 namespace AppliStation.Util\r
9 {\r
10         /// <summary>\r
11         /// Description of ExecutionProgressViewer.\r
12         /// </summary>\r
13         public partial class ExecutionProgressViewer : Form\r
14         {\r
15                 private NaGetTaskSet taskSet;\r
16                 \r
17                 private Downloader downloader;\r
18                 \r
19                 private Thread tasksetRunningThread = null;\r
20                 \r
21                 /// <summary>\r
22                 /// \8fI\97¹\8e\9e\82É\89½\82ð\8ds\82¤\82©\82Ì\83t\83\89\83O\r
23                 /// </summary>\r
24                 public enum ActionOnDoneFlags {\r
25                         None = 0,\r
26                         FlashWindow = 1,\r
27                         AutoCloseOnSuccess = 2,\r
28                 }\r
29                 \r
30                 /// <summary>\r
31                 /// \8fI\97¹\8e\9e\82É\89½\82ð\8ds\82¤\82©\r
32                 /// </summary>\r
33                 public ActionOnDoneFlags ActionOnDone = ActionOnDoneFlags.FlashWindow;\r
34                 \r
35                 public Downloader Downloader {\r
36                         get { return downloader; }\r
37                 }\r
38                 \r
39                 public ExecutionProgressViewer()\r
40                 {\r
41                         //\r
42                         // The InitializeComponent() call is required for Windows Forms designer support.\r
43                         //\r
44                         InitializeComponent();\r
45                         \r
46                         downloader = new Downloader();\r
47                         downloader.DownloadEventRaised += delegate(object sender, DownloadEventArgs e) {\r
48                                 if (InvokeRequired) {\r
49                                         Invoke(new EventHandler<DownloadEventArgs>(onDownloadEvent), sender, e);\r
50                                 } else {\r
51                                         onDownloadEvent(sender, e);\r
52                                 }\r
53                         };\r
54                 }\r
55                 \r
56                 private void onDownloadEvent(object sender, DownloadEventArgs a)\r
57                 {\r
58                         if (a.TaskProgressPercent >= 0) {\r
59                                 progressBarSub.Value = (int) a.TaskProgressPercent;\r
60                                 progressBarSub.Style = ProgressBarStyle.Continuous;\r
61                         } else {\r
62                                 progressBarSub.Style = ProgressBarStyle.Marquee;\r
63                         }\r
64                         \r
65                         switch (a.Type) {\r
66                                 case DownloadEventType.INITED:\r
67                                 case DownloadEventType.CONNECTED:\r
68                                         progressSubLabel.Text = a.TaskMessage;\r
69                                         \r
70                                         progressBarSub.Visible = true;\r
71                                         progressSubLabel.Visible = true;\r
72                                         break;\r
73                                 case DownloadEventType.DOWNLOADING:\r
74                                         progressSubLabel.Text = a.TaskMessage;\r
75                                         break;\r
76                                 case DownloadEventType.COMPLETED:\r
77                                         progressBarSub.Visible = false;\r
78                                         progressSubLabel.Visible = false;\r
79                                         break;\r
80                                 case DownloadEventType.ERROR:\r
81                                         progressBarSub.Visible = false;\r
82                                         progressSubLabel.Visible = false;\r
83                                         \r
84                                         logBox.SelectionColor = System.Drawing.Color.Red;\r
85                                         logBox.AppendText("   [\83G\83\89\81[] " + a.TaskMessage + System.Environment.NewLine);\r
86                                         logBox.SelectionColor = logBox.ForeColor;\r
87                                         break;\r
88                         }\r
89                 }\r
90                 \r
91                 #region NaGetTaskSet\8aÖ\98A\r
92                 \r
93                 private void onTaskSetRaised(object sender, NaGetTaskSetEventArgs e)\r
94                 {\r
95                         NaGetTaskSet taskSet = (NaGetTaskSet) sender;\r
96                         \r
97                         if (e.TaskProgressPercent >= 0) {\r
98                                 progressBar.Value = (int) e.TaskProgressPercent;\r
99                                 progressBar.Style = ProgressBarStyle.Continuous;\r
100                         } else {\r
101                                 progressBar.Style = ProgressBarStyle.Marquee;\r
102                         }\r
103                         progressLabel.Text = e.TaskMessage ?? string.Empty;\r
104                         \r
105                         cancelButton.Enabled = taskSet.Running && taskSet.Cancelable;\r
106                         \r
107                         switch (e.Type) {\r
108                                 case NaGetTaskSetEventType.COMPLETED:\r
109                                         logBox.AppendText("\8a®\97¹." + System.Environment.NewLine);\r
110                                         if (taskSet.Done) {\r
111                                                 okButton.Enabled = true;\r
112                                                 cancelButton.Enabled = false;\r
113                                         }\r
114                                         break;\r
115                                 case NaGetTaskSetEventType.STARTED_TASKSET:\r
116                                         subtitleLabel.Text = taskSet.TaskSetNames[taskSet.CurrentTaskSetIndex];\r
117                                         logBox.AppendText("  " + e.TaskMessage + System.Environment.NewLine);\r
118                                         break;\r
119                                 case NaGetTaskSetEventType.COMPLETED_TASKSET:\r
120                                         if (progressBarSub.Visible) progressBarSub.Hide();\r
121                                         if (progressSubLabel.Visible) progressSubLabel.Hide();\r
122                                         \r
123                                         NativeMethods.ProgressBar_SetState(progressBar, 1); // VistaProgress\90F\81F\83m\81[\83}\83\8b\r
124                                         \r
125                                         logBox.AppendText(string.Format(" ... \8a®\97¹. [{0}%]", (int) e.TaskProgressPercent));\r
126                                         logBox.AppendText(System.Environment.NewLine);\r
127                                         break;\r
128                                 case NaGetTaskSetEventType.INFO:\r
129                                         logBox.AppendText("  " + e.TaskMessage + System.Environment.NewLine);\r
130                                         break;\r
131                                 case NaGetTaskSetEventType.ERROR:\r
132                                         logBox.SelectionColor = System.Drawing.Color.Red;\r
133                                         logBox.AppendText("  [\83G\83\89\81[] " + e.TaskMessage + System.Environment.NewLine);\r
134                                         logBox.SelectionColor = logBox.ForeColor;\r
135                                         \r
136                                         NativeMethods.ProgressBar_SetState(progressBar, 2); // VistaProgress\90F\81F\83G\83\89\81[\r
137                                         \r
138                                         okButton.Enabled = true;\r
139                                         cancelButton.Enabled = false;\r
140                                         break;\r
141                                 case NaGetTaskSetEventType.CANCELED:\r
142                                         logBox.SelectionColor = System.Drawing.Color.Red;\r
143                                         logBox.AppendText(e.TaskMessage + System.Environment.NewLine);\r
144                                         logBox.SelectionColor = logBox.ForeColor;\r
145                                         \r
146                                         NativeMethods.ProgressBar_SetState(progressBar, 1); // VistaProgress\90F\81F\92\86\92f\r
147                                         \r
148                                         okButton.Enabled = true;\r
149                                         cancelButton.Enabled = false;\r
150                                         break;\r
151                                 case NaGetTaskSetEventType.WARNING:\r
152                                         logBox.SelectionColor = System.Drawing.Color.Red;\r
153                                         logBox.AppendText("  [\83G\83\89\81[] " + e.TaskMessage + System.Environment.NewLine);\r
154                                         logBox.SelectionColor = logBox.ForeColor;\r
155                                         \r
156                                         break;\r
157                         }\r
158                         \r
159                         if (taskSet.Done) {\r
160                                 if ((ActionOnDone & ActionOnDoneFlags.FlashWindow) != 0) {\r
161                                         NativeMethods.Form_FlashWindow(this,\r
162                                                                NativeMethods.FlashFlag.All | NativeMethods.FlashFlag.TimerNoFG,\r
163                                                                uint.MaxValue, 0);\r
164                                 }\r
165                                 if (okButton.Enabled && (ActionOnDone & ActionOnDoneFlags.AutoCloseOnSuccess) != 0) {\r
166                                         this.DialogResult = DialogResult.OK;\r
167                                         Close();\r
168                                         Dispose();\r
169                                 }\r
170                         }\r
171                 }\r
172                 \r
173                 private NaGetTaskQueryResult onTaskQueryRaised(object sender, NaGetTaskQueryArgs e)\r
174                 {\r
175                         MessageBoxButtons buttons = MessageBoxButtons.OKCancel;\r
176                         if (e.SelectionFlag == (NaGetTaskQueryResult.CONTINUE | NaGetTaskQueryResult.RETRY | NaGetTaskQueryResult.CANCEL)) {\r
177                                 buttons = MessageBoxButtons.AbortRetryIgnore;\r
178                         } else if (e.SelectionFlag == (NaGetTaskQueryResult.RETRY | NaGetTaskQueryResult.CANCEL)) {\r
179                                 buttons = MessageBoxButtons.RetryCancel;\r
180                         } else if (e.SelectionFlag == NaGetTaskQueryResult.CONTINUE) {\r
181                                 buttons = MessageBoxButtons.OK;\r
182                         }\r
183                         \r
184                         DialogResult result = MessageBox.Show(e.Message, this.Text, buttons);\r
185                         \r
186                         switch (result) {\r
187                                 case DialogResult.OK:\r
188                                 case DialogResult.Ignore:\r
189                                         return NaGetTaskQueryResult.CONTINUE;\r
190                                 case DialogResult.Cancel:\r
191                                 case DialogResult.Abort:\r
192                                         return NaGetTaskQueryResult.CANCEL;\r
193                                 case DialogResult.Retry:\r
194                                         return NaGetTaskQueryResult.RETRY;\r
195                                 default:\r
196                                         return NaGetTaskQueryResult.CANCELED_AUTOMATICALLY;\r
197                         }\r
198                 }\r
199                 \r
200                 public void SetTaskSet(NaGetTaskSet taskSet)\r
201                 {\r
202                         this.taskSet = taskSet;\r
203                         \r
204                         taskSet.TaskSetRaised += delegate(object sender, NaGetTaskSetEventArgs e) {\r
205                                 if (InvokeRequired) {\r
206                                         Invoke(new EventHandler<NaGetTaskSetEventArgs>(onTaskSetRaised), taskSet, e);\r
207                                 } else {\r
208                                         onTaskSetRaised(taskSet, e);\r
209                                 }\r
210                         };\r
211                         \r
212                         taskSet.TaskQueryRaised += onTaskQueryRaised;\r
213                 }\r
214 \r
215                 public void StartTaskSet()\r
216                 {\r
217                         tasksetRunningThread = new Thread(taskSet.Run);\r
218                         tasksetRunningThread.Start();\r
219                 }\r
220                 \r
221                 #endregion\r
222                 \r
223                 void OkButtonClick(object sender, EventArgs e)\r
224                 {\r
225                         if (taskSet == null || taskSet.Done) {\r
226                                 this.Close();\r
227                                 this.Dispose();\r
228                         }\r
229                 }\r
230                 \r
231                 void CancelButtonClick(object sender, EventArgs e)\r
232                 {\r
233                         if (InvokeRequired) {\r
234                                 Invoke(new EventHandler(CancelButtonClickConcrete), sender, e);\r
235                         } else {\r
236                                 CancelButtonClickConcrete(sender,e);\r
237                         }\r
238                 }\r
239                 \r
240                 void CancelButtonClickConcrete(object sender, EventArgs e)\r
241                 {\r
242                         if (taskSet != null && taskSet.Running && taskSet.Cancelable) {\r
243                                 cancelButton.Enabled = false;\r
244                                 \r
245                                 NativeMethods.ProgressBar_SetState(progressBar, 3); // VistaProgress\90F\81F\92\86\92f\r
246                                 \r
247                                 taskSet.Cancel();\r
248                         }\r
249                 }\r
250                 \r
251                 void ExecutionProgressViewerShown(object sender, EventArgs e)\r
252                 {\r
253                         this.BringToFront();\r
254                 }\r
255         }\r
256 }\r