summaryrefslogtreecommitdiff
path: root/plugins/GmailNotifier/src/main.cpp
blob: e0f98c2383cbc1648e78e65bef405a1ef6ccb1d6 (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
/*
Miranda plugin template, originally by Richard Hughes
http://miranda-icq.sourceforge.net/

This file is placed in the public domain. Anybody is free to use or
modify it as they wish with no restriction.
There is no warranty.
*/

#include "stdafx.h"
#include "version.h"

CLIST_INTERFACE *pcli;
HINSTANCE hInst;
int hLangpack;
UINT hTimer;
HANDLE hMirandaStarted, hOptionsInitial;
HNETLIBUSER hNetlibUser;
NOTIFYICONDATA niData;
optionSettings opt;

OBJLIST<Account> g_accs(1);
BOOL optionWindowIsOpen = FALSE;
short ID_STATUS_NONEW;

static PLUGININFOEX pluginInfoEx =
{
	sizeof(PLUGININFOEX),
	__PLUGIN_NAME,
	PLUGIN_MAKE_VERSION(__MAJOR_VERSION, __MINOR_VERSION, __RELEASE_NUM, __BUILD_NUM),
	__DESCRIPTION,
	__AUTHOR,
	__AUTHOREMAIL,
	__COPYRIGHT,
	__AUTHORWEB,
	UNICODE_AWARE,
	// {243955E0-75D9-4CC3-9B28-6F9C5AF4532D}
	{ 0x243955e0, 0x75d9, 0x4cc3, { 0x9b, 0x28, 0x6f, 0x9c, 0x5a, 0xf4, 0x53, 0x2d } }
};

INT_PTR GetCaps(WPARAM wParam, LPARAM)
{
	if (wParam == PFLAGNUM_2 && opt.ShowCustomIcon)
		return PF2_ONLINE | PF2_LIGHTDND | PF2_SHORTAWAY;

	return 0;
}

INT_PTR GetStatus(WPARAM, LPARAM)
{
	return ID_STATUS_ONLINE;
}

INT_PTR GetName(WPARAM wParam, LPARAM lParam)
{
	mir_strncpy((char*)lParam, MODULE_NAME, wParam);
	return 0;
}

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD, LPVOID)
{
	hInst = hinstDLL;
	return TRUE;
}

void CALLBACK TimerProc(HWND, UINT, UINT_PTR, DWORD)
{
	PluginMenuCommand(0, 0);
}

INT_PTR PluginMenuCommand(WPARAM hContact, LPARAM)
{
	if (!optionWindowIsOpen)
		mir_forkthread(Check_ThreadFunc, GetAccountByContact(hContact));

	return 0;
}

static int OnMirandaStart(WPARAM, LPARAM)
{
	PluginMenuCommand(0, 0);
	return 0;
}

extern "C" __declspec(dllexport) PLUGININFOEX* MirandaPluginInfoEx(DWORD)
{
	return &pluginInfoEx;
}

