OSDN Git Service

13f812feecd154678e4ad5434fd3cc41565fbe4e
[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                 /// \83p\83b\83P\81[\83W\83C\83\93\83X\83g\81[\83\8b\8f\88\97\9d\82Ì\8fW\8d\87\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                         // \8aÇ\97\9d\8eÒ\8c \8cÀ\82Å\93®\82¢\82Ä\82¢\82é\82È\82ç\82Îrunas\82ª\95K\97v\82É\82Í\82È\82ç\82È\82¢\82Ì\82Å\95\\8e¦\82µ\82È\82¢\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[instOptsColumnHeader.Index] = inst.Silent? "\83T\83C\83\8c\83\93\83g" : string.Empty;\r
73                                         \r
74                                         ListViewItem item = new ListViewItem(itemData);\r
75                                         item.Tag = inst;\r
76                                         item.ToolTipText = pkg.Summary;\r
77                                         item.Checked = true;\r
78                                         \r
79                                         instsListView.Items.Add(item);\r
80                                 }\r
81                         }\r
82                         \r
83                         InstsListViewItemChecked(instsListView, null);\r
84                         instsListView.Refresh();\r
85                 }\r
86                 \r
87                 void InstsListViewItemChecked(object sender, ItemCheckedEventArgs e)\r
88                 {\r
89                         System.Windows.Forms.ListView.CheckedListViewItemCollection checkeds = instsListView.CheckedItems;\r
90                         \r
91                         okButton.Enabled = checkeds != null && checkeds.Count > 0;\r
92                 }\r
93                 \r
94                 AppliStation.Util.ListViewItemSortComparer packageListViewSortComparer;\r
95 \r
96                 void InstsListViewColumnClick(object sender, ColumnClickEventArgs e)\r
97                 {\r
98                         SortOrder order = SortOrder.None;\r
99                         \r
100                         if (packageListViewSortComparer == null) {\r
101                                 order = SortOrder.Ascending;\r
102                                 packageListViewSortComparer = new AppliStation.Util.ListViewItemSortComparer(e.Column, order);\r
103                                 instsListView.ListViewItemSorter = packageListViewSortComparer;\r
104                         } else {\r
105                                 if (packageListViewSortComparer.Column == e.Column) {\r
106                                         order = (packageListViewSortComparer.Order == SortOrder.Ascending)? SortOrder.Descending : SortOrder.Ascending;\r
107                                         \r
108                                         packageListViewSortComparer.Order = order;\r
109                                 } else {\r
110                                         order = packageListViewSortComparer.Order;\r
111                                         packageListViewSortComparer.Column = e.Column;\r
112                                 }\r
113                                 \r
114                                 instsListView.Sort();\r
115                         }\r
116                         AppliStation.Util.NativeMethods.ColumnHeader_SetSortState(instsListView, e.Column, order);\r
117                         \r
118                         // \83\\81[\83g\91Î\8fÛ\97ñ\82Ì\90F\95t\82¯\r
119                         try {\r
120                                 // SendMessage(hWnd, LVM_SETSELECTEDCOLUMN, column, NULL);\r
121                                 AppliStation.Util.NativeMethods.SendMessage(instsListView.Handle, 0x1000+140, (uint) e.Column, 0);\r
122                         } catch (Exception) {   \r
123                         }\r
124                 }\r
125                 \r
126                                 \r
127                 void InstsListViewContextMenuStripOpening(object sender, System.ComponentModel.CancelEventArgs e)\r
128                 {\r
129                         System.Windows.Forms.ListView.CheckedIndexCollection  chkIdxes = instsListView.CheckedIndices;\r
130                         System.Windows.Forms.ListView.SelectedIndexCollection selIdxes = instsListView.SelectedIndices;\r
131                         \r
132                         silentInstallStripMenuItem.Visible = selIdxes.Count > 0;\r
133                         if (selIdxes.Count > 0) {\r
134                                 Installation inst0th = ((Installation) instsListView.Items[selIdxes[0]].Tag);\r
135                                 bool bChecked = inst0th.Silent;\r
136                                 bool bEnabled = (inst0th.IsSupportsSilent && (!inst0th.SupportsSilentOnly));\r
137                                 silentInstallStripMenuItem.Checked = bChecked;\r
138                                 silentInstallStripMenuItem.Enabled = bEnabled;\r
139                                 \r
140                                 for (int i = 1; i < selIdxes.Count; i++) {\r
141                                         Installation inst = ((Installation) instsListView.Items[selIdxes[i]].Tag);\r
142                                         \r
143                                         if ( (bChecked != inst.Silent) ||\r
144                                             (bEnabled != (inst.IsSupportsSilent && (!inst.SupportsSilentOnly))) ) {\r
145                                                 silentInstallStripMenuItem.CheckState = CheckState.Indeterminate;\r
146                                                 silentInstallStripMenuItem.Enabled = false;\r
147                                                 break;\r
148                                         }\r
149                                 }\r
150                         }\r
151                         instListToolStripSeparator.Visible = selIdxes.Count > 0;\r
152                 \r
153                         \r
154                         selectAllInstsStripMenuItem.Enabled = chkIdxes.Count < instsListView.Items.Count;\r
155                         unselectAllInstsStripMenuItem.Enabled = chkIdxes.Count > 0;\r
156                 }\r
157                 \r
158                 void SilentInstallStripMenuItemClick(object sender, EventArgs e)\r
159                 {\r
160                         bool silent = ! silentInstallStripMenuItem.Checked;\r
161                         foreach (ListViewItem item in instsListView.CheckedItems) {\r
162                                 ((Installation) item.Tag).Silent = silent;\r
163                         }\r
164                         updateInstsListView();\r
165                 }\r
166                                 \r
167                 void SelectAllInstsStripMenuItemClick(object sender, EventArgs e)\r
168                 {\r
169                         foreach (ListViewItem item in instsListView.Items) {\r
170                                 item.Checked = true;\r
171                         }\r
172                         instsListView.Refresh();\r
173                 }\r
174                 \r
175                 void UnselectAllInstsStripMenuItemClick(object sender, EventArgs e)\r
176                 {\r
177                         foreach (ListViewItem item in instsListView.CheckedItems) {\r
178                                 item.Checked = false;\r
179                         }\r
180                         instsListView.Refresh();\r
181                 }\r
182                 \r
183                 /// <summary>\r
184                 /// \83C\83\93\83X\83g\81[\83\8b\82·\82é\82æ\82¤\91I\91ð\82³\82ê\82½\83p\83b\83P\81[\83W\82Ì\94z\97ñ\r
185                 /// </summary>\r
186                 public Installation[] CheckedInstallations {\r
187                         get {\r
188                                 System.Collections.Generic.List<Installation> insts = new System.Collections.Generic.List<Installation>();\r
189                                 foreach (ListViewItem item in instsListView.CheckedItems) {\r
190                                         insts.Add((Installation) item.Tag);\r
191                                 }\r
192                                 return insts.ToArray();\r
193                         }\r
194                 }\r
195                 \r
196                 #region runas\8aÖ\98A\r
197                 \r
198                 /// <summary>\r
199                 /// runas\82Å\8eÀ\8ds\82·\82é\82©\94Û\82©\r
200                 /// </summary>\r
201                 public bool UseRunas {\r
202                         set {\r
203                                 runasCheckBox.Checked = (! NaGet.Utils.IsAdministrators()) && value;\r
204                                 \r
205                                 updateUseRunas();\r
206                         }\r
207                         get {\r
208                                 return runasCheckBox.Checked;\r
209                         }\r
210                 }\r
211                 \r
212                 /// <summary>\r
213                 /// \91I\91ð\82³\82ê\82½\83p\83b\83P\81[\83W\82ð\92²\8d¸\82µ\82Ä\81ARunas\82ð\8eg\82¤\82×\82«\82©\82¢\82È\82©\82ð\95Ô\82·\r
214                 /// </summary>\r
215                 public bool GetShouldUseRunas()\r
216                 {\r
217                         if (NaGet.Utils.IsAdministrators()) {\r
218                                 // \8aÇ\97\9d\8eÒ\8c \8cÀ\82Å\93®\82¢\82Ä\82¢\82é\8fê\8d\87\82Í\95s\97v\r
219                                 return false;   \r
220                         } else if (NaGet.Utils.IsUACEnabled()) {\r
221                                 // UAC\82ª\93K\97p\82³\82ê\82Ä\82¢\82é\8fê\8d\87\82Í\95W\8f\80\82Å\82Í\95s\97v\82Æ\82·\82é\r
222                                 return false;   \r
223                         }\r
224                         \r
225                         // \82Ð\82Æ\82Â\82Å\82àPC\83^\81[\83Q\83b\83g\82È\83C\83\93\83X\83g\81[\83\89\82ª\82 \82ê\82Î\95K\97v\82Æ\82·\82é\r
226                         foreach (Installation inst in CheckedInstallations) {\r
227                                 if (inst.TargetPC) return true;\r
228                         }\r
229                         // \82»\82ê\88È\8aO\82Í\95s\97v\r
230                         return false;\r
231                 }\r
232                 \r
233                 void RunasCheckBoxCheckedChanged(object sender, EventArgs e)\r
234                 {\r
235                         updateUseRunas();\r
236                 }\r
237                 \r
238                 private void updateUseRunas()\r
239                 {\r
240                         if (UseRunas) {\r
241                                 System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(InstallationConfirmForm));\r
242                                 okButton.Image = ((System.Drawing.Bitmap)(resources.GetObject("okButton.Image")));\r
243                         } else {\r
244                                 okButton.Image = null;\r
245                         }\r
246                 }\r
247                 \r
248                 #endregion\r
249         }\r
250 }\r