-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainForm.cs
618 lines (558 loc) · 29.6 KB
/
MainForm.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Threading;
using System.Text;
using System.Windows.Forms;
using System.Web;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
using System.Linq;
namespace IMV
{
public enum InternetConnectionState : int // необходимое перечисления для InternetGetConnectedState
{
INTERNET_CONNECTION_MODEM = 0x1,
INTERNET_CONNECTION_LAN = 0x2,
INTERNET_CONNECTION_PROXY = 0x4,
INTERNET_RAS_INSTALLED = 0x10,
INTERNET_CONNECTION_OFFLINE = 0x20,
INTERNET_CONNECTION_CONFIGURED = 0x40
}
public partial class MainForm : Form
{
[DllImport("WININET", CharSet = CharSet.Auto)]
static extern bool InternetGetConnectedState(ref InternetConnectionState lpdwFlags, int dwReserved); // проверка интернет соединения
vk start = new vk();
LongPoolServer serv = new LongPoolServer();
Wait wt;
bool wtcreate;
ContextMenuStrip context;
private DwmApi.MARGINS m_glassMargins;
// Делегаты для...
delegate void getEvent(); // отлов общих событий
delegate void getPhoto(List<uint> a, string size); // фото (в другом потоке, чтоб не подвисало)
delegate void NotifyEvent(string name, string text, uint ID); // всплывающее окошко (тоже в другом, чтоб не подвисало)
System.Threading.Timer timerKey, timerCount; // таймеры для технических нужд
System.Windows.Forms.Timer wait; // таймер для скрытия оповещений
TimerCallback getKey, getCounters;
public MainForm()
{
// Инициализируем главное окно
InitializeComponent();
context = new ContextMenuStrip();
context.Items.Add("Настройки");
context.Items.Add("Выход");
context.Items[0].Name = "Settings";
context.Items[1].Name = "Exit";
context.ItemClicked += new ToolStripItemClickedEventHandler(context_ItemClicked); // Вешаем обработки события
notifyIcon1.ContextMenuStrip = context;
serv.NewStatus += new ChangeStatusEventHandler(serv_NewStatus);
serv.NewMessage += new IncomingMessageEventHandler(serv_NewMessage);
}
void serv_NewMessage(IncomingMessageArgs info)
{
if ((info.flag & 32) == 32 && (info.flag & 2) != 2)
{
if (vars.VARS.Visual_notify && vars.VARS.Notify_income)
{
string txt; // текст, который будет отображаться в окошке
if (info.text.Length > 20)
{
txt = info.text.Substring(0, 20);
txt += "...";
}
else
txt = info.text;
NotifyEvent ShowNotify = new NotifyEvent(ShowNotifyWindow);
this.Invoke(ShowNotify, vars.VARS.Contact[info.outID].UserName, txt, info.outID); // инвочим для того, чтобы окно оповещения всплывало
}
}
}
void serv_NewStatus(ChangeStatusArgs info)
{
if (vars.VARS.Visual_notify && vars.VARS.Notify_online && info.flag == 1) // если визуальные оповещения включены и пользователь онлайн
{
NotifyEvent ShowNotify = new NotifyEvent(ShowNotifyWindow);
this.Invoke(ShowNotify, vars.VARS.Contact[info.id].UserName, "В сети", info.id); // инвочим для того, чтобы она всплывала
}
if (vars.VARS.Visual_notify && vars.VARS.Notify_offline && info.flag == 0) // если визуальные оповещения включены и пользователь оффлайн
{
NotifyEvent ShowNotify = new NotifyEvent(ShowNotifyWindow);
this.Invoke(ShowNotify, vars.VARS.Contact[info.id].UserName, "Не в сети", info.id);
}
}
void ShowNotifyWindow(string name, string text, uint ID)
{
Notify noti = new Notify(name, text, ID);
noti.ShowWindow();
noti.ShowTime();
}
void context_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
if (e.ClickedItem.Name == "Settings") // если имя пункта меню равно...
{
Setting set = new Setting();
set.Show();
}
if (e.ClickedItem.Name == "Exit")
{
if (vars.VARS.ExitVK)
{
WebBrowser web = new WebBrowser();
web.Navigate("http://m.vkontakte.ru/logout");
string[] theCookies = System.IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.Cookies));
foreach (string currentFile in theCookies) // удаляем cookies с данными авторизации
{
try
{
if (currentFile.IndexOf("login.vk") != -1 || currentFile.IndexOf("vkontakte") != -1)
System.IO.File.Delete(currentFile);
}
catch (Exception exe)
{
GeneralMethods.WriteError(exe.Source, exe.Message, exe.TargetSite);
}
}
}
if (vars.VARS.SaveSettings) // если надо, то сохраняем настройки
{
if (vars.VARS.Frequency) // если включена настройка сортировки по частоте
{
try
{
System.IO.FileStream stream = new System.IO.FileStream(vars.VARS.Directory + "frequency.dat", System.IO.FileMode.Create, System.IO.FileAccess.Write); // Открываем файл с частотой исользования контактов
System.IO.StreamWriter writer = new System.IO.StreamWriter(stream, Encoding.UTF8);
foreach (uint id in vars.VARS.FrequencyUse.Keys)
{
writer.Write(id);
writer.Write(":");
writer.WriteLine(vars.VARS.FrequencyUse[id]);
writer.Flush();
}
writer.Close();
writer.Dispose();
stream.Dispose();
}
catch (Exception exe)
{
GeneralMethods.WriteError(exe.Source, exe.Message, exe.TargetSite);
}
}
}
else // если не сохраняем настройки, то удаляем ранее созданный файл
{
try
{
System.IO.File.Delete(vars.VARS.Directory + "settings.cfg");
}
catch
{
}
}
Application.Exit();
}
}
#region Glass
/// <summary>
/// Методы, реализующие эффект прозрачного стекла
/// </summary>
/// <param name="msg"></param>
protected override void WndProc(ref Message msg)
{
base.WndProc(ref msg); // Вызываем базовый метод
if (Environment.OSVersion.Version.Major >= 6.0) // Если версия видовс больше 6 (то есть виста и выше, то будем обрабатывать сообщения для прозрачности)
{
const int WM_DWMCOMPOSITIONCHANGED = 0x031E;
switch (msg.Msg)
{
case WM_DWMCOMPOSITIONCHANGED:
if (!DwmApi.DwmIsCompositionEnabled()) // если режим aero не включен, зануляем область стекла
{
m_glassMargins = null;
}
break;
}
}
}
private void ResetDwmBlurBehind()
{
// Настраиваем прозрачность в windows 7
if (DwmApi.DwmIsCompositionEnabled())
{
DwmApi.DWM_BLURBEHIND blur_behind = new DwmApi.DWM_BLURBEHIND();
blur_behind.dwFlags = DwmApi.DWM_BLURBEHIND.DWM_BB_ENABLE | DwmApi.DWM_BLURBEHIND.DWM_BB_BLURREGION;
blur_behind.fEnable = false;
blur_behind.hRegionBlur = IntPtr.Zero;
DwmApi.DwmEnableBlurBehindWindow(this.Handle, blur_behind); // включает эффект стекла
}
}
private void OnClientArea()
{
ResetDwmBlurBehind();
m_glassMargins = new DwmApi.MARGINS(0, 0, 0, 32); // устанавливаем отступы от края, которые будут прозрачными
if (DwmApi.DwmIsCompositionEnabled()) DwmApi.DwmExtendFrameIntoClientArea(this.Handle, m_glassMargins); // если режим aero включён, задаём область где будет стекло
this.Invalidate();
}
#endregion
private void StartWork()
{
IntPtr hand = vars.VARS.Chat.Handle; // Инициализация окна чата, получение заголовка окна
Thread checkVersion = new Thread(new ThreadStart(GeneralMethods.CheckNewVersion));
checkVersion.Start();
if (vars.VARS.GetOfflineMsg)
{
Thread newThrd = new Thread(new ThreadStart(start.messageGet)); // Получение списка контактов
newThrd.Start();
}
getEvent Icon = new getEvent(start.getIcon);
IAsyncResult res1 = Icon.BeginInvoke(null, null); // в асинхронном потоке грузим фотографии для контакт листа
getEvent newEvent = new getEvent(serv.getLongPollServer);
IAsyncResult res2 = newEvent.BeginInvoke(null, null); // в асинхронном потоке обрабатываем события о приходе сообщения и о статусах пользователей
getCounters = new TimerCallback(UpdateCounters);
timerCount = new System.Threading.Timer(getCounters, null, 0, 300000);
if (vars.VARS.Expire != 0) // если время истечения ключа не равно 0, то
{
getKey = new TimerCallback(GetNewToken);
timerKey = new System.Threading.Timer(getKey, null, (vars.VARS.Expire - 60) * 1000, (vars.VARS.Expire - 60) * 1000); // пускаем таймер обновлять каждые expire количество времени ключ token
}
if (vars.VARS.UpdateFriends) // если включена функция посика удалившихся друзей
{
Thread newThrd = new Thread(new ThreadStart(GeneralMethods.SearchFriends)); // Получение списка контактов
newThrd.Start();
}
}
private void GetNewToken(object state)
{
try
{
Invoke(new MethodInvoker(() =>
{
AccessForm acc = new AccessForm();
acc.ShowDialog();
if (acc.DialogResult == DialogResult.OK)
acc.Close();
else
{
MessageBox.Show("Сервер вас отключил!\nПриложение автоматически перезапустится\n и постарается восстановить связь.", "Внимание!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
Application.Exit();
}
}
)); // инвочим метод, потому что находимся в другом потоке. Получаем новые данные сессии
timerKey.Change((vars.VARS.Expire - 60) * 1000, (vars.VARS.Expire - 60) * 1000); // перезапускаем таймер с новым expire
}
catch (Exception exe)
{
GeneralMethods.WriteError(exe.Source, exe.Message, exe.TargetSite);
}
}
private void UpdateCounters(object state)
{
Hashtable couter = start.getStat();
if (couter != null)
{
if (couter.ContainsKey("friends"))
{
if (Convert.ToUInt32(couter["friends"]) > 0)
{
NotifyEvent ShowNotify = new NotifyEvent(ShowNotifyWindow);
this.Invoke(ShowNotify, "Новое событие!", "Новый друг", (uint)0);
getEvent ShowButton = new getEvent(this.ShowButton);
this.button5.Invoke(ShowButton);
}
}
if (couter.ContainsKey("photos"))
{
if (Convert.ToUInt32(couter["photos"]) > 0)
{
NotifyEvent ShowNotify = new NotifyEvent(ShowNotifyWindow);
this.Invoke(ShowNotify, "Новое событие!", "Новая фотография", (uint)0);
getEvent ShowButton = new getEvent(this.ShowButton);
this.button5.Invoke(ShowButton);
}
}
if (couter.ContainsKey("videos"))
{
if (Convert.ToUInt32(couter["videos"]) > 0)
{
NotifyEvent ShowNotify = new NotifyEvent(ShowNotifyWindow);
this.Invoke(ShowNotify, "Новое событие!", "Новое видео", (uint)0);
getEvent ShowButton = new getEvent(this.ShowButton);
this.button5.Invoke(ShowButton);
}
}
if (couter.ContainsKey("events"))
{
if (Convert.ToUInt32(couter["events"]) > 0)
{
NotifyEvent ShowNotify = new NotifyEvent(ShowNotifyWindow);
this.Invoke(ShowNotify, "Новое событие!", "Новая встреча", (uint)0);
getEvent ShowButton = new getEvent(this.ShowButton);
this.button5.Invoke(ShowButton);
}
}
}
else
{
getEvent HideButton = new getEvent(this.HideButton);
this.button5.Invoke(HideButton);
}
}
private void ShowButton()
{
this.button5.Visible = true;
}
private void HideButton()
{
this.button5.Visible = false;
}
private void MainForm_Shown(object sender, EventArgs e)
{
myContactList1.FinishUpdate(); // обновляем настройки контакт листа
}
private void MainForm_Load(object sender, EventArgs e)
{
if (Environment.OSVersion.Version.Major >= 6.0) // если версия выше, делаём всё черным (черный GDI прозрачен)
{
if (DwmApi.DwmIsCompositionEnabled())
{
button1.BackColor = Color.Black;
button2.BackColor = Color.Black;
button3.BackColor = Color.Black;
button4.BackColor = Color.Black;
button5.BackColor = Color.Black;
panel1.BackColor = Color.Black;
OnClientArea();
}
}
if (!System.IO.Directory.Exists(vars.VARS.Directory)) // если директории каким-то образом не оказалось, создаём её и кидаем туда файл ошибок
{
System.IO.Directory.CreateDirectory(vars.VARS.Directory);
System.IO.File.Create(vars.VARS.Directory + "errors.txt");
}
bool flag = true;
InternetConnectionState flags = 0;
bool InternetConnect = InternetGetConnectedState(ref flags, 0); // проверяем соединение с интернетом
while (!InternetConnect) // пока не появится, будет цикл повторяться
{
if (flag)
{
MessageBox.Show("Сервер временно недоступен!\nПрограмма подключится, когда появится соединение!", "Внимание!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
flag = false;
notifyIcon1.Visible = true;
}
Application.DoEvents();
Thread.Sleep(2000);
InternetConnect = InternetGetConnectedState(ref flags, 0);
}
AccessForm acc = new AccessForm(); // создаём форму авторизации
if (acc.ShowDialog() == DialogResult.OK) // если ок, выполняем
{
GeneralMethods.tryGetSettings(); // читаем настройки
if (!vars.VARS.ExitOnCloser) // устанавливаем режим сворачивания
MinimizeBox = false;
if (!vars.VARS.Sound) // устанавливаем в верное положение картинку звука
button1.Image = global::IMV.Properties.Resources._1299773905_no_sound;
wait = new System.Windows.Forms.Timer(); // создаём таймер
wait.Interval = 4000;
wait.Tick += new EventHandler(wait_Tick);
wait.Start(); // запускаем таймер
Thread newThrd = new Thread(new ThreadStart(start.getProfiles)); // Получение списка контактов
newThrd.Start();
while (newThrd.IsAlive) // приложение отправляет сообщения виндовс пока поток не завершился
Application.DoEvents();
wait.Stop();
wait.Dispose();
if (wtcreate) // если открывалось окно ожидания, то уничтожаем его
wt.Dispose();
acc.Dispose();
GeneralMethods.AddItem(); // добавляем контакты в список
if (vars.VARS.Frequency) // если включена настройка
GeneralMethods.tryGetFrequency(); // пытаемся загрузить частоту
myContactList1.Sort(); // сортируем
StartWork(); // Запуск "отлова" обновлений
}
else
Application.Exit();
}
void wait_Tick(object sender, EventArgs e)
{
wt = new Wait(); // если больше 4 секунд грузится, то появляется окно ожидания
wtcreate = true;
wait.Stop();
wt.Show();
//throw new NotImplementedException();
}
private void button1_Click(object sender, EventArgs e)
{
if (vars.VARS.Sound)
{
vars.VARS.Sound = false;
button1.Image = global::IMV.Properties.Resources._1299773905_no_sound;
}
else
{
vars.VARS.Sound = true;
button1.Image = global::IMV.Properties.Resources._1299773922_sound;
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{ // обновляем маленькие фотографии в списке контактов если кажется, что они устарели, например)
vk start = new vk();
List<uint> a = new List<uint>();
foreach (KeyValuePair<uint, vk.profile> item in vars.VARS.Contact) // для кого грузим новые фотки (в данном случае для всех)
a.Add(item.Key);
getPhoto newPhoto = new getPhoto(start.getPhoto);
IAsyncResult res2 = newPhoto.BeginInvoke(a, "photo", null, null); // пускаем в асинхронном потоке
}
catch (Exception exe)
{
MessageBox.Show("Неизвестная ошибка!", "Внимание!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
GeneralMethods.WriteError(exe.Source, exe.Message, exe.TargetSite);
}
}
private void MainForm_Deactivate(object sender, EventArgs e)
{// сворачивание в трей
if (this.WindowState == FormWindowState.Minimized)
{
this.Hide();
notifyIcon1.Visible = true;
}
}
private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
{ // разворачивание окна из трея
if ((!this.Visible) && (e.Button == MouseButtons.Left))
{
this.MinimizeBox = vars.VARS.ExitOnCloser;
this.ShowInTaskbar = true;
this.Show();
this.WindowState = FormWindowState.Normal;
notifyIcon1.Visible = false;
}
}
private void MainForm_Activated(object sender, EventArgs e)
{
if (Environment.OSVersion.Version.Major >= 6.0) // если версия ос выше, то прознарчное окно, иначе по-умолчанию
OnClientArea();
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (vars.VARS.ExitOnCloser)
{
if (vars.VARS.ExitVK)
{
WebBrowser web = new WebBrowser();
web.Navigate("http://m.vkontakte.ru/logout");
string[] theCookies = System.IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.Cookies));
foreach (string currentFile in theCookies) // удаляем cookies с данными авторизации
{
try
{
if (currentFile.IndexOf("login.vk") != -1 || currentFile.IndexOf("vkontakte") != -1)
System.IO.File.Delete(currentFile);
}
catch (Exception exe)
{
GeneralMethods.WriteError(exe.Source, exe.Message, exe.TargetSite);
}
}
}
if (vars.VARS.SaveSettings) // если сохраняем настройки
{
if (vars.VARS.Frequency) // если надо, то записываем частоту использования
{
try
{
System.IO.FileStream stream = new System.IO.FileStream(vars.VARS.Directory + "frequency.dat", System.IO.FileMode.Create, System.IO.FileAccess.Write);
System.IO.StreamWriter writer = new System.IO.StreamWriter(stream, Encoding.UTF8);
foreach (uint id in vars.VARS.FrequencyUse.Keys)
{
writer.Write(id);
writer.Write(":");
writer.WriteLine(vars.VARS.FrequencyUse[id]);
writer.Flush();
}
writer.Close();
writer.Dispose();
stream.Dispose();
}
catch (Exception exe)
{
GeneralMethods.WriteError(exe.Source, exe.Message, exe.TargetSite);
}
}
}
else // иначе....
{
try
{ // удаляем файл настроек
System.IO.File.Delete(vars.VARS.Directory + "settings.cfg");
}
catch (Exception exe)
{
GeneralMethods.WriteError(exe.Source, exe.Message, exe.TargetSite);
}
}
}
else // если на крестик указано в настройках сворачивать в трей
{
if (e.CloseReason != CloseReason.ApplicationExitCall &&
e.CloseReason != CloseReason.FormOwnerClosing &&
e.CloseReason != CloseReason.TaskManagerClosing &&
e.CloseReason != CloseReason.WindowsShutDown)
{
e.Cancel = true;
this.Hide();
//this.ShowInTaskbar = false;
notifyIcon1.Visible = true;
}
}
}
private void button3_Click(object sender, EventArgs e)
{//показываем окно ввода статуса
Status note = new Status();
note.ShowDialog();
}
private void button4_Click(object sender, EventArgs e)
{// показываем поле ввода имени
textBox1.Visible = true;
textBox1.Focus();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
myContactList1.Items.Clear();
IEnumerable<vk.profile> findUser;
findUser = Enumerable.Select(vars.VARS.Contact, n => n.Value); // Выбираем все структуры профилей пользователей
findUser = Enumerable.Where(findUser, n => n.UserName.Contains(textBox1.Text)); // Ищем имена, которые включают заданную строку
if (vars.VARS.ShowOffline != true)
findUser = Enumerable.Where(findUser, n => n.online == true);
foreach (vk.profile item in findUser) // Добавляем найденное в контакт лист
myContactList1.Items.Add(item.uid);
if (myContactList1.Items.Count != 0) // если не равно 0 количество найденных, обновляем настройки
myContactList1.FinishUpdate();
else
myContactList1.FinishUpdate(); // если равно, то посылаем запрос об обновлении параметров
if (textBox1.Text == "") // если строка поиска пуста, обновляем настроки
myContactList1.FinishUpdate();
}
private void MainForm_KeyDown(object sender, KeyEventArgs e)
{// скрываем поле ввода имени
if (e.KeyCode == Keys.Escape)
{
textBox1.Visible = false;
myContactList1.Sort();
myContactList1.Invalidate();
}
}
private void button5_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("http://vkontakte.ru/id" + vars.VARS.Mid);
button5.Visible = false;
}
}
}