OSDN Git Service

AppliAppliStation-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                 \r
31                 void PackageListViewSelectedIndexChanged(object sender, EventArgs e)\r
32                 {\r
33                         bool installBtnEnabled = false;\r
34                         bool uninstallBtnEnabled = false;\r
35                         \r
36                         detailBox.Clear();\r
37                         foreach (ListViewItem item in packageListView.SelectedItems) {\r
38                                 Package pkg = (Package) item.Tag;\r
39                                 \r
40                                 bool isInstalledPackage = pkg is InstalledPackage;\r
41                                 uninstallBtnEnabled = isInstalledPackage;\r
42                                 installBtnEnabled = ! isInstalledPackage;\r
43                                 \r
44                                 detailBox.SelectionFont = new Font(detailBox.Font.FontFamily, 12);\r
45                                 detailBox.SelectedText += string.Format("{0} ({1})\r\n", pkg.Name, pkg.Version);\r
46                                 if (! string.IsNullOrEmpty(pkg.Tags) ) {\r
47                                         detailBox.SelectionFont = new Font(detailBox.Font.FontFamily, 8);\r
48                                         detailBox.SelectedText += string.Format("\83^\83O: {0}\r\n", pkg.Tags);\r
49                                 }\r
50                                 if (isInstalledPackage) {\r
51                                         InstalledPackage iPkg = (InstalledPackage) pkg;\r
52                                         System.Text.StringBuilder sb = new System.Text.StringBuilder();\r
53                                         if (iPkg.UninstallInfo.InstallDate != null) {\r
54                                                 sb.AppendFormat("\83C\83\93\83X\83g\81[\83\8b\82µ\82½\93ú: {0:d}  ", iPkg.UninstallInfo.InstallDate.Value);\r
55                                         }\r
56                                         if (iPkg.UninstallInfo.EstimatedSize > 0) {\r
57                                                 sb.AppendFormat("\83T\83C\83Y: {0}  ", NaGet.Utils.FormatSize(iPkg.UninstallInfo.EstimatedSize*1024));\r
58                                         }\r
59                                         \r
60                                         if (sb.Length > 0) {\r
61                                                 detailBox.SelectionFont = new Font(detailBox.Font.FontFamily, 8);\r
62                                                 detailBox.SelectedText += sb.ToString();\r
63                                                 detailBox.SelectedText += "\r\n";\r
64                                         }\r
65                                 }\r
66                                 detailBox.SelectionFont = detailBox.Font;\r
67                                 if (pkg.Url != null && pkg.Url.Href != null) {\r
68                                         detailBox.SelectedText += "\8cö\8e®\83T\83C\83g: " + pkg.Url.Href + "\r\n";\r
69                                 }\r
70                                 detailBox.SelectedText += pkg.Summary;\r
71                                 \r
72                                 break;\r
73                         }\r
74                         \r
75                         uninstallToolStripButton.Visible = uninstallBtnEnabled;\r
76                         installToolStripButton.Visible = installBtnEnabled;\r
77                         \r
78                         if (packageListView.SelectedItems.Count <= 0) {\r
79                                 detailBox.SelectionFont = detailBox.Font;\r
80                                 int count = packageListView.Items.Count;\r
81                                 detailBox.Text = (count > 0) ? string.Format("{0}\8cÂ\82Ì\83\\83t\83g\82ª\82 \82è\82Ü\82·\81B", count)\r
82                                         : "\8aY\93\96\82·\82é\83\\83t\83g\82ª\82 \82è\82Ü\82¹\82ñ\81B";\r
83                         }\r
84                         \r
85                         detailBox.SelectionStart = 0;\r
86                         detailBox.ScrollToCaret();\r
87                 }\r
88 \r
89                 void PackageListViewItemActivate(object sender, EventArgs e)\r
90                 {\r
91                         if (packageListView.SelectedItems.Count <= 0) {\r
92                                 return; // exit \r
93                         }\r
94                         \r
95                         foreach (ListViewItem item in packageListView.SelectedItems) {\r
96                                 Package pkg = (Package) item.Tag;\r
97                                 \r
98                                 if (pkg is InstalledPackage) {\r
99                                         UninstallToolStripButtonClick(sender, e);\r
100                                 } else {\r
101                                         InstallToolStripButtonClick(sender, e);\r
102                                 }\r
103                                 \r
104                                 break;\r
105                         }\r
106                 }\r
107                 \r
108                 AppliStation.Util.ListViewItemSortComparer packageListViewSortComparer;\r
109 \r
110                 void PackageListViewColumnClick(object sender, ColumnClickEventArgs e)\r
111                 {\r
112                         SortOrder order = SortOrder.None;\r
113                         \r
114                         if (packageListViewSortComparer == null) {\r
115                                 order = SortOrder.Ascending;\r
116                                 packageListViewSortComparer = new AppliStation.Util.ListViewItemSortComparer(e.Column, order);\r
117                                 packageListView.ListViewItemSorter = packageListViewSortComparer;\r
118                         } else {\r
119                                 if (packageListViewSortComparer.Column == e.Column) {\r
120                                         order = (packageListViewSortComparer.Order == SortOrder.Ascending)? SortOrder.Descending : SortOrder.Ascending;\r
121                                         \r
122                                         packageListViewSortComparer.Order = order;\r
123                                 } else {\r
124                                         order = packageListViewSortComparer.Order;\r
125                                         packageListViewSortComparer.Column = e.Column;\r
126                                 }\r
127                                 \r
128                                 packageListView.Sort();\r
129                         }\r
130                         AppliStation.Util.NativeMethods.ColumnHeader_SetSortState(packageListView, e.Column, order);\r
131                         \r
132                         // \83\\81[\83g\91Î\8fÛ\97ñ\82Ì\90F\95t\82¯\r
133                         try {\r
134                                 // SendMessage(hWnd, LVM_SETSELECTEDCOLUMN, column, NULL);\r
135                                 AppliStation.Util.NativeMethods.SendMessage(packageListView.Handle, 0x1000+140, (uint) e.Column, 0);\r
136                         } catch (Exception) {   \r
137                         }\r
138                 }\r
139                 \r
140                 void Form_OnLoad(object sender, EventArgs e)\r
141                 {\r
142                         System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(PackageListViewForm));\r
143                         ImageList imageList = new ImageList();\r
144                         imageList.Images.Add("installed", this.Icon);\r
145                         imageList.Images.Add("sys", (Icon)(resources.GetObject("sysinstalled.Icon")));\r
146                         packageListView.SmallImageList = imageList;\r
147                         \r
148                         updatePackageFilterToolStripMenuItemCheckState();\r
149                         installedASPackageFilterToolStripMenuItem.Image = this.Icon.ToBitmap();\r
150                         installedSysPackageFilterToolStripMenuItem.Image = ((Icon)(resources.GetObject("sysinstalled.Icon"))).ToBitmap();\r
151                 }\r
152                 \r
153                 #region PackageFilter\8aÖ\98A\r
154                 \r
155                 private enum PackageFilterIndex : int\r
156                 {\r
157                         All = 0,\r
158                         NotInstalled = 1,\r
159                         InstalledAS = 2,\r
160                         InstalledSys = 3,\r
161                 }\r
162                 \r
163                 private PackageFilterIndex currentPackageFilter = PackageFilterIndex.All;\r
164                 \r
165                 private PackageFilterIndex CurrentPackageFilter {\r
166                         get {\r
167                                 return currentPackageFilter;\r
168                         }\r
169                         set {\r
170                                 currentPackageFilter = value;\r
171                                 \r
172                                 updatePackageFilterToolStripMenuItemCheckState();\r
173                         }\r
174                 }\r
175                 \r
176                 private void updatePackageFilterToolStripMenuItemCheckState()\r
177                 {\r
178                         ToolStripMenuItem selected = getCheckedPackageFilterToolStripItem();\r
179                         \r
180                         foreach (ToolStripMenuItem item in packageFilterToolStripDropDownButton.DropDown.Items) {\r
181                                 item.Checked = selected == item;\r
182                         }\r
183                         \r
184                         packageFilterToolStripDropDownButton.Text = selected.Text;\r
185                         packageFilterToolStripDropDownButton.Image = selected.Image;\r
186                         packageFilterToolStripDropDownButton.ToolTipText = selected.ToolTipText;\r
187                 }\r
188                 \r
189                 private ToolStripMenuItem getCheckedPackageFilterToolStripItem()\r
190                 {\r
191                         switch (currentPackageFilter) {\r
192                                 case PackageFilterIndex.NotInstalled:\r
193                                         return notInstalledPackageFilterToolStripMenuItem;\r
194                                 case PackageFilterIndex.InstalledAS:\r
195                                         return installedASPackageFilterToolStripMenuItem;\r
196                                 case PackageFilterIndex.InstalledSys:\r
197                                         return installedSysPackageFilterToolStripMenuItem;\r
198                                 default:\r
199                                         return allPackageFilterToolStripMenuItem;\r
200                         }\r
201                 }\r
202                 \r
203                 void AnyPackageFilterToolStripMenuItemClicked(object sender, EventArgs e)\r
204                 {\r
205                         if (sender == notInstalledPackageFilterToolStripMenuItem) {\r
206                                 currentPackageFilter = PackageFilterIndex.NotInstalled;\r
207                         } else if (sender == installedASPackageFilterToolStripMenuItem) {\r
208                                 currentPackageFilter = PackageFilterIndex.InstalledAS;\r
209                         } else if (sender == installedSysPackageFilterToolStripMenuItem) {\r
210                                 currentPackageFilter = PackageFilterIndex.InstalledSys;\r
211                         } else {\r
212                                 currentPackageFilter = PackageFilterIndex.All;\r
213                         }\r
214                         \r
215                         updatePackageFilterToolStripMenuItemCheckState();\r
216                         searchTextBoxUpdate(false);\r
217                 }\r
218                 \r
219                 #endregion\r
220                 \r
221                 void PackageListViewUpdate()\r
222                 {\r
223                         this.packageListView.Items.Clear();\r
224                         \r
225                         if (currentPackageFilter == PackageFilterIndex.All || currentPackageFilter == PackageFilterIndex.NotInstalled) {\r
226                                 foreach (Package pkg in pkgListsMan.AvailablePkgList.Search(this.SearchKeyword)) {\r
227                                         if (Installation.GetPreferInstallerIndex(pkg) >= 0) { // \83C\83\93\83X\83g\81[\83\8b\89Â\94\\r
228                                                 if (currentPackageFilter == PackageFilterIndex.NotInstalled &&\r
229                                                     (pkgListsMan.InstalledPkgList.GetPackageForName(pkg.Name) != null||\r
230                                                      pkgListsMan.SystemInstalledPkgList.GetPackageForName(pkg.Name) != null) ) {\r
231                                                         continue; // \83C\83\93\83X\83g\81[\83\8b\8dÏ\82Ý\82Í\8e\9f\82Ì\83\8b\81[\83v\82Ö(\96¢\83C\83\93\83X\83g\81[\83\8b\83\\83t\83g\92\8a\8fo\8e\9e)\r
232                                                 }\r
233                                                 \r
234                                                 ListViewItem item = new ListViewItem(new string[]{pkg.Name, pkg.Version, pkg.Summary});\r
235                                                 item.Tag = pkg;\r
236                                                 item.ToolTipText = pkg.Summary;\r
237                 \r
238                                                 this.packageListView.Items.Add(item);\r
239                                         }\r
240                                 }\r
241                         }\r
242                         \r
243                         if (currentPackageFilter == PackageFilterIndex.All || currentPackageFilter == PackageFilterIndex.InstalledAS) {\r
244                                 foreach (Package pkg in pkgListsMan.InstalledPkgList.Search(this.SearchKeyword)) {\r
245                                         ListViewItem item = new ListViewItem(new string[]{pkg.Name, pkg.Version, pkg.Summary});\r
246                                         item.Tag = pkg;\r
247                                         item.ToolTipText = pkg.Summary;\r
248                                         item.ImageKey = "installed";\r
249         \r
250                                         this.packageListView.Items.Add(item);\r
251                                 }\r
252                         }\r
253                         \r
254                         if (currentPackageFilter == PackageFilterIndex.All || currentPackageFilter == PackageFilterIndex.InstalledSys) {\r
255                                 foreach (Package pkg in pkgListsMan.SystemInstalledPkgList.Search(this.SearchKeyword)) {\r
256                                         ListViewItem item = new ListViewItem(new string[]{pkg.Name, pkg.Version, pkg.Summary});\r
257                                         item.Tag = pkg;\r
258                                         item.ToolTipText = pkg.Summary;\r
259                                         item.ImageKey = "sys";\r
260         \r
261                                         this.packageListView.Items.Add(item);\r
262                                 }\r
263                         }\r
264                         \r
265                         PackageListViewSelectedIndexChanged(packageListView, null);\r
266                         detailBox.Refresh();\r
267                         \r
268                         //this.packageListView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);\r
269                 }\r
270                 \r
271                 void PackageDetailBoxLinkClicked(object sender, LinkClickedEventArgs e)\r
272                 {\r
273                         System.Diagnostics.Process.Start(e.LinkText);\r
274                 }\r
275                 \r
276                 internal void updateActionInvoke(bool downloadPackageListsFlag)\r
277                 {\r
278                         AppliStation.Util.ExecutionProgressViewer prog = new AppliStation.Util.ExecutionProgressViewer();\r
279                         prog.Shown += delegate(object sender2, EventArgs e2) {\r
280                                 NaGet.SubCommands.NaGetUpdate tasks = new NaGet.SubCommands.NaGetUpdate(pkgListsMan, downloadPackageListsFlag);\r
281                                 tasks.Downloader = prog.Downloader;\r
282                                 prog.SetTaskSet(tasks);\r
283                                 prog.Refresh();\r
284                                 prog.StartTaskSet();\r
285                         };\r
286                         prog.Text = "\83\8a\83X\83g\82Ì\8dX\90V";\r
287                         prog.ShowDialog(this);\r
288                 }\r
289                         \r
290                 void UpdateToolStripButtonClick(object sender, EventArgs e)\r
291                 {\r
292                         updateActionInvoke(true);\r
293                         \r
294                         UpdatePackageList();\r
295                 }\r
296                 \r
297                 void LocalupdateToolStripMenuItemClick(object sender, EventArgs e)\r
298                 {\r
299                         updateActionInvoke(false);\r
300                         \r
301                         UpdatePackageList();\r
302                 }\r
303                 \r
304                 #region searchTextBox\82Ü\82í\82è\r
305                 \r
306                 private bool searchTextBoxIsEmpty = true;\r
307                 \r
308                 private Timer searchUpdateTimer = null;\r
309                 \r
310                 string SearchKeyword {\r
311                         get {\r
312                                 return searchTextBoxIsEmpty? string.Empty : searchTextBox.Text;\r
313                         }\r
314                 }\r
315                 \r
316                 void SearchTextBoxEnter(object sender, EventArgs e)\r
317                 {\r
318                         if (searchTextBoxIsEmpty) {\r
319                                 searchTextBox.Text = string.Empty;\r
320                                 searchTextBox.ForeColor = DefaultForeColor;\r
321                                 searchTextBoxIsEmpty = false;\r
322                         }\r
323                 }\r
324                 \r
325                 void SearchTextBoxLeave(object sender, EventArgs e)\r
326                 {\r
327                         if (string.IsNullOrEmpty(searchTextBox.Text)) {\r
328                                 searchTextBox.Text = "\8c\9f\8dõ";\r
329                                 searchTextBox.ForeColor = SystemColors.GrayText;\r
330                                 searchTextBoxIsEmpty = true;\r
331                         }\r
332                 }\r
333                 \r
334                 void SearchTextBoxKeyPress(object sender, KeyPressEventArgs e)\r
335                 {\r
336                         switch (e.KeyChar) {\r
337                                 case (char)Keys.Enter:\r
338                                         searchTextBoxUpdate(true);\r
339                                         break;\r
340                                 case (char)Keys.Escape:\r
341                                         searchTextBox.Text = "";\r
342                                         searchTextBoxUpdate(false);\r
343                                         break;\r
344                                 default:\r
345                                         searchTextBoxUpdate(false);\r
346                                         break;\r
347                         }\r
348                 }\r
349                 \r
350                 private void searchTextBoxUpdate(bool force)\r
351                 {\r
352                         if (! this.Created) return;\r
353                         \r
354                         if (searchUpdateTimer != null) {\r
355                                 searchUpdateTimer.Stop();\r
356                         } else {\r
357                                 searchUpdateTimer = new Timer();\r
358                                 searchUpdateTimer.Tick += searchUpdateTimerTick;\r
359                         }\r
360                         searchUpdateTimer.Interval = (force)? 10 : 500;\r
361                         searchUpdateTimer.Start();\r
362                 }\r
363                 \r
364                 private void searchUpdateTimerTick(object sender, EventArgs e)\r
365                 {\r
366                         searchUpdateTimer.Stop();\r
367                         \r
368                         PackageListViewUpdate();\r
369                 }\r
370                 \r
371                 #endregion\r
372                 \r
373                 internal void installActionInvoke(Package[] pkgs)\r
374                 {\r
375                         AppliStation.Util.ExecutionProgressViewer prog = new AppliStation.Util.ExecutionProgressViewer();\r
376                         prog.Shown += delegate(object sender2, EventArgs e2) {\r
377                                 NaGet.SubCommands.NaGetInstall tasks = new NaGet.SubCommands.NaGetInstall(pkgListsMan, pkgs);\r
378                                 tasks.Downloader = prog.Downloader;\r
379                                 prog.SetTaskSet(tasks);\r
380                                 prog.Refresh();\r
381                                 prog.StartTaskSet();\r
382                         };\r
383                         prog.Text = string.Format("\83\\83t\83g\83E\83F\83A\82Ì\83C\83\93\83X\83g\81[\83\8b");\r
384                         prog.ShowDialog(this);\r
385                 }\r
386                 \r
387                 void InstallToolStripButtonClick(object sender, EventArgs e)\r
388                 {\r
389                         PackagesInstallConfirmForm confirm = new PackagesInstallConfirmForm();\r
390                         confirm.PkgListsManager = pkgListsMan;\r
391                         confirm.Packages = SelectedPackages<Package>();\r
392                         confirm.UseRunas = confirm.GetShouldUseRunas();\r
393                         DialogResult result = confirm.ShowDialog(this);\r
394                         \r
395                         if (result == DialogResult.OK) {\r
396                                 Package[] instPkgs = confirm.CheckedPackages;\r
397                                 \r
398                                 if (confirm.UseRunas) {\r
399                                         this.Enabled = false;\r
400                                         installRunasActionInvoke(instPkgs);\r
401                                         this.Enabled = true;\r
402                                         this.Focus();\r
403                                 } else {\r
404                                         installActionInvoke(instPkgs);\r
405                                 }\r
406                                 \r
407                                 UpdatePackageList();\r
408                         }\r
409                 }\r
410                 \r
411                 public void installRunasActionInvoke(Package[] pkgs)\r
412                 {\r
413                         string tmpfileName = System.IO.Path.GetTempFileName();\r
414                         try {\r
415                                 NaGet.Utils.PutSerializeObject(tmpfileName, pkgs);\r
416                                 \r
417                                 System.Diagnostics.ProcessStartInfo procInfo = new System.Diagnostics.ProcessStartInfo();\r
418                                 procInfo.FileName = Application.ExecutablePath;\r
419                                 procInfo.Arguments = string.Format("--noupdate --cmd=install \"--pkgsref={0}\"", tmpfileName);\r
420                                 procInfo.Verb = "runas";\r
421                                 procInfo.WorkingDirectory = Environment.CurrentDirectory;\r
422                                 \r
423                                 using (System.Diagnostics.Process hProc = System.Diagnostics.Process.Start(procInfo)) {\r
424                                         hProc.WaitForExit();\r
425                                 }\r
426                                 \r
427                                 pkgListsMan.LoadPackageLists();\r
428                         } catch (System.ComponentModel.Win32Exception ex) {\r
429                                 MessageBox.Show(ex.Message, "\83C\83\93\83X\83g\81[\83\8b", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
430                         } finally {\r
431                                 if (File.Exists(tmpfileName)) {\r
432                                         File.Delete(tmpfileName);\r
433                                 }\r
434                         }\r
435                 }\r
436                 \r
437                 internal void uninstallActionInvoke(InstalledPackage[] pkgs)\r
438                 {\r
439                         AppliStation.Util.ExecutionProgressViewer prog = new AppliStation.Util.ExecutionProgressViewer();\r
440                         prog.Shown += delegate(object sender2, EventArgs e2) {\r
441                                 NaGet.SubCommands.NaGetUninstall tasks = new NaGet.SubCommands.NaGetUninstall(pkgListsMan, pkgs);\r
442                                 prog.SetTaskSet(tasks);\r
443                                 prog.Refresh();\r
444                                 prog.StartTaskSet();\r
445                         };\r
446                         prog.Text = string.Format("\83\\83t\83g\83E\83F\83A\82Ì\83A\83\93\83C\83\93\83X\83g\81[\83\8b");\r
447                         prog.ShowDialog(this);\r
448                 }\r
449                 \r
450                 internal void uninstallRunasActionInvoke(InstalledPackage[] pkgs)\r
451                 {\r
452                         string tmpfileName = System.IO.Path.GetTempFileName();\r
453                         try {\r
454                                 NaGet.Utils.PutSerializeObject(tmpfileName, pkgs);\r
455                                 \r
456                                 System.Diagnostics.ProcessStartInfo procInfo = new System.Diagnostics.ProcessStartInfo();\r
457                                 procInfo.FileName = Application.ExecutablePath;\r
458                                 procInfo.Arguments = string.Format("--noupdate --cmd=uninstall \"--pkgsref={0}\"", tmpfileName);\r
459                                 procInfo.Verb = "runas";\r
460                                 procInfo.WorkingDirectory = Environment.CurrentDirectory;\r
461                                 \r
462                                 using (System.Diagnostics.Process hProc = System.Diagnostics.Process.Start(procInfo)) {\r
463                                         hProc.WaitForExit();\r
464                                 }\r
465                                 \r
466                                 pkgListsMan.LoadPackageLists();\r
467                         } catch (System.ComponentModel.Win32Exception ex) {\r
468                                 MessageBox.Show(ex.Message, "\83A\83\93\83C\83\93\83X\83g\81[\83\8b", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
469                         } finally {\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                                         this.Enabled = false;\r
491                                         uninstallRunasActionInvoke(instPkgs);\r
492                                         this.Enabled = true;\r
493                                         this.Focus();\r
494                                 } else {\r
495                                         uninstallActionInvoke(instPkgs);\r
496                                 }\r
497                                 \r
498                                 UpdatePackageList();\r
499                         }\r
500                 }\r
501                 \r
502                 \r
503                 void WebOfficialToolStripMenuItemClick(object sender, EventArgs e)\r
504                 {\r
505                         foreach (Package pkg in SelectedPackages<Package>()) {\r
506                                 string linkURL = pkg.Url.Href;\r
507                                 \r
508                                 if (! (pkg == null || string.IsNullOrEmpty(linkURL))) {\r
509                                         System.Diagnostics.Process.Start(linkURL);\r
510                                 }\r
511                                 break;\r
512                         }\r
513                 }\r
514                 \r
515                 void WebGoogleSearchToolStripMenuItemClick(object sender, EventArgs e)\r
516                 {\r
517                         foreach (Package pkg in SelectedPackages<Package>()) {\r
518                                 string q = System.Web.HttpUtility.UrlEncode(pkg.Name, System.Text.Encoding.UTF8);\r
519                                 string googleURL = @"http://www.google.co.jp/search?q="+q;\r
520                                 \r
521                                 System.Diagnostics.Process.Start(googleURL);\r
522 \r
523                                 break;\r
524                         }\r
525                 }\r
526                 \r
527                 void OpenInstalledDirectoryStripMenuItemClick(object sender, EventArgs e)\r
528                 {\r
529                         foreach (InstalledPackage pkg in SelectedPackages<InstalledPackage>()) {\r
530                                 if (pkg.Type == InstallerType.ARCHIVE) {\r
531                                         System.Diagnostics.Process.Start(Path.Combine(NaGet.Env.ArchiveProgramFiles, pkg.Name));\r
532                                 } else if (Directory.Exists(pkg.UninstallInfo.InstallLocation)) {\r
533                                         System.Diagnostics.Process.Start(pkg.UninstallInfo.InstallLocation);\r
534                                 }\r
535 \r
536                                 break;\r
537                         }\r
538                 }\r
539                 \r
540                 void PackageListContextMenuStripOpening(object sender, System.ComponentModel.CancelEventArgs e)\r
541                 {\r
542                         // \91I\91ð\82³\82ê\82Ä\82¢\82È\82¢\82È\82ç\8aJ\82©\82È\82¢\r
543                         if (packageListView.SelectedItems.Count <= 0) {\r
544                                 e.Cancel = true;\r
545                                 return;\r
546                         }\r
547                         \r
548                         foreach (Package pkg in SelectedPackages<Package>()) {\r
549                                 bool isInstalledPackage = pkg is InstalledPackage;\r
550                                 \r
551                                 installToolStripMenuItem.Visible = ! isInstalledPackage;\r
552                                 uninstallToolStripMenuItem.Visible = isInstalledPackage;\r
553                                 \r
554                                 webResourcesToolStripMenuItem.Text = string.Format(webResourcesToolStripMenuItem.Tag.ToString(), pkg.Name);\r
555                                 webOfficialToolStripMenuItem.Enabled = ! (pkg.Url == null || string.IsNullOrEmpty(pkg.Url.Href));\r
556                                 // webGoogleSearchToolStripMenuItem always active.\r
557                                 \r
558                                 openInstalledDirectoryStripMenuItem.Visible = isInstalledPackage &&\r
559                                         ( (pkg.Type == InstallerType.ARCHIVE) || Directory.Exists(((InstalledPackage) pkg).UninstallInfo.InstallLocation) );\r
560 \r
561                                 break;\r
562                         }\r
563                 }\r
564                 \r
565                 private IEnumerable<Package> getUpdatedPackages(PackageList<InstalledPackage> installedPkgList, PackageList<Package> avaiablePkgList, IComparer<string> verComp)\r
566                 {\r
567                         foreach (InstalledPackage pkg in installedPkgList.Packages) {\r
568                                 Package avaiablePkg = avaiablePkgList.GetPackageForName(pkg.Name);\r
569                                 \r
570                                 if (avaiablePkgList != null) {\r
571                                         if (verComp.Compare(pkg.Version, avaiablePkg.Version) < 0 &&\r
572                                             installedPkgList.GetPackageForPackage(pkg.Name, avaiablePkg.Version) == null) {\r
573                                                 \r
574                                                 yield return avaiablePkg;\r
575                                         }\r
576                                 }\r
577                         }\r
578                 }\r
579                 \r
580                 void UpgradeToolStripButtonClick(object sender, EventArgs e)\r
581                 {\r
582                         List<Package> pkgs = new List<Package>();\r
583                         VersionComparetor verComp = new VersionComparetor();\r
584                         PackageList<Package> avaiablePackageList = pkgListsMan.AvailablePkgList;\r
585                         \r
586                         pkgs.AddRange(getUpdatedPackages(pkgListsMan.InstalledPkgList, avaiablePackageList, verComp));\r
587                         pkgs.AddRange(getUpdatedPackages(pkgListsMan.SystemInstalledPkgList, avaiablePackageList, verComp));\r
588                         \r
589                         if (pkgs.Count <= 0) {\r
590                                 MessageBox.Show(this, "\8dX\90V\82³\82ê\82½\83\\83t\83g\82Í\82 \82è\82Ü\82¹\82ñ", "\83\\83t\83g\82Ì\8dX\90V");\r
591                                 return;\r
592                         }\r
593                         \r
594                         PackagesInstallConfirmForm confirm = new PackagesInstallConfirmForm();\r
595                         confirm.PkgListsManager = pkgListsMan;\r
596                         confirm.Packages = pkgs.ToArray();\r
597                         DialogResult result = confirm.ShowDialog(this);\r
598                         \r
599                         if (result == DialogResult.OK) {\r
600                                 Package[] instPkgs = confirm.CheckedPackages;\r
601                                 \r
602                                 if (confirm.UseRunas) {\r
603                                         this.Enabled = false;\r
604                                         installRunasActionInvoke(instPkgs);\r
605                                         this.Enabled = true;\r
606                                 } else {\r
607                                         installActionInvoke(instPkgs);\r
608                                 }\r
609                                 \r
610                                 UpdatePackageList();\r
611                         }\r
612                 }\r
613                 \r
614                 public void UpdatePackageList()\r
615                 {\r
616                         pkgListsMan.LoadPackageLists();\r
617                         this.PackageListViewUpdate();\r
618                 }\r
619                 \r
620                 /// <summary>\r
621                 /// \83C\83\93\83X\83g\81[\83\8b\82·\82é\82æ\82¤\91I\91ð\82³\82ê\82½\83p\83b\83P\81[\83W\82Ì\94z\97ñ\r
622                 /// </summary>\r
623                 public TPackage[] SelectedPackages<TPackage>() where TPackage : Package\r
624                 {\r
625                                 List<TPackage> pkgs = new List<TPackage>();\r
626                                 foreach (ListViewItem item in packageListView.SelectedItems) {\r
627                                         pkgs.Add((TPackage) item.Tag);\r
628                                 }\r
629                                 \r
630                                 return pkgs.ToArray();\r
631                 }\r
632         }\r
633 \r
634 }\r