OSDN Git Service

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