summaryrefslogtreecommitdiff
path: root/plugins/UserInfoEx/src/svc_reminder.cpp
blob: 82936b60abec34472ee28c2f1d3b813f424a4f71 (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
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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
/*
UserinfoEx plugin for Miranda IM

Copyright:
� 2006-2010 DeathAxe, Yasnovidyashii, Merlin, K. Romanov, Kreol

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.
*/

/**
 * System Includes:
 **/

#include "commonheaders.h"

/**
* The CEvent structure describes the next anniversary to remind of.
**/

struct CEvent
{
	enum EType { NONE, BIRTHDAY, ANNIVERSARY };

	EType	_eType;
	WORD	_wDaysLeft;

	CEvent();
	CEvent(EType eType, WORD wDaysLeft);

	BYTE operator << (const CEvent& e);
};

typedef struct _REMINDEROPTIONS
{
	WORD	wDaysEarlier;
	BYTE	bPopups;
	BYTE	bCListExtraIcon;
	BYTE	bFlashCList;
	BYTE	bCheckVisibleOnly;
	BYTE	RemindState;
	CEvent	evt;
}
REMINDEROPTIONS, *LPREMINDEROPTIONS;

static HANDLE ExtraIcon = INVALID_HANDLE_VALUE;


static HANDLE	ghCListIA = NULL;
static HANDLE	ghCListIR = NULL;
static HANDLE	ghSettingsChanged = NULL;

static UINT_PTR	ghRemindTimer = 0;
static UINT_PTR	ghRemindDateChangeTimer = 0;

HANDLE ghCListAnnivIcons[11];
HANDLE ghCListBirthdayIcons[11];

static REMINDEROPTIONS	gRemindOpts;

static void UpdateTimer(BYTE bStartup);

/***********************************************************************************************************
 * struct CEvent
 ***********************************************************************************************************/

/**
* This is the default constructor.
*
* @param	none
*
* @return nothing
**/

CEvent::CEvent()
{
	_wDaysLeft = 0xFFFF;
	_eType = NONE;
}

/**
* This is the default constructor.
*
* @param	eType			- initial type
* @param	wDaysLeft		- initial days to event
*
* @return	nothing
**/

CEvent::CEvent(EType eType, WORD wDaysLeft)
{
	_wDaysLeft = wDaysLeft;
	_eType = eType;
}

/**
* This operator dups the attributes of the given CEvent object if
* the event comes up earlier then the one of the object.
*
* @param	evt				- the reference to the event object whose attributes to assign.
*
* @retval	TRUE			- The values of @e evt have been assigned.
* @retval	FALSE			- The values are not assigned.
**/

BYTE CEvent::operator << (const CEvent& evt)
{
	if (_wDaysLeft > evt._wDaysLeft) {
		_wDaysLeft = evt._wDaysLeft;
		_eType = evt._eType;
		return TRUE;
	}
	return FALSE;
}

/***********************************************************************************************************
 * notification functions
 ***********************************************************************************************************/

/**
* This function returns the icon for the given anniversary,
* which is the given number of days in advance.
*
* @param	evt				- structure specifying the next anniversary
*
* @return	The function returns icolib's icon if found or NULL otherwise.
**/

static HICON GetAnnivIcon(const CEvent &evt)
{
	HICON hIcon = NULL;

	CHAR szIcon[MAXSETTING];

	switch (evt._eType) {
	case CEvent::BIRTHDAY:
		if (evt._wDaysLeft > 9)
			hIcon = IcoLib_GetIcon(ICO_RMD_DTBX);
		else {
			mir_snprintf(szIcon, _countof(szIcon), MODNAME"_rmd_dtb%u", evt._wDaysLeft);
			hIcon = IcoLib_GetIcon(szIcon);
		}
		break;

	case CEvent::ANNIVERSARY:
		if (evt._wDaysLeft > 9)
			hIcon = IcoLib_GetIcon(ICO_RMD_DTAX);
		else {
			mir_snprintf(szIcon, _countof(szIcon), MODNAME"_rmd_dta%u", evt._wDaysLeft);
			hIcon = IcoLib_GetIcon(szIcon);
		}
	}
	return hIcon;
}

/**
* Displays an clist extra icon according to the kind of anniversary
* and the days in advance.
*
* @param	evt				- structure specifying the next anniversary
*
* @return	nothing
**/

static void NotifyWithExtraIcon(MCONTACT hContact, const CEvent &evt)
{
	if (gRemindOpts.bCListExtraIcon) {
		char szIcon[MAXSETTING], *icoName;
		
		switch (evt._eType) {
		case CEvent::BIRTHDAY:
			if (evt._wDaysLeft > 9)
				icoName = ICO_RMD_DTAX;
			else {
				mir_snprintf(szIcon, _countof(szIcon), MODNAME"_rmd_dtb%u", evt._wDaysLeft);
				icoName = szIcon;
			}
			break;

		case CEvent::ANNIVERSARY:
			if (evt._wDaysLeft > 9)
				icoName = ICO_RMD_DTAX;
			else {
				mir_snprintf(szIcon, _countof(szIcon), MODNAME"_rmd_dta%u", evt._wDaysLeft);
				icoName = szIcon;
			}
			break;

		default:	
			return;
		}
		ExtraIcon_SetIcon(ExtraIcon, hContact, icoName);
	}
}

/**
* Message procedure for popup messages
*
* @param	hWnd			- handle to the popupwindow
* @param	uMsg			- message to handle
* @param	wParam			- message specific parameter
* @param	lParam			- message specific parameter
*
* @return	message specific
**/

static LRESULT CALLBACK PopupWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch (uMsg) {
	case WM_COMMAND:
		if (HIWORD(wParam) == STN_CLICKED) {
			PUDeletePopup(hWnd);
			return TRUE;
		}
		break;

	case WM_CONTEXTMENU:
		PUDeletePopup(hWnd);
		return TRUE;
	}
	return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

/**
* Displays a popup
*
* @param	hContact		- contact to display popup for
* @param	eventType		- indicates which popup settings to apply
* @param	DaysToAnniv		- days left until anniversary occures
* @param	pszDesc			- this is the headline
* @param	szMsg			- message to display
*
* @return	return value of the popup service
**/

static int NotifyWithPopup(MCONTACT hContact, CEvent::EType eventType, int DaysToAnniv, LPCTSTR pszDesc, LPCTSTR pszMsg)
{
	if (!gRemindOpts.bPopups)
		return 1;

	POPUPDATAT ppd = { 0 };
	ppd.PluginWindowProc = PopupWindowProc;
	ppd.iSeconds = (int)db_get_b(NULL, MODNAME, SET_POPUP_DELAY, 0);

	if (hContact) {
		ppd.lchContact = hContact;
		mir_sntprintf(ppd.lptzContactName, _countof(ppd.lptzContactName),
			_T("%s - %s"), TranslateTS(pszDesc), DB::Contact::DisplayName(hContact));
	}
	else mir_tstrncpy(ppd.lptzContactName, TranslateT("Reminder"), _countof(ppd.lptzContactName));

	mir_tstrncpy(ppd.lptzText, pszMsg, MAX_SECONDLINE);

	ppd.lchIcon = GetAnnivIcon(CEvent(eventType, DaysToAnniv));

	switch (eventType) {
	case CEvent::BIRTHDAY:
		switch (db_get_b(NULL, MODNAME, SET_POPUP_BIRTHDAY_COLORTYPE, POPUP_COLOR_CUSTOM)) {
		case POPUP_COLOR_WINDOWS:
			ppd.colorBack = GetSysColor(COLOR_BTNFACE);
			ppd.colorText = GetSysColor(COLOR_WINDOWTEXT);
			break;

		case POPUP_COLOR_CUSTOM:
			ppd.colorBack = db_get_dw(NULL, MODNAME, SET_POPUP_BIRTHDAY_COLOR_BACK, RGB(192, 180, 30));
			ppd.colorText = db_get_dw(NULL, MODNAME, SET_POPUP_BIRTHDAY_COLOR_TEXT, 0);
			break;
		}
		break;

	case CEvent::ANNIVERSARY:
		switch (db_get_b(NULL, MODNAME, SET_POPUP_ANNIVERSARY_COLORTYPE, POPUP_COLOR_CUSTOM)) {
		case POPUP_COLOR_WINDOWS:
			ppd.colorBack = GetSysColor(COLOR_BTNFACE);
			ppd.colorText = GetSysColor(COLOR_WINDOWTEXT);
			break;

		case POPUP_COLOR_CUSTOM:
			ppd.colorBack = db_get_dw(NULL, MODNAME, SET_POPUP_ANNIVERSARY_COLOR_BACK, RGB(90, 190, 130));
			ppd.colorText = db_get_dw(NULL, MODNAME, SET_POPUP_ANNIVERSARY_COLOR_TEXT, 0);
			break;
		}
	}
	return PUAddPopupT(&ppd);
}

/**
* Flash contact list's contact icon.
*
* @param	hContact		- contact whose icon to flash
* @param	evt				- structure specifying the next anniversary
*
* @return	nothing
**/

static void NotifyFlashCListIcon(MCONTACT hContact, const CEvent &evt)
{
	if (!gRemindOpts.bFlashCList || evt._wDaysLeft != 0)
		return;

	TCHAR szMsg[MAX_PATH];

	CLISTEVENT cle = { sizeof(cle) };
	cle.hContact = hContact;
	cle.flags = CLEF_URGENT|CLEF_TCHAR;
	cle.hDbEvent = NULL;

	switch (evt._eType) {
	case CEvent::BIRTHDAY:
		mir_sntprintf(szMsg, _countof(szMsg), TranslateT("%s has %s today."), DB::Contact::DisplayName(hContact), TranslateT("Birthday"));
		cle.hIcon = IcoLib_GetIcon(ICO_COMMON_BIRTHDAY);
		break;

	case CEvent::ANNIVERSARY:
		mir_sntprintf(szMsg, _countof(szMsg), TranslateT("%s has %s today."), DB::Contact::DisplayName(hContact), TranslateT("an anniversary"));
		cle.hIcon = IcoLib_GetIcon(ICO_COMMON_ANNIVERSARY);
		break;

	default:
		return;
	}
	cle.ptszTooltip = szMsg;

	// pszService = NULL get error (crash),
	// pszService = "dummy" get 'service not fount' and continue;
	cle.pszService = "dummy";
	cle.lParam = NULL;

	CallService(MS_CLIST_ADDEVENT, 0, (LPARAM)&cle);
}

/**
* Play a sound for the nearest upcoming anniversary
*
* @param	evt				- structure specifying the next anniversary
*
* @retval	0 if sound was played
* @retval	1 otherwise
**/

static BYTE NotifyWithSound(const CEvent &evt)
{
	if (evt._wDaysLeft <= min(db_get_b(NULL, MODNAME, SET_REMIND_SOUNDOFFSET, DEFVAL_REMIND_SOUNDOFFSET), gRemindOpts.wDaysEarlier)) {
		switch (evt._eType) {
		case CEvent::BIRTHDAY:
			SkinPlaySound(evt._wDaysLeft == 0 ? SOUND_BIRTHDAY_TODAY : SOUND_BIRTHDAY_SOON);
			return 0;

		case CEvent::ANNIVERSARY:
			SkinPlaySound(SOUND_ANNIVERSARY);
			return 0;
		}
	}
	return 1;
}

/***********************************************************************************************************
 * "check for anniversary" functions
 ***********************************************************************************************************/

static BYTE CheckAnniversaries(MCONTACT hContact, MTime &Now, CEvent &evt, BYTE bNotify)
{
	int numAnniversaries = 0;
	int Diff;
	MAnnivDate mta;
	CMString tszMsg;

	if (gRemindOpts.RemindState == REMIND_ANNIV || gRemindOpts.RemindState == REMIND_ALL) {
		for (int i = 0; i < ANID_LAST && !mta.DBGetAnniversaryDate(hContact, i); i++) {
			mta.DBGetReminderOpts(hContact);

			if (mta.RemindOption() != BST_UNCHECKED) {
				WORD wDaysEarlier = (mta.RemindOption() == BST_CHECKED) ? mta.RemindOffset() : -1;
				if (wDaysEarlier == (WORD)-1)
					wDaysEarlier = gRemindOpts.wDaysEarlier;

				Diff = mta.CompareDays(Now);
				if ((Diff >= 0) && (Diff <= wDaysEarlier)) {
					if (evt._wDaysLeft > Diff) {
						evt._wDaysLeft = Diff;
						evt._eType = CEvent::ANNIVERSARY;
					}
					numAnniversaries++;

					// create displayed text for popup
					if (bNotify) {
						// first anniversary found
						if (numAnniversaries == 1)
							switch (GenderOf(hContact)){
							case 0:
								tszMsg += TranslateT("He/she has the following anniversaries:");
								break;
							case 'M':
								tszMsg += TranslateT("He has the following anniversaries:");
								break;
							case 'F':
								tszMsg += TranslateT("She has the following anniversaries:");
								break;
							}						
						tszMsg.Append(_T("\n- "));

						switch (Diff) {
						case 0:
							tszMsg.AppendFormat(TranslateT("%d. %s today"), mta.Age(), mta.Description());
							break;
						case 1:
							tszMsg.AppendFormat(TranslateT("%d. %s tomorrow"), mta.Age() + 1, mta.Description());
							break;
						default:
							tszMsg.AppendFormat(TranslateT("%d. %s in %d days"), mta.Age() + 1, mta.Description(), Diff);
						}
					}
				}
			}
		}
	}

	// show one popup for all anniversaries
	if (numAnniversaries != 0 && bNotify) {
		if (tszMsg.GetLength() >= MAX_SECONDLINE) {
			tszMsg.Truncate(MAX_SECONDLINE - 5);
			tszMsg.Append(_T("\n..."));
		}

		NotifyWithPopup(hContact, CEvent::ANNIVERSARY, Diff, LPGENT("Anniversaries"), tszMsg);
	}

	return numAnniversaries != 0;
}

/**
* This function checks, whether a contact has a birthday and it is within the period of time to remind of or not.
*
* @param	hContact		- the contact to check
* @param	Now				- current time
* @param	evt				- the reference to a structure, which retrieves the resulting DTB
* @param	bNotify			- if TRUE, a popup will be displayed for a contact having birthday within the next few days.
* @param	LastAnswer		- this parameter is used for the automatic backup function
*
* @retval	TRUE			- contact has a birthday to remind of
* @retval	FALSE			- contact has no birthday or it is not within the desired period of time.
**/

static bool CheckBirthday(MCONTACT hContact, MTime &Now, CEvent &evt, BYTE bNotify, PWORD LastAnwer)
{
	if (gRemindOpts.RemindState == REMIND_BIRTH || gRemindOpts.RemindState == REMIND_ALL) {
		MAnnivDate mtb;
		if (!mtb.DBGetBirthDate(hContact)) {
			int Diff;

			mtb.DBGetReminderOpts(hContact);

			// make backup of each protocol based birthday
			if (db_get_b(NULL, MODNAME, SET_REMIND_SECUREBIRTHDAY, TRUE))
				mtb.BackupBirthday(hContact, NULL, 0, LastAnwer);

			if (mtb.RemindOption() != BST_UNCHECKED) {
				WORD wDaysEarlier = (mtb.RemindOption() == BST_CHECKED) ? mtb.RemindOffset() : -1;
				if (wDaysEarlier == (WORD)-1)
					wDaysEarlier = gRemindOpts.wDaysEarlier;

				Diff = mtb.CompareDays(Now);
				if ((Diff >= 0) && (Diff <= wDaysEarlier)) {
					if (evt._wDaysLeft > Diff) {
						evt._wDaysLeft = Diff;
						evt._eType = CEvent::BIRTHDAY;
					}

					if (bNotify) {
						TCHAR szMsg[MAXDATASIZE];
						WORD cchMsg = 0;

						switch (Diff) {
						case 0:
							cchMsg = mir_sntprintf(szMsg, _countof(szMsg), TranslateT("%s has birthday today."), DB::Contact::DisplayName(hContact));
							break;
						case 1:
							cchMsg = mir_sntprintf(szMsg, _countof(szMsg), TranslateT("%s has birthday tomorrow."), DB::Contact::DisplayName(hContact));
							break;
						default:
							cchMsg = mir_sntprintf(szMsg, _countof(szMsg), TranslateT("%s has birthday in %d days."), DB::Contact::DisplayName(hContact), Diff);
						}
						int age = mtb.Age(&Now);
						if (age > 0)
							switch (GenderOf(hContact)){
							case 0:
								mir_sntprintf(szMsg + cchMsg, _countof(szMsg) - cchMsg,
									TranslateT("\nHe/she becomes %d years old."),
									age + (Diff > 0));
								break;
							case 'M':
								mir_sntprintf(szMsg + cchMsg, _countof(szMsg) - cchMsg,
									TranslateT("\nHe becomes %d years old."),
									age + (Diff > 0));
								break;
							case 'F':
								mir_sntprintf(szMsg + cchMsg, _countof(szMsg) - cchMsg,
									TranslateT("\nShe becomes %d years old."),
									age + (Diff > 0));
								break;
							}
						NotifyWithPopup(hContact, CEvent::BIRTHDAY, Diff, mtb.Description(), szMsg);
					}
					return true;
				}
			}
		}
	}
	return false;
}

/**
* This function checks one contact. It is mainly used for clist extra icon rebuild notification handler.
*
* @param	hContact		- the contact to check
* @param	Now				- current time
* @param	evt				- the reference to a structure, which retrieves the resulting DTB
* @param	bNotify			- if TRUE, a popup will be displayed for a contact having birthday within the next few days.
* @param	LastAnswer		- this parameter is used for the automatic backup function
*
* @return	nothing
**/

static void CheckContact(MCONTACT hContact, MTime &Now, CEvent &evt, BYTE bNotify, PWORD LastAnwer = 0)
{
	// ignore meta subcontacts here as their birthday information are collected explicitly
	if (hContact && (!gRemindOpts.bCheckVisibleOnly || !db_get_b(hContact, MOD_CLIST, "Hidden", FALSE)) && !db_mc_isSub(hContact)) {
		CEvent ca;

		if (CheckBirthday(hContact, Now, ca, bNotify, LastAnwer) || CheckAnniversaries(hContact, Now, ca, bNotify)) {
			evt << ca;
			if (bNotify)
				NotifyFlashCListIcon(hContact, ca);
		}
		NotifyWithExtraIcon(hContact, ca);
	}
}

/**
* This function checks all contacts.
*
* @param	notify			- notification type
*
* @return	nothing
**/

void SvcReminderCheckAll(const ENotify notify)
{
	if (gRemindOpts.RemindState == REMIND_OFF)
		return;

	MTime now;
	now.GetLocalTime();

	// walk through all the contacts stored in the DB
	CEvent evt;
	WORD a1 = 0;
	for (MCONTACT hContact = db_find_first(); hContact != NULL; hContact = db_find_next(hContact))
		CheckContact(hContact, now, evt, notify != NOTIFY_CLIST, &a1);

	if (notify != NOTIFY_CLIST) {
		// play sound for the next anniversary
		NotifyWithSound(evt);

		// popup anniversary list
		if (db_get_b(NULL, MODNAME, SET_ANNIVLIST_POPUP, FALSE))
			DlgAnniversaryListShow(0, 0);

		if (evt._wDaysLeft > gRemindOpts.wDaysEarlier && notify == NOTIFY_NOANNIV)
			NotifyWithPopup(NULL, CEvent::NONE, 0, NULL, TranslateT("No anniversaries to remind of"));
	}
	UpdateTimer(FALSE);
}

/***********************************************************************************************************
 * Event Handler functions
 ***********************************************************************************************************/

/**
* This is the notification handler to tell reminder to reload required icons.
* The reminder only loads icons to clist, which are really required at the moment.
* This should help to save a bit memory.
*
* @param:	wParam			- not used
* @param:	lParam			- not used
*
* @return	This function must return 0 in order to continue in the notification chain.
**/

static int OnCListRebuildIcons(WPARAM, LPARAM)
{
	for (int i = 0; i < _countof(ghCListAnnivIcons); i++)
		ghCListAnnivIcons[i] = INVALID_HANDLE_VALUE;

	for (int k = 0; k < _countof(ghCListBirthdayIcons); k++)
		ghCListBirthdayIcons[k] = INVALID_HANDLE_VALUE;

	return 0;
}

/**
* This function is the notification handler for clist extra icons to be applied for a contact.
*
* @param	hContact		- handle to the contact whose extra icon is to apply
* @param	lParam			- not used
*
* @return	This function must return 0 in order to continue in the notification chain.
**/

int OnCListApplyIcon(MCONTACT hContact, LPARAM)
{
	if (gRemindOpts.RemindState != REMIND_OFF) {
		CEvent evt;
		MTime now;
		now.GetLocalTime();
		CheckContact(hContact, now, evt, FALSE);
	}
	return 0;
}

/**
* This is a notification handler for changed contact settings.
* If any anniversary setting has changed for a meta sub contact,
* the parental meta contact is rescanned.
*
* @param	hContact		- handle of the contect the notification was fired for
* @param	pdbcws			- pointer to a DBCONTACTWRITESETTING structure
*
* @return	This function must return 0 in order to continue in the notification chain.
**/

static int OnContactSettingChanged(MCONTACT hContact, DBCONTACTWRITESETTING* pdbcws)
{
	if (hContact &&										// valid contact not owner!
			ghCListIA &&								// extraicons active
			pdbcws && pdbcws->szSetting &&				// setting structure valid
			(pdbcws->value.type < DBVT_DWORD) &&		// anniversary datatype
			(gRemindOpts.RemindState != REMIND_OFF) &&	// reminder active
			(!strncmp(pdbcws->szSetting, "Birth", 5) ||
			 !strncmp(pdbcws->szSetting, "Anniv", 5) ||
			 !strncmp(pdbcws->szSetting, "DOB", 3)))
	{
		// check metacontact instead of subcontact
		hContact = db_mc_tryMeta(hContact);

		CEvent evt;
		MTime now;
		now.GetLocalTime();
		if (!mir_strcmp(pdbcws->szModule, SvcReminderGetMyBirthdayModule())) {
			WORD LastAnswer = IDNONE;
			CheckContact(hContact, now, evt, FALSE, &LastAnswer);
		}
		else CheckContact(hContact, now, evt, FALSE, 0);
	}
	return 0;
}

#define TBB_IDBTN		"CheckAnniv"
#define TBB_ICONAME	TOOLBARBUTTON_ICONIDPREFIX TBB_IDBTN TOOLBARBUTTON_ICONIDPRIMARYSUFFIX

/**
* This function is called by the ME_TTB_MODULELOADED event.
* It adds a set of buttons to the TopToolbar plugin.
*
* @param	none
*
* @return	nothing
**/

void SvcReminderOnTopToolBarLoaded()
{
	TTBButton ttb = { sizeof(ttb) };
	ttb.dwFlags = TTBBF_VISIBLE | TTBBF_SHOWTOOLTIP;
	ttb.pszService = MS_USERINFO_REMINDER_CHECK;
	ttb.name = ttb.pszTooltipUp = LPGEN("Check anniversaries");
	ttb.hIconHandleUp = IcoLib_GetIconHandle(ICO_COMMON_BIRTHDAY);
	TopToolbar_AddButton(&ttb);
}

/***********************************************************************************************************
 * services
 ***********************************************************************************************************/

/**
* This is the service function for MS_USERINFO_REMINDER_CHECK.
*
* @param:	wParam			- not used
* @param:	lParam			- not used
*
* @return	0
**/

static INT_PTR CheckService(WPARAM, LPARAM)
{
	if (gRemindOpts.RemindState != REMIND_OFF)
		SvcReminderCheckAll(NOTIFY_NOANNIV);
	return 0;
}

/**
* This is the service function for MS_USERINFO_REMINDER_AGGRASIVEBACKUP.
*
* @param	hContact		- handle to single contact or NULL to backup all
* @param	lParam			- if 1, the messagebox will not be displayed
*
* return:	0
**/

static INT_PTR BackupBirthdayService(WPARAM hContact, LPARAM lParam)
{
	MAnnivDate mdb;

	if (hContact) {
		if (!mdb.DBGetBirthDate(hContact))
			mdb.BackupBirthday(hContact, NULL, TRUE);
	}
	else {
		WORD a1 = 0;

		//walk through all the contacts stored in the DB
		for (hContact = db_find_first(); hContact != NULL; hContact = db_find_next(hContact))
			if (!db_mc_isSub(hContact) && !mdb.DBGetBirthDate(hContact))
				mdb.BackupBirthday(hContact, NULL, TRUE, &a1);
	}

	if (lParam != TRUE) {
		MSGBOX mBox;
		mBox.cbSize = sizeof(MSGBOX);
		mBox.hParent = NULL;
		mBox.hiLogo = IcoLib_GetIcon(ICO_COMMON_BIRTHDAY);
		mBox.uType = MB_ICON_INFO;
		mBox.ptszTitle = TranslateT("Update custom birthday");
		mBox.ptszMsg = TranslateT("Backing up and syncing all birthdays complete!");
		MsgBoxService(NULL, (LPARAM)&mBox);
	}
	return 0;
}

/**
* This function returns a constant pointer to the module the date should be saved to
*
* @param	none
*
* @return	module to write birthday information to, MOD_MBIRTHDAY by default
**/

LPCSTR SvcReminderGetMyBirthdayModule()
{
	return ((db_get_b(NULL, MODNAME, SET_REMIND_BIRTHMODULE, DEFVAL_REMIND_BIRTHMODULE) == 1) ? USERINFO : MOD_MBIRTHDAY);
}

/***********************************************************************************************************
 * timer stuff
 ***********************************************************************************************************/

/**
* Timer procedure, called if date changed. This updates clist icons.
*
* @param	hwnd			- not used
* @param	uMsg			- not used
* @param	idEvent			- not used
* @param	dwTime			- not used
* @return	nothing
**/

static void CALLBACK TimerProc_DateChanged(HWND, UINT, UINT_PTR, DWORD)
{
	static MTime last;
	MTime now;
	now.GetLocalTime();
	if (now.Day() > last.Day() || now.Month() > last.Month() || now.Year() > last.Year()) {
		SvcReminderCheckAll(NOTIFY_CLIST);
		last = now;
	}
}

/**
* Timer procedure, called again and again if the notification interval ellapsed
*
* @param	hwnd			- not used
* @param	uMsg			- not used
* @param	idEvent			- not used
* @param	dwTime			- not used
*
* @return	nothing
**/

static void CALLBACK TimerProc_Check(HWND, UINT, UINT_PTR, DWORD)
{
	SvcReminderCheckAll(NOTIFY_POPUP);
}

/**
* Load timers or update them.
*
* @param	bStartup		- is only TRUE if module is loaded to indicate startup process
*
* @return	nothing
**/

static void UpdateTimer(BYTE bStartup)
{
	LONG	wNotifyInterval =	60 * 60 * (LONG)db_get_w(NULL, MODNAME, SET_REMIND_NOTIFYINTERVAL, DEFVAL_REMIND_NOTIFYINTERVAL);
	MTime	now, last;

	now.GetTimeUTC();

	if (bStartup) {
		last.DBGetStamp(NULL, MODNAME, SET_REMIND_LASTCHECK);

		// if last check occured at least one day before just do it on startup again
		if (now.Year() > last.Year() ||	now.Month() > last.Month() ||	now.Day() > last.Day() || db_get_b(NULL, MODNAME, SET_REMIND_CHECKON_STARTUP, FALSE))
			wNotifyInterval = 5;
		else
			wNotifyInterval -= now.Compare(last);

		ghRemindDateChangeTimer = SetTimer(0, 0, 1000 * 60 * 5, (TIMERPROC)TimerProc_DateChanged);
	}
	else now.DBWriteStamp(NULL, MODNAME, SET_REMIND_LASTCHECK);

	// wait at least 5 seconds before checking at startup, to give miranda a better chance to load faster
	KillTimer(0, ghRemindTimer);
	ghRemindTimer = SetTimer(0, 0, 1000 * wNotifyInterval, TimerProc_Check);
}

/***********************************************************************************************************
 * module loading & unloading
 ***********************************************************************************************************/

void SvcReminderEnable(BYTE bEnable)
{
	if (bEnable) { // Reminder is on
		// init hooks
		if (!ghCListIA)
			ghCListIA = HookEvent(ME_CLIST_EXTRA_IMAGE_APPLY, (MIRANDAHOOK)OnCListApplyIcon);

		if (!ghSettingsChanged && !myGlobals.UseDbxTree)
			ghSettingsChanged = HookEvent(ME_DB_CONTACT_SETTINGCHANGED, (MIRANDAHOOK)OnContactSettingChanged);

		// reinit reminder options
		gRemindOpts.RemindState	= db_get_b(NULL, MODNAME, SET_REMIND_ENABLED, DEFVAL_REMIND_ENABLED);
		gRemindOpts.wDaysEarlier = db_get_w(NULL, MODNAME, SET_REMIND_OFFSET, DEFVAL_REMIND_OFFSET);
		gRemindOpts.bCListExtraIcon = db_get_b(NULL, MODNAME, SET_REMIND_EXTRAICON, 1);
		gRemindOpts.bCheckVisibleOnly = db_get_b(NULL, MODNAME, SET_REMIND_CHECKVISIBLE, DEFVAL_REMIND_CHECKVISIBLE);
		gRemindOpts.bFlashCList = db_get_b(NULL, MODNAME, SET_REMIND_FLASHICON, FALSE);
		gRemindOpts.bPopups = ServiceExists(MS_POPUP_ADDPOPUPT) && db_get_b(NULL, MODNAME, SET_POPUP_ENABLED, DEFVAL_POPUP_ENABLED);

		// init the timer
		UpdateTimer(TRUE);
	}
	else { // Reminder is off
		for (MCONTACT hContact = db_find_first(); hContact != NULL; hContact = db_find_next(hContact))
			NotifyWithExtraIcon(hContact, CEvent());

		gRemindOpts.RemindState	= REMIND_OFF;
		SvcReminderUnloadModule();
	}
}

/**
* This function is called by Miranda just after loading all system modules.
*
* @param	none
*
* @return	nothing
**/

void SvcReminderOnModulesLoaded(void)
{
	// init clist extra icon structure
	OnCListRebuildIcons(0, 0);

	SvcReminderEnable(db_get_b(NULL, MODNAME, SET_REMIND_ENABLED, DEFVAL_REMIND_ENABLED) != REMIND_OFF);
}

/**
* This function initially loads all required stuff for reminder.
*
* @param	none
*
* @return	nothing
**/

void SvcReminderLoadModule(void)
{
	// init sounds
	SKINSOUNDDESCEX ssd = { 0 };
	ssd.cbSize = sizeof(ssd);
	ssd.pszSection = MODNAME;

	ssd.pszName = SOUND_BIRTHDAY_TODAY;
	ssd.pszDescription = LPGEN("Birthday reminder");
	ssd.pszDefaultFile = "Sounds\\BirthDay.wav";
	Skin_AddSound(&ssd);

	ssd.pszName = SOUND_BIRTHDAY_SOON;
	ssd.pszDescription = LPGEN("Birthday reminder: it's coming");
	ssd.pszDefaultFile = "Sounds\\BirthDayComing.wav";
	Skin_AddSound(&ssd);

	ssd.pszName = SOUND_ANNIVERSARY;
	ssd.pszDescription = LPGEN("Anniversary Reminder");
	ssd.pszDefaultFile = "Sounds\\Reminder.wav";
	Skin_AddSound(&ssd);

	// create service functions
	CreateServiceFunction(MS_USERINFO_REMINDER_CHECK, CheckService);
	CreateServiceFunction(MS_USERINFO_REMINDER_AGGRASIVEBACKUP, BackupBirthdayService);

	// register hotkey
	HOTKEYDESC hk = { 0 };
	hk.cbSize = sizeof(HOTKEYDESC);
	hk.pszSection = MODNAME;
	hk.pszName = "ReminderCheck";
	hk.pszDescription = LPGEN("Check anniversaries");
	hk.pszService = MS_USERINFO_REMINDER_CHECK;
	Hotkey_Register(&hk);

	if (db_get_b(NULL, MODNAME, SET_REMIND_ENABLED, DEFVAL_REMIND_ENABLED) != REMIND_OFF && ExtraIcon == INVALID_HANDLE_VALUE)
		ExtraIcon = ExtraIcon_Register("Reminder", LPGEN("Reminder (uinfoex)"), ICO_COMMON_ANNIVERSARY);

}

/**
* This function unloads the reminder module.
*
* @param	none
*
* @return	nothing
**/

void SvcReminderUnloadModule(void)
{
	// kill timers
	KillTimer(0, ghRemindTimer);
	ghRemindTimer = 0;
	KillTimer(0, ghRemindDateChangeTimer);
	ghRemindDateChangeTimer = 0;

	// unhook event handlers
	UnhookEvent(ghCListIR);
	ghCListIR = 0;
	UnhookEvent(ghCListIA);
	ghCListIA = 0;
	UnhookEvent(ghSettingsChanged);
	ghSettingsChanged = 0;
}