OSDN Git Service

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