/* 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 #include #include #include // Miranda Includes #include #include #include #include #include #include #include // Fortune include #include "fortune.h" #include "resource.h" #include "m_fortunemsg.h" #ifndef BIF_NEWDIALOGSTYLE #define BIF_NEWDIALOGSTYLE 0x0040 #endif // Prototypes int InitOptions(WPARAM, LPARAM); INT_PTR CALLBACK DlgOptionsProc(HWND, UINT, WPARAM, LPARAM); int CALLBACK prcHookBrowseForFolder(HWND, UINT, LPARAM, LPARAM); extern char *StatusModeToDbSetting(int, const char *); // Program globals extern HINSTANCE hInst; struct OptDlgData { BOOL oneLine; BOOL removeCR; char fortuneDir[MAX_PATH+1]; char statusFile[9][MAX_PATH+1]; int protoCount; char **protoName; char **protoFile; int *protoLength; }; int InitOptions(WPARAM wParam, LPARAM lParam) { OPTIONSDIALOGPAGE odp; ZeroMemory(&odp, sizeof(odp)); odp.cbSize = sizeof(odp); odp.hInstance = hInst; odp.pszTemplate = MAKEINTRESOURCE(IDD_OPTIONDLG); odp.pszTitle = Translate("Fortune Messages"); odp.pszGroup = Translate("Status"); odp.flags = ODPF_BOLDGROUPS; odp.pfnDlgProc = DlgOptionsProc; CallService(MS_OPT_ADDPAGE, wParam, (LPARAM)&odp); return 0; } INT_PTR CALLBACK DlgOptionsProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) { struct OptDlgData *data; static BOOL initDlg=FALSE; data = (struct OptDlgData *)GetWindowLong(hwndDlg, GWL_USERDATA); switch (uMsg) { case WM_INITDIALOG: { PROTOCOLDESCRIPTOR **proto; DBVARIANT dbv; DWORD protoStatusFlags, auxStatusFlags; int i, protoCount, index; initDlg=TRUE; TranslateDialogDefault(hwndDlg); data = (struct OptDlgData *)malloc(sizeof(struct OptDlgData)); SetWindowLong(hwndDlg, GWL_USERDATA, (LONG)data); SendDlgItemMessage(hwndDlg, IDC_FORTUNEDIR, EM_LIMITTEXT, MAX_PATH, 0); SendDlgItemMessage(hwndDlg, IDC_PROTOFILE, EM_LIMITTEXT, MAX_PATH, 0); SendDlgItemMessage(hwndDlg, IDC_STATUSFILE, EM_LIMITTEXT, MAX_PATH, 0); SendDlgItemMessage(hwndDlg, IDC_EMAXLEN, EM_LIMITTEXT, 4, 0); SendMessage(GetDlgItem(hwndDlg, IDC_SMAXLEN), UDM_SETBUDDY, (WPARAM)GetDlgItem(hwndDlg, IDC_EMAXLEN), 0); SendMessage(GetDlgItem(hwndDlg, IDC_SMAXLEN), UDM_SETRANGE32, (WPARAM)MIN_FORTUNEMSG, (LPARAM)MAX_FORTUNEMSG); if(!DBGetContactSetting(NULL, MODULE_NAME, "FortuneDir", &dbv)) { makeAbsolute(data->fortuneDir, dbv.pszVal); DBFreeVariant(&dbv); } else data->fortuneDir[0] = '\0'; EnableWindow(GetDlgItem(hwndDlg, IDC_FORTUNEDIR), TRUE); SetDlgItemText(hwndDlg, IDC_FORTUNEDIR, data->fortuneDir); CallService(MS_PROTO_ENUMPROTOCOLS, (WPARAM)&protoCount,(LPARAM)&proto); data->protoName = (char **)malloc(protoCount * sizeof(char *)); data->protoFile = (char **)malloc(protoCount * sizeof(char *)); data->protoLength = (int *)malloc(protoCount * sizeof(int)); if (!data->protoName || !data->protoFile || !data->protoLength) { data->protoCount = 0; EnableWindow(GetDlgItem(hwndDlg, IDC_PROTOFILE), FALSE); EnableWindow(GetDlgItem(hwndDlg, IDC_CBOPTPROTO), FALSE); EnableWindow(GetDlgItem(hwndDlg, IDC_PROTOBROWSE), FALSE); EnableWindow(GetDlgItem(hwndDlg, IDC_EMAXLEN), FALSE); EnableWindow(GetDlgItem(hwndDlg, IDC_SMAXLEN), FALSE); } else { char setting[MAX_PATH+1]; data->protoCount = protoCount; for(i=0; i < protoCount; i++) { char protoLabel[MAXMODULELABELLENGTH+1]; data->protoName[i] = NULL; data->protoFile[i] = NULL; if (proto[i]->type != PROTOTYPE_PROTOCOL) continue; if (!(auxStatusFlags = CallProtoService(proto[i]->szName, PS_GETCAPS,PFLAGNUM_3, 0))) continue; protoStatusFlags |= auxStatusFlags; if (!(data->protoName[i] = (char *)malloc(strlen(proto[i]->szName)+1))) continue; if (!(data->protoFile[i] = (char *)malloc(MAX_PATH+1))) continue; CallProtoService(proto[i]->szName, PS_GETNAME, MAXMODULELABELLENGTH, (LPARAM)protoLabel); index = SendMessage(GetDlgItem(hwndDlg, IDC_CBOPTPROTO), CB_ADDSTRING, 0, (LPARAM)protoLabel); SendMessage(GetDlgItem(hwndDlg, IDC_CBOPTPROTO), CB_SETITEMDATA, index, (LPARAM)i); if (index != CB_ERR && index != CB_ERRSPACE) { strcpy(data->protoName[i], proto[i]->szName); _snprintf(setting, sizeof(setting), "%sFile", proto[i]->szName); if(!DBGetContactSetting(NULL, MODULE_NAME, setting, &dbv)) { makeAbsolute(data->protoFile[i], dbv.pszVal); DBFreeVariant(&dbv); } else data->protoFile[i][0] = '\0'; _snprintf(setting, sizeof(setting), "%sLength", proto[i]->szName); data->protoLength[i] = DBGetContactSettingWord(NULL, MODULE_NAME, setting, MAX_FORTUNEMSG); SendMessage(GetDlgItem(hwndDlg, IDC_CBOPTPROTO), CB_SETITEMDATA, (WPARAM)index, (LPARAM)i); } } SendMessage(GetDlgItem(hwndDlg, IDC_CBOPTPROTO), CB_SETCURSEL, (WPARAM)0, 0); SendMessage(hwndDlg, WM_COMMAND, MAKEWPARAM(IDC_CBOPTPROTO, CBN_SELCHANGE), (LPARAM)GetDlgItem(hwndDlg, IDC_CBOPTPROTO)); } for (i=ID_STATUS_ONLINE; i <= ID_STATUS_OUTTOLUNCH; i++) if (protoStatusFlags & Proto_Status2Flag(i)) { index = SendMessage(GetDlgItem(hwndDlg, IDC_CBOPTSTATUS), CB_INSERTSTRING, (WPARAM)-1, (LPARAM)CallService(MS_CLIST_GETSTATUSMODEDESCRIPTION, i, 0)); if (index != CB_ERR && index != CB_ERRSPACE) { SendMessage(GetDlgItem(hwndDlg, IDC_CBOPTSTATUS), CB_SETITEMDATA, (WPARAM)index, (LPARAM)i-ID_STATUS_ONLINE); if(!DBGetContactSetting(NULL, MODULE_NAME, StatusModeToDbSetting(i, "File"), &dbv)) { makeAbsolute(data->statusFile[i-ID_STATUS_ONLINE], dbv.pszVal); DBFreeVariant(&dbv); } else data->statusFile[i-ID_STATUS_ONLINE][0] = '\0'; } } SendMessage(GetDlgItem(hwndDlg, IDC_CBOPTSTATUS), CB_SETCURSEL, (WPARAM)0, 0); SendMessage(hwndDlg, WM_COMMAND, MAKEWPARAM(IDC_CBOPTSTATUS, CBN_SELCHANGE),(LPARAM)GetDlgItem(hwndDlg, IDC_CBOPTSTATUS)); if (data->oneLine = DBGetContactSettingByte(NULL, MODULE_NAME, "OneLine", 0)) { CheckDlgButton(hwndDlg, IDC_CCHECKONELINE, BST_CHECKED); data->removeCR = TRUE; CheckDlgButton(hwndDlg, IDC_CCHECKREMOVECR, BST_CHECKED); EnableWindow(GetDlgItem(hwndDlg, IDC_CCHECKREMOVECR), FALSE); } else { CheckDlgButton(hwndDlg, IDC_CCHECKONELINE, BST_UNCHECKED); if (data->removeCR = DBGetContactSettingByte(NULL, MODULE_NAME, "RemoveCR", 0)) CheckDlgButton(hwndDlg, IDC_CCHECKREMOVECR, BST_CHECKED); else CheckDlgButton(hwndDlg, IDC_CCHECKREMOVECR, BST_UNCHECKED); } initDlg=FALSE; return TRUE; } case WM_COMMAND: { BOOL sthChanged = FALSE; switch (LOWORD(wParam)) { case IDC_FORTUNEDIR: if(HIWORD(wParam) == EN_CHANGE) { char path[MAX_PATH+1]; GetDlgItemText(hwndDlg, IDC_FORTUNEDIR, path, sizeof(path)); strcpy(data->fortuneDir, path); sthChanged = TRUE; } break; case IDC_DIRBROWSE: { char path[MAX_PATH+1]; LPMALLOC psMalloc; BROWSEINFOA bi; LPITEMIDLIST pidl; #ifdef __WINE__ path[0] = '\0'; #else GetDlgItemTextA(hwndDlg, IDC_FORTUNEDIR, path, sizeof(path)); #endif if(SUCCEEDED(CoGetMalloc(1, &psMalloc))) { ZeroMemory(&bi, sizeof(bi)); bi.hwndOwner = hwndDlg; bi.pszDisplayName = path; bi.lpszTitle = Translate("Select Fortune Folder"); bi.ulFlags = BIF_NEWDIALOGSTYLE | BIF_EDITBOX | BIF_RETURNONLYFSDIRS; bi.lpfn = prcHookBrowseForFolder; bi.lParam = (LPARAM)path; if (pidl = SHBrowseForFolderA(&bi)) { if (SHGetPathFromIDListA(pidl, path)) SetDlgItemTextA(hwndDlg, IDC_FORTUNEDIR, path); psMalloc->lpVtbl->Free(psMalloc, pidl); } psMalloc->lpVtbl->Release(psMalloc); } break; } case IDC_CBOPTPROTO: if (HIWORD(wParam) == CBN_SELCHANGE || HIWORD(wParam) == CBN_SELENDOK) { int i = SendMessage((HWND)lParam, CB_GETITEMDATA, (WPARAM)SendMessage((HWND)lParam, CB_GETCURSEL, 0, 0), 0); EnableWindow(GetDlgItem(hwndDlg, IDC_PROTOFILE), TRUE); SetDlgItemText(hwndDlg, IDC_PROTOFILE, data->protoFile[i]); SetDlgItemInt(hwndDlg, IDC_EMAXLEN, data->protoLength[i], FALSE); } break; case IDC_PROTOFILE: if(HIWORD(wParam) == EN_CHANGE) { char path[MAX_PATH+1]; int i = SendMessage(GetDlgItem(hwndDlg, IDC_CBOPTPROTO), CB_GETITEMDATA, (WPARAM)SendMessage(GetDlgItem(hwndDlg, IDC_CBOPTPROTO), CB_GETCURSEL, 0, 0), 0); GetDlgItemText(hwndDlg, IDC_PROTOFILE, path, sizeof(path)); strcpy(data->protoFile[i], path); sthChanged = TRUE; } break; case IDC_PROTOBROWSE: { char path[MAX_PATH+1]; OPENFILENAME ofn={0}; GetDlgItemText(hwndDlg, IDC_PROTOFILE, path, sizeof(path)); ofn.lStructSize = sizeof(OPENFILENAME); ofn.hwndOwner = hwndDlg; ofn.hInstance = NULL; ofn.lpstrFilter = "Fortune header file (*.dat)\0*.dat\0\0"; ofn.lpstrFile = path; ofn.Flags = OFN_FILEMUSTEXIST|OFN_HIDEREADONLY; ofn.nMaxFile = sizeof(path); ofn.nMaxFileTitle = MAX_PATH; ofn.lpstrDefExt = "dat"; if(GetOpenFileName(&ofn)) SetDlgItemText(hwndDlg, IDC_PROTOFILE, path); break; } case IDC_EMAXLEN: if(HIWORD(wParam) == EN_CHANGE) { BOOL translated; int val = GetDlgItemInt(hwndDlg, IDC_EMAXLEN, &translated, FALSE); if (translated) { int i = SendMessage(GetDlgItem(hwndDlg, IDC_CBOPTPROTO), CB_GETITEMDATA, (WPARAM)SendMessage(GetDlgItem(hwndDlg, IDC_CBOPTPROTO), CB_GETCURSEL, 0, 0), 0); data->protoLength[i] = val; if (data->protoLength[i] < MIN_FORTUNEMSG) data->protoLength[i] = MIN_FORTUNEMSG; if (data->protoLength[i] > MAX_FORTUNEMSG) data->protoLength[i] = MAX_FORTUNEMSG; sthChanged = TRUE; } } break; case IDC_CBOPTSTATUS: if (HIWORD(wParam) == CBN_SELCHANGE || HIWORD(wParam) == CBN_SELENDOK) { int i = SendMessage((HWND)lParam, CB_GETITEMDATA, (WPARAM)SendMessage((HWND)lParam, CB_GETCURSEL, 0, 0), 0); EnableWindow(GetDlgItem(hwndDlg, IDC_STATUSFILE), TRUE); SetDlgItemText(hwndDlg, IDC_STATUSFILE, data->statusFile[i]); } break; case IDC_STATUSFILE: if(HIWORD(wParam) == EN_CHANGE) { char path[MAX_PATH+1]; int i = SendMessage(GetDlgItem(hwndDlg, IDC_CBOPTSTATUS), CB_GETITEMDATA, (WPARAM)SendMessage(GetDlgItem(hwndDlg, IDC_CBOPTSTATUS), CB_GETCURSEL, 0, 0), 0); GetDlgItemText(hwndDlg, IDC_STATUSFILE, path, sizeof(path)); strcpy(data->statusFile[i], path); sthChanged = TRUE; } break; case IDC_STATUSBROWSE: { char path[MAX_PATH+1]; OPENFILENAME ofn={0}; GetDlgItemText(hwndDlg, IDC_STATUSFILE, path, sizeof(path)); ofn.lStructSize = sizeof(OPENFILENAME); ofn.hwndOwner = hwndDlg; ofn.hInstance = NULL; ofn.lpstrFilter = "Fortune header file (*.dat)\0*.dat\0\0"; ofn.lpstrFile = path; ofn.Flags = OFN_FILEMUSTEXIST|OFN_HIDEREADONLY; ofn.nMaxFile = sizeof(path); ofn.nMaxFileTitle = MAX_PATH; ofn.lpstrDefExt = "dat"; if(GetOpenFileName(&ofn)) SetDlgItemText(hwndDlg, IDC_STATUSFILE, path); break; } case IDC_CCHECKONELINE: if (HIWORD(wParam) == BN_CLICKED) { if (IsDlgButtonChecked(hwndDlg, IDC_CCHECKONELINE) == BST_CHECKED) { data->oneLine = TRUE; data->removeCR = TRUE; CheckDlgButton(hwndDlg, IDC_CCHECKREMOVECR, BST_CHECKED); EnableWindow(GetDlgItem(hwndDlg, IDC_CCHECKREMOVECR), FALSE); } else { data->oneLine = FALSE; EnableWindow(GetDlgItem(hwndDlg, IDC_CCHECKREMOVECR), TRUE); } sthChanged = TRUE; } break; case IDC_CCHECKREMOVECR: if (HIWORD(wParam) == BN_CLICKED) { if (IsDlgButtonChecked(hwndDlg, IDC_CCHECKREMOVECR) == BST_CHECKED) data->removeCR = TRUE; else data->removeCR = FALSE; sthChanged = TRUE; } break; } if (!initDlg && sthChanged) SendMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0); break; } case WM_NOTIFY: switch(((LPNMHDR)lParam)->idFrom) { case 0: switch (((LPNMHDR)lParam)->code) { case PSN_APPLY: { int i; char auxPath[MAX_PATH+1]; DBWriteContactSettingString(NULL, MODULE_NAME, "FortuneDir", makeRelative(auxPath, data->fortuneDir)); for (i=0; i < data->protoCount; i++) { char setting[MAX_PATH+1]; if (data->protoName[i] && data->protoFile[i]) { _snprintf(setting, sizeof(setting), "%sFile", data->protoName[i]); DBWriteContactSettingString(NULL, MODULE_NAME, setting, makeRelative(auxPath, data->protoFile[i])); _snprintf(setting, sizeof(setting), "%sLength", data->protoName[i]); DBWriteContactSettingWord(NULL, MODULE_NAME, setting, (WORD)data->protoLength[i]); } } for (i=ID_STATUS_ONLINE; i <= ID_STATUS_OUTTOLUNCH; i++) DBWriteContactSettingString(NULL, MODULE_NAME, StatusModeToDbSetting(i, "File"), makeRelative(auxPath, data->statusFile[i-ID_STATUS_ONLINE])); DBWriteContactSettingByte(NULL, MODULE_NAME, "OneLine", (BYTE)data->oneLine); DBWriteContactSettingByte(NULL, MODULE_NAME, "RemoveCR", (BYTE)data->removeCR); return TRUE; } } break; } break; case WM_DESTROY: { int i; for (i=0; i < data->protoCount; i++) { if (data->protoName[i]) free(data->protoName[i]); if (data->protoFile[i]) free(data->protoFile[i]); } if (data->protoName) free(data->protoName); if (data->protoFile) free(data->protoFile); if (data->protoLength) free(data->protoLength); free(data); break; } } return FALSE; } // Hook for SHBrowseForFolder() // Sets the initial directory (a LPCSTR in lpData) int CALLBACK prcHookBrowseForFolder(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData) { char szDir[MAX_PATH+1]; switch(uMsg) { case BFFM_INITIALIZED: SendMessageA(hwnd, BFFM_SETSELECTION, TRUE, lpData); break; case BFFM_SELCHANGED: if (SHGetPathFromIDListA((LPITEMIDLIST)lParam, szDir)) SendMessageA(hwnd, BFFM_SETSTATUSTEXT, 0, (LPARAM)szDir); break; } return 0; }