OSDN Git Service

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