OSDN Git Service

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