OSDN Git Service

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