-
Notifications
You must be signed in to change notification settings - Fork 1
/
Form1.cs
272 lines (247 loc) · 10.4 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
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Media;
using System.Net;
using System.Reflection;
using System.Threading;
using System.Windows.Forms;
namespace VolnovNotificator
{
public partial class Form1 : Form
{
private CreditsForm _crefitsForm = new CreditsForm();
private AppSettingsWrapper _appSettingsWrapper = new AppSettingsWrapper("settings.cnf");
KeyValuePair<string, string> _lastMessagesValuePair;
public Form1()
{
InitializeComponent();
CheckNewAppVersion();
if (File.Exists("settings.cnf"))
LoadAppSettings();
else
FirstLoadApp();
}
private void FirstLoadApp()
{
_appSettingsWrapper.EditRecord("sound", "off");
if (SetAppAutorunValue(true))
_appSettingsWrapper.EditRecord("autorun", "on");
else
_appSettingsWrapper.EditRecord("autorun", "off");
firstLoadAppBw.RunWorkerAsync();
}
private void LoadAppSettings()
{
var settingsValues = _appSettingsWrapper.GetAllConfigData();
foreach(KeyValuePair<string, string> keyValueSetting in settingsValues)
{
switch(keyValueSetting.Key)
{
case "sound":
if (keyValueSetting.Value == "on")
soundNotifyToolStripMenuItem.Checked = true;
else
soundNotifyToolStripMenuItem.Checked = false;
break;
case "autorun":
if (keyValueSetting.Value == "on")
{
if(CheckAutorun())
autorunToolStripMenuItem.Checked = true;
else
{
if(SetAppAutorunValue(true))
autorunToolStripMenuItem.Checked = true;
else
autorunToolStripMenuItem.Checked = false;
}
}
else
autorunToolStripMenuItem.Checked = false;
break;
}
}
backgroundWorker1.RunWorkerAsync();
messagesReceiveTimer.Enabled = true;
messagesReceiveTimer.Start();
}
private void CheckNewAppVersion()
{
var currentAppVersion = Convert.ToDouble(Assembly.GetExecutingAssembly().GetName().Version.ToString().Replace(".", ""));
double siteAppVersion;
try
{
using (var webClient = new WebClient())
siteAppVersion = Convert.ToDouble(webClient.DownloadString("http://prankota.com/version.txt").Replace(".", ""));
if (currentAppVersion < siteAppVersion)
{
var result = MessageBox.Show("Обнаружена новая версия приложения Prankota Notificator! Обновить приложение?", "Prankota Notificator",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
try
{
using (var webClient = new WebClient())
webClient.DownloadFile("http://prankota.com/notificator.exe", "notificator_new.exe");
MessageBox.Show("Готово, обновленная версия приложения находится рядом с запущенным файлом.");
}
catch
{
MessageBox.Show("Ошибка обновления приложения!");
}
}
}
}
catch { }
}
#region Autorun
private bool SetAppAutorunValue(bool autorun)
{
const string registryKeyName = "PrankotaNf";
string exePath = Application.ExecutablePath;
RegistryKey regKey = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run\\");
try
{
if (autorun)
regKey.SetValue(registryKeyName, exePath);
else
{
if (regKey.GetValue(registryKeyName) != null)
regKey.DeleteValue(registryKeyName);
}
regKey.Close();
}
catch
{
if (autorun)
MessageBox.Show("Ошибка добавления программы в автозагрузку! Возможно, программе не хватает прав.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
else
MessageBox.Show("Ошибка при удалении программы из автозагрузки! Возможно, программе не хватает прав.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
return true;
}
private bool CheckAutorun()
{
const string registryKeyName = "PrankotaNf";
RegistryKey regKey = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run\\");
string[] allKeys = regKey.GetValueNames();
bool inAutorun = false;
foreach (string key in allKeys)
{
if (key == registryKeyName)
{
if (regKey.GetValue(registryKeyName).ToString() == Application.ExecutablePath)
inAutorun = true;
}
else
inAutorun = false;
}
return inAutorun;
}
#endregion
#region GUI
private void notifyIcon1_BalloonTipShown(object sender, EventArgs e)
{
if (soundNotifyToolStripMenuItem.Checked)
{
SoundPlayer player = new System.Media.SoundPlayer(Resource1.lolNotifySound1);
player.Play();
}
}
private void soundNotifyToolStripMenuItem_CheckedChanged(object sender, EventArgs e)
{
if (soundNotifyToolStripMenuItem.Checked)
_appSettingsWrapper.EditRecord("sound", "on");
else
_appSettingsWrapper.EditRecord("sound", "off");
}
private void autorunToolStripMenuItem_CheckedChanged(object sender, EventArgs e)
{
if (autorunToolStripMenuItem.Checked)
{
if (!SetAppAutorunValue(true))
autorunToolStripMenuItem.Checked = true;
_appSettingsWrapper.EditRecord("autorun", "on");
}
else
{
if (!SetAppAutorunValue(false))
autorunToolStripMenuItem.Checked = false;
_appSettingsWrapper.EditRecord("autorun", "off");
}
}
private void exitToolStripMenuItem1_Click(object sender, EventArgs e)
{
Environment.Exit(0);
}
private void creditsToolStripMenuItem_Click(object sender, EventArgs e)
{
_crefitsForm.Show();
}
#endregion
string link;
private void notifyIcon1_BalloonTipClicked(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(link))
{
Process.Start(link);
link = null;
}
}
private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
try
{
var newMessageValuePair = MessagesReceive.GetLastMessages();
if(newMessageValuePair.Key != _lastMessagesValuePair.Key)
{
_lastMessagesValuePair = newMessageValuePair;
if (!string.IsNullOrEmpty(newMessageValuePair.Value))
link = newMessageValuePair.Value;
notifyIcon1.BalloonTipTitle = "Евгений Вольнов";
notifyIcon1.BalloonTipText = newMessageValuePair.Key;
notifyIcon1.ShowBalloonTip(10000);
Thread.Sleep(10000);
}
}
catch
{
}
}
private void messagesReceiveTimer_Tick(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
private void ShowBalloonMessage(string text, int timeout)
{
notifyIcon1.BalloonTipTitle = "Евгений Вольнов";
notifyIcon1.BalloonTipText = text;
notifyIcon1.ShowBalloonTip(timeout);
}
private void firstLoadAppBw_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
ShowBalloonMessage("Вот так работают уведомления в Prankota Notificator.", 10000);
Thread.Sleep(10000);
soundNotifyToolStripMenuItem.Checked = true;
ShowBalloonMessage("А так работает уведомление со звуковым сигналом.", 10000);
soundNotifyToolStripMenuItem.Checked = false;
Thread.Sleep(10000);
ShowBalloonMessage("Как только появится новый пранк Prankota Notificator уведомит тебя.", 10000);
Thread.Sleep(10000);
link = "http://prankota.com/prankota-notificator";
ShowBalloonMessage("Для перехода к новости просто нажми на уведомление.", 10000);
Thread.Sleep(10000);
}
private void firstLoadAppBw_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
backgroundWorker1.RunWorkerAsync();
messagesReceiveTimer.Enabled = true;
messagesReceiveTimer.Start();
}
}
}