-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettingsForm.Designer.cs
More file actions
217 lines (198 loc) · 9.21 KB
/
SettingsForm.Designer.cs
File metadata and controls
217 lines (198 loc) · 9.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
using Microsoft.VisualBasic;
using System;
using System.IO;
using System.Linq;
using System.Windows.Forms;
namespace BlogPingSender
{
/// <summary>
/// 各種設定を行うためのフォームクラス
/// </summary>
public partial class SettingsForm : Form
{
/// <summary>
/// このフォームで編集中の設定情報を保持するプロパティ
/// </summary>
public Settings CurrentSettings { get; private set; }
/// <summary>
/// コンストラクタ
/// </summary>
/// <param name="settings">メインフォームから渡される現在の設定</param>
public SettingsForm(Settings settings)
{
InitializeComponent();
// 渡された設定を直接編集せず、ディープコピー(複製)を作成する。
// これにより、キャンセルボタンが押されたときに変更が破棄される。
CurrentSettings = new Settings
{
MonitoredBlogs = settings.MonitoredBlogs.Select(b => new BlogInfo { BlogTitle = b.BlogTitle, BlogRssUrl = b.BlogRssUrl, LastPostId = b.LastPostId }).ToList(),
PingUrls = settings.PingUrls.ToList(),
CheckIntervalMinutes = settings.CheckIntervalMinutes,
MinimizeToTrayOnClose = settings.MinimizeToTrayOnClose,
StartWithWindows = settings.StartWithWindows,
StartMonitoringOnLaunch = settings.StartMonitoringOnLaunch
};
}
/// <summary>
/// フォームが読み込まれたときのイベント
/// </summary>
private void SettingsForm_Load(object sender, EventArgs e)
{
// CurrentSettingsの内容をUIコントロールに反映させる
LoadBlogsToListBox();
LoadPingsToListBox();
numCheckInterval.Value = CurrentSettings.CheckIntervalMinutes;
chkMinimizeToTray.Checked = CurrentSettings.MinimizeToTrayOnClose;
chkStartWithWindows.Checked = CurrentSettings.StartWithWindows;
chkStartMonitoringOnLaunch.Checked = CurrentSettings.StartMonitoringOnLaunch;
}
/// <summary>
/// ブログリストをUIに表示する
/// </summary>
private void LoadBlogsToListBox()
{
lstBlogs.DataSource = null; // データソースを一旦クリア
lstBlogs.DataSource = CurrentSettings.MonitoredBlogs;
lstBlogs.DisplayMember = "DisplayName"; // 表示するプロパティ名を指定
}
/// <summary>
/// Ping送信先リストをUIに表示する
/// </summary>
private void LoadPingsToListBox()
{
lstPingUrls.DataSource = null; // データソースを一旦クリア
lstPingUrls.DataSource = CurrentSettings.PingUrls;
}
// --- イベントハンドラ ---
private void btnAddBlog_Click(object sender, EventArgs e)
{
// 入力ボックスを表示してURLを受け取る
string newUrl = Interaction.InputBox("追加するブログのRSSフィードURLを入力してください:", "ブログ追加", "http://");
if (!string.IsNullOrWhiteSpace(newUrl))
{
CurrentSettings.MonitoredBlogs.Add(new BlogInfo(newUrl));
LoadBlogsToListBox(); // UIを更新
}
}
private void btnEditBlog_Click(object sender, EventArgs e)
{
if (lstBlogs.SelectedItem is BlogInfo selectedBlog)
{
string newUrl = Interaction.InputBox("ブログのRSSフィードURLを編集してください:", "ブログ編集", selectedBlog.BlogRssUrl);
if (!string.IsNullOrWhiteSpace(newUrl))
{
selectedBlog.BlogRssUrl = newUrl;
LoadBlogsToListBox(); // UIを更新
}
}
}
private void btnDeleteBlog_Click(object sender, EventArgs e)
{
if (lstBlogs.SelectedItem is BlogInfo selectedBlog)
{
if (MessageBox.Show($"{selectedBlog.DisplayName} を削除しますか?", "確認", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
CurrentSettings.MonitoredBlogs.Remove(selectedBlog);
LoadBlogsToListBox(); // UIを更新
}
}
}
private void btnAddPing_Click(object sender, EventArgs e)
{
string newUrl = Interaction.InputBox("追加するPing送信先のURLを入力してください:", "Ping送信先追加", "http://");
if (!string.IsNullOrWhiteSpace(newUrl))
{
CurrentSettings.PingUrls.Add(newUrl);
LoadPingsToListBox(); // UIを更新
}
}
private void btnDeletePing_Click(object sender, EventArgs e)
{
if (lstPingUrls.SelectedItem is string selectedPing)
{
if (MessageBox.Show($"{selectedPing} を削除しますか?", "確認", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
CurrentSettings.PingUrls.Remove(selectedPing);
LoadPingsToListBox(); // UIを更新
}
}
}
/// <summary>
/// OKボタンがクリックされたときのイベント
/// </summary>
private void btnOK_Click(object sender, EventArgs e)
{
// UIコントロールの現在の値をCurrentSettingsに保存
CurrentSettings.CheckIntervalMinutes = (int)numCheckInterval.Value;
CurrentSettings.MinimizeToTrayOnClose = chkMinimizeToTray.Checked;
CurrentSettings.StartWithWindows = chkStartWithWindows.Checked;
CurrentSettings.StartMonitoringOnLaunch = chkStartMonitoringOnLaunch.Checked;
// フォームの結果をOKとして設定し、フォームを閉じる
this.DialogResult = DialogResult.OK;
this.Close();
}
/// <summary>
/// キャンセルボタンがクリックされたときのイベント
/// </summary>
private void btnCancel_Click(object sender, EventArgs e)
{
// フォームの結果をCancelとして設定し、フォームを閉じる
this.DialogResult = DialogResult.Cancel;
this.Close();
}
/// <summary>
/// 設定のエクスポート
/// </summary>
private void btnExport_Click(object sender, EventArgs e)
{
// 現在のUIの状態をメモリ上の設定に反映させてから保存する
CurrentSettings.Save();
using (SaveFileDialog sfd = new SaveFileDialog())
{
sfd.Filter = "設定ファイル (*.json)|*.json|すべてのファイル (*.*)|*.*";
sfd.FileName = "settings.json";
sfd.Title = "設定ファイルをエクスポート";
if (sfd.ShowDialog() == DialogResult.OK)
{
try
{
// 現在の設定ファイルを指定の場所にコピー
File.Copy(Settings.settingsFilePath, sfd.FileName, true);
MessageBox.Show("設定をエクスポートしました。", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show($"エクスポートに失敗しました。\n{ex.Message}", "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
/// <summary>
/// 設定のインポート
/// </summary>
private void btnImport_Click(object sender, EventArgs e)
{
using (OpenFileDialog ofd = new OpenFileDialog())
{
ofd.Filter = "設定ファイル (*.json)|*.json|すべてのファイル (*.*)|*.*";
ofd.Title = "設定ファイルをインポート";
if (ofd.ShowDialog() == DialogResult.OK)
{
try
{
// 選択されたファイルで現在の設定ファイルを上書き
File.Copy(ofd.FileName, ofd.FileName, true);
MessageBox.Show("設定をインポートしました。\n画面に設定を再読み込みします。", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
// 画面にインポートした設定を再読み込みして反映させる
CurrentSettings = Settings.Load();
SettingsForm_Load(sender, e); // Loadイベントを再度呼び出してUIを更新
}
catch (Exception ex)
{
MessageBox.Show($"インポートに失敗しました。\n{ex.Message}", "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
}
}