OSDN Git Service

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