From e20cb1fed6dafc7fa414476acde502e999088aab Mon Sep 17 00:00:00 2001 From: Mataes Date: Sat, 25 Apr 2020 19:02:43 +0300 Subject: NewStory: first version of json export. Format like Msg_export --- plugins/NewStory/src/history.cpp | 186 +++++++++++++++++++++++++++++++-------- plugins/NewStory/src/stdafx.h | 4 + plugins/NewStory/src/utils.cpp | 170 ----------------------------------- plugins/NewStory/src/utils.h | 1 - 4 files changed, 155 insertions(+), 206 deletions(-) (limited to 'plugins/NewStory') diff --git a/plugins/NewStory/src/history.cpp b/plugins/NewStory/src/history.cpp index 6e52e486dd..3dde394cc6 100644 --- a/plugins/NewStory/src/history.cpp +++ b/plugins/NewStory/src/history.cpp @@ -38,60 +38,59 @@ void InitHistory() HookEvent(ME_DB_EVENT_EDITED, evtEventEdited); } -/* -bool ExportHistoryDialog(HANDLE hContact, HWND hwndHistory) +/*bool ExportHistoryDialog(HANDLE hContact, HWND hwndHistory) { int filterIndex = 0; - char *filter = 0; + wchar_t *filter = 0; char filterSize = 0; - char *templates[100] = {0}; + wchar_t *templates[100] = {0}; int nTemplates = 0; WIN32_FIND_DATA ffd; - HANDLE hFind = FindFirstFile("plugins\\newstory\\x_*.txt", &ffd); + HANDLE hFind = FindFirstFile(L"plugins\\newstory\\x_*.txt", &ffd); while (hFind != INVALID_HANDLE_VALUE) { - char *fn = (char *)malloc(MAX_PATH); - wsprintf(fn, "plugins\\newstory\\%s", ffd.cFileName); - - char *szSignature = "newstory export template"; - char line[1024]; - FILE *f = fopen(fn, "r"); - fgets(line, 1024, f); - if (*line) line[lstrlen(line)-1] = 0; - if (!lstrcmp(line, szSignature)) + wchar_t *fn = (wchar_t*)mir_alloc(MAX_PATH); + wsprintf(fn, L"plugins\\newstory\\%s", ffd.cFileName); + + wchar_t *szSignature = L"newstory export template"; + wchar_t line[1024]; + FILE *f = _wfopen(fn, L"r"); + fgetws(line, 1024, f); + if (*line) line[mir_wstrlen(line)-1] = 0; + if (!mir_wstrcmp(line, szSignature)) { - fgets(line, 1024, f); - if (*line) line[lstrlen(line)-1] = 0; + fgetws(line, 1024, f); + if (*line) line[mir_wstrlen(line)-1] = 0; - char *title = strdup(Translate(line)); + wchar_t *title = mir_wstrdup(TranslateW(line)); - fgets(line, 1024, f); - if (*line) line[lstrlen(line)-1] = 0; - char *ext = line; + fgetws(line, 1024, f); + if (*line) line[mir_wstrlen(line)-1] = 0; + wchar_t *ext = line; // (*.<ext>)\0*.<ext>\0 - int newFilterSize = filterSize + lstrlen(title) + 2*lstrlen(ext) + 9; - char *newFilter = (char *)calloc(newFilterSize+1, 1); + int newFilterSize = filterSize + mir_wstrlen(title) + 2*mir_wstrlen(ext) + 9; + wchar_t *newFilter = (wchar_t *)mir_calloc(newFilterSize+1); if (filterSize) memcpy(newFilter, filter, filterSize); - char buf[1024]; - wsprintf(buf, "%s (*.%s)%c*.%s%c", title, ext, '\0', ext, '\0'); + wchar_t buf[1024]; + wsprintf(buf, L"%s (*.%s)%c*.%s%c", title, ext, '\0', ext, '\0'); memcpy(newFilter+filterSize, buf, newFilterSize-filterSize); - free(filter); + mir_free(filter); filter = newFilter; filterSize = newFilterSize; templates[nTemplates++] = fn; - free(title); + mir_free(title); } else { - free(fn); + mir_free(fn); } fclose(f); @@ -100,12 +99,12 @@ bool ExportHistoryDialog(HANDLE hContact, HWND hwndHistory) } - char filename[MAX_PATH] = {0}; + wchar_t filename[MAX_PATH] = {0}; OPENFILENAME ofn = {0}; ofn.lStructSize = sizeof(ofn); ofn.hwndOwner = hwndHistory; - ofn.hInstance = hInst; + ofn.hInstance = g_plugin.getInst(); ofn.lpstrFilter = filter; ofn.lpstrCustomFilter = 0; ofn.nMaxCustFilter = 0; @@ -115,7 +114,7 @@ bool ExportHistoryDialog(HANDLE hContact, HWND hwndHistory) ofn.lpstrFileTitle = 0; ofn.nMaxFileTitle = 0; ofn.lpstrInitialDir = 0; - ofn.lpstrTitle = Translate("Export History..."); + ofn.lpstrTitle = TranslateT("Export History..."); ofn.Flags = OFN_ENABLESIZING|OFN_LONGNAMES|OFN_NOCHANGEDIR|OFN_NOREADONLYRETURN|OFN_OVERWRITEPROMPT|OFN_PATHMUSTEXIST; ofn.lpstrDefExt = 0; ofn.lCustData = 0; @@ -131,11 +130,11 @@ bool ExportHistoryDialog(HANDLE hContact, HWND hwndHistory) for (int i = 0; i < 100; i++) if (templates[i]) - free(templates[i]); + mir_free(templates[i]); return false; -} -*/ +}*/ + ///////////////////////////////////////////////////////////////////////////////////////// // Main history dialog @@ -201,6 +200,21 @@ void LayoutFilterBar(HDWP hDwp, int x, int y, int w, InfoBarEvents *ib) x + 32 + WND_SPACING, y + (16 + WND_SPACING) * 2, w - WND_SPACING - 32, 16, SWP_NOZORDER); } +const char* pSettings[] = +{ + LPGEN("FirstName"), + LPGEN("LastName"), + LPGEN("e-mail"), + LPGEN("Nick"), + LPGEN("Age"), + LPGEN("Gender"), + LPGEN("City"), + LPGEN("State"), + LPGEN("Phone"), + LPGEN("Homepage"), + LPGEN("About") +}; + class CHistoryDlg : public CDlgBase { HMENU m_hMenu; @@ -617,8 +631,110 @@ public: void onClick_Export(CCtrlButton *) { - // ExportHistoryDialog(m_hContact, m_hwnd); - // DialogBox(hInst, MAKEINTRESOURCE(IDD_EXPORT), m_hwnd, ExportWndProc); + //get folder + CMStringW tszRoot; + MCONTACT hContact = m_hContact; + if (db_mc_isMeta(m_hContact)) + hContact = db_event_getContact(m_hContact); + char* proto = Proto_GetBaseAccountName(hContact); + ptrW id(Contact_GetInfo(CNF_UNIQUEID, hContact, proto)); + tszRoot.AppendFormat(VARSW(L"%miranda_userdata%\\NewStoryExport\\%s.json"), id); + + //create file + if (PathFileExistsW(tszRoot)) + DeleteFile(tszRoot); + HANDLE hFile = CreateFile(tszRoot, GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); + if (hFile == INVALID_HANDLE_VALUE) { + // this might be because the path isent created + // so we will try to create it + if (!CreatePathToFileW(tszRoot)) + hFile = CreateFile(tszRoot, GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); + } + + //export contact info + JSONNode pRoot, pInfo, pHist(JSON_ARRAY); + pInfo.set_name("info"); + pInfo.push_back(JSONNode("proto", proto)); + + if (id != NULL) + pInfo.push_back(JSONNode("uid", T2Utf(id).get())); + + for (auto& it : pSettings) { + wchar_t *szValue = db_get_wsa(hContact, proto, it); + if (szValue) + pInfo.push_back(JSONNode(it, T2Utf(szValue).get())); + mir_free(szValue); + } + + pRoot.push_back(pInfo); + + pHist.set_name("history"); + pRoot.push_back(pHist); + + std::string output = pRoot.write_formatted(); + DWORD dwBytesWritten; + WriteFile(hFile, output.c_str(), (int)output.size(), &dwBytesWritten, nullptr); + + SetFilePointer(hFile, -3, nullptr, FILE_CURRENT); + + //export events + MEVENT hDbEvent = db_event_first(hContact); + bool bAppendOnly = false; + while (hDbEvent != NULL) { + DBEVENTINFO dbei = {}; + int nSize = db_event_getBlobSize(hDbEvent); + if (nSize > 0) { + dbei.cbBlob = nSize; + dbei.pBlob = (PBYTE)mir_alloc(dbei.cbBlob + 2); + dbei.pBlob[dbei.cbBlob] = 0; + dbei.pBlob[dbei.cbBlob + 1] = 0; + // Double null terminate, this should prevent most errors + // where the blob received has an invalid format + } + + if (!db_event_get(hDbEvent, &dbei)) { + if (bAppendOnly) { + SetFilePointer(hFile, -3, nullptr, FILE_END); + WriteFile(hFile, ",", 1, &dwBytesWritten, nullptr); + } + + pRoot.push_back(JSONNode("type", dbei.eventType)); + + if (mir_strcmp(dbei.szModule, proto)) + pRoot.push_back(JSONNode("module", dbei.szModule)); + + pRoot.push_back(JSONNode("timestamp", dbei.timestamp)); + + wchar_t szTemp[500]; + TimeZone_PrintTimeStamp(UTC_TIME_HANDLE, dbei.timestamp, L"I", szTemp, _countof(szTemp), 0); + pRoot.push_back(JSONNode("isotime", T2Utf(szTemp).get())); + + std::string flags; + if (dbei.flags & DBEF_SENT) + flags += "m"; + if (dbei.flags & DBEF_READ) + flags += "r"; + pRoot.push_back(JSONNode("flags", flags)); + + ptrW msg(DbEvent_GetTextW(&dbei, CP_ACP)); + if (msg) + pRoot.push_back(JSONNode("body", T2Utf(msg).get())); + + output = pRoot.write_formatted(); + output += "\n]}"; + + WriteFile(hFile, output.c_str(), (int)output.size(), &dwBytesWritten, nullptr); + if (dbei.pBlob) + mir_free(dbei.pBlob); + } + + bAppendOnly = true; + hDbEvent = db_event_next(hContact, hDbEvent); + } + + // Close the file + CloseHandle(hFile); + MessageBox(m_hwnd, TranslateT("Complete"), TranslateT("History export"), MB_OK | MB_ICONINFORMATION); } void onClick_Filter(CCtrlButton *) diff --git a/plugins/NewStory/src/stdafx.h b/plugins/NewStory/src/stdafx.h index 6646640da4..8316520318 100644 --- a/plugins/NewStory/src/stdafx.h +++ b/plugins/NewStory/src/stdafx.h @@ -25,6 +25,7 @@ Boston, MA 02111-1307, USA. #include <tchar.h> #include <commctrl.h> #include <time.h> +#include <Shlwapi.h> //Miranda headers #include "newpluginapi.h" @@ -48,6 +49,9 @@ Boston, MA 02111-1307, USA. #include "m_text.h" #include "m_contacts.h" #include "m_srmm_int.h" +#include <m_json.h> +#include <m_metacontacts.h> +#include <m_timezones.h> #include "m_smileyadd.h" #ifndef MTEXT_NOHELPERS diff --git a/plugins/NewStory/src/utils.cpp b/plugins/NewStory/src/utils.cpp index 41ae656c75..a413a75e8c 100644 --- a/plugins/NewStory/src/utils.cpp +++ b/plugins/NewStory/src/utils.cpp @@ -30,173 +30,3 @@ void CopyText(HWND hwnd, const wchar_t *text) CloseClipboard(); // GlobalFree(hMem); } - -/*void ExportHistory(HANDLE hContact, char *fnTemplate, char *fn, HWND hwndHistory) -{ -/* - char *szHeader = "---header---"; - char *szMsgIn = "---msgin---"; - char *szMsgOut = "---msgout---"; - char *szFile = "---file---"; - char *szURL = "---url---"; - char *szStatus = "---status---"; - char *szOther = "---other---"; - char *szFooter = "---footer---"; - char *szEnd = "---end---"; - - char *tHeader = 0; - char *tMsgIn = 0; - char *tMsgOut = 0; - char *tFile = 0; - char *tURL = 0; - char *tStatus = 0; - char *tOther = 0; - char *tFooter = 0; - char *tEnd = 0; - - char **tActive = 0; - int size; - - char line[1024]; - - FILE *f; - - f = fopen(fnTemplate, "rt"); - while (!feof(f)) - { - fgets(line, 1024, f); - int linesize = lstrlen(line)-1; - if (*line) line[linesize] = 0; - - if (!lstrcmp(line, szHeader)) - { - tActive = &tHeader; - size = 0; - } else - if (!lstrcmp(line, szMsgIn)) - { - tActive = &tMsgIn; - size = 0; - } else - if (!lstrcmp(line, szMsgOut)) - { - tActive = &tMsgOut; - size = 0; - } else - if (!lstrcmp(line, szFile)) - { - tActive = &tFile; - size = 0; - } else - if (!lstrcmp(line, szURL)) - { - tActive = &tURL; - size = 0; - } else - if (!lstrcmp(line, szStatus)) - { - tActive = &tStatus; - size = 0; - } else - if (!lstrcmp(line, szOther)) - { - tActive = &tOther; - size = 0; - } else - if (!lstrcmp(line, szFooter)) - { - tActive = &tFooter; - size = 0; - } else - if (!lstrcmp(line, szEnd)) - { - break; - } else - if (tActive) - { - line[linesize+0] = '\n'; - line[linesize+1] = '\0'; - *tActive = appendString(*tActive, line); - } - } - fclose(f); - - f = fopen(fn, "wt"); - if (!f) - { - MessageBox(hwndHistory, Translate("Can't open optput file."), Translate("Newstory Export"), MB_ICONSTOP|MB_OK); - free(tHeader); - free(tMsgIn); - free(tMsgOut); - free(tFile); - free(tURL); - free(tStatus); - free(tOther); - free(tFooter); - free(tEnd); - return; - } - - char *s; - - VFArgs args; - args.hContact = hContact; - args.event = 0; - - s = TplFormatStringEx(TPL_TITLE, tHeader, &args); - fputs(s,f); - free(s); - - int eventCount = SendMessage(hwndHistory, UM_GETEVENTCOUNT, 0, 0); - for (int i = 0; i < eventCount; i++) - { - if (!SendMessage(hwndHistory, UM_SELECTED, i, 0)) - continue; - - args.event = (DBEVENTINFO *)SendMessage(hwndHistory, UM_GETEVENT, i, TRUE); - switch (args.event->eventType) - { - case EVENTTYPE_MESSAGE: - if (args.event->flags & DBEF_SENT) - { - s = TplFormatStringEx(TPL_MESSAGE, tMsgOut?tMsgOut:tOther, &args); - } else - { - s = TplFormatStringEx(TPL_MESSAGE, tMsgIn?tMsgOut:tOther, &args); - } - break; - - case EVENTTYPE_FILE: - s = TplFormatStringEx(TPL_FILE, tFile?tFile:tOther, &args); - break; - - case EVENTTYPE_STATUSCHANGE: - s = TplFormatStringEx(TPL_SIGN, tStatus?tStatus:tOther, &args); - break; - - default: - s = TplFormatStringEx(TPL_OTHER, tOther, &args); - break; - } - - fputs(s,f); - free(s); - } - - s = TplFormatStringEx(TPL_TITLE, tHeader, &args); - fputs(s,f); - free(s); - - fclose(f); - - free(tHeader); - free(tMsgIn); - free(tMsgOut); - free(tFile); - free(tURL); - free(tStatus); - free(tOther); - free(tFooter); - free(tEnd); - -}*/ diff --git a/plugins/NewStory/src/utils.h b/plugins/NewStory/src/utils.h index 47fd360d6f..3b558bda94 100644 --- a/plugins/NewStory/src/utils.h +++ b/plugins/NewStory/src/utils.h @@ -2,4 +2,3 @@ DWORD toggleBit(DWORD dw, DWORD bit); bool CheckFilter(wchar_t *buf, wchar_t *filter); void CopyText(HWND hwnd, const wchar_t *text); -void ExportHistory(HANDLE hContact, char *fnTemplate, char *fn, HWND hwndList); -- cgit v1.2.3