// BDE - (C) Copyright 1994 by Borland International #include "address.h" //====================================================================== // Name: FileOpenHook() // // Input: hWnd, msg, wParam, lParam. // // Return: TRUE - The table was opened successfully. // FALSE - The table failed to open. // // Desc: This routine will process all I/O for the dialog that will // open the table for us. //====================================================================== BOOL FAR _pascal _export FileHook(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { DLGPROC lpProc; PASSSTRUCT *Passwd = NULL; static BOOL bUpper; static phDBIDb phDb; static UINT32 lNumAliases; static pDBDesc pAliases = NULL; static UINT32 lSel = 0; static BOOL bServer = FALSE; static pCS pCs; static BOOL bNew = FALSE; CHAR szAlias[MAXALIAS]; CHAR szFile[DBIMAXNAMELEN]={0}; DBIResult rslt; UINT16 iTableLen; CHAR szExt[5]={"\0"}; pCHAR pszMsg = NULL; pCHAR pTemp; if(iMSGFileOK == message) { if(!bServer && bNew) { // Get the actual file name so that we can check if it // exists. GetFileTitle(((OPENFILENAME*)lParam)->lpstrFile, szFile, DBIMAXNAMELEN); strlwr(szFile); if(!ChkExt(szFile)) { lpProc = (DLGPROC)MakeProcInstance((FARPROC) TypeDlg, hInst); if(DialogBoxParam(hInst, "TypeDlg", hDlg, lpProc, (LONG)(void far*)szExt)==IDOK) { strcat(szFile, szExt); pTemp = ((OPENFILENAME*)lParam)->lpstrFile; pTemp[((OPENFILENAME*)lParam)->nFileOffset] = '\0'; strcat(((OPENFILENAME*)lParam)->lpstrFile, szFile); FreeProcInstance((FARPROC) lpProc); return 0; } else { FreeProcInstance((FARPROC) lpProc); return 1; } } else { return 0; } } } switch (message) { case WM_INITDIALOG: // Get the Database pointer. pCs = (pCS)((OPENFILENAME*)lParam)->lCustData; phDb = pCs->phDb; if(pCs->uType == 1) { bNew = TRUE; } else { bNew = FALSE; } PostMessage(hDlg, WM_SETUP, 0, 0L); return TRUE; case WM_SETUP: // Hide the Addressbook type group if this is an open dialog. if(!bNew) { ShowWindow(GetDlgItem(hDlg, IDC_PERGROUP), SW_HIDE); ShowWindow(GetDlgItem(hDlg, IDC_BUSINESS), SW_HIDE); ShowWindow(GetDlgItem(hDlg, IDC_PERSONAL), SW_HIDE); } SetupDialog(hDlg, SW_SHOW); // Fill the Alias string with the aliases that are available to // the client. if(FillAliasStr(&pAliases, &lNumAliases)!=DBIERR_NONE) { SendMessage(hDlg, WM_DESTROY, 0, 0L); } // Fill the alias CB. FillAliasCBBox(pAliases, lNumAliases, cmb1, hDlg); // Choose the IDC_BUSINESS addressbook type as the defualt type. SendDlgItemMessage(hDlg, IDC_BUSINESS, BM_SETCHECK, 1, 0L); PostMessage(hDlg, WM_ALL, 0, 0L); bServer = FALSE; break; case WM_ALL: SetWindowText(GetDlgItem(hDlg, edt1), "*.dbf;*.db"); PostMessage(hDlg, WM_COMMAND, IDOK, 0L); break; case WM_DESTROY: if(pAliases) { free(pAliases); } if(Passwd) { free(Passwd); } if(pszMsg) { free(pszMsg); } break; case WM_COMMAND: switch(wParam) { case IDC_BUSINESS: pCs->uType = 1; break; case IDC_PERSONAL: pCs->uType = 2; break; case IDOK: if(bServer) { bIsServer = TRUE; memset((pCHAR)szTblName, '\0', DBIMAXTBLNAMELEN); GetWindowText(GetDlgItem(hDlg, edt1), szFile, 149); // Check if the table ends with a period. iTableLen = strlen((pCHAR)szFile); if(szFile[iTableLen - 1]=='.') { // If it does delete the period. szFile[iTableLen - 1] = '\0'; } strcpy((pCHAR)szTblName, szFile); // Check the file case type on the server and set // the table name accordingly. if(bUpper) { strupr((pCHAR)szTblName); } else { strlwr((pCHAR)szTblName); } // We set the name legal.txt into the edit control // so that the Common Dialog DLL will not say that // the table name is illegal. SetWindowText(GetDlgItem(hDlg, edt1), "legal.txt"); // Close the dialog with a TRUE return value. EndDialog(hDlg, TRUE); return FALSE; } else { bIsServer = FALSE; return FALSE; } case cmb1: switch(HIWORD(lParam)) { case CBN_SELCHANGE: lSel=SendDlgItemMessage(hDlg, wParam, CB_GETCURSEL, 0, 0L); SendDlgItemMessage(hDlg, wParam, CB_GETLBTEXT, (UINT16)lSel, (LPARAM)szAlias); PostMessage(hDlg, WM_NEXTDLGCTL, (WPARAM)GetDlgItem(hDlg, edt1), (LPARAM)(BOOL) LOWORD(TRUE)); // See if the alias is standard or if it is the // NULL alias. if((strnicmp(szAlias, "NULL", 4)==0) || IsStandard(szAlias, pAliases, lNumAliases)) { SetupDialog(hDlg, SW_SHOW); // If bServer than close the Database // that we opened up. if(bServer) { CloseDb(phDb); bServer = FALSE; OpenDB(phDb, NULL, NULL, NULL); } // Set the file extension to .db and .dbf // then send OK to display all local files. SetWindowText(GetDlgItem(hDlg, edt1), "*.db;*.dbf"); PostMessage(hDlg, WM_COMMAND, IDOK, 0L); } // Otherwise this is a server alias. Therefore, // we need to know if it is a server alias. else { // This is a server alias - so display the // password dialogbox. // Hide the right side of the dialog box. SetupDialog(hDlg, SW_HIDE); SetWindowText(GetDlgItem(hDlg, edt1), ""); // Clear the file list box SendMessage(GetDlgItem(hDlg, lst1), LB_RESETCONTENT, 0, 0L); if((Passwd = (PASSSTRUCT*) malloc (sizeof(PASSSTRUCT))) == NULL) { return FALSE; } // Copy in the alias and the physical // Database name. strcpy(Passwd->Alias, pAliases[(UINT16) lSel].szName); strcpy(Passwd->Database, pAliases[ (UINT16)lSel]. szPhyName); lpProc = (DLGPROC)MakeProcInstance ((FARPROC) PasswdDlg, hInst); // Since we have get the password and // pass in the alias and database names // we need to pass in the Passwd // structure. if(DialogBoxParam(hInst, "PasswdDlg", hDlg, lpProc, (LONG)(void far*) Passwd)==IDOK) { if(*phDb!=NULL) { CloseDb(phDb); } // Open the SQL Database. rslt=OpenDB(phDb, pAliases [(UINT16)lSel].szDbType, (pCHAR)Passwd, pAliases[(UINT16)lSel]. szName); // If there were no errors then // get the list of available tables. if(rslt == DBIERR_NONE) { bServer = TRUE; HourGlassCursor(TRUE); // Fill the file listbox bUpper = FillFileList(hDlg, lst1, *phDb); // Set the alias as the directory // name in the file dialog box. SetWindowText(GetDlgItem(hDlg, stc1), pAliases[(UINT16) lSel].szPhyName); PostMessage(hDlg, WM_NEXTDLGCTL, (WPARAM)GetDlgItem(hDlg, edt1), (LPARAM)(BOOL) LOWORD(TRUE)); HourGlassCursor(FALSE); } else { // Get the full information on // why the connection failed. DisplayError(rslt, &pszMsg); // Display the full error string. MessageBox(hDlg, pszMsg, "AddressBook Manager Error", MB_OK|MB_ICONINFORMATION); // Free the error string. free(pszMsg); } } FreeProcInstance((FARPROC) lpProc); free(Passwd); break; } return TRUE; } return TRUE; } break; } return FALSE; } //====================================================================== // Name: PasswdDlg() // // Input: hWnd, msg, wParam, lParam. In the lParam is a structure that // holds the Alias, Database name, and Password string to fill. // // Return: TRUE - Password was entered. // FALSE - Password was not entered.. // // Desc: This routine will process all I/O for the Password dialog. //====================================================================== BOOL FAR _pascal _export PasswdDlg (HWND hWnd, WORD msg, WORD wParam, LONG lParam) { BOOL bRet = FALSE; static PASSSTRUCT *PassWord = NULL; // Done to clear the unused warning. lParam = lParam; switch (msg) { case WM_INITDIALOG: PassWord = (PASSSTRUCT*)lParam; SetWindowText(GetDlgItem(hWnd, IDS_ALIAS), PassWord->Alias); SetWindowText(GetDlgItem(hWnd, IDS_DATABASE), PassWord->Database); PostMessage(hWnd,WM_NEXTDLGCTL, (WPARAM)GetDlgItem(hWnd,IDC_PASS),TRUE); break; case WM_COMMAND: switch (wParam) { case IDOK: GetDlgItemText(hWnd, IDC_PASS, PassWord->Password, MAXPASSLEN -1); bRet = TRUE; EndDialog(hWnd, bRet); break; case IDCANCEL: bRet = FALSE; EndDialog(hWnd, bRet); break; } break; } return(bRet); } //====================================================================== // Name: TypeDlg() // // Input: hWnd, msg, wParam, lParam. In the lParam is the extension // string. // // Return: TRUE - Table type was chosen. // FALSE - Table type was not chosen. // // Desc: This routine will process all I/O for the Table Type dialog. //====================================================================== BOOL FAR _pascal _export TypeDlg (HWND hWnd, WORD msg, WORD wParam, LONG lParam) { BOOL bRet = FALSE; static pCHAR pszExtension; static CHAR szString[301] = {'\0'}; // Done to clear the unused warning. lParam = lParam; switch (msg) { case WM_INITDIALOG: pszExtension = (pCHAR)lParam; LoadString(hInst, IDS_TABLETYPE, szString, 300); SetWindowText(GetDlgItem(hWnd, ID_TYPE_DESC), szString); // Choose the Paradox table type as the default. SendDlgItemMessage(hWnd, IDC_PARADOX, BM_SETCHECK, 1, 0L); break; case WM_COMMAND: switch (wParam) { case IDOK: if(SendDlgItemMessage(hWnd, IDC_PARADOX, BM_GETCHECK, 0, 0L) == 1) { strcpy(pszExtension,".db\0"); } else { strcpy(pszExtension, ".dbf\0"); } bRet = TRUE; EndDialog(hWnd, bRet); break; case IDCANCEL: bRet = FALSE; EndDialog(hWnd, bRet); break; } break; } return(bRet); } //========================================================================= // Function: InitializeStruct() // // Input: Pointer to the common dialog structure (lpStruct), and a // pointer to the CREATESTRUCT structure (pCS). // // Returns: None. // // Desc: This function is used to initialize the structure for the // open/save file common dialog. This routine is called just // before the common dialogs API is called. //========================================================================= void InitializeStruct(LPSTR lpStruct, pCS pCs) { LPFOCHUNK lpFOChunk; lpFOChunk = (LPFOCHUNK)lpStruct; // If this is an open table structure. if(pCs->uType == 0) { strcpy(lpFOChunk->szTitle, "Open Table"); } else { strcpy(lpFOChunk->szTitle, "New Table"); } *(lpFOChunk->szFile) = 0; *(lpFOChunk->szFileTitle) = 0; lpFOChunk->ofn.lStructSize = sizeof(OPENFILENAME); lpFOChunk->ofn.hwndOwner = (HWND)hMainWnd; lpFOChunk->ofn.hInstance = hInst; lpFOChunk->ofn.lpstrFilter = NULL; lpFOChunk->ofn.lpstrCustomFilter = (LPSTR)NULL; lpFOChunk->ofn.nMaxCustFilter = 0L; lpFOChunk->ofn.nFilterIndex = 1L; lpFOChunk->ofn.lpstrFile = lpFOChunk->szFile; lpFOChunk->ofn.nMaxFile = (DWORD)sizeof(lpFOChunk->szFile); lpFOChunk->ofn.lpstrFileTitle = lpFOChunk->szFileTitle; lpFOChunk->ofn.nMaxFileTitle = MAXFILETITLELEN; lpFOChunk->ofn.lpstrInitialDir = szTblDirectory; lpFOChunk->ofn.lpstrTitle = lpFOChunk->szTitle; lpFOChunk->ofn.Flags = OFN_HIDEREADONLY | OFN_ENABLETEMPLATE | OFN_ENABLEHOOK; lpFOChunk->ofn.nFileOffset = 0; lpFOChunk->ofn.nFileExtension = 0; lpFOChunk->ofn.lpstrDefExt = (LPSTR)NULL; lpFOChunk->ofn.lCustData = (LPARAM)pCs; lpFOChunk->ofn.lpfnHook = (FARHOOK)lpfnFileHook; lpFOChunk->ofn.lpTemplateName = (LPSTR)MAKEINTRESOURCE (FILEOPENORD); return; } //========================================================================= // Function: ProcessCDError() // // Input: CommDlgError(UINT16), Pointer to the common dialog structure // (lpStruct). // // Returns: None. // // Desc: This function reports an error that has occurred during the // last call to a Common Dialog routine. //========================================================================= void ProcessCDError(UINT32 dwErrorCode) { UINT16 iStringID; CHAR szBuffer[200]; switch(dwErrorCode) { case CDERR_DIALOGFAILURE: iStringID=IDS_DIALOGFAILURE; break; case CDERR_INITIALIZATION: iStringID=IDS_INITIALIZATION; break; case CDERR_NOTEMPLATE: iStringID=IDS_NOTEMPLATE; break; case CDERR_NOHINSTANCE: iStringID=IDS_NOHINSTANCE; break; case CDERR_LOCKRESFAILURE: iStringID=IDS_LOCKRESFAILURE; break; case CDERR_MEMALLOCFAILURE: iStringID=IDS_MEMALLOCFAILURE; break; case CDERR_MEMLOCKFAILURE: iStringID=IDS_MEMLOCKFAILURE; break; case 0: // User may have hit CANCEL or we got a *very* random // error default: return; } if (!LoadString(hInst, iStringID, szBuffer, sizeof(szBuffer))) { return; } MessageBox(hMainWnd, szBuffer, "AddressBook Error", MB_OK); return; } //========================================================================= // Function: ChkExt() // // Input: Pointer to a string that contains the filename to check // (pCHAR). // // Returns: TRUE: If the file has an extension of .db or .dbf // FALSE: If the file has an extension of anything else. // // Desc: This function simply checks if the filename has a .db in it. // It then checks if the next character is a 'f' (.dbf) or // a '/0' (.db). It then returns the result of its check. //========================================================================= BOOL ChkExt(pCHAR pszFileName) { pCHAR pName; BOOL bRet = FALSE; UINT16 i=0; strlwr(pszFileName); if((pName = strstr(pszFileName, ".db"))!=NULL) { if((pName[3]=='f') || (pName[3]=='\0')) { bRet = TRUE; } } if(!bRet) { i=0; while (i