OSDN Git Service

c909c68eeace6241052e84e471c7eaa3c0f30b03
[applistation/AppliStation.git] / AppliStation / UserPrefForm.cs
1 using System;\r
2 using System.Collections.Generic;\r
3 using System.Drawing;\r
4 using System.Windows.Forms;\r
5 using System.Diagnostics;\r
6 \r
7 using NaGet.Packages;\r
8 \r
9 namespace AppliStation\r
10 {\r
11         /// <summary>\r
12         /// ユーザ設定用フォーム\r
13         /// </summary>\r
14         public partial class UserPrefForm : Form\r
15         {\r
16                 private List<RepositoryInfo> repos;\r
17                 \r
18                 private bool isRepoListChanged;\r
19                 \r
20                 /// <summary>\r
21                 /// コンストラクタ\r
22                 /// </summary>\r
23                 public UserPrefForm()\r
24                 {\r
25                         repos = new List<RepositoryInfo>();\r
26                         \r
27                         InitializeComponent();\r
28                         \r
29                         this.openInternetOptionLinkAdminLabel.Visible = ! NaGet.Utils.IsAdministrators();\r
30                         \r
31                         loadCurrentPref();\r
32                 }\r
33                 \r
34                 /// <summary>\r
35                 /// 現在の設定を読み込む\r
36                 /// </summary>\r
37                 public void loadCurrentPref()\r
38                 {\r
39                         this.RepositoriesListSetting = NaGet.Utils.GetDeserializedObject<RepositoriesList>(NaGet.Env.RepositoriesListFile);\r
40                         \r
41                         NaGet.NaGetLibPref userPref = NaGet.Env.Pref;\r
42                         this.ProxyAddress = userPref.ProxyAddress;\r
43                         this.EnableScanInstallerFile = userPref.EnableScanInstallerFile;\r
44                         this.InstallOnBackground = userPref.InstallOnBackground;\r
45                 }\r
46                 \r
47                 #region レポジトリリスト設定関連\r
48                 \r
49                 /// <summary>\r
50                 /// レポジトリリストの設定を読み書きする\r
51                 /// </summary>\r
52                 public RepositoriesList RepositoriesListSetting {\r
53                         get {\r
54                                 RepositoriesList repoList = new RepositoriesList();\r
55                                 repoList.Repositories = repos.ToArray();\r
56                                 return repoList;\r
57                         }\r
58                         set {\r
59                                 repos.Clear();\r
60                                 repos.AddRange(value.Repositories);\r
61                                 \r
62                                 updateRepos();\r
63                                 \r
64                                 // レポジトリ変更状態をリセットする\r
65                                 isRepoListChanged = false;\r
66                         }\r
67                 }\r
68                 \r
69                 /// <summary>\r
70                 /// ListBoxへ表示するレポジトリ表現文字列を返す\r
71                 /// </summary>\r
72                 /// <param name="repo">対象レポジトリ</param>\r
73                 /// <returns>ListBoxに表示すべき文字列</returns>\r
74                 private string repoListCheckedListBoxRenderer(RepositoryInfo repo) {\r
75                         return string.Format("{0}[{1}]", repo.Name, repo.Url.Href);\r
76                 }\r
77 \r
78                 /// <summary>\r
79                 /// ListBoxの内容を更新(再構築)する。\r
80                 /// </summary>\r
81                 private void updateRepos()\r
82                 {\r
83                         repoListCheckedListBox.Items.Clear();\r
84                         \r
85                         foreach (RepositoryInfo repo in repos) {\r
86                                 string label = repoListCheckedListBoxRenderer(repo);\r
87                                 repoListCheckedListBox.Items.Add(label, repo.Enabled);\r
88                         }\r
89                 }\r
90                 \r
91                 void RepoListCheckedListBoxSelectedIndexChanged(object sender, EventArgs e)\r
92                 {\r
93                         int selectedIndex = repoListCheckedListBox.SelectedIndex;\r
94                         bool selected = (selectedIndex >= 0);\r
95                         \r
96                         removeRepoButton.Enabled        = selected;\r
97                         upRepoButton.Enabled            = selected && ((1 <= selectedIndex) && (selectedIndex < repos.Count));\r
98                         downRepoButton.Enabled          = selected && ((0 <= selectedIndex) && (selectedIndex < (repos.Count-1)));\r
99                         repoUrlLabel.Enabled            = selected;\r
100                         repoUrlTextBox.Enabled          = selected;\r
101                         if (selected) {\r
102                                 repoUrlTextBox.Text = repos[repoListCheckedListBox.SelectedIndex].Url.Href;\r
103                                 repoUrlTextBox.SelectAll();\r
104                         } else {\r
105                                 repoUrlTextBox.Clear();\r
106                         }\r
107                 }\r
108                 \r
109                 void AddRepoButtonClick(object sender, EventArgs e)\r
110                 {\r
111                         RepositoryInfo repo = new RepositoryInfo();\r
112                         repo.Name = string.Format("新しいレポジトリ");\r
113                         repo.Url = new LocationEntry();\r
114                         repo.Enabled = true;\r
115                         \r
116                         repos.Add(repo);\r
117                         \r
118                         updateRepos();\r
119                         repoListCheckedListBox.SelectedIndex = repos.Count - 1;\r
120                         \r
121                         isRepoListChanged = true;\r
122                 }\r
123                 \r
124                 void RepoListCheckedListBoxItemCheck(object sender, ItemCheckEventArgs e)\r
125                 {\r
126                         repos[e.Index].Enabled = (e.NewValue == CheckState.Checked);\r
127                         \r
128                         isRepoListChanged = true;\r
129                 }\r
130                 \r
131                 void RemoveRepoButtonClick(object sender, EventArgs e)\r
132                 {\r
133                         int selectedIndex = repoListCheckedListBox.SelectedIndex;\r
134                         if ((0 <= selectedIndex) && (selectedIndex < repos.Count)) {\r
135                                 string text = string.Format("本当にレポジトリ「{0}」を消去しますか?", repos[selectedIndex].Name);\r
136                                 DialogResult result = MessageBox.Show(text, "レポジトリの削除", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);\r
137                                 if (result == DialogResult.OK) {\r
138                                         repos.RemoveAt(selectedIndex);\r
139                                         repoListCheckedListBox.Items.RemoveAt(selectedIndex);\r
140                                         \r
141                                         isRepoListChanged = true;\r
142                                 }\r
143                         }\r
144                 }\r
145                 \r
146                 void UpRepoButtonClick(object sender, EventArgs e)\r
147                 {\r
148                         int selectedIndex = repoListCheckedListBox.SelectedIndex;\r
149                         if ((1 <= selectedIndex) && (selectedIndex < repos.Count)) {\r
150                                 NaGet.Utils.ListSwap(repos, selectedIndex-1, selectedIndex);\r
151                                 AppliStation.Util.GUIUtils.CheckedListBox_SwapItems(repoListCheckedListBox, selectedIndex-1, selectedIndex);\r
152                                 repoListCheckedListBox.SelectedIndex --;\r
153                                 \r
154                                 isRepoListChanged = true;\r
155                         }\r
156                 }\r
157                 \r
158                 void DownRepoButtonClick(object sender, EventArgs e)\r
159                 {\r
160                         int selectedIndex = repoListCheckedListBox.SelectedIndex;\r
161                         if ((0 <= selectedIndex) && (selectedIndex < (repos.Count-1))) {\r
162                                 NaGet.Utils.ListSwap(repos, selectedIndex, selectedIndex+1);\r
163                                 AppliStation.Util.GUIUtils.CheckedListBox_SwapItems(repoListCheckedListBox, selectedIndex, selectedIndex+1);\r
164                                 repoListCheckedListBox.SelectedIndex ++;\r
165                                 \r
166                                 isRepoListChanged = true;\r
167                         }\r
168                 }\r
169                 \r
170                 void RepoUrlTextBoxLeave(object sender, EventArgs e)\r
171                 {\r
172                         int selectedIndex = repoListCheckedListBox.SelectedIndex;\r
173                         if ((0 <= selectedIndex) && (selectedIndex < repos.Count)) {\r
174                                 if (repoUrlTextBox.Text != repos[selectedIndex].Url.Href) {\r
175                                         repos[selectedIndex].Url = new LocationEntry(repoUrlTextBox.Text);\r
176                                         repoListCheckedListBox.Items[selectedIndex] = repoListCheckedListBoxRenderer(repos[selectedIndex]);\r
177                                         \r
178                                         isRepoListChanged = true;\r
179                                 }\r
180                         }\r
181                 }\r
182                 \r
183                 /// <summary>\r
184                 /// レポジトリリストが編集されたか否かのフラグ\r
185                 /// </summary>\r
186                 /// <remarks>ソフトリストの再読み込みが必要か否かの判断に使われる</remarks>\r
187                 public bool IsRepositoryListSettingChanged {\r
188                         get { return isRepoListChanged; }\r
189                 }\r
190                 \r
191                 /// <summary>\r
192                 /// レポジトリリストの設定を反映する\r
193                 /// </summary>\r
194                 private void commitRepositoryListSetting()\r
195                 {\r
196                         if (isRepoListChanged) {\r
197                                 NaGet.Utils.PutSerializeObject<RepositoriesList>(NaGet.Env.RepositoriesListFile, this.RepositoriesListSetting);\r
198                         }\r
199                 }\r
200                 \r
201                 #endregion\r
202                 \r
203                 #region プロキシサーバ設定関連\r
204 \r
205                 /// <summary>\r
206                 /// プロキシアドレスを設定あるいは取得する\r
207                 /// </summary>\r
208                 public string ProxyAddress {\r
209                         get {\r
210                                 if (proxySameAsIERadioButton.Checked) {\r
211                                         return string.Empty;    \r
212                                 } else if (directConnRadioButton.Checked) {\r
213                                         return "-";\r
214                                 } else {\r
215                                         return proxyURLTextBox.Text;\r
216                                 }\r
217                         }\r
218                         set {\r
219                                 if (string.IsNullOrEmpty(value)) {\r
220                                         proxySameAsIERadioButton.Checked = true;        \r
221                                 } else if ("-" == value) {\r
222                                         directConnRadioButton.Checked = true;\r
223                                 } else {\r
224                                         specifyProxyRadioButton.Checked = true;\r
225                                         proxyURLTextBox.Text = value;\r
226                                 }\r
227                                 \r
228                                 updateProxyURLEnability();\r
229                         }\r
230                 }\r
231                 \r
232                 /// <summary>\r
233                 /// ProxyURLのテキストボックス領域の有効状態を切り替える\r
234                 /// </summary>\r
235                 private void updateProxyURLEnability()\r
236                 {\r
237                         bool isSpecifiedProxy = specifyProxyRadioButton.Checked;\r
238                         \r
239                         proxyURLLabel.Enabled   = isSpecifiedProxy;\r
240                         proxyURLTextBox.Enabled = isSpecifiedProxy;\r
241                 }\r
242                 \r
243                 void ProxyRadioButtonsCheckedChanged(object sender, EventArgs e)\r
244                 {\r
245                         updateProxyURLEnability();\r
246                 }\r
247                 \r
248                 void OpenInternetOptionLinkLabelLinkClicked(object sender, LinkLabelLinkClickedEventArgs e)\r
249                 {\r
250                         string verb = "open";\r
251                         \r
252                         if (sender == openInternetOptionLinkAdminLabel) {\r
253                                 verb = "runas"; \r
254                         }\r
255                         \r
256                         try {\r
257                                 ProcessStartInfo procInfo = new ProcessStartInfo("control.exe");\r
258                                 procInfo.Arguments = "inetcpl.cpl,,4";\r
259                                 procInfo.UseShellExecute = true;\r
260                                 procInfo.Verb = verb;\r
261                                 \r
262                                 Process.Start(procInfo);\r
263                         } catch (System.ComponentModel.Win32Exception ex) {\r
264                                 MessageBox.Show(ex.Message, "インターネットオプション", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
265                         } catch (Exception) {\r
266                                 MessageBox.Show("インターネットオプションが開けませんでした", "インターネットオプション", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
267                         }\r
268                 }\r
269                 \r
270                 /// <summary>\r
271                 /// プロキシ設定を、指定された設定オブジェクトに設定する。\r
272                 /// </summary>\r
273                 /// <param name="pref">設定オブジェクト</param>\r
274                 private void commitProxySetting(NaGet.NaGetLibPref pref)\r
275                 {\r
276                         pref.ProxyAddress = this.ProxyAddress;\r
277                 }\r
278                 \r
279                 #endregion\r
280                 \r
281                 #region インストール設定関連\r
282                 \r
283                 /// <summary>\r
284                 /// インストーラーファイルをウイルススキャンするかを設定あるいは取得する\r
285                 /// </summary>\r
286                 public bool EnableScanInstallerFile {\r
287                         set {   this.installScanInstallerFileCheckbox.Checked = value;  }\r
288                         get {   return this.installScanInstallerFileCheckbox.Checked;   }\r
289                 }\r
290                 \r
291                 /// <summary>\r
292                 /// インストール・アンインストールを優先度を下げて実行するかを設定あるいは取得する\r
293                 /// </summary>\r
294                 public bool InstallOnBackground {\r
295                         set {   this.installOnBackgroundCheckBox.Checked = value;       }\r
296                         get {   return this.installOnBackgroundCheckBox.Checked;        }\r
297                 }\r
298                 \r
299                 /// <summary>\r
300                 /// インストール関連設定を、指定された設定オブジェクトに設定する。\r
301                 /// </summary>\r
302                 /// <param name="pref">設定オブジェクト</param>\r
303                 private void commitInstallSetting(NaGet.NaGetLibPref pref)\r
304                 {\r
305                         pref.EnableScanInstallerFile = this.EnableScanInstallerFile;\r
306                         pref.InstallOnBackground = this.InstallOnBackground;\r
307                 }\r
308                 \r
309                 #endregion\r
310                 \r
311                 /// <summary>\r
312                 /// 指定された設定オブジェクトをファイルとして保存する\r
313                 /// </summary>\r
314                 /// <param name="pref">設定ファイル</param>\r
315                 private void commitNaGetLibPref(NaGet.NaGetLibPref pref)\r
316                 {\r
317                         // ファイルに書き込む\r
318                         string path = NaGet.Env.PrefPath;\r
319                         NaGet.Utils.PutSerializeObject<NaGet.NaGetLibPref>(path, pref);\r
320                         \r
321                         // 設定についてファイルから設定を再読み込みさせる\r
322                         NaGet.Env.LoadPref();\r
323                 }\r
324                 \r
325                 void OkButtonClick(object sender, EventArgs e)\r
326                 {\r
327                         NaGet.NaGetLibPref pref = NaGet.Env.Pref;\r
328                         \r
329                         commitRepositoryListSetting();\r
330                         commitProxySetting(pref);\r
331                         commitInstallSetting(pref);\r
332                         \r
333                         commitNaGetLibPref(pref);\r
334                 }\r
335                 \r
336                 void CancelButtonClick(object sender, EventArgs e)\r
337                 {\r
338                 }\r
339                 \r
340                 void RepoUrlTextBoxValidating(object sender, System.ComponentModel.CancelEventArgs e)\r
341                 {\r
342                         string urlText = repoUrlTextBox.Text;\r
343                         \r
344                         if (string.IsNullOrEmpty(urlText)) {\r
345                                 return; // special case.\r
346                         } if (Uri.IsWellFormedUriString(urlText, UriKind.Absolute) == false) {\r
347                                 repoUrlTextBoxErrorProvider.SetError(repoUrlLabel, "URLの記述が不正です");\r
348                                 e.Cancel = true;\r
349                         } else {\r
350                                 Uri uri = new Uri(urlText);\r
351                                 if ((uri.Scheme != Uri.UriSchemeFile) &&\r
352                                     (uri.Scheme != Uri.UriSchemeFtp) &&\r
353                                     (uri.Scheme != Uri.UriSchemeHttp) &&\r
354                                     (uri.Scheme != Uri.UriSchemeHttps)){\r
355                                         repoUrlTextBoxErrorProvider.SetError(repoUrlLabel, "URLの記述が不正です");\r
356                                         e.Cancel = true;\r
357                                 } else {\r
358                                         repoUrlTextBoxErrorProvider.Clear();\r
359                                 }\r
360                         }\r
361                 }\r
362         }\r
363 }\r