OSDN Git Service

AppliStation,サイレントインストールのロジック部分とall-get.exeからの部分を書いた
[applistation/AppliStation.git] / AppliStation / PackagesInstallConfirmForm.cs
1 using System;\r
2 using System.Drawing;\r
3 using System.Windows.Forms;\r
4 using NaGet.Packages;\r
5 \r
6 namespace AppliStation\r
7 {\r
8         /// <summary>\r
9         /// Description of PackagesInstallConfirmForm.\r
10         /// </summary>\r
11         public partial class PackagesInstallConfirmForm : Form\r
12         {\r
13                 private Package[] packages;\r
14                 \r
15                 private PackageListsManager pkgListMan;\r
16                 \r
17                 /// <summary>\r
18                 /// \83p\83b\83P\81[\83W\82Ì\8fW\8d\87\r
19                 /// </summary>\r
20                 public Package[] Packages {\r
21                         get { return packages; }\r
22                         set {\r
23                                 packages = value;\r
24                                 \r
25                                 updatePackagesListView();\r
26                         }\r
27                 }\r
28                 \r
29                 public PackageListsManager PkgListsManager {\r
30                         get { return pkgListMan; }\r
31                         set {\r
32                                 pkgListMan = value;\r
33                                 \r
34                                 updatePackagesListView();\r
35                         }\r
36                 }\r
37                 \r
38                 public PackagesInstallConfirmForm()\r
39                 {\r
40                         //\r
41                         // The InitializeComponent() call is required for Windows Forms designer support.\r
42                         //\r
43                         InitializeComponent();\r
44                         \r
45                         // \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
46                         if (NaGet.Utils.IsAdministrators()) {\r
47                                 runasCheckBox.Checked = false;\r
48                                 runasCheckBox.Visible = false;\r
49                         }\r
50                 }\r
51                 \r
52                 private void updatePackagesListView()\r
53                 {\r
54                         packageListView.Items.Clear();\r
55                         \r
56                         if (packages != null && packages.Length > 0) {\r
57                                 foreach (Package pkg in packages) {\r
58                                         Package curPkg = null;\r
59                                         if (pkgListMan != null) {       \r
60                                                 curPkg = pkgListMan.InstalledPkgList.GetPackageForName(pkg.Name);\r
61                                                 if (curPkg == null) curPkg = pkgListMan.SystemInstalledPkgList.GetPackageForName(pkg.Name);\r
62                                         }\r
63                                         string curVersion = (curPkg == null)? "-" : curPkg.Version;\r
64                                         \r
65                                         ListViewItem item = new ListViewItem(new string[]{pkg.Name, pkg.Version, curVersion});\r
66                                         item.Tag = pkg;\r
67                                         item.ToolTipText = pkg.Summary;\r
68                                         item.Checked = true;\r
69                                         \r
70                                         packageListView.Items.Add(item);\r
71                                 }\r
72                         }\r
73                         \r
74                         PackageListViewItemChecked(packageListView, null);\r
75                         packageListView.Refresh();\r
76                 }\r
77                 \r
78                 void PackageListViewItemChecked(object sender, ItemCheckedEventArgs e)\r
79                 {\r
80                         System.Windows.Forms.ListView.CheckedListViewItemCollection checkeds = packageListView.CheckedItems;\r
81                         \r
82                         okButton.Enabled = checkeds != null && checkeds.Count > 0;\r
83                 }\r
84                 \r
85                 AppliStation.Util.ListViewItemSortComparer packageListViewSortComparer;\r
86 \r
87                 void PackageListViewColumnClick(object sender, ColumnClickEventArgs e)\r
88                 {\r
89                         SortOrder order = SortOrder.None;\r
90                         \r
91                         if (packageListViewSortComparer == null) {\r
92                                 order = SortOrder.Ascending;\r
93                                 packageListViewSortComparer = new AppliStation.Util.ListViewItemSortComparer(e.Column, order);\r
94                                 packageListView.ListViewItemSorter = packageListViewSortComparer;\r
95                         } else {\r
96                                 if (packageListViewSortComparer.Column == e.Column) {\r
97                                         order = (packageListViewSortComparer.Order == SortOrder.Ascending)? SortOrder.Descending : SortOrder.Ascending;\r
98                                         \r
99                                         packageListViewSortComparer.Order = order;\r
100                                 } else {\r
101                                         order = packageListViewSortComparer.Order;\r
102                                         packageListViewSortComparer.Column = e.Column;\r
103                                 }\r
104                                 \r
105                                 packageListView.Sort();\r
106                         }\r
107                         AppliStation.Util.NativeMethods.ColumnHeader_SetSortState(packageListView, e.Column, order);\r
108                         \r
109                         // \83\\81[\83g\91Î\8fÛ\97ñ\82Ì\90F\95t\82¯\r
110                         try {\r
111                                 // SendMessage(hWnd, LVM_SETSELECTEDCOLUMN, column, NULL);\r
112                                 AppliStation.Util.NativeMethods.SendMessage(packageListView.Handle, 0x1000+140, (uint) e.Column, 0);\r
113                         } catch (Exception) {   \r
114                         }\r
115                 }\r
116                 \r
117                                 \r
118                 void PackageListViewContextMenuStripOpening(object sender, System.ComponentModel.CancelEventArgs e)\r
119                 {\r
120                         System.Windows.Forms.ListView.CheckedIndexCollection coll = packageListView.CheckedIndices;\r
121                         selectAllPackagesStripMenuItem.Enabled = coll.Count < packages.Length;\r
122                         unselectAllPackagesStripMenuItem.Enabled = coll.Count > 0;\r
123                 }\r
124                                 \r
125                 void SelectAllPackagesStripMenuItemClick(object sender, EventArgs e)\r
126                 {\r
127                         foreach (ListViewItem item in packageListView.Items) {\r
128                                 item.Checked = true;\r
129                         }\r
130                         packageListView.Refresh();\r
131                 }\r
132                 \r
133                 void UnselectAllPackagesStripMenuItemClick(object sender, EventArgs e)\r
134                 {\r
135                         foreach (ListViewItem item in packageListView.CheckedItems) {\r
136                                 item.Checked = false;\r
137                         }\r
138                         packageListView.Refresh();\r
139                 }\r
140                 \r
141                 /// <summary>\r
142                 /// \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
143                 /// </summary>\r
144                 public Package[] CheckedPackages {\r
145                         get {\r
146                                 System.Collections.Generic.List<Package> pkgs = new System.Collections.Generic.List<Package>();\r
147                                 foreach (ListViewItem item in packageListView.CheckedItems) {\r
148                                         pkgs.Add((Package) item.Tag);\r
149                                 }\r
150                                 return pkgs.ToArray();\r
151                         }\r
152                 }\r
153                 \r
154                 #region runas\8aÖ\98A\r
155                 \r
156                 /// <summary>\r
157                 /// runas\82Å\8eÀ\8ds\82·\82é\82©\94Û\82©\r
158                 /// </summary>\r
159                 public bool UseRunas {\r
160                         set {\r
161                                 runasCheckBox.Checked = (! NaGet.Utils.IsAdministrators()) && value;\r
162                                 \r
163                                 updateUseRunas();\r
164                         }\r
165                         get {\r
166                                 return runasCheckBox.Checked;\r
167                         }\r
168                 }\r
169                 \r
170                 /// <summary>\r
171                 /// \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
172                 /// </summary>\r
173                 public bool GetShouldUseRunas()\r
174                 {\r
175                         if (NaGet.Utils.IsAdministrators()) {\r
176                                 // \8aÇ\97\9d\8eÒ\8c \8cÀ\82Å\93®\82¢\82Ä\82¢\82é\8fê\8d\87\82Í\95s\97v\r
177                                 return false;   \r
178                         } else if (NaGet.Utils.IsUACEnabled()) {\r
179                                 // UAC\82ª\93K\97p\82³\82ê\82Ä\82¢\82é\8fê\8d\87\82Í\95W\8f\80\82Å\82Í\95s\97v\82Æ\82·\82é\r
180                                 return false;   \r
181                         }\r
182                         \r
183                         bool useRunas = false;\r
184                         foreach (Package pkg in CheckedPackages) {\r
185                                 if (pkg.Type != InstallerType.ARCHIVE) {\r
186                                         useRunas = true;\r
187                                         break;\r
188                                 }\r
189                         }\r
190                         return useRunas;\r
191                 }\r
192                 \r
193                 void RunasCheckBoxCheckedChanged(object sender, EventArgs e)\r
194                 {\r
195                         updateUseRunas();\r
196                 }\r
197                 \r
198                 private void updateUseRunas()\r
199                 {\r
200                         if (UseRunas) {\r
201                                 System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(PackagesInstallConfirmForm));\r
202                                 okButton.Image = ((System.Drawing.Bitmap)(resources.GetObject("okButton.Image")));\r
203                         } else {\r
204                                 okButton.Image = null;\r
205                         }\r
206                 }\r
207                 \r
208                 #endregion\r
209 \r
210 \r
211         }\r
212 }\r