Winify – Blame information for rev 48

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 using System;
4 office 2 using System.Linq;
30 office 3 using System.Threading;
1 office 4 using System.Windows.Forms;
15 office 5 using Announcements;
6 office 6 using Servers;
1 office 7 using Winify.Utilities;
48 office 8 using static System.Windows.Forms.VisualStyles.VisualStyleElement;
1 office 9  
25 office 10 namespace Winify.Settings
1 office 11 {
12 public partial class SettingsForm : Form
13 {
25 office 14 #region Public Events & Delegates
15  
16 public event EventHandler<SettingsSavedEventArgs> Save;
17  
18 #endregion
19  
4 office 20 #region Private Delegates, Events, Enums, Properties, Indexers and Fields
21  
21 office 22 private readonly BindingSource _announcementBindingSource;
15 office 23  
21 office 24 private readonly Announcements.Announcements _announcements;
25  
26 private readonly BindingSource _serverBindingSource;
27  
30 office 28 private readonly Servers.Servers _servers;
4 office 29  
21 office 30 private Announcement _announcement;
31  
32 private Server _server;
30 office 33 private readonly MainForm _mainForm;
34 private readonly CancellationToken _cancellationToken;
44 office 35 private readonly BindingSource _configurationBindingSource;
21 office 36  
4 office 37 #endregion
38  
1 office 39 #region Constructors, Destructors and Finalizers
40  
30 office 41 public SettingsForm()
1 office 42 {
43 InitializeComponent();
44 office 44  
45 Utilities.WindowState.FormTracker.Track(this);
1 office 46 }
47  
30 office 48 public SettingsForm(MainForm mainForm) : this()
4 office 49 {
30 office 50 _mainForm = mainForm;
51 }
52  
53 public SettingsForm(MainForm mainForm, Servers.Servers servers, Announcements.Announcements announcements,
54 CancellationToken cancellationToken) : this(mainForm)
55 {
4 office 56 _servers = servers;
21 office 57 _server = _servers.Server.FirstOrDefault() ?? new Server();
30 office 58 _cancellationToken = cancellationToken;
7 office 59  
21 office 60 _serverBindingSource = new BindingSource();
61 _serverBindingSource.DataSource = _server;
44 office 62 _configurationBindingSource = new BindingSource();
63 _configurationBindingSource.DataSource = _mainForm.Configuration;
21 office 64  
44 office 65 checkBox1.DataBindings.Add(new Binding("Checked", _configurationBindingSource,
66 nameof(_mainForm.Configuration.LaunchOnBoot), true, DataSourceUpdateMode.OnPropertyChanged));
67  
68 checkBox2.DataBindings.Add(new Binding("Checked", _configurationBindingSource,
69 nameof(_mainForm.Configuration.IgnoreSelfSignedCertificates), true, DataSourceUpdateMode.OnPropertyChanged));
70  
21 office 71 serverNameTextBox.DataBindings.Add(new Binding("Text", _serverBindingSource, nameof(_server.Name), true,
72 DataSourceUpdateMode.OnPropertyChanged));
73 serverUrlTextBox.DataBindings.Add(new Binding("Text", _serverBindingSource, nameof(_server.Url), true,
74 DataSourceUpdateMode.OnPropertyChanged));
75 serverUsernameTextBox.DataBindings.Add(new Binding("Text", _serverBindingSource, nameof(_server.Username),
76 true,
77 DataSourceUpdateMode.OnPropertyChanged));
78 serverPasswordTextBox.DataBindings.Add(new Binding("Text", _serverBindingSource, nameof(_server.Password),
79 true,
80 DataSourceUpdateMode.OnPropertyChanged));
81  
7 office 82 listBox1.DataSource = _servers.Server;
83 listBox1.DisplayMember = "Name";
84 listBox1.DataBindings.Add(new Binding("Text", _servers.Server, "Name", true,
85 DataSourceUpdateMode.OnPropertyChanged));
15 office 86  
21 office 87 _announcements = announcements;
88 _announcement = _announcements.Announcement.FirstOrDefault() ?? new Announcement();
89  
90 _announcementBindingSource = new BindingSource();
91 _announcementBindingSource.DataSource = _announcement;
92  
93 appIdTextBox.DataBindings.Add(new Binding("Text", _announcementBindingSource, nameof(_announcement.AppId),
94 true,
95 DataSourceUpdateMode.OnPropertyChanged));
96 lingerTimeTextBox.DataBindings.Add(new Binding("Text", _announcementBindingSource,
97 nameof(_announcement.LingerTime), true,
98 DataSourceUpdateMode.OnPropertyChanged));
99  
100 listBox2.DataSource = _announcements.Announcement;
15 office 101 listBox2.DisplayMember = "Id";
21 office 102 listBox2.DataBindings.Add(new Binding("Text", _announcements.Announcement, "Id", true,
15 office 103 DataSourceUpdateMode.OnPropertyChanged));
4 office 104 }
105  
1 office 106 #endregion
107  
108 #region Event Handlers
109  
110 private void CheckBox1_CheckedChanged(object sender, EventArgs e)
111 {
30 office 112 _mainForm.Configuration.LaunchOnBoot = ((CheckBox)sender).Checked;
1 office 113  
30 office 114 _mainForm.ChangedConfigurationContinuation.Schedule(TimeSpan.FromSeconds(1),
115 async () => { await _mainForm.SaveConfiguration(); }, _cancellationToken);
116  
117 Miscellaneous.LaunchOnBootSet(_mainForm.Configuration.LaunchOnBoot);
1 office 118 }
119  
4 office 120 private void Button1_Click(object sender, EventArgs e)
121 {
21 office 122 var server = new Server();
48 office 123 server.Name = "New Server";
4 office 124  
21 office 125 _servers.Server.Add(server);
48 office 126 _serverBindingSource.DataSource = server;
25 office 127  
43 office 128 Save?.Invoke(this, new SettingsSavedEventArgs(_servers, _announcements));
129  
25 office 130 listBox1.SelectedItem = server;
4 office 131 }
132  
133 private void Button2_Click(object sender, EventArgs e)
134 {
48 office 135 var index = -1;
136  
137 if (listBox1.SelectedItem is Server server)
138 {
139 index = listBox1.Items.IndexOf(server);
140 _servers.Server.Remove(server);
141 }
142  
143 if (index >= listBox1.Items.Count)
144 {
145 --index;
146 }
147  
148 listBox1.SelectedIndex = index;
4 office 149 }
150  
151 private void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
152 {
48 office 153  
28 office 154 var listBox = (ListBox)sender;
48 office 155 if (listBox.SelectedIndex == -1)
156 {
157 _serverBindingSource.DataSource = new
158 { Name = string.Empty, Url = string.Empty, Username = string.Empty, Password = string.Empty };
159 return;
160 }
4 office 161  
21 office 162 if (listBox.SelectedItem is Server server)
4 office 163 {
21 office 164 _server = server;
4 office 165  
21 office 166 _serverBindingSource.DataSource = _server;
4 office 167 }
168 }
169  
15 office 170 private void Button3_Click(object sender, EventArgs e)
171 {
44 office 172 if (listBox2.SelectedItem is Announcement announcement) _announcements.Announcement.Remove(announcement);
15 office 173 }
174  
175 private void Button4_Click(object sender, EventArgs e)
176 {
21 office 177 var announcement = new Announcement();
44 office 178 if (int.TryParse(appIdTextBox.Text, out var appId)) announcement.AppId = appId;
15 office 179  
44 office 180 if (int.TryParse(lingerTimeTextBox.Text, out var lingerTime)) announcement.LingerTime = lingerTime;
43 office 181  
21 office 182 _announcements.Announcement.Add(announcement);
25 office 183  
43 office 184 Save?.Invoke(this, new SettingsSavedEventArgs(_servers, _announcements));
185  
25 office 186 listBox2.SelectedItem = announcement;
15 office 187 }
188  
189 private void ListBox2_SelectedIndexChanged(object sender, EventArgs e)
190 {
28 office 191 var listBox = (ListBox)sender;
15 office 192  
21 office 193 if (listBox.SelectedItem is Announcement announcement)
15 office 194 {
21 office 195 _announcement = announcement;
15 office 196  
21 office 197 _announcementBindingSource.DataSource = _announcement;
15 office 198 }
199 }
200  
25 office 201 private void Button6_Click(object sender, EventArgs e)
202 {
203 Close();
204 }
205  
206 private void Button5_Click(object sender, EventArgs e)
207 {
208 Save?.Invoke(this, new SettingsSavedEventArgs(_servers, _announcements));
30 office 209  
25 office 210 Close();
211 }
212  
30 office 213 private void SettingsForm_Load(object sender, EventArgs e)
214 {
215 checkBox1.Checked = _mainForm.Configuration.LaunchOnBoot;
216 }
217  
1 office 218 #endregion
44 office 219  
220 private void CheckBox2_CheckedChanged(object sender, EventArgs e)
221 {
222 _mainForm.Configuration.IgnoreSelfSignedCertificates = ((CheckBox)sender).Checked;
223  
224 _mainForm.ChangedConfigurationContinuation.Schedule(TimeSpan.FromSeconds(1),
225 async () => { await _mainForm.SaveConfiguration(); }, _cancellationToken);
226 }
1 office 227 }
228 }