From bcb27264ba737778e5d3edad36088bacf74f0236 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Tue, 9 Apr 2013 20:03:46 +0000 Subject: - short function names allows to write database loops in one string; - 'continue' operator can be used then; - multiple bugs fixed in clists; - code becomes much more compact; git-svn-id: http://svn.miranda-ng.org/main/trunk@4403 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/FavContacts/src/contact_cache.cpp | 10 +- plugins/FavContacts/src/favlist.h | 11 +- plugins/FavContacts/src/main.cpp | 277 +++++++++++++----------------- 3 files changed, 129 insertions(+), 169 deletions(-) (limited to 'plugins/FavContacts/src') diff --git a/plugins/FavContacts/src/contact_cache.cpp b/plugins/FavContacts/src/contact_cache.cpp index 1167d98f0d..0cfc3b4757 100644 --- a/plugins/FavContacts/src/contact_cache.cpp +++ b/plugins/FavContacts/src/contact_cache.cpp @@ -81,16 +81,12 @@ void CContactCache::Rebuild() unsigned long timestamp = time(NULL); m_lastUpdate = time(NULL); - HANDLE hContact = db_find_first(); - while (hContact) - { + for (HANDLE hContact = db_find_first(); hContact; hContact = db_find_next(hContact)) { TContactInfo *info = new TContactInfo; info->hContact = hContact; info->rate = 0; - HANDLE hEvent = db_event_last(hContact); - while (hEvent) - { + for (HANDLE hEvent = db_event_last(hContact); hEvent; hEvent = db_event_prev(hEvent)) { DBEVENTINFO dbei = { sizeof(dbei) }; if (!db_event_get(hEvent, &dbei)) { if (float weight = GetEventWeight(timestamp - dbei.timestamp)) @@ -100,11 +96,9 @@ void CContactCache::Rebuild() } else break; } - hEvent = db_event_prev(hEvent); } m_cache.insert(info); - hContact = db_find_next(hContact); } } diff --git a/plugins/FavContacts/src/favlist.h b/plugins/FavContacts/src/favlist.h index 40f44b6e6d..a9db3fb3dd 100644 --- a/plugins/FavContacts/src/favlist.h +++ b/plugins/FavContacts/src/favlist.h @@ -99,12 +99,10 @@ public: void build() { TCHAR *prevGroup = NULL; - int i; - + nGroups = 1; - HANDLE hContact = db_find_first(); - for ( ; hContact; hContact = db_find_next(hContact)) + for (HANDLE hContact = db_find_first(); hContact; hContact = db_find_next(hContact)) if (db_get_b(hContact, "FavContacts", "IsFavourite", 0)) { TCHAR *group = addContact(hContact, true)->getGroup(); @@ -114,9 +112,8 @@ public: } int nRecent = 0; - for (i = 0; nRecent < g_Options.wMaxRecent; ++i) - { - hContact = g_contactCache->get(i); + for (int i = 0; nRecent < g_Options.wMaxRecent; ++i) { + HANDLE hContact = g_contactCache->get(i); if (!hContact) break; if (!db_get_b(hContact, "FavContacts", "IsFavourite", 0)) { diff --git a/plugins/FavContacts/src/main.cpp b/plugins/FavContacts/src/main.cpp index fdef5aeb9b..3abcf07a58 100644 --- a/plugins/FavContacts/src/main.cpp +++ b/plugins/FavContacts/src/main.cpp @@ -231,8 +231,7 @@ int ProcessModulesLoaded(WPARAM wParam, LPARAM lParam) Hotkey_Register(&hotkey); if (ServiceExists(MS_AV_GETAVATARBITMAP)) { - HANDLE hContact = db_find_first(); - for ( ; hContact; hContact = db_find_next(hContact)) + for (HANDLE hContact = db_find_first(); hContact; hContact = db_find_next(hContact)) if (db_get_b(hContact, "FavContacts", "IsFavourite", 0)) CallService(MS_AV_GETAVATARBITMAP, (WPARAM)hContact, 0); } @@ -1097,50 +1096,43 @@ static INT_PTR CALLBACK OptionsDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARA static bool bInitialized = false; static HANDLE hSelectedContact = 0; - switch (msg) - { - case WM_INITDIALOG: + switch (msg) { + case WM_INITDIALOG: + bInitialized = false; + + TranslateDialogDefault(hwnd); + + CheckDlgButton(hwnd, IDC_CHK_GROUPS, g_Options.bUseGroups ? BST_CHECKED : BST_UNCHECKED); + CheckDlgButton(hwnd, IDC_CHK_GROUPCOLUMS, g_Options.bUseColumns ? BST_CHECKED : BST_UNCHECKED); + CheckDlgButton(hwnd, IDC_CHK_SECONDLINE, g_Options.bSecondLine ? BST_CHECKED : BST_UNCHECKED); + CheckDlgButton(hwnd, IDC_CHK_AVATARS, g_Options.bAvatars ? BST_CHECKED : BST_UNCHECKED); + CheckDlgButton(hwnd, IDC_CHK_AVATARBORDER, g_Options.bAvatarBorder ? BST_CHECKED : BST_UNCHECKED); + CheckDlgButton(hwnd, IDC_CHK_NOTRANSPARENTBORDER, g_Options.bNoTransparentBorder ? BST_CHECKED : BST_UNCHECKED); + CheckDlgButton(hwnd, IDC_CHK_SYSCOLORS, g_Options.bSysColors ? BST_CHECKED : BST_UNCHECKED); + CheckDlgButton(hwnd, IDC_CHK_CENTERHOTKEY, g_Options.bCenterHotkey ? BST_CHECKED : BST_UNCHECKED); + CheckDlgButton(hwnd, IDC_CHK_RIGHTAVATARS, g_Options.bRightAvatars ? BST_CHECKED : BST_UNCHECKED); + CheckDlgButton(hwnd, IDC_CHK_DIMIDLE, g_Options.bDimIdle ? BST_CHECKED : BST_UNCHECKED); + SetDlgItemInt(hwnd, IDC_TXT_RADIUS, g_Options.wAvatarRadius, FALSE); + SetDlgItemInt(hwnd, IDC_TXT_MAXRECENT, g_Options.wMaxRecent, FALSE); + + SetWindowLongPtr(GetDlgItem(hwnd, IDC_CLIST), GWL_STYLE, + GetWindowLongPtr(GetDlgItem(hwnd, IDC_CLIST), GWL_STYLE)|CLS_CHECKBOXES|CLS_HIDEEMPTYGROUPS|CLS_USEGROUPS|CLS_GREYALTERNATE|CLS_GROUPCHECKBOXES); + SendMessage(GetDlgItem(hwnd, IDC_CLIST), CLM_SETEXSTYLE, CLS_EX_DISABLEDRAGDROP|CLS_EX_TRACKSELECT, 0); + sttResetListOptions(GetDlgItem(hwnd, IDC_CLIST)); + + hSelectedContact = db_find_first(); { - bInitialized = false; - - TranslateDialogDefault(hwnd); - - CheckDlgButton(hwnd, IDC_CHK_GROUPS, g_Options.bUseGroups ? BST_CHECKED : BST_UNCHECKED); - CheckDlgButton(hwnd, IDC_CHK_GROUPCOLUMS, g_Options.bUseColumns ? BST_CHECKED : BST_UNCHECKED); - CheckDlgButton(hwnd, IDC_CHK_SECONDLINE, g_Options.bSecondLine ? BST_CHECKED : BST_UNCHECKED); - CheckDlgButton(hwnd, IDC_CHK_AVATARS, g_Options.bAvatars ? BST_CHECKED : BST_UNCHECKED); - CheckDlgButton(hwnd, IDC_CHK_AVATARBORDER, g_Options.bAvatarBorder ? BST_CHECKED : BST_UNCHECKED); - CheckDlgButton(hwnd, IDC_CHK_NOTRANSPARENTBORDER, g_Options.bNoTransparentBorder ? BST_CHECKED : BST_UNCHECKED); - CheckDlgButton(hwnd, IDC_CHK_SYSCOLORS, g_Options.bSysColors ? BST_CHECKED : BST_UNCHECKED); - CheckDlgButton(hwnd, IDC_CHK_CENTERHOTKEY, g_Options.bCenterHotkey ? BST_CHECKED : BST_UNCHECKED); - CheckDlgButton(hwnd, IDC_CHK_RIGHTAVATARS, g_Options.bRightAvatars ? BST_CHECKED : BST_UNCHECKED); - CheckDlgButton(hwnd, IDC_CHK_DIMIDLE, g_Options.bDimIdle ? BST_CHECKED : BST_UNCHECKED); - SetDlgItemInt(hwnd, IDC_TXT_RADIUS, g_Options.wAvatarRadius, FALSE); - SetDlgItemInt(hwnd, IDC_TXT_MAXRECENT, g_Options.wMaxRecent, FALSE); - - SetWindowLongPtr(GetDlgItem(hwnd, IDC_CLIST), GWL_STYLE, - GetWindowLongPtr(GetDlgItem(hwnd, IDC_CLIST), GWL_STYLE)|CLS_CHECKBOXES|CLS_HIDEEMPTYGROUPS|CLS_USEGROUPS|CLS_GREYALTERNATE|CLS_GROUPCHECKBOXES); - SendMessage(GetDlgItem(hwnd, IDC_CLIST), CLM_SETEXSTYLE, CLS_EX_DISABLEDRAGDROP|CLS_EX_TRACKSELECT, 0); - sttResetListOptions(GetDlgItem(hwnd, IDC_CLIST)); - - hSelectedContact = db_find_first(); - - HANDLE hContact = db_find_first(); - for ( ; hContact; hContact = db_find_next(hContact)) - { + for (HANDLE hContact = db_find_first(); hContact; hContact = db_find_next(hContact)) SendDlgItemMessage(hwnd, IDC_CLIST, CLM_SETCHECKMARK, - SendDlgItemMessage(hwnd, IDC_CLIST, CLM_FINDCONTACT, (WPARAM)hContact, 0), - db_get_b(hContact, "FavContacts", "IsFavourite", 0)); - } - - bInitialized = true; - - PostMessage(hwnd, WM_APP, 0, 0); - - return TRUE; + SendDlgItemMessage(hwnd, IDC_CLIST, CLM_FINDCONTACT, (WPARAM)hContact, 0), + db_get_b(hContact, "FavContacts", "IsFavourite", 0)); } - case WM_APP: + bInitialized = true; + PostMessage(hwnd, WM_APP, 0, 0); + return TRUE; + + case WM_APP: { BOOL bGroups = IsDlgButtonChecked(hwnd, IDC_CHK_GROUPS); EnableWindow(GetDlgItem(hwnd, IDC_CHK_GROUPCOLUMS), bGroups); @@ -1151,20 +1143,18 @@ static INT_PTR CALLBACK OptionsDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARA EnableWindow(GetDlgItem(hwnd, IDC_CHK_RIGHTAVATARS), bAvatars); EnableWindow(GetDlgItem(hwnd, IDC_CHK_NOTRANSPARENTBORDER), bAvatars && bBorders); EnableWindow(GetDlgItem(hwnd, IDC_TXT_RADIUS), bAvatars && bBorders); - return TRUE; } + return TRUE; - case WM_DRAWITEM: + case WM_DRAWITEM: { LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT)lParam; - if (lpdis->CtlID == IDC_CANVAS) - { + if (lpdis->CtlID == IDC_CANVAS) { MEASUREITEMSTRUCT mis = {0}; DRAWITEMSTRUCT dis = *lpdis; FillRect(lpdis->hDC, &lpdis->rcItem, GetSysColorBrush(COLOR_BTNFACE)); - if (hSelectedContact) - { + if (hSelectedContact) { Options options; options.bSecondLine = IsDlgButtonChecked(hwnd, IDC_CHK_SECONDLINE); options.bAvatars = IsDlgButtonChecked(hwnd, IDC_CHK_AVATARS); @@ -1192,128 +1182,107 @@ static INT_PTR CALLBACK OptionsDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARA rc.bottom = rc.top + mis.itemHeight; FrameRect(lpdis->hDC, &rc, GetSysColorBrush(COLOR_HIGHLIGHT)); } - return TRUE; } - return FALSE; } + return FALSE; - case WM_COMMAND: - { - switch (LOWORD(wParam)) - { - case IDC_CHK_SECONDLINE: - case IDC_CHK_AVATARS: - case IDC_CHK_AVATARBORDER: - case IDC_CHK_NOTRANSPARENTBORDER: - case IDC_CHK_SYSCOLORS: - case IDC_CHK_CENTERHOTKEY: - case IDC_CHK_GROUPS: - case IDC_CHK_GROUPCOLUMS: - case IDC_CHK_RIGHTAVATARS: - SendMessage(GetParent(hwnd), PSM_CHANGED, 0, 0); - RedrawWindow(GetDlgItem(hwnd, IDC_CANVAS), NULL, NULL, RDW_INVALIDATE); - PostMessage(hwnd, WM_APP, 0, 0); - break; - - case IDC_BTN_FONTS: - { - OPENOPTIONSDIALOG ood = {0}; - ood.cbSize = sizeof(ood); - ood.pszGroup = "Customize"; - ood.pszPage = "Fonts & Colors"; - ood.pszTab = NULL; - Options_Open(&ood); - } - break; - - case IDC_TXT_RADIUS: - if ((HIWORD(wParam) == EN_CHANGE) && bInitialized) - { - RedrawWindow(GetDlgItem(hwnd, IDC_CANVAS), NULL, NULL, RDW_INVALIDATE); - SendMessage(GetParent(hwnd), PSM_CHANGED, 0, 0); - } - break; + case WM_COMMAND: + switch (LOWORD(wParam)) { + case IDC_CHK_SECONDLINE: + case IDC_CHK_AVATARS: + case IDC_CHK_AVATARBORDER: + case IDC_CHK_NOTRANSPARENTBORDER: + case IDC_CHK_SYSCOLORS: + case IDC_CHK_CENTERHOTKEY: + case IDC_CHK_GROUPS: + case IDC_CHK_GROUPCOLUMS: + case IDC_CHK_RIGHTAVATARS: + SendMessage(GetParent(hwnd), PSM_CHANGED, 0, 0); + RedrawWindow(GetDlgItem(hwnd, IDC_CANVAS), NULL, NULL, RDW_INVALIDATE); + PostMessage(hwnd, WM_APP, 0, 0); + break; - case IDC_TXT_MAXRECENT: - if ((HIWORD(wParam) == EN_CHANGE) && bInitialized) - SendMessage(GetParent(hwnd), PSM_CHANGED, 0, 0); - break; + case IDC_BTN_FONTS: + { + OPENOPTIONSDIALOG ood = { sizeof(ood) }; + ood.pszGroup = "Customize"; + ood.pszPage = "Fonts & Colors"; + ood.pszTab = NULL; + Options_Open(&ood); } break; - } - case WM_NOTIFY: - { - if ((((LPNMHDR)lParam)->idFrom == 0) && (((LPNMHDR)lParam)->code == PSN_APPLY)) - { - g_Options.bSecondLine = IsDlgButtonChecked(hwnd, IDC_CHK_SECONDLINE); - g_Options.bAvatars = IsDlgButtonChecked(hwnd, IDC_CHK_AVATARS); - g_Options.bAvatarBorder = IsDlgButtonChecked(hwnd, IDC_CHK_AVATARBORDER); - g_Options.bNoTransparentBorder = IsDlgButtonChecked(hwnd, IDC_CHK_NOTRANSPARENTBORDER); - g_Options.bSysColors = IsDlgButtonChecked(hwnd, IDC_CHK_SYSCOLORS); - g_Options.bCenterHotkey = IsDlgButtonChecked(hwnd, IDC_CHK_CENTERHOTKEY); - g_Options.bUseGroups = IsDlgButtonChecked(hwnd, IDC_CHK_GROUPS); - g_Options.bUseColumns = IsDlgButtonChecked(hwnd, IDC_CHK_GROUPCOLUMS); - g_Options.bRightAvatars = IsDlgButtonChecked(hwnd, IDC_CHK_RIGHTAVATARS); - g_Options.bDimIdle = IsDlgButtonChecked(hwnd, IDC_CHK_DIMIDLE); - g_Options.wAvatarRadius = GetDlgItemInt(hwnd, IDC_TXT_RADIUS, NULL, FALSE); - g_Options.wMaxRecent = GetDlgItemInt(hwnd, IDC_TXT_MAXRECENT, NULL, FALSE); - - sttSaveOptions(); - - HANDLE hContact = db_find_first(); - for ( ; hContact; hContact = db_find_next(hContact)) - { - BYTE fav = SendDlgItemMessage(hwnd, IDC_CLIST, CLM_GETCHECKMARK, - SendDlgItemMessage(hwnd, IDC_CLIST, CLM_FINDCONTACT, (WPARAM)hContact, 0), 0); - if (fav != db_get_b(hContact, "FavContacts", "IsFavourite", 0)) - db_set_b(hContact, "FavContacts", "IsFavourite", fav); - if (fav) CallService(MS_AV_GETAVATARBITMAP, (WPARAM)hContact, 0); - } - } else - if (((LPNMHDR)lParam)->idFrom == IDC_CLIST) - { - switch (((LPNMHDR)lParam)->code) - { - case CLN_OPTIONSCHANGED: - { - sttResetListOptions(GetDlgItem(hwnd,IDC_CLIST)); - break; - } + case IDC_TXT_RADIUS: + if ((HIWORD(wParam) == EN_CHANGE) && bInitialized) { + RedrawWindow(GetDlgItem(hwnd, IDC_CANVAS), NULL, NULL, RDW_INVALIDATE); + SendMessage(GetParent(hwnd), PSM_CHANGED, 0, 0); + } + break; - case CLN_NEWCONTACT: - { - int iSelection = (int)((NMCLISTCONTROL *)lParam)->hItem; - HANDLE hContact = db_find_first(); - for ( ; hContact; hContact = db_find_next(hContact)) - if (SendDlgItemMessage(hwnd, IDC_CLIST, CLM_FINDCONTACT, (WPARAM)hContact, 0) == iSelection) - { - SendDlgItemMessage(hwnd, IDC_CLIST, CLM_SETCHECKMARK, iSelection, - db_get_b(hContact, "FavContacts", "IsFavourite", 0)); - break; - } + case IDC_TXT_MAXRECENT: + if ((HIWORD(wParam) == EN_CHANGE) && bInitialized) + SendMessage(GetParent(hwnd), PSM_CHANGED, 0, 0); + break; + } + break; + + case WM_NOTIFY: + if ((((LPNMHDR)lParam)->idFrom == 0) && (((LPNMHDR)lParam)->code == PSN_APPLY)) { + g_Options.bSecondLine = IsDlgButtonChecked(hwnd, IDC_CHK_SECONDLINE); + g_Options.bAvatars = IsDlgButtonChecked(hwnd, IDC_CHK_AVATARS); + g_Options.bAvatarBorder = IsDlgButtonChecked(hwnd, IDC_CHK_AVATARBORDER); + g_Options.bNoTransparentBorder = IsDlgButtonChecked(hwnd, IDC_CHK_NOTRANSPARENTBORDER); + g_Options.bSysColors = IsDlgButtonChecked(hwnd, IDC_CHK_SYSCOLORS); + g_Options.bCenterHotkey = IsDlgButtonChecked(hwnd, IDC_CHK_CENTERHOTKEY); + g_Options.bUseGroups = IsDlgButtonChecked(hwnd, IDC_CHK_GROUPS); + g_Options.bUseColumns = IsDlgButtonChecked(hwnd, IDC_CHK_GROUPCOLUMS); + g_Options.bRightAvatars = IsDlgButtonChecked(hwnd, IDC_CHK_RIGHTAVATARS); + g_Options.bDimIdle = IsDlgButtonChecked(hwnd, IDC_CHK_DIMIDLE); + g_Options.wAvatarRadius = GetDlgItemInt(hwnd, IDC_TXT_RADIUS, NULL, FALSE); + g_Options.wMaxRecent = GetDlgItemInt(hwnd, IDC_TXT_MAXRECENT, NULL, FALSE); + + sttSaveOptions(); + + for (HANDLE hContact = db_find_first(); hContact; hContact = db_find_next(hContact)) { + BYTE fav = SendDlgItemMessage(hwnd, IDC_CLIST, CLM_GETCHECKMARK, + SendDlgItemMessage(hwnd, IDC_CLIST, CLM_FINDCONTACT, (WPARAM)hContact, 0), 0); + if (fav != db_get_b(hContact, "FavContacts", "IsFavourite", 0)) + db_set_b(hContact, "FavContacts", "IsFavourite", fav); + if (fav) CallService(MS_AV_GETAVATARBITMAP, (WPARAM)hContact, 0); + } + } + else if (((LPNMHDR)lParam)->idFrom == IDC_CLIST) { + int iSelection; + + switch (((LPNMHDR)lParam)->code) { + case CLN_OPTIONSCHANGED: + sttResetListOptions(GetDlgItem(hwnd,IDC_CLIST)); + break; + + case CLN_NEWCONTACT: + iSelection = (int)((NMCLISTCONTROL *)lParam)->hItem; + for (HANDLE hContact = db_find_first(); hContact; hContact = db_find_next(hContact)) { + if (SendDlgItemMessage(hwnd, IDC_CLIST, CLM_FINDCONTACT, (WPARAM)hContact, 0) == iSelection) { + SendDlgItemMessage(hwnd, IDC_CLIST, CLM_SETCHECKMARK, iSelection, + db_get_b(hContact, "FavContacts", "IsFavourite", 0)); break; } + } + break; - case CLN_CHECKCHANGED: - { - int iSelection = (int)((NMCLISTCONTROL *)lParam)->hItem; - HANDLE hContact = db_find_first(); - for ( ; hContact; hContact = db_find_next(hContact)) - if (SendDlgItemMessage(hwnd, IDC_CLIST, CLM_FINDCONTACT, (WPARAM)hContact, 0) == iSelection) - break; - if (hContact) - { - hSelectedContact = hContact; - RedrawWindow(GetDlgItem(hwnd, IDC_CANVAS), NULL, NULL, RDW_INVALIDATE); - } - SendMessage(GetParent(hwnd), PSM_CHANGED, 0, 0); + case CLN_CHECKCHANGED: + iSelection = (int)((NMCLISTCONTROL *)lParam)->hItem; + for (HANDLE hContact = db_find_first(); hContact; hContact = db_find_next(hContact)) { + if (SendDlgItemMessage(hwnd, IDC_CLIST, CLM_FINDCONTACT, (WPARAM)hContact, 0) == iSelection) { + hSelectedContact = hContact; + RedrawWindow(GetDlgItem(hwnd, IDC_CANVAS), NULL, NULL, RDW_INVALIDATE); } } + SendMessage(GetParent(hwnd), PSM_CHANGED, 0, 0); } - break; } + break; } return FALSE; -- cgit v1.2.3