OSDN Git Service

9f704bac627332b044e3306e679f158c1b23c25b
[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                 }\r
42                                 \r
43                 private void onDownloadEvent(object sender, DownloadEventArgs a)\r
44                 {\r
45                         if (InvokeRequired) {\r
46                                 Invoke(new EventHandler<DownloadEventArgs>(onDownloadEvent), sender, a);\r
47                                 return;\r
48                         }\r
49                         \r
50                         \r
51                         if (a.TaskProgressPercent >= 0) {\r
52                                 progressBarSub.Value = (int) a.TaskProgressPercent;\r
53                                 progressBarSub.Style = ProgressBarStyle.Continuous;\r
54                         } else {\r
55                                 progressBarSub.Style = ProgressBarStyle.Marquee;\r
56                         }\r
57                         \r
58                         switch (a.Type) {\r
59                                 case DownloadEventType.INITED:\r
60                                 case DownloadEventType.CONNECTED:\r
61                                         progressSubLabel.Text = a.TaskMessage;\r
62                                         \r
63                                         progressBarSub.Visible = true;\r
64                                         progressSubLabel.Visible = true;\r
65                                         break;\r
66                                 case DownloadEventType.DOWNLOADING:\r
67                                         progressSubLabel.Text = a.TaskMessage;\r
68                                         break;\r
69                                 case DownloadEventType.COMPLETED:\r
70                                         progressBarSub.Visible = false;\r
71                                         progressSubLabel.Visible = false;\r
72                                         break;\r
73                                 case DownloadEventType.ERROR:\r
74                                         progressBarSub.Visible = false;\r
75                                         progressSubLabel.Visible = false;\r
76                                         \r
77                                         logBox.SelectionColor = System.Drawing.Color.Red;\r
78                                         logBox.AppendText("   [エラー] " + a.TaskMessage + System.Environment.NewLine);\r
79                                         logBox.SelectionColor = logBox.ForeColor;\r
80                                         break;\r
81                         }\r
82                 }\r
83                 \r
84                 #region NaGetTaskSet関連\r
85                 \r
86                 private void onTaskSetRaised(object sender, NaGetTaskSetEventArgs e)\r
87                 {\r
88                         NaGetTaskSet taskSet = (NaGetTaskSet) sender;\r
89                         \r
90                         if (e.TaskProgressPercent >= 0) {\r
91                                 progressBar.Value = (int) e.TaskProgressPercent;\r
92                                 progressBar.Style = ProgressBarStyle.Continuous;\r
93                         } else {\r
94                                 progressBar.Style = ProgressBarStyle.Marquee;\r
95                         }\r
96                         progressLabel.Text = e.TaskMessage ?? string.Empty;\r
97                         \r
98                         cancelButton.Enabled = taskSet.Running && taskSet.Cancelable;\r
99                         \r
100                         switch (e.Type) {\r
101                                 case NaGetTaskSetEventType.COMPLETED:\r
102                                         logBox.AppendText("完了." + System.Environment.NewLine);\r
103                                         if (taskSet.Done) {\r
104                                                 okButton.Enabled = true;\r
105                                                 cancelButton.Enabled = false;\r
106                                         }\r
107                                         break;\r
108                                 case NaGetTaskSetEventType.STARTED_TASKSET:\r
109                                         subtitleLabel.Text = taskSet.TaskSetNames[taskSet.CurrentTaskSetIndex];\r
110                                         logBox.AppendText("  " + e.TaskMessage + System.Environment.NewLine);\r
111                                         break;\r
112                                 case NaGetTaskSetEventType.COMPLETED_TASKSET:\r
113                                         if (progressBarSub.Visible) progressBarSub.Hide();\r
114                                         if (progressSubLabel.Visible) progressSubLabel.Hide();\r
115                                         \r
116                                         NativeMethods.ProgressBar_SetState(progressBar, 1); // VistaProgress色:ノーマル\r
117                                         \r
118                                         logBox.AppendText(string.Format(" ... 完了. [{0}%]", (int) e.TaskProgressPercent));\r
119                                         logBox.AppendText(System.Environment.NewLine);\r
120                                         break;\r
121                                 case NaGetTaskSetEventType.INFO:\r
122                                         logBox.AppendText("  " + e.TaskMessage + System.Environment.NewLine);\r
123                                         break;\r
124                                 case NaGetTaskSetEventType.ERROR:\r
125                                         logBox.SelectionColor = System.Drawing.Color.Red;\r
126                                         logBox.AppendText("  [エラー] " + e.TaskMessage + System.Environment.NewLine);\r
127                                         logBox.SelectionColor = logBox.ForeColor;\r
128                                         \r
129                                         NativeMethods.ProgressBar_SetState(progressBar, 2); // VistaProgress色:エラー\r
130                                         \r
131                                         okButton.Enabled = true;\r
132                                         cancelButton.Enabled = false;\r
133                                         break;\r
134                                 case NaGetTaskSetEventType.CANCELED:\r
135                                         logBox.SelectionColor = System.Drawing.Color.Red;\r
136                                         logBox.AppendText(e.TaskMessage + System.Environment.NewLine);\r
137                                         logBox.SelectionColor = logBox.ForeColor;\r
138                                         \r
139                                         NativeMethods.ProgressBar_SetState(progressBar, 1); // VistaProgress色:中断\r
140                                         \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                                         break;\r
150                         }\r
151                         \r
152                         if (taskSet.Done) {\r
153                                 if ((ActionOnDone & ActionOnDoneFlags.FlashWindow) != 0) {\r
154                                         NativeMethods.Form_FlashWindow(this,\r
155                                                                NativeMethods.FlashFlag.All | NativeMethods.FlashFlag.TimerNoFG,\r
156                                                                uint.MaxValue, 0);\r
157                                 }\r
158                                 if (okButton.Enabled && (ActionOnDone & ActionOnDoneFlags.AutoCloseOnSuccess) != 0) {\r
159                                         this.DialogResult = DialogResult.OK;\r
160                                         Close();\r
161                                         Dispose();\r
162                                 }\r
163                         }\r
164                 }\r
165                 \r
166                 private NaGetTaskQueryResult onTaskQueryRaised(object sender, NaGetTaskQueryArgs e)\r
167                 {\r
168                         MessageBoxButtons buttons = MessageBoxButtons.OKCancel;\r
169                         if (e.SelectionFlag == (NaGetTaskQueryResult.CONTINUE | NaGetTaskQueryResult.RETRY | NaGetTaskQueryResult.CANCEL)) {\r
170                                 buttons = MessageBoxButtons.AbortRetryIgnore;\r
171                         } else if (e.SelectionFlag == (NaGetTaskQueryResult.RETRY | NaGetTaskQueryResult.CANCEL)) {\r
172                                 buttons = MessageBoxButtons.RetryCancel;\r
173                         } else if (e.SelectionFlag == NaGetTaskQueryResult.CONTINUE) {\r
174                                 buttons = MessageBoxButtons.OK;\r
175                         }\r
176                         \r
177                         DialogResult result = MessageBox.Show(e.Message, this.Text, buttons);\r
178                         \r
179                         switch (result) {\r
180                                 case DialogResult.OK:\r
181                                 case DialogResult.Ignore:\r
182                                         return NaGetTaskQueryResult.CONTINUE;\r
183                                 case DialogResult.Cancel:\r
184                                 case DialogResult.Abort:\r
185                                         return NaGetTaskQueryResult.CANCEL;\r
186                                 case DialogResult.Retry:\r
187                                         return NaGetTaskQueryResult.RETRY;\r
188                                 default:\r
189                                         return NaGetTaskQueryResult.CANCELED_AUTOMATICALLY;\r
190                         }\r
191                 }\r
192                 \r
193                 public void SetTaskSet(NaGetTaskSet taskSet)\r
194                 {\r
195                         this.taskSet = taskSet;\r
196                         \r
197                         taskSet.TaskSetRaised += delegate(object sender, NaGetTaskSetEventArgs e) {\r
198                                 if (InvokeRequired) {\r
199                                         Invoke(new EventHandler<NaGetTaskSetEventArgs>(onTaskSetRaised), taskSet, e);\r
200                                 } else {\r
201                                         onTaskSetRaised(taskSet, e);\r
202                                 }\r
203                         };\r
204                         \r
205                         System.Reflection.PropertyInfo fDownloader = taskSet.GetType().GetProperty("Downloader");\r
206                         if (fDownloader != null && (fDownloader.PropertyType == typeof(Downloader))) {\r
207                                 ((Downloader) fDownloader.GetValue(taskSet, null)).DownloadEventRaised += onDownloadEvent;\r
208                         }\r
209                         \r
210                         taskSet.TaskQueryRaised += onTaskQueryRaised;\r
211                 }\r
212 \r
213                 public void StartTaskSet()\r
214                 {\r
215                         tasksetRunningThread = new Thread(taskSet.Run);\r
216                         tasksetRunningThread.Start();\r
217                 }\r
218                 \r
219                 #endregion\r
220                 \r
221                 void OkButtonClick(object sender, EventArgs e)\r
222                 {\r
223                         if (taskSet == null || taskSet.Done) {\r
224                                 this.Close();\r
225                                 this.Dispose();\r
226                         }\r
227                 }\r
228                 \r
229                 void CancelButtonClick(object sender, EventArgs e)\r
230                 {\r
231                         if (InvokeRequired) {\r
232                                 Invoke(new EventHandler(CancelButtonClickConcrete), sender, e);\r
233                         } else {\r
234                                 CancelButtonClickConcrete(sender,e);\r
235                         }\r
236                 }\r
237                 \r
238                 void CancelButtonClickConcrete(object sender, EventArgs e)\r
239                 {\r
240                         if (taskSet != null && taskSet.Running && taskSet.Cancelable) {\r
241                                 cancelButton.Enabled = false;\r
242                                 \r
243                                 NativeMethods.ProgressBar_SetState(progressBar, 3); // VistaProgress色:中断\r
244                                 \r
245                                 taskSet.Cancel();\r
246                         }\r
247                 }\r
248                 \r
249                 void ExecutionProgressViewerShown(object sender, EventArgs e)\r
250                 {\r
251                         this.BringToFront();\r
252                 }\r
253         }\r
254 }\r