OSDN Git Service

555bfa10a1d2e05265b857f54964d4fc7ca9337c
[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                 public UserPrefForm()\r
20                 {\r
21                         repos = new List<RepositoryInfo>();\r
22                         \r
23                         InitializeComponent();\r
24                         \r
25                         loadCurrentPref();\r
26                 }\r
27                 \r
28                 /// <summary>\r
29                 /// 現在の設定を読み込む\r
30                 /// </summary>\r
31                 public void loadCurrentPref()\r
32                 {\r
33                         this.RepositoriesListSetting = NaGet.Utils.GetDeserializedObject<RepositoriesList>(NaGet.Env.RepositoriesListFile);\r
34                         \r
35                         NaGet.NaGetLibPref userPref = NaGet.Env.Pref;\r
36                         this.ProxyAddress = userPref.ProxyAddress;\r
37                 }\r
38                 \r
39                 #region レポジトリリスト設定関連\r
40                 \r
41                 public RepositoriesList RepositoriesListSetting {\r
42                         get {\r
43                                 RepositoriesList repoList = new RepositoriesList();\r
44                                 repoList.Repositories = repos.ToArray();\r
45                                 return repoList;\r
46                         }\r
47                         set {\r
48                                 repos.Clear();\r
49                                 repos.AddRange(value.Repositories);\r
50                                 \r
51                                 updateRepos();\r
52                                 \r
53                                 // レポジトリ変更状態をリセットする\r
54                                 isRepoListChanged = false;\r
55                         }\r
56                 }\r
57                 \r
58                 private string repoListCheckedListBoxRenderer(RepositoryInfo repo) {\r
59                         return string.Format("{0}[{1}]", repo.Name, repo.Url.Href);\r
60                 }\r
61                 \r
62                 private void updateRepos()\r
63                 {\r
64                         repoListCheckedListBox.Items.Clear();\r
65                         \r
66                         foreach (RepositoryInfo repo in repos) {\r
67                                 string label = repoListCheckedListBoxRenderer(repo);\r
68                                 repoListCheckedListBox.Items.Add(label, repo.Enabled);\r
69                         }\r
70                 }\r
71                 \r
72                 \r
73                 void RepoListCheckedListBoxSelectedIndexChanged(object sender, EventArgs e)\r
74                 {\r
75                         int selectedIndex = repoListCheckedListBox.SelectedIndex;\r
76                         bool selected = (selectedIndex >= 0);\r
77                         \r
78                         removeRepoButton.Enabled        = selected;\r
79                         upRepoButton.Enabled            = selected && ((1 <= selectedIndex) && (selectedIndex < repos.Count));\r
80                         downRepoButton.Enabled          = selected && ((0 <= selectedIndex) && (selectedIndex < (repos.Count-1)));\r
81                         repoUrlLabel.Enabled            = selected;\r
82                         repoUrlTextBox.Enabled          = selected;\r
83                         if (selected) {\r
84                                 repoUrlTextBox.Text = repos[repoListCheckedListBox.SelectedIndex].Url.Href;\r
85                                 repoUrlTextBox.SelectAll();\r
86                         } else {\r
87                                 repoUrlTextBox.Clear();\r
88                         }\r
89                 }\r
90                 \r
91                 void AddRepoButtonClick(object sender, EventArgs e)\r
92                 {\r
93                         RepositoryInfo repo = new RepositoryInfo();\r
94                         repo.Name = string.Format("新しいレポジトリ");\r
95                         repo.Url = new LocationEntry();\r
96                         repo.Enabled = true;\r
97                         \r
98                         repos.Add(repo);\r
99                         \r
100                         updateRepos();\r
101                         repoListCheckedListBox.SelectedIndex = repos.Count - 1;\r
102                         \r
103                         isRepoListChanged = true;\r
104                 }\r
105                 \r
106                 void RepoListCheckedListBoxItemCheck(object sender, ItemCheckEventArgs e)\r
107                 {\r
108                         repos[e.Index].Enabled = (e.NewValue == CheckState.Checked);\r
109                         \r
110                         isRepoListChanged = true;\r
111                 }\r
112                 \r
113                 void RemoveRepoButtonClick(object sender, EventArgs e)\r
114                 {\r
115                         int selectedIndex = repoListCheckedListBox.SelectedIndex;\r
116                         if ((0 <= selectedIndex) && (selectedIndex < repos.Count)) {\r
117                                 string text = string.Format("本当にレポジトリ「{0}」を消去しますか?", repos[selectedIndex].Name);\r
118                                 DialogResult result = MessageBox.Show(text, "レポジトリの削除", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);\r
119                                 if (result == DialogResult.OK) {\r
120                                         repos.RemoveAt(selectedIndex);\r
121                                         repoListCheckedListBox.Items.RemoveAt(selectedIndex);\r
122                                         \r
123                                         isRepoListChanged = true;\r
124                                 }\r
125                         }\r
126                 }\r
127                 \r
128                 void UpRepoButtonClick(object sender, EventArgs e)\r
129                 {\r
130                         int selectedIndex = repoListCheckedListBox.SelectedIndex;\r
131                         if ((1 <= selectedIndex) && (selectedIndex < repos.Count)) {\r
132                                 NaGet.Utils.ListSwap(repos, selectedIndex-1, selectedIndex);\r
133                                 AppliStation.Util.GUIUtils.CheckedListBox_SwapItems(repoListCheckedListBox, selectedIndex-1, selectedIndex);\r
134                                 repoListCheckedListBox.SelectedIndex --;\r
135                                 \r
136                                 isRepoListChanged = true;\r
137                         }\r
138                 }\r
139                 \r
140                 void DownRepoButtonClick(object sender, EventArgs e)\r
141                 {\r
142                         int selectedIndex = repoListCheckedListBox.SelectedIndex;\r
143                         if ((0 <= selectedIndex) && (selectedIndex < (repos.Count-1))) {\r
144                                 NaGet.Utils.ListSwap(repos, selectedIndex, selectedIndex+1);\r
145                                 AppliStation.Util.GUIUtils.CheckedListBox_SwapItems(repoListCheckedListBox, selectedIndex, selectedIndex+1);\r
146                                 repoListCheckedListBox.SelectedIndex ++;\r
147                                 \r
148                                 isRepoListChanged = true;\r
149                         }\r
150                 }\r
151                 \r
152                 void RepoUrlTextBoxLeave(object sender, EventArgs e)\r
153                 {\r
154                         int selectedIndex = repoListCheckedListBox.SelectedIndex;\r
155                         if ((0 <= selectedIndex) && (selectedIndex < repos.Count)) {\r
156                                 if (repoUrlTextBox.Text != repos[selectedIndex].Url.Href) {\r
157                                         repos[selectedIndex].Url = new LocationEntry(repoUrlTextBox.Text);\r
158                                         repoListCheckedListBox.Items[selectedIndex] = repoListCheckedListBoxRenderer(repos[selectedIndex]);\r
159                                         \r
160                                         isRepoListChanged = true;\r
161                                 }\r
162                         }\r
163                 }\r
164                 \r
165                 public bool IsRepositoryListSettingChanged {\r
166                         get { return isRepoListChanged; }\r
167                 }\r
168                 \r
169                 private void commitRepositoryListSetting()\r
170                 {\r
171                         if (isRepoListChanged) {\r
172                                 NaGet.Utils.PutSerializeObject<RepositoriesList>(NaGet.Env.RepositoriesListFile, this.RepositoriesListSetting);\r
173                         }\r
174                 }\r
175                 \r
176                 #endregion\r
177                 \r
178                 #region プロキシサーバ設定関連\r
179 \r
180                 public string ProxyAddress {\r
181                         get {\r
182                                 if (proxySameAsIERadioButton.Checked) {\r
183                                         return string.Empty;    \r
184                                 } else if (directConnRadioButton.Checked) {\r
185                                         return "-";\r
186                                 } else {\r
187                                         return proxyURLTextBox.Text;\r
188                                 }\r
189                         }\r
190                         set {\r
191                                 if (string.IsNullOrEmpty(value)) {\r
192                                         proxySameAsIERadioButton.Checked = true;        \r
193                                 } else if ("-" == value) {\r
194                                         directConnRadioButton.Checked = true;\r
195                                 } else {\r
196                                         specifyProxyRadioButton.Checked = true;\r
197                                         proxyURLTextBox.Text = value;\r
198                                 }\r
199                                 \r
200                                 updateProxyURLEnability();\r
201                         }\r
202                 }\r
203                 \r
204                 private void updateProxyURLEnability()\r
205                 {\r
206                         bool isSpecifiedProxy = specifyProxyRadioButton.Checked;\r
207                         \r
208                         proxyURLLabel.Enabled   = isSpecifiedProxy;\r
209                         proxyURLTextBox.Enabled = isSpecifiedProxy;\r
210                 }\r
211                 \r
212                 void ProxyRadioButtonsCheckedChanged(object sender, EventArgs e)\r
213                 {\r
214                         updateProxyURLEnability();\r
215                 }\r
216                 \r
217                 private void commitProxySetting(NaGet.NaGetLibPref pref)\r
218                 {\r
219                         pref.ProxyAddress = this.ProxyAddress;\r
220                         \r
221                         NaGet.Utils.PutSerializeObject<NaGet.NaGetLibPref>(NaGet.Env.PrefPath, pref);\r
222                 }\r
223                 \r
224                 #endregion\r
225                 \r
226                 private void commitNaGetLibPref(NaGet.NaGetLibPref pref)\r
227                 {\r
228                         // ファイルに書き込む\r
229                         string path = NaGet.Env.PrefPath;\r
230                         NaGet.Utils.PutSerializeObject<NaGet.NaGetLibPref>(path, pref);\r
231                         \r
232                         // 設定についてファイルから設定を再読み込みさせる\r
233                         NaGet.Env.LoadPref();\r
234                 }\r
235                 \r
236                 void OkButtonClick(object sender, EventArgs e)\r
237                 {\r
238                         NaGet.NaGetLibPref pref = NaGet.Env.Pref;\r
239                         \r
240                         commitRepositoryListSetting();\r
241                         commitProxySetting(pref);\r
242                         \r
243                         commitNaGetLibPref(pref);\r
244                 }\r
245                 \r
246                 void CancelButtonClick(object sender, EventArgs e)\r
247                 {\r
248                 }\r
249         }\r
250 }\r