OSDN Git Service

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