OSDN Git Service

AppliStation-GUI,UserPrefForm.csへのコメント追加
[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 Thread tasksetRunningThread = null;\r
18                 \r
19                 /// <summary>\r
20                 /// 終了時に何を行うかのフラグ\r
21                 /// </summary>\r
22                 public enum ActionOnDoneFlags {\r
23                         None = 0,\r
24                         FlashWindow = 1,\r
25                         AutoCloseOnSuccess = 2,\r
26                 }\r
27                 \r
28                 /// <summary>\r
29                 /// 終了時に何を行うか\r
30                 /// </summary>\r
31                 public ActionOnDoneFlags ActionOnDone = ActionOnDoneFlags.FlashWindow;\r
32                                 \r
33                 public ExecutionProgressViewer()\r
34                 {\r
35                         //\r
36                         // The InitializeComponent() call is required for Windows Forms designer support.\r
37                         //\r
38                         InitializeComponent();\r
39                         \r
40                         this.Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);\r
41                         toolTip.SetToolTip(autoCloseCheckBox,\r
42                                            string.Format("エラーがなく正常に終了した場合、{0}秒後に自動的にこのダイアログを閉じます", autoCloseTimer.Interval/1000));\r
43                 }\r
44                                 \r
45                 private void onDownloadEvent(object sender, DownloadEventArgs a)\r
46                 {\r
47                         if (InvokeRequired) {\r
48                                 Invoke(new EventHandler<DownloadEventArgs>(onDownloadEvent), sender, a);\r
49                                 return;\r
50                         }\r
51                         \r
52                         \r
53                         if (a.TaskProgressPercent >= 0) {\r
54                                 progressBarSub.Value = (int) a.TaskProgressPercent;\r
55                                 progressBarSub.Style = ProgressBarStyle.Continuous;\r
56                         } else {\r
57                                 progressBarSub.Style = ProgressBarStyle.Marquee;\r
58                         }\r
59                         \r
60                         switch (a.Type) {\r
61                                 case DownloadEventType.INITED:\r
62                                 case DownloadEventType.CONNECTED:\r
63                                         progressSubLabel.Text = a.TaskMessage;\r
64                                         \r
65                                         progressBarSub.Visible = true;\r
66                                         progressSubLabel.Visible = true;\r
67                                         break;\r
68                                 case DownloadEventType.DOWNLOADING:\r
69                                         progressSubLabel.Text = a.TaskMessage;\r
70                                         break;\r
71                                 case DownloadEventType.COMPLETED:\r
72                                         progressBarSub.Visible = false;\r
73                                         progressSubLabel.Visible = false;\r
74                                         break;\r
75                                 case DownloadEventType.ERROR:\r
76                                         progressBarSub.Visible = false;\r
77                                         progressSubLabel.Visible = false;\r
78                                         \r
79                                         logBox.SelectionColor = System.Drawing.Color.Red;\r
80                                         logBox.AppendText("   [エラー] " + a.TaskMessage + System.Environment.NewLine);\r
81                                         logBox.SelectionColor = logBox.ForeColor;\r
82                                         break;\r
83                         }\r
84                 }\r
85                 \r
86                 #region NaGetTaskSet関連\r
87                 \r
88                 private void onTaskSetRaised(object sender, NaGetTaskSetEventArgs e)\r
89                 {\r
90                         NaGetTaskSet taskSet = (NaGetTaskSet) sender;\r
91                         \r
92                         if (e.TaskProgressPercent >= 0) {\r
93                                 progressBar.Value = (int) e.TaskProgressPercent;\r
94                                 progressBar.Style = ProgressBarStyle.Continuous;\r
95                         } else {\r
96                                 progressBar.Style = ProgressBarStyle.Marquee;\r
97                         }\r
98                         \r
99                         cancelButton.Enabled = taskSet.Running && taskSet.Cancelable;\r
100                         \r
101                         switch (e.Type) {\r
102                                 case NaGetTaskSetEventType.COMPLETED:\r
103                                         logBox.AppendText("完了." + System.Environment.NewLine);\r
104                                         if (taskSet.Done) {\r
105                                                 okButton.Enabled = true;\r
106                                                 cancelButton.Enabled = false;\r
107                                                 \r
108                                                 // タスクが完了したらしばらく待って閉じるために、自動クローズタイマーを起動する\r
109                                                 autoCloseTimer.Start();\r
110                                         }\r
111                                         break;\r
112                                 case NaGetTaskSetEventType.STARTED_TASKSET:\r
113                                         progressLabel.Text = taskSet.TaskSetNames[taskSet.CurrentTaskSetIndex];\r
114                                         logBox.AppendText("  " + e.TaskMessage + System.Environment.NewLine);\r
115                                         break;\r
116                                 case NaGetTaskSetEventType.COMPLETED_TASKSET:\r
117                                         if (progressBarSub.Visible) progressBarSub.Hide();\r
118                                         if (progressSubLabel.Visible) progressSubLabel.Hide();\r
119                                         \r
120                                         NativeMethods.ProgressBar_SetState(progressBar, 1); // VistaProgress色:ノーマル\r
121                                         \r
122                                         logBox.AppendText(string.Format(" ... 完了. [{0}%]", (int) e.TaskProgressPercent));\r
123                                         logBox.AppendText(System.Environment.NewLine);\r
124                                         break;\r
125                                 case NaGetTaskSetEventType.INFO:\r
126                                         logBox.AppendText("  " + e.TaskMessage + System.Environment.NewLine);\r
127                                         break;\r
128                                 case NaGetTaskSetEventType.ERROR:\r
129                                         logBox.SelectionColor = System.Drawing.Color.Red;\r
130                                         logBox.AppendText("  [エラー] " + e.TaskMessage + System.Environment.NewLine);\r
131                                         logBox.SelectionColor = logBox.ForeColor;\r
132                                         \r
133                                         NativeMethods.ProgressBar_SetState(progressBar, 2); // VistaProgress色:エラー\r
134                                         \r
135                                         autoCloseCheckBox.Enabled = false;\r
136                                         okButton.Enabled = true;\r
137                                         cancelButton.Enabled = false;\r
138                                         break;\r
139                                 case NaGetTaskSetEventType.CANCELED:\r
140                                         logBox.SelectionColor = System.Drawing.Color.Red;\r
141                                         logBox.AppendText(e.TaskMessage + System.Environment.NewLine);\r
142                                         logBox.SelectionColor = logBox.ForeColor;\r
143                                         \r
144                                         NativeMethods.ProgressBar_SetState(progressBar, 1); // VistaProgress色:中断\r
145                                         NativeMethods.ProgressBar_SetState(progressBarSub, 1); // VistaProgress色:中断\r
146                                         \r
147                                         autoCloseCheckBox.Enabled = false;\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("  [エラー] " + e.TaskMessage + System.Environment.NewLine);\r
154                                         logBox.SelectionColor = logBox.ForeColor;\r
155                                         \r
156                                         NativeMethods.ProgressBar_SetState(progressBar, 2); // VistaProgress色:エラー\r
157                                         \r
158                                         autoCloseCheckBox.Enabled = false;\r
159                                         break;\r
160                         }\r
161                         \r
162                         if (taskSet.Done) {\r
163                                 if ((ActionOnDone & ActionOnDoneFlags.FlashWindow) != 0) {\r
164                                         NativeMethods.Form_FlashWindow(this,\r
165                                                                NativeMethods.FlashFlag.All | NativeMethods.FlashFlag.TimerNoFG,\r
166                                                                uint.MaxValue, 0);\r
167                                 }\r
168                                 if (okButton.Enabled && (ActionOnDone & ActionOnDoneFlags.AutoCloseOnSuccess) != 0) {\r
169                                         this.DialogResult = DialogResult.OK;\r
170                                         Close();\r
171                                         Dispose();\r
172                                 }\r
173                         }\r
174                 }\r
175                 \r
176                 private NaGetTaskQueryResult onTaskQueryRaised(object sender, NaGetTaskQueryArgs e)\r
177                 {\r
178                         MessageBoxButtons buttons = MessageBoxButtons.OKCancel;\r
179                         if (e.SelectionFlag == (NaGetTaskQueryResult.CONTINUE | NaGetTaskQueryResult.RETRY | NaGetTaskQueryResult.CANCEL)) {\r
180                                 buttons = MessageBoxButtons.AbortRetryIgnore;\r
181                         } else if (e.SelectionFlag == (NaGetTaskQueryResult.RETRY | NaGetTaskQueryResult.CANCEL)) {\r
182                                 buttons = MessageBoxButtons.RetryCancel;\r
183                         } else if (e.SelectionFlag == NaGetTaskQueryResult.CONTINUE) {\r
184                                 buttons = MessageBoxButtons.OK;\r
185                         }\r
186                         \r
187                         DialogResult result = MessageBox.Show(e.Message, this.Text, buttons);\r
188                         \r
189                         switch (result) {\r
190                                 case DialogResult.OK:\r
191                                 case DialogResult.Ignore:\r
192                                         return NaGetTaskQueryResult.CONTINUE;\r
193                                 case DialogResult.Cancel:\r
194                                 case DialogResult.Abort:\r
195                                         return NaGetTaskQueryResult.CANCEL;\r
196                                 case DialogResult.Retry:\r
197                                         return NaGetTaskQueryResult.RETRY;\r
198                                 default:\r
199                                         return NaGetTaskQueryResult.CANCELED_AUTOMATICALLY;\r
200                         }\r
201                 }\r
202                 \r
203                 public void SetTaskSet(NaGetTaskSet taskSet)\r
204                 {\r
205                         this.taskSet = taskSet;\r
206                         \r
207                         taskSet.TaskSetRaised += delegate(object sender, NaGetTaskSetEventArgs e) {\r
208                                 if (InvokeRequired) {\r
209                                         Invoke(new EventHandler<NaGetTaskSetEventArgs>(onTaskSetRaised), taskSet, e);\r
210                                 } else {\r
211                                         onTaskSetRaised(taskSet, e);\r
212                                 }\r
213                         };\r
214                         \r
215                         System.Reflection.PropertyInfo fDownloader = taskSet.GetType().GetProperty("Downloader");\r
216                         if (fDownloader != null && (fDownloader.PropertyType == typeof(Downloader))) {\r
217                                 ((Downloader) fDownloader.GetValue(taskSet, null)).DownloadEventRaised += onDownloadEvent;\r
218                         }\r
219                         \r
220                         taskSet.TaskQueryRaised += onTaskQueryRaised;\r
221                 }\r
222 \r
223                 public void StartTaskSet()\r
224                 {\r
225                         tasksetRunningThread = new Thread(taskSet.Run);\r
226                         // スレッドをSTAにしないとCOMアクセスできず、ウイルススキャンができない。\r
227                         tasksetRunningThread.SetApartmentState(ApartmentState.STA);\r
228                         tasksetRunningThread.Start();\r
229                 }\r
230                 \r
231                 #endregion\r
232                 \r
233                 void OkButtonClick(object sender, EventArgs e)\r
234                 {\r
235                         if (taskSet == null || taskSet.Done) {\r
236                                 this.Close();\r
237                                 this.Dispose();\r
238                         }\r
239                 }\r
240                 \r
241                 void CancelButtonClick(object sender, EventArgs e)\r
242                 {\r
243                         if (InvokeRequired) {\r
244                                 Invoke(new EventHandler(CancelButtonClickConcrete), sender, e);\r
245                         } else {\r
246                                 CancelButtonClickConcrete(sender,e);\r
247                         }\r
248                 }\r
249                 \r
250                 void CancelButtonClickConcrete(object sender, EventArgs e)\r
251                 {\r
252                         autoCloseCheckBox.Enabled = false;\r
253                         if (taskSet != null && taskSet.Running && taskSet.Cancelable) {\r
254                                 cancelButton.Enabled = false;\r
255                                 \r
256                                 NativeMethods.ProgressBar_SetState(progressBar, 3); // VistaProgress色:中断\r
257                                 \r
258                                 taskSet.Cancel();\r
259                         }\r
260                 }\r
261                 \r
262                 void ExecutionProgressViewerShown(object sender, EventArgs e)\r
263                 {\r
264                         this.BringToFront();\r
265                 }\r
266                 \r
267                 void ExecutionProgressViewerFormClosed(object sender, FormClosedEventArgs e)\r
268                 {\r
269                         autoCloseTimer.Enabled = false;\r
270                 }\r
271                 \r
272                 void AutoCloseTimerTick(object sender, EventArgs e)\r
273                 {\r
274                         autoCloseTimer.Stop();\r
275                         \r
276                         if (InvokeRequired) {\r
277                                 Invoke(new EventHandler(AutoCloseTimerTickConcrete), sender, e);\r
278                         } else {\r
279                                 AutoCloseTimerTickConcrete(sender,e);\r
280                         }\r
281                 }\r
282                 \r
283                 void AutoCloseTimerTickConcrete(object sender, EventArgs e)\r
284                 {\r
285                         // autoCloseCheckBoxが有効(正常終了)かつチェックのときに限り、OKボタンを自動的にクリック\r
286                         if (autoCloseCheckBox.Enabled && autoCloseCheckBox.Checked && okButton.Enabled) {\r
287                                 OkButtonClick(sender, e);\r
288                         } else if (! autoCloseCheckBox.Enabled) {\r
289                                 // タイマーの時間がすぎて、そのとき自動で閉じないようにしていしてあるならば、手動で閉じるように\r
290                                 autoCloseCheckBox.Enabled = false;\r
291                         }\r
292                 }\r
293         }\r
294 }\r