OSDN Git Service

na-get-lib, a4ab378a911e444c1d35e49e4de87bc1ea86755b でのミスを修正。
[applistation/AppliStation.git] / AppliStation / UserPrefForm.cs
index c08a7e3..712222e 100644 (file)
@@ -2,6 +2,7 @@
 using System.Collections.Generic;\r
 using System.Drawing;\r
 using System.Windows.Forms;\r
+using System.Diagnostics;\r
 \r
 using NaGet.Packages;\r
 \r
@@ -14,14 +15,20 @@ namespace AppliStation
        {\r
                private List<RepositoryInfo> repos;\r
                \r
-               private bool isRepositoriesChanged;\r
+               private bool isRepoListChanged;\r
                \r
+               /// <summary>\r
+               /// コンストラクタ\r
+               /// </summary>\r
                public UserPrefForm()\r
                {\r
                        repos = new List<RepositoryInfo>();\r
                        \r
                        InitializeComponent();\r
                        \r
+                       this.openInternetOptionLinkAdminLabel.Visible = ! NaGet.Utils.IsAdministrators();\r
+                       AppliStation.Util.NativeMethods.LinkLabel_SetElevationRequiredState(this.openInternetOptionLinkAdminLabel, true);\r
+                       \r
                        loadCurrentPref();\r
                }\r
                \r
@@ -30,9 +37,25 @@ namespace AppliStation
                /// </summary>\r
                public void loadCurrentPref()\r
                {\r
-                       this.RepositoriesListSetting = NaGet.Utils.GetDeserializedObject<RepositoriesList>(NaGet.Env.RepositoriesListFile);\r
+                       try {\r
+                               this.RepositoriesListSetting = NaGet.Utils.GetDeserializedObject<RepositoriesList>(NaGet.Env.RepositoriesListFile);\r
+                       } catch {\r
+                               this.RepositoriesListSetting = new RepositoriesList();\r
+                       }\r
+                       \r
+                       NaGet.NaGetLibPref userPref = NaGet.Env.Pref;\r
+                       this.ProxyAddress = userPref.ProxyAddress;\r
+                       this.EnableScanInstallerFile = userPref.EnableScanInstallerFile;\r
+                       this.InstallOnBackground = userPref.InstallOnBackground;\r
+                       this.CacheFolder = userPref.CacheFolder;\r
+                       this.ExcludeUpdatePackageNames = userPref.ExcludeUpdatePackageNames;\r
                }\r
                \r
+               #region レポジトリリスト設定関連\r
+               \r
+               /// <summary>\r
+               /// レポジトリリストの設定を読み書きする\r
+               /// </summary>\r
                public RepositoriesList RepositoriesListSetting {\r
                        get {\r
                                RepositoriesList repoList = new RepositoriesList();\r
@@ -42,12 +65,26 @@ namespace AppliStation
                        set {\r
                                repos.Clear();\r
                                repos.AddRange(value.Repositories);\r
-                               isRepositoriesChanged = false;\r
                                \r
                                updateRepos();\r
+                               \r
+                               // レポジトリ変更状態をリセットする\r
+                               isRepoListChanged = false;\r
                        }\r
                }\r
                \r
+               /// <summary>\r
+               /// ListBoxへ表示するレポジトリ表現文字列を返す\r
+               /// </summary>\r
+               /// <param name="repo">対象レポジトリ</param>\r
+               /// <returns>ListBoxに表示すべき文字列</returns>\r
+               private static string repoListCheckedListBoxRenderer(RepositoryInfo repo) {\r
+                       return string.Format("{0}[{1}]", repo.Name, repo.Url.Href);\r
+               }\r
+\r
+               /// <summary>\r
+               /// ListBoxの内容を更新(再構築)する。\r
+               /// </summary>\r
                private void updateRepos()\r
                {\r
                        repoListCheckedListBox.Items.Clear();\r
@@ -58,12 +95,6 @@ namespace AppliStation
                        }\r
                }\r
                \r
-               public string repoListCheckedListBoxRenderer(RepositoryInfo repo)\r
-               {\r
-                       return string.Format("{0}[{1}]", repo.Name, repo.Url.Href);\r
-               }\r
-               \r
-               \r
                void RepoListCheckedListBoxSelectedIndexChanged(object sender, EventArgs e)\r
                {\r
                        int selectedIndex = repoListCheckedListBox.SelectedIndex;\r
@@ -94,14 +125,14 @@ namespace AppliStation
                        updateRepos();\r
                        repoListCheckedListBox.SelectedIndex = repos.Count - 1;\r
                        \r
-                       isRepositoriesChanged = true;\r
+                       isRepoListChanged = true;\r
                }\r
                \r
                void RepoListCheckedListBoxItemCheck(object sender, ItemCheckEventArgs e)\r
                {\r
                        repos[e.Index].Enabled = (e.NewValue == CheckState.Checked);\r
                        \r
-                       isRepositoriesChanged = true;\r
+                       isRepoListChanged = true;\r
                }\r
                \r
                void RemoveRepoButtonClick(object sender, EventArgs e)\r
@@ -114,7 +145,7 @@ namespace AppliStation
                                        repos.RemoveAt(selectedIndex);\r
                                        repoListCheckedListBox.Items.RemoveAt(selectedIndex);\r
                                        \r
-                                       isRepositoriesChanged = true;\r
+                                       isRepoListChanged = true;\r
                                }\r
                        }\r
                }\r
