-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathSysTrayApplicationContext.cs
221 lines (207 loc) · 7.8 KB
/
SysTrayApplicationContext.cs
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
218
219
220
221
using System;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Squirrel;
using WallpaperChange.Settings;
using Timer = System.Threading.Timer;
namespace WallpaperChange
{
internal class SysTrayApplicationContext : ApplicationContext
{
private const string CheckForApplicationUpdates = "Check for Updates";
private const string UpdateTheApplication = "Apply Updates";
private readonly object _syncLock = new object();
private readonly string[] _updateUrls =
{
"http://tamarau.com/WallpaperChange",
"http://192.168.1.9/WallpaperChange"
};
private ToolStripMenuItem _btnAbout;
private ToolStripMenuItem _btnSettings;
private ToolStripMenuItem _btnStop;
private ToolStripMenuItem _btnUpdates;
private readonly TimeSpan _checkUpdatePeriod = TimeSpan.FromDays(1);
private readonly TimeSpan _checkWallpaperPeriod = TimeSpan.FromSeconds(10);
private ContextMenuStrip _contextMenuStrip1;
private FileAtTime _currentFileAtTime;
private NotifyIcon _notifyIcon1;
private Timer _updateTimer;
private WallpaperChanger _wallpaperChanger;
private Timer _wallpaperTimer;
private IContainer components;
public SysTrayApplicationContext()
{
InitializeComponent();
}
protected override void Dispose(bool disposing)
{
if (disposing)
components?.Dispose();
base.Dispose(disposing);
}
private void InitializeComponent()
{
components = new Container();
_notifyIcon1 = new NotifyIcon(components);
_contextMenuStrip1 = new ContextMenuStrip(components);
_btnAbout = new ToolStripMenuItem();
_btnStop = new ToolStripMenuItem();
_btnSettings = new ToolStripMenuItem();
_btnUpdates = new ToolStripMenuItem();
_contextMenuStrip1.SuspendLayout();
_notifyIcon1.ContextMenuStrip = _contextMenuStrip1;
var imgStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("WallpaperChange.favicon.ico");
if (imgStream != null)
{
var ico = new Icon(imgStream);
_notifyIcon1.Icon = ico;
}
_notifyIcon1.Tag = "WallpaperChange";
_notifyIcon1.Text = @"WallpaperChange";
_notifyIcon1.Visible = true;
_contextMenuStrip1.Items.AddRange(new ToolStripItem[] {_btnAbout, _btnUpdates, _btnSettings, _btnStop});
_contextMenuStrip1.Name = "_contextMenuStrip1";
_contextMenuStrip1.Size = new Size(176, 76);
_btnAbout.Name = "_btnAbout";
_btnAbout.AutoSize = true;
_btnAbout.Text = @"About";
_btnAbout.Click += About_OnClick;
_btnStop.Name = "_btnStop";
_btnStop.AutoSize = true;
_btnStop.Text = @"Stop";
_btnStop.Click += Stop_OnClick;
_btnSettings.Name = "_btnSettings";
_btnSettings.Text = @"Settings";
_btnSettings.AutoSize = true;
_btnSettings.Click += Settings_OnClick;
_btnUpdates.Name = "_btnUpdates";
_btnUpdates.Text = CheckForApplicationUpdates;
_btnUpdates.AutoSize = true;
_btnUpdates.Click += Updates_OnClick;
_contextMenuStrip1.ResumeLayout(false);
_wallpaperTimer = new Timer(WallpaperTimerElapsed, null, _checkWallpaperPeriod, Timeout.InfiniteTimeSpan);
_updateTimer = new Timer(
s =>
{
var u = CheckForUpdates();
u.Wait();
},
null,
TimeSpan.FromSeconds(30),
Timeout.InfiniteTimeSpan);
}
private async void Updates_OnClick(object sender, EventArgs e)
{
var errors = 0;
_btnUpdates.Enabled = false;
foreach (var updateUrl in _updateUrls)
{
try
{
var btn = (ToolStripMenuItem) sender;
if (btn.Text == CheckForApplicationUpdates)
{
var updates = await CheckForUpdates();
if (!updates) MessageBox.Show(@"Your app is up to date.");
}
if (btn.Text != UpdateTheApplication) return;
using (var updateManager = new UpdateManager(updateUrl))
{
await updateManager.UpdateApp();
MessageBox.Show(@"WallpaperChange just got updated. Restarting...", @"Huzzah!");
}
UpdateManager.RestartApp();
break;
}
catch (Exception ex)
{
errors++;
if (errors > 1) MessageBox.Show($@"There was an error updating the app: {ex.Message}.");
}
finally
{
_btnUpdates.Enabled = true;
}
}
}
private static void About_OnClick(object sender, EventArgs e)
{
new AboutBox().ShowDialog();
}
private void WallpaperTimerElapsed(object sender)
{
try
{
lock (_syncLock)
{
if (_wallpaperChanger != null)
return;
_wallpaperChanger = new WallpaperChanger();
}
_currentFileAtTime = _wallpaperChanger.Start(_currentFileAtTime);
_wallpaperChanger = null;
}
finally
{
_wallpaperTimer.Change(_checkWallpaperPeriod, Timeout.InfiniteTimeSpan);
}
}
private async Task<bool> CheckForUpdates()
{
var errors = 0;
var updates = false;
foreach (var url in _updateUrls)
{
try
{
using (var updateManager = new UpdateManager(url))
{
var updateInfo = await updateManager.CheckForUpdate();
if (updateInfo == null || !updateInfo.ReleasesToApply.Any())
{
_btnUpdates.Text = CheckForApplicationUpdates;
}
else
{
_btnUpdates.Text = UpdateTheApplication;
updates = true;
}
}
UserSettings.Load().HandleStartupShortcut();
break;
}
catch (Exception ex)
{
errors++;
if (errors > 1) MessageBox.Show($@"There was an error checking for updates: {ex.Message}.");
}
finally
{
_updateTimer.Change(_checkUpdatePeriod, Timeout.InfiniteTimeSpan);
}
}
return updates;
}
private void Stop_OnClick(object sender, EventArgs e)
{
_updateTimer.Dispose();
lock (_syncLock)
{
_wallpaperTimer.Dispose();
}
Application.Exit();
}
private static void Settings_OnClick(object sender, EventArgs e)
{
var settingsForm = SettingsForm.GetInstance();
settingsForm.Show();
if (!settingsForm.Visible)
settingsForm.BringToFront();
}
}
}