OSDN Git Service

AppliStation-GUI,プロキシ設定GUIの改善
[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                         loadCurrentPref();\r
30                 }\r
31                 \r
32                 /// <summary>\r
33                 /// 現在の設定を読み込む\r
34                 /// </summary>\r
35                 public void loadCurrentPref()\r
36                 {\r
37                         this.RepositoriesListSetting = NaGet.Utils.GetDeserializedObject<RepositoriesList>(NaGet.Env.RepositoriesListFile);\r
38                         \r
39                         NaGet.NaGetLibPref userPref = NaGet.Env.Pref;\r
40                         this.ProxyAddress = userPref.ProxyAddress;\r
41                 }\r
42                 \r
43                 #region レポジトリリスト設定関連\r
44                 \r
45                 /// <summary>\r
46                 /// レポジトリリストの設定を読み書きする\r
47                 /// </summary>\r
48                 public RepositoriesList RepositoriesListSetting {\r
49                         get {\r
50                                 RepositoriesList repoList = new RepositoriesList();\r
51                                 repoList.Repositories = repos.ToArray();\r
52                                 return repoList;\r
53                         }\r
54                         set {\r
55                                 repos.Clear();\r
56                                 repos.AddRange(value.Repositories);\r
57                                 \r
58                                 updateRepos();\r
59                                 \r
60                                 // レポジトリ変更状態をリセットする\r
61                                 isRepoListChanged = false;\r
62                         }\r
63                 }\r
64                 \r
65                 /// <summary>\r
66                 /// ListBoxへ表示するレポジトリ表現文字列を返す\r
67                 /// </summary>\r
68                 /// <param name="repo">対象レポジトリ</param>\r
69                 /// <returns>ListBoxに表示すべき文字列</returns>\r
70                 private string repoListCheckedListBoxRenderer(RepositoryInfo repo) {\r
71                         return string.Format("{0}[{1}]", repo.Name, repo.Url.Href);\r
72                 }\r
73 \r
74                 /// <summary>\r
75                 /// ListBoxの内容を更新(再構築)する。\r
76                 /// </summary>\r
77                 private void updateRepos()\r
78                 {\r
79                         repoListCheckedListBox.Items.Clear();\r
80                         \r
81                         foreach (RepositoryInfo repo in repos) {\r
82                                 string label = repoListCheckedListBoxRenderer(repo);\r
83                                 repoListCheckedListBox.Items.Add(label, repo.Enabled);\r
84                         }\r
85                 }\r
86                 \r
87                 void RepoListCheckedListBoxSelectedIndexChanged(object sender, EventArgs e)\r
88                 {\r
89                         int selectedIndex = repoListCheckedListBox.SelectedIndex;\r
90                         bool selected = (selectedIndex >= 0);\r
91                         \r
92                         removeRepoButton.Enabled        = selected;\r
93                         upRepoButton.Enabled            = selected && ((1 <= selectedIndex) && (selectedIndex < repos.Count));\r
94                         downRepoButton.Enabled          = selected && ((0 <= selectedIndex) && (selectedIndex < (repos.Count-1)));\r
95                         repoUrlLabel.Enabled            = selected;\r
96                         repoUrlTextBox.Enabled          = selected;\r
97                         if (selected) {\r
98                                 repoUrlTextBox.Text = repos[repoListCheckedListBox.SelectedIndex].Url.Href;\r
99                                 repoUrlTextBox.SelectAll();\r
100                         } else {\r
101                                 repoUrlTextBox.Clear();\r
102                         }\r
103                 }\r
104                 \r
105                 void AddRepoButtonClick(object sender, EventArgs e)\r
106                 {\r
107                         RepositoryInfo repo = new RepositoryInfo();\r
108                         repo.Name = string.Format("新しいレポジトリ");\r
109                         repo.Url = new LocationEntry();\r
110                         repo.Enabled = true;\r
111                         \r
112                         repos.Add(repo);\r
113                         \r
114                         updateRepos();\r
115                         repoListCheckedListBox.SelectedIndex = repos.Count - 1;\r
116                         \r
117                         isRepoListChanged = true;\r
118                 }\r
119                 \r
120                 void RepoListCheckedListBoxItemCheck(object sender, ItemCheckEventArgs e)\r
121                 {\r
122                         repos[e.Index].Enabled = (e.NewValue == CheckState.Checked);\r
123                         \r
124                         isRepoListChanged = true;\r
125                 }\r
126                 \r
127                 void RemoveRepoButtonClick(object sender, EventArgs e)\r
128                 {\r
129                         int selectedIndex = repoListCheckedListBox.SelectedIndex;\r
130                         if ((0 <= selectedIndex) && (selectedIndex < repos.Count)) {\r
131                                 string text = string.Format("本当にレポジトリ「{0}」を消去しますか?", repos[selectedIndex].Name);\r
132                                 DialogResult result = MessageBox.Show(text, "レポジトリの削除", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);\r
133                                 if (result == DialogResult.OK) {\r
134                                         repos.RemoveAt(selectedIndex);\r
135                                         repoListCheckedListBox.Items.RemoveAt(selectedIndex);\r
136                                         \r
137                                         isRepoListChanged = true;\r
138                                 }\r
139                         }\r
140                 }\r
141                 \r
142                 void UpRepoButtonClick(object sender, EventArgs e)\r
143                 {\r
144                         int selectedIndex = repoListCheckedListBox.SelectedIndex;\r
145                         if ((1 <= selectedIndex) && (selectedIndex < repos.Count)) {\r
146                                 NaGet.Utils.ListSwap(repos, selectedIndex-1, selectedIndex);\r
147                                 AppliStation.Util.GUIUtils.CheckedListBox_SwapItems(repoListCheckedListBox, selectedIndex-1, selectedIndex);\r
148                                 repoListCheckedListBox.SelectedIndex --;\r
149                                 \r
150                                 isRepoListChanged = true;\r
151                         }\r
152                 }\r
153                 \r
154                 void DownRepoButtonClick(object sender, EventArgs e)\r
155                 {\r
156                         int selectedIndex = repoListCheckedListBox.SelectedIndex;\r
157                         if ((0 <= selectedIndex) && (selectedIndex < (repos.Count-1))) {\r
158                                 NaGet.Utils.ListSwap(repos, selectedIndex, selectedIndex+1);\r
159                                 AppliStation.Util.GUIUtils.CheckedListBox_SwapItems(repoListCheckedListBox, selectedIndex, selectedIndex+1);\r
160                                 repoListCheckedListBox.SelectedIndex ++;\r
161                                 \r
162                                 isRepoListChanged = true;\r
163                         }\r
164                 }\r
165                 \r
166                 void RepoUrlTextBoxLeave(object sender, EventArgs e)\r
167                 {\r
168                         int selectedIndex = repoListCheckedListBox.SelectedIndex;\r
169                         if ((0 <= selectedIndex) && (selectedIndex < repos.Count)) {\r
170                                 if (repoUrlTextBox.Text != repos[selectedIndex].Url.Href) {\r
171                                         repos[selectedIndex].Url = new LocationEntry(repoUrlTextBox.Text);\r
172                                         repoListCheckedListBox.Items[selectedIndex] = repoListCheckedListBoxRenderer(repos[selectedIndex]);\r
173                                         \r
174                                         isRepoListChanged = true;\r
175                                 }\r
176                         }\r
177                 }\r
178                 \r
179                 /// <summary>\r
180                 /// レポジトリリストが編集されたか否かのフラグ\r
181                 /// </summary>\r
182                 /// <remarks>ソフトリストの再読み込みが必要か否かの判断に使われる</remarks>\r
183                 public bool IsRepositoryListSettingChanged {\r
184                         get { return isRepoListChanged; }\r
185                 }\r
186                 \r
187                 /// <summary>\r
188                 /// レポジトリリストの設定を反映する\r
189                 /// </summary>\r
190                 private void commitRepositoryListSetting()\r
191                 {\r
192                         if (isRepoListChanged) {\r
193                                 NaGet.Utils.PutSerializeObject<RepositoriesList>(NaGet.Env.RepositoriesListFile, this.RepositoriesListSetting);\r
194                         }\r
195                 }\r
196                 \r
197                 #endregion\r
198                 \r
199                 #region プロキシサーバ設定関連\r
200 \r
201                 /// <summary>\r
202                 /// プロキシアドレスを設定あるいは取得する\r
203                 /// </summary>\r
204                 public string ProxyAddress {\r
205                         get {\r
206                                 if (proxySameAsIERadioButton.Checked) {\r
207                                         return string.Empty;    \r
208                                 } else if (directConnRadioButton.Checked) {\r
209                                         return "-";\r
210                                 } else {\r
211                                         return proxyURLTextBox.Text;\r
212                                 }\r
213                         }\r
214                         set {\r
215                                 if (string.IsNullOrEmpty(value)) {\r
216                                         proxySameAsIERadioButton.Checked = true;        \r
217                                 } else if ("-" == value) {\r
218                                         directConnRadioButton.Checked = true;\r
219                                 } else {\r
220                                         specifyProxyRadioButton.Checked = true;\r
221                                         proxyURLTextBox.Text = value;\r
222                                 }\r
223                                 \r
224                                 updateProxyURLEnability();\r
225                         }\r
226                 }\r
227                 \r
228                 /// <summary>\r
229                 /// ProxyURLのテキストボックス領域の有効状態を切り替える\r
230                 /// </summary>\r
231                 private void updateProxyURLEnability()\r
232                 {\r
233                         bool isSpecifiedProxy = specifyProxyRadioButton.Checked;\r
234                         \r
235                         proxyURLLabel.Enabled   = isSpecifiedProxy;\r
236                         proxyURLTextBox.Enabled = isSpecifiedProxy;\r
237                 }\r
238                 \r
239                 void ProxyRadioButtonsCheckedChanged(object sender, EventArgs e)\r
240                 {\r
241                         updateProxyURLEnability();\r
242                 }\r
243                 \r
244                 void OpenInternetOptionLinkLabelLinkClicked(object sender, LinkLabelLinkClickedEventArgs e)\r
245                 {\r
246                         try {\r
247                                 ProcessStartInfo procInfo = new ProcessStartInfo("rundll32.exe");\r
248                                 procInfo.Arguments = "shell32.dll,Control_RunDLL inetcpl.cpl,,4";\r
249                                 procInfo.UseShellExecute = true;\r
250                                 procInfo.Verb = "open";\r
251                                 \r
252                                 Process.Start(procInfo);\r
253                         } catch (Exception) {\r
254                                 MessageBox.Show("インターネットオプションが開けませんでした", openInternetOptionLinkLabel.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);\r
255                         }\r
256                 }\r
257                 \r
258                 /// <summary>\r
259                 /// プロキシ設定を、指定された設定オブジェクトに設定する。\r
260                 /// </summary>\r
261                 /// <param name="pref">設定オブジェクト</param>\r
262                 private void commitProxySetting(NaGet.NaGetLibPref pref)\r
263                 {\r
264                         pref.ProxyAddress = this.ProxyAddress;\r
265                 }\r
266                 \r
267                 #endregion\r
268                 \r
269                 /// <summary>\r
270                 /// 指定された設定オブジェクトをファイルとして保存する\r
271                 /// </summary>\r
272                 /// <param name="pref">設定ファイル</param>\r
273                 private void commitNaGetLibPref(NaGet.NaGetLibPref pref)\r
274                 {\r
275                         // ファイルに書き込む\r
276                         string path = NaGet.Env.PrefPath;\r
277                         NaGet.Utils.PutSerializeObject<NaGet.NaGetLibPref>(path, pref);\r
278                         \r
279                         // 設定についてファイルから設定を再読み込みさせる\r
280                         NaGet.Env.LoadPref();\r
281                 }\r
282                 \r
283                 void OkButtonClick(object sender, EventArgs e)\r
284                 {\r
285                         NaGet.NaGetLibPref pref = NaGet.Env.Pref;\r
286                         \r
287                         commitRepositoryListSetting();\r
288                         commitProxySetting(pref);\r
289                         \r
290                         commitNaGetLibPref(pref);\r
291                 }\r
292                 \r
293                 void CancelButtonClick(object sender, EventArgs e)\r
294                 {\r
295                 }\r
296         }\r
297 }\r