summaryrefslogtreecommitdiff
path: root/plugins/WhenWasIt/src/hooked_events.cpp
blob: c25bcc9eebb9047ad69e57fe364f1fed8195ed85 (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
177
178
179
180
/*
WhenWasIt (birthday reminder) plugin for Miranda IM

Copyright © 2006-2011 Cristian Libotean

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"

#define DATE_CHANGE_CHECK_INTERVAL 20

#define EXCLUDE_HIDDEN  1
#define EXCLUDE_IGNORED 2
UINT_PTR hCheckTimer = NULL;
UINT_PTR hDateChangeTimer = NULL;
static int currentDay = 0;


static int OnTopToolBarModuleLoaded(WPARAM, LPARAM)
{
	TTBButton ttb = {};
	ttb.dwFlags = TTBBF_VISIBLE | TTBBF_SHOWTOOLTIP;
	ttb.pszService = MS_WWI_CHECK_BIRTHDAYS;
	ttb.hIconHandleUp = hCheckMenu;
	ttb.name = ttb.pszTooltipUp = LPGEN("Check for birthdays");
	TopToolbar_AddButton(&ttb);
	return 0;
}

static int OnOptionsInitialise(WPARAM wParam, LPARAM)
{
	OPTIONSDIALOGPAGE odp = { 0 };
	odp.position = 100000000;
	odp.hInstance = hInstance;
	odp.pszTemplate = MAKEINTRESOURCEA(IDD_OPT_WWI);
	odp.szTitle.w = LPGENW("Birthdays");
	odp.szGroup.w = LPGENW("Contacts");
	odp.flags = ODPF_BOLDGROUPS | ODPF_UNICODE;
	odp.pfnDlgProc = DlgProcOptions;
	Options_AddPage(wParam, &odp);
	return 0;
}

static int OnContactSettingChanged(WPARAM hContact, LPARAM lParam)
{
	DBCONTACTWRITESETTING *dw = (DBCONTACTWRITESETTING *)lParam;
	if ((strcmp(dw->szModule, DUMMY_MODULE) == 0) && (strcmp(dw->szSetting, DUMMY_SETTING) == 0))
		RefreshContactListIcons(hContact);

	return 0;
}

int OnModulesLoaded(WPARAM, LPARAM)
{
	HookEvent(ME_DB_CONTACT_SETTINGCHANGED, OnContactSettingChanged);
	HookEvent(ME_TTB_MODULELOADED, OnTopToolBarModuleLoaded);

	UpdateTimers();
	return 0;
}

int HookEvents()
{
	HookEvent(ME_SYSTEM_MODULESLOADED, OnModulesLoaded);
	HookEvent(ME_OPT_INITIALISE, OnOptionsInitialise);
	return 0;
}

int UnhookEvents()
{
	KillTimers();
	return 0;
}

/////////////////////////////////////////////////////////////////////////////////////////

int RefreshContactListIcons(MCONTACT hContact)
{
	if (hContact == 0)
		return 0;

	int hidden = db_get_b(hContact, "CList", "Hidden", 0);
	int ignored = db_get_dw(hContact, "Ignore", "Mask1", 0);
	ignored = ((ignored & 0x3f) != 0) ? 1 : 0;
	int ok = 1;
	if (commonData.notifyFor & EXCLUDE_HIDDEN)
		ok &= (hidden == 0);

	if (commonData.notifyFor & EXCLUDE_IGNORED)
		ok &= (ignored == 0);

	time_t today = Today();

	int dtb = NotifyContactBirthday(hContact, today, commonData.daysInAdvance);
	int dab = NotifyMissedContactBirthday(hContact, today, commonData.daysAfter);

	if (ok && (dtb >= 0 || dab > 0)) {
		int age = GetContactAge(hContact);
		db_set_b(hContact, "UserInfo", "Age", age);

		if ((bShouldCheckBirthdays) && (commonData.bUsePopups))
		{
			if (dtb >= 0) {
				bBirthdayFound = 1; //only set it if we're called from our CheckBirthdays service
				PopupNotifyBirthday(hContact, dtb, age);
			}
			else if (dab > 0)
				PopupNotifyMissedBirthday(hContact, dab, age);
		}

		if (bShouldCheckBirthdays)
			if (dtb >= 0)
				SoundNotifyBirthday(dtb);

		if ((bShouldCheckBirthdays) && (commonData.bUseDialog)) {
			if (dtb >= 0)
				DialogNotifyBirthday(hContact, dtb, age);
			else if (dab > 0)
				DialogNotifyMissedBirthday(hContact, dab, age);
		}

		if (dtb >= 0)
			ExtraIcon_SetIcon(hWWIExtraIcons, hContact, GetDTBIconHandle(dtb));
	}
	else ExtraIcon_Clear(hWWIExtraIcons, hContact);

	return 0;
}

int UpdateTimers()
{
	UINT interval = db_get_dw(NULL, ModuleName, "Interval", CHECK_INTERVAL);
	interval *= (1000 * 60 * 60); //go from miliseconds to hours
	hCheckTimer = SetTimer(nullptr, 0, interval, OnCheckTimer);
	if (!hDateChangeTimer)
		hDateChangeTimer = SetTimer(nullptr, 0, (1000 * DATE_CHANGE_CHECK_INTERVAL), OnDateChangeTimer);

	return 0;
}

int KillTimers()
{
	KillTimer(nullptr, hCheckTimer);
	hCheckTimer = NULL;

	KillTimer(nullptr, hDateChangeTimer);
	hDateChangeTimer = NULL;

	return 0;
}

VOID CALLBACK OnCheckTimer(HWND, UINT, UINT_PTR, DWORD)
{
	CheckBirthdaysService(0, 1);
}

VOID CALLBACK OnDateChangeTimer(HWND, UINT, UINT_PTR, DWORD)
{
	SYSTEMTIME now;
	GetLocalTime(&now);

	if (currentDay != now.wDay)
		CheckBirthdaysService(0, 1);

	currentDay = now.wDay;
}