/
Form1.cs
346 lines (283 loc) · 12.3 KB
/
Form1.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using WordPressSharp;
using WordPressSharp.Models;
using System.Diagnostics;
using System.Configuration;
using System.Collections.Specialized;
using System.Windows.Threading;
// http://stackoverflow.com/questions/1766610/how-to-store-int-array-in-application-settings
namespace WPUpCheck
{
public partial class Form1 : Form
{
private double timerTickCount = 0;
private DispatcherTimer timer;
public Form1()
{
InitializeComponent();
comboBox1.Items.Add("1");
comboBox1.Items.Add("4");
comboBox1.Items.Add("12");
comboBox1.Items.Add("24");
// Set the images to blank
imgCore.Visible = false;
imgPlugins.Visible = false;
imgThemes.Visible = false;
timer = new DispatcherTimer();
timer.Interval = new TimeSpan(0, 0, 1); // will 'tick' once every second
timer.Tick += new EventHandler(Timer_Tick);
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
Debug.Write("Local user config path: {0}", config.FilePath);
Debug.Write("Config file: " + AppDomain.CurrentDomain.SetupInformation.ConfigurationFile.ToString() + "\n");
if (Properties.Settings.Default["waittime"].ToString() != "0") // FIXME: how to test to see if waittime is set for first run?
{
Debug.Write("Wait time loaded from properties: " + Properties.Settings.Default["waittime"].ToString() + "\n");
comboBox1.SelectedItem = Properties.Settings.Default["waittime"].ToString();
}
else
{
Debug.Write("Setting default waittime\n");
comboBox1.SelectedItem = "24";
}
if (Properties.Settings.Default.sitesList == null)
{
Debug.Write("BaseURL not set, aborting!\n");
button1.Enabled = false;
MessageBox.Show("Can't proceed until you've set a site - see Options menu");
}
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
checkUpdates();
}
private Int32 getWaitTime()
{
var waittime = Int32.Parse(comboBox1.GetItemText(comboBox1.SelectedItem));
return waittime;
}
async private void checkUpdates()
{
timer.Start();
// Set the images to blank
imgCore.Visible = false;
imgPlugins.Visible = false;
imgThemes.Visible = false;
progressBar.Style = ProgressBarStyle.Continuous;
progressBar.Maximum = 100;
progressBar.Value = 5;
string BaseUrl = Properties.Settings.Default.sitesList[0].BaseUrl.ToString();
string Username = Properties.Settings.Default.sitesList[0].Username.ToString();
string Password = Properties.Settings.Default.sitesList[0].Password.ToString();
corecheck.Text = plugincheck.Text = themecheck.Text = "Checking...";
FormOptions Options = new FormOptions();
// WordPressSharp: https://github.com/abrudtkuhl/WordPressSharp
using (var client = new WordPressClient(new WordPressSiteConfig
{
BaseUrl = BaseUrl,
Username = Username,
Password = Options.DecryptString(Password),
BlogId = 1
}))
{
//var id = Convert.ToInt32(client.NewPost(post));
//var username = Convert.ToString(client.GetUser(1).Nicename);
//var plugins = Convert.ToString(client.GetPluginUpdateCheck());
// Close off the Options object, only needed to Decrypt Password
Options = null;
var thereAreUpdates = false;
progressBar.Value = 40;
var response = client.GetAllUpdatesCheck();
if (response.core)
{
corecheck.Text = "Core updates!";
thereAreUpdates = true;
imgCore.Image = Properties.Resources.error;
imgCore.Visible = true;
}
else
{
corecheck.Text = "No updates for Core.";
imgCore.Image = Properties.Resources.check;
imgCore.Visible = true;
}
if (response.plugins)
{
plugincheck.Text = "Plugin Updates!";
thereAreUpdates = true;
imgPlugins.Image = Properties.Resources.error;
imgPlugins.Visible = true;
}
else
{
plugincheck.Text = "No updates for Plugins.";
imgPlugins.Image = Properties.Resources.check;
imgPlugins.Visible = true;
}
if (response.themes)
{
themecheck.Text = "Theme updates!";
thereAreUpdates = true;
imgThemes.Image = Properties.Resources.error;
imgThemes.Visible = true;
}
else
{
themecheck.Text = "No updates for Themes.";
imgThemes.Image = Properties.Resources.check;
imgThemes.Visible = true;
}
if (thereAreUpdates == true)
{
//if (this.WindowState == FormWindowState.Minimized)
{
this.notifyIcon1.BalloonTipText = "There are some updates! Double click me to check the details.";
this.notifyIcon1.BalloonTipTitle = "Updates!";
this.notifyIcon1.Visible = true;
this.notifyIcon1.ShowBalloonTip(3000);
}
}
progressBar.Value = 100;
//var plugins = Convert.ToString(client.GetAllUpdatesCheck());
//plugincheck.Text = username;
//plugincheck.Text = plugins;
// Figure out next run time
var numHours = Int32.Parse(comboBox1.GetItemText(comboBox1.SelectedItem));
var nextTime = DateTime.Now.AddHours(numHours);
//var nextTime = DateTime.Now.AddMinutes(numHours); // FIXME!! should be hours
labelNextRun.Text = nextTime.ToString();
timerTickCount = DateTimeToUnixTimestamp(nextTime) - DateTimeToUnixTimestamp(DateTime.Now);
await Task.Delay(getWaitTime() * 3600 * 1000);
//await Task.Delay(getWaitTime() * 60 * 1000);
checkUpdates();
}
}
private void Timer_Tick(object sender, EventArgs e)
{
timerTickCount = timerTickCount - 1;
countdownBox.Text = "Next run in: " + Math.Round(timerTickCount).ToString() + " seconds.";
}
// http://stackoverflow.com/questions/249760/how-to-convert-unix-timestamp-to-datetime-and-vice-versa
public static double DateTimeToUnixTimestamp(DateTime dateTime)
{
return (dateTime - new DateTime(1970, 1, 1).ToLocalTime()).TotalSeconds;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
// http://stackoverflow.com/questions/453161/best-practice-to-save-application-settings-in-a-windows-forms-application
Properties.Settings.Default["waittime"] = getWaitTime();
Properties.Settings.Default.Save();
Debug.Write("Saved waittime default to " + getWaitTime() + "\n");
}
private void wordPressSitesToolStripMenuItem_Click(object sender, EventArgs e)
{
FormOptions Options = new FormOptions();
Options.ShowDialog();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.WindowState = FormWindowState.Normal;
this.ShowInTaskbar = true;
this.Show();
}
private void Form1_Resize(object sender, EventArgs e)
{
// http://stackoverflow.com/questions/7625421/minimize-app-to-system-tray
if (this.WindowState == FormWindowState.Minimized)
{
notifyIcon1.Visible = true;
//notifyIcon1.ShowBalloonTip(3000);
this.ShowInTaskbar = false;
// http://stackoverflow.com/questions/3181594/handling-a-click-over-a-balloon-tip-displayed-with-trayicons-showballoontip
notifyIcon1.BalloonTipClicked += new EventHandler(notifyIcon_BalloonTipClicked);
}
}
void notifyIcon_BalloonTipClicked(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Normal;
this.ShowInTaskbar = true;
this.Show();
}
private void progressBar_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
if (Properties.Settings.Default.sitesList != null && Properties.Settings.Default.sitesList.Count() > 0)
{
/*
targetSiteLabel.Text = Properties.Settings.Default["wpUrl"].ToString();
LinkLabel.Link link = new LinkLabel.Link();
link.LinkData = Properties.Settings.Default["wpUrl"].ToString();
targetSiteLabel.Links.Add(link);
*/
UpdateCurrentSiteLabel(Properties.Settings.Default.sitesList[0].BaseUrl.ToString(), Properties.Settings.Default.sitesList[0].BaseUrl.ToString());
}
}
private void targetSiteLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
string url;
if (e.Link.LinkData != null)
{
url = e.Link.LinkData.ToString();
Debug.Write("Opening URL" + url + "\n");
ProcessStartInfo sInfo = new ProcessStartInfo(url);
Process.Start(sInfo);
}
}
public void UpdateCurrentSiteLabel(string txt, string url)
{
if (txt != "")
{
targetSiteLabel.Text = txt;
//targetSiteLabel.Text = "http://example.com";
LinkLabel.Link link = new LinkLabel.Link();
link.LinkData = url + "/wp-admin/";
targetSiteLabel.Links.RemoveAt(0); // FIXME: leaving this out causes a weird exception.
targetSiteLabel.Links.Add(link);
}
}
private void optionsToolStripMenuItem1_Click(object sender, EventArgs e)
{
FormOptions Options = new FormOptions();
Options.FormClosing += new FormClosingEventHandler(FormOptions_Close);
Options.ShowDialog();
}
void FormOptions_Close(object sender, FormClosingEventArgs e)
{
if (Properties.Settings.Default.sitesList != null && Properties.Settings.Default.sitesList.Count() > 0)
{
UpdateCurrentSiteLabel(Properties.Settings.Default.sitesList[0].BaseUrl.ToString(), Properties.Settings.Default.sitesList[0].BaseUrl.ToString());
button1.Enabled = true;
}
}
private void pictureBox1_Click_1(object sender, EventArgs e)
{
}
private void optionsToolStripMenuItem2_Click(object sender, EventArgs e)
{
FormOptions Options = new FormOptions();
Options.FormClosing += new FormClosingEventHandler(FormOptions_Close);
Options.ShowDialog();
}
private void imgCore_Click(object sender, EventArgs e)
{
//Debug.Write(
//imgCore.Image = Properties.Resources.check;
}
}
}