OSDN Git Service

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