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