OSDN Git Service

AppliStation,キャッシュフォルダ選択ダイアログを操作した後にカレントディレクトリが変化する不具合を修正
[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                         this.CacheFolder = userPref.CacheFolder;\r
46                 }\r
47                 \r
48                 #region レポジトリリスト設定関連\r
49                 \r
50                 /// <summary>\r
51                 /// レポジトリリストの設定を読み書きする\r
52                 /// </summary>\r
53                 public RepositoriesList RepositoriesListSetting {\r
54                         get {\r
55                                 RepositoriesList repoList = new RepositoriesList();\r
56                                 repoList.Repositories = repos.ToArray();\r
57                                 return repoList;\r
58                         }\r
59                         set {\r
60                                 repos.Clear();\r
61                                 repos.AddRange(value.Repositories);\r
62                                 \r
63                                 updateRepos();\r
64                                 \r
65                                 // レポジトリ変更状態をリセットする\r
66                                 isRepoListChanged = false;\r
67                         }\r
68                 }\r
69                 \r
70                 /// <summary>\r
71                 /// ListBoxへ表示するレポジトリ表現文字列を返す\r
72                 /// </summary>\r
73                 /// <param name="repo">対象レポジトリ</param>\r
74                 /// <returns>ListBoxに表示すべき文字列</returns>\r
75                 private static string repoListCheckedListBoxRenderer(RepositoryInfo repo) {\r
76                         return string.Format("{0}[{1}]", repo.Name, repo.Url.Href);\r
77                 }\r
78 \r
79                 /// <summary>\r
80                 /// ListBoxの内容を更新(再構築)する。\r
81                 /// </summary>\r
82                 private void updateRepos()\r
83                 {\r
84                         repoListCheckedListBox.Items.Clear();\r
85                         \r
86                         foreach (RepositoryInfo repo in repos) {\r
87                                 string label = repoListCheckedListBoxRenderer(repo);\r
88                                 repoListCheckedListBox.Items.Add(label, repo.Enabled);\r
89                         }\r
90                 }\r
91                 \r
92                 void RepoListCheckedListBoxSelectedIndexChanged(object sender, EventArgs e)\r
93                 {\r
94                         int selectedIndex = repoListCheckedListBox.SelectedIndex;\r
95                         bool selected = (selectedIndex >= 0);\r
96                         \r
97                         removeRepoButton.Enabled        = selected;\r
98                         upRepoButton.Enabled            = selected && ((1 <= selectedIndex) && (selectedIndex < repos.Count));\r
99                         downRepoButton.Enabled          = selected && ((0 <= selectedIndex) && (selectedIndex < (repos.Count-1)));\r
100                         repoUrlLabel.Enabled            = selected;\r
101                         repoUrlTextBox.Enabled          = selected;\r
102                         if (selected) {\r
103                                 repoUrlTextBox.Text = repos[repoListCheckedListBox.SelectedIndex].Url.Href;\r
104                                 repoUrlTextBox.SelectAll();\r
105                         } else {\r
106                                 repoUrlTextBox.Clear();\r
107                         }\r
108                 }\r
109                 \r
110                 void AddRepoButtonClick(object sender, EventArgs e)\r
111                 {\r
112                         RepositoryInfo repo = new RepositoryInfo();\r
113                         repo.Name = string.Format("新しいレポジトリ");\r
114                         repo.Url = new LocationEntry();\r
115                         repo.Enabled = true;\r
116                         \r
117                         repos.Add(repo);\r
118                         \r
119                         updateRepos();\r
120                         repoListCheckedListBox.SelectedIndex = repos.Count - 1;\r
121                         \r
122                         isRepoListChanged = true;\r
123                 }\r
124                 \r
125                 void RepoListCheckedListBoxItemCheck(object sender, ItemCheckEventArgs e)\r
126                 {\r
127                         repos[e.Index].Enabled = (e.NewValue == CheckState.Checked);\r
128                         \r
129                         isRepoListChanged = true;\r
130                 }\r
131                 \r
132                 void RemoveRepoButtonClick(object sender, EventArgs e)\r
133                 {\r
134                         int selectedIndex = repoListCheckedListBox.SelectedIndex;\r
135                         if ((0 <= selectedIndex) && (selectedIndex < repos.Count)) {\r
136                                 string text = string.Format("本当にレポジトリ「{0}」を消去しますか?", repos[selectedIndex].Name);\r
137                                 DialogResult result = MessageBox.Show(text, "レポジトリの削除", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);\r
138                                 if (result == DialogResult.OK) {\r
139                                         repos.RemoveAt(selectedIndex);\r
140                                         repoListCheckedListBox.Items.RemoveAt(selectedIndex);\r
141                                         \r
142                                         isRepoListChanged = true;\r
143                                 }\r
144                         }\r
145                 }\r
146                 \r
147                 void UpRepoButtonClick(object sender, EventArgs e)\r
148                 {\r
149                         int selectedIndex = repoListCheckedListBox.SelectedIndex;\r
150                         if ((1 <= selectedIndex) && (selectedIndex < repos.Count)) {\r
151                                 NaGet.Utils.ListSwap(repos, selectedIndex-1, selectedIndex);\r
152                                 AppliStation.Util.GUIUtils.CheckedListBox_SwapItems(repoListCheckedListBox, selectedIndex-1, selectedIndex);\r
153                                 repoListCheckedListBox.SelectedIndex --;\r
154                                 \r
155                                 isRepoListChanged = true;\r
156                         }\r
157                 }\r
158                 \r
159                 void DownRepoButtonClick(object sender, EventArgs e)\r
160                 {\r
161                         int selectedIndex = repoListCheckedListBox.SelectedIndex;\r
162                         if ((0 <= selectedIndex) && (selectedIndex < (repos.Count-1))) {\r
163                                 NaGet.Utils.ListSwap(repos, selectedIndex, selectedIndex+1);\r
164                                 AppliStation.Util.GUIUtils.CheckedListBox_SwapItems(repoListCheckedListBox, selectedIndex, selectedIndex+1);\r
165                                 repoListCheckedListBox.SelectedIndex ++;\r
166                                 \r
167                                 isRepoListChanged = true;\r
168                         }\r
169                 }\r
170                 \r
171                 void RepoUrlTextBoxLeave(object sender, EventArgs e)\r
172                 {\r
173                         int selectedIndex = repoListCheckedListBox.SelectedIndex;\r
174                         if ((0 <= selectedIndex) && (selectedIndex < repos.Count)) {\r
175                                 if (repoUrlTextBox.Text != repos[selectedIndex].Url.Href) {\r
176                                         repos[selectedIndex].Url = new LocationEntry(repoUrlTextBox.Text);\r
177                                         repoListCheckedListBox.Items[selectedIndex] = repoListCheckedListBoxRenderer(repos[selectedIndex]);\r
178                                         \r
179                                         isRepoListChanged = true;\r
180                                 }\r
181                         }\r
182                 }\r
183                 \r
184                 /// <summary>\r
185                 /// レポジトリリストが編集されたか否かのフラグ\r
186                 /// </summary>\r
187                 /// <remarks>ソフトリストの再読み込みが必要か否かの判断に使われる</remarks>\r
188                 public bool IsRepositoryListSettingChanged {\r
189                         get { return isRepoListChanged; }\r
190                 }\r
191                 \r
192                 /// <summary>\r
193                 /// レポジトリリストの設定を反映する\r
194                 /// </summary>\r
195                 private void commitRepositoryListSetting()\r
196                 {\r
197                         if (isRepoListChanged) {\r
198                                 NaGet.Utils.PutSerializeObject<RepositoriesList>(NaGet.Env.RepositoriesListFile, this.RepositoriesListSetting);\r
199                         }\r
200                 }\r
201                 \r
202                 #endregion\r
203                 \r
204                 #region プロキシサーバ設定関連\r
205 \r
206                 /// <summary>\r
207                 /// プロキシアドレスを設定あるいは取得する\r
208                 /// </summary>\r
209                 public string ProxyAddress {\r
210                         get {\r
211                                 if (proxySameAsIERadioButton.Checked) {\r
212                                         return string.Empty;    \r
213                                 } else if (directConnRadioButton.Checked) {\r
214                                         return "-";\r
215                                 } else {\r
216                                         return proxyURLTextBox.Text;\r
217                                 }\r
218                         }\r
219                         set {\r
220                                 if (string.IsNullOrEmpty(value)) {\r
221                                         proxySameAsIERadioButton.Checked = true;        \r
222                                 } else if ("-" == value) {\r
223                                         directConnRadioButton.Checked = true;\r
224                                 } else {\r
225                                         specifyProxyRadioButton.Checked = true;\r
226                                         proxyURLTextBox.Text = value;\r
227                                 }\r
228                                 \r
229                                 updateProxyURLEnability();\r
230                         }\r
231                 }\r
232                 \r
233                 /// <summary>\r
234                 /// ProxyURLのテキストボックス領域の有効状態を切り替える\r
235                 /// </summary>\r
236                 private void updateProxyURLEnability()\r
237                 {\r
238                         bool isSpecifiedProxy = specifyProxyRadioButton.Checked;\r
239                         \r
240                         proxyURLLabel.Enabled   = isSpecifiedProxy;\r
241                         proxyURLTextBox.Enabled = isSpecifiedProxy;\r
242                 }\r
243                 \r
244                 void ProxyRadioButtonsCheckedChanged(object sender, EventArgs e)\r
245                 {\r
246                         updateProxyURLEnability();\r
247                 }\r
248                 \r
249                 void OpenInternetOptionLinkLabelLinkClicked(object sender, LinkLabelLinkClickedEventArgs e)\r
250                 {\r
251                         string verb = "open";\r
252                         \r
253                         if (sender == openInternetOptionLinkAdminLabel) {\r
254                                 verb = "runas"; \r
255                         }\r
256                         \r
257                         try {\r
258                                 ProcessStartInfo procInfo = new ProcessStartInfo("control.exe");\r
259                                 procInfo.Arguments = "inetcpl.cpl,,4";\r
260                                 procInfo.UseShellExecute = true;\r
261                                 procInfo.Verb = verb;\r
262                                 \r
263                                 Process.Start(procInfo);\r
264                         } catch (System.ComponentModel.Win32Exception ex) {\r
265                                 MessageBox.Show(ex.Message, "インターネットオプション", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
266                         } catch (Exception) {\r
267                                 MessageBox.Show("インターネットオプションが開けませんでした", "インターネットオプション", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
268                         }\r
269                 }\r
270                 \r
271                 /// <summary>\r
272                 /// プロキシ設定を、指定された設定オブジェクトに設定する。\r
273                 /// </summary>\r
274                 /// <param name="pref">設定オブジェクト</param>\r
275                 private void commitProxySetting(NaGet.NaGetLibPref pref)\r
276                 {\r
277                         pref.ProxyAddress = this.ProxyAddress;\r
278                 }\r
279                 \r
280                 #endregion\r
281                 \r
282                 #region インストール設定関連\r
283                 \r
284                 /// <summary>\r
285                 /// インストーラーファイルをウイルススキャンするかを設定あるいは取得する\r
286                 /// </summary>\r
287                 public bool EnableScanInstallerFile {\r
288                         set {   this.installScanInstallerFileCheckbox.Checked = value;  }\r
289                         get {   return this.installScanInstallerFileCheckbox.Checked;   }\r
290                 }\r
291                 \r
292                 /// <summary>\r
293                 /// インストール・アンインストールを優先度を下げて実行するかを設定あるいは取得する\r
294                 /// </summary>\r
295                 public bool InstallOnBackground {\r
296                         set {   this.installOnBackgroundCheckBox.Checked = value;       }\r
297                         get {   return this.installOnBackgroundCheckBox.Checked;        }\r
298                 }\r
299                 \r
300                 public string CacheFolder {\r
301                         set {\r
302                                 if (string.IsNullOrEmpty(value)) {\r
303                                         this.cacheFolderCustomCheckBox.Checked = false;\r
304                                         this.cacheFolderTextBox.Text = string.Empty;\r
305                                 } else {\r
306                                         this.cacheFolderCustomCheckBox.Checked = true;\r
307                                         this.cacheFolderTextBox.Text = value;\r
308                                 }\r
309                         }\r
310                         get {\r
311                                 if (this.cacheFolderCustomCheckBox.Checked) {\r
312                                         return this.cacheFolderTextBox.Text;\r
313                                 } else {\r
314                                         return null;\r
315                                 }\r
316                         }\r
317                 }\r
318                 \r
319                 /// <summary>\r
320                 /// インストール関連設定を、指定された設定オブジェクトに設定する。\r
321                 /// </summary>\r
322                 /// <param name="pref">設定オブジェクト</param>\r
323                 private void commitInstallSetting(NaGet.NaGetLibPref pref)\r
324                 {\r
325                         pref.EnableScanInstallerFile = this.EnableScanInstallerFile;\r
326                         pref.InstallOnBackground = this.InstallOnBackground;\r
327                         pref.CacheFolder = this.CacheFolder;\r
328                 }\r
329                 \r
330                 #endregion\r
331                 \r
332                 /// <summary>\r
333                 /// 指定された設定オブジェクトをファイルとして保存する\r
334                 /// </summary>\r
335                 /// <param name="pref">設定ファイル</param>\r
336                 private static void commitNaGetLibPref(NaGet.NaGetLibPref pref)\r
337                 {\r
338                         // ファイルに書き込む\r
339                         string path = NaGet.Env.PrefPath;\r
340                         NaGet.Utils.PutSerializeObject<NaGet.NaGetLibPref>(path, pref);\r
341                         \r
342                         // 設定についてファイルから設定を再読み込みさせる\r
343                         NaGet.Env.LoadPref();\r
344                 }\r
345                 \r
346                 void OkButtonClick(object sender, EventArgs e)\r
347                 {\r
348                         NaGet.NaGetLibPref pref = NaGet.Env.Pref;\r
349                         \r
350                         commitRepositoryListSetting();\r
351                         commitProxySetting(pref);\r
352                         commitInstallSetting(pref);\r
353                         \r
354                         commitNaGetLibPref(pref);\r
355                 }\r
356                 \r
357                 void CancelButtonClick(object sender, EventArgs e)\r
358                 {\r
359                 }\r
360                 \r
361                 void RepoUrlTextBoxValidating(object sender, System.ComponentModel.CancelEventArgs e)\r
362                 {\r
363                         string urlText = repoUrlTextBox.Text;\r
364                         \r
365                         if (string.IsNullOrEmpty(urlText)) {\r
366                                 return; // special case.\r
367                         } if (Uri.IsWellFormedUriString(urlText, UriKind.Absolute) == false) {\r
368                                 errorProvider.SetError(repoUrlLabel, "URLの記述が不正です");\r
369                                 e.Cancel = true;\r
370                         } else {\r
371                                 Uri uri = new Uri(urlText);\r
372                                 if ((uri.Scheme != Uri.UriSchemeFile) &&\r
373                                     (uri.Scheme != Uri.UriSchemeFtp) &&\r
374                                     (uri.Scheme != Uri.UriSchemeHttp) &&\r
375                                     (uri.Scheme != Uri.UriSchemeHttps)){\r
376                                         errorProvider.SetError(repoUrlLabel, "URLの記述が不正です");\r
377                                         e.Cancel = true;\r
378                                 } else {\r
379                                         errorProvider.Clear();\r
380                                 }\r
381                         }\r
382                 }\r
383                 \r
384                 \r
385                 void CacheFolderCustomCheckBoxCheckedChanged(object sender, EventArgs e)\r
386                 {\r
387                         cacheFolderTextBox.Enabled = cacheFolderCustomCheckBox.Checked;\r
388                         cacheFolderBrowseButton.Enabled = cacheFolderCustomCheckBox.Checked;\r
389                 }\r
390                 \r
391                 void CacheFolderBrowseButtonClick(object sender, EventArgs e)\r
392                 {\r
393                         string pwd = System.IO.Directory.GetCurrentDirectory();\r
394                         FolderBrowserDialog fd = new FolderBrowserDialog();\r
395                         fd.Description = "キャッシュフォルダーを指定してください。";\r
396                         fd.SelectedPath = cacheFolderTextBox.Text;\r
397                         fd.ShowNewFolderButton = true;\r
398                         \r
399                         if (fd.ShowDialog(this) == DialogResult.OK) {\r
400                                 cacheFolderTextBox.Text = fd.SelectedPath;\r
401                         }\r
402                         \r
403                         System.IO.Directory.SetCurrentDirectory(pwd); // ダイアログで変わったカレントディレクトリを元に戻す\r
404                 }\r
405                 \r
406                 void CacheFolderTextBoxValidating(object sender, System.ComponentModel.CancelEventArgs e)\r
407                 {\r
408                         string folderPath = cacheFolderTextBox.Text;\r
409                         \r
410                         if (string.IsNullOrEmpty(folderPath)) {\r
411                                 errorProvider.Clear();\r
412                                 return; // special case\r
413                         } else if (! System.IO.Directory.Exists(folderPath)) {\r
414                                 errorProvider.SetError(cacheFolderTextBox, "存在しないフォルダーパスを指定しています。");\r
415                                 e.Cancel = true;\r
416                         } else {\r
417                                 errorProvider.Clear();\r
418                         }\r
419                 }\r
420                 \r
421                 void CacheFolderOpenLinkLabelLinkClicked(object sender, LinkLabelLinkClickedEventArgs e)\r
422                 {\r
423                         string folderPath;\r
424                         \r
425                         if (cacheFolderCustomCheckBox.Checked) {\r
426                                 folderPath = cacheFolderTextBox.Text;\r
427                         } else {\r
428                                 // デフォルトは AppDataFolderPath/Cache。\r
429                                 folderPath = System.IO.Path.Combine(NaGet.Env.AppDataFolderPath, "Cache");\r
430                         }\r
431                         \r
432                         if (System.IO.Directory.Exists(folderPath)) {\r
433                                 try {\r
434                                         Process.Start(folderPath);\r
435                                 } catch (Exception ex) {\r
436                                         MessageBox.Show(ex.Message, "キャッシュフォルダー", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
437                                 }\r
438                         } else {\r
439                                 MessageBox.Show(string.Format("フォルダーパス\"{0}\"は存在しませんでした。", folderPath), "キャッシュフォルダー", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
440                         }\r
441                 }\r
442         }\r
443 }\r