OSDN Git Service

na-get-lib,Fujitsu Scand Allがインストールされている環境下で例外が発生する問題を修正(レジストリのキーのタイプが想定しているのと異なるとき例外...
[applistation/AppliStation.git] / AppliStation / InstallationConfirmForm.cs
index 8414b9b..f6afea9 100644 (file)
@@ -1,4 +1,5 @@
 using System;\r
+using System.Collections.Generic;\r
 using System.Drawing;\r
 using System.Windows.Forms;\r
 using NaGet.Packages;\r
@@ -11,22 +12,37 @@ namespace AppliStation
        /// </summary>\r
        public partial class InstallationConfirmForm : Form\r
        {\r
-               private Installation[] installations;\r
+               private Installation[] selectedInstallations = null;\r
+               \r
+               private Installation[] updateInstallations = null;\r
+               \r
+               private Installation[] requiredInstallations = null;\r
                \r
                private PackageListsManager pkgListMan;\r
                \r
-               /// <summary>\r
-               /// パッケージインストール処理の集合\r
-               /// </summary>\r
-               public Installation[] Installations {\r
-                       get { return installations; }\r
+               private ushort instslistviewitemcheckedguardcounter = 0;\r
+\r
+               public IEnumerable<Installation> Installations {\r
+                       get {\r
+                               return NaGet.Utils.MergeEnumerable<Installation>(requiredInstallations, selectedInstallations);\r
+                       }\r
                        set {\r
-                               installations = value;\r
+                               List<Installation> selectedInstList = new List<Installation>();\r
+                               List<Installation> updateInstList = new List<Installation>();\r
                                \r
-                               // 基本はサイレントインストールを行うこととする\r
-                               foreach (Installation inst in installations) {\r
-                                       inst.Silent = inst.IsSupportsSilent;\r
+                               requiredInstallations = null;\r
+                               foreach (Installation inst in value) {\r
+                                       Package instPkg = pkgListMan.InstalledPkgList.GetPackageForName(inst.InstalledPackage.Name) ??\r
+                                               pkgListMan.SystemInstalledPkgList.GetPackageForName(inst.InstalledPackage.Name);\r
+                                       \r
+                                       if (instPkg != null) {\r
+                                               updateInstList.Add(inst);\r
+                                       } else {\r
+                                               selectedInstList.Add(inst);\r
+                                       }\r
                                }\r
+                               selectedInstallations = selectedInstList.ToArray();\r
+                               updateInstallations = updateInstList.ToArray();\r
                                \r
                                updateInstsListView();\r
                        }\r
@@ -59,48 +75,90 @@ namespace AppliStation
                        AppliStation.Util.NativeMethods.ListView_SetDoubleBuffer(instsListView, true);\r
                }\r
                \r
+               #region インストールリスト表示処理部\r
+               \r
+               #region インストールリストの反映\r
+               \r
                /// <summary>\r
                /// インストールリストを更新したなどで、リストの表示を更新する\r
                /// </summary>\r
                private void updateInstsListView()\r
                {\r
-                       instsListView.Items.Clear();\r
+                       instsListView.BeginUpdate();\r
                        \r
-                       if (installations != null) {\r
-                               foreach (Installation inst in installations) {\r
-                                       Package pkg = inst.InstalledPackage;\r
-                                       \r
-                                       string[] itemData = new string[instsListView.Columns.Count];\r
-                                       itemData[nameColumnHeader.Index] = pkg.Name;\r
-                                       \r
-                                       Package curPkg = null;\r
-                                       if (pkgListMan != null) {       \r
-                                               curPkg = pkgListMan.InstalledPkgList.GetPackageForName(pkg.Name) ??\r
-                                                       pkgListMan.SystemInstalledPkgList.GetPackageForName(pkg.Name);\r
-                                       }\r
-                                       itemData[versionColumnHeader.Index]        = pkg.Version;\r
-                                       itemData[currentVersionColumnHeader.Index] = (curPkg != null)? curPkg.Version : "-";\r
-                                       // itemData[silentInstColumnHeader.Index] の設定は instViewUpdateSilentInstallView で\r
-                                       \r
-                                       ListViewItem item = new ListViewItem(itemData);\r
-                                       item.Tag = inst;\r
-                                       item.ToolTipText = pkg.Summary;\r
-                                       item.Checked = true;\r
-                                       item.Group = instsListView.Groups["install"];\r
-                                       instViewUpdateSilentInstallView(item);\r
-                                       \r
-                                       instsListView.Items.Add(item);\r
+                       if (instsListView.Items.Count > 0) {\r
+                               instsListView.Items.Clear();\r
+                       }\r
+                       \r
+                       addInstsListItemPerGroup(requiredInstallations, instsListView.Groups["requires"], true);\r
+                       addInstsListItemPerGroup(selectedInstallations, instsListView.Groups["install"], false);\r
+                       addInstsListItemPerGroup(updateInstallations, instsListView.Groups["update"], false);\r
+                       \r
+                       updateCheckBoxStatuses();\r
+                       updateSilentInstallAsPossibleCheckBox();\r
+                       \r
+                       instsListView.EndUpdate();\r
+               }\r
+\r
+               /// <summary>\r
+               /// 指定したグループのリストの表示を更新する。\r
+               /// </summary>\r
+               /// <param name="insts">インストールリスト</param>\r
+               /// <param name="group">対象のグループ</param>\r
+               /// <param name="firstAppend">先頭に追加するか</param>\r
+               private void addInstsListItemPerGroup(IEnumerable<Installation> insts, ListViewGroup group, bool firstAppend)\r
+               {\r
+                       // まず所属グループのアイテムをすべて削除する\r
+                       if (insts == null) return;\r
+                       \r
+                       instsListView.BeginUpdate();\r
+                       \r
+                       List<ListViewItem> itemsToAdd = new List<ListViewItem>();\r
+                       foreach (Installation inst in insts) {\r
+                               Package pkg = inst.InstalledPackage;\r
+                               \r
+                               string[] itemData = new string[instsListView.Columns.Count];\r
+                               itemData[nameColumnHeader.Index] = pkg.Name;\r
+                               \r
+                               inst.Silent = true; // silent install as possible!\r
+                               \r
+                               Package curPkg = null;\r
+                               if (pkgListMan != null) {\r
+                                       curPkg = pkgListMan.InstalledPkgList.GetPackageForName(pkg.Name) ??\r
+                                               pkgListMan.SystemInstalledPkgList.GetPackageForName(pkg.Name);\r
                                }\r
+                               itemData[versionColumnHeader.Index]        = pkg.Version;\r
+                               itemData[currentVersionColumnHeader.Index] = (curPkg != null)? curPkg.Version : "-";\r
+                               // itemData[silentInstColumnHeader.Index] の設定は instViewUpdateSilentInstallView で\r
+                               itemData[pkgListNameColumnHeader.Index]    = pkg.PackageListName;\r
+                               \r
+                               ListViewItem item = new ListViewItem(itemData);\r
+                               item.Tag = inst;\r
+                               item.ToolTipText = pkg.Summary;\r
+                               item.Checked = true;\r
+                               item.Group = group;\r
+                               instViewUpdateSilentInstallView(item);\r
+                               \r
+                               itemsToAdd.Add(item);\r
                        }\r
                        \r
-                       InstsListViewItemChecked(instsListView, null);\r
-                       instsListView.Refresh();\r
+                       if (firstAppend) {\r
+                               for (int i = 0; i < itemsToAdd.Count; i++) {\r
+                                       instsListView.Items.Insert(i, itemsToAdd[i]);\r
+                               }\r
+                       } else {\r
+                               instsListView.Items.AddRange(itemsToAdd.ToArray());\r
+                       }\r
+                       \r
+                       instsListView.EndUpdate();\r
                }\r
+\r
+               #endregion\r
                \r
                /// <summary>\r
                /// アイテムのサイレントインストール部分の表示の更新を行う。\r
                /// </summary>\r
-               /// <param name="item">対象のインストーラのリストアイテム</param>\r
+               /// <param name="item">対象ã\81®ã\82¤ã\83³ã\82¹ã\83\88ã\83¼ã\83©ã\83¼ã\81®ã\83ªã\82¹ã\83\88ã\82¢ã\82¤ã\83\86ã\83 </param>\r
                private void instViewUpdateSilentInstallView(ListViewItem item)\r
                {\r
                        Installation inst = (Installation) item.Tag;\r
@@ -154,13 +212,40 @@ namespace AppliStation
                \r
                #endregion\r
                \r
-               void InstsListViewItemChecked(object sender, ItemCheckedEventArgs e)\r
+               #endregion\r
+               \r
+               private void updateCheckBoxStatuses()\r
                {\r
+                       System.Windows.Forms.ListView.ListViewItemCollection items = instsListView.Items;\r
                        System.Windows.Forms.ListView.CheckedListViewItemCollection checkeds = instsListView.CheckedItems;\r
                        \r
-                       okButton.Enabled = checkeds != null && checkeds.Count > 0;\r
+                       instslistviewitemcheckedguardcounter ++;\r
+                       \r
+                       // すべて選択/非選択\r
+                       selectAllCheckBox.CheckState =\r
+                               (checkeds == null || checkeds.Count == 0)? CheckState.Unchecked :\r
+                               (checkeds.Count == items.Count)? CheckState.Checked :\r
+                               CheckState.Indeterminate;\r
+                       \r
+                       // runas情報\r
+                       runasCheckBox.Checked = GetShouldUseRunas();\r
+                       updateUseRunas();\r
+                       \r
+                       // インストール可能か\r
+                       okButton.Enabled = (checkeds != null) && (checkeds.Count > 0);\r
+                       \r
+                       checkUnselectedDependencies();\r
+                       \r
+                       instslistviewitemcheckedguardcounter --;\r
                }\r
-                               \r
+               \r
+               void InstsListViewItemChecked(object sender, ItemCheckedEventArgs e)\r
+               {\r
+                       if (instslistviewitemcheckedguardcounter == 0) {\r
+                               updateCheckBoxStatuses();\r
+                       }\r
+               }\r
+               \r
                void InstsListViewContextMenuStripOpening(object sender, System.ComponentModel.CancelEventArgs e)\r
                {\r
                        System.Windows.Forms.ListView.CheckedIndexCollection  chkIdxes = instsListView.CheckedIndices;\r
@@ -184,37 +269,169 @@ namespace AppliStation
                                                break;\r
                                        }\r
                                }\r
+                       } else {\r
+                               e.Cancel = true;\r
                        }\r
-                       instListToolStripSeparator.Visible = selIdxes.Count > 0;\r
-               \r
-                       \r
-                       selectAllInstsStripMenuItem.Enabled = chkIdxes.Count < instsListView.Items.Count;\r
-                       unselectAllInstsStripMenuItem.Enabled = chkIdxes.Count > 0;\r
                }\r
                \r
                void SilentInstallStripMenuItemClick(object sender, EventArgs e)\r
                {\r
+                       instsListView.BeginUpdate();\r
                        bool silent = ! silentInstallStripMenuItem.Checked;\r
                        foreach (ListViewItem item in instsListView.SelectedItems) {\r
                                ((Installation) item.Tag).Silent = silent;\r
                                instViewUpdateSilentInstallView(item);\r
                        }\r
+                       updateSilentInstallAsPossibleCheckBox();\r
+                       instsListView.EndUpdate();\r
                }\r
-                               \r
-               void SelectAllInstsStripMenuItemClick(object sender, EventArgs e)\r
+               \r
+               void SelectAllCheckBoxCheckedChanged(object sender, EventArgs e)\r
+               {\r
+                       instsListView.BeginUpdate();\r
+                       \r
+                       instslistviewitemcheckedguardcounter ++;\r
+                       \r
+                       if (selectAllCheckBox.CheckState == CheckState.Checked) {\r
+                               foreach (ListViewItem item in instsListView.Items) {\r
+                                       item.Checked = true;\r
+                               }\r
+                       }\r
+                       if (selectAllCheckBox.CheckState == CheckState.Unchecked) {\r
+                               foreach (ListViewItem item in instsListView.Items) {\r
+                                       item.Checked = false;\r
+                               }\r
+                       }\r
+                       \r
+                       instslistviewitemcheckedguardcounter --;\r
+                       \r
+                       updateCheckBoxStatuses();\r
+                       \r
+                       instsListView.EndUpdate();\r
+               }\r
+               \r
+               void updateSilentInstallAsPossibleCheckBox()\r
                {\r
+                       bool isAllSilentAsPossible = true;\r
+                       bool isAllNotSilentAsPossible = true;\r
+                       bool canChangeSilent = false;\r
+                       \r
                        foreach (ListViewItem item in instsListView.Items) {\r
-                               item.Checked = true;\r
+                               Installation inst = item.Tag as Installation;\r
+                               if (inst != null) {\r
+                                       if (inst.Silent) {\r
+                                               if (! inst.SupportsSilentOnly) {\r
+                                                       isAllNotSilentAsPossible = false;\r
+                                                       canChangeSilent = true;\r
+                                               }\r
+                                       } else {\r
+                                               if (inst.IsSupportsSilent) {\r
+                                                       isAllSilentAsPossible = false;\r
+                                                       canChangeSilent = true;\r
+                                               }\r
+                                       }\r
+                               }\r
                        }\r
-                       instsListView.Refresh();\r
+                       \r
+                       silentInstallAsPossibleCheckBox.Enabled = canChangeSilent;\r
+                       silentInstallAsPossibleCheckBox.CheckState =\r
+                               (isAllSilentAsPossible)? CheckState.Checked :\r
+                               (isAllNotSilentAsPossible)? CheckState.Unchecked :\r
+                               CheckState.Indeterminate;\r
                }\r
                \r
-               void UnselectAllInstsStripMenuItemClick(object sender, EventArgs e)\r
+               void SilentInstallAsPossibleCheckBoxCheckedChanged(object sender, EventArgs e)\r
                {\r
-                       foreach (ListViewItem item in instsListView.CheckedItems) {\r
-                               item.Checked = false;\r
+                       instsListView.BeginUpdate();\r
+                       if (silentInstallAsPossibleCheckBox.CheckState == CheckState.Checked) {\r
+                               foreach (ListViewItem item in instsListView.Items) {\r
+                                       Installation inst = item.Tag as Installation;\r
+                                       if (inst != null) {\r
+                                               if (inst.IsSupportsSilent && inst.Silent == false) {\r
+                                                       inst.Silent = true;\r
+                                                       instViewUpdateSilentInstallView(item);\r
+                                               }\r
+                                       }\r
+                               }\r
+                       }\r
+                       if (silentInstallAsPossibleCheckBox.CheckState == CheckState.Unchecked) {\r
+                               foreach (ListViewItem item in instsListView.Items) {\r
+                                       Installation inst = item.Tag as Installation;\r
+                                       if (inst != null) {\r
+                                               if ((!inst.SupportsSilentOnly) && inst.Silent == true) {\r
+                                                       inst.Silent = false;\r
+                                                       instViewUpdateSilentInstallView(item);\r
+                                               }\r
+                                       }\r
+                               }\r
+                       }\r
+                       instsListView.EndUpdate();\r
+               }\r
+\r
+               void InstallationConfirmFormShown(object sender, EventArgs e)\r
+               {\r
+                       if (InvokeRequired) {\r
+                               Invoke(new MethodInvoker(resolveDependecies));\r
+                       } else {\r
+                               resolveDependecies();\r
                        }\r
-                       instsListView.Refresh();\r
+               }\r
+               \r
+               /// <summary>\r
+               /// 依存関係を解決する\r
+               /// </summary>\r
+               private void resolveDependecies()\r
+               {\r
+                       if (requiredInstallations == null) {\r
+                               Installation[] resolved, dependencies;\r
+                               \r
+                               instsListView.BeginUpdate();\r
+                               \r
+                               DependeciesResolver.ResolveInstallations(\r
+                                       selectedInstallations,\r
+                                       pkgListMan,\r
+                                       out resolved,\r
+                                       out dependencies);\r
+                               \r
+                               requiredInstallations = dependencies;\r
+                               \r
+                               addInstsListItemPerGroup(requiredInstallations, instsListView.Groups["requires"], true);\r
+                               \r
+                               updateCheckBoxStatuses();\r
+                               updateSilentInstallAsPossibleCheckBox();\r
+                               instsListView.EndUpdate();\r
+                       }\r
+               }\r
+               \r
+               /// <summary>\r
+               /// 依存関係を確認してGUIに反映させる。\r
+               /// 選択されていないが依存関係上必要なソフトを探し出す。\r
+               /// </summary>\r
+               /// <returns>選択されていないが依存関係上必要なソフトの個数(何もない場合はゼロ)</returns>\r
+               private uint checkUnselectedDependencies()\r
+               {\r
+                       uint retVal = 0;\r
+                       \r
+                       List<Package> instPkgs = new List<Package>();\r
+                       foreach (Installation inst in Installations) {\r
+                               instPkgs.Add(inst.InstalledPackage);\r
+                       }\r
+                       \r
+                       List<Package> pkg = new List<Package>();\r
+                       foreach (Installation inst in DependeciesResolver.CreateRequiresInstallations(CheckedInstallations, pkgListMan, instPkgs)) {\r
+                               pkg.Add(inst.InstalledPackage);\r
+                       }\r
+                       \r
+                       foreach (ListViewItem item in instsListView.Items) {\r
+                               if ((pkg.IndexOf(((Installation) item.Tag).InstalledPackage) >= 0) && !item.Checked) {\r
+                                       item.ForeColor = Color.Red;\r
+                                       retVal++;\r
+                               } else {\r
+                                       item.ForeColor = Color.Empty;\r
+                               }\r
+                               \r
+                       }\r
+                       return retVal;\r
                }\r
                \r
                /// <summary>\r
@@ -222,7 +439,7 @@ namespace AppliStation
                /// </summary>\r
                public Installation[] CheckedInstallations {\r
                        get {\r
-                               System.Collections.Generic.List<Installation> insts = new System.Collections.Generic.List<Installation>();\r
+                               List<Installation> insts = new List<Installation>();\r
                                foreach (ListViewItem item in instsListView.CheckedItems) {\r
                                        insts.Add((Installation) item.Tag);\r
                                }\r
@@ -253,13 +470,13 @@ namespace AppliStation
                {\r
                        if (NaGet.Utils.IsAdministrators()) {\r
                                // 管理者権限で動いている場合は不要\r
-                               return false;   \r
+                               return false;\r
                        } else if (NaGet.Utils.IsUACEnabled()) {\r
                                // UACが適用されている場合は標準では不要とする\r
-                               return false;   \r
+                               return false;\r
                        }\r
                        \r
-                       // ひとつでもPCターゲットなインストーラがあれば必要とする\r
+                       // ã\81²ã\81¨ã\81¤ã\81§ã\82\82PCã\82¿ã\83¼ã\82²ã\83\83ã\83\88ã\81ªã\82¤ã\83³ã\82¹ã\83\88ã\83¼ã\83©ã\83¼ã\81\8cã\81\82ã\82\8cã\81°å¿\85è¦\81ã\81¨ã\81\99ã\82\8b\r
                        foreach (Installation inst in CheckedInstallations) {\r
                                if (inst.TargetPC) return true;\r
                        }\r
@@ -274,15 +491,9 @@ namespace AppliStation
                \r
                private void updateUseRunas()\r
                {\r
-                       if (UseRunas) {\r
-                               System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(InstallationConfirmForm));\r
-                               okButton.Image = ((System.Drawing.Bitmap)(resources.GetObject("okButton.Image")));\r
-                       } else {\r
-                               okButton.Image = null;\r
-                       }\r
+                       AppliStation.Util.NativeMethods.Button_SetElevationRequiredState(okButton, UseRunas);\r
                }\r
                \r
                #endregion\r
-               \r
        }\r
 }\r