OSDN Git Service

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