From f8f31804a06f1944353a69e05eb30a0278c64415 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Mon, 1 Aug 2022 21:01:49 +0300 Subject: fixes #3130 (more glitches in WWI) --- plugins/WhenWasIt/src/WhenWasIt.cpp | 1 - plugins/WhenWasIt/src/add_birthday.cpp | 17 +++-- plugins/WhenWasIt/src/date_utils.cpp | 58 +++++++++------ plugins/WhenWasIt/src/date_utils.h | 18 ++--- plugins/WhenWasIt/src/dlg_handlers.cpp | 124 ++++++++++++++++---------------- plugins/WhenWasIt/src/dlg_handlers.h | 3 +- plugins/WhenWasIt/src/hooked_events.cpp | 5 +- plugins/WhenWasIt/src/services.cpp | 5 +- plugins/WhenWasIt/src/stdafx.h | 1 - 9 files changed, 127 insertions(+), 105 deletions(-) (limited to 'plugins/WhenWasIt/src') diff --git a/plugins/WhenWasIt/src/WhenWasIt.cpp b/plugins/WhenWasIt/src/WhenWasIt.cpp index b58b577ac6..703a59e00c 100644 --- a/plugins/WhenWasIt/src/WhenWasIt.cpp +++ b/plugins/WhenWasIt/src/WhenWasIt.cpp @@ -20,7 +20,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "stdafx.h" -HWND hBirthdaysDlg = nullptr; MWindowList hAddBirthdayWndsList = nullptr; CMPlugin g_plugin; diff --git a/plugins/WhenWasIt/src/add_birthday.cpp b/plugins/WhenWasIt/src/add_birthday.cpp index 9a1fac5ff5..802da3df80 100644 --- a/plugins/WhenWasIt/src/add_birthday.cpp +++ b/plugins/WhenWasIt/src/add_birthday.cpp @@ -26,15 +26,23 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #define COLOR_MICQBIRTHDAY RGB(88, 88, 240) #define COLOR_PROTOCOL RGB(255, 153, 153) +void UpdateBirthday(MCONTACT); + class CAddBirthdayDlg : public CDlgBase { MCONTACT m_hContact; + int m_saveMethod; public: CAddBirthdayDlg(MCONTACT hContact) : CDlgBase(g_plugin, IDD_ADD_BIRTHDAY), m_hContact(hContact) { + if ((int)m_hContact < 0) { + m_saveMethod = DOB_USERINFO; + m_hContact = -m_hContact; + } + else m_saveMethod = DOB_PROTOCOL; } bool OnInitDialog() override @@ -50,7 +58,7 @@ public: HWND hDate = GetDlgItem(m_hwnd, IDC_DATE); int year, month, day; - GetContactDOB(m_hContact, year, month, day); + GetContactDOB(m_hContact, year, month, day, m_saveMethod); if (IsDOBValid(year, month, day)) { SYSTEMTIME st = { 0 }; st.wDay = day; @@ -69,12 +77,11 @@ public: HWND hDate = GetDlgItem(m_hwnd, IDC_DATE); SYSTEMTIME st; if (DateTime_GetSystemtime(hDate, &st) == GDT_VALID) - SaveBirthday(m_hContact, st.wYear, st.wMonth, st.wDay, SAVE_MODE_STANDARD); + SaveBirthday(m_hContact, st.wYear, st.wMonth, st.wDay, m_saveMethod); else - SaveBirthday(m_hContact, 0, 0, 0, SAVE_MODE_DELETEALL); + DeleteBirthday(m_hContact); - if (hBirthdaysDlg != nullptr) - SendMessage(hBirthdaysDlg, WWIM_UPDATE_BIRTHDAY, m_hContact, NULL); + UpdateBirthday((m_saveMethod == DOB_PROTOCOL) ? m_hContact : -m_hContact); return true; } diff --git a/plugins/WhenWasIt/src/date_utils.cpp b/plugins/WhenWasIt/src/date_utils.cpp index bc24fc89ab..eba383bd48 100644 --- a/plugins/WhenWasIt/src/date_utils.cpp +++ b/plugins/WhenWasIt/src/date_utils.cpp @@ -33,15 +33,15 @@ bool IsDOBValid(int, int month, int day) return (month != 0 && day != 0); } -int GetContactDOB(MCONTACT hContact, int &year, int &month, int &day, int module) +int GetContactDOB(MCONTACT hContact, int &year, int &month, int &day, int iModule) { - if (module != DOB_PROTOCOL) { + if (iModule != DOB_PROTOCOL) { year = db_get_w(hContact, "UserInfo", "BirthYear", 0); month = db_get_b(hContact, "UserInfo", "BirthMonth", 0); day = db_get_b(hContact, "UserInfo", "BirthDay", 0); if (IsDOBValid(year, month, day)) return DOB_USERINFO; - if (module == DOB_USERINFO) + if (iModule == DOB_USERINFO) return DOB_UNKNOWN; } @@ -55,17 +55,30 @@ int GetContactDOB(MCONTACT hContact, int &year, int &month, int &day, int module return DOB_UNKNOWN; } +int GetContactAge(int year, int month, int day) +{ + if (year == 0) + return 0; + + time_t now = Today(); + struct tm *today = gmtime(&now); + int currentDay = today->tm_mday + 1; + int currentMonth = today->tm_mon + 1; + + int age = (today->tm_year + 1900) - year; + + if (g_plugin.cShowAgeMode) + if (month > currentMonth|| (month == currentMonth) && (day > currentDay)) // birthday still to come + age--; + + return age; +} + int GetContactAge(MCONTACT hContact) { int year, month, day; - time_t tNow; - time(&tNow); - struct tm *now = localtime(&tNow); GetContactDOB(hContact, year, month, day); - if (year == 0) - return 0; - else - return (now->tm_year + 1900) - year; + return GetContactAge(year, month, day); } char GetContactGender(MCONTACT hContact) @@ -151,17 +164,18 @@ int DaysAfterBirthday(time_t now, int ctYear, int ctMonth, int ctDay) return -1; } -int SaveBirthday(MCONTACT hContact, int year, int month, int day, int mode) +void DeleteBirthday(MCONTACT hContact) { - if (mode == SAVE_MODE_DELETEALL) { - db_unset(hContact, "UserInfo", "BirthYear"); - db_unset(hContact, "UserInfo", "BirthMonth"); - db_unset(hContact, "UserInfo", "BirthDay"); - } - else { - db_set_dw(hContact, "UserInfo", "BirthYear", year); - db_set_b(hContact, "UserInfo", "BirthMonth", month); - db_set_b(hContact, "UserInfo", "BirthDay", day); - } - return 0; + db_unset(hContact, "UserInfo", "BirthYear"); + db_unset(hContact, "UserInfo", "BirthMonth"); + db_unset(hContact, "UserInfo", "BirthDay"); +} + +void SaveBirthday(MCONTACT hContact, int year, int month, int day, int mode) +{ + const char *szModule = (mode == DOB_PROTOCOL) ? Proto_GetBaseAccountName(hContact) : "UserInfo"; + + db_set_dw(hContact, szModule, "BirthYear", year); + db_set_b(hContact, szModule, "BirthMonth", month); + db_set_b(hContact, szModule, "BirthDay", day); } diff --git a/plugins/WhenWasIt/src/date_utils.h b/plugins/WhenWasIt/src/date_utils.h index a111a1c0b8..341ccabb3f 100644 --- a/plugins/WhenWasIt/src/date_utils.h +++ b/plugins/WhenWasIt/src/date_utils.h @@ -23,12 +23,9 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "errno.h" -#define SAVE_MODE_STANDARD 0 -#define SAVE_MODE_DELETEALL 100 - -#define DOB_UNKNOWN 100 -#define DOB_USERINFO 101 -#define DOB_PROTOCOL 103 +#define DOB_UNKNOWN 100 +#define DOB_USERINFO 101 +#define DOB_PROTOCOL 103 time_t Today(); bool IsLeapYear(int year); @@ -36,10 +33,13 @@ bool IsDOBValid(int year, int month, int day); unsigned int DaysToBirthday(time_t now, int ctYear, int ctMonth, int ctDay); int DaysAfterBirthday(time_t now, int ctYear, int ctMonth, int ctDay); -int GetContactAge(MCONTACT hContact); -int GetContactDOB(MCONTACT hContact, int &year, int &month, int &day, int module = DOB_UNKNOWN); +int GetContactDOB(MCONTACT hContact, int &year, int &month, int &day, int iModule = DOB_UNKNOWN); char GetContactGender(MCONTACT hContact); -int SaveBirthday(MCONTACT hContact, int year, int month, int day, int mode); +int GetContactAge(MCONTACT hContact); +int GetContactAge(int year, int month, int day); + +void SaveBirthday(MCONTACT hContact, int year, int month, int day, int mode); +void DeleteBirthday(MCONTACT hContact); #endif //H_WWI_DATE_UTILS_H \ No newline at end of file diff --git a/plugins/WhenWasIt/src/dlg_handlers.cpp b/plugins/WhenWasIt/src/dlg_handlers.cpp index f6ac30149c..2b5d24ed10 100644 --- a/plugins/WhenWasIt/src/dlg_handlers.cpp +++ b/plugins/WhenWasIt/src/dlg_handlers.cpp @@ -23,10 +23,10 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #define NA TranslateT("N/A") -wchar_t* GetBirthdayModule(int module, MCONTACT) +wchar_t* GetBirthdayModule(int iModule, MCONTACT) { - switch (module) { - case DOB_PROTOCOL: return TranslateT("Protocol module"); + switch (iModule) { + case DOB_PROTOCOL: return TranslateT("Protocol iModule"); case DOB_USERINFO: return L"UserInfo"; } return NA; @@ -79,7 +79,7 @@ CBasicListDlg::CBasicListDlg(int dlgId) : m_list.OnBuildMenu = Callback(this, &CBasicListDlg::onMenu_List); } -void CBasicListDlg::onDblClick_List(CCtrlListView::TEventInfo*) +MCONTACT CBasicListDlg::SelectedItem() { int count = m_list.GetItemCount(); @@ -89,29 +89,27 @@ void CBasicListDlg::onDblClick_List(CCtrlListView::TEventInfo*) if (m_list.GetItemState(i, LVIS_SELECTED)) { item.iItem = i; m_list.GetItem(&item); - CallService(MS_WWI_ADD_BIRTHDAY, (MCONTACT)item.lParam, 0); - break; + + int res = item.lParam; + return (res < 0) ? -res : res; } } + return 0; } -void CBasicListDlg::onMenu_List(CContextMenuPos *pos) +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); + if (MCONTACT hContact = SelectedItem()) + CallService(MS_WWI_ADD_BIRTHDAY, hContact, 0); +} - 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::onMenu_List(CContextMenuPos *pos) +{ + if (MCONTACT hContact = SelectedItem()) { + HMENU hMenu = Menu_BuildContactMenu(hContact); + if (hMenu != nullptr) { + Clist_MenuProcessCommand(TrackPopupMenu(hMenu, TPM_RIGHTBUTTON | TPM_RETURNCMD, pos->pt.x, pos->pt.y, 0, m_list.GetHwnd(), nullptr), MPCF_CONTACTMENU, hContact); + DestroyMenu(hMenu); } } } @@ -127,12 +125,10 @@ void CBasicListDlg::Sort(int iCol) ///////////////////////////////////////////////////////////////////////////////////////// // Birthday list dialog +static class CBirthdaysDlg *g_pDialog; + 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())); @@ -152,44 +148,18 @@ class CBirthdaysDlg : public CBasicListDlg 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) + // 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 iModule) { - 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); + GetContactDOB(hContact, year, month, day, iModule); 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--; + int age = GetContactAge(year, month, day); char *szProto = Proto_GetBaseAccountName(hContact); PROTOACCOUNT *pAcc = Proto_GetAccount(szProto); @@ -198,7 +168,7 @@ class CBirthdaysDlg : public CBasicListDlg LVITEM item = { 0 }; item.mask = LVIF_TEXT | LVIF_PARAM; item.iItem = entry; - item.lParam = (module == DOB_PROTOCOL) ? hContact : -hContact; + item.lParam = (iModule == DOB_PROTOCOL) ? hContact : -hContact; item.pszText = ptszAccName; if (bAdd) @@ -227,7 +197,7 @@ class CBirthdaysDlg : public CBasicListDlg mir_snwprintf(buffer, NA); m_list.SetItemText(entry, 4, buffer); - m_list.SetItemText(entry, 5, GetBirthdayModule(module, hContact)); + m_list.SetItemText(entry, 5, GetBirthdayModule(iModule, hContact)); res++; } else if (!bShowAll && !bAdd) @@ -252,6 +222,7 @@ public: bool OnInitDialog() override { + g_pDialog = this; Window_SetIcon_IcoLib(m_hwnd, hListMenu); m_list.SetExtendedListViewStyleEx(LVS_EX_FULLROWSELECT, LVS_EX_FULLROWSELECT); @@ -305,7 +276,7 @@ public: void OnDestroy() override { - hBirthdaysDlg = nullptr; + g_pDialog = nullptr; Utils_SaveWindowPosition(m_hwnd, NULL, MODULENAME, "BirthdayList"); Window_FreeIcon_IcoLib(m_hwnd); lastColumn = -1; @@ -324,17 +295,50 @@ public: lastColumn = (column == lastColumn) ? -1 : column; } + + void UpdateContact(MCONTACT hContact) + { + LVFINDINFO fi = { 0 }; + fi.flags = LVFI_PARAM; + fi.lParam = hContact; + int idx = m_list.FindItem(-1, &fi); + + int iModule; + if ((int)hContact < 0) { + iModule = DOB_USERINFO; + hContact = -hContact; + } + else iModule = DOB_PROTOCOL; + + if (-1 == idx) + UpdateBirthdayEntry(hContact, m_list.GetItemCount(), 1, iModule); + else + UpdateBirthdayEntry(hContact, idx, 0, iModule); + SetBirthdaysCount(); + } }; INT_PTR ShowListService(WPARAM, LPARAM) { - if (!hBirthdaysDlg) + if (!g_pDialog) (new CBirthdaysDlg())->Show(); else - ShowWindow(hBirthdaysDlg, SW_SHOW); + ShowWindow(g_pDialog->GetHwnd(), SW_SHOW); return 0; } +void UpdateBirthday(MCONTACT hContact) +{ + if (g_pDialog) + g_pDialog->UpdateContact(hContact); +} + +void CloseBirthdayList() +{ + if (g_pDialog) + g_pDialog->Close(); +} + ///////////////////////////////////////////////////////////////////////////////////////// // Popups diff --git a/plugins/WhenWasIt/src/dlg_handlers.h b/plugins/WhenWasIt/src/dlg_handlers.h index 587a1c9936..d9678427c1 100644 --- a/plugins/WhenWasIt/src/dlg_handlers.h +++ b/plugins/WhenWasIt/src/dlg_handlers.h @@ -33,13 +33,14 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #define CLIST_ICON 4 -#define WWIM_UPDATE_BIRTHDAY (WM_USER + 101) #define WWIM_ADD_UPCOMING_BIRTHDAY (WM_USER + 102) int OnOptionsInitialise(WPARAM wParam, LPARAM); class CBasicListDlg : public CDlgBase { + MCONTACT SelectedItem(); + protected: CCtrlListView m_list; diff --git a/plugins/WhenWasIt/src/hooked_events.cpp b/plugins/WhenWasIt/src/hooked_events.cpp index 621505ccf3..6c83cd2408 100644 --- a/plugins/WhenWasIt/src/hooked_events.cpp +++ b/plugins/WhenWasIt/src/hooked_events.cpp @@ -28,6 +28,7 @@ UINT_PTR hCheckTimer = NULL; UINT_PTR hDateChangeTimer = NULL; static int currentDay = 0; +void CloseBirthdayList(); void CloseUpcoming(); static int OnTopToolBarModuleLoaded(WPARAM, LPARAM) @@ -52,9 +53,7 @@ static int OnContactSettingChanged(WPARAM hContact, LPARAM lParam) static int OnShutdown(WPARAM, LPARAM) { - if (hBirthdaysDlg) - SendMessage(hBirthdaysDlg, WM_CLOSE, 0, 0); - + CloseBirthdayList(); CloseUpcoming(); WindowList_Broadcast(hAddBirthdayWndsList, WM_CLOSE, 0, 0); diff --git a/plugins/WhenWasIt/src/services.cpp b/plugins/WhenWasIt/src/services.cpp index e5f67af75f..d3cea83f54 100644 --- a/plugins/WhenWasIt/src/services.cpp +++ b/plugins/WhenWasIt/src/services.cpp @@ -242,7 +242,7 @@ int DoImport(wchar_t *fileName) int year, month, day; swscanf(delAccount, L" : %02d/%02d/%04d", &day, &month, &year); - SaveBirthday(hContact, year, month, day, SAVE_MODE_STANDARD); + SaveBirthday(hContact, year, month, day, DOB_USERINFO); } else { CMStringW msg(FORMAT, TranslateT("Could not find UID '%s [%S]' in current database, skipping"), szHandle, szProto); @@ -263,9 +263,8 @@ int DoExport(wchar_t *fileName) MessageBox(nullptr, TranslateT("Could not open file to export birthdays"), TranslateT("Error"), MB_OK | MB_ICONERROR); return 1; } + fwprintf(fout, L"%c%s", COMMENT_CHAR, TranslateT("Please do not edit this file by hand. Use the export function of WhenWasIt plugin.\n")); - fwprintf(fout, L"%c%s", COMMENT_CHAR, TranslateT("Warning! Please do not mix Unicode and Ansi exported birthday files. You should use the same version (Ansi/Unicode) of WhenWasIt that was used to export the info.\n")); - fwprintf(fout, L"%c%s", COMMENT_CHAR, TranslateT("This file was exported with a Unicode version of WhenWasIt. Please only use a Unicode version of the plugin to import the birthdays.\n")); for (auto &hContact : Contacts()) { int year, month, day; diff --git a/plugins/WhenWasIt/src/stdafx.h b/plugins/WhenWasIt/src/stdafx.h index d6ffa93b56..4726a622d7 100644 --- a/plugins/WhenWasIt/src/stdafx.h +++ b/plugins/WhenWasIt/src/stdafx.h @@ -76,7 +76,6 @@ struct CMPlugin : public PLUGIN int Unload() override; }; -extern HWND hBirthdaysDlg; extern MWindowList hAddBirthdayWndsList; #endif //M_WWI_COMMONHEADERS_H \ No newline at end of file -- cgit v1.2.3