OSDN Git Service

AppliStation-GUI,進捗ダイアログのログテキスト表示領域がインストーラーファイルをダウンロードしているときに表示領域がカクカクする不具合を修正
[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                 private 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                 private 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                         switch (a.Type) {\r
53                                 case DownloadEventType.INITED:\r
54                                 case DownloadEventType.CONNECTED:\r
55                                         progressSubLabel.Text = a.TaskMessage;\r
56                                         \r
57                                         progressBarSub.Visible = true;\r
58                                         progressSubLabel.Visible = true;\r
59                                         break;\r
60                                 case DownloadEventType.DOWNLOADING:\r
61                                         progressSubLabel.Text = a.TaskMessage;\r
62                                         break;\r
63                                 case DownloadEventType.COMPLETED:\r
64                                         progressBarSub.Visible = false;\r
65                                         progressSubLabel.Visible = false;\r
66                                         break;\r
67                                 case DownloadEventType.ERROR:\r
68                                         progressBarSub.Visible = false;\r
69                                         progressSubLabel.Visible = false;\r
70                                         \r
71                                         logBox.SelectionColor = System.Drawing.Color.Red;\r
72                                         logBox.AppendText("   [エラー] " + a.TaskMessage + System.Environment.NewLine);\r
73                                         logBox.SelectionColor = logBox.ForeColor;\r
74                                         break;\r
75                         }\r
76                         \r
77                         // ダウンロードの進捗を表示\r
78                         if (a.TaskProgressPercent >= 0) {\r
79                                 progressBarSub.Value = (int) a.TaskProgressPercent;\r
80                                 progressBarSub.Style = ProgressBarStyle.Continuous;\r
81                         } else {\r
82                                 progressBarSub.Style = ProgressBarStyle.Marquee;\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                         NativeMethods.ProgressBarState progressState = NativeMethods.ProgressBarState.Normal;\r
92                         \r
93                         cancelButton.Enabled = taskSet.Running && taskSet.Cancelable;\r
94                         \r
95                         switch (e.Type) {\r
96                                 case NaGetTaskSetEventType.COMPLETED:\r
97                                         logBox.AppendText("完了." + System.Environment.NewLine);\r
98                                         if (taskSet.Done) {\r
99                                                 okButton.Enabled = true;\r
100                                                 cancelButton.Enabled = false;\r
101                                                 \r
102                                                 // タスクが完了したらしばらく待って閉じるために、自動クローズタイマーを起動する\r
103                                                 autoCloseTimer.Start();\r
104                                         }\r
105                                         break;\r
106                                 case NaGetTaskSetEventType.STARTED_TASKSET:\r
107                                         progressLabel.Text = taskSet.TaskSetNames[taskSet.CurrentTaskSetIndex];\r
108                                         logBox.AppendText("  " + e.TaskMessage + System.Environment.NewLine);\r
109                                         break;\r
110                                 case NaGetTaskSetEventType.COMPLETED_TASKSET:\r
111                                         if (progressBarSub.Visible) progressBarSub.Hide();\r
112                                         if (progressSubLabel.Visible) progressSubLabel.Hide();\r
113                                         \r
114                                         progressState = NativeMethods.ProgressBarState.Normal;\r
115                                         \r
116                                         logBox.AppendText(string.Format(" ... 完了. [{0}%]", (int) e.TaskProgressPercent));\r
117                                         logBox.AppendText(System.Environment.NewLine);\r
118                                         break;\r
119                                 case NaGetTaskSetEventType.INFO:\r
120                                         logBox.AppendText("  " + e.TaskMessage + System.Environment.NewLine);\r
121                                         break;\r
122                                 case NaGetTaskSetEventType.ERROR:\r
123                                         logBox.SelectionColor = System.Drawing.Color.Red;\r
124                                         logBox.AppendText("  [エラー] " + e.TaskMessage + System.Environment.NewLine);\r
125                                         logBox.SelectionColor = logBox.ForeColor;\r
126                                         \r
127                                         progressState = NativeMethods.ProgressBarState.Error;\r
128                                         \r
129                                         autoCloseCheckBox.Enabled = false;\r
130                                         okButton.Enabled = true;\r
131                                         cancelButton.Enabled = false;\r
132                                         break;\r
133                                 case NaGetTaskSetEventType.CANCELED:\r
134                                         logBox.SelectionColor = System.Drawing.Color.Red;\r
135                                         logBox.AppendText(e.TaskMessage + System.Environment.NewLine);\r
136                                         logBox.SelectionColor = logBox.ForeColor;\r
137                                         \r
138                                         progressState = NativeMethods.ProgressBarState.Error;\r
139                                         \r
140                                         autoCloseCheckBox.Enabled = false;\r
141                                         okButton.Enabled = true;\r
142                                         cancelButton.Enabled = false;\r
143                                         break;\r
144                                 case NaGetTaskSetEventType.WARNING:\r
145                                         logBox.SelectionColor = System.Drawing.Color.Red;\r
146                                         logBox.AppendText("  [エラー] " + e.TaskMessage + System.Environment.NewLine);\r
147                                         logBox.SelectionColor = logBox.ForeColor;\r
148                                         \r
149                                         progressState = NativeMethods.ProgressBarState.Error;\r
150                                         \r
151                                         autoCloseCheckBox.Enabled = false;\r
152                                         break;\r
153                         }\r
154                         \r
155                         // タスクセット全体の進捗をプログレスバーに表示する\r
156                         if (e.TaskProgressPercent >= 0) {\r
157                                 progressBar.Value = (int) e.TaskProgressPercent;\r
158                                 progressBar.Style = ProgressBarStyle.Continuous;\r
159                         } else {\r
160                                 progressBar.Style = ProgressBarStyle.Marquee;\r
161                         }\r
162                         NativeMethods.ProgressBar_SetState(progressBar, progressState);\r
163                         NativeMethods.ProgressBar_SetState(progressBarSub, progressState);\r
164                         // タスクセット全体の進捗をタスクバーに表示する(Windows 7以降のみ)\r
165                         if (e.TaskProgressPercent >= 0 && e.TaskProgressPercent < 100) {\r
166                                 NativeMethods.Form_SetTaskbarProgressParams(this, progressBar.Style, progressState, (ulong) e.TaskProgressPercent, 100);\r
167                         } else {\r
168                                 NativeMethods.Form_SetTaskbarProgressParams(this, progressBar.Style, progressState, 0, 0);\r
169                         }\r
170                         \r
171                         logBox.Select(logBox.TextLength, 0);\r
172                         logBox.ScrollToCaret();\r
173                         \r
174                         if (taskSet.Done) {\r
175                                 if ((ActionOnDone & ActionOnDoneFlags.FlashWindow) != 0) {\r
176                                         NativeMethods.Form_FlashWindow(this,\r
177                                                                NativeMethods.FlashFlag.All | NativeMethods.FlashFlag.TimerNoFG,\r
178                                                                uint.MaxValue, 0);\r
179                                 }\r
180                                 if (okButton.Enabled && (ActionOnDone & ActionOnDoneFlags.AutoCloseOnSuccess) != 0) {\r
181                                         this.DialogResult = DialogResult.OK;\r
182                                         Close();\r
183                                         Dispose();\r
184                                 }\r
185                         }\r
186                 }\r
187                 \r
188                 private NaGetTaskQueryResult onTaskQueryRaised(object sender, NaGetTaskQueryArgs e)\r
189                 {\r
190                         MessageBoxButtons buttons = MessageBoxButtons.OKCancel;\r
191                         if (e.SelectionFlag == (NaGetTaskQueryResult.CONTINUE | NaGetTaskQueryResult.RETRY | NaGetTaskQueryResult.CANCEL)) {\r
192                                 buttons = MessageBoxButtons.AbortRetryIgnore;\r
193                         } else if (e.SelectionFlag == (NaGetTaskQueryResult.RETRY | NaGetTaskQueryResult.CANCEL)) {\r
194                                 buttons = MessageBoxButtons.RetryCancel;\r
195                         } else if (e.SelectionFlag == NaGetTaskQueryResult.CONTINUE) {\r
196                                 buttons = MessageBoxButtons.OK;\r
197                         }\r
198                         \r
199                         DialogResult result = MessageBox.Show(e.Message, this.Text, buttons);\r
200                         \r
201                         switch (result) {\r
202                                 case DialogResult.OK:\r
203                                 case DialogResult.Ignore:\r
204                                         return NaGetTaskQueryResult.CONTINUE;\r
205                                 case DialogResult.Cancel:\r
206                                 case DialogResult.Abort:\r
207                                         return NaGetTaskQueryResult.CANCEL;\r
208                                 case DialogResult.Retry:\r
209                                         return NaGetTaskQueryResult.RETRY;\r
210                                 default:\r
211                                         return NaGetTaskQueryResult.CANCELED_AUTOMATICALLY;\r
212                         }\r
213                 }\r
214                 \r
215                 public void SetTaskSet(NaGetTaskSet taskSet)\r
216                 {\r
217                         this.taskSet = taskSet;\r
218                         \r
219                         taskSet.TaskSetRaised += delegate(object sender, NaGetTaskSetEventArgs e) {\r
220                                 if (InvokeRequired) {\r
221                                         Invoke(new EventHandler<NaGetTaskSetEventArgs>(onTaskSetRaised), taskSet, e);\r
222                                 } else {\r
223                                         onTaskSetRaised(taskSet, e);\r
224                                 }\r
225                         };\r
226                         \r
227                         System.Reflection.PropertyInfo fDownloader = taskSet.GetType().GetProperty("Downloader");\r
228                         if (fDownloader != null && (fDownloader.PropertyType == typeof(Downloader))) {\r
229                                 ((Downloader) fDownloader.GetValue(taskSet, null)).DownloadEventRaised += onDownloadEvent;\r
230                         }\r
231                         \r
232                         taskSet.TaskQueryRaised += onTaskQueryRaised;\r
233                 }\r
234 \r
235                 public void StartTaskSet()\r
236                 {\r
237                         tasksetRunningThread = new Thread(taskSet.Run);\r
238                         // スレッドをSTAにしないとCOMアクセスできず、ウイルススキャンができない。\r
239                         tasksetRunningThread.SetApartmentState(ApartmentState.STA);\r
240                         tasksetRunningThread.Start();\r
241                 }\r
242                 \r
243                 #endregion\r
244                 \r
245                 void OkButtonClick(object sender, EventArgs e)\r
246                 {\r
247                         if (taskSet == null || taskSet.Done) {\r
248                                 this.Close();\r
249                                 this.Dispose();\r
250                         }\r
251                 }\r
252                 \r
253                 void CancelButtonClick(object sender, EventArgs e)\r
254                 {\r
255                         if (InvokeRequired) {\r
256                                 Invoke(new EventHandler(CancelButtonClickConcrete), sender, e);\r
257                         } else {\r
258                                 CancelButtonClickConcrete(sender,e);\r
259                         }\r
260                 }\r
261                 \r
262                 void CancelButtonClickConcrete(object sender, EventArgs e)\r
263                 {\r
264                         autoCloseCheckBox.Enabled = false;\r
265                         if (taskSet != null && taskSet.Running && taskSet.Cancelable) {\r
266                                 cancelButton.Enabled = false;\r
267                                 \r
268                                 NativeMethods.ProgressBar_SetState(progressBar, NativeMethods.ProgressBarState.Paused);\r
269                                 NativeMethods.Form_SetTaskbarProgressParams(this, progressBar.Style,NativeMethods.ProgressBarState.Paused, (ulong) progressBar.Value, (ulong) progressBar.Maximum);\r
270                                 \r
271                                 taskSet.Cancel();\r
272                         }\r
273                 }\r
274                 \r
275                 void ExecutionProgressViewerShown(object sender, EventArgs e)\r
276                 {\r
277                         this.BringToFront();\r
278                 }\r
279                 \r
280                 void ExecutionProgressViewerFormClosed(object sender, FormClosedEventArgs e)\r
281                 {\r
282                         autoCloseTimer.Enabled = false;\r
283                 }\r
284                 \r
285                 void AutoCloseTimerTick(object sender, EventArgs e)\r
286                 {\r
287                         autoCloseTimer.Stop();\r
288                         \r
289                         if (InvokeRequired) {\r
290                                 Invoke(new EventHandler(AutoCloseTimerTickConcrete), sender, e);\r
291                         } else {\r
292                                 AutoCloseTimerTickConcrete(sender,e);\r
293                         }\r
294                 }\r
295                 \r
296                 void AutoCloseTimerTickConcrete(object sender, EventArgs e)\r
297                 {\r
298                         // autoCloseCheckBoxが有効(正常終了)かつチェックのときに限り、OKボタンを自動的にクリック\r
299                         if (autoCloseCheckBox.Enabled && autoCloseCheckBox.Checked && okButton.Enabled) {\r
300                                 OkButtonClick(sender, e);\r
301                         } else if (! autoCloseCheckBox.Enabled) {\r
302                                 // タイマーの時間がすぎて、そのとき自動で閉じないようにしていしてあるならば、手動で閉じるように\r
303                                 autoCloseCheckBox.Enabled = false;\r
304                         }\r
305                 }\r
306         }\r
307 }\r