OSDN Git Service

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