OSDN Git Service

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