@@ -127,7 +158,7 @@ namespace AppliStation
                                AppliStation.Util.GUIUtils.CheckedListBox_SwapItems(repoListCheckedListBox, selectedIndex-1, selectedIndex);\r
                                repoListCheckedListBox.SelectedIndex --;\r
                                \r
-                               isRepositoriesChanged = true;\r
+                               isRepoListChanged = true;\r
                        }\r
                }\r
                \r
@@ -139,39 +170,340 @@ namespace AppliStation
                                AppliStation.Util.GUIUtils.CheckedListBox_SwapItems(repoListCheckedListBox, selectedIndex, selectedIndex+1);\r
                                repoListCheckedListBox.SelectedIndex ++;\r
                                \r
-                               isRepositoriesChanged = true;\r
+                               isRepoListChanged = true;\r
                        }\r
                }\r
                \r
-               void RepoUrlTextBoxLeave(object sender, EventArgs e)\r
+               void RepoUrlTextBoxValidated(object sender, EventArgs e)\r
                {\r
                        int selectedIndex = repoListCheckedListBox.SelectedIndex;\r
                        if ((0 <= selectedIndex) && (selectedIndex < repos.Count)) {\r
-                               repos[selectedIndex].Url = new LocationEntry(repoUrlTextBox.Text);\r
-                               repoListCheckedListBox.Items[selectedIndex] = repoListCheckedListBoxRenderer(repos[selectedIndex]);\r
-                               \r
-                               isRepositoriesChanged = true;\r
+                               if (repoUrlTextBox.Text != repos[selectedIndex].Url.Href) {\r
+                                       repos[selectedIndex].Url = new LocationEntry(repoUrlTextBox.Text);\r
+                                       repoListCheckedListBox.Items[selectedIndex] = repoListCheckedListBoxRenderer(repos[selectedIndex]);\r
+                                       \r
+                                       isRepoListChanged = true;\r
+                               }\r
                        }\r
                }\r
                \r
+               /// <summary>\r
+               /// レポジトリリストが編集されたか否かのフラグ\r
+               /// </summary>\r
+               /// <remarks>ソフトリストの再読み込みが必要か否かの判断に使われる</remarks>\r
                public bool IsRepositoryListSettingChanged {\r
-                       get { return isRepositoriesChanged; }\r
+                       get { return isRepoListChanged; }\r
                }\r
                \r
