-
Notifications
You must be signed in to change notification settings - Fork 3
/
AboutForm.cs
214 lines (189 loc) · 6.87 KB
/
AboutForm.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
using Newtonsoft.Json;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Bing_Wallpaper
{
public partial class AboutForm : Form
{
public AboutForm()
{
InitializeComponent();
}
private Configuration _configuration;
protected override void OnLoad(EventArgs e)
{
HideForm();
base.OnLoad(e);
}
private bool _showing;
private void HideForm()
{
Visible = _showing = ShowInTaskbar = false;
Opacity = 0;
}
private void ShowForm()
{
Visible = _showing = ShowInTaskbar = true;
Opacity = 1;
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Environment.Exit(0);
}
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
if (_showing)
{
HideForm();
}
else
{
ShowForm();
}
}
private void Form1_Load(object sender, EventArgs e)
{
if (File.Exists("config.json"))
{
_configuration = JsonConvert.DeserializeObject<Configuration>(File.ReadAllText("config.json"));
}
else
{
//Load Default Configuration
_configuration = new Configuration
{
Path = $@"{Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)}\Bing Wallpapers\",
ShowNotification = true
};
}
timer1_Tick(null, null);
lblVersion.Text = Assembly.GetEntryAssembly().GetName().Version.ToString(3);
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
toolStripMenuItem1.PerformClick();
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Process.Start("https://github.com/cdemi/Bing-Wallpaper-2.0");
}
private void AboutForm_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
HideForm();
}
private string _description;
private string _detailsUrl;
private string _title;
private async Task<T> getUrl<T>(string url)
{
try
{
using (HttpClient client = new HttpClient())
using (HttpResponseMessage response = await client.GetAsync(url))
using (HttpContent content = response.Content)
{
if (!response.IsSuccessStatusCode)
{
AppLogger.Logger.Warning("Non Success Status Code: {StatusCode} Full Response: {response}", response.StatusCode, response);
}
string result = await content.ReadAsStringAsync();
try
{
return JsonConvert.DeserializeObject<T>(result);
}
catch (Exception ex)
{
AppLogger.Logger.Error(ex, "Error trying to deserialize response: {result}", result);
throw;
}
}
}
catch (Exception ex)
{
AppLogger.Logger.Error(ex, "Error trying to get Content");
throw;
}
}
private async Task<bool> updateWallpaper(bool force = false)
{
try
{
BingImage bingResponse;
string imageUrl;
using (WebClient bingClient = new WebClient())
{
bingResponse = await getUrl<BingImage>("https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=en-US");
imageUrl = $"http://www.bing.com{bingResponse.images.FirstOrDefault()?.url}";
_detailsUrl = bingResponse.images.FirstOrDefault()?.copyrightlink;
_title = bingResponse.images.FirstOrDefault()?.title;
_description = Regex.Match(bingResponse.images.FirstOrDefault()?.copyright, @"(.+?)(\s\(.+?\))").Groups[1].Value;
}
toolStripMenuItem2.Visible = true;
string wallpapersPath = _configuration.Path;
string picturePath = $"{wallpapersPath}\\{bingResponse.images.FirstOrDefault()?.hsh}.jpg";
if (!File.Exists(picturePath) || force)
{
if (!Directory.Exists(wallpapersPath))
{
Directory.CreateDirectory(wallpapersPath);
}
using (WebClient imageClient = new WebClient())
{
imageClient.DownloadFile(imageUrl, picturePath);
}
Wallpaper.Set(picturePath);
if (_configuration.ShowNotification)
{
notifyIcon1.ShowBalloonTip(10000, _title, _description, ToolTipIcon.None);
}
return true;
}
else
{
return false;
}
}
catch (WebException wex)
{
AppLogger.Logger.Error(wex, "Error trying to update wallpaper");
return false;
}
}
private async void timer1_Tick(object sender, EventArgs e)
{
if (!(await updateWallpaper()))
{
timer1.Interval = 600000;
}
else
{
timer1.Interval = 3600000;
}
}
private void notifyIcon1_BalloonTipClicked(object sender, EventArgs e)
{
Process.Start(_detailsUrl);
}
private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
notifyIcon1.ShowBalloonTip(10000, _title, _description, ToolTipIcon.None);
}
private async void toolStripMenuItem3_Click(object sender, EventArgs e)
{
await updateWallpaper(true);
}
private void lblVersion_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Process.Start("https://github.com/cdemi/Bing-Wallpaper-2.0/releases/latest");
}
}
}