OSDN Git Service

AppliStation-GUI,基本的にサイレントインストールをデフォルトで利用するようにした
[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                 \r
58                 private void updateInstsListView()\r
59                 {\r
60                         instsListView.Items.Clear();\r
61                         \r
62                         if (installations != null) {\r
63                                 foreach (Installation inst in installations) {\r
64                                         Package pkg = inst.InstalledPackage;\r
65                                         \r
66                                         string[] itemData = new string[instsListView.Columns.Count];\r
67                                         itemData[nameColumnHeader.Index] = pkg.Name;\r
68                                         \r
69                                         Package curPkg = null;\r
70                                         if (pkgListMan != null) {       \r
71                                                 curPkg = pkgListMan.InstalledPkgList.GetPackageForName(pkg.Name) ??\r
72                                                         pkgListMan.SystemInstalledPkgList.GetPackageForName(pkg.Name);\r
73                                         }\r
74                                         itemData[versionColumnHeader.Index]        = pkg.Version;\r
75                                         itemData[currentVersionColumnHeader.Index] = (curPkg != null)? curPkg.Version : "-";\r
76                                         \r
77                                         itemData[silentInstColumnHeader.Index]     = \r
78                                                 (inst.SupportsSilentOnly)? "無人インストールのみサポート" :\r
79                                                 (inst.Silent)? "無人インストール" :\r
80                                                 (inst.IsSupportsSilent)? "マニュアルでインストール" :\r
81                                                 "マニュアルでしかインストールできません";\r
82                                         \r
83                                         ListViewItem item = new ListViewItem(itemData);\r
84                                         item.Tag = inst;\r
85                                         item.ToolTipText = pkg.Summary;\r
86                                         item.Checked = true;\r
87                                         \r
88                                         instsListView.Items.Add(item);\r
89                                 }\r
90                         }\r
91                         \r
92                         InstsListViewItemChecked(instsListView, null);\r
93                         instsListView.Refresh();\r
94                 }\r
95                 \r
96                 #region instsListViewのオーナードドロー関連\r
97                 \r
98                 void InstsListViewDrawSubItem(object sender, DrawListViewSubItemEventArgs e)\r
99                 {\r
100                         if (e.Header == silentInstColumnHeader) {\r
101                                 Installation inst = ((Installation) e.Item.Tag);\r
102                                 \r
103                                 //e.DrawBackground();\r
104                                 e.Graphics.Clip.Intersect(e.Bounds);\r
105                                 \r
106                                 if (inst.Silent) {\r
107                                         AppliStation.Util.GUIUtils.Graphics_DrawCenterImage(\r
108                                                 e.Graphics,\r
109                                                 instListViewSilentInstallImageList.Images[0],\r
110                                                 e.Bounds, null);\r
111                                 } else if (inst.IsSupportsSilent) {\r
112                                         AppliStation.Util.GUIUtils.Graphics_DrawCenterImage(\r
113                                                 e.Graphics,\r
114                                                 instListViewSilentInstallImageList.Images[0],\r
115                                                 e.Bounds,\r
116                                                 AppliStation.Util.GUIUtils.GetImageAttributeToGrayOut(0.5f));\r
117                                 }\r
118                         } else {\r
119                                 e.DrawDefault = true;\r
120                         }\r
121                 }\r
122                 \r
123                 void InstsListViewDrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)\r
124                 {\r
125                         if (e.Header == silentInstColumnHeader) {\r
126                                 e.DrawBackground();\r
127                                 e.Graphics.Clip.Intersect(e.Bounds);\r
128                                 AppliStation.Util.GUIUtils.Graphics_DrawCenterImage(\r
129                                         e.Graphics,\r
130                                         instListViewSilentInstallImageList.Images[0],\r
131                                         e.Bounds, null);\r
132                         } else {\r
133                                 e.DrawDefault = true;\r
134                         }\r
135                 }\r
136                 \r
137                 #endregion\r
138                 \r
139                 void InstsListViewItemChecked(object sender, ItemCheckedEventArgs e)\r
140                 {\r
141                         System.Windows.Forms.ListView.CheckedListViewItemCollection checkeds = instsListView.CheckedItems;\r
142                         \r
143                         okButton.Enabled = checkeds != null && checkeds.Count > 0;\r
144                 }\r
145                 \r
146                 AppliStation.Util.ListViewItemSortComparer packageListViewSortComparer;\r
147 \r
148                 void InstsListViewColumnClick(object sender, ColumnClickEventArgs e)\r
149                 {\r
150                         SortOrder order = SortOrder.None;\r
151                         \r
152                         if (packageListViewSortComparer == null) {\r
153                                 order = SortOrder.Ascending;\r
154                                 packageListViewSortComparer = new AppliStation.Util.ListViewItemSortComparer(e.Column, order);\r
155                                 instsListView.ListViewItemSorter = packageListViewSortComparer;\r
156                         } else {\r
157                                 if (packageListViewSortComparer.Column == e.Column) {\r
158                                         order = (packageListViewSortComparer.Order == SortOrder.Ascending)? SortOrder.Descending : SortOrder.Ascending;\r
159                                         \r
160                                         packageListViewSortComparer.Order = order;\r
161                                 } else {\r
162                                         order = packageListViewSortComparer.Order;\r
163                                         packageListViewSortComparer.Column = e.Column;\r
164                                 }\r
165                                 \r
166                                 instsListView.Sort();\r
167                         }\r
168                         AppliStation.Util.NativeMethods.ColumnHeader_SetSortState(instsListView, e.Column, order);\r
169                         \r
170                         // ソート対象列の色付け\r
171                         try {\r
172                                 // SendMessage(hWnd, LVM_SETSELECTEDCOLUMN, column, NULL);\r
173                                 AppliStation.Util.NativeMethods.SendMessage(instsListView.Handle, 0x1000+140, (uint) e.Column, 0);\r
174                         } catch (Exception) {   \r
175                         }\r
176                 }\r
177                 \r
178                                 \r
179                 void InstsListViewContextMenuStripOpening(object sender, System.ComponentModel.CancelEventArgs e)\r
180                 {\r
181                         System.Windows.Forms.ListView.CheckedIndexCollection  chkIdxes = instsListView.CheckedIndices;\r
182                         System.Windows.Forms.ListView.SelectedIndexCollection selIdxes = instsListView.SelectedIndices;\r
183                         \r
184                         silentInstallStripMenuItem.Visible = selIdxes.Count > 0;\r
185                         if (selIdxes.Count > 0) {\r
186                                 Installation inst0th = ((Installation) instsListView.Items[selIdxes[0]].Tag);\r
187                                 bool bChecked = inst0th.Silent;\r
188                                 bool bEnabled = (inst0th.IsSupportsSilent && (!inst0th.SupportsSilentOnly));\r
189                                 silentInstallStripMenuItem.Checked = bChecked;\r
190                                 silentInstallStripMenuItem.Enabled = bEnabled;\r
191                                 \r
192                                 for (int i = 1; i < selIdxes.Count; i++) {\r
193                                         Installation inst = ((Installation) instsListView.Items[selIdxes[i]].Tag);\r
194                                         \r
195                                         if ( (bChecked != inst.Silent) ||\r
196                                             (bEnabled != (inst.IsSupportsSilent && (!inst.SupportsSilentOnly))) ) {\r
197                                                 silentInstallStripMenuItem.CheckState = CheckState.Indeterminate;\r
198                                                 silentInstallStripMenuItem.Enabled = false;\r
199                                                 break;\r
200                                         }\r
201                                 }\r
202                         }\r
203                         instListToolStripSeparator.Visible = selIdxes.Count > 0;\r
204                 \r
205                         \r
206                         selectAllInstsStripMenuItem.Enabled = chkIdxes.Count < instsListView.Items.Count;\r
207                         unselectAllInstsStripMenuItem.Enabled = chkIdxes.Count > 0;\r
208                 }\r
209                 \r
210                 void SilentInstallStripMenuItemClick(object sender, EventArgs e)\r
211                 {\r
212                         bool silent = ! silentInstallStripMenuItem.Checked;\r
213                         foreach (ListViewItem item in instsListView.CheckedItems) {\r
214                                 ((Installation) item.Tag).Silent = silent;\r
215                         }\r
216                         updateInstsListView();\r
217                 }\r
218                                 \r
219                 void SelectAllInstsStripMenuItemClick(object sender, EventArgs e)\r
220                 {\r
221                         foreach (ListViewItem item in instsListView.Items) {\r
222                                 item.Checked = true;\r
223                         }\r
224                         instsListView.Refresh();\r
225                 }\r
226                 \r
227                 void UnselectAllInstsStripMenuItemClick(object sender, EventArgs e)\r
228                 {\r
229                         foreach (ListViewItem item in instsListView.CheckedItems) {\r
230                                 item.Checked = false;\r
231                         }\r
232                         instsListView.Refresh();\r
233                 }\r
234                 \r
235                 /// <summary>\r
236                 /// インストールするよう選択されたパッケージの配列\r
237                 /// </summary>\r
238                 public Installation[] CheckedInstallations {\r
239                         get {\r
240                                 System.Collections.Generic.List<Installation> insts = new System.Collections.Generic.List<Installation>();\r
241                                 foreach (ListViewItem item in instsListView.CheckedItems) {\r
242                                         insts.Add((Installation) item.Tag);\r
243                                 }\r
244                                 return insts.ToArray();\r
245                         }\r
246                 }\r
247                 \r
248                 #region runas関連\r
249                 \r
250                 /// <summary>\r
251                 /// runasで実行するか否か\r
252                 /// </summary>\r
253                 public bool UseRunas {\r
254                         set {\r
255                                 runasCheckBox.Checked = (! NaGet.Utils.IsAdministrators()) && value;\r
256                                 \r
257                                 updateUseRunas();\r
258                         }\r
259                         get {\r
260                                 return runasCheckBox.Checked;\r
261                         }\r
262                 }\r
263                 \r
264                 /// <summary>\r
265                 /// 選択されたパッケージを調査して、Runasを使うべきかいなかを返す\r
266                 /// </summary>\r
267                 public bool GetShouldUseRunas()\r
268                 {\r
269                         if (NaGet.Utils.IsAdministrators()) {\r
270                                 // 管理者権限で動いている場合は不要\r
271                                 return false;   \r
272                         } else if (NaGet.Utils.IsUACEnabled()) {\r
273                                 // UACが適用されている場合は標準では不要とする\r
274                                 return false;   \r
275                         }\r
276                         \r
277                         // ひとつでもPCターゲットなインストーラがあれば必要とする\r
278                         foreach (Installation inst in CheckedInstallations) {\r
279                                 if (inst.TargetPC) return true;\r
280                         }\r
281                         // それ以外は不要\r
282                         return false;\r
283                 }\r
284                 \r
285                 void RunasCheckBoxCheckedChanged(object sender, EventArgs e)\r
286                 {\r
287                         updateUseRunas();\r
288                 }\r
289                 \r
290                 private void updateUseRunas()\r
291                 {\r
292                         if (UseRunas) {\r
293                                 System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(InstallationConfirmForm));\r
294                                 okButton.Image = ((System.Drawing.Bitmap)(resources.GetObject("okButton.Image")));\r
295                         } else {\r
296                                 okButton.Image = null;\r
297                         }\r
298                 }\r
299                 \r
300                 #endregion\r
301                 \r
302         }\r
303 }\r