-               private void commitRepositorySetting()\r
+               /// <summary>\r
+               /// レポジトリリストの設定を反映する\r
+               /// </summary>\r
+               private void commitRepositoryListSetting()\r
                {\r
-                       if (isRepositoriesChanged) {\r
+                       if (isRepoListChanged) {\r
                                NaGet.Utils.PutSerializeObject<RepositoriesList>(NaGet.Env.RepositoriesListFile, this.RepositoriesListSetting);\r
                        }\r
                }\r
                \r
+               #endregion\r
+               \r
+               #region プロキシサーバ設定関連\r
+\r
+               /// <summary>\r
+               /// プロキシアドレスを設定あるいは取得する\r
+               /// </summary>\r
+               public string ProxyAddress {\r
+                       get {\r
+                               if (proxySameAsIERadioButton.Checked) {\r
+                                       return string.Empty;    \r
+                               } else if (directConnRadioButton.Checked) {\r
+                                       return "-";\r
+                               } else {\r
+                                       return proxyURLTextBox.Text;\r
+                               }\r
+                       }\r
+                       set {\r
+                               if (string.IsNullOrEmpty(value)) {\r
+                                       proxySameAsIERadioButton.Checked = true;        \r
+                               } else if ("-" == value) {\r
+                                       directConnRadioButton.Checked = true;\r
+                               } else {\r
+                                       specifyProxyRadioButton.Checked = true;\r
+                                       proxyURLTextBox.Text = value;\r
+                               }\r
+                               \r
+                               updateProxyURLEnability();\r
+                       }\r
+               }\r
+               \r
+               /// <summary>\r
+               /// ProxyURLのテキストボックス領域の有効状態を切り替える\r
+               /// </summary>\r
+               private void updateProxyURLEnability()\r
+               {\r
+                       bool isSpecifiedProxy = specifyProxyRadioButton.Checked;\r
+                       \r
+                       proxyURLLabel.Enabled   = isSpecifiedProxy;\r
+                       proxyURLTextBox.Enabled = isSpecifiedProxy;\r
+               }\r
+               \r
+               void ProxyRadioButtonsCheckedChanged(object sender, EventArgs e)\r
+               {\r
+                       updateProxyURLEnability();\r
+               }\r
+               \r
+               void OpenInternetOptionLinkLabelLinkClicked(object sender, LinkLabelLinkClickedEventArgs e)\r
+               {\r
+                       string verb = "open";\r
+                       \r
+                       if (sender == openInternetOptionLinkAdminLabel) {\r
+                               verb = "runas"; \r
+                       }\r
+                       \r
+                       try {\r
+                               ProcessStartInfo procInfo = new ProcessStartInfo("control.exe");\r
+                               procInfo.Arguments = "inetcpl.cpl,,4";\r
+                               procInfo.UseShellExecute = true;\r
+                               procInfo.Verb = verb;\r
+                               \r
+                               Process.Start(procInfo);\r
+                       } catch (System.ComponentModel.Win32Exception ex) {\r
+                               MessageBox.Show(ex.Message, "インターネットオプション", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
+                       } catch (Exception) {\r
+                               MessageBox.Show("インターネットオプションが開けませんでした", "インターネットオプション", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
+                       }\r
+               }\r
+               \r
+               /// <summary>\r
+               /// プロキシ設定を、指定された設定オブジェクトに設定する。\r
+               /// </summary>\r
+               /// <param name="pref">設定オブジェクト</param>\r
+               private void commitProxySetting(NaGet.NaGetLibPref pref)\r
+               {\r
+                       pref.ProxyAddress = this.ProxyAddress;\r
+               }\r
+               \r
+               #endregion\r
+               \r
+               #region インストール設定関連\r
+               \r
+               /// <summary>\r
+               /// インストーラーファイルをウイルススキャンするかを設定あるいは取得する\r
+               /// </summary>\r
+               public bool EnableScanInstallerFile {\r
+                       set {   this.installScanInstallerFileCheckbox.Checked = value;  }\r
+                       get {   return this.installScanInstallerFileCheckbox.Checked;   }\r
+               }\r
+               \r
+               /// <summary>\r
+               /// インストール・アンインストールを優先度を下げて実行するかを設定あるいは取得する\r
+               /// </summary>\r
+               public bool InstallOnBackground {\r
+                       set {   this.installOnBackgroundCheckBox.Checked = value;       }\r
+                       get {   return this.installOnBackgroundCheckBox.Checked;        }\r
+               }\r
+               \r
+               public string CacheFolder {\r
+                       set {\r
+                               if (string.IsNullOrEmpty(value)) {\r
+                                       this.cacheFolderCustomCheckBox.Checked = false;\r
+                                       this.cacheFolderTextBox.Text = string.Empty;\r
+                               } else {\r
+                                       this.cacheFolderCustomCheckBox.Checked = true;\r
+                                       this.cacheFolderTextBox.Text = value;\r
+                               }\r
+                       }\r
+                       get {\r
+                               if (this.cacheFolderCustomCheckBox.Checked) {\r
+                                       return this.cacheFolderTextBox.Text;\r
+                               } else {\r
+                                       return null;\r
+                               }\r
+                       }\r
+               }\r
+               \r
+               /// <summary>\r
+               /// インストール関連設定を、指定された設定オブジェクトに設定する。\r
+               /// </summary>\r
+               /// <param name="pref">設定オブジェクト</param>\r
+               private void commitInstallSetting(NaGet.NaGetLibPref pref)\r
+               {\r
+                       pref.EnableScanInstallerFile = this.EnableScanInstallerFile;\r
+                       pref.InstallOnBackground = this.InstallOnBackground;\r
+                       pref.CacheFolder = this.CacheFolder;\r
+               }\r
+               \r
+               #endregion\r
+               \r
+               #region ソフト更新設定関連\r
+               \r
+               \r
+               public string[] ExcludeUpdatePackageNames {\r
+                       set {\r
+                               this.upgradeExcludeTextBox.Lines = value;\r
+                       }\r
+                       get {\r
+                               List<string> list = new List<string>();\r
+                               \r
+                               foreach (string line in this.upgradeExcludeTextBox.Lines) {\r
+                                       if (!string.IsNullOrEmpty(line)) {\r
+                                               list.Add(line);\r
+                                       }\r
+                               }\r
+                               \r
+                               return list.ToArray();\r
+                       }\r
+               }\r
+               \r
+               /// <summary>\r
+               /// ソフト更新関連設定を、指定された設定オブジェクトに設定する。\r
+               /// </summary>\r
+               /// <param name="pref">設定オブジェクト</param>\r
+               private void commitUpgradeSetting(NaGet.NaGetLibPref pref)\r
+               {\r
+                       pref.ExcludeUpdatePackageNames = this.ExcludeUpdatePackageNames;\r
+               }\r
+               \r
+               #endregion\r
+               \r
+               /// <summary>\r
+               /// 指定された設定オブジェクトをファイルとして保存する\r
+               /// </summary>\r
+               /// <param name="pref">設定ファイル</param>\r
+               private static void commitNaGetLibPref(NaGet.NaGetLibPref pref)\r
+               {\r
+                       // ファイルに書き込む\r
+                       string path = NaGet.Env.PrefPath;\r
+                       NaGet.Utils.PutSerializeObject<NaGet.NaGetLibPref>(path, pref);\r
+                       \r
+                       // 設定についてファイルから設定を再読み込みさせる\r
+                       NaGet.Env.LoadPref();\r
+               }\r
+               \r
                void OkButtonClick(object sender, EventArgs e)\r
                {\r
-                       commitRepositorySetting();\r
+                       NaGet.NaGetLibPref pref = NaGet.Env.Pref;\r
+                       \r
+                       commitRepositoryListSetting();\r
+                       commitProxySetting(pref);\r
+                       commitInstallSetting(pref);\r
+                       commitUpgradeSetting(pref);\r
+                       \r
+                       commitNaGetLibPref(pref);\r
                }\r
                \r
                void CancelButtonClick(object sender, EventArgs e)\r
                {\r
                }\r
+               \r
+               void RepoUrlTextBoxValidating(object sender, System.ComponentModel.CancelEventArgs e)\r
+               {\r
+                       string urlText = repoUrlTextBox.Text;\r
+                       \r
+                       if (string.IsNullOrEmpty(urlText)) {\r
+                               return; // special case.\r
+                       } if (Uri.IsWellFormedUriString(urlText, UriKind.Absolute) == false) {\r
+                               errorProvider.SetError(repoUrlLabel, "URLの記述が不正です");\r
+                               e.Cancel = true;\r
+                       } else {\r
+                               Uri uri = new Uri(urlText);\r
+                               if ((uri.Scheme != Uri.UriSchemeFile) &&\r
+                                   (uri.Scheme != Uri.UriSchemeFtp) &&\r
+                                   (uri.Scheme != Uri.UriSchemeHttp) &&\r
+                                   (uri.Scheme != Uri.UriSchemeHttps)){\r
+                                       errorProvider.SetError(repoUrlLabel, "URLの記述が不正です");\r
+                                       e.Cancel = true;\r
+                               } else {\r
+                                       errorProvider.Clear();\r
+                               }\r
+                       }\r
+               }\r
+               \r
+               \r
+               void CacheFolderCustomCheckBoxCheckedChanged(object sender, EventArgs e)\r
+               {\r
+                       cacheFolderTextBox.Enabled = cacheFolderCustomCheckBox.Checked;\r
+                       cacheFolderBrowseButton.Enabled = cacheFolderCustomCheckBox.Checked;\r
+               }\r
+               \r
+               void CacheFolderBrowseButtonClick(object sender, EventArgs e)\r
+               {\r
+                       string pwd = System.IO.Directory.GetCurrentDirectory();\r
+                       FolderBrowserDialog fd = new FolderBrowserDialog();\r
+                       fd.Description = "キャッシュフォルダーを指定してください。";\r
+                       fd.SelectedPath = cacheFolderTextBox.Text;\r
+                       fd.ShowNewFolderButton = true;\r
+                       \r
+                       if (fd.ShowDialog(this) == DialogResult.OK) {\r
+                               cacheFolderTextBox.Text = fd.SelectedPath;\r
+                       }\r
+                       \r
+                       System.IO.Directory.SetCurrentDirectory(pwd); // ダイアログで変わったカレントディレクトリを元に戻す\r
+               }\r
+               \r
+               void CacheFolderTextBoxValidating(object sender, System.ComponentModel.CancelEventArgs e)\r
+               {\r
+                       string folderPath = cacheFolderTextBox.Text;\r
+                       \r
+                       if (string.IsNullOrEmpty(folderPath)) {\r
+                               errorProvider.Clear();\r
+                               return; // special case\r
+                       } else if (! System.IO.Directory.Exists(folderPath)) {\r
+                               errorProvider.SetError(cacheFolderTextBox, "存在しないフォルダーパスを指定しています。");\r
+                               e.Cancel = true;\r
+                       } else {\r
+                               errorProvider.Clear();\r
+                       }\r
+               }\r
+               \r
+               void CacheFolderOpenLinkLabelLinkClicked(object sender, LinkLabelLinkClickedEventArgs e)\r
+               {\r
+                       string folderPath;\r
+                       \r
+                       if (cacheFolderCustomCheckBox.Checked) {\r
+                               folderPath = cacheFolderTextBox.Text;\r
+                       } else {\r
+                               // デフォルトは AppDataFolderPath/Cache。\r
+                               folderPath = System.IO.Path.Combine(NaGet.Env.AppDataFolderPath, "Cache");\r
+                       }\r
+                       \r
+                       if (System.IO.Directory.Exists(folderPath)) {\r
+                               try {\r
+                                       Process.Start(folderPath);\r
+                               } catch (Exception ex) {\r
+                                       MessageBox.Show(ex.Message, "キャッシュフォルダー", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
+                               }\r
+                       } else {\r
+                               MessageBox.Show(string.Format("フォルダーパス\"{0}\"は存在しませんでした。", folderPath), "キャッシュフォルダー", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
+                       }\r
+               }\r
+               \r
+               void CacheFolderClearLinkLabelLinkClicked(object sender, LinkLabelLinkClickedEventArgs e)\r
+               {\r
+                       string folderPath;\r
+                       \r
+                       if (cacheFolderCustomCheckBox.Checked) {\r
+                               folderPath = cacheFolderTextBox.Text;\r
+                       } else {\r
+                               // デフォルトは AppDataFolderPath/Cache。\r
+                               folderPath = System.IO.Path.Combine(NaGet.Env.AppDataFolderPath, "Cache");\r
+                       }\r
+                       \r
+                       if (System.IO.Directory.Exists(folderPath)) {\r
+                               DialogResult result = MessageBox.Show(string.Format("フォルダーパス\"{0}\"のファイルを削除して構いませんか?", folderPath), "キャッシュフォルダー", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);\r
+                               if (result == DialogResult.OK) {\r
+                                       foreach (string targetDir in System.IO.Directory.GetDirectories(folderPath)) {\r
+                                               try {\r
+                                                       NaGet.Utils.SetAttributeRecursive(targetDir, System.IO.FileAttributes.Normal);\r
+                                                       System.IO.Directory.Delete(targetDir, true);\r
+                                               } catch {\r
+                                                       // 何もせずに静かに先に進む\r
+                                               }\r
+                                       }\r
+                               }\r
+                       } else {\r
+                               MessageBox.Show(string.Format("フォルダーパス\"{0}\"は存在しませんでした。", folderPath), "キャッシュフォルダー", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
+                       }\r
+               }\r
        }\r
 }\r