OSDN Git Service

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