OSDN Git Service

AppliStation-*,とりあえずの実装 of 「インストール先フォルダ」のところで実行ファイルを直接 実行できるように
[applistation/AppliStation.git] / AppliStation / PackageListViewForm.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 using System.IO;\r
7 using System.Collections.Generic;\r
8 \r
9 namespace AppliStation\r
10 {\r
11         /// <summary>\r
12         /// Description of PackageListViewForm.\r
13         /// </summary>\r
14         public partial class PackageListViewForm : Form\r
15         {\r
16                 protected PackageListsManager pkgListsMan = null;\r
17                 \r
18                 public PackageListViewForm()\r
19                 {\r
20                         //\r
21                         // The InitializeComponent() call is required for Windows Forms designer support.\r
22                         //\r
23                         InitializeComponent();\r
24                         \r
25                         installToolStripMenuItem.Font = new Font(installToolStripMenuItem.Font, FontStyle.Bold);\r
26                         uninstallToolStripMenuItem.Font = new Font(uninstallToolStripMenuItem.Font, FontStyle.Bold);\r
27                         \r
28                         pkgListsMan = new PackageListsManager();\r
29                         \r
30                         this.Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);\r
31                 }\r
32                 \r
33                 private void ShowInfoToDetailBoxFor(Package pkg)\r
34                 {\r
35                         detailBox.Clear();\r
36                         \r
37                         detailBox.SelectionFont = new Font(detailBox.Font.FontFamily, 12);\r
38                         detailBox.SelectedText += string.Format("{0} ({1})\r\n", pkg.Name, pkg.Version);\r
39                         if (! string.IsNullOrEmpty(pkg.Tags) ) {\r
40                                 detailBox.SelectionFont = new Font(detailBox.Font.FontFamily, 8);\r
41                                 detailBox.SelectedText += string.Format("タグ: {0}\r\n", pkg.Tags);\r
42                         }\r
43                         if (pkg is InstalledPackage) {\r
44                                 InstalledPackage iPkg = (InstalledPackage) pkg;\r
45                                 System.Text.StringBuilder sb = new System.Text.StringBuilder();\r
46                                 if (iPkg.UninstallInfo.InstallDate != null) {\r
47                                         sb.AppendFormat("インストールした日: {0:d}  ", iPkg.UninstallInfo.InstallDate.Value);\r
48                                 }\r
49                                 if (iPkg.UninstallInfo.EstimatedSize > 0) {\r
50                                         sb.AppendFormat("サイズ: {0}  ", NaGet.Utils.FormatSize(iPkg.UninstallInfo.EstimatedSize*1024));\r
51                                 }\r
52                                 \r
53                                 if (sb.Length > 0) {\r
54                                         detailBox.SelectionFont = new Font(detailBox.Font.FontFamily, 8);\r
55                                         detailBox.SelectedText += sb.ToString();\r
56                                         detailBox.SelectedText += "\r\n";\r
57                                 }\r
58                         }\r
59                         detailBox.SelectionFont = detailBox.Font;\r
60                         if (pkg.Url != null && pkg.Url.Href != null) {\r
61                                 detailBox.SelectedText += "公式サイト: " + pkg.Url.Href + "\r\n";\r
62                         }\r
63                         detailBox.SelectedText += pkg.Summary;\r
64                 }\r
65                 \r
66                 void PackageListViewSelectedIndexChanged(object sender, EventArgs e)\r
67                 {\r
68                         uint installPkgCount = 0;\r
69                         uint uninstallPkgCount = 0;\r
70                         bool installBtnEnabled, uninstallBtnEnabled;\r
71                         \r
72                         Package[] selectedPkgs = SelectedPackages<Package>();\r
73                         foreach (Package pkg in selectedPkgs) {\r
74                                 if (pkg is InstalledPackage) {\r
75                                         uninstallPkgCount ++;\r
76                                 } else {\r
77                                         installPkgCount ++;\r
78                                 }\r
79                         }\r
80                         installBtnEnabled = (installPkgCount > 0) && (uninstallPkgCount == 0);\r
81                         uninstallBtnEnabled = (uninstallPkgCount == 1) && (installPkgCount == 0);\r
82                         \r
83                         uninstallToolStripButton.Visible = uninstallBtnEnabled;\r
84                         installToolStripButton.Visible = installBtnEnabled;\r
85                         packageCommandsToolStripSeparator.Visible = uninstallBtnEnabled || installBtnEnabled;\r
86                         \r
87                         switch (selectedPkgs.Length) {\r
88                                 case 0:\r
89                                         detailBox.SelectionFont = detailBox.Font;\r
90                                         int count = packageListView.Items.Count;\r
91                                         detailBox.Text = (count > 0) ? string.Format("{0}個のソフトがあります。", count)\r
92                                                 : "該当するソフトがありません。";\r
93                                         break;\r
94                                 case 1:\r
95                                         ShowInfoToDetailBoxFor(selectedPkgs[0]);\r
96                                         break;\r
97                                 default: // case 2 and over:\r
98                                         detailBox.SelectionFont = detailBox.Font;\r
99                                         detailBox.Text = (installBtnEnabled)? string.Format("{0}個のソフトが選択されています。", installPkgCount) :\r
100                                                 (uninstallBtnEnabled)? string.Format("{0}個のインストール済みのソフトが選択されています。", uninstallPkgCount) :\r
101                                                 string.Format("{0}個のソフトが選択されています。\r\n(うち{1}個はインストール済み、{2}個はインストール可能)", installPkgCount+uninstallPkgCount, uninstallPkgCount, installPkgCount);\r
102                                         break;\r
103                         }\r
104                         \r
105                         detailBox.SelectionStart = 0;\r
106                         detailBox.ScrollToCaret();\r
107                 }\r
108 \r
109                 void PackageListViewItemActivate(object sender, EventArgs e)\r
110                 {\r
111                         if (packageListView.SelectedItems.Count <= 0) {\r
112                                 return; // exit \r
113                         }\r
114                         \r
115                         foreach (ListViewItem item in packageListView.SelectedItems) {\r
116                                 Package pkg = (Package) item.Tag;\r
117                                 \r
118                                 if (pkg is InstalledPackage) {\r
119                                         UninstallToolStripButtonClick(sender, e);\r
120                                 } else {\r
121                                         InstallToolStripButtonClick(sender, e);\r
122                                 }\r
123                                 \r
124                                 break;\r
125                         }\r
126                 }\r
127                 \r
128                 private void buildInstalledDirectoryMenuItemStripChildren(InstalledPackage pkg)\r
129                 {\r
130                         string targetDir = pkg.UninstallInfo.InstallLocation;                   \r
131                         System.Collections.Generic.List<ToolStripItem> items = new System.Collections.Generic.List<ToolStripItem>();\r
132                         \r
133                         if (Directory.Exists(targetDir)) {\r
134                                 foreach (string exeFile in Directory.GetFiles(targetDir, "*.exe")) {\r
135                                         if (NaGet.InteropServices.PEFileInfoUtils.GetPEFileType(exeFile) == NaGet.InteropServices.PEFileInfoUtils.PEFileType.WinGUI) {\r
136                                                 items.Add(AppliStation.Util.GUIUtils.CreateMenuItemForFile(exeFile));\r
137                                         }\r
138                                 }\r
139                                 \r
140                                 if (items.Count > 0) {\r
141                                         items.Add(new ToolStripSeparator());\r
142                                 }\r
143                                 \r
144                                 {\r
145                                         ToolStripMenuItem item = AppliStation.Util.GUIUtils.CreateMenuItemForFile(targetDir);\r
146                                         item.Text = "フォルダを開く(&O)";\r
147                                         items.Add(item);\r
148                                 }\r
149                         }\r
150                         \r
151                         installedDirectoryStripMenuItem.DropDownItems.Clear();\r
152                         installedDirectoryStripMenuItem.DropDownItems.AddRange(items.ToArray());\r
153                 }\r
154                 \r
155                 #region packageListViewのSort関連\r
156                 \r
157                 AppliStation.Util.ListViewItemSortComparer packageListViewSortComparer;\r
158 \r
159                 void PackageListViewColumnClick(object sender, ColumnClickEventArgs e)\r
160                 {\r
161                         SortOrder order = SortOrder.None;\r
162                         \r
163                         if (packageListViewSortComparer == null) {\r
164                                 order = SortOrder.Ascending;\r
165                                 packageListViewSortComparer = new AppliStation.Util.ListViewItemSortComparer(e.Column, order);\r
166                                 packageListView.ListViewItemSorter = packageListViewSortComparer;\r
167                         } else {\r
168                                 if (packageListViewSortComparer.Column == e.Column) {\r
169                                         order = (packageListViewSortComparer.Order == SortOrder.Ascending)? SortOrder.Descending : SortOrder.Ascending;\r
170                                         \r
171                                         packageListViewSortComparer.Order = order;\r
172                                 } else {\r
173                                         order = packageListViewSortComparer.Order;\r
174                                         packageListViewSortComparer.Column = e.Column;\r
175                                 }\r
176                                 \r
177                                 packageListView.Sort();\r
178                         }\r
179                         AppliStation.Util.NativeMethods.ColumnHeader_SetSortState(packageListView, e.Column, order);\r
180                         \r
181                         // ソート対象列の色付け\r
182                         try {\r
183                                 // SendMessage(hWnd, LVM_SETSELECTEDCOLUMN, column, NULL);\r
184                                 AppliStation.Util.NativeMethods.SendMessage(packageListView.Handle, 0x1000+140, (uint) e.Column, 0);\r
185                         } catch (Exception) {   \r
186                         }\r
187                 }\r
188                 \r
189                 #endregion\r
190                 \r
191                 void Form_OnLoad(object sender, EventArgs e)\r
192                 {\r
193                         System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(PackageListViewForm));\r
194                         ImageList imageList = new ImageList();\r
195                         imageList.Images.Add("installed", this.Icon);\r
196                         imageList.Images.Add("sys", (Icon)(resources.GetObject("sysinstalled.Icon")));\r
197                         packageListView.SmallImageList = imageList;\r
198                         \r
199                         updatePackageFilterToolStripMenuItemCheckState();\r
200                         installedASPackageFilterToolStripMenuItem.Image = this.Icon.ToBitmap();\r
201                         installedSysPackageFilterToolStripMenuItem.Image = ((Icon)(resources.GetObject("sysinstalled.Icon"))).ToBitmap();\r
202                 }\r
203                 \r
204                 #region PackageFilter関連\r
205                 \r
206                 private enum PackageFilterIndex : int\r
207                 {\r
208                         All = 0,\r
209                         NotInstalled = 1,\r
210                         InstalledAS = 2,\r
211                         InstalledSys = 3,\r
212                 }\r
213                 \r
214                 private PackageFilterIndex currentPackageFilter = PackageFilterIndex.All;\r
215                 \r
216                 private PackageFilterIndex CurrentPackageFilter {\r
217                         get {\r
218                                 return currentPackageFilter;\r
219                         }\r
220                         set {\r
221                                 currentPackageFilter = value;\r
222                                 \r
223                                 updatePackageFilterToolStripMenuItemCheckState();\r
224                         }\r
225                 }\r
226                 \r
227                 private void updatePackageFilterToolStripMenuItemCheckState()\r
228                 {\r
229                         ToolStripMenuItem selected = getCheckedPackageFilterToolStripItem();\r
230                         \r
231                         foreach (ToolStripMenuItem item in packageFilterToolStripDropDownButton.DropDown.Items) {\r
232                                 item.Checked = selected == item;\r
233                         }\r
234                         \r
235                         packageFilterToolStripDropDownButton.Text = selected.Text;\r
236                         packageFilterToolStripDropDownButton.Image = selected.Image;\r
237                         packageFilterToolStripDropDownButton.ToolTipText = selected.ToolTipText;\r
238                 }\r
239                 \r
240                 private ToolStripMenuItem getCheckedPackageFilterToolStripItem()\r
241                 {\r
242                         switch (currentPackageFilter) {\r
243                                 case PackageFilterIndex.NotInstalled:\r
244                                         return notInstalledPackageFilterToolStripMenuItem;\r
245                                 case PackageFilterIndex.InstalledAS:\r
246                                         return installedASPackageFilterToolStripMenuItem;\r
247                                 case PackageFilterIndex.InstalledSys:\r
248                                         return installedSysPackageFilterToolStripMenuItem;\r
249                                 default:\r
250                                         return allPackageFilterToolStripMenuItem;\r
251                         }\r
252                 }\r
253                 \r
254                 void AnyPackageFilterToolStripMenuItemClicked(object sender, EventArgs e)\r
255                 {\r
256                         if (sender == notInstalledPackageFilterToolStripMenuItem) {\r
257                                 currentPackageFilter = PackageFilterIndex.NotInstalled;\r
258                         } else if (sender == installedASPackageFilterToolStripMenuItem) {\r
259                                 currentPackageFilter = PackageFilterIndex.InstalledAS;\r
260                         } else if (sender == installedSysPackageFilterToolStripMenuItem) {\r
261                                 currentPackageFilter = PackageFilterIndex.InstalledSys;\r
262                         } else {\r
263                                 currentPackageFilter = PackageFilterIndex.All;\r
264                         }\r
265                         \r
266                         updatePackageFilterToolStripMenuItemCheckState();\r
267                         searchTextBox.FireTextChangedTrigger();\r
268                         PackageListViewUpdate();\r
269                 }\r
270                 \r
271                 #endregion\r
272                 \r
273                 void PackageListViewUpdate()\r
274                 {\r
275                         this.packageListView.Items.Clear();\r
276                         \r
277                         if (currentPackageFilter == PackageFilterIndex.All || currentPackageFilter == PackageFilterIndex.NotInstalled) {\r
278                                 foreach (Package pkg in pkgListsMan.AvailablePkgList.Search(searchTextBox.Text)) {\r
279                                         if (Installation.GetPreferInstallerIndex(pkg) >= 0) { // インストール可能\r
280                                                 if (currentPackageFilter == PackageFilterIndex.NotInstalled &&\r
281                                                     (pkgListsMan.InstalledPkgList.GetPackageForName(pkg.Name) != null||\r
282                                                      pkgListsMan.SystemInstalledPkgList.GetPackageForName(pkg.Name) != null) ) {\r
283                                                         continue; // インストール済みは次のループへ(未インストールソフト抽出時)\r
284                                                 }\r
285                                                 \r
286                                                 ListViewItem item = new ListViewItem(new string[]{pkg.Name, pkg.Version, pkg.Summary});\r
287                                                 item.Tag = pkg;\r
288                                                 item.ToolTipText = pkg.Summary;\r
289                 \r
290                                                 this.packageListView.Items.Add(item);\r
291                                         }\r
292                                 }\r
293                         }\r
294                         \r
295                         if (currentPackageFilter == PackageFilterIndex.All || currentPackageFilter == PackageFilterIndex.InstalledAS) {\r
296                                 foreach (Package pkg in pkgListsMan.InstalledPkgList.Search(searchTextBox.Text)) {\r
297                                         ListViewItem item = new ListViewItem(new string[]{pkg.Name, pkg.Version, pkg.Summary});\r
298                                         item.Tag = pkg;\r
299                                         item.ToolTipText = pkg.Summary;\r
300                                         item.ImageKey = "installed";\r
301         \r
302                                         this.packageListView.Items.Add(item);\r
303                                 }\r
304                         }\r
305                         \r
306                         if (currentPackageFilter == PackageFilterIndex.All || currentPackageFilter == PackageFilterIndex.InstalledSys) {\r
307                                 foreach (Package pkg in pkgListsMan.SystemInstalledPkgList.Search(searchTextBox.Text)) {\r
308                                         ListViewItem item = new ListViewItem(new string[]{pkg.Name, pkg.Version, pkg.Summary});\r
309                                         item.Tag = pkg;\r
310                                         item.ToolTipText = pkg.Summary;\r
311                                         item.ImageKey = "sys";\r
312         \r
313                                         this.packageListView.Items.Add(item);\r
314                                 }\r
315                         }\r
316                         \r
317                         PackageListViewSelectedIndexChanged(packageListView, null);\r
318                         detailBox.Refresh();\r
319                         \r
320                         //this.packageListView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);\r
321                 }\r
322                 \r
323                 void PackageDetailBoxLinkClicked(object sender, LinkClickedEventArgs e)\r
324                 {\r
325                         System.Diagnostics.Process.Start(e.LinkText);\r
326                 }\r
327                 \r
328                 internal void updateActionInvoke(bool downloadPackageListsFlag)\r
329                 {\r
330                         AppliStation.Util.ExecutionProgressViewer prog = new AppliStation.Util.ExecutionProgressViewer();\r
331                         prog.Shown += delegate(object sender2, EventArgs e2) {\r
332                                 NaGet.SubCommands.NaGetUpdate tasks = new NaGet.SubCommands.NaGetUpdate(pkgListsMan, downloadPackageListsFlag);\r
333                                 prog.SetTaskSet(tasks);\r
334                                 prog.Refresh();\r
335                                 prog.StartTaskSet();\r
336                         };\r
337                         prog.Text = "リストの更新";\r
338                         prog.ShowDialog(this);\r
339                 }\r
340                         \r
341                 void UpdateToolStripButtonClick(object sender, EventArgs e)\r
342                 {\r
343                         updateActionInvoke(true);\r
344                         \r
345                         UpdatePackageList();\r
346                 }\r
347                 \r
348                 void LocalupdateToolStripMenuItemClick(object sender, EventArgs e)\r
349                 {\r
350                         updateActionInvoke(false);\r
351                         \r
352                         UpdatePackageList();\r
353                 }\r
354                 \r
355                 #region searchTextBoxまわり\r
356                 \r
357                 void SearchTextBoxKeyPress(object sender, KeyPressEventArgs e)\r
358                 {\r
359                         switch (e.KeyChar) {\r
360                                 case (char)Keys.Enter:\r
361                                         searchTextBox.FireTextChangedTrigger();\r
362                                         break;\r
363                                 case (char)Keys.Escape:\r
364                                         searchTextBox.Text = string.Empty;\r
365                                         break;\r
366                         }\r
367                 }\r
368                 \r
369                 void SearchTextBoxTextChangedTriggerFired(object sender, EventArgs e)\r
370                 {\r
371                         PackageListViewUpdate();\r
372                 }\r
373                 \r
374                 #endregion\r
375                 \r
376                 internal void installActionInvoke(Installation[] insts)\r
377                 {\r
378                         AppliStation.Util.ExecutionProgressViewer prog = new AppliStation.Util.ExecutionProgressViewer();\r
379                         prog.Shown += delegate(object sender2, EventArgs e2) {\r
380                                 NaGet.SubCommands.NaGetInstall tasks = new NaGet.SubCommands.NaGetInstall(pkgListsMan, insts);\r
381                                 prog.SetTaskSet(tasks);\r
382                                 prog.Refresh();\r
383                                 prog.StartTaskSet();\r
384                         };\r
385                         prog.Text = string.Format("ソフトウェアのインストール");\r
386                         prog.ShowDialog(this);\r
387                 }\r
388                 \r
389                 void InstallToolStripButtonClick(object sender, EventArgs e)\r
390                 {\r
391                         InstallationConfirmForm confirm = new InstallationConfirmForm();\r
392                         confirm.PkgListsManager = pkgListsMan;\r
393                         confirm.Installations = Installation.ConvertInstallations(SelectedPackages<Package>());\r
394                         confirm.UseRunas = confirm.GetShouldUseRunas();\r
395                         DialogResult result = confirm.ShowDialog(this);\r
396                         \r
397                         if (result == DialogResult.OK) {\r
398                                 Installation[] insts = confirm.CheckedInstallations;\r
399                                 \r
400                                 if (confirm.UseRunas) {\r
401                                         installRunasActionInvoke(insts);\r
402                                 } else {\r
403                                         installActionInvoke(insts);\r
404                                 }\r
405                                 \r
406                                 UpdatePackageList();\r
407                         }\r
408                 }\r
409                 \r
410                 public void installRunasActionInvoke(Installation[] insts)\r
411                 {\r
412                         this.setWindowEnabled(false);\r
413                         \r
414                         string tmpfileName = Path.GetTempFileName();\r
415                         try {\r
416                                 NaGet.Utils.PutSerializeObject(tmpfileName, insts);\r
417                                 \r
418                                 System.Diagnostics.ProcessStartInfo procInfo = new System.Diagnostics.ProcessStartInfo();\r
419                                 procInfo.FileName = Application.ExecutablePath;\r
420                                 procInfo.Arguments = string.Format("--noupdate --cmd=install \"--instsref={0}\"", tmpfileName);\r
421                                 procInfo.Verb = "runas";\r
422                                 procInfo.WorkingDirectory = Environment.CurrentDirectory;\r
423                                 \r
424                                 System.Diagnostics.Process hProc = System.Diagnostics.Process.Start(procInfo);\r
425                                 hProc.EnableRaisingEvents = true;\r
426                                 hProc.SynchronizingObject = this;\r
427                                 hProc.Exited += delegate(object sender, EventArgs e) {\r
428                                         pkgListsMan.LoadPackageLists();\r
429                                         \r
430                                         this.setWindowEnabled(true);\r
431                                         this.BringToFront();\r
432                                         \r
433                                         if (File.Exists(tmpfileName)) {\r
434                                                 File.Delete(tmpfileName);\r
435                                         }\r
436                                 };\r
437                         } catch (System.ComponentModel.Win32Exception ex) {\r
438                                 MessageBox.Show(ex.Message, "インストール", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
439                                 \r
440                                 if (File.Exists(tmpfileName)) {\r
441                                         File.Delete(tmpfileName);\r
442                                 }\r
443                                 this.setWindowEnabled(true);\r
444                         }\r
445                 }\r
446                 \r
447                 internal void uninstallActionInvoke(InstalledPackage[] pkgs)\r
448                 {\r
449                         AppliStation.Util.ExecutionProgressViewer prog = new AppliStation.Util.ExecutionProgressViewer();\r
450                         prog.Shown += delegate(object sender2, EventArgs e2) {\r
451                                 NaGet.SubCommands.NaGetUninstall tasks = new NaGet.SubCommands.NaGetUninstall(pkgListsMan, pkgs);\r
452                                 prog.SetTaskSet(tasks);\r
453                                 prog.Refresh();\r
454                                 prog.StartTaskSet();\r
455                         };\r
456                         prog.Text = string.Format("ソフトウェアのアンインストール");\r
457                         prog.ShowDialog(this);\r
458                 }\r
459                 \r
460                 internal void uninstallRunasActionInvoke(InstalledPackage[] pkgs)\r
461                 {                       \r
462                         this.setWindowEnabled(false);\r
463                         \r
464                         string tmpfileName = Path.GetTempFileName();\r
465                         try {\r
466                                 NaGet.Utils.PutSerializeObject(tmpfileName, pkgs);\r
467                                 \r
468                                 System.Diagnostics.ProcessStartInfo procInfo = new System.Diagnostics.ProcessStartInfo();\r
469                                 procInfo.FileName = Application.ExecutablePath;\r
470                                 procInfo.Arguments = string.Format("--noupdate --cmd=uninstall \"--pkgsref={0}\"", tmpfileName);\r
471                                 procInfo.Verb = "runas";\r
472                                 procInfo.WorkingDirectory = Environment.CurrentDirectory;\r
473                                 \r
474                                 System.Diagnostics.Process hProc = System.Diagnostics.Process.Start(procInfo);\r
475                                 hProc.EnableRaisingEvents = true;\r
476                                 hProc.SynchronizingObject = this;\r
477                                 hProc.Exited += delegate(object sender, EventArgs e) {\r
478                                         pkgListsMan.LoadPackageLists();\r
479                                         \r
480                                         this.setWindowEnabled(true);\r
481                                         this.BringToFront();\r
482                                         \r
483                                         if (File.Exists(tmpfileName)) {\r
484                                                 File.Delete(tmpfileName);\r
485                                         }\r
486                                 };\r
487                         } catch (System.ComponentModel.Win32Exception ex) {\r
488                                 MessageBox.Show(ex.Message, "アンインストール", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
489                                 \r
490                                 this.setWindowEnabled(true);\r
491                                 if (File.Exists(tmpfileName)) {\r
492                                         File.Delete(tmpfileName);\r
493                                 }\r
494                         }\r
495                 }\r
496                 \r
497                 void UninstallToolStripButtonClick(object sender, EventArgs e)\r
498                 {\r
499                         PackageUninstallConfirmForm confirm = new PackageUninstallConfirmForm();\r
500                         foreach (InstalledPackage pkg in SelectedPackages<InstalledPackage>()) {\r
501                                 confirm.UninstallPackage = pkg;\r
502                                 break;\r
503                         }\r
504                         confirm.UseRunas = confirm.GetShouldUseRunas();\r
505                         DialogResult result = confirm.ShowDialog(this);\r
506                         \r
507                         if (result == DialogResult.OK) {\r
508                                 InstalledPackage[] instPkgs = new InstalledPackage[]{confirm.UninstallPackage};\r
509                                 \r
510                                 if (confirm.UseRunas) {\r
511                                         uninstallRunasActionInvoke(instPkgs);\r
512                                 } else {\r
513                                         uninstallActionInvoke(instPkgs);\r
514                                 }\r
515                                 \r
516                                 UpdatePackageList();\r
517                         }\r
518                 }\r
519                 \r
520                 \r
521                 void WebOfficialToolStripMenuItemClick(object sender, EventArgs e)\r
522                 {\r
523                         foreach (Package pkg in SelectedPackages<Package>()) {\r
524                                 string linkURL = pkg.Url.Href;\r
525                                 \r
526                                 if (! (pkg == null || string.IsNullOrEmpty(linkURL))) {\r
527                                         System.Diagnostics.Process.Start(linkURL);\r
528                                 }\r
529                                 break;\r
530                         }\r
531                 }\r
532                 \r
533                 void WebGoogleSearchToolStripMenuItemClick(object sender, EventArgs e)\r
534                 {\r
535                         foreach (Package pkg in SelectedPackages<Package>()) {\r
536                                 string q = System.Web.HttpUtility.UrlEncode(pkg.Name, System.Text.Encoding.UTF8);\r
537                                 string googleURL = @"http://www.google.co.jp/search?q="+q;\r
538                                 \r
539                                 System.Diagnostics.Process.Start(googleURL);\r
540 \r
541                                 break;\r
542                         }\r
543                 }\r
544                 \r
545                 void OpenInstalledDirectoryStripMenuItemClick(object sender, EventArgs e)\r
546                 {\r
547                         foreach (InstalledPackage pkg in SelectedPackages<InstalledPackage>()) {\r
548                                 if (pkg.Type == InstallerType.ARCHIVE) {\r
549                                         System.Diagnostics.Process.Start(Path.Combine(NaGet.Env.ArchiveProgramFiles, pkg.Name));\r
550                                 } else if (Directory.Exists(pkg.UninstallInfo.InstallLocation)) {\r
551                                         System.Diagnostics.Process.Start(pkg.UninstallInfo.InstallLocation);\r
552                                 }\r
553 \r
554                                 break;\r
555                         }\r
556                 }\r
557                 \r
558                 \r
559                 void PropertiesToolStripMenuItemClick(object sender, EventArgs e)\r
560                 {\r
561                         foreach (Package pkg in SelectedPackages<Package>()) {\r
562                                 PackageInfoForm form = new PackageInfoForm();\r
563                                 form.SelectedObject = pkg;\r
564                                 form.Text = string.Format("{0}({1})のプロパティ", pkg.Name, pkg.Version);\r
565                                 \r
566                                 form.ShowDialog(this);\r
567 \r
568                                 break;\r
569                         }\r
570                 }\r
571                 \r
572                 void PackageListContextMenuStripOpening(object sender, System.ComponentModel.CancelEventArgs e)\r
573                 {\r
574                         // メニューアイテムのインストール/アンインストールの表示はツールストリップに同じ\r
575                         installToolStripMenuItem.Visible = installToolStripButton.Visible;\r
576                         uninstallToolStripMenuItem.Visible = uninstallToolStripButton.Visible;\r
577                         \r
578                         // インストールもアンインストールもできない状態ならば、メニューを表示しない\r
579                         if (! (installToolStripButton.Visible || uninstallToolStripButton.Visible)) {\r
580                                 e.Cancel = true;\r
581                                 return;\r
582                         }\r
583                         \r
584                         // 選択パッケージが1つのとき\r
585                         bool selectionIsOnlyOne = packageListView.SelectedItems.Count == 1;\r
586                         if (selectionIsOnlyOne) {\r
587                                 Package pkg = SelectedPackage<Package>();\r
588                                 bool isInstalledPackage = pkg is InstalledPackage;\r
589                                 \r
590                                 webResourcesToolStripMenuItem.Text = string.Format(\r
591                                         webResourcesToolStripMenuItem.Tag.ToString(),\r
592                                         pkg.Name.Replace("&", "&&")); // pkg.Nameに&が含まれているときはエンコード\r
593                                 webOfficialToolStripMenuItem.Enabled = ! (pkg.Url == null || string.IsNullOrEmpty(pkg.Url.Href));\r
594                                 // webGoogleSearchToolStripMenuItem always active.\r
595                                 \r
596                                 bool installedDirectoryStripMenuItemVisible = isInstalledPackage &&\r
597                                         ( (pkg.Type == InstallerType.ARCHIVE) || Directory.Exists(((InstalledPackage) pkg).UninstallInfo.InstallLocation) );\r
598                                 installedDirectoryStripMenuItem.Visible = installedDirectoryStripMenuItemVisible;\r
599                                 if (installedDirectoryStripMenuItemVisible) {\r
600                                         buildInstalledDirectoryMenuItemStripChildren((InstalledPackage) pkg);\r
601                                 }\r
602                         }\r
603                         \r
604                         toolStripSeparator1.Visible = selectionIsOnlyOne;\r
605                         webResourcesToolStripMenuItem.Visible = selectionIsOnlyOne;\r
606                         if (! selectionIsOnlyOne) installedDirectoryStripMenuItem.Visible = false;\r
607                         propertiesToolStripMenuItem.Visible = selectionIsOnlyOne;\r
608                 }\r
609                 \r
610                 private IEnumerable<Package> getUpdatedPackages(PackageList<InstalledPackage> installedPkgList, PackageList<Package> avaiablePkgList, IComparer<string> verComp)\r
611                 {\r
612                         foreach (InstalledPackage pkg in installedPkgList) {\r
613                                 Package avaiablePkg = avaiablePkgList.GetPackageForName(pkg.Name);\r
614                                 \r
615                                 if (avaiablePkgList != null) {\r
616                                         if (verComp.Compare(pkg.Version, avaiablePkg.Version) < 0 &&\r
617                                             installedPkgList.GetPackageForPackage(pkg.Name, avaiablePkg.Version) == null) {\r
618                                                 \r
619                                                 yield return avaiablePkg;\r
620                                         }\r
621                                 }\r
622                         }\r
623                 }\r
624                 \r
625                 void UpgradeToolStripButtonClick(object sender, EventArgs e)\r
626                 {\r
627                         List<Package> pkgs = new List<Package>();\r
628                         VersionComparetor verComp = new VersionComparetor();\r
629                         PackageList<Package> avaiablePackageList = pkgListsMan.AvailablePkgList;\r
630                         \r
631                         pkgs.AddRange(getUpdatedPackages(pkgListsMan.InstalledPkgList, avaiablePackageList, verComp));\r
632                         pkgs.AddRange(getUpdatedPackages(pkgListsMan.SystemInstalledPkgList, avaiablePackageList, verComp));\r
633                         \r
634                         if (pkgs.Count <= 0) {\r
635                                 MessageBox.Show(this, "更新されたソフトはありません", "ソフトの更新");\r
636                                 return;\r
637                         }\r
638                         \r
639                         InstallationConfirmForm confirm = new InstallationConfirmForm();\r
640                         confirm.PkgListsManager = pkgListsMan;\r
641                         confirm.Installations = Installation.ConvertInstallations(pkgs.ToArray());\r
642                         DialogResult result = confirm.ShowDialog(this);\r
643                         \r
644                         if (result == DialogResult.OK) {\r
645                                 Installation[] insts = confirm.CheckedInstallations;\r
646                                 \r
647                                 if (confirm.UseRunas) {\r
648                                         installRunasActionInvoke(insts);\r
649                                 } else {\r
650                                         installActionInvoke(insts);\r
651                                 }\r
652                                 \r
653                                 UpdatePackageList();\r
654                         }\r
655                 }\r
656                 \r
657                 public void UpdatePackageList()\r
658                 {\r
659                         pkgListsMan.LoadPackageLists();\r
660                         this.PackageListViewUpdate();\r
661                 }\r
662                 \r
663                 /// <summary>\r
664                 /// インストールするよう選択されたパッケージの配列\r
665                 /// </summary>\r
666                 public TPackage[] SelectedPackages<TPackage>() where TPackage : Package\r
667                 {\r
668                         List<TPackage> pkgs = new List<TPackage>();\r
669                         foreach (ListViewItem item in packageListView.SelectedItems) {\r
670                                 pkgs.Add((TPackage) item.Tag);\r
671                         }\r
672                         \r
673                         return pkgs.ToArray();\r
674                 }\r
675                 \r
676                 /// <summary>\r
677                 /// インストールするよう選択されたパッケージをひとつ返す。\r
678                 /// </summary>\r
679                 /// <returns>選択されたパッケージ(選択されていないときはnull)</returns>\r
680                 public TPackage SelectedPackage<TPackage>() where TPackage : Package\r
681                 {\r
682                         foreach (ListViewItem item in packageListView.SelectedItems) {\r
683                                 return (TPackage) item.Tag;\r
684                         }\r
685                         return null;\r
686                 }\r
687                 \r
688                 /// <summary>\r
689                 /// 自ウィンドウの有効無効(Enabled)を(必要あればInvokeして)実行する \r
690                 /// </summary>\r
691                 /// <param name="enabled">有効か否か。Enabledの値に入れられる</param>\r
692                 private void setWindowEnabled(bool enabled)\r
693                 {\r
694                         MethodInvoker process = (MethodInvoker) delegate() {\r
695                                 this.Enabled = enabled;\r
696                         };\r
697                         \r
698                         if (InvokeRequired) {\r
699                                 Invoke(process);\r
700                         } else {\r
701                                 process.Invoke();\r
702                         }\r
703                 }\r
704         }\r
705 \r
706 }\r