From 1bb9a86b26d3a9964844d42fa25690ce0a028258 Mon Sep 17 00:00:00 2001 From: Dmitry Kuzkin Date: Thu, 18 Jun 2015 12:36:07 +0000 Subject: true unicode build (ansi build is possible but useless) new search/replace dialog edit improvements (type check) resident settings support (fully resident modules are still invisible) remove obsolete things code cleanup git-svn-id: http://svn.miranda-ng.org/main/trunk@14243 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/DbEditorPP/src/exportimport.cpp | 329 +++++++++++++++----------------- 1 file changed, 149 insertions(+), 180 deletions(-) (limited to 'plugins/DbEditorPP/src/exportimport.cpp') diff --git a/plugins/DbEditorPP/src/exportimport.cpp b/plugins/DbEditorPP/src/exportimport.cpp index 25d1d4779a..61278154f4 100644 --- a/plugins/DbEditorPP/src/exportimport.cpp +++ b/plugins/DbEditorPP/src/exportimport.cpp @@ -1,140 +1,147 @@ #include "headers.h" -int Mode; -HWND hwnd2importWindow; -static int Openfile(TCHAR *outputFile, const char *module) +TCHAR *GetFilter() +{ + static TCHAR filter[MAX_PATH]; + mir_sntprintf(filter, _T("%s%c*.ini%c%s%c*.*%c"), TranslateT("INI Files"), 0, 0, TranslateT("All Files"), 0, 0); + return filter; +} + + +int Openfile(TCHAR *outputFile, const char *module, int maxlen) { OPENFILENAME ofn = { 0 }; - char filename[MAX_PATH] = ""; - char filter[MAX_PATH]; - mir_snprintf(filter, SIZEOF(filter), "%s%c*.ini%c%s%c*.*%c", Translate("INI Files"), 0, 0, Translate("All Files"), 0, 0); - char *title = Translate("Export to file"); + TCHAR filename[MAX_PATH]; if (module) { int n = 0; - mir_strncpy(filename, module, SIZEOF(filename)); + mir_tstrncpy(filename, _A2T(module), SIZEOF(filename)); while (filename[n]) { switch (filename[n]) { - case '*': - case ':': - case '/': - case '?': - case '|': - case '\\': - filename[n] = '_'; + case _T('*'): + case _T(':'): + case _T('/'): + case _T('?'): + case _T('|'): + case _T('\\'): + filename[n] = _T('_'); break; } n++; } - } + } + else + filename[0] = 0; + ofn.lStructSize = sizeof(ofn); ofn.lpstrFile = filename; - ofn.lpstrFilter = filter; + ofn.lpstrFilter = GetFilter(); ofn.Flags = OFN_HIDEREADONLY | OFN_SHAREAWARE | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT; - ofn.lpstrTitle = title; - ofn.nMaxFile = MAX_PATH; - ofn.lpstrDefExt = "ini"; + ofn.lpstrTitle = TranslateT("Export to file"); + ofn.nMaxFile = maxlen; + ofn.lpstrDefExt = _T("ini"); if (!GetSaveFileName(&ofn)) return 0; - _tcsncpy_s(outputFile, MAX_PATH, filename, _TRUNCATE); + mir_tstrncpy(outputFile, filename, maxlen); return 1; } -void exportModule(MCONTACT hContact, char *module, FILE *file) +void exportModule(MCONTACT hContact, const char *module, FILE *file) { char tmp[32]; ModuleSettingLL settinglist; ModSetLinkLinkItem *setting; - EnumSettings(hContact, module, &settinglist); + if (IsModuleEmpty(hContact, module) || !EnumSettings(hContact, module, &settinglist)) + return; // print the module header.. fprintf(file, "\n[%s]", module); setting = settinglist.first; while (setting) { DBVARIANT dbv; - if (!GetSetting(hContact, module, setting->name, &dbv)) { + + if (!db_get_s(hContact, module, setting->name, &dbv, 0)) { + switch (dbv.type) { case DBVT_BYTE: - fprintf(file, "\n%s=b%s", setting->name, itoa(dbv.bVal, tmp, 10)); - db_free(&dbv); + fprintf(file, "\n%s=b%s", setting->name, _ultoa(dbv.bVal, tmp, 10)); break; case DBVT_WORD: - fprintf(file, "\n%s=w%s", setting->name, itoa(dbv.wVal, tmp, 10)); - db_free(&dbv); + fprintf(file, "\n%s=w%s", setting->name, _ultoa(dbv.wVal, tmp, 10)); break; case DBVT_DWORD: - fprintf(file, "\n%s=d%s", setting->name, itoa(dbv.dVal, tmp, 10)); - db_free(&dbv); + fprintf(file, "\n%s=d%s", setting->name, _ultoa(dbv.dVal, tmp, 10)); break; + case DBVT_BLOB: + { + ptrA data(StringFromBlob(dbv.pbVal, dbv.cpbVal)); + fprintf(file, "\n%s=n%s", setting->name, data); + break; + } + case DBVT_WCHAR: case DBVT_ASCIIZ: case DBVT_UTF8: - if (strchr(dbv.pszVal, '\r')) { - CMStringA end = dbv.pszVal; + { + char *str = (dbv.type == DBVT_WCHAR) ? mir_utf8encodeW(dbv.pwszVal) : dbv.pszVal; + + if (strchr(str, '\r')) { + CMStringA end = str; end.Replace("\\", "\\\\"); end.Replace("\r", "\\r"); end.Replace("\n", "\\n"); fprintf(file, "\n%s=g%s", setting->name, end.c_str()); - break; + } else { + fprintf(file, "\n%s=%c", setting->name, (dbv.type == DBVT_ASCIIZ) ? 's' : 'u'); + fputs(str, file); } - fprintf(file, "\n%s=%c", setting->name, (dbv.type == DBVT_UTF8) ? 'u' : 's'); - fputs(dbv.pszVal, file); - db_free(&dbv); - break; - - case DBVT_BLOB: - char *data = (char*)mir_alloc(3 * (dbv.cpbVal + 1)*sizeof(char)); - data[0] = '\0'; - for (int j = 0; j < dbv.cpbVal; j++) { - char tmp[16]; - mir_snprintf(tmp, "%02X ", (BYTE)dbv.pbVal[j]); - mir_strcat(data, tmp); - } - fprintf(file, "\n%s=n%s", setting->name, data); - mir_free(data); - db_free(&dbv); + if (str != dbv.pszVal) + mir_free(str); break; } + } // switch + db_free(&dbv); } setting = (ModSetLinkLinkItem *)setting->next; } FreeModuleSettingLL(&settinglist); } + char* NickFromHContact(MCONTACT hContact) { - static char nick[512] = ""; + static char nick[NAME_SIZE] = ""; if (hContact) { - char szProto[256]; + char szProto[FLD_SIZE], name[NAME_SIZE]; int loaded = 0; - - if (GetValue(hContact, "Protocol", "p", szProto, SIZEOF(szProto))) - loaded = IsProtocolLoaded(szProto); - - if (!szProto[0] || !loaded) { - char name[256]; - - if (szProto[0]) { - if (GetValue(hContact, szProto, "Nick", name, SIZEOF(name))) - mir_snprintf(nick, SIZEOF(nick), "%s (%s)", name, szProto); - else - mir_snprintf(nick, SIZEOF(nick), "(UNKNOWN) (%s)", szProto); - } - else mir_snprintf(nick, SIZEOF(nick), "(UNKNOWN)"); + szProto[0] = 0; + + if (!db_get_static(hContact, "Protocol", "p", szProto, SIZEOF(szProto))) + loaded = Proto_IsProtocolLoaded(szProto) ? 1 : 0; + + if (!szProto[0] || db_get_static(hContact, szProto, "Nick", name, SIZEOF(name))) + mir_strncpy(name, "(UNKNOWN)", SIZEOF(name)); + + if (!loaded) { + if (szProto[0]) + mir_snprintf(nick, SIZEOF(nick), "%s (%s)", name, szProto); + else + mir_strncpy(nick, name, SIZEOF(nick)); } else { char *uid = (char*)CallProtoService(szProto, PS_GETCAPS, PFLAG_UNIQUEIDSETTING, 0); if ((INT_PTR)uid != CALLSERVICE_NOTFOUND && uid) { - char szUID[256]; - GetValue(hContact, szProto, uid, szUID, SIZEOF(szUID)); - mir_snprintf(nick, SIZEOF(nick), "%s *(%s)*<%s>*{%s}*", (char*)GetContactName(hContact, szProto, 0), szProto, uid, szUID); + char szUID[FLD_SIZE]; + GetValueA(hContact, szProto, uid, szUID, SIZEOF(szUID)); + mir_snprintf(nick, SIZEOF(nick), "%s *(%s)*<%s>*{%s}*", name, szProto, uid, szUID); } - else mir_snprintf(nick, SIZEOF(nick), "%s (%s)", (char*)GetContactName(hContact, szProto, 0), szProto); + else + mir_snprintf(nick, SIZEOF(nick), "%s (%s)", name, szProto); } } @@ -143,22 +150,21 @@ char* NickFromHContact(MCONTACT hContact) // hContact == -1 export entire db. module == NULL export entire contact. // hContact == -1, module == "" - all contacts -void exportDB(MCONTACT hContact, char *module) +void exportDB(MCONTACT hContact, const char *module) { ModSetLinkLinkItem *mod; // enum all the modules ModuleSettingLL modlist; - if (!EnumModules(&modlist)) { - msg(Translate("Error loading module list"), modFullname); + if (!EnumModules(&modlist)) return; - } TCHAR fileName[MAX_PATH]; - if (Openfile(fileName, (hContact == INVALID_CONTACT_ID) ? NULL : module)) { + + if (Openfile(fileName, (hContact == INVALID_CONTACT_ID) ? NULL : module, MAX_PATH)) { FILE *file = _tfopen(fileName, _T("wt")); if (!file) { - msg(Translate("Couldn't open file for writing"), modFullname); + msg(TranslateT("Couldn't open file for writing")); return; } @@ -192,18 +198,10 @@ void exportDB(MCONTACT hContact, char *module) fprintf(file, "\n\n"); while (hContact) { - // filter - if (Mode != MODE_ALL) { - char szProto[256]; - int loaded = 0; - - if (GetValue(hContact, "Protocol", "p", szProto, SIZEOF(szProto))) - loaded = IsProtocolLoaded(szProto); - - if ((loaded && Mode == MODE_UNLOADED) || (!loaded && Mode == MODE_LOADED)) { - hContact = db_find_next(hContact); - continue; - } + + if (ApplyProtoFilter(hContact)) { + hContact = db_find_next(hContact); + continue; } fprintf(file, "CONTACT: %s\n", NickFromHContact(hContact)); @@ -266,23 +264,27 @@ void exportDB(MCONTACT hContact, char *module) FreeModuleSettingLL(&modlist); } -MCONTACT CheckNewContact(char *myProto, char *uid, char *myName) +MCONTACT CheckNewContact(const char *myProto, const char *uid, const char *myName) { - char szProto[256], szName[256]; + char szProto[FLD_SIZE], szName[NAME_SIZE]; for (MCONTACT hContact = db_find_first(); hContact; hContact = db_find_next(hContact)) - if (DBGetContactSettingStringStatic(hContact, "Protocol", "p", szProto, 256)) + if (!db_get_static(hContact, "Protocol", "p", szProto, SIZEOF(szProto))) if (!mir_strcmp(szProto, myProto)) - if (GetValue(hContact, szProto, uid, szName, SIZEOF(szName)) && !mir_strcmp(szName, myName)) + if (GetValueA(hContact, szProto, uid, szName, SIZEOF(szName)) && !mir_strcmp(szName, myName)) return hContact; return INVALID_CONTACT_ID; } -void importSettings(MCONTACT hContact, char *importstring) + +void importSettings(MCONTACT hContact, char *utf8) { - char module[256] = "", setting[256] = "", *end; + char module[FLD_SIZE] = "", setting[FLD_SIZE] = "", *end; int i = 0, value, type; + char *importstring = utf8; + char uid[FLD_SIZE], szUID[FLD_SIZE], szProto[FLD_SIZE]; + importstring = strtok(importstring, "\n"); SetCursor(LoadCursor(NULL, IDC_WAIT)); @@ -307,11 +309,10 @@ void importSettings(MCONTACT hContact, char *importstring) int len = (int)mir_strlen(&importstring[i]); if (len > 10) { - char uid[256] = "", szUID[256] = "", szProto[512] = ""; - char *p1, *p2; + uid[0] = 0; szUID[0] = 0, szProto[0] = 0; - p1 = strrchr(&importstring[i], '>*{'); - p2 = strrchr(&importstring[i], '}*'); + char *p1 = strrchr(&importstring[i], '>*{'); + char *p2 = strrchr(&importstring[i], '}*'); if (p1 && p2 && p1 + 3 < p2 && p2 - p1 < SIZEOF(szUID)) { strncpy(szUID, p1 + 1, p2 - p1 - 2); @@ -355,7 +356,7 @@ void importSettings(MCONTACT hContact, char *importstring) if (end = strpbrk(&importstring[i + 2], "]")) { *end = '\0'; mir_strcpy(module, &importstring[i + 2]); - deleteModule(module, hContact, 1); + deleteModule(hContact, module, 0); } } else if (strstr(&importstring[i], "=") && module[0]) { // get the setting @@ -365,8 +366,8 @@ void importSettings(MCONTACT hContact, char *importstring) // get the type type = *(end + 1); - if (mir_tstrcmp(module, "CList") == 0 && mir_tstrcmp(setting, "Group") == 0) { - ptrW GroupName(mir_utf8decodeW(end + 2)); + if (mir_strcmp(module, "CList") == 0 && mir_strcmp(setting, "Group") == 0) { + ptrA GroupName(mir_utf8decodeA(end + 2)); if (!GroupName) continue; @@ -383,17 +384,17 @@ void importSettings(MCONTACT hContact, char *importstring) switch (type) { case 'b': case 'B': - if (sscanf((end + 2), "%d", &value) == 1) + if (sscanf((end + 2), "%u", &value) == 1) db_set_b(hContact, module, setting, (BYTE)value); break; case 'w': case 'W': - if (sscanf((end + 2), "%d", &value) == 1) + if (sscanf((end + 2), "%u", &value) == 1) db_set_w(hContact, module, setting, (WORD)value); break; case 'd': case 'D': - if (sscanf((end + 2), "%d", &value) == 1) + if (sscanf((end + 2), "%u", &value) == 1) db_set_dw(hContact, module, setting, (DWORD)value); break; case 's': @@ -434,70 +435,39 @@ void importSettings(MCONTACT hContact, char *importstring) SetCursor(LoadCursor(NULL, IDC_ARROW)); } + INT_PTR CALLBACK ImportDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_INITDIALOG: - hwnd2importWindow = hwnd; SetWindowLongPtr(hwnd, GWLP_USERDATA, lParam); TranslateDialogDefault(hwnd); - SendDlgItemMessage(hwnd, IDC_TEXT, EM_LIMITTEXT, (WPARAM)sizeof(TCHAR) * 0x7FFFFFFF, 0); + SendDlgItemMessage(hwnd, IDC_TEXT, EM_LIMITTEXT, (WPARAM)0x7FFFFFFF, 0); + + TCHAR name[NAME_SIZE], msg[MSG_SIZE]; + + GetContactName((MCONTACT)lParam, NULL, name, SIZEOF(name)); + + mir_sntprintf(msg, TranslateT("Import to \"%s\""), name); + SetWindowText(hwnd, msg); + break; case WM_COMMAND: switch (LOWORD(wParam)) { - case IDC_CRLF: - { - int length = GetWindowTextLength(GetDlgItem(hwnd, IDC_TEXT)); - char *string = (char*)_alloca(length + 3); - int Pos = 2; - - if (length) { - int Range = SendDlgItemMessage(hwnd, IDC_TEXT, EM_GETSEL, 0, 0); - int Min = LOWORD(Range); - int Max = HIWORD(Range); - - GetDlgItemText(hwnd, IDC_TEXT, string, length + 1); - - if (Min == -1) - memcpy(string, crlf_string, sizeof(crlf_string)); - else if (Max == -1 || Max >= length) - memcpy(&string[Min], crlf_string, sizeof(crlf_string)); - else if (Max - Min > 2) { - memcpy(&string[Min], crlf_string, sizeof(crlf_string)); - memmove(&string[Min + 2], &string[Max], length - Max + 1); - } - else { - memmove(&string[Min + 2], &string[Max], length - Max + 1); - memcpy(&string[Min], crlf_string, sizeof(crlf_string)); - } - - if (Min) Pos += Min; - } - else memcpy(string, crlf_string, sizeof(crlf_string)); - - SetDlgItemText(hwnd, IDC_TEXT, string); - SendDlgItemMessage(hwnd, IDC_TEXT, EM_SETSEL, Pos, Pos); - SetFocus(GetDlgItem(hwnd, IDC_TEXT)); - } - break; case IDCANCEL: DestroyWindow(hwnd); - hwnd2importWindow = 0; break; case IDOK: MCONTACT hContact = (MCONTACT)GetWindowLongPtr(hwnd, GWLP_USERDATA); int length = GetWindowTextLength(GetDlgItem(hwnd, IDC_TEXT)); if (length) { - char *string = (char*)_alloca(length + 1); - if (!string) { - msg(Translate("Couldn't allocate enough memory!"), modFullname); - DestroyWindow(hwnd); - } - GetDlgItemText(hwnd, IDC_TEXT, string, length + 1); - importSettings(hContact, string); + TCHAR *data = (TCHAR*)mir_alloc((length + 1)*sizeof(TCHAR)); + GetDlgItemText(hwnd, IDC_TEXT, data, length + 1); + importSettings(hContact, T2Utf(data)); + mir_free(data); refreshTree(1); } } @@ -506,27 +476,24 @@ INT_PTR CALLBACK ImportDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam return 0; } + void ImportSettingsMenuItem(MCONTACT hContact) { - if (hwnd2importWindow) - DestroyWindow(hwnd2importWindow); - - CreateDialogParam(hInst, MAKEINTRESOURCE(IDD_IMPORT), 0, ImportDlgProc, hContact); + CreateDialogParam(hInst, MAKEINTRESOURCE(IDD_IMPORT), hwnd2mainWindow, ImportDlgProc, hContact); } -int Openfile2Import(char *outputFiles) + +int Openfile2Import(TCHAR *outputFiles, int maxlen) { - char filter[MAX_PATH]; - mir_snprintf(filter, SIZEOF(filter), "%s%c*.ini%c%s%c*.*%c", Translate("INI Files"), 0, 0, Translate("All Files"), 0, 0); OPENFILENAME ofn = { 0 }; - ofn.lStructSize = OPENFILENAME_SIZE_VERSION_400; - ofn.lpstrFilter = filter; + ofn.lStructSize = sizeof(ofn); + ofn.lpstrFilter = GetFilter(); ofn.lpstrFile = outputFiles; - ofn.nMaxFile = MAX_PATH * 10; + ofn.nMaxFile = maxlen; ofn.nMaxFileTitle = MAX_PATH; - ofn.Flags = OFN_HIDEREADONLY | OFN_SHAREAWARE | OFN_PATHMUSTEXIST | OFN_ALLOWMULTISELECT | OFN_EXPLORER; - ofn.lpstrTitle = Translate("Import from files"); + ofn.Flags = OFN_SHAREAWARE | OFN_PATHMUSTEXIST | OFN_ALLOWMULTISELECT | OFN_EXPLORER; + ofn.lpstrTitle = TranslateT("Import from files"); if (!GetOpenFileName(&ofn)) return 0; @@ -538,43 +505,45 @@ BOOL Exists(LPCTSTR strName) return GetFileAttributes(strName) != INVALID_FILE_ATTRIBUTES; } -void ImportSettingsFromFileMenuItem(MCONTACT hContact, char* FilePath) +void ImportSettingsFromFileMenuItem(MCONTACT hContact, const char *FilePath) { - char szFileNames[MAX_PATH * 10] = { 0 }; - char szPath[MAX_PATH] = ""; - char szFile[MAX_PATH]; + TCHAR szFileNames[MAX_PATH * 10]; + TCHAR szPath[MAX_PATH] = { 0 }; + TCHAR szFile[MAX_PATH]; DWORD offset = 0; - if (mir_tstrcmp(FilePath, "") == 0) - offset = Openfile2Import(szFileNames); + + mir_tstrcpy(szFileNames, _T("")); + + if (!FilePath) + offset = Openfile2Import(szFileNames, SIZEOF(szFileNames)); else { - if (Exists(FilePath)) - mir_tstrcpy(szFileNames, FilePath); - else - mir_tstrcpy(szFileNames, ""); + _A2T tmp(FilePath); + if (GetFileAttributes(tmp) != INVALID_FILE_ATTRIBUTES) + mir_tstrncpy(szFileNames, tmp, SIZEOF(szFileNames)); } int index = 0; - if (!mir_tstrcmp(szFileNames, "") == 0) { - if ((DWORD)mir_strlen(szFileNames) < offset) { + if (mir_tstrcmp(szFileNames, _T(""))) { + if ((DWORD)mir_tstrlen(szFileNames) < offset) { index += offset; - strncpy(szPath, szFileNames, offset); - mir_strcat(szPath, "\\"); + mir_tstrncpy(szPath, szFileNames, offset); + mir_tstrcat(szPath, _T("\\")); } while (szFileNames[index]) { - mir_strcpy(szFile, szPath); - mir_strcat(szFile, &szFileNames[index]); - index += (int)mir_strlen(&szFileNames[index]) + 1; + mir_tstrcpy(szFile, szPath); + mir_tstrcat(szFile, &szFileNames[index]); + index += (int)mir_tstrlen(&szFileNames[index]) + 1; HANDLE hFile = CreateFile(szFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); if (hFile != INVALID_HANDLE_VALUE) { if (GetFileSize(hFile, NULL) > 0) { HANDLE hMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL); if (hMap) { - PBYTE pFile = (PBYTE)MapViewOfFile(hMap, FILE_MAP_COPY, 0, 0, 0); + char *pFile = (char*)MapViewOfFile(hMap, FILE_MAP_COPY, 0, 0, 0); if (pFile) { - importSettings(hContact, (char*)pFile); + importSettings(hContact, pFile); UnmapViewOfFile(pFile); } CloseHandle(hMap); @@ -584,7 +553,7 @@ void ImportSettingsFromFileMenuItem(MCONTACT hContact, char* FilePath) } else break; } - if (mir_tstrcmp(FilePath, "") == 0) + if (mir_strcmp(FilePath, "") == 0) refreshTree(1); } } -- cgit v1.2.3