OSDN Git Service

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