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                                 System.Diagnostics.Process.Start(e.LinkText);\r
317                         } else {\r
318                                 /* それ以外はタグとみなして検索 */\r
319                                 searchTextBox.Text = e.LinkText;\r
320                         }\r
321                 }\r
322                 \r
323                 internal void updateActionInvoke(bool downloadPackageListsFlag)\r
324                 {\r
325                         AppliStation.Util.ExecutionProgressViewer prog = new AppliStation.Util.ExecutionProgressViewer();\r
326                         prog.Shown += delegate(object sender2, EventArgs e2) {\r
327                                 NaGet.SubCommands.NaGetUpdate tasks = new NaGet.SubCommands.NaGetUpdate(pkgListsMan, downloadPackageListsFlag);\r
328                                 prog.SetTaskSet(tasks);\r
329                                 prog.Refresh();\r
330                                 prog.StartTaskSet();\r
331                         };\r
332                         prog.Text = "リストの更新";\r
333                         prog.ShowDialog(this);\r
334                 }\r
335                         \r
336                 void UpdateToolStripButtonClick(object sender, EventArgs e)\r
337                 {\r
338                         updateActionInvoke(true);\r
339                         \r
340                         UpdatePackageList();\r
341                 }\r
342                 \r
343                 void LocalupdateToolStripMenuItemClick(object sender, EventArgs e)\r
344                 {\r
345                         updateActionInvoke(false);\r
346                         \r
347                         UpdatePackageList();\r
348                 }\r
349                 \r
350                 #region searchTextBoxまわり\r
351                 \r
352                 void SearchTextBoxKeyPress(object sender, KeyPressEventArgs e)\r
353                 {\r
354                         switch (e.KeyChar) {\r
355                                 case (char)Keys.Enter:\r
356                                         searchTextBox.FireTextChangedTrigger();\r
357                                         break;\r
358                                 case (char)Keys.Escape:\r
359                                         searchTextBox.Text = string.Empty;\r
360                                         break;\r
361                         }\r
362                 }\r
363                 \r
364                 void SearchTextBoxTextChangedTriggerFired(object sender, EventArgs e)\r
365                 {\r
366                         PackageListViewUpdate();\r
367                 }\r
368                 \r
369                 #endregion\r
370                 \r
371                 internal void installActionInvoke(Installation[] insts)\r
372                 {\r
373                         AppliStation.Util.ExecutionProgressViewer prog = new AppliStation.Util.ExecutionProgressViewer();\r
374                         prog.Shown += delegate(object sender2, EventArgs e2) {\r
375                                 NaGet.SubCommands.NaGetInstall tasks = new NaGet.SubCommands.NaGetInstall(pkgListsMan, insts);\r
376                                 prog.SetTaskSet(tasks);\r
377                                 prog.Refresh();\r
378                                 prog.StartTaskSet();\r
379                         };\r
380                         prog.Text = string.Format("ソフトウェアのインストール");\r
381                         prog.ShowDialog(this);\r
382                 }\r
383                 \r
384                 void InstallToolStripButtonClick(object sender, EventArgs e)\r
385                 {\r
386                         InstallationConfirmForm confirm = new InstallationConfirmForm();\r
387                         confirm.PkgListsManager = pkgListsMan;\r
388                         confirm.Installations = Installation.ConvertInstallations( NaGet.Utils.IEnumerable2Array(SelectedPackages<Package>()) );\r
389                         confirm.UseRunas = confirm.GetShouldUseRunas();\r
390                         DialogResult result = confirm.ShowDialog(this);\r
391                         \r
392                         if (result == DialogResult.OK) {\r
393                                 Installation[] insts = confirm.CheckedInstallations;\r
394                                 \r
395                                 if (confirm.UseRunas) {\r
396                                         installRunasActionInvoke(insts);\r
397                                 } else {\r
398                                         installActionInvoke(insts);\r
399                                 }\r
400                                 \r
401                                 UpdatePackageList();\r
402                         }\r
403                 }\r
404                 \r
405                 public void installRunasActionInvoke(Installation[] insts)\r
406                 {\r
407                         this.setWindowEnabled(false);\r
408                         \r
409                         string tmpfileName = Path.GetTempFileName();\r
410                         try {\r
411                                 NaGet.Utils.PutSerializeObject(tmpfileName, insts);\r
412                                 \r
413                                 System.Diagnostics.ProcessStartInfo procInfo = new System.Diagnostics.ProcessStartInfo();\r
414                                 procInfo.FileName = Application.ExecutablePath;\r
415                                 procInfo.Arguments = string.Format("--noupdate --cmd=install \"--instsref={0}\"", tmpfileName);\r
416                                 procInfo.Verb = "runas";\r
417                                 procInfo.WorkingDirectory = Environment.CurrentDirectory;\r
418                                 \r
419                                 System.Diagnostics.Process hProc = System.Diagnostics.Process.Start(procInfo);\r
420                                 hProc.EnableRaisingEvents = true;\r
421                                 hProc.SynchronizingObject = this;\r
422                                 hProc.Exited += delegate(object sender, EventArgs e) {\r
423                                         UpdatePackageList();\r
424                                         \r
425                                         this.setWindowEnabled(true);\r
426                                         this.BringToFront();\r
427                                         \r
428                                         if (File.Exists(tmpfileName)) {\r
429                                                 File.Delete(tmpfileName);\r
430                                         }\r
431                                 };\r
432                         } catch (System.ComponentModel.Win32Exception ex) {\r
433                                 MessageBox.Show(ex.Message, "インストール", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
434                                 \r
435                                 if (File.Exists(tmpfileName)) {\r
436                                         File.Delete(tmpfileName);\r
437                                 }\r
438                                 this.setWindowEnabled(true);\r
439                         }\r
440                 }\r
441                 \r
442                 internal void uninstallActionInvoke(InstalledPackage[] pkgs)\r
443                 {\r
444                         AppliStation.Util.ExecutionProgressViewer prog = new AppliStation.Util.ExecutionProgressViewer();\r
445                         prog.Shown += delegate(object sender2, EventArgs e2) {\r
446                                 NaGet.SubCommands.NaGetUninstall tasks = new NaGet.SubCommands.NaGetUninstall(pkgListsMan, pkgs);\r
447                                 prog.SetTaskSet(tasks);\r
448                                 prog.Refresh();\r
449                                 prog.StartTaskSet();\r
450                         };\r
451                         prog.Text = string.Format("ソフトウェアのアンインストール");\r
452                         prog.ShowDialog(this);\r
453                 }\r
454                 \r
455                 internal void uninstallRunasActionInvoke(InstalledPackage[] pkgs)\r
456                 {                       \r
457                         this.setWindowEnabled(false);\r
458                         \r
459                         string tmpfileName = Path.GetTempFileName();\r
460                         try {\r
461                                 NaGet.Utils.PutSerializeObject(tmpfileName, pkgs);\r
462                                 \r
463                                 System.Diagnostics.ProcessStartInfo procInfo = new System.Diagnostics.ProcessStartInfo();\r
464                                 procInfo.FileName = Application.ExecutablePath;\r
465                                 procInfo.Arguments = string.Format("--noupdate --cmd=uninstall \"--pkgsref={0}\"", tmpfileName);\r
466                                 procInfo.Verb = "runas";\r
467                                 procInfo.WorkingDirectory = Environment.CurrentDirectory;\r
468                                 \r
469                                 System.Diagnostics.Process hProc = System.Diagnostics.Process.Start(procInfo);\r
470                                 hProc.EnableRaisingEvents = true;\r
471                                 hProc.SynchronizingObject = this;\r
472                                 hProc.Exited += delegate(object sender, EventArgs e) {\r
473                                         UpdatePackageList();\r
474                                         \r
475                                         this.setWindowEnabled(true);\r
476                                         this.BringToFront();\r
477                                         \r
478                                         if (File.Exists(tmpfileName)) {\r
479                                                 File.Delete(tmpfileName);\r
480                                         }\r
481                                 };\r
482                         } catch (System.ComponentModel.Win32Exception ex) {\r
483                                 MessageBox.Show(ex.Message, "アンインストール", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
484                                 \r
485                                 this.setWindowEnabled(true);\r
486                                 if (File.Exists(tmpfileName)) {\r
487                                         File.Delete(tmpfileName);\r
488                                 }\r
489                         }\r
490                 }\r
491                 \r
492                 void UninstallToolStripButtonClick(object sender, EventArgs e)\r
493                 {\r
494                         PackageUninstallConfirmForm confirm = new PackageUninstallConfirmForm();\r
495                         foreach (InstalledPackage pkg in SelectedPackages<InstalledPackage>()) {\r
496                                 confirm.UninstallPackage = pkg;\r
497                                 break;\r
498                         }\r
499                         confirm.UseRunas = confirm.GetShouldUseRunas();\r
500                         DialogResult result = confirm.ShowDialog(this);\r
501                         \r
502                         if (result == DialogResult.OK) {\r
503                                 InstalledPackage[] instPkgs = new InstalledPackage[]{confirm.UninstallPackage};\r
504                                 \r
505                                 if (confirm.UseRunas) {\r
506                                         uninstallRunasActionInvoke(instPkgs);\r
507                                 } else {\r
508                                         uninstallActionInvoke(instPkgs);\r
509                                 }\r
510                                 \r
511                                 UpdatePackageList();\r
512                         }\r
513                 }\r
514                 \r
515                 \r
516                 void WebOfficialToolStripMenuItemClick(object sender, EventArgs e)\r
517                 {\r
518                         foreach (Package pkg in SelectedPackages<Package>()) {\r
519                                 string linkURL = pkg.Url.Href;\r
520                                 \r
521                                 if (! (pkg == null || string.IsNullOrEmpty(linkURL))) {\r
522                                         System.Diagnostics.Process.Start(linkURL);\r
523                                 }\r
524                                 break;\r
525                         }\r
526                 }\r
527                 \r
528                 void WebGoogleSearchToolStripMenuItemClick(object sender, EventArgs e)\r
529                 {\r
530                         foreach (Package pkg in SelectedPackages<Package>()) {\r
531                                 string q = System.Web.HttpUtility.UrlEncode(pkg.Name, System.Text.Encoding.UTF8);\r
532                                 string googleURL = @"http://www.google.co.jp/search?q="+q;\r
533                                 \r
534                                 System.Diagnostics.Process.Start(googleURL);\r
535 \r
536                                 break;\r
537                         }\r
538                 }\r
539                 \r
540                 void OpenInstalledDirectoryStripMenuItemClick(object sender, EventArgs e)\r
541                 {\r
542                         foreach (InstalledPackage pkg in SelectedPackages<InstalledPackage>()) {\r
543                                 if (pkg.Type == InstallerType.ARCHIVE) {\r
544                                         System.Diagnostics.Process.Start(Path.Combine(NaGet.Env.ArchiveProgramFiles, pkg.Name));\r
545                                 } else if (Directory.Exists(pkg.discoverInstalledLocation())) {\r
546                                         System.Diagnostics.Process.Start(pkg.discoverInstalledLocation());\r
547                                 }\r
548 \r
549                                 break;\r
550                         }\r
551                 }\r
552                 \r
553                 \r
554                 void PropertiesToolStripMenuItemClick(object sender, EventArgs e)\r
555                 {\r
556                         foreach (Package pkg in SelectedPackages<Package>()) {\r
557                                 PackageInfoForm form = new PackageInfoForm();\r
558                                 form.SelectedObject = pkg;\r
559                                 form.Text = string.Format("{0}({1})のプロパティ", pkg.Name, pkg.Version);\r
560                                 \r
561                                 form.ShowDialog(this);\r
562 \r
563                                 break;\r
564                         }\r
565                 }\r
566                 \r
567                 void PackageListContextMenuStripOpening(object sender, System.ComponentModel.CancelEventArgs e)\r
568                 {\r
569                         // コンテクストメニューから開かれなかった場合はインストール/アンインストールは表示しない\r
570                         bool parentIsPkgListView = (packageListContextMenuStrip.SourceControl == packageListView);\r
571                         // メニューアイテムのインストール/アンインストールの表示はツールストリップに同じ\r
572                         installToolStripMenuItem.Visible = parentIsPkgListView && installToolStripButton.Visible;\r
573                         uninstallToolStripMenuItem.Visible = parentIsPkgListView && uninstallToolStripButton.Visible;\r
574                         \r
575                         // インストールもアンインストールもできない状態ならば、メニューを表示しない\r
576                         if (! (installToolStripButton.Visible || uninstallToolStripButton.Visible)) {\r
577                                 e.Cancel = true;\r
578                                 return;\r
579                         }\r
580                         \r
581                         // 選択パッケージが1つのとき\r
582                         bool selectionIsOnlyOne = packageListView.SelectedItems.Count == 1;\r
583                         if (selectionIsOnlyOne) {\r
584                                 Package pkg = SelectedPackage<Package>();\r
585                                 bool isInstalledPackage = pkg is InstalledPackage;\r
586                                 \r
587                                 webResourcesToolStripMenuItem.Text = string.Format(\r
588                                         webResourcesToolStripMenuItem.Tag.ToString(),\r
589                                         pkg.Name.Replace("&", "&&")); // pkg.Nameに&が含まれているときはエンコード\r
590                                 webOfficialToolStripMenuItem.Enabled = ! (pkg.Url == null || string.IsNullOrEmpty(pkg.Url.Href));\r
591                                 // webGoogleSearchToolStripMenuItem always active.\r
592                                 \r
593                                 bool installedDirectoryStripMenuItemVisible = isInstalledPackage &&\r
594                                         ( (pkg.Type == InstallerType.ARCHIVE) || Directory.Exists(((InstalledPackage) pkg).discoverInstalledLocation()) );\r
595                                 installedDirectoryStripMenuItem.Visible = installedDirectoryStripMenuItemVisible;\r
596                                 if (installedDirectoryStripMenuItemVisible) {\r
597                                         buildInstalledDirectoryMenuItemStripChildren((InstalledPackage) pkg);\r
598                                 }\r
599                         }\r
600                         \r
601                         packageListContextMenuStripSeparator.Visible = parentIsPkgListView && selectionIsOnlyOne;\r
602                         webResourcesToolStripMenuItem.Visible = selectionIsOnlyOne;\r
603                         if (! selectionIsOnlyOne) installedDirectoryStripMenuItem.Visible = false;\r
604                         propertiesToolStripMenuItem.Visible = selectionIsOnlyOne;\r
605                 }\r
606                 \r
607                 private IEnumerable<Package> getUpdatedPackages(PackageList<InstalledPackage> installedPkgList, PackageList<Package> avaiablePkgList, IComparer<string> verComp)\r
608                 {\r
609                         foreach (InstalledPackage pkg in installedPkgList) {\r
610                                 Package avaiablePkg = avaiablePkgList.GetPackageForName(pkg.Name);\r
611                                 \r
612                                 if (avaiablePkgList != null) {\r
613                                         if (verComp.Compare(pkg.Version, avaiablePkg.Version) < 0 &&\r
614                                             installedPkgList.GetPackageForPackage(pkg.Name, avaiablePkg.Version) == null) {\r
615                                                 \r
616                                                 yield return avaiablePkg;\r
617                                         }\r
618                                 }\r
619                         }\r
620                 }\r
621                 \r
622                 void UpgradeToolStripButtonClick(object sender, EventArgs e)\r
623                 {\r
624                         List<Package> pkgs;\r
625                         VersionComparetor verComp = new VersionComparetor();\r
626                         PackageList<Package> avaiablePackageList = pkgListsMan.AvailablePkgList;\r
627                         \r
628                         pkgs = NaGet.Utils.MeargeList(\r
629                                 getUpdatedPackages(pkgListsMan.InstalledPkgList, avaiablePackageList, verComp),\r
630                                 getUpdatedPackages(pkgListsMan.SystemInstalledPkgList, avaiablePackageList, verComp)\r
631                         );\r
632                         \r
633                         if (pkgs.Count <= 0) {\r
634                                 MessageBox.Show(this, "更新されたソフトはありません", "ソフトの更新");\r
635                                 return;\r
636                         }\r
637                         \r
638                         InstallationConfirmForm confirm = new InstallationConfirmForm();\r
639                         confirm.PkgListsManager = pkgListsMan;\r
640                         confirm.Installations = Installation.ConvertInstallations(pkgs.ToArray());\r
641                         DialogResult result = confirm.ShowDialog(this);\r
642                         \r
643                         if (result == DialogResult.OK) {\r
644                                 Installation[] insts = confirm.CheckedInstallations;\r
645                                 \r
646                                 if (confirm.UseRunas) {\r
647                                         installRunasActionInvoke(insts);\r
648                                 } else {\r
649                                         installActionInvoke(insts);\r
650                                 }\r
651                                 \r
652                                 UpdatePackageList();\r
653                         }\r
654                 }\r
655                 \r
656                 public void UpdatePackageList()\r
657                 {\r
658                         pkgListsMan.LoadPackageLists();\r
659                         this.PackageListViewUpdate();\r
660                 }\r
661                 \r
662                 /// <summary>\r
663                 /// インストールするよう選択されたパッケージの配列\r
664                 /// </summary>\r
665                 public IEnumerable<TPackage> SelectedPackages<TPackage>() where TPackage : Package\r
666                 {\r
667                         List<TPackage> pkgs = new List<TPackage>();\r
668                         foreach (ListViewItem item in packageListView.SelectedItems) {\r
669                                 pkgs.Add((TPackage) item.Tag);\r
670                         }\r
671                         \r
672                         return pkgs;\r
673                 }\r
674                 \r
675                 /// <summary>\r
676                 /// インストールするよう選択されたパッケージをひとつ返す。\r
677                 /// </summary>\r
678                 /// <returns>選択されたパッケージ(選択されていないときはnull)</returns>\r
679                 public TPackage SelectedPackage<TPackage>() where TPackage : Package\r
680                 {\r
681                         foreach (ListViewItem item in packageListView.SelectedItems) {\r
682                                 return (TPackage) item.Tag;\r
683                         }\r
684                         return null;\r
685                 }\r
686                 \r
687                 /// <summary>\r
688                 /// 自ウィンドウの有効無効(Enabled)を(必要あればInvokeして)実行する \r
689                 /// </summary>\r
690                 /// <param name="enabled">有効か否か。Enabledの値に入れられる</param>\r
691                 private void setWindowEnabled(bool enabled)\r
692                 {\r
693                         MethodInvoker process = (MethodInvoker) delegate() {\r
694                                 this.Enabled = enabled;\r
695                         };\r
696                         \r
697                         if (InvokeRequired) {\r
698                                 Invoke(process);\r
699                         } else {\r
700                                 process.Invoke();\r
701                         }\r
702                 }\r
703         }\r
704 \r
705 }\r