summaryrefslogtreecommitdiff
path: root/plugins/Exchange/src/hooked_events.cpp
blob: ecf569ef70869a4ca67cf4a338a95d082bcad54c (plain)
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
/*
Exchange notifier plugin for Miranda IM

Copyright © 2006 Cristian Libotean, Attila Vajda

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include "stdafx.h"
#include "dlg_handlers.h"
#include "hooked_events.h"

HANDLE hModulesLoaded;
HANDLE hOptionsInitialise;
HANDLE hPreShutdown;

UINT_PTR hCheckTimer = NULL;
UINT_PTR hReconnectTimer = NULL;
UINT_PTR hFirstCheckTimer = NULL;

#define HOST "http://eblis.tla.ro/projects"

#define EXCHANGE_VERSION_URL HOST "/miranda/Exchange/updater/Exchange.html"
#define EXCHANGE_UPDATE_URL HOST "/miranda/Exchange/updater/Exchange.zip"
#define EXCHANGE_VERSION_PREFIX "Exchange notifier plugin version "

int HookEvents()
{
	hModulesLoaded = HookEvent(ME_SYSTEM_MODULESLOADED, OnModulesLoaded);
	hOptionsInitialise = HookEvent(ME_OPT_INITIALISE, OnOptionsInitialise);
	hPreShutdown = HookEvent(ME_SYSTEM_PRESHUTDOWN, OnSystemPreShutdown);
	
	return 0;
}

int UnhookEvents()
{
	UnhookEvent(hModulesLoaded);
	UnhookEvent(hOptionsInitialise);
	UnhookEvent(hPreShutdown);
	
	KillTimers();
	
	return 0;
}

int OnModulesLoaded(WPARAM, LPARAM)
{
	UpdateTimers();

	CMenuItem mi;
	SET_UID(mi, 0xcbfbfd3d, 0x5002, 0x4c64, 0x92, 0xb, 0x9c, 0x12, 0x4b, 0x6, 0x51, 0x2a);
	mi.hIcolibItem = hiMailIcon;
	mi.position = 10000000;
	mi.pszService = MS_EXCHANGE_CHECKEMAIL;
	mi.flags = CMIF_TCHAR;
	mi.name.t = LPGENT("Check exchange mailbox");
	Menu_AddMainMenuItem(&mi);
	
	hEmailsDlg = NULL; //CreateDialog(hInstance, MAKEINTRESOURCE(IDD_EMAILS), NULL, DlgProcEmails); //create emails window
	FirstTimeCheck();	
//	CheckEmail();
	return 0;
}

//add the exchange options dialog to miranda
int OnOptionsInitialise(WPARAM wParam, LPARAM)
{
	OPTIONSDIALOGPAGE odp = { 0 };
	odp.position = 100000000;
	odp.hInstance = hInstance;
	odp.pszTemplate = MAKEINTRESOURCEA(IDD_OPT_EXCHANGE);
	odp.ptszTitle = LPGENT("Exchange notify");
	odp.ptszGroup = LPGENT("Plugins");
	odp.groupPosition = 910000000;
	odp.flags = ODPF_BOLDGROUPS | ODPF_TCHAR;
	odp.pfnDlgProc = DlgProcOptions;
	Options_AddPage(wParam, &odp);
	return 0;
}

int OnSystemPreShutdown(WPARAM, LPARAM)
{
	if (hEmailsDlg)
	{
		SendMessage(hEmailsDlg, WM_CLOSE, 0, 0); //close the window if it's opened
	}
	
	exchangeServer.Disconnect();
	
	return 0;
}

void FirstTimeCheck()
{
	hFirstCheckTimer = SetTimer(NULL, 0, 5 * 1000, OnFirstCheckTimer);
}

int UpdateTimers()
{
	KillTimers();
	int interval;
	interval = db_get_dw(NULL, ModuleName, "Interval", DEFAULT_INTERVAL);
	interval *= 1000; //go from miliseconds to seconds
	hCheckTimer = SetTimer(NULL, 0, interval, (TIMERPROC) OnCheckTimer);
	
	int bReconnect = db_get_b(NULL, ModuleName, "Reconnect", 0);
	if (bReconnect) //user wants to forcefully reconnect every x minutes
		{
			interval = db_get_dw(NULL, ModuleName, "ReconnectInterval", DEFAULT_RECONNECT_INTERVAL);
			interval *= 1000 * 60; //go from miliseconds to seconds to minutes
			hReconnectTimer = SetTimer(NULL, 0, interval, (TIMERPROC) OnReconnectTimer);
		}
	
	return 0;
}

int KillTimers()
{
	if (hCheckTimer)
		{
			KillTimer(NULL, hCheckTimer);
			hCheckTimer = NULL;
		}
	if (hReconnectTimer)
		{
			KillTimer(NULL, hReconnectTimer);
			hReconnectTimer = NULL;
		}
	return 0;
}

VOID CALLBACK OnCheckTimer(HWND, UINT, UINT_PTR, DWORD)
{
	/*if (exchangeServer.IsConnected())
		{
			exchangeServer.Check();
		}
		else{
			exchangeServer.Connect();
		}*/
	int bCheck = db_get_b(NULL, ModuleName, "Check", 1);
	
	if (bCheck) //only check if we were told to
		{
			ThreadCheckEmail(FALSE);
		}
}

VOID CALLBACK OnReconnectTimer(HWND, UINT, UINT_PTR, DWORD)
{

	_popupUtil(TranslateT("Forcefully reconnecting to Exchange server ..."));
	
	exchangeServer.Reconnect(); //reconnect
}

VOID CALLBACK OnFirstCheckTimer(HWND hWnd, UINT msg, UINT_PTR idEvent, DWORD dwTime)
{
	KillTimer(NULL, hFirstCheckTimer);
	OnCheckTimer(hWnd, msg, idEvent, dwTime);
	
	hFirstCheckTimer = NULL;
}