From ea922a1ddd0526d37c6a299ab6260729612a8dfa Mon Sep 17 00:00:00 2001 From: George Hazan Date: Thu, 28 Feb 2013 17:02:58 +0000 Subject: new template, VARST, to hold the result of calling Utils_ReplaceVarsT() git-svn-id: http://svn.miranda-ng.org/main/trunk@3806 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- src/modules/clist/clui.cpp | 4 +- src/modules/database/database.cpp | 84 +++++++-------------------------- src/modules/database/dbini.cpp | 20 ++++---- src/modules/database/profilemanager.cpp | 60 ++++++++++++++++++----- src/modules/database/profilemanager.h | 2 +- src/modules/netlib/netliblog.cpp | 15 ++---- src/modules/utils/path.cpp | 7 ++- src/modules/utils/utils.cpp | 10 ++-- 8 files changed, 87 insertions(+), 115 deletions(-) (limited to 'src/modules') diff --git a/src/modules/clist/clui.cpp b/src/modules/clist/clui.cpp index ab4b7db76a..26e9894606 100644 --- a/src/modules/clist/clui.cpp +++ b/src/modules/clist/clui.cpp @@ -460,9 +460,7 @@ LRESULT CALLBACK fnContactListWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM int rc; // wParam = (ATOM)hProfileAtom, lParam = 0 if (GlobalGetAtomName((ATOM) wParam, profile, SIZEOF(profile))) { - TCHAR *pfd = Utils_ReplaceVarsT(_T("%miranda_userdata%\\%miranda_profilename%.dat")); - rc = lstrcmpi(profile, pfd) == 0; - mir_free(pfd); + rc = lstrcmpi(profile, VARST(_T("%miranda_userdata%\\%miranda_profilename%.dat"))) == 0; ReplyMessage(rc); if (rc) { ShowWindow(hwnd, SW_RESTORE); diff --git a/src/modules/database/database.cpp b/src/modules/database/database.cpp index 168a6d5f27..adea9e0caa 100644 --- a/src/modules/database/database.cpp +++ b/src/modules/database/database.cpp @@ -26,7 +26,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. // contains the location of mirandaboot.ini extern TCHAR mirandabootini[MAX_PATH]; -bool dbCreated; +bool g_bDbCreated; TCHAR g_profileDir[MAX_PATH], g_profileName[MAX_PATH]; TCHAR* g_defaultProfile; void EnsureCheckerLoaded(bool); @@ -55,15 +55,11 @@ static void fillProfileName(const TCHAR* ptszFileName) bool IsInsideRootDir(TCHAR* profiledir, bool exact) { - int res; - TCHAR* pfd = Utils_ReplaceVarsT(_T("%miranda_path%")); + VARST pfd( _T("%miranda_path%")); if (exact) - res = _tcsicmp(profiledir, pfd); - else - res = _tcsnicmp(profiledir, pfd, _tcslen(pfd)); - - mir_free(pfd); - return res == 0; + return _tcsicmp(profiledir, pfd) == 0; + + return _tcsnicmp(profiledir, pfd, _tcslen(pfd)) == 0; } // returns 1 if the profile path was returned, without trailing slash @@ -75,9 +71,7 @@ int getProfilePath(TCHAR *buf, size_t cch) if (profiledir[0] == 0) _tcscpy(profiledir, _T("%miranda_path%\\Profiles")); - TCHAR* exprofiledir = Utils_ReplaceVarsT(profiledir); - size_t len = PathToAbsoluteT(exprofiledir, buf, NULL); - mir_free(exprofiledir); + size_t len = PathToAbsoluteT( VARST(profiledir), buf, NULL); if (buf[len-1] == '/' || buf[len-1] == '\\') buf[len-1] = 0; @@ -123,12 +117,11 @@ static void getDefaultProfile(TCHAR *szProfile, size_t cch, TCHAR *profiledir) if (defaultProfile[0] == 0) return; - TCHAR* res = Utils_ReplaceVarsT(defaultProfile); - if (res) { - mir_sntprintf(szProfile, cch, _T("%s\\%s\\%s%s"), profiledir, res, res, isValidProfileName(res) ? _T("") : _T(".dat")); - mir_free(res); - } - else szProfile[0] = 0; + VARST res(defaultProfile); + if (res) + mir_sntprintf(szProfile, cch, _T("%s\\%s\\%s%s"), profiledir, (TCHAR*)res, (TCHAR*)res, isValidProfileName(res) ? _T("") : _T(".dat")); + else + szProfile[0] = 0; } // returns 1 if something that looks like a profile is there @@ -183,12 +176,10 @@ void getProfileDefault(TCHAR *szProfile, size_t cch, TCHAR *profiledir) static void moveProfileDirProfiles(TCHAR *profiledir, BOOL isRootDir = TRUE) { TCHAR pfd[MAX_PATH]; - if (isRootDir) { - TCHAR *path = Utils_ReplaceVarsT(_T("%miranda_path%\\*.dat")); - mir_sntprintf(pfd, SIZEOF(pfd), _T("%s"), path); - mir_free(path); - } - else mir_sntprintf(pfd, SIZEOF(pfd), _T("%s\\*.dat"), profiledir); + if (isRootDir) + _tcsncpy(pfd, VARST( _T("%miranda_path%\\*.dat")), SIZEOF(pfd)); + else + mir_sntprintf(pfd, SIZEOF(pfd), _T("%s\\*.dat"), profiledir); WIN32_FIND_DATA ffd; HANDLE hFind = FindFirstFile(pfd, &ffd); @@ -288,7 +279,6 @@ static int getProfileAutoRun(TCHAR *szProfile) // returns 1 if a profile was selected static int getProfile(TCHAR *szProfile, size_t cch) { - PROFILEMANAGERDATA pd = {0}; getProfilePath(g_profileDir, SIZEOF(g_profileDir)); if (IsInsideRootDir(g_profileDir, true)) if (WritePrivateProfileString(_T("Database"), _T("ProfileDir"), _T(""), mirandabootini)) @@ -304,6 +294,7 @@ static int getProfile(TCHAR *szProfile, size_t cch) return 0; } + PROFILEMANAGERDATA pd = {0}; if ( CmdLine_GetOption( _T("ForceShowPM"))) { LBL_Show: pd.szProfile = szProfile; @@ -341,47 +332,6 @@ char* makeFileName(const TCHAR* tszOriginalName) return szResult; } -// called by the UI, return 1 on success, use link to create profile, set error if any -int makeDatabase(TCHAR *profile, DATABASELINK * link, HWND hwndDlg) -{ - TCHAR buf[256]; - int err = 0; - // check if the file already exists - TCHAR *file = _tcsrchr(profile, '\\'); - if (file) file++; - if (_taccess(profile, 0) == 0) { - // file already exists! - mir_sntprintf(buf, SIZEOF(buf), - TranslateT("The profile '%s' already exists. Do you want to move it to the Recycle Bin?\n\nWARNING: The profile will be deleted if Recycle Bin is disabled.\nWARNING: A profile may contain confidential information and should be properly deleted."), - file); - if (MessageBox(hwndDlg, buf, TranslateT("The profile already exists"), MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2) != IDYES) - return 0; - - // move the file - SHFILEOPSTRUCT sf = {0}; - sf.wFunc = FO_DELETE; - sf.pFrom = buf; - sf.fFlags = FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_SILENT | FOF_ALLOWUNDO; - mir_sntprintf(buf, SIZEOF(buf), _T("%s\0"), profile); - if (SHFileOperation(&sf) != 0) { - mir_sntprintf(buf, SIZEOF(buf), TranslateT("Couldn't move '%s' to the Recycle Bin, Please select another profile name."), file); - MessageBox(0, buf, TranslateT("Problem moving profile"), MB_ICONINFORMATION|MB_OK); - return 0; - } - // now the file should be gone! - } - // ask the database to create the profile - CreatePathToFileT(profile); - if ((err = link->makeDatabase(profile)) != ERROR_SUCCESS) { - mir_sntprintf(buf, SIZEOF(buf), TranslateT("Unable to create the profile '%s', the error was %x"), file, err); - MessageBox(hwndDlg, buf, TranslateT("Problem creating profile"), MB_ICONERROR|MB_OK); - return 0; - } - dbCreated = true; - // the profile has been created! woot - return 1; -} - // enumerate all plugins that had valid DatabasePluginInfo() int tryOpenDatabase(const TCHAR *tszProfile) { @@ -428,7 +378,7 @@ static int tryCreateDatabase(const TCHAR* ptszProfile) int err = p->makeDatabase(tszProfile); if (err == ERROR_SUCCESS) { - dbCreated = true; + g_bDbCreated = true; MIDatabase *pDb = p->Load(tszProfile); if (pDb != NULL) { fillProfileName(tszProfile); diff --git a/src/modules/database/dbini.cpp b/src/modules/database/dbini.cpp index 8a15b3eb08..0445da160e 100644 --- a/src/modules/database/dbini.cpp +++ b/src/modules/database/dbini.cpp @@ -22,12 +22,12 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include "..\..\core\commonheaders.h" +#include "profilemanager.h" static bool bModuleInitialized = false; static HANDLE hIniChangeNotification; extern TCHAR mirandabootini[MAX_PATH]; -extern bool dbCreated; static INT_PTR CALLBACK InstallIniDlgProc(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam) { @@ -395,7 +395,6 @@ static void ProcessIniFile(TCHAR* szIniPath, char *szSafeSections, char *szUnsaf static void DoAutoExec(void) { TCHAR szUse[7], szIniPath[MAX_PATH], szFindPath[MAX_PATH]; - TCHAR *str2; TCHAR buf[2048], szSecurity[11], szOverrideSecurityFilename[MAX_PATH], szOnCreateFilename[MAX_PATH]; char *szSafeSections, *szUnsafeSections; int secur; @@ -415,17 +414,12 @@ static void DoAutoExec(void) GetPrivateProfileString(_T("AutoExec"), _T("OnCreateFilename"), _T(""), szOnCreateFilename, SIZEOF(szOnCreateFilename), mirandabootini); GetPrivateProfileString(_T("AutoExec"), _T("Glob"), _T("autoexec_*.ini"), szFindPath, SIZEOF(szFindPath), mirandabootini); - if (dbCreated && szOnCreateFilename[0]) { - str2 = Utils_ReplaceVarsT(szOnCreateFilename); - PathToAbsoluteT(str2, szIniPath, NULL); - mir_free(str2); - + if (g_bDbCreated && szOnCreateFilename[0]) { + PathToAbsoluteT( VARST(szOnCreateFilename), szIniPath, NULL); ProcessIniFile(szIniPath, szSafeSections, szUnsafeSections, 0, 1); } - str2 = Utils_ReplaceVarsT(szFindPath); - PathToAbsoluteT(str2, szFindPath, NULL); - mir_free(str2); + PathToAbsoluteT( VARST(szFindPath), szFindPath, NULL); WIN32_FIND_DATA fd; HANDLE hFind = FindFirstFile(szFindPath, &fd); @@ -435,7 +429,7 @@ static void DoAutoExec(void) return; } - str2 = _tcsrchr(szFindPath, '\\'); + TCHAR *str2 = _tcsrchr(szFindPath, '\\'); if (str2 == NULL) szFindPath[0] = 0; else str2[1] = 0; @@ -478,7 +472,9 @@ static void DoAutoExec(void) else if ( !lstrcmpi(szOnCompletion, _T("ask"))) DialogBoxParam(hInst, MAKEINTRESOURCE(IDD_INIIMPORTDONE), NULL, IniImportDoneDlgProc, (LPARAM)szIniPath); } - } while (FindNextFile(hFind, &fd)); + } + while (FindNextFile(hFind, &fd)); + FindClose(hFind); mir_free(szSafeSections); mir_free(szUnsafeSections); diff --git a/src/modules/database/profilemanager.cpp b/src/modules/database/profilemanager.cpp index 19f219a463..d597d50a3f 100644 --- a/src/modules/database/profilemanager.cpp +++ b/src/modules/database/profilemanager.cpp @@ -107,6 +107,47 @@ static int findProfiles(TCHAR *szProfileDir, ENUMPROFILECALLBACK callback, LPARA return 1; } +static int CreateProfile(TCHAR *profile, DATABASELINK * link, HWND hwndDlg) +{ + TCHAR buf[256]; + int err = 0; + // check if the file already exists + TCHAR *file = _tcsrchr(profile, '\\'); + if (file) file++; + if (_taccess(profile, 0) == 0) { + // file already exists! + mir_sntprintf(buf, SIZEOF(buf), + TranslateT("The profile '%s' already exists. Do you want to move it to the Recycle Bin?\n\nWARNING: The profile will be deleted if Recycle Bin is disabled.\nWARNING: A profile may contain confidential information and should be properly deleted."), + file); + if (MessageBox(hwndDlg, buf, TranslateT("The profile already exists"), MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2) != IDYES) + return 0; + + // move the file + SHFILEOPSTRUCT sf = {0}; + sf.wFunc = FO_DELETE; + sf.pFrom = buf; + sf.fFlags = FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_SILENT | FOF_ALLOWUNDO; + mir_sntprintf(buf, SIZEOF(buf), _T("%s\0"), profile); + if (SHFileOperation(&sf) != 0) { + mir_sntprintf(buf, SIZEOF(buf), TranslateT("Couldn't move '%s' to the Recycle Bin, Please select another profile name."), file); + MessageBox(0, buf, TranslateT("Problem moving profile"), MB_ICONINFORMATION|MB_OK); + return 0; + } + // now the file should be gone! + } + // ask the database to create the profile + CreatePathToFileT(profile); + if ((err = link->makeDatabase(profile)) != ERROR_SUCCESS) { + mir_sntprintf(buf, SIZEOF(buf), TranslateT("Unable to create the profile '%s', the error was %x"), file, err); + MessageBox(hwndDlg, buf, TranslateT("Problem creating profile"), MB_ICONERROR|MB_OK); + return 0; + } + + // the profile has been created! + g_bDbCreated = true; + return 1; +} + static LRESULT CALLBACK ProfileNameValidate(HWND edit, UINT msg, WPARAM wParam, LPARAM lParam) { if (msg == WM_CHAR) { @@ -119,7 +160,7 @@ static LRESULT CALLBACK ProfileNameValidate(HWND edit, UINT msg, WPARAM wParam, static INT_PTR CALLBACK DlgProfileNew(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam) { - struct DlgProfData * dat = (struct DlgProfData *)GetWindowLongPtr(hwndDlg, GWLP_USERDATA); + struct DlgProfData *dat = (struct DlgProfData *)GetWindowLongPtr(hwndDlg, GWLP_USERDATA); switch (msg) { case WM_INITDIALOG: TranslateDialogDefault(hwndDlg); @@ -189,7 +230,7 @@ static INT_PTR CALLBACK DlgProfileNew(HWND hwndDlg, UINT msg, WPARAM wParam, LPA case WM_NOTIFY: { - NMHDR* hdr = (NMHDR*)lParam; + NMHDR *hdr = (NMHDR*)lParam; if (hdr && hdr->code == PSN_APPLY && dat && IsWindowVisible(hwndDlg)) { TCHAR szName[MAX_PATH]; LRESULT curSel = SendDlgItemMessage(hwndDlg, IDC_PROFILEDRIVERS, CB_GETCURSEL, 0, 0); @@ -203,9 +244,8 @@ static INT_PTR CALLBACK DlgProfileNew(HWND hwndDlg, UINT msg, WPARAM wParam, LPA dat->pd->newProfile = 1; dat->pd->dblink = (DATABASELINK *)SendDlgItemMessage(hwndDlg, IDC_PROFILEDRIVERS, CB_GETITEMDATA, (WPARAM)curSel, 0); - if (makeDatabase(dat->pd->szProfile, dat->pd->dblink, hwndDlg) == 0) { + if ( CreateProfile(dat->pd->szProfile, dat->pd->dblink, hwndDlg) == 0) SetWindowLongPtr(hwndDlg, DWLP_MSGRESULT, PSNRET_INVALID_NOCHANGEPAGE); - } } } break; @@ -751,7 +791,7 @@ static INT_PTR CALLBACK DlgProfileManager(HWND hwndDlg, UINT msg, WPARAM wParam, return FALSE; } -static int AddProfileManagerPage(struct DetailsPageInit * opi, OPTIONSDIALOGPAGE * odp) +static int AddProfileManagerPage(struct DetailsPageInit *opi, OPTIONSDIALOGPAGE *odp) { if (odp->cbSize != sizeof(OPTIONSDIALOGPAGE)) return 1; @@ -777,12 +817,9 @@ static int AddProfileManagerPage(struct DetailsPageInit * opi, OPTIONSDIALOGPAGE int getProfileManager(PROFILEMANAGERDATA * pd) { - DetailsPageInit opi; - opi.pageCount = 0; - opi.odp = NULL; + DetailsPageInit opi = { 0 }; - OPTIONSDIALOGPAGE odp = { 0 }; - odp.cbSize = sizeof(odp); + OPTIONSDIALOGPAGE odp = { sizeof(odp) }; odp.pszTitle = LPGEN("My Profiles"); odp.pfnDlgProc = DlgProfileSelect; odp.pszTemplate = MAKEINTRESOURCEA(IDD_PROFILE_SELECTION); @@ -797,16 +834,13 @@ int getProfileManager(PROFILEMANAGERDATA * pd) PROPSHEETHEADER psh = { 0 }; psh.dwSize = sizeof(psh); psh.dwFlags = PSH_PROPSHEETPAGE|PSH_NOAPPLYNOW; - psh.hwndParent = NULL; psh.nPages = opi.pageCount; - psh.pStartPage = 0; psh.ppsp = (PROPSHEETPAGE*)opi.odp; DlgProfData prof; prof.pd = pd; prof.psh = &psh; int rc = DialogBoxParam(hInst, MAKEINTRESOURCE(IDD_PROFILEMANAGER), NULL, DlgProfileManager, (LPARAM)&prof); - if (rc != -1) for (int i=0; i < opi.pageCount; i++) { mir_free((char*)opi.odp[i].pszTitle); diff --git a/src/modules/database/profilemanager.h b/src/modules/database/profilemanager.h index ae68e20388..f289a77ef8 100644 --- a/src/modules/database/profilemanager.h +++ b/src/modules/database/profilemanager.h @@ -31,7 +31,6 @@ struct PROFILEMANAGERDATA }; char* makeFileName(const TCHAR* tszOriginalName); -int makeDatabase(TCHAR *profile, DATABASELINK * link, HWND hwndDlg); int getProfileManager(PROFILEMANAGERDATA * pd); int getProfilePath(TCHAR *buf, size_t cch); int isValidProfileName(const TCHAR *name); @@ -40,3 +39,4 @@ bool shouldAutoCreate(TCHAR *szProfile); extern TCHAR g_profileDir[MAX_PATH]; extern TCHAR g_profileName[MAX_PATH]; +extern bool g_bDbCreated; diff --git a/src/modules/netlib/netliblog.cpp b/src/modules/netlib/netliblog.cpp index 4b2272932c..81ae80c604 100644 --- a/src/modules/netlib/netliblog.cpp +++ b/src/modules/netlib/netliblog.cpp @@ -138,18 +138,15 @@ static INT_PTR CALLBACK LogOptionsDlgProc(HWND hwndDlg, UINT message, WPARAM wPa break; */ case IDC_FILENAME: - if (HIWORD(wParam) != EN_CHANGE) break; - if ((HWND)lParam == GetFocus()) - CheckDlgButton(hwndDlg, IDC_TOFILE, BST_CHECKED); + if (HIWORD(wParam) == EN_CHANGE) { + if ((HWND)lParam == GetFocus()) + CheckDlgButton(hwndDlg, IDC_TOFILE, BST_CHECKED); - { TCHAR path[MAX_PATH]; GetWindowText((HWND)lParam, path, MAX_PATH); - TCHAR *pszNewPath = Utils_ReplaceVarsT(path); - PathToAbsoluteT(pszNewPath, path, NULL); + PathToAbsoluteT( VARST(path), path, NULL); SetDlgItemText(hwndDlg, IDC_PATH, path); - mir_free(pszNewPath); } break; case IDC_FILENAMEBROWSE: @@ -568,13 +565,11 @@ void NetlibLogInit(void) if ( !DBGetContactSettingTString(NULL, "Netlib", "File", &dbv)) { logOptions.szUserFile = mir_tstrdup(dbv.ptszVal); - TCHAR *pszNewPath = Utils_ReplaceVarsT(dbv.ptszVal); TCHAR path[MAX_PATH]; - PathToAbsoluteT(pszNewPath, path, NULL); + PathToAbsoluteT( VARST(dbv.ptszVal), path, NULL); logOptions.szFile = mir_tstrdup(path); - mir_free(pszNewPath); db_free(&dbv); } else { diff --git a/src/modules/utils/path.cpp b/src/modules/utils/path.cpp index 8c9cfbc6fe..0e9f38eb42 100644 --- a/src/modules/utils/path.cpp +++ b/src/modules/utils/path.cpp @@ -388,11 +388,10 @@ XCHAR *ReplaceVariables(XCHAR *str, REPLACEVARSDATA *data) static INT_PTR replaceVars(WPARAM wParam, LPARAM lParam) { REPLACEVARSDATA *data = (REPLACEVARSDATA *)lParam; - if ( !(data->dwFlags & RVF_UNICODE)) - return (INT_PTR)ReplaceVariables((char *)wParam, data); + if (data->dwFlags & RVF_UNICODE) + return (INT_PTR)ReplaceVariables((WCHAR *)wParam, data); - - return (INT_PTR)ReplaceVariables((WCHAR *)wParam, data); + return (INT_PTR)ReplaceVariables((char *)wParam, data); } int InitPathUtils(void) diff --git a/src/modules/utils/utils.cpp b/src/modules/utils/utils.cpp index 18d5e88973..8c577679c5 100644 --- a/src/modules/utils/utils.cpp +++ b/src/modules/utils/utils.cpp @@ -429,11 +429,11 @@ static INT_PTR RestartMiranda(WPARAM wParam, LPARAM) si.cb = sizeof(si); GetModuleFileName(NULL, mirandaPath, SIZEOF(mirandaPath)); if (wParam) { - TCHAR *profilename = Utils_ReplaceVarsT(_T("%miranda_profilename%")); - mir_sntprintf(cmdLine, SIZEOF(cmdLine), _T("\"%s\" /restart:%d /profile=%s"), mirandaPath, GetCurrentProcessId(), profilename); - mir_free(profilename); - } else - mir_sntprintf(cmdLine, SIZEOF(cmdLine), _T("\"%s\" /restart:%d"), mirandaPath, GetCurrentProcessId()); + VARST profilename( _T("%miranda_profilename%")); + mir_sntprintf(cmdLine, SIZEOF(cmdLine), _T("\"%s\" /restart:%d /profile=%s"), mirandaPath, GetCurrentProcessId(), (TCHAR*)profilename); + } + else mir_sntprintf(cmdLine, SIZEOF(cmdLine), _T("\"%s\" /restart:%d"), mirandaPath, GetCurrentProcessId()); + CallService("CloseAction", 0, 0); CreateProcess(mirandaPath, cmdLine, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi); return 0; -- cgit v1.2.3