From bebfb51465b364d5673a3f555655213dbff0b2c6 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Tue, 31 Mar 2015 11:55:37 +0000 Subject: Non-IM Contact: - fix for a crash on exit; - code cleaning git-svn-id: http://svn.miranda-ng.org/main/trunk@12573 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/Non-IM Contact/src/files.cpp | 261 ++++++++++++++++------------------- 1 file changed, 118 insertions(+), 143 deletions(-) (limited to 'plugins/Non-IM Contact/src/files.cpp') diff --git a/plugins/Non-IM Contact/src/files.cpp b/plugins/Non-IM Contact/src/files.cpp index 704a7533f3..1c183c482d 100644 --- a/plugins/Non-IM Contact/src/files.cpp +++ b/plugins/Non-IM Contact/src/files.cpp @@ -1,13 +1,13 @@ #include "commonheaders.h" -INT_PTR exportContacts(WPARAM wParam,LPARAM lParam) +INT_PTR exportContacts(WPARAM wParam, LPARAM lParam) { char fn[MAX_PATH]; if (!Openfile(fn, 0)) return 0; FILE* file; - if (MessageBox(0, TranslateT("Do you want to overwrite the contents of the file?\r\n\r\nPressing No will append these contacts to the end of the file."),_T(modFullname), MB_YESNO) == IDYES) + if (MessageBox(0, TranslateT("Do you want to overwrite the contents of the file?\r\n\r\nPressing No will append these contacts to the end of the file."), _T(modFullname), MB_YESNO) == IDYES) file = fopen(fn, "w"); else file = fopen(fn, "a"); @@ -17,15 +17,15 @@ INT_PTR exportContacts(WPARAM wParam,LPARAM lParam) for (MCONTACT hContact = db_find_first(MODNAME); hContact; hContact = db_find_next(hContact, MODNAME)) { int tmp; char DBVar[1024]; - if (db_get_static(hContact, MODNAME, "Name", DBVar)) { + if (db_get_static(hContact, MODNAME, "Name", DBVar, SIZEOF(DBVar))) { fprintf(file, "\r\n[Non-IM Contact]\r\nName=%s\r\n", DBVar); - if (db_get_static(hContact, MODNAME, "ProgramString", DBVar)) + if (db_get_static(hContact, MODNAME, "ProgramString", DBVar, SIZEOF(DBVar))) fprintf(file, "ProgramString=%s\r\n", DBVar); - if (db_get_static(hContact, MODNAME, "ProgramParamString", DBVar)) + if (db_get_static(hContact, MODNAME, "ProgramParamString", DBVar, SIZEOF(DBVar))) fprintf(file, "ProgramParamString=%s\r\n", DBVar); - if (db_get_static(hContact, MODNAME, "ToolTip", DBVar)) + if (db_get_static(hContact, MODNAME, "ToolTip", DBVar, SIZEOF(DBVar))) fprintf(file, "ToolTip=%s\r\n", DBVar); - if (db_get_static(hContact, "CList", "Group", DBVar)) + if (db_get_static(hContact, "CList", "Group", DBVar, SIZEOF(DBVar))) fprintf(file, "Group=%s\r\n", DBVar); if (tmp = db_get_w(hContact, MODNAME, "Icon", 40072)) fprintf(file, "Icon=%d\r\n", tmp); @@ -55,7 +55,7 @@ int Openfile(char *outputFile, int saveOpen) //0=save, 1=open OPENFILENAMEA ofn = { sizeof(ofn) }; ofn.lpstrFile = filename; ofn.lpstrFilter = filter; - ofn.Flags = saveOpen? OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_SHAREAWARE | OFN_PATHMUSTEXIST: OFN_HIDEREADONLY | OFN_SHAREAWARE | OFN_PATHMUSTEXIST; + ofn.Flags = saveOpen ? OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_SHAREAWARE | OFN_PATHMUSTEXIST : OFN_HIDEREADONLY | OFN_SHAREAWARE | OFN_PATHMUSTEXIST; ofn.lpstrTitle = title; ofn.nMaxFile = MAX_PATH; @@ -65,23 +65,21 @@ int Openfile(char *outputFile, int saveOpen) //0=save, 1=open r = GetSaveFileNameA(&ofn); if (!r) return 0; - mir_strcpy(outputFile,filename); + mir_strcpy(outputFile, filename); return 1; } - void reloadFiles(HWND fileList) { int i, index; char file[MAX_PATH], fn[6]; - SendMessage(fileList,CB_RESETCONTENT, 0,0); - for (i=0; ;i++) - { + SendMessage(fileList, CB_RESETCONTENT, 0, 0); + for (i = 0;; i++) { mir_snprintf(fn, SIZEOF(fn), "fn%d", i); - if (db_get_static(NULL, MODNAME, fn, file)) { - index = SendMessageA(fileList, CB_ADDSTRING,0, (LPARAM)file); - SendMessage(fileList, CB_SETITEMDATA, index, (LPARAM)i); - SendMessage(fileList, CB_SETCURSEL, index,0); + if (db_get_static(NULL, MODNAME, fn, file, SIZEOF(file))) { + index = SendMessageA(fileList, CB_ADDSTRING, 0, (LPARAM)file); + SendMessage(fileList, CB_SETITEMDATA, index, (LPARAM)i); + SendMessage(fileList, CB_SETCURSEL, index, 0); SetDlgItemTextA(GetParent(fileList), IDC_FN, _itoa(i, fn, 10)); /* add the file contents to the edit box */ } @@ -92,9 +90,8 @@ void reloadFiles(HWND fileList) int savehtml(char* outFile) { FILE* file = fopen(outFile, "w"); - if (!file) - { - fclose(file); + if (!file) { + fclose(file); return 0; } fprintf(file, "%s", szInfo); @@ -104,39 +101,38 @@ int savehtml(char* outFile) void readFile(HWND hwnd) { - int lineNumber, fileLength=0, width=0; + int lineNumber, fileLength = 0, width = 0; char temp[MAX_STRING_LENGTH], szFileName[512], temp1[MAX_STRING_LENGTH], fn[8]; - int fileNumber = SendDlgItemMessage(hwnd, IDC_FILE_LIST,CB_GETCURSEL, 0,0); + int fileNumber = SendDlgItemMessage(hwnd, IDC_FILE_LIST, CB_GETCURSEL, 0, 0); mir_snprintf(fn, SIZEOF(fn), "fn%d", fileNumber); - if (!db_get_static(NULL, MODNAME, fn, szFileName)) { - msg(Translate("File couldn't be opened"),fn); + if (!db_get_static(NULL, MODNAME, fn, szFileName, SIZEOF(szFileName))) { + msg(Translate("File couldn't be opened"), fn); return; } - if ( !strncmp("http://", szFileName, strlen("http://")) || !strncmp("https://", szFileName, strlen("https://"))) + if (!strncmp("http://", szFileName, strlen("http://")) || !strncmp("https://", szFileName, strlen("https://"))) mir_snprintf(szFileName, SIZEOF(szFileName), "%s\\plugins\\fn%d.html", getMimDir(temp), fileNumber); - FILE *filen = fopen(szFileName,"r"); + FILE *filen = fopen(szFileName, "r"); if (!filen) { - MessageBox(0, TranslateT("File couldn't be opened,2"), _T(modFullname),MB_OK); + MessageBox(0, TranslateT("File couldn't be opened,2"), _T(modFullname), MB_OK); return; } lineNumber = 0; - SendDlgItemMessage(hwnd, IDC_FILE_CONTENTS,LB_RESETCONTENT, 0,0); - while (lineNumber < (MAXLINES) && (fgets(temp, MAX_STRING_LENGTH, filen))) - { + SendDlgItemMessage(hwnd, IDC_FILE_CONTENTS, LB_RESETCONTENT, 0, 0); + while (lineNumber < (MAXLINES) && (fgets(temp, MAX_STRING_LENGTH, filen))) { if (temp[0] == '\t') temp[0] = ' '; - if (temp[strlen(temp)-1]=='\n' && temp[strlen(temp)-2]=='\r') - temp[strlen(temp)-2]='\0'; - else if (temp[strlen(temp)-1]=='\n') - temp[strlen(temp)-1]='\0'; - else temp[strlen(temp)]='\0'; + if (temp[strlen(temp) - 1] == '\n' && temp[strlen(temp) - 2] == '\r') + temp[strlen(temp) - 2] = '\0'; + else if (temp[strlen(temp) - 1] == '\n') + temp[strlen(temp) - 1] = '\0'; + else temp[strlen(temp)] = '\0'; mir_snprintf(temp1, SIZEOF(temp1), Translate("line(%-3d) = | %s"), lineNumber, temp); - SendDlgItemMessageA(hwnd, IDC_FILE_CONTENTS,LB_ADDSTRING,0,(LPARAM)temp1); + SendDlgItemMessageA(hwnd, IDC_FILE_CONTENTS, LB_ADDSTRING, 0, (LPARAM)temp1); lineNumber++; fileLength++; - if ((unsigned int)SendDlgItemMessage(hwnd, IDC_FILE_CONTENTS,LB_GETHORIZONTALEXTENT,0,0) <= (strlen(temp1)*db_get_b(NULL, MODNAME, "WidthMultiplier", 5))) - SendDlgItemMessage(hwnd, IDC_FILE_CONTENTS,LB_SETHORIZONTALEXTENT,(strlen(temp1)*db_get_b(NULL, MODNAME, "WidthMultiplier", 5)),0); + if ((unsigned int)SendDlgItemMessage(hwnd, IDC_FILE_CONTENTS, LB_GETHORIZONTALEXTENT, 0, 0) <= (strlen(temp1)*db_get_b(NULL, MODNAME, "WidthMultiplier", 5))) + SendDlgItemMessage(hwnd, IDC_FILE_CONTENTS, LB_SETHORIZONTALEXTENT, (strlen(temp1)*db_get_b(NULL, MODNAME, "WidthMultiplier", 5)), 0); } fclose(filen); } @@ -145,20 +141,18 @@ void readFile(HWND hwnd) INT_PTR CALLBACK DlgProcFiles(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { - switch(msg) { + switch (msg) { case WM_RELOADWINDOW: + char fn[MAX_PATH], string[MAX_STRING_LENGTH], tmp[MAX_STRING_LENGTH]; + reloadFiles(GetDlgItem(hwnd, IDC_FILE_LIST)); { - char fn[MAX_PATH], string[MAX_STRING_LENGTH], tmp[MAX_STRING_LENGTH]; - reloadFiles(GetDlgItem(hwnd, IDC_FILE_LIST)); - int i = SendDlgItemMessage(hwnd, IDC_FILE_LIST,CB_GETCURSEL, 0 ,0); + int i = SendDlgItemMessage(hwnd, IDC_FILE_LIST, CB_GETCURSEL, 0, 0); mir_snprintf(fn, SIZEOF(fn), "fn%d", i); - SendDlgItemMessage(hwnd, IDC_FILE_CONTENTS,LB_RESETCONTENT, 0,0); - if (db_get_static(NULL, MODNAME, fn, string) ) - { - if ( (!strncmp("http://", string, strlen("http://"))) || (!strncmp("https://", string, strlen("https://"))) ) - { - SetDlgItemTextA(hwnd,IDC_URL, string); - mir_snprintf(fn,SIZEOF(fn),"fn%d_timer", i); + SendDlgItemMessage(hwnd, IDC_FILE_CONTENTS, LB_RESETCONTENT, 0, 0); + if (db_get_static(NULL, MODNAME, fn, string, SIZEOF(string))) { + if ((!strncmp("http://", string, strlen("http://"))) || (!strncmp("https://", string, strlen("https://")))) { + SetDlgItemTextA(hwnd, IDC_URL, string); + mir_snprintf(fn, SIZEOF(fn), "fn%d_timer", i); SetDlgItemTextA(hwnd, IDC_WWW_TIMER, _itoa(db_get_w(NULL, MODNAME, fn, 60), tmp, 10)); } readFile(hwnd); @@ -167,132 +161,112 @@ INT_PTR CALLBACK DlgProcFiles(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) break; case WM_INITDIALOG: - { - SendMessage(hwnd, WM_RELOADWINDOW, 0,0); - TranslateDialogDefault(hwnd); - } + SendMessage(hwnd, WM_RELOADWINDOW, 0, 0); + TranslateDialogDefault(hwnd); return TRUE; + case WM_COMMAND: SendMessage(GetParent(hwnd), PSM_CHANGED, 0, 0); - switch(LOWORD(wParam)) { + switch (LOWORD(wParam)) { case IDC_ADD_URL: - { - if (GetWindowTextLength(GetDlgItem(hwnd,IDC_URL))) - { - char text[512], url[512], fn[10] = "fn0", szFileName[MAX_PATH], temp[512]; - int i, timer; - GetDlgItemTextA(hwnd,IDC_URL,text,SIZEOF(text)); - strcpy(url, text); - if (!InternetDownloadFile(text)) - { - for (i=0; ;i++) - { - mir_snprintf(fn, SIZEOF(fn), "fn%d", i); - if (!db_get_static(NULL, MODNAME, fn, text)) - break; - } - mir_snprintf(szFileName, SIZEOF(szFileName), "%s\\plugins\\%s.html", getMimDir(temp), fn); - if (savehtml(szFileName)) - { - mir_snprintf(fn, SIZEOF(fn), "fn%d", i); - db_set_s(NULL, MODNAME, fn, url); - if (!GetWindowTextLength(GetDlgItem(hwnd,IDC_WWW_TIMER))) - timer = 60; - else - { - GetDlgItemTextA(hwnd,IDC_WWW_TIMER,text,SIZEOF(text)); - timer = atoi(text); - } - mir_snprintf(fn, SIZEOF(fn), "fn%d_timer", i); - db_set_w(NULL, MODNAME, fn, (WORD)timer); - SendMessage(hwnd, WM_RELOADWINDOW, 0,0); + if (GetWindowTextLength(GetDlgItem(hwnd, IDC_URL))) { + char text[512], url[512], fn[10] = "fn0", szFileName[MAX_PATH], temp[512]; + int i, timer; + GetDlgItemTextA(hwnd, IDC_URL, text, SIZEOF(text)); + strcpy(url, text); + if (!InternetDownloadFile(text)) { + for (i = 0;; i++) { + mir_snprintf(fn, SIZEOF(fn), "fn%d", i); + if (!db_get_static(NULL, MODNAME, fn, text, SIZEOF(text))) + break; + } + mir_snprintf(szFileName, SIZEOF(szFileName), "%s\\plugins\\%s.html", getMimDir(temp), fn); + if (savehtml(szFileName)) { + mir_snprintf(fn, SIZEOF(fn), "fn%d", i); + db_set_s(NULL, MODNAME, fn, url); + if (!GetWindowTextLength(GetDlgItem(hwnd, IDC_WWW_TIMER))) + timer = 60; + else { + GetDlgItemTextA(hwnd, IDC_WWW_TIMER, text, SIZEOF(text)); + timer = atoi(text); } + mir_snprintf(fn, SIZEOF(fn), "fn%d_timer", i); + db_set_w(NULL, MODNAME, fn, (WORD)timer); + SendMessage(hwnd, WM_RELOADWINDOW, 0, 0); } } } break; + case IDC_WWW_TIMER: if (HIWORD(wParam) == EN_CHANGE) - SendMessage(GetParent(hwnd),PSM_CHANGED,0,0); + SendMessage(GetParent(hwnd), PSM_CHANGED, 0, 0); break; case IDC_ADD_FILE: - { - int i, index; - char file[MAX_PATH], fn[6]; - for (i=0; ;i++) - { - mir_snprintf(fn, SIZEOF(fn), "fn%d", i); - if (!db_get_static(NULL, MODNAME, fn, file)) - break; - } - if (Openfile(file,1)) - { - db_set_s(NULL, MODNAME, fn, file); - index = SendDlgItemMessageA(hwnd, IDC_FILE_LIST, CB_ADDSTRING, 0, (LPARAM)file); - SendDlgItemMessage(hwnd, IDC_FILE_LIST,CB_SETITEMDATA,index,(LPARAM)i); - SendDlgItemMessage(hwnd, IDC_FILE_LIST,CB_SETCURSEL, index ,0); - SetDlgItemTextA(hwnd, IDC_FN, _itoa(i, fn, 10)); - mir_snprintf(fn, SIZEOF(fn), "fn%d", index); - readFile(hwnd); - } - + int i, index; + char file[MAX_PATH], fn[6]; + for (i = 0;; i++) { + mir_snprintf(fn, SIZEOF(fn), "fn%d", i); + if (!db_get_static(NULL, MODNAME, fn, file, SIZEOF(file))) + break; + } + if (Openfile(file, 1)) { + db_set_s(NULL, MODNAME, fn, file); + index = SendDlgItemMessageA(hwnd, IDC_FILE_LIST, CB_ADDSTRING, 0, (LPARAM)file); + SendDlgItemMessage(hwnd, IDC_FILE_LIST, CB_SETITEMDATA, index, (LPARAM)i); + SendDlgItemMessage(hwnd, IDC_FILE_LIST, CB_SETCURSEL, index, 0); + SetDlgItemTextA(hwnd, IDC_FN, _itoa(i, fn, 10)); + mir_snprintf(fn, SIZEOF(fn), "fn%d", index); + readFile(hwnd); } break; + case IDC_DEL_FILE: + char fn1[4], tmp[256]; { - int index = SendDlgItemMessage(hwnd, IDC_FILE_LIST,CB_GETCURSEL, 0,0),i= (int)SendDlgItemMessage(hwnd, IDC_FILE_LIST,CB_GETITEMDATA, index,0); - char fn[6], fn1[4], tmp[256]; - int count = SendDlgItemMessage(hwnd, IDC_FILE_LIST,CB_GETCOUNT, 0,0) -1; - if (index == count) - { + int index = SendDlgItemMessage(hwnd, IDC_FILE_LIST, CB_GETCURSEL, 0, 0), i = (int)SendDlgItemMessage(hwnd, IDC_FILE_LIST, CB_GETITEMDATA, index, 0); + int count = SendDlgItemMessage(hwnd, IDC_FILE_LIST, CB_GETCOUNT, 0, 0) - 1; + if (index == count) { mir_snprintf(fn, SIZEOF(fn), "fn%d", index); db_unset(NULL, MODNAME, fn); - SendDlgItemMessage(hwnd, IDC_FILE_LIST,CB_DELETESTRING, index ,0); - SendMessage(hwnd, WM_RELOADWINDOW, 0,0); + SendDlgItemMessage(hwnd, IDC_FILE_LIST, CB_DELETESTRING, index, 0); + SendMessage(hwnd, WM_RELOADWINDOW, 0, 0); if (!index) { - SetDlgItemText(hwnd, IDC_FN,_T("")); - SetDlgItemText(hwnd, IDC_FILE_CONTENTS,_T("")); + SetDlgItemText(hwnd, IDC_FN, _T("")); + SetDlgItemText(hwnd, IDC_FILE_CONTENTS, _T("")); } } - else - { + else { mir_snprintf(fn, SIZEOF(fn), "fn%d", i); - while (db_get_static(NULL, MODNAME, fn,tmp)) - { - mir_snprintf(fn1, SIZEOF(fn1), "fn%d", i-1); - db_set_s(NULL, MODNAME, fn1 , tmp); + while (db_get_static(NULL, MODNAME, fn, tmp, SIZEOF(tmp))) { + mir_snprintf(fn1, SIZEOF(fn1), "fn%d", i - 1); + db_set_s(NULL, MODNAME, fn1, tmp); mir_snprintf(fn, SIZEOF(fn), "fn%d", ++i); } mir_snprintf(fn, SIZEOF(fn), "fn%d", --i); db_unset(NULL, MODNAME, fn); - SendDlgItemMessage(hwnd, IDC_FILE_LIST,CB_DELETESTRING, index ,0); - SendMessage(hwnd, WM_RELOADWINDOW, 0,0); + SendDlgItemMessage(hwnd, IDC_FILE_LIST, CB_DELETESTRING, index, 0); + SendMessage(hwnd, WM_RELOADWINDOW, 0, 0); } - } break; - case IDC_FILE_LIST: - if (HIWORD(wParam) == CBN_SELCHANGE ) - { - int index = SendDlgItemMessage(hwnd, IDC_FILE_LIST,CB_GETCURSEL, 0,0); + if (HIWORD(wParam) == CBN_SELCHANGE) { + int index = SendDlgItemMessage(hwnd, IDC_FILE_LIST, CB_GETCURSEL, 0, 0); char fn[20], tmp[MAX_PATH]; SetDlgItemTextA(hwnd, IDC_FN, _itoa(index, fn, 10)); mir_snprintf(fn, SIZEOF(fn), "fn%d", index); - if (db_get_static(NULL, MODNAME, fn, tmp) ) - { - if (!strncmp("http://", tmp, strlen("http://")) || !strncmp("https://", tmp, strlen("https://"))) - { - SetDlgItemTextA(hwnd,IDC_URL, tmp); + if (db_get_static(NULL, MODNAME, fn, tmp, SIZEOF(tmp))) { + if (!strncmp("http://", tmp, strlen("http://")) || !strncmp("https://", tmp, strlen("https://"))) { + SetDlgItemTextA(hwnd, IDC_URL, tmp); mir_snprintf(fn, SIZEOF(fn), "fn%d_timer", index); SetDlgItemTextA(hwnd, IDC_WWW_TIMER, _itoa(db_get_w(NULL, MODNAME, fn, 60), tmp, 10)); } - else - { - SetDlgItemText(hwnd,IDC_URL, _T("")); - SetDlgItemText(hwnd, IDC_WWW_TIMER,_T("")); + else { + SetDlgItemText(hwnd, IDC_URL, _T("")); + SetDlgItemText(hwnd, IDC_WWW_TIMER, _T("")); } readFile(hwnd); } @@ -304,23 +278,24 @@ INT_PTR CALLBACK DlgProcFiles(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) return TRUE; } break; + case WM_NOTIFY: - switch(((LPNMHDR)lParam)->idFrom) { + switch (((LPNMHDR)lParam)->idFrom) { case 0: switch (((LPNMHDR)lParam)->code) { case PSN_APPLY: - int i = SendDlgItemMessage(hwnd, IDC_FILE_LIST,CB_GETCURSEL, 0 ,0); + int i = SendDlgItemMessage(hwnd, IDC_FILE_LIST, CB_GETCURSEL, 0, 0); int timer; char fn[MAX_PATH], string[1000]; mir_snprintf(fn, SIZEOF(fn), "fn%d", i); - if (GetWindowTextLength(GetDlgItem(hwnd,IDC_WWW_TIMER))) { + if (GetWindowTextLength(GetDlgItem(hwnd, IDC_WWW_TIMER))) { TCHAR text[5]; - GetDlgItemText(hwnd,IDC_WWW_TIMER,text,SIZEOF(text)); + GetDlgItemText(hwnd, IDC_WWW_TIMER, text, SIZEOF(text)); timer = _ttoi(text); } else timer = 60; - if (db_get_static(NULL, MODNAME, fn, string)) + if (db_get_static(NULL, MODNAME, fn, string, SIZEOF(string))) if (!strncmp("http://", string, strlen("http://")) || !strncmp("https://", string, strlen("https://"))) { mir_snprintf(fn, SIZEOF(fn), "fn%d_timer", i); db_set_w(NULL, MODNAME, fn, (WORD)timer); @@ -338,12 +313,12 @@ char* getMimDir(char* file) { GetModuleFileNameA(NULL, file, MAX_PATH); - char *p1 = strrchr(file,'\\'); + char *p1 = strrchr(file, '\\'); if (p1) *p1 = '\0'; if (file[0] == '\\') - file[strlen(file)-1] = '\0'; + file[strlen(file) - 1] = '\0'; return file; } -- cgit v1.2.3