Winify – Blame information for rev 14

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 using System;
7 office 2 using System.Collections.Specialized;
1 office 3 using System.ComponentModel;
4 using System.Configuration;
4 office 5 using System.Diagnostics;
6 using System.IO;
7 using System.Threading.Tasks;
1 office 8 using System.Windows.Forms;
9 using AutoUpdaterDotNET;
10 using Winify.Gotify;
11 using Winify.Properties;
8 office 12 using Winify.Servers.Serialization;
1 office 13  
14 namespace Winify
15 {
16 public partial class Form1 : Form
17 {
18 #region Private Delegates, Events, Enums, Properties, Indexers and Fields
19  
8 office 20 private readonly global::Servers.Servers _servers;
4 office 21  
14 office 22 private readonly TaskScheduler _taskScheduler;
23  
1 office 24 private AboutForm _aboutForm;
25  
7 office 26 private GotifyConnectionManager _gotifyConnectionManager;
1 office 27  
11 office 28 private NotificationManager _notificationManager;
29  
1 office 30 private SettingsForm _settingsForm;
31  
32 #endregion
33  
34 #region Constructors, Destructors and Finalizers
35  
36 public Form1()
37 {
38 InitializeComponent();
39  
40 AutoUpdater.Start("http://winify.grimore.org/update/winify.xml");
41  
42 // Upgrade settings if required.
43 if (!ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).HasFile)
44 {
45 Settings.Default.Upgrade();
46 }
47  
48 // Bind to settings changed event.
49 Settings.Default.SettingsLoaded += Default_SettingsLoaded;
50 Settings.Default.SettingsSaving += Default_SettingsSaving;
51 Settings.Default.PropertyChanged += Default_PropertyChanged;
52  
8 office 53 _servers = new global::Servers.Servers();
7 office 54 _servers.Server.CollectionChanged += Server_CollectionChanged;
1 office 55  
14 office 56 _taskScheduler = TaskScheduler.FromCurrentSynchronizationContext();
11 office 57  
14 office 58 _notificationManager = new NotificationManager(_taskScheduler);
59  
7 office 60 _gotifyConnectionManager = new GotifyConnectionManager(_servers);
11 office 61 _gotifyConnectionManager.GotifyNotification += GotifyConnectionManager_GotifyNotification;
7 office 62  
14 office 63 LoadServers().ContinueWith(async task =>
4 office 64 {
14 office 65 var restoredServers = await task;
4 office 66  
7 office 67 foreach (var server in restoredServers.Server)
68 {
69 _servers.Server.Add(server);
70 }
4 office 71 });
1 office 72 }
73  
74 /// <summary>
75 /// Clean up any resources being used.
76 /// </summary>
77 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
78 protected override void Dispose(bool disposing)
79 {
80 if (disposing && components != null)
81 {
7 office 82 _servers.Server.CollectionChanged -= Server_CollectionChanged;
83  
84 Settings.Default.SettingsLoaded -= Default_SettingsLoaded;
85 Settings.Default.SettingsSaving -= Default_SettingsSaving;
86 Settings.Default.PropertyChanged -= Default_PropertyChanged;
87  
11 office 88 _gotifyConnectionManager.GotifyNotification -= GotifyConnectionManager_GotifyNotification;
7 office 89 _gotifyConnectionManager?.Dispose();
90 _gotifyConnectionManager = null;
91  
11 office 92 _notificationManager?.Dispose();
93 _notificationManager = null;
94  
1 office 95 components.Dispose();
96 }
97  
98 base.Dispose(disposing);
99 }
100  
101 #endregion
102  
103 #region Event Handlers
104  
11 office 105 private void GotifyConnectionManager_GotifyNotification(object sender, GotifyNotificationEventArgs e)
106 {
107 _notificationManager.ShowNotification(e);
108 }
109  
7 office 110 private async void Server_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
111 {
112 switch (await ServersSerialization.Serialize(_servers, Constants.ServersFile))
113 {
8 office 114 case ServersSerializationFailure serializationFailure:
7 office 115 Debug.WriteLine(serializationFailure.Exception.Message);
116 break;
117 }
118 }
119  
1 office 120 private static void Default_PropertyChanged(object sender, PropertyChangedEventArgs e)
121 {
122 Settings.Default.Save();
123 }
124  
125 private static void Default_SettingsSaving(object sender, CancelEventArgs e)
126 {
127 }
128  
129 private static void Default_SettingsLoaded(object sender, SettingsLoadedEventArgs e)
130 {
131 }
132  
133 private void SettingsToolStripMenuItem_Click(object sender, EventArgs e)
134 {
135 if (_settingsForm != null)
136 {
137 return;
138 }
139  
4 office 140 _settingsForm = new SettingsForm(_servers);
1 office 141 _settingsForm.Closing += SettingsForm_Closing;
142 _settingsForm.Show();
143 }
144  
145 private void SettingsForm_Closing(object sender, CancelEventArgs e)
146 {
5 office 147 if (_settingsForm == null)
1 office 148 {
149 return;
150 }
6 office 151  
1 office 152 _settingsForm.Closing -= SettingsForm_Closing;
153 _settingsForm.Dispose();
154 _settingsForm = null;
155 }
156  
157 private void AboutToolStripMenuItem_Click(object sender, EventArgs e)
158 {
159 if (_aboutForm != null)
160 {
161 return;
162 }
163  
164 _aboutForm = new AboutForm();
165 _aboutForm.Closing += AboutForm_Closing;
166 _aboutForm.Show();
167 }
168  
169 private void AboutForm_Closing(object sender, CancelEventArgs e)
170 {
171 if (_aboutForm == null)
172 {
173 return;
174 }
175  
176 _aboutForm.Closing -= AboutForm_Closing;
177 _aboutForm.Dispose();
178 _aboutForm = null;
179 }
180  
181 private void QuitToolStripMenuItem_Click(object sender, EventArgs e)
182 {
183 Application.Exit();
184 }
185  
9 office 186 private void UpdateToolStripMenuItem_Click(object sender, EventArgs e)
187 {
188 AutoUpdater.Start("http://winify.grimore.org/update/winify.xml");
189 }
190  
1 office 191 #endregion
4 office 192  
193 #region Private Methods
194  
8 office 195 private static async Task<global::Servers.Servers> LoadServers()
4 office 196 {
197 if (!Directory.Exists(Constants.UserApplicationDirectory))
198 {
199 Directory.CreateDirectory(Constants.UserApplicationDirectory);
200 }
201  
202 var deserializationResult = await ServersSerialization.Deserialize(Constants.ServersFile);
203  
204 switch (deserializationResult)
205 {
8 office 206 case ServersSerializationSuccess serializationSuccess:
4 office 207 return serializationSuccess.Servers;
208  
209 default:
8 office 210 return new global::Servers.Servers();
4 office 211 }
212 }
213  
214 #endregion
1 office 215 }
216 }