OSDN Git Service

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