OSDN Git Service

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