OSDN Git Service

5b60704de4f311af5d502b29673807bc8a7474a0
[applistation/AppliStation.git] / AppliStation / InstallationConfirmForm.cs
1 using System;\r
2 using System.Collections.Generic;\r
3 using System.Drawing;\r
4 using System.Windows.Forms;\r
5 using NaGet.Packages;\r
6 using NaGet.Packages.Install;\r
7 \r
8 namespace AppliStation\r
9 {\r
10         /// <summary>\r
11         /// Description of PackagesInstallConfirmForm.\r
12         /// </summary>\r
13         public partial class InstallationConfirmForm : Form\r
14         {\r
15                 private Installation[] requiredInstallations = null;\r
16                 \r
17                 private Installation[] selectedInstallations = null;\r
18                 \r
19                 private PackageListsManager pkgListMan;\r
20 \r
21                 public IEnumerable<Installation> Installations {\r
22                         get {\r
23                                 return NaGet.Utils.MeargeEnumerable<Installation>(requiredInstallations, selectedInstallations);\r
24                         }\r
25                         set {\r
26                                 requiredInstallations = null;\r
27                                 selectedInstallations = NaGet.Utils.IEnumerable2Array<Installation>(value);\r
28                                 \r
29                                 updateInstsListView();\r
30                         }\r
31                 }\r
32                 \r
33                 public PackageListsManager PkgListsManager {\r
34                         get { return pkgListMan; }\r
35                         set {\r
36                                 pkgListMan = value;\r
37                                 \r
38                                 updateInstsListView();\r
39                         }\r
40                 }\r
41                 \r
42                 public InstallationConfirmForm()\r
43                 {\r
44                         //\r
45                         // The InitializeComponent() call is required for Windows Forms designer support.\r
46                         //\r
47                         InitializeComponent();\r
48                         \r
49                         // 管理者権限で動いているならばrunasが必要にはならないので表示しない\r
50                         if (NaGet.Utils.IsAdministrators()) {\r
51                                 runasCheckBox.Checked = false;\r
52                                 runasCheckBox.Visible = false;\r
53                         }\r
54                         \r
55                         // ListViewの効果\r
56                         AppliStation.Util.NativeMethods.ListView_EnableVistaExplorerTheme(instsListView);\r
57                         AppliStation.Util.NativeMethods.ListView_SetDoubleBuffer(instsListView, true);\r
58                 }\r
59                 \r
60                 #region インストールリスト表示処理部\r
61                 \r
62                 #region インストールリストの反映\r
63                 \r
64                 /// <summary>\r
65                 /// インストールリストを更新したなどで、リストの表示を更新する\r
66                 /// </summary>\r
67                 private void updateInstsListView()\r
68                 {\r
69                         instsListView.Items.Clear();\r
70                         \r
71                         addInstsListItemPerGroup(requiredInstallations, instsListView.Groups["requires"], true);\r
72                         addInstsListItemPerGroup(selectedInstallations, instsListView.Groups["install"], false);\r
73                         \r
74                         InstsListViewItemChecked(instsListView, null);\r
75                         instsListView.Refresh();\r
76                 }\r
77 \r
78                 /// <summary>\r
79                 /// 指定したグループのリストの表示を更新する。\r
80                 /// </summary>\r
81                 /// <param name="insts">インストールリスト</param>\r
82                 /// <param name="group">対象のグループ</param>\r
83                 /// <param name="firstAppend">先頭に追加するか</param>\r
84                 private void addInstsListItemPerGroup(IEnumerable<Installation> insts, ListViewGroup group, bool firstAppend)\r
85                 {\r
86                         // まず所属グループのアイテムをすべて削除する\r
87                         if (insts == null) return;\r
88                         \r
89                         List<ListViewItem> itemsToAdd = new List<ListViewItem>();\r
90                         foreach (Installation inst in insts) {\r
91                                 Package pkg = inst.InstalledPackage;\r
92                                 \r
93                                 string[] itemData = new string[instsListView.Columns.Count];\r
94                                 itemData[nameColumnHeader.Index] = pkg.Name;\r
95                                 \r
96                                 inst.Silent = true; // silent install as possible!\r
97                                 \r
98                                 Package curPkg = null;\r
99                                 if (pkgListMan != null) {       \r
100                                         curPkg = pkgListMan.InstalledPkgList.GetPackageForName(pkg.Name) ??\r
101                                                 pkgListMan.SystemInstalledPkgList.GetPackageForName(pkg.Name);\r
102                                 }\r
103                                 itemData[versionColumnHeader.Index]        = pkg.Version;\r
104                                 itemData[currentVersionColumnHeader.Index] = (curPkg != null)? curPkg.Version : "-";\r
105                                 // itemData[silentInstColumnHeader.Index] の設定は instViewUpdateSilentInstallView で\r
106                                 \r
107                                 ListViewItem item = new ListViewItem(itemData);\r
108                                 item.Tag = inst;\r
109                                 item.ToolTipText = pkg.Summary;\r
110                                 item.Checked = true;\r
111                                 item.Group = group;\r
112                                 instViewUpdateSilentInstallView(item);\r
113                                 \r
114                                 itemsToAdd.Add(item);\r
115                         }\r
116                         \r
117                         if (firstAppend) {\r
118                                 for (int i = 0; i < itemsToAdd.Count; i++) {\r
119                                         instsListView.Items.Insert(i, itemsToAdd[i]);\r
120                                 }\r
121                         } else {\r
122                                 instsListView.Items.AddRange(itemsToAdd.ToArray());\r
123                         }\r
124                 }\r
125 \r
126                 #endregion\r
127                 \r
128                 /// <summary>\r
129                 /// アイテムのサイレントインストール部分の表示の更新を行う。\r
130                 /// </summary>\r
131                 /// <param name="item">対象のインストーラのリストアイテム</param>\r
132                 private void instViewUpdateSilentInstallView(ListViewItem item)\r
133                 {\r
134                         Installation inst = (Installation) item.Tag;\r
135                         item.SubItems[silentInstColumnHeader.Index].Text =\r
136                                 (inst.SupportsSilentOnly)? "サイレントインストールのみサポート" :\r
137                                 (inst.Silent)? "サイレントインストール" :\r
138                                 (inst.IsSupportsSilent)? "手動でインストール" :\r
139                                 "サイレントインストールできませんので、手動でインストールします";\r
140                 }\r
141                 \r
142                 #region instsListViewのオーナードドロー関連\r
143                 \r
144                 void InstsListViewDrawSubItem(object sender, DrawListViewSubItemEventArgs e)\r
145                 {\r
146                         if (e.Header == silentInstColumnHeader) {\r
147                                 Installation inst = ((Installation) e.Item.Tag);\r
148                                 \r
149                                 //e.DrawBackground();\r
150                                 e.Graphics.Clip.Intersect(e.Bounds);\r
151                                 \r
152                                 if (inst.Silent) {\r
153                                         AppliStation.Util.GUIUtils.Graphics_DrawCenterImage(\r
154                                                 e.Graphics,\r
155                                                 instListViewSilentInstallImageList.Images[0],\r
156                                                 e.Bounds, null);\r
157                                 } else if (inst.IsSupportsSilent) {\r
158                                         AppliStation.Util.GUIUtils.Graphics_DrawCenterImage(\r
159                                                 e.Graphics,\r
160                                                 instListViewSilentInstallImageList.Images[0],\r
161                                                 e.Bounds,\r
162                                                 AppliStation.Util.GUIUtils.GetImageAttributeToGrayOut(0.5f));\r
163                                 }\r
164                         } else {\r
165                                 e.DrawDefault = true;\r
166                         }\r
167                 }\r
168                 \r
169                 void InstsListViewDrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)\r
170                 {\r
171                         if (e.Header == silentInstColumnHeader) {\r
172                                 e.DrawBackground();\r
173                                 e.Graphics.Clip.Intersect(e.Bounds);\r
174                                 AppliStation.Util.GUIUtils.Graphics_DrawCenterImage(\r
175                                         e.Graphics,\r
176                                         instListViewSilentInstallImageList.Images[0],\r
177                                         e.Bounds, null);\r
178                         } else {\r
179                                 e.DrawDefault = true;\r
180                         }\r
181                 }\r
182                 \r
183                 #endregion\r
184                 \r
185                 #endregion\r
186                 \r
187                 void InstsListViewItemChecked(object sender, ItemCheckedEventArgs e)\r
188                 {\r
189                         System.Windows.Forms.ListView.CheckedListViewItemCollection checkeds = instsListView.CheckedItems;\r
190                         \r
191                         okButton.Enabled = (checkeds != null) && (checkeds.Count > 0);\r
192                 }\r
193                                 \r
194                 void InstsListViewContextMenuStripOpening(object sender, System.ComponentModel.CancelEventArgs e)\r
195                 {\r
196                         System.Windows.Forms.ListView.CheckedIndexCollection  chkIdxes = instsListView.CheckedIndices;\r
197                         System.Windows.Forms.ListView.SelectedIndexCollection selIdxes = instsListView.SelectedIndices;\r
198                         \r
199                         silentInstallStripMenuItem.Visible = selIdxes.Count > 0;\r
200                         if (selIdxes.Count > 0) {\r
201                                 Installation inst0th = ((Installation) instsListView.Items[selIdxes[0]].Tag);\r
202                                 bool bChecked = inst0th.Silent;\r
203                                 bool bEnabled = (inst0th.IsSupportsSilent && (!inst0th.SupportsSilentOnly));\r
204                                 silentInstallStripMenuItem.Checked = bChecked;\r
205                                 silentInstallStripMenuItem.Enabled = bEnabled;\r
206                                 \r
207                                 for (int i = 1; i < selIdxes.Count; i++) {\r
208                                         Installation inst = ((Installation) instsListView.Items[selIdxes[i]].Tag);\r
209                                         \r
210                                         if ( (bChecked != inst.Silent) ||\r
211                                             (bEnabled != (inst.IsSupportsSilent && (!inst.SupportsSilentOnly))) ) {\r
212                                                 silentInstallStripMenuItem.CheckState = CheckState.Indeterminate;\r
213                                                 silentInstallStripMenuItem.Enabled = false;\r
214                                                 break;\r
215                                         }\r
216                                 }\r
217                         }\r
218                         instListToolStripSeparator.Visible = selIdxes.Count > 0;\r
219                 \r
220                         \r
221                         selectAllInstsStripMenuItem.Enabled = chkIdxes.Count < instsListView.Items.Count;\r
222                         unselectAllInstsStripMenuItem.Enabled = chkIdxes.Count > 0;\r
223                 }\r
224                 \r
225                 void SilentInstallStripMenuItemClick(object sender, EventArgs e)\r
226                 {\r
227                         bool silent = ! silentInstallStripMenuItem.Checked;\r
228                         foreach (ListViewItem item in instsListView.SelectedItems) {\r
229                                 ((Installation) item.Tag).Silent = silent;\r
230                                 instViewUpdateSilentInstallView(item);\r
231                         }\r
232                 }\r
233                                 \r
234                 void SelectAllInstsStripMenuItemClick(object sender, EventArgs e)\r
235                 {\r
236                         foreach (ListViewItem item in instsListView.Items) {\r
237                                 item.Checked = true;\r
238                         }\r
239                         instsListView.Refresh();\r
240                 }\r
241                 \r
242                 void UnselectAllInstsStripMenuItemClick(object sender, EventArgs e)\r
243                 {\r
244                         foreach (ListViewItem item in instsListView.CheckedItems) {\r
245                                 item.Checked = false;\r
246                         }\r
247                         instsListView.Refresh();\r
248                 }\r
249 \r
250                 void InstallationConfirmFormShown(object sender, EventArgs e)\r
251                 {\r
252                         System.Threading.Thread thread = new System.Threading.Thread(\r
253                                 new System.Threading.ThreadStart(resolveDependecies));\r
254                         thread.Start();\r
255                 }\r
256                 \r
257                 /// <summary>\r
258                 /// 依存関係を解決する\r
259                 /// </summary>\r
260                 private void resolveDependecies()\r
261                 {\r
262                         if (requiredInstallations == null) {\r
263                                 Installation[] resolved, dependencies;\r
264                                 \r
265                                 DependeciesResolver.ResolveInstallations(\r
266                                         selectedInstallations,\r
267                                         pkgListMan,\r
268                                         out resolved,\r
269                                         out dependencies);\r
270                                 \r
271                                 requiredInstallations = dependencies;\r
272                                 \r
273                                 updateInstsListView();\r
274                         }\r
275                 }\r
276                 \r
277                 /// <summary>\r
278                 /// インストールするよう選択されたパッケージの配列\r
279                 /// </summary>\r
280                 public Installation[] CheckedInstallations {\r
281                         get {\r
282                                 List<Installation> insts = new List<Installation>();\r
283                                 foreach (ListViewItem item in instsListView.CheckedItems) {\r
284                                         insts.Add((Installation) item.Tag);\r
285                                 }\r
286                                 return insts.ToArray();\r
287                         }\r
288                 }\r
289                 \r
290                 #region runas関連\r
291                 \r
292                 /// <summary>\r
293                 /// runasで実行するか否か\r
294                 /// </summary>\r
295                 public bool UseRunas {\r
296                         set {\r
297                                 runasCheckBox.Checked = (! NaGet.Utils.IsAdministrators()) && value;\r
298                                 \r
299                                 updateUseRunas();\r
300                         }\r
301                         get {\r
302                                 return runasCheckBox.Checked;\r
303                         }\r
304                 }\r
305                 \r
306                 /// <summary>\r
307                 /// 選択されたパッケージを調査して、Runasを使うべきかいなかを返す\r
308                 /// </summary>\r
309                 public bool GetShouldUseRunas()\r
310                 {\r
311                         if (NaGet.Utils.IsAdministrators()) {\r
312                                 // 管理者権限で動いている場合は不要\r
313                                 return false;   \r
314                         } else if (NaGet.Utils.IsUACEnabled()) {\r
315                                 // UACが適用されている場合は標準では不要とする\r
316                                 return false;   \r
317                         }\r
318                         \r
319                         // ひとつでもPCターゲットなインストーラがあれば必要とする\r
320                         foreach (Installation inst in CheckedInstallations) {\r
321                                 if (inst.TargetPC) return true;\r
322                         }\r
323                         // それ以外は不要\r
324                         return false;\r
325                 }\r
326                 \r
327                 void RunasCheckBoxCheckedChanged(object sender, EventArgs e)\r
328                 {\r
329                         updateUseRunas();\r
330                 }\r
331                 \r
332                 private void updateUseRunas()\r
333                 {\r
334                         if (UseRunas) {\r
335                                 System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(InstallationConfirmForm));\r
336                                 okButton.Image = ((System.Drawing.Bitmap)(resources.GetObject("okButton.Image")));\r
337                         } else {\r
338                                 okButton.Image = null;\r
339                         }\r
340                 }\r
341                 \r
342                 #endregion\r
343 \r
344         }\r
345 }\r