OSDN Git Service

#27314 記事名にコロンを含むページが正しく処理できないの対応(MediaWikiのAPIと設定からウィキ間リンクを判別するよう変更),
[wptscs/wpts.git] / Wptscs / ConfigForm.cs
1 // ================================================================================================
2 // <summary>
3 //      Wikipedia翻訳支援ツール設定画面クラスソース</summary>
4 //
5 // <copyright file="ConfigForm.cs" company="honeplusのメモ帳">
6 //      Copyright (C) 2012 Honeplus. All rights reserved.</copyright>
7 // <author>
8 //      Honeplus</author>
9 // ================================================================================================
10
11 namespace Honememo.Wptscs
12 {
13     using System;
14     using System.Collections.Generic;
15     using System.ComponentModel;
16     using System.Data;
17     using System.Drawing;
18     using System.Linq;
19     using System.Reflection;
20     using System.Text;
21     using System.Windows.Forms;
22     using Honememo.Utilities;
23     using Honememo.Wptscs.Models;
24     using Honememo.Wptscs.Properties;
25     using Honememo.Wptscs.Utilities;
26     using Honememo.Wptscs.Websites;
27
28     /// <summary>
29     /// Wikipedia翻訳支援ツール設定画面のクラスです。
30     /// </summary>
31     public partial class ConfigForm : Form
32     {
33         #region private変数
34
35         /// <summary>
36         /// 現在設定中のアプリケーションの設定。
37         /// </summary>
38         /// <remarks>設定画面を閉じた後は再読み込みされるので、必要に応じて随時更新してよい。</remarks>
39         private Config config;
40
41         /// <summary>
42         /// <seealso cref="comboBoxLanguage"/>で選択していたアイテムのバックアップ。
43         /// </summary>
44         private string comboBoxLanguageSelectedText;
45
46         #endregion
47
48         #region コンストラクタ
49
50         /// <summary>
51         /// コンストラクタ。
52         /// </summary>
53         /// <param name="config">設定対象のConfig。</param>
54         /// <remarks>configは設定画面の操作により随時更新される。呼び出し元では再読み込みすること。</remarks>
55         public ConfigForm(Config config)
56         {
57             this.InitializeComponent();
58
59             // 設定対象のConfigを受け取る
60             this.config = Honememo.Utilities.Validate.NotNull(config, "config");
61         }
62
63         #endregion
64         
65         #region フォームの各イベントのメソッド
66
67         /// <summary>
68         /// フォームロード時の処理。
69         /// </summary>
70         /// <param name="sender">イベント発生オブジェクト。</param>
71         /// <param name="e">発生したイベント。</param>
72         private void ConfigForm_Load(object sender, EventArgs e)
73         {
74             try
75             {
76                 // 各タブの内容を初期化する
77
78                 // 記事の置き換えタブの初期化
79                 this.ImportTranslationDictionaryView(this.dataGridViewItems, this.config.ItemTables);
80
81                 // 見出しの置き換えタブの初期化
82                 this.ImportTranslationTableView(this.dataGridViewHeading, this.config.HeadingTable);
83
84                 // サーバー/言語タブの初期化
85                 foreach (Website site in this.config.Websites)
86                 {
87                     this.comboBoxLanguage.Items.Add(site.Language.Code);
88                 }
89
90                 // その他タブの初期化
91                 this.textBoxCacheExpire.Text = Settings.Default.CacheExpire.Days.ToString();
92                 this.textBoxUserAgent.Text = Settings.Default.UserAgent;
93                 this.textBoxReferer.Text = Settings.Default.Referer;
94                 this.textBoxMaxConnectRetries.Text = Settings.Default.MaxConnectRetries.ToString();
95                 this.textBoxConnectRetryTime.Text = Settings.Default.ConnectRetryTime.ToString();
96                 this.checkBoxIgnoreError.Checked = Settings.Default.IgnoreError;
97                 this.labelApplicationName.Text = FormUtils.ApplicationName();
98                 AssemblyCopyrightAttribute copyright = Attribute.GetCustomAttribute(
99                     Assembly.GetExecutingAssembly(),
100                     typeof(AssemblyCopyrightAttribute)) as AssemblyCopyrightAttribute;
101                 if (copyright != null)
102                 {
103                     this.labelCopyright.Text = copyright.Copyright;
104                 }
105             }
106             catch (Exception ex)
107             {
108                 // 通常この処理では例外は発生しないはず(Configに読めているので)。想定外のエラー用
109                 FormUtils.ErrorDialog(Resources.ErrorMessageDevelopmentError, ex.Message, ex.StackTrace);
110             }
111         }
112
113         /// <summary>
114         /// OKボタン押下時の処理。
115         /// </summary>
116         /// <param name="sender">イベント発生オブジェクト。</param>
117         /// <param name="e">発生したイベント。</param>
118         private void ButtonOk_Click(object sender, EventArgs e)
119         {
120             try
121             {
122                 // 各タブの内容を設定ファイルに保存する
123
124                 // 記事の置き換えタブの保存
125                 this.config.ItemTables = this.ExportTranslationDictionaryView(this.dataGridViewItems);
126
127                 // 見出しの置き換えタブの保存
128                 this.config.HeadingTable = this.ExportTranslationTableView(this.dataGridViewHeading);
129
130                 // サーバー/言語タブの保存
131                 // ※ このタブはコンボボックス変更のタイミングで保存されるので、そのメソッドを呼ぶ
132                 this.ComboBoxLanguuage_SelectedIndexChanged(sender, e);
133
134                 // その他タブの保存
135                 Settings.Default.CacheExpire = new TimeSpan(int.Parse(this.textBoxCacheExpire.Text), 0, 0, 0);
136                 Settings.Default.UserAgent = this.textBoxUserAgent.Text;
137                 Settings.Default.Referer = this.textBoxReferer.Text;
138                 Settings.Default.MaxConnectRetries = int.Parse(this.textBoxMaxConnectRetries.Text);
139                 Settings.Default.ConnectRetryTime = int.Parse(this.textBoxConnectRetryTime.Text);
140                 Settings.Default.IgnoreError = this.checkBoxIgnoreError.Checked;
141
142                 // 設定をファイルに保存
143                 Settings.Default.Save();
144                 try
145                 {
146                     this.config.Save();
147
148                     // 全部成功なら画面を閉じる
149                     // ※ エラーの場合、どうしても駄目ならキャンセルボタンで閉じてもらう
150                     this.Close();
151                 }
152                 catch (Exception ex)
153                 {
154                     // 異常時はエラーメッセージを表示
155                     System.Diagnostics.Debug.WriteLine(ex.StackTrace);
156                     FormUtils.ErrorDialog(Resources.ErrorMessageConfigSaveFailed, ex.Message);
157                 }
158             }
159             catch (Exception ex)
160             {
161                 // 通常ファイル保存以外では例外は発生しないはず。想定外のエラー用
162                 FormUtils.ErrorDialog(Resources.ErrorMessageDevelopmentError, ex.Message, ex.StackTrace);
163             }
164         }
165
166         #endregion
167
168         #region 記事の置き換えタブのイベントのメソッド
169
170         /// <summary>
171         /// 記事の置き換え対訳表への行追加時の処理。
172         /// </summary>
173         /// <param name="sender">イベント発生オブジェクト。</param>
174         /// <param name="e">発生したイベント。</param>
175         private void DataGridViewItems_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
176         {
177             for (int i = e.RowIndex - 1; i < this.dataGridViewItems.Rows.Count; i++)
178             {
179                 // プログラムから追加された場合は現在のインデックス、画面から追加した場合は+1したインデックスが来る
180                 if (i >= 0)
181                 {
182                     this.dataGridViewItems.Rows[i].Cells["ColumnArrow"].Value = Resources.RightArrow;
183                 }
184             }
185         }
186
187         /// <summary>
188         /// 記事の置き換え対訳表のセル編集時のバリデート処理。
189         /// </summary>
190         /// <param name="sender">イベント発生オブジェクト。</param>
191         /// <param name="e">発生したイベント。</param>
192         private void DataGridViewItems_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
193         {
194             // 取得日時列のみチェック
195             if (this.dataGridViewItems.Columns[e.ColumnIndex].Name != "ColumnTimestamp")
196             {
197                 return;
198             }
199
200             // 空または日付として認識可能な値の場合OK
201             string value = e.FormattedValue.ToString();
202             DateTime dummy;
203             if (String.IsNullOrWhiteSpace(value) || DateTime.TryParse(value, out dummy))
204             {
205                 return;
206             }
207
208             // 不許可値の場合、NGメッセージを表示
209             this.dataGridViewItems.Rows[e.RowIndex].ErrorText = Resources.WarningMessageUnformatedTimestamp;
210             e.Cancel = true;
211         }
212
213         /// <summary>
214         /// 記事の置き換え対訳表のセル編集時のバリデート成功時の処理。
215         /// </summary>
216         /// <param name="sender">イベント発生オブジェクト。</param>
217         /// <param name="e">発生したイベント。</param>
218         private void DataGridViewItems_CellValidated(object sender, DataGridViewCellEventArgs e)
219         {
220             // 取得日時列の場合、バリデートNGメッセージを消す
221             // ※ 他の列で消さないのは、エラーを出しているのがRowValidatingの場合もあるから
222             if (this.dataGridViewItems.Columns[e.ColumnIndex].Name == "ColumnTimestamp")
223             {
224                 this.dataGridViewItems.Rows[e.RowIndex].ErrorText = String.Empty;
225             }
226         }
227
228         /// <summary>
229         /// 記事の置き換え対訳表のセル変更時の処理。
230         /// </summary>
231         /// <param name="sender">イベント発生オブジェクト。</param>
232         /// <param name="e">発生したイベント。</param>
233         private void DataGridViewItems_CellValueChanged(object sender, DataGridViewCellEventArgs e)
234         {
235             // 取得日時列が空の場合、有効期限が無期限として背景色を変更
236             // ※ ただし全列が空(新規行など)の場合は無視
237             if (e.RowIndex >= 0)
238             {
239                 DataGridViewRow row = this.dataGridViewItems.Rows[e.RowIndex];
240                 if (String.IsNullOrWhiteSpace(FormUtils.ToString(row.Cells["ColumnTimestamp"]))
241                     && !this.IsEmptyDataGridViewItemsRow(row))
242                 {
243                     // 背景色を変更
244                     row.DefaultCellStyle.BackColor = Color.Bisque;
245                 }
246                 else if (row.InheritedStyle.BackColor != this.dataGridViewItems.DefaultCellStyle.BackColor)
247                 {
248                     // 背景色を戻す
249                     // ※ DefaultCellStyleプロパティにアクセスしたタイミングでインスタンスが
250                     //    作成されてしまうため、InheritedStyleを調べて変更が必要な場合だけアクセス
251                     row.DefaultCellStyle.BackColor = this.dataGridViewItems.DefaultCellStyle.BackColor;
252                 }
253             }
254         }
255
256         /// <summary>
257         /// 記事の置き換え対訳表の行編集時のバリデート処理。
258         /// </summary>
259         /// <param name="sender">イベント発生オブジェクト。</param>
260         /// <param name="e">発生したイベント。</param>
261         private void DataGridViewItems_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
262         {
263             // 翻訳元、記事名、翻訳先が未入力の場合、バリデートNGメッセージを表示
264             // ※ ただし全列が空(新規行など)の場合は無視
265             DataGridViewRow row = this.dataGridViewItems.Rows[e.RowIndex];
266             if ((String.IsNullOrWhiteSpace(FormUtils.ToString(row.Cells["ColumnFromCode"]))
267                 || String.IsNullOrWhiteSpace(FormUtils.ToString(row.Cells["ColumnToCode"]))
268                 || String.IsNullOrWhiteSpace(FormUtils.ToString(row.Cells["ColumnFromTitle"])))
269                 && !this.IsEmptyDataGridViewItemsRow(row))
270             {
271                 row.ErrorText = Resources.WarningMessageEmptyTranslationDictionary;
272                 e.Cancel = true;
273             }
274         }
275
276         /// <summary>
277         /// 記事の置き換え対訳表を使用する<see cref="DataGridView"/>の値設定を行う。
278         /// </summary>
279         /// <param name="view">対訳表を表示するビュー。</param>
280         /// <param name="dictionaries">対訳表データ。</param>
281         private void ImportTranslationDictionaryView(DataGridView view, IList<TranslationDictionary> dictionaries)
282         {
283             // 初期設定以外の場合も想定して最初にクリア
284             view.Rows.Clear();
285             foreach (TranslationDictionary dic in dictionaries)
286             {
287                 foreach (KeyValuePair<string, TranslationDictionary.Item> item in dic)
288                 {
289                     // 行を追加しその行を取得
290                     DataGridViewRow row = view.Rows[view.Rows.Add()];
291
292                     // 1行分の初期値を設定。右矢印は別途イベントで追加すること
293                     row.Cells["ColumnFromCode"].Value = dic.From;
294                     row.Cells["ColumnFromTitle"].Value = item.Key;
295                     row.Cells["ColumnAlias"].Value = item.Value.Alias;
296                     row.Cells["ColumnToCode"].Value = dic.To;
297                     row.Cells["ColumnToTitle"].Value = item.Value.Word;
298                     if (item.Value.Timestamp.HasValue)
299                     {
300                         row.Cells["ColumnTimestamp"].Value = item.Value.Timestamp.Value.ToLocalTime().ToString("G");
301                     }
302                 }
303             }
304
305             // 取得日時の降順でソート、空の列は先頭にする
306             this.dataGridViewItems.Sort(new TranslationDictionaryViewComparer());
307
308             // 列幅をデータ長に応じて自動調整
309             // ※ 常に行ってしまうと、読み込みに非常に時間がかかるため
310             view.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
311         }
312
313         /// <summary>
314         /// 記事の置き換え対訳表を使用する<see cref="DataGridView"/>からデータを抽出する。
315         /// </summary>
316         /// <param name="view">対訳表を表示するビュー。</param>
317         /// <returns>対訳表データ。</returns>
318         private IList<TranslationDictionary> ExportTranslationDictionaryView(DataGridView view)
319         {
320             IList<TranslationDictionary> dictionaries = new List<TranslationDictionary>();
321             foreach (DataGridViewRow row in view.Rows)
322             {
323                 // 画面での追加用の最終行が空で渡されてくるので無視
324                 if (this.IsEmptyDataGridViewItemsRow(row))
325                 {
326                     continue;
327                 }
328
329                 // その行で対象とする言語を探索、無ければ新規作成
330                 string from = FormUtils.ToString(row.Cells["ColumnFromCode"]);
331                 string to = FormUtils.ToString(row.Cells["ColumnToCode"]);
332                 TranslationDictionary dic
333                     = TranslationDictionary.GetDictionaryNeedCreate(dictionaries, from, to);
334
335                 // 値を格納
336                 TranslationDictionary.Item item = new TranslationDictionary.Item
337                 {
338                     Word = FormUtils.ToString(row.Cells["ColumnToTitle"]),
339                     Alias = FormUtils.ToString(row.Cells["ColumnAlias"])
340                 };
341
342                 string timestamp = FormUtils.ToString(row.Cells["ColumnTimestamp"]);
343                 if (!String.IsNullOrWhiteSpace(timestamp))
344                 {
345                     item.Timestamp = DateTime.Parse(timestamp);
346
347                     // UTCでもなくタイムゾーンでも無い場合、ローカル時刻として設定する
348                     if (item.Timestamp.Value.Kind == DateTimeKind.Unspecified)
349                     {
350                         item.Timestamp = DateTime.SpecifyKind(item.Timestamp.Value, DateTimeKind.Local);
351                     }
352                 }
353
354                 dic[FormUtils.ToString(row.Cells["ColumnFromTitle"])] = item;
355             }
356
357             return dictionaries;
358         }
359         
360         /// <summary>
361         /// 記事の置き換え対訳表の行が空かを判定する。
362         /// </summary>
363         /// <param name="row">対訳表の1行。</param>
364         /// <returns>空の場合<c>true</c>。</returns>
365         private bool IsEmptyDataGridViewItemsRow(DataGridViewRow row)
366         {
367             return String.IsNullOrWhiteSpace(FormUtils.ToString(row.Cells["ColumnFromCode"]))
368                 && String.IsNullOrWhiteSpace(FormUtils.ToString(row.Cells["ColumnFromTitle"]))
369                 && String.IsNullOrWhiteSpace(FormUtils.ToString(row.Cells["ColumnAlias"]))
370                 && String.IsNullOrWhiteSpace(FormUtils.ToString(row.Cells["ColumnToCode"]))
371                 && String.IsNullOrWhiteSpace(FormUtils.ToString(row.Cells["ColumnToTitle"]))
372                 && String.IsNullOrWhiteSpace(FormUtils.ToString(row.Cells["ColumnTimestamp"]));
373         }
374
375         #endregion
376
377         #region 見出しの置き換えタブのイベントのメソッド
378
379         /// <summary>
380         /// 見出しの置き換え対訳表を使用する<see cref="DataGridView"/>の値設定を行う。
381         /// </summary>
382         /// <param name="view">対訳表を表示するビュー。</param>
383         /// <param name="table">対訳表データ。</param>
384         private void ImportTranslationTableView(DataGridView view, TranslationTable table)
385         {
386             // 初期設定以外の場合も想定して最初にクリア
387             view.Columns.Clear();
388
389             // 言語コードを列、語句を行とする。登録されている全言語分の列を作成する
390             foreach (Website site in this.config.Websites)
391             {
392                 this.AddTranslationTableColumn(view.Columns, site.Language.Code, this.GetHeaderLanguage(site.Language));
393             }
394
395             // 各行にデータを取り込み
396             foreach (IDictionary<string, string> record in table)
397             {
398                 // 行を追加しその行を取得
399                 DataGridViewRow row = view.Rows[view.Rows.Add()];
400
401                 foreach (KeyValuePair<string, string> cell in record)
402                 {
403                     // 上で登録した列では足りなかった場合、その都度生成する
404                     if (!view.Columns.Contains(cell.Key))
405                     {
406                         this.AddTranslationTableColumn(view.Columns, cell.Key, cell.Key);
407                     }
408
409                     row.Cells[cell.Key].Value = cell.Value;
410                 }
411             }
412
413             // 列幅をデータ長に応じて自動調整
414             // ※ 常に行ってしまうと、読み込みに時間がかかるため
415             view.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
416         }
417
418         /// <summary>
419         /// 見出しの置き換え対訳表を使用する<see cref="DataGridView"/>からデータを抽出する。
420         /// </summary>
421         /// <param name="view">対訳表を表示するビュー。</param>
422         /// <returns>対訳表データ。</returns>
423         private TranslationTable ExportTranslationTableView(DataGridView view)
424         {
425             TranslationTable table = new TranslationTable();
426             foreach (DataGridViewRow row in view.Rows)
427             {
428                 IDictionary<string, string> record = new SortedDictionary<string, string>();
429                 foreach (DataGridViewCell cell in row.Cells)
430                 {
431                     // 空のセルは格納しない、該当の組み合わせは消える
432                     string value = FormUtils.ToString(cell);
433                     if (!String.IsNullOrWhiteSpace(value))
434                     {
435                         record[cell.OwningColumn.Name] = value;
436                     }
437                 }
438
439                 // 1件もデータが無い行は丸々カットする
440                 if (record.Count > 0)
441                 {
442                     table.Add(record);
443                 }
444             }
445
446             return table;
447         }
448
449         /// <summary>
450         /// 指定された情報を元に見出しの置き換え対訳表の列を追加する。
451         /// </summary>
452         /// <param name="columns">列コレクション。</param>
453         /// <param name="columnName">列名。</param>
454         /// <param name="headerText">列見出し。</param>
455         private void AddTranslationTableColumn(DataGridViewColumnCollection columns, string columnName, string headerText)
456         {
457             columns.Add(columnName, headerText);
458         }
459
460         /// <summary>
461         /// 指定された言語用の表示名を返す。
462         /// </summary>
463         /// <param name="lang">表示言語コード。</param>
464         /// <returns>表示名、無ければ言語コード。</returns>
465         private string GetHeaderLanguage(Language lang)
466         {
467             Language.LanguageName name;
468             if (lang.Names.TryGetValue(
469                 System.Globalization.CultureInfo.CurrentCulture.TwoLetterISOLanguageName, out name))
470             {
471                 if (!String.IsNullOrEmpty(name.Name))
472                 {
473                     return String.Format(Resources.HeadingViewHeaderText, name.Name, lang.Code);
474                 }
475             }
476
477             return lang.Code;
478         }
479
480         #endregion
481
482         #region 言語/サーバータブのイベントのメソッド
483
484         /// <summary>
485         /// 言語コンボボックス変更時の処理。
486         /// </summary>
487         /// <param name="sender">イベント発生オブジェクト。</param>
488         /// <param name="e">発生したイベント。</param>
489         private void ComboBoxLanguuage_SelectedIndexChanged(object sender, EventArgs e)
490         {
491             try
492             {
493                 // 変更前の設定を保存
494                 if (!String.IsNullOrEmpty(this.comboBoxLanguageSelectedText))
495                 {
496                     // 設定が存在しなければ自動生成される
497                     this.SaveChangedValue(this.GetMediaWikiNeedCreate(this.config.Websites, this.comboBoxLanguageSelectedText));
498                 }
499
500                 // 変更後の値に応じて、画面表示を更新
501                 if (!String.IsNullOrEmpty(this.comboBoxLanguage.Text))
502                 {
503                     // 設定が存在しなければ基本的に自動生成されるのでそのまま使用
504                     this.LoadCurrentValue(this.GetMediaWikiNeedCreate(this.config.Websites, this.comboBoxLanguage.Text));
505
506                     // 各入力欄を有効に
507                     this.buttonLanguageRemove.Enabled = true;
508                     this.groupBoxServer.Enabled = true;
509                     this.groupBoxLanguage.Enabled = true;
510
511                     // 現在の選択値を更新
512                     this.comboBoxLanguageSelectedText = this.comboBoxLanguage.Text;
513                 }
514                 else
515                 {
516                     // 各入力欄を無効に
517                     this.buttonLanguageRemove.Enabled = false;
518                     this.groupBoxServer.Enabled = false;
519                     this.groupBoxLanguage.Enabled = false;
520
521                     // 現在の選択値を更新
522                     this.comboBoxLanguageSelectedText = String.Empty;
523                 }
524             }
525             catch (Exception ex)
526             {
527                 // 通常この処理では例外は発生しないはず。想定外のエラー用
528                 FormUtils.ErrorDialog(Resources.ErrorMessageDevelopmentError, ex.Message, ex.StackTrace);
529             }
530         }
531
532         /// <summary>
533         /// 言語の追加ボタン押下時の処理。
534         /// </summary>
535         /// <param name="sender">イベント発生オブジェクト。</param>
536         /// <param name="e">発生したイベント。</param>
537         private void ButtonLunguageAdd_Click(object sender, EventArgs e)
538         {
539             // 言語追加用ダイアログを表示
540             InputLanguageCodeDialog form = new InputLanguageCodeDialog(this.config);
541             form.ShowDialog();
542
543             // 値が登録された場合
544             if (!String.IsNullOrWhiteSpace(form.LanguageCode))
545             {
546                 // 値を一覧・見出しの対訳表に追加、登録した値を選択状態に変更
547                 this.comboBoxLanguage.Items.Add(form.LanguageCode);
548                 this.dataGridViewHeading.Columns.Add(form.LanguageCode, form.LanguageCode);
549                 this.comboBoxLanguage.SelectedItem = form.LanguageCode;
550             }
551         }
552
553         /// <summary>
554         /// 言語の削除ボタン押下時の処理。
555         /// </summary>
556         /// <param name="sender">イベント発生オブジェクト。</param>
557         /// <param name="e">発生したイベント。</param>
558         private void ButtonLanguageRemove_Click(object sender, EventArgs e)
559         {
560             // 表示されている言語を設定から削除する
561             for (int i = this.config.Websites.Count - 1; i >= 0; i--)
562             {
563                 if (this.config.Websites[i].Language.Code == this.comboBoxLanguage.Text)
564                 {
565                     // 万が一複数あれば全て削除
566                     this.config.Websites.RemoveAt(i);
567                 }
568             }
569
570             // コンボボックスからも削除し、表示を更新する
571             this.comboBoxLanguageSelectedText = null;
572             this.comboBoxLanguage.Items.Remove(this.comboBoxLanguage.Text);
573             this.ComboBoxLanguuage_SelectedIndexChanged(sender, e);
574         }
575
576         /// <summary>
577         /// 各名前空間のIDボックスバリデート処理。
578         /// </summary>
579         /// <param name="sender">イベント発生オブジェクト。</param>
580         /// <param name="e">発生したイベント。</param>
581         private void TextBoxNamespace_Validating(object sender, CancelEventArgs e)
582         {
583             // 空か数値のみ許可
584             TextBox box = (TextBox)sender;
585             box.Text = StringUtils.DefaultString(box.Text).Trim();
586             int value;
587             if (!String.IsNullOrEmpty(box.Text) && !int.TryParse(box.Text, out value))
588             {
589                 this.errorProvider.SetError(box, Resources.WarningMessageIgnoreNumericNamespace);
590                 e.Cancel = true;
591             }
592         }
593
594         /// <summary>
595         /// 言語の設定表の行編集時のバリデート処理。
596         /// </summary>
597         /// <param name="sender">イベント発生オブジェクト。</param>
598         /// <param name="e">発生したイベント。</param>
599         private void DataGridViewLanguageName_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
600         {
601             DataGridViewRow row = this.dataGridViewLanguageName.Rows[e.RowIndex];
602
603             // 空行(新規行など)の場合無視
604             if (FormUtils.IsEmptyRow(row))
605             {
606                 return;
607             }
608
609             // 言語コードは必須、またトリムして小文字に変換
610             string code = FormUtils.ToString(row.Cells["ColumnCode"]).Trim().ToLower();
611             row.Cells["ColumnCode"].Value = code;
612             if (String.IsNullOrEmpty(code))
613             {
614                 row.ErrorText = Resources.WarningMessageEmptyCodeColumn;
615                 e.Cancel = true;
616                 return;
617             }
618
619             // 略称を設定する場合、呼称を必須とする
620             if (!String.IsNullOrWhiteSpace(FormUtils.ToString(row.Cells["ColumnShortName"]))
621                 && String.IsNullOrWhiteSpace(FormUtils.ToString(row.Cells["ColumnName"])))
622             {
623                 row.ErrorText = Resources.WarningMessageShortNameColumnOnly;
624                 e.Cancel = true;
625             }
626         }
627
628         /// <summary>
629         /// 言語の設定表バリデート処理。
630         /// </summary>
631         /// <param name="sender">イベント発生オブジェクト。</param>
632         /// <param name="e">発生したイベント。</param>
633         private void DataGridViewLanguageName_Validating(object sender, CancelEventArgs e)
634         {
635             // 言語コードの重複チェック
636             IDictionary<string, int> codeMap = new Dictionary<string, int>();
637             for (int i = 0; i < this.dataGridViewLanguageName.RowCount - 1; i++)
638             {
639                 string code = FormUtils.ToString(this.dataGridViewLanguageName["ColumnCode", i]);
640                 int y;
641                 if (codeMap.TryGetValue(code, out y))
642                 {
643                     // 重複の場合、両方の行にエラーを設定
644                     this.dataGridViewLanguageName.Rows[i].ErrorText = Resources.WarningMessageDuplicateCodeColumn;
645                     this.dataGridViewLanguageName.Rows[y].ErrorText = Resources.WarningMessageDuplicateCodeColumn;
646                     e.Cancel = true;
647                 }
648                 else
649                 {
650                     // それ以外はマップに出現行とともに追加
651                     codeMap[code] = i;
652                 }
653             }
654         }
655
656         #region イベント実装支援用メソッド
657
658         /// <summary>
659         /// コレクションから指定された言語のMediaWikiを取得する。
660         /// 存在しない場合は空のインスタンスを生成、コレクションに追加して返す。
661         /// </summary>
662         /// <param name="collection">翻訳元言語。</param>
663         /// <param name="lang">言語コード。</param>
664         /// <returns>翻訳パターン。存在しない場合は新たに作成した翻訳パターンを返す。</returns>
665         private MediaWiki GetMediaWikiNeedCreate(ICollection<Website> collection, string lang)
666         {
667             // 設定が存在すれば取得した値を返す
668             foreach (Website s in collection)
669             {
670                 if (s.Language.Code == lang)
671                 {
672                     if (s is MediaWiki)
673                     {
674                         return s as MediaWiki;
675                     }
676
677                     // 万が一同じ言語コードで違う型の値があったら上書き
678                     collection.Remove(s);
679                     break;
680                 }
681             }
682
683             // 存在しないか上書きの場合、作成した翻訳パターンをコレクションに追加し、返す
684             MediaWiki site = new MediaWiki(new Language(lang));
685             collection.Add(site);
686             return site;
687         }
688
689         /// <summary>
690         /// 指定されたLanguage設定を画面表示/編集用に読み込む。
691         /// </summary>
692         /// <param name="lang">読込元Language設定。</param>
693         /// <remarks>一部パラメータには初期値が存在するが、格納時に対処するため全て読み込む。</remarks>
694         private void LoadCurrentValue(Language lang)
695         {
696             // 言語情報を読み込み
697             // ※ Bracketは初期値があるパラメータのため、必ず値が返る
698             this.textBoxBracket.Text = lang.Bracket;
699
700             // 呼称の情報を表に設定
701             this.dataGridViewLanguageName.Rows.Clear();
702             foreach (KeyValuePair<string, Language.LanguageName> name in lang.Names)
703             {
704                 int index = this.dataGridViewLanguageName.Rows.Add();
705                 this.dataGridViewLanguageName["ColumnCode", index].Value = name.Key;
706                 this.dataGridViewLanguageName["ColumnName", index].Value = name.Value.Name;
707                 this.dataGridViewLanguageName["ColumnShortName", index].Value = name.Value.ShortName;
708             }
709         }
710
711         /// <summary>
712         /// 指定されたWebsite設定を画面表示/編集用に読み込む。
713         /// </summary>
714         /// <param name="site">読込元Website設定。</param>
715         private void LoadCurrentValue(Website site)
716         {
717             // Languageクラス分の読み込みを行う
718             this.LoadCurrentValue(site.Language);
719
720             // サイト情報を読み込み
721             this.textBoxLocation.Text = site.Location;
722         }
723
724         /// <summary>
725         /// 指定されたMediaWiki設定を画面表示/編集用に読み込む。
726         /// </summary>
727         /// <param name="site">読込元MediaWiki設定。</param>
728         /// <remarks>一部パラメータには初期値が存在するが、格納時に対処するため全て読み込む。</remarks>
729         private void LoadCurrentValue(MediaWiki site)
730         {
731             // Websiteクラス分の読み込みを行う
732             this.LoadCurrentValue((Website)site);
733
734             // MediaWikiクラス分の読み込み
735             this.textBoxExportPath.Text = StringUtils.DefaultString(site.ExportPath);
736             this.textBoxMetaApi.Text = StringUtils.DefaultString(site.MetaApi);
737             this.textBoxTemplateNamespace.Text = site.TemplateNamespace.ToString();
738             this.textBoxCategoryNamespace.Text = site.CategoryNamespace.ToString();
739             this.textBoxFileNamespace.Text = site.FileNamespace.ToString();
740             this.textBoxRedirect.Text = StringUtils.DefaultString(site.Redirect);
741
742             // Template:Documentionは改行区切りのマルチテキストとして扱う
743             StringBuilder b = new StringBuilder();
744             foreach (string s in site.DocumentationTemplates)
745             {
746                 b.Append(s).Append(Environment.NewLine);
747             }
748
749             this.textBoxDocumentationTemplate.Text = b.ToString();
750             this.textBoxDocumentationTemplateDefaultPage.Text = StringUtils.DefaultString(site.DocumentationTemplateDefaultPage);
751             this.textBoxLinkInterwikiFormat.Text = StringUtils.DefaultString(site.LinkInterwikiFormat);
752             this.textBoxLangFormat.Text = StringUtils.DefaultString(site.LangFormat);
753         }
754
755         /// <summary>
756         /// 指定されたLanguage設定に画面上で変更された値の格納を行う。
757         /// </summary>
758         /// <param name="lang">格納先Language設定。</param>
759         /// <remarks>一部パラメータには初期値が存在するため、変更がある場合のみ格納する。</remarks>
760         private void SaveChangedValue(Language lang)
761         {
762             // Bracketは初期値を持つパラメータのため、変更された場合のみ格納する。
763             // ※ この値は前後の空白に意味があるため、Trimしてはいけない
764             string str = StringUtils.DefaultString(this.textBoxBracket.Text);
765             if (str != lang.Bracket)
766             {
767                 lang.Bracket = str;
768             }
769
770             // 表から呼称の情報も保存
771             this.dataGridViewLanguageName.Sort(this.dataGridViewLanguageName.Columns["ColumnCode"], ListSortDirection.Ascending);
772             lang.Names.Clear();
773             for (int y = 0; y < this.dataGridViewLanguageName.RowCount - 1; y++)
774             {
775                 // 値が入ってないとかはガードしているはずだが、一応チェック
776                 string code = FormUtils.ToString(this.dataGridViewLanguageName["ColumnCode", y]).Trim();
777                 if (!String.IsNullOrEmpty(code))
778                 {
779                     Language.LanguageName name = new Language.LanguageName();
780                     name.Name = FormUtils.ToString(this.dataGridViewLanguageName["ColumnName", y]).Trim();
781                     name.ShortName = FormUtils.ToString(this.dataGridViewLanguageName["ColumnShortName", y]).Trim();
782                     lang.Names[code] = name;
783                 }
784             }
785         }
786
787         /// <summary>
788         /// 指定されたWebsite設定に画面上で変更された値の格納を行う。
789         /// </summary>
790         /// <param name="site">格納先Website設定。</param>
791         /// <remarks>Websiteについては特に特殊な処理は無いため全て上書きする。</remarks>
792         private void SaveChangedValue(Website site)
793         {
794             // Languageクラス分の設定を行う
795             this.SaveChangedValue(site.Language);
796
797             // サイト情報を格納
798             site.Location = StringUtils.DefaultString(this.textBoxLocation.Text).Trim();
799         }
800
801         /// <summary>
802         /// 指定されたMediaWiki設定に画面上で変更された値の格納を行う。
803         /// </summary>
804         /// <param name="site">格納先MediaWiki設定。</param>
805         /// <remarks>一部パラメータには初期値が存在するため、変更がある場合のみ格納する。</remarks>
806         private void SaveChangedValue(MediaWiki site)
807         {
808             // Websiteクラス分の設定を行う
809             this.SaveChangedValue((Website)site);
810
811             // 初期値を持つパラメータがあるため、全て変更された場合のみ格納する。
812             // ※ もうちょっと綺麗に書きたかったが、うまい手が思いつかなかったので力技
813             //    MediaWikiクラス側で行わないのは、場合によっては意図的に初期値と同じ値を設定すること
814             //    もありえるから(初期値が変わる可能性がある場合など)。
815             string str = StringUtils.DefaultString(this.textBoxExportPath.Text).Trim();
816             if (str != site.ExportPath)
817             {
818                 site.ExportPath = str;
819             }
820             
821             str = StringUtils.DefaultString(this.textBoxMetaApi.Text).Trim();
822             if (str != site.MetaApi)
823             {
824                 site.MetaApi = str;
825             }
826
827             str = StringUtils.DefaultString(this.textBoxRedirect.Text).Trim();
828             if (str != site.Redirect)
829             {
830                 site.Redirect = str;
831             }
832
833             // Template:Documentionの設定は行ごとに格納
834             // ※ この値は初期値を持たないパラメータ
835             site.DocumentationTemplates.Clear();
836             foreach (string s in StringUtils.DefaultString(this.textBoxDocumentationTemplate.Text).Split('\n'))
837             {
838                 if (!String.IsNullOrWhiteSpace(s))
839                 {
840                     site.DocumentationTemplates.Add(s.Trim());
841                 }
842             }
843
844             str = StringUtils.DefaultString(this.textBoxDocumentationTemplateDefaultPage.Text).Trim();
845             if (str != site.DocumentationTemplateDefaultPage)
846             {
847                 site.DocumentationTemplateDefaultPage = str;
848             }
849
850             str = StringUtils.DefaultString(this.textBoxLinkInterwikiFormat.Text).Trim();
851             if (str != site.LinkInterwikiFormat)
852             {
853                 site.LinkInterwikiFormat = str;
854             }
855
856             str = StringUtils.DefaultString(this.textBoxLangFormat.Text).Trim();
857             if (str != site.LangFormat)
858             {
859                 site.LangFormat = str;
860             }
861
862             // 以下、数値へのparseは事前にチェックしてあるので、ここではチェックしない
863             if (!String.IsNullOrWhiteSpace(this.textBoxTemplateNamespace.Text))
864             {
865                 int num = int.Parse(this.textBoxTemplateNamespace.Text);
866                 if (site.TemplateNamespace != num)
867                 {
868                     site.TemplateNamespace = num;
869                 }
870             }
871
872             if (!String.IsNullOrWhiteSpace(this.textBoxCategoryNamespace.Text))
873             {
874                 int num = int.Parse(this.textBoxCategoryNamespace.Text);
875                 if (site.CategoryNamespace != num)
876                 {
877                     site.CategoryNamespace = num;
878                 }
879             }
880
881             if (!String.IsNullOrWhiteSpace(this.textBoxFileNamespace.Text))
882             {
883                 int num = int.Parse(this.textBoxFileNamespace.Text);
884                 if (site.FileNamespace != num)
885                 {
886                     site.FileNamespace = num;
887                 }
888             }
889         }
890
891         #endregion
892
893         #endregion
894
895         #region その他タブのイベントのメソッド
896         
897         /// <summary>
898         /// キャッシュ有効期限ボックスバリデート処理。
899         /// </summary>
900         /// <param name="sender">イベント発生オブジェクト。</param>
901         /// <param name="e">発生したイベント。</param>
902         private void TextBoxCacheExpire_Validating(object sender, CancelEventArgs e)
903         {
904             // 値が0以上の数値かをチェック
905             this.TextBoxGreaterThanValidating((TextBox)sender, e, 0, Resources.WarningMessageIgnoreCacheExpire);
906         }
907
908         /// <summary>
909         /// リトライ回数ボックスバリデート処理。
910         /// </summary>
911         /// <param name="sender">イベント発生オブジェクト。</param>
912         /// <param name="e">発生したイベント。</param>
913         private void TextBoxMaxConnectRetries_Validating(object sender, CancelEventArgs e)
914         {
915             // 値が0以上の数値かをチェック
916             this.TextBoxGreaterThanValidating((TextBox)sender, e, 0, Resources.WarningMessageIgnoreMaxConnectRetries);
917         }
918
919         /// <summary>
920         /// ウェイト時間ボックスバリデート処理。
921         /// </summary>
922         /// <param name="sender">イベント発生オブジェクト。</param>
923         /// <param name="e">発生したイベント。</param>
924         private void TextBoxConnectRetryTime_Validating(object sender, CancelEventArgs e)
925         {
926             // 値が0以上の数値かをチェック
927             this.TextBoxGreaterThanValidating((TextBox)sender, e, 0, Resources.WarningMessageIgnoreConnectRetryTime);
928         }
929
930         /// <summary>
931         /// ウェブサイトURLクリック時の処理。
932         /// </summary>
933         /// <param name="sender">イベント発生オブジェクト。</param>
934         /// <param name="e">発生したイベント。</param>
935         private void LinkLabelWebsite_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
936         {
937             // リンクを開く
938             System.Diagnostics.Process.Start(((LinkLabel)sender).Text);
939         }
940
941         #region イベント実装支援用メソッド
942
943         /// <summary>
944         /// メッセージのみ差し替え可能なテキストボックス用の値がxx以上の数値か、のバリデート処理。
945         /// </summary>
946         /// <param name="box">イベント発生テキストボックス。</param>
947         /// <param name="e">発生したイベント。</param>
948         /// <param name="num">比較対象の数値。</param>
949         /// <param name="message">バリデートメッセージ。</param>
950         private void TextBoxGreaterThanValidating(TextBox box, CancelEventArgs e, int num, string message)
951         {
952             box.Text = StringUtils.DefaultString(box.Text).Trim();
953             int value;
954             if (!int.TryParse(box.Text, out value) || value < num)
955             {
956                 this.errorProvider.SetError(box, message);
957                 e.Cancel = true;
958             }
959         }
960
961         #endregion
962
963         #endregion
964
965         #region 共通のイベントメソッド
966
967         /// <summary>
968         /// 汎用のエラープロバイダ初期化処理。
969         /// </summary>
970         /// <param name="sender">イベント発生オブジェクト。</param>
971         /// <param name="e">発生したイベント。</param>
972         private void ResetErrorProvider_Validated(object sender, EventArgs e)
973         {
974             this.errorProvider.SetError((Control)sender, null);
975         }
976
977         /// <summary>
978         /// 汎用の行編集時のエラーテキスト初期化処理。
979         /// </summary>
980         /// <param name="sender">イベント発生オブジェクト。</param>
981         /// <param name="e">発生したイベント。</param>
982         private void ResetErrorText_RowValidated(object sender, DataGridViewCellEventArgs e)
983         {
984             ((DataGridView)sender).Rows[e.RowIndex].ErrorText = String.Empty;
985         }
986
987         /// <summary>
988         /// 汎用のテーブルエラーテキスト初期化処理。
989         /// </summary>
990         /// <param name="sender">イベント発生オブジェクト。</param>
991         /// <param name="e">発生したイベント。</param>
992         private void ResetErrorText_Validated(object sender, EventArgs e)
993         {
994             // 全行のエラーメッセージを解除
995             foreach (DataGridViewRow row in ((DataGridView)sender).Rows)
996             {
997                 row.ErrorText = String.Empty;
998             }
999         }
1000
1001         #endregion
1002
1003         #region 内部クラス
1004
1005         /// <summary>
1006         /// 記事の置き換え対訳表の日付並び替え用クラスです。
1007         /// </summary>
1008         /// <remarks>取得日時の降順でソート、空の列は先頭にします。</remarks>
1009         public class TranslationDictionaryViewComparer : System.Collections.IComparer
1010         {
1011             /// <summary>
1012             /// 2行を比較し、一方が他方より小さいか、等しいか、大きいかを示す値を返します。
1013             /// </summary>
1014             /// <param name="y">比較する最初の行です。</param>
1015             /// <param name="x">比較する 2 番目の行。</param>
1016             /// <returns>1以下:xはyより小さい, 0:等しい, 1以上:xはyより大きい</returns>
1017             public int Compare(object y, object x)
1018             {
1019                 string xstr = ObjectUtils.ToString(((DataGridViewRow)x).Cells["ColumnTimestamp"].Value);
1020                 string ystr = ObjectUtils.ToString(((DataGridViewRow)y).Cells["ColumnTimestamp"].Value);
1021                 if (String.IsNullOrWhiteSpace(xstr) && String.IsNullOrWhiteSpace(ystr))
1022                 {
1023                     return 0;
1024                 }
1025                 else if (String.IsNullOrWhiteSpace(xstr))
1026                 {
1027                     return 1;
1028                 }
1029                 else if (String.IsNullOrWhiteSpace(ystr))
1030                 {
1031                     return -1;
1032                 }
1033
1034                 return xstr.CompareTo(ystr);
1035             }
1036         }
1037
1038         #endregion
1039     }
1040 }