extern "C" int __declspec(dllexport) Load()
{
	mir_getLP(&pluginInfoEx);
	pcli = Clist_GetInterface();

	Skin_AddSound("Gmail", LPGENW("Other"), LPGENW("Gmail: New thread(s)"));
	HookEvent(ME_CLIST_DOUBLECLICKED, OpenBrowser);

	PROTOCOLDESCRIPTOR pd = { PROTOCOLDESCRIPTOR_V3_SIZE };
	pd.szName = MODULE_NAME;
	pd.type = PROTOTYPE_VIRTUAL;
	Proto_RegisterModule(&pd);

	NETLIBUSER nlu = {};
	nlu.flags = NUF_OUTGOING | NUF_HTTPCONNS | NUF_NOHTTPSOPTION | NUF_UNICODE;
	nlu.szSettingsModule = MODULE_NAME;
	nlu.szDescriptiveName.w = TranslateT("Gmail Notifier connection");
	hNetlibUser = Netlib_RegisterUser(&nlu);

	CreateProtoServiceFunction(MODULE_NAME, PS_GETCAPS, GetCaps);
	CreateProtoServiceFunction(MODULE_NAME, PS_GETSTATUS, GetStatus);
	CreateProtoServiceFunction(MODULE_NAME, PS_GETNAME, GetName);
	CreateServiceFunction("GmailMNotifier/Notifying", Notifying);

	opt.circleTime = db_get_dw(NULL, MODULE_NAME, "circleTime", 30);
	opt.notifierOnTray = db_get_dw(NULL, MODULE_NAME, "notifierOnTray", TRUE);
	opt.notifierOnPop = db_get_dw(NULL, MODULE_NAME, "notifierOnPop", TRUE);
	opt.popupDuration = db_get_dw(NULL, MODULE_NAME, "popupDuration", -1);
	opt.popupBgColor = db_get_dw(NULL, MODULE_NAME, "popupBgColor", RGB(173, 206, 247));
	opt.popupTxtColor = db_get_dw(NULL, MODULE_NAME, "popupTxtColor", RGB(0, 0, 0));
	opt.OpenUsePrg = db_get_dw(NULL, MODULE_NAME, "OpenUsePrg", 0);
	opt.ShowCustomIcon = db_get_dw(NULL, MODULE_NAME, "ShowCustomIcon", FALSE);
	opt.UseOnline = db_get_dw(NULL, MODULE_NAME, "UseOnline", FALSE);
	opt.AutoLogin = db_get_dw(NULL, MODULE_NAME, "AutoLogin", TRUE);
	opt.LogThreads = db_get_dw(NULL, MODULE_NAME, "LogThreads", FALSE);

	DBVARIANT dbv;
	if (db_get_s(NULL, "SkinIcons", "core_status_" MODULE_NAME "4", &dbv)) {
		db_set_s(NULL, "SkinIcons", "core_status_" MODULE_NAME "0", "plugins\\GmailNotifier.dll,2");
		db_set_s(NULL, "SkinIcons", "core_status_" MODULE_NAME "1", "plugins\\GmailNotifier.dll,2");
		db_set_s(NULL, "SkinIcons", "core_status_" MODULE_NAME "2", "plugins\\GmailNotifier.dll,0");
		db_set_s(NULL, "SkinIcons", "core_status_" MODULE_NAME "4", "plugins\\GmailNotifier.dll,1");
	}
	else db_free(&dbv);

	BuildList();
	ID_STATUS_NONEW = opt.UseOnline ? ID_STATUS_ONLINE : ID_STATUS_OFFLINE;
	for (int i = 0; i < g_accs.getCount(); i++)
		db_set_dw(g_accs[i].hContact, MODULE_NAME, "Status", ID_STATUS_NONEW);

	hTimer = SetTimer(nullptr, 0, opt.circleTime * 60000, TimerProc);
	hMirandaStarted = HookEvent(ME_SYSTEM_MODULESLOADED, OnMirandaStart);
	hOptionsInitial = HookEvent(ME_OPT_INITIALISE, OptInit);

	CreateServiceFunction(MODULE_NAME "/MenuCommand", PluginMenuCommand);

	CMenuItem mi;
	SET_UID(mi, 0xbe16f37, 0x17be, 0x4494, 0xaa, 0xb2, 0x3a, 0xa7, 0x38, 0xfa, 0xf9, 0xcc);
	mi.position = -0x7FFFFFFF;
	mi.hIcolibItem = Skin_LoadProtoIcon(MODULE_NAME, ID_STATUS_ONLINE);
	mi.name.a = LPGEN("&Check all Gmail inboxes");
	mi.pszService = MODULE_NAME "/MenuCommand";
	mi.Module = MODULE_NAME;
	Menu_AddMainMenuItem(&mi);

	SET_UID(mi, 0x22c6ace1, 0xba0c, 0x44b5, 0xa4, 0xd2, 0x1, 0x7d, 0xb1, 0xe0, 0x51, 0xeb);
	mi.name.a = LPGEN("&Check Gmail inbox");
	mi.pszService = "/MenuCommand";
	Menu_AddContactMenuItem(&mi, MODULE_NAME);
	return 0;
}

extern "C" int __declspec(dllexport) Unload(void)
{
	if (hTimer)
		KillTimer(nullptr, hTimer);
	
	for (int i = 0; i < g_accs.getCount(); i++)
		DeleteResults(g_accs[i].results.next);
	g_accs.destroy();

	Netlib_CloseHandle(hNetlibUser);

	UnhookEvent(hMirandaStarted);
	UnhookEvent(hOptionsInitial);
	return 0;
}