OSDN Git Service

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