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
|
/*
WhenWasIt (birthday reminder) plugin for Miranda IM
Copyright © 2006 Cristian Libotean
Copyright (C) 2014-22 Rozhuk Ivan
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 NA TranslateT("N/A")
wchar_t* GetBirthdayModule(int module, MCONTACT)
{
switch (module) {
case DOB_PROTOCOL: return TranslateT("Protocol module");
case DOB_USERINFO: return L"UserInfo";
}
return NA;
}
static int lastColumn = -1;
struct BirthdaysSortParams
{
HWND hList;
int column;
};
int CALLBACK BirthdaysCompare(LPARAM lParam1, LPARAM lParam2, LPARAM myParam)
{
BirthdaysSortParams params = *(BirthdaysSortParams *)myParam;
const int maxSize = 1024;
wchar_t text1[maxSize];
wchar_t text2[maxSize];
long value1, value2;
ListView_GetItemText(params.hList, (int)lParam1, params.column, text1, _countof(text1));
ListView_GetItemText(params.hList, (int)lParam2, params.column, text2, _countof(text2));
int res;
if ((params.column == 2) || (params.column == 4)) {
wchar_t *err1, *err2;
value1 = wcstol(text1, &err1, 10);
value2 = wcstol(text2, &err2, 10);
if ((err1[0]) || (err2[0]))
res = (err1[0]) ? 1 : -1;
else if (value1 < value2)
res = -1;
else
res = (value1 != value2);
}
else res = mir_wstrcmpi(text1, text2);
return (params.column == lastColumn) ? -res : res;
}
/////////////////////////////////////////////////////////////////////////////////////////
// Basic list dialog
CBasicListDlg::CBasicListDlg(int dlgId) :
CDlgBase(g_plugin, dlgId),
m_list(this, IDC_BIRTHDAYS_LIST)
{
m_list.OnDoubleClick = Callback(this, &CBasicListDlg::onDblClick_List);
m_list.OnBuildMenu = Callback(this, &CBasicListDlg::onMenu_List);
}
void CBasicListDlg::onDblClick_List(CCtrlListView::TEventInfo*)
{
int count = m_list.GetItemCount();
LVITEM item = {};
item.mask = LVIF_PARAM;
for (int i = 0; i < count; i++) {
if (m_list.GetItemState(i, LVIS_SELECTED)) {
item.iItem = i;
m_list.GetItem(&item);
CallService(MS_WWI_ADD_BIRTHDAY, (MCONTACT)item.lParam, 0);
break;
}
}
}
void CBasicListDlg::onMenu_List(CContextMenuPos *pos)
{
int count = m_list.GetItemCount();
LVITEM item = {};
item.mask = LVIF_PARAM;
for (int i = 0; i < count; i++) {
if (m_list.GetItemState(i, LVIS_SELECTED)) {
item.iItem = i;
m_list.GetItem(&item);
HMENU hMenu = Menu_BuildContactMenu((MCONTACT)item.lParam);
if (hMenu != nullptr) {
Clist_MenuProcessCommand(TrackPopupMenu(hMenu, TPM_RIGHTBUTTON | TPM_RETURNCMD, pos->pt.x, pos->pt.y, 0, m_list.GetHwnd(), nullptr), MPCF_CONTACTMENU, (MCONTACT)item.lParam);
DestroyMenu(hMenu);
}
break;
}
}
}
void CBasicListDlg::Sort(int iCol)
{
BirthdaysSortParams params = {};
params.hList = m_list.GetHwnd();
params.column = iCol;
m_list.SortItemsEx(BirthdaysCompare, (LPARAM)¶ms);
}
/////////////////////////////////////////////////////////////////////////////////////////
// Birthday list dialog
class CBirthdaysDlg : public CBasicListDlg
{
UI_MESSAGE_MAP(CBirthdaysDlg, CBasicListDlg);
UI_MESSAGE(WWIM_UPDATE_BIRTHDAY, OnUpdateContact);
UI_MESSAGE_MAP_END();
void SetBirthdaysCount()
{
SetCaption(CMStringW(FORMAT, TranslateT("Birthday list (%d)"), m_list.GetItemCount()));
}
int LoadBirthdays()
{
m_list.DeleteAllItems();
int count = 0;
for (auto &hContact : Contacts()) {
count = UpdateBirthdayEntry(hContact, count, 1, DOB_USERINFO);
count = UpdateBirthdayEntry(hContact, count, 1, DOB_PROTOCOL);
}
SetBirthdaysCount();
return 0;
}
INT_PTR OnUpdateContact(UINT, WPARAM hContact, LPARAM)
{
LVFINDINFO fi = { 0 };
fi.flags = LVFI_PARAM;
fi.lParam = -hContact;
int idx = m_list.FindItem(-1, &fi);
if (-1 == idx)
UpdateBirthdayEntry(hContact, m_list.GetItemCount(), 1, DOB_USERINFO);
else
UpdateBirthdayEntry(hContact, idx, 0, DOB_USERINFO);
SetBirthdaysCount();
return 0;
}
//only updates the birthday part of the list view entry. Won't update the szProto and the contact name (those shouldn't change anyway :))
int UpdateBirthdayEntry(MCONTACT hContact, int entry, int bAdd, int module)
{
int currentMonth, currentDay;
int res = entry;
bool bShowAll = chkShowAll.GetState();
if (g_plugin.cShowAgeMode) {
time_t now = Today();
struct tm *today = gmtime(&now);
currentDay = today->tm_mday + 1;
currentMonth = today->tm_mon + 1;
}
else currentMonth = currentDay = 0;
int year = 0, month = 0, day = 0;
GetContactDOB(hContact, year, month, day, module);
if (bShowAll || IsDOBValid(year, month, day)) {
lastColumn = -1; //list isn't sorted anymore
int dtb = DaysToBirthday(Today(), year, month, day);
int age = GetContactAge(hContact);
if (g_plugin.cShowAgeMode)
if (month > currentMonth || (month == currentMonth) && (day > currentDay)) // birthday still to come
age--;
char *szProto = Proto_GetBaseAccountName(hContact);
PROTOACCOUNT *pAcc = Proto_GetAccount(szProto);
wchar_t *ptszAccName = (pAcc == nullptr) ? TranslateT("Unknown") : pAcc->tszAccountName;
LVITEM item = { 0 };
item.mask = LVIF_TEXT | LVIF_PARAM;
item.iItem = entry;
item.lParam = (module == DOB_PROTOCOL) ? hContact : -hContact;
item.pszText = ptszAccName;
if (bAdd)
m_list.InsertItem(&item);
else
m_list.SetItemText(entry, 0, ptszAccName);
m_list.SetItemText(entry, 1, Clist_GetContactDisplayName(hContact));
wchar_t buffer[2048];
if ((dtb <= 366) && (dtb >= 0))
mir_snwprintf(buffer, L"%d", dtb);
else
mir_snwprintf(buffer, NA);
m_list.SetItemText(entry, 2, buffer);
if ((month != 0) && (day != 0))
mir_snwprintf(buffer, L"%04d-%02d-%02d", year, month, day);
else
mir_snwprintf(buffer, NA);
m_list.SetItemText(entry, 3, buffer);
if (age < 400 && age > 0) //hopefully noone lives longer than this :)
mir_snwprintf(buffer, L"%d", age);
else
mir_snwprintf(buffer, NA);
m_list.SetItemText(entry, 4, buffer);
m_list.SetItemText(entry, 5, GetBirthdayModule(module, hContact));
res++;
}
else if (!bShowAll && !bAdd)
m_list.DeleteItem(entry);
return res;
}
CCtrlCheck chkShowAll;
public:
CBirthdaysDlg() :
CBasicListDlg(IDD_BIRTHDAYS),
chkShowAll(this, IDC_SHOW_ALL)
{
SetMinSize(200, 200);
chkShowAll.OnChange = Callback(this, &CBirthdaysDlg::onChange_ShowAll);
m_list.OnColumnClick = Callback(this, &CBirthdaysDlg::onColumnClick);
}
bool OnInitDialog() override
{
Window_SetIcon_IcoLib(m_hwnd, hListMenu);
m_list.SetExtendedListViewStyleEx(LVS_EX_FULLROWSELECT, LVS_EX_FULLROWSELECT);
LVCOLUMN col;
col.mask = LVCF_TEXT | LVCF_WIDTH;
col.pszText = TranslateT("Protocol");
col.cx = 80;
m_list.InsertColumn(0, &col);
col.pszText = TranslateT("Contact");
col.cx = 180;
m_list.InsertColumn(1, &col);
col.pszText = TranslateT("DTB");
col.cx = 50;
m_list.InsertColumn(2, &col);
col.pszText = TranslateT("Birthday");
col.cx = 80;
m_list.InsertColumn(3, &col);
col.pszText = TranslateT("Age");
col.cx = 50;
m_list.InsertColumn(4, &col);
col.pszText = TranslateT("Module");
col.cx = 108;
m_list.InsertColumn(5, &col);
LoadBirthdays();
int column = g_plugin.getByte("SortColumn", 0);
Sort(column);
Utils_RestoreWindowPosition(m_hwnd, NULL, MODULENAME, "BirthdayList");
return true;
}
int Resizer(UTILRESIZECONTROL *urc) override
{
switch (urc->wId) {
case IDC_SHOW_ALL:
return RD_ANCHORX_LEFT | RD_ANCHORY_BOTTOM;
case IDC_BIRTHDAYS_LIST:
return RD_ANCHORX_WIDTH | RD_ANCHORY_HEIGHT;
}
return RD_ANCHORX_RIGHT | RD_ANCHORY_BOTTOM;
}
void OnDestroy() override
{
hBirthdaysDlg = nullptr;
Utils_SaveWindowPosition(m_hwnd, NULL, MODULENAME, "BirthdayList");
Window_FreeIcon_IcoLib(m_hwnd);
lastColumn = -1;
}
void onChange_ShowAll(CCtrlCheck*)
{
LoadBirthdays();
}
void onColumnClick(CCtrlListView::TEventInfo *ev)
{
int column = ev->nmlv->iSubItem;
g_plugin.setByte("SortColumn", column);
Sort(column);
lastColumn = (column == lastColumn) ? -1 : column;
}
};
INT_PTR ShowListService(WPARAM, LPARAM)
{
if (!hBirthdaysDlg)
(new CBirthdaysDlg())->Show();
else
ShowWindow(hBirthdaysDlg, SW_SHOW);
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////
// Popups
int HandlePopupClick(HWND hWnd, int action)
{
MCONTACT hContact;
switch (action) {
case 2: //OPEN MESSAGE WINDOW
hContact = (MCONTACT)PUGetContact(hWnd);
if (hContact)
CallServiceSync(MS_MSG_SENDMESSAGE, hContact, 0);
case 1: //DISMISS
PUDeletePopup(hWnd);
break;
}
return 0;
}
LRESULT CALLBACK DlgProcPopup(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg) {
case WM_COMMAND:
switch (HIWORD(wParam)) {
case STN_CLICKED:
HandlePopupClick(hWnd, g_plugin.lPopupClick);
break;
}
break;
case WM_CONTEXTMENU:
HandlePopupClick(hWnd, g_plugin.rPopupClick);
break;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
|