OSDN Git Service

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