OSDN Git Service

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