OSDN Git Service

na-get-lib,新構造タスクのキャンセル処理を修正。
[applistation/AppliStation.git] / na-get-lib / NaGet.SubCommands / NaGetTaskSet2.cs
1 using System;
2 using System.Collections.Generic;
3 using NaGet.SubCommands.SubTask;
4 using NaGet.Tasks;
5
6 namespace NaGet.SubCommands
7 {
8         /// <summary>
9         /// NaGetタスク処理のパック
10         /// </summary>
11         public abstract class NaGetTaskSet2 : NaGetTaskSet
12         {
13                 /// <summary>
14                 /// サブタスクのハンドラ
15                 /// </summary>
16                 public virtual event EventHandler<TaskEventArgs> SubTaskEventRaised;
17                 
18                 /// <summary>
19                 /// サブタスクのリスト
20                 /// </summary>
21                 protected IList<NaGetSubTask> subTasks;
22                 
23                 /// <summary>
24                 /// 現在実行中のサブタスクのインデックス
25                 /// </summary>
26                 private int currentSubTaskIndex = -1;
27                 
28                 /// <summary>
29                 /// 終了したか
30                 /// </summary>
31                 protected bool isDone = false;
32                 
33                 /// <summary>
34                 /// キャンセルされたか
35                 /// </summary>
36                 protected bool isCancelled = false;
37                 
38                 /// <summary>
39                 /// キャンセルが呼ばれたか
40                 /// </summary>
41                 protected bool cancelCalled = false;
42                 
43                 /// <summary>
44                 /// キャンセル呼び出し時のイベント。
45                 /// </summary>
46                 protected event Action<object> onCancelCalled;
47                 
48                 public NaGetTaskSet2()
49                 {
50                 }
51                 
52                 /// <summary>
53                 /// キャンセル処理を行う
54                 /// </summary>
55                 /// <returns>成功したか否か</returns>
56                 public override bool Cancel()
57                 {
58                         if (Cancelable) {
59                                 if (! cancelCalled && ! isDone) {
60                                         cancelCalled = true;
61                                         if (onCancelCalled != null) {
62                                                 onCancelCalled(null);
63                                         }
64                                         
65                                         if (currentSubTask != null && currentSubTask.Cancelable) {
66                                                 return currentSubTask.Cancel();
67                                         } else {
68                                                 return true;
69                                         }
70                                 } else {
71                                         return false;
72                                 }
73                         } else {
74                                 return false;
75                         }
76                 }
77                 
78                 public override int CurrentTaskSetIndex {
79                         get { return currentSubTaskIndex; }
80                 }
81                 
82                 public override bool Done {
83                         get { return isDone; }
84                 }
85                 
86                 public virtual bool Cancelled {
87                         get { return isCancelled; }
88                 }
89                 
90                 #region フラグ処理の便利メソッド
91                 
92                 /// <summary>
93                 /// 開始時に関するフラグの処理を行う
94                 /// </summary>
95                 protected virtual void NotifyStarted()
96                 {
97                         currentSubTaskIndex = 0;
98                         isDone = false;
99                 }
100                 
101                 /// <summary>
102                 /// キャンセル中断時に関するフラグの処理を行う
103                 /// </summary>
104                 protected virtual void NotifyCancelled()
105                 {
106                         isCancelled = true;
107                         isDone = true;
108                 }
109                 
110                 /// <summary>
111                 /// 成功終了時に関するフラグの処理を行う
112                 /// </summary>
113                 protected virtual void NotifyCompleted()
114                 {
115                         isDone = true;
116                 }
117                 
118                 /// <summary>
119                 /// サブタスク実行を次へ。
120                 /// </summary>
121                 protected virtual void NotifyGoToNextSubTask()
122                 {
123                         currentSubTaskIndex ++;
124                 }
125                 
126                 /// <summary>
127                 /// サブタスク実行をジャンプする。
128                 /// </summary>
129                 /// <param name="subTaskIndex">ジャンプ先のサブタスク番号</param>
130                 protected virtual void NotifyGoToSubTask(int subTaskIndex)
131                 {
132                         currentSubTaskIndex = subTaskIndex;
133                 }
134                 
135                 #endregion
136                 
137                 #region サブタスク実行時における便利メソッド
138                                 
139                 protected virtual string currentSubTaskName {
140                         get { return taskSetNames[currentSubTaskIndex]; }
141                 }
142                 
143                 protected virtual NaGetSubTask currentSubTask {
144                         get { return subTasks[currentSubTaskIndex]; }
145                 }
146                 
147                 protected virtual bool hasMoreSubTask {
148                         get { return currentSubTaskIndex < taskSetNames.Count; }
149                 }
150                 
151                 #endregion
152                 
153                 #region サブタスク初期化・登録便利メソッド
154                 
155                 protected void initSubTask()
156                 {
157                         taskSetNames = new List<string>();
158                         subTasks = new List<NaGetSubTask>();
159                 }
160                 
161                 /// <summary>
162                 /// サブタスクを登録する
163                 /// </summary>
164                 /// <param name="name">名称</param>
165                 /// <param name="task">サブタスクオブジェクト</param>
166                 protected void registSubTask(string name, NaGetSubTask task)
167                 {
168                         taskSetNames.Add(name);
169                         subTasks.Add(task);
170                         
171                         // サブタスクのイベントをSubTaskEventRaised,TaskEventRaisedに転送
172                         task.TaskEventRaised += new EventHandler<TaskEventArgs>(subtaskEventTransformer);
173                 }
174                 
175                 /// <summary>
176                 /// サブタスクのイベントをSubTaskEventRaised,TaskEventRaisedに転送する関数
177                 /// </summary>
178                 /// <param name="sender">送信元のサブタスク</param>
179                 /// <param name="e">イベントオブジェクト</param>
180                 private void subtaskEventTransformer(object sender, TaskEventArgs e)
181                 {
182                         if (SubTaskEventRaised != null) {
183                                 SubTaskEventRaised(sender, e);
184                         }
185                         if (e.ProgressPercent >= 0) {
186                                 RaiseTaskSetEvent(TaskEventType.PING, null, GetProgressPercent(TaskEventType.PING, e.ProgressPercent));
187                         }
188                 }
189                 
190                 #endregion
191
192         }
193 }