OSDN Git Service

na-get-lib,複数ソフトインストールかつハッシュ検証失敗したとき、ハッシュ検証成功したソフトの状態がおかしくなる不具合を修正
[applistation/AppliStation.git] / AppliStation / PackageListViewForm.cs
index b280c30..a66d9a6 100644 (file)
@@ -6,6 +6,8 @@ using NaGet.Packages.Install;
 using System.IO;\r
 using System.Collections.Generic;\r
 \r
+using AppliStation.PackageInfo;\r
+\r
 namespace AppliStation\r
 {\r
        /// <summary>\r
@@ -106,11 +108,13 @@ namespace AppliStation
                        webResourceToolStripMenuItem.Visible = (pkgCount == 1);\r
                        uninstallToolStripMenuItem.Visible = uninstallBtnEnabled;\r
                        installToolStripMenuItem.Visible = installBtnEnabled;\r
+                       downloadToolStripMenuItem.Visible = (pkgCount > 0);\r
                        \r
                        // detailBoxのメッセージ設定\r
                        switch (pkgCount) {\r
                                case 0:\r
                                        int count = packageListView.Items.Count;\r
+                                       detailBox.Clear();\r
                                        detailBox.Text = (count > 0) ? string.Format("{0}個のソフトがあります。", count)\r
                                                : "該当するソフトがありません。";\r
                                        break;\r
@@ -118,6 +122,7 @@ namespace AppliStation
                                        ShowInfoToDetailBoxFor(packageListView.SelectedPackage);\r
                                        break;\r
                                default: // case 2 and over:\r
+                                       detailBox.Clear();\r
                                        detailBox.Text = (installBtnEnabled)? string.Format("{0}個のソフトが選択されています。", installPkgCount) :\r
                                                (uninstallBtnEnabled)? string.Format("{0}個のインストール済みのソフトが選択されています。", uninstallPkgCount) :\r
                                                string.Format("{0}個のソフトが選択されています。\r\n(うち{1}個はインストール済み、{2}個はインストール可能)", pkgCount, uninstallPkgCount, installPkgCount);\r
@@ -227,7 +232,7 @@ namespace AppliStation
                {\r
                        AppliStation.Util.ExecutionProgressViewer prog = new AppliStation.Util.ExecutionProgressViewer();\r
                        prog.Shown += delegate(object sender2, EventArgs e2) {\r
-                               NaGet.SubCommands.NaGetUpdate tasks = new NaGet.SubCommands.NaGetUpdate(pkgListsMan, downloadPackageListsFlag);\r
+                               NaGet.SubCommands.NaGetUpdate2 tasks = new NaGet.SubCommands.NaGetUpdate2(pkgListsMan, downloadPackageListsFlag);\r
                                prog.SetTaskSet(tasks);\r
                                prog.Refresh();\r
                                prog.StartTaskSet();\r
@@ -250,6 +255,129 @@ namespace AppliStation
                        UpdatePackageList();\r
                }\r
                \r
+               void SoftCollectionFileImportToolStripMenuItemClick(object sender, EventArgs e)\r
+               {\r
+                       string pwd = Directory.GetCurrentDirectory();\r
+                       \r
+                       OpenFileDialog fd = new OpenFileDialog();\r
+                       fd.Filter = "ソフトコレクションファイル (*.txt)|*.txt";\r
+                       fd.DefaultExt = "txt";\r
+                       fd.CheckFileExists = true;\r
+                       fd.CheckPathExists = true;\r
+                       fd.Multiselect = false;\r
+                       fd.ShowDialog();\r
+                       if (fd.FileNames.Length > 0) {\r
+                               string filepath = Path.GetFullPath(fd.FileName);\r
+                               Directory.SetCurrentDirectory(pwd); // ファイルダイアログで変わったカレントディレクトリを戻す\r
+                               \r
+                               IList<Package> pkgs;\r
+                               IList<string> invalid;\r
+                               \r
+                               try {\r
+                                       PackageCollectionFileData collectionData = new PackageCollectionFileData();\r
+                                       collectionData.load(filepath);\r
+                                       collectionData.generatePackages(pkgListsMan, out pkgs, out invalid);\r
+                                       \r
+                                       if (invalid.Count > 0) {\r
+                                               DialogResult result = MessageBox.Show("一部読み込みの失敗したパッケージがありますが続行しますか?", "インポート", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);\r
+                                               if (result != DialogResult.OK) {\r
+                                                       return;\r
+                                               }\r
+                                       }\r
+                               } catch (IOException) {\r
+                                       MessageBox.Show("ファイルの読み込みに失敗しました", "インポート", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
+                                       return;\r
+                               }\r
+                               \r
+                               {\r
+                                       InstallationConfirmForm confirm = new InstallationConfirmForm();\r
+                                       confirm.PkgListsManager = pkgListsMan;\r
+                                       confirm.Installations = Installation.ConvertInstallations( NaGet.Utils.IEnumerable2Array(pkgs) );\r
+                                       confirm.UseRunas = confirm.GetShouldUseRunas();\r
+                                       DialogResult result = confirm.ShowDialog(this);\r
+                                       \r
+                                       if (result == DialogResult.OK) {\r
+                                               Installation[] insts = confirm.CheckedInstallations;\r
+                                               \r
+                                               if (confirm.UseRunas) {\r
+                                                       installRunasActionInvoke(insts);\r
+                                               } else {\r
+                                                       installActionInvoke(insts);\r
+                                               }\r
+                                               \r
+                                               UpdatePackageList();\r
+                                       }\r
+                               }\r
+                       } else {\r
+                               Directory.SetCurrentDirectory(pwd); // ファイルダイアログで変わったカレントディレクトリを戻す\r
+                       }\r
+               }\r
+               \r
+               void SoftCollectionFileExportToolStripMenuItemClick(object sender, EventArgs e)\r
+               {\r
+                       string pwd = Directory.GetCurrentDirectory();\r
+                       string[] softtargets = new string[]{\r
+                               "PCにインストールされたソフト",\r
+                               "AppliStation内でインストールされたソフト",\r
+                               "インストールされたソフトすべて;PCとAppliStationにインストールされたソフトの両方"\r
+                       };\r
+                       int softtargetid = softtargets.Length - 1;\r
+                       \r
+                       {\r
+                               AppliStation.Util.OptionDialog optdialog = AppliStation.Util.OptionDialog.createCommandSelectionDialog(\r
+                                       "ソフトコレクションファイルに出力するソフトの種類を選択してください。", "エクスポート", "エクスポートするソフト",\r
+                                       System.Drawing.SystemIcons.Question,\r
+                                       softtargets, softtargets.Length-1);\r
+                               if (optdialog.ShowDialog(this) != DialogResult.OK) {\r
+                                       return; // canceled\r
+                               } else if (optdialog.UserInputValue != null) {\r
+                                       softtargetid = (int) optdialog.UserInputValue;\r
+                               }\r
+                       }\r
+                       \r
+                       SaveFileDialog fd = new SaveFileDialog();\r
+                       fd.Filter = "ソフトコレクションファイル (*.txt)|*.txt";\r
+                       fd.DefaultExt = "txt";\r
+                       fd.CheckPathExists = true;\r
+                       fd.OverwritePrompt = true;\r
+                       fd.ShowDialog();\r
+                       if (fd.FileNames.Length > 0) {\r
+                               string filepath = Path.GetFullPath(fd.FileName);\r
+                               Directory.SetCurrentDirectory(pwd); // ファイルダイアログで変わったカレントディレクトリを戻す\r
+                               \r
+                               PackageCollectionFileData collectionData = new PackageCollectionFileData();\r
+                               \r
+                               switch (softtargetid) {\r
+                                       case 0: // PCにインストール\r
+                                               collectionData.loadPackages(pkgListsMan.SystemInstalledPkgList.GetEnumerator());\r
+                                               break;\r
+                                       case 1: // AppliStation内にインストール\r
+                                               collectionData.loadPackages(pkgListsMan.InstalledPkgList.GetEnumerator());\r
+                                               break;\r
+                                       case 2: // すべて\r
+                                       default:\r
+                                               collectionData.loadPackages(pkgListsMan.GetAllInstalledPackages());\r
+                                               break;\r
+                               }\r
+                               \r
+                               try {\r
+                                       collectionData.saveAs(fd.FileName);\r
+                               } catch (UnauthorizedAccessException) {\r
+                                       if ((File.GetAttributes(fd.FileName) & FileAttributes.ReadOnly) != 0) {\r
+                                               MessageBox.Show("読み取り専用属性が設定されています。\n別のファイルを指定してください。", "エクスポート", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
+                                       } else {\r
+                                               MessageBox.Show("ファイルへの書き込みが許可されていません。\n別のファイルを指定してください。", "エクスポート", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
+                                       }\r
+                                       return;\r
+                               } catch (IOException) {\r
+                                       MessageBox.Show("ファイルの書き込みに失敗しました", "エクスポート", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
+                                       return;\r
+                               }\r
+                       } else {\r
+                               Directory.SetCurrentDirectory(pwd); // ファイルダイアログで変わったカレントディレクトリを戻す\r
+                       }\r
+               }\r
+               \r
                void OptionToolStripMenuItemClick(object sender, EventArgs e)\r
                {\r
                        UserPrefForm userPrefForm = new UserPrefForm();\r
@@ -288,7 +416,7 @@ namespace AppliStation
                {\r
                        AppliStation.Util.ExecutionProgressViewer prog = new AppliStation.Util.ExecutionProgressViewer();\r
                        prog.Shown += delegate(object sender2, EventArgs e2) {\r
-                               NaGet.SubCommands.NaGetInstall tasks = new NaGet.SubCommands.NaGetInstall(pkgListsMan, insts);\r
+                               NaGet.SubCommands.NaGetInstall2 tasks = new NaGet.SubCommands.NaGetInstall2(pkgListsMan, insts);\r
                                prog.SetTaskSet(tasks);\r
                                prog.Refresh();\r
                                prog.StartTaskSet();\r
@@ -358,7 +486,7 @@ namespace AppliStation
                {\r
                        AppliStation.Util.ExecutionProgressViewer prog = new AppliStation.Util.ExecutionProgressViewer();\r
                        prog.Shown += delegate(object sender2, EventArgs e2) {\r
-                               NaGet.SubCommands.NaGetUninstall tasks = new NaGet.SubCommands.NaGetUninstall(pkgListsMan, pkgs);\r
+                               NaGet.SubCommands.NaGetUninstall2 tasks = new NaGet.SubCommands.NaGetUninstall2(pkgListsMan, pkgs);\r
                                prog.SetTaskSet(tasks);\r
                                prog.Refresh();\r
                                prog.StartTaskSet();\r
@@ -424,6 +552,27 @@ namespace AppliStation
                        }\r
                }\r
                \r
+               internal void downloadActionInvoke(Installation[] pkgs)\r
+               {\r
+                       AppliStation.Util.ExecutionProgressViewer prog = new AppliStation.Util.ExecutionProgressViewer();\r
+                       prog.Shown += delegate(object sender2, EventArgs e2) {\r
+                               NaGet.SubCommands.NaGetDownloadToCache2 tasks = new NaGet.SubCommands.NaGetDownloadToCache2(pkgListsMan, pkgs);\r
+                               prog.SetTaskSet(tasks);\r
+                               prog.Refresh();\r
+                               prog.StartTaskSet();\r
+                       };\r
+                       prog.Text = string.Format("キャッシュへのダウンロード");\r
+                       prog.ShowDialog(this);\r
+               }\r
+               \r
+               void DownloadToolStripMenuItemClick(object sender, EventArgs e)\r
+               {\r
+                       Installation[] insts = Installation.ConvertInstallations( NaGet.Utils.IEnumerable2Array(packageListView.SelectedPackages) );\r
+                       \r
+                       downloadActionInvoke(insts);\r
+                       \r
+                       UpdatePackageList();\r
+               }\r
                \r
                void WebOfficialMenuItemClick(object sender, EventArgs e)\r
                {\r
@@ -462,8 +611,8 @@ namespace AppliStation
                        if (pkg != null) {\r
                                if (pkg.Type == InstallerType.ARCHIVE || pkg.Type == InstallerType.ITSELF) {\r
                                        System.Diagnostics.Process.Start(Path.Combine(NaGet.Env.ArchiveProgramFiles, pkg.Name));\r
-                               } else if (Directory.Exists(pkg.discoverInstalledLocation())) {\r
-                                       System.Diagnostics.Process.Start(pkg.discoverInstalledLocation());\r
+                               } else if (Directory.Exists(pkg.DiscoverInstalledLocation())) {\r
+                                       System.Diagnostics.Process.Start(pkg.DiscoverInstalledLocation());\r
                                }\r
                        }\r
                }\r
@@ -500,11 +649,11 @@ namespace AppliStation
                                        bool launcherMenuItemVisible = \r
                                                (pkg.Type == InstallerType.ARCHIVE) ||\r
                                                (pkg.Type == InstallerType.ITSELF) ||\r
-                                               Directory.Exists(iPkg.discoverInstalledLocation());\r
+                                               Directory.Exists(iPkg.DiscoverInstalledLocation());\r
                                        \r
                                        launcherMenuItem.Visible = launcherMenuItemVisible;\r
                                        if (launcherMenuItemVisible) {\r
-                                               launcherMenuItem.BaseFolderPath = iPkg.discoverInstalledLocation();\r
+                                               launcherMenuItem.BaseFolderPath = iPkg.DiscoverInstalledLocation();\r
                                        }\r
                                } else {\r
                                        launcherMenuItem.Visible = false;\r
@@ -519,13 +668,25 @@ namespace AppliStation
                        bool selectionIsOnlyOne = packageListView.SelectedItems.Count == 1;\r
                        bool hasSelection = packageListView.SelectedItems.Count > 0;\r
                        \r
+                       updateSelectedPackages();\r
+                       \r
                        // インストール先のフォルダの設定\r
                        updateLauncherMenuItem(installedDirectoryToolStripMenuItem);\r
                        \r
+                       if (packageListView.View == View.Details) {\r
+                               // ヘッダ部がクリックされたとき、パッケージが選択されていないものとして扱って処理をする。\r
+                               int headerHeight = AppliStation.Util.NativeMethods.ColumnHeader_GetSize(packageListView).Height;\r
+                               if (packageListView.PointToClient(packageListContextMenuStrip.Location).Y < headerHeight) {\r
+                                       selectionIsOnlyOne = hasSelection = false;\r
+                                       installToolStripMenuItem.Visible = false;\r
+                                       uninstallToolStripMenuItem.Visible = false;\r
+                                       installedDirectoryToolStripMenuItem.Visible = false;\r
+                               }\r
+                       }\r
+                       \r
                        packageListContextMenuStripSeparator.Visible = selectionIsOnlyOne;\r
                        webResourceToolStripMenuItem.Visible = selectionIsOnlyOne;\r
                        propertiesToolStripMenuItem.Visible = hasSelection;\r
-                       viewStyleToolStripMenuItem.Visible = ! hasSelection;\r
                        columnToolStripMenuItem.Visible = (! hasSelection) && (packageListView.View == View.Details);\r
                }\r
                \r
@@ -544,34 +705,22 @@ namespace AppliStation
                void WebResourceCommonContextMenuStripOpening(object sender, System.ComponentModel.CancelEventArgs e)\r
                {\r
                        Package pkg = packageListView.SelectedPackage;\r
-                       webOfficialMenuItem.Enabled = (pkg != null && pkg.Url != null && !string.IsNullOrEmpty(pkg.Url.Href));\r
-                       // webGoogleSearchMenuItem always active.\r
-               }\r
-               \r
-               private IEnumerable<Package> getUpdatedPackages(PackageList<InstalledPackage> installedPkgList, PackageList<Package> avaiablePkgList, IComparer<string> verComp)\r
-               {\r
-                       foreach (InstalledPackage pkg in installedPkgList) {\r
-                               Package avaiablePkg = avaiablePkgList.GetPackageForName(pkg.Name);\r
-                               \r
-                               if (avaiablePkg != null) {\r
-                                       if (verComp.Compare(pkg.Version, avaiablePkg.Version) < 0 &&\r
-                                           installedPkgList.GetPackageForPackage(pkg.Name, avaiablePkg.Version) == null) {\r
-                                               \r
-                                               yield return avaiablePkg;\r
-                                       }\r
-                               }\r
+                       \r
+                       if (pkg != null && pkg.Url != null && !string.IsNullOrEmpty(pkg.Url.Href)) {\r
+                               webOfficialMenuItem.Enabled = true;\r
+                               webOfficialMenuItem.ToolTipText = pkg.Url.Href;\r
+                       } else {\r
+                               webOfficialMenuItem.Enabled = false;\r
+                               webOfficialMenuItem.ToolTipText = null;\r
                        }\r
+                       \r
+                       // webGoogleSearchMenuItem always active.\r
                }\r
                \r
                void UpgradeToolStripButtonClick(object sender, EventArgs e)\r
                {\r
-                       List<Package> pkgs;\r
-                       VersionComparetor verComp = new VersionComparetor();\r
-                       PackageList<Package> avaiablePackageList = pkgListsMan.AvailablePkgList;\r
-                       \r
-                       pkgs = NaGet.Utils.MergeList(\r
-                               getUpdatedPackages(pkgListsMan.InstalledPkgList, avaiablePackageList, verComp),\r
-                               getUpdatedPackages(pkgListsMan.SystemInstalledPkgList, avaiablePackageList, verComp)\r
+                       List<Package> pkgs = new List<Package>(\r
+                               UpgradeFinder.GetUpgradePackages(pkgListsMan)\r
                        );\r
                        \r
                        if (pkgs.Count <= 0) {\r
@@ -632,24 +781,10 @@ namespace AppliStation
                        }\r
                }\r
                \r
-               void ViewStyleCommonToolStripMenuItemClick(object sender, EventArgs e)\r
-               {\r
-                       viewStyleDetailToolStripMenuItem.Checked = (sender == viewStyleDetailToolStripMenuItem);\r
-                       viewStyleListToolStripMenuItem.Checked = (sender == viewStyleListToolStripMenuItem);\r
-                       viewStyleTileToolStripMenuItem.Checked = (sender == viewStyleTileToolStripMenuItem);\r
-                       \r
-                       if (sender == viewStyleDetailToolStripMenuItem) {\r
-                               packageListView.View = View.Details;\r
-                       } else if (sender == viewStyleListToolStripMenuItem) {\r
-                               packageListView.View = View.List;\r
-                       } else if (sender == viewStyleTileToolStripMenuItem) {\r
-                               packageListView.View = View.Tile;\r
-                       }\r
-               }\r
-               \r
                void ColumnCommonToolStripMenuItemClick(object sender, EventArgs e)\r
                {\r
                        packageListView.BeginUpdate();\r
+                       ColumnHeader sortcolumn = packageListView.SortColumn;\r
                        \r
                        // 列の追加と削除\r
                        foreach (ToolStripItem item in columnToolStripMenuItem.DropDownItems) {\r
@@ -660,6 +795,9 @@ namespace AppliStation
                                        foreach (ColumnHeader header in packageListView.Columns) {\r
                                                if (header.Tag == menu.Tag) {\r
                                                        exists = true;\r
+                                                       if (sortcolumn == header) {\r
+                                                               packageListView.SortColumn = sortcolumn = null;\r
+                                                       }\r
                                                        if (menu.Checked == false) {\r
                                                                packageListView.Columns.Remove(header);\r
                                                        }\r
@@ -677,6 +815,8 @@ namespace AppliStation
                                }\r
                        }\r
                        \r
+                       AppliStation.Util.NativeMethods.ColumnHeader_SetSortState(packageListView, (sortcolumn != null)? sortcolumn.Index : -1, SortOrder.None);\r
+                       \r
                        packageListView.UpdateItems();\r
                        \r
                        packageListView.EndUpdate();\r