summaryrefslogtreecommitdiff
path: root/plugins/Import/src
diff options
context:
space:
mode:
authorGeorge Hazan <ghazan@miranda.im>2019-04-25 12:54:22 +0300
committerGeorge Hazan <ghazan@miranda.im>2019-04-25 12:54:22 +0300
commit462deb27452322b8540f6d31230e766760b817bb (patch)
tree3b65a39d7089730a444205cb1d0f9ccad6db302c /plugins/Import/src
parent644e81723335e445a7e53c6d858867ba416b31cd (diff)
first version of pattern import that could be compiled
Diffstat (limited to 'plugins/Import/src')
-rw-r--r--plugins/Import/src/import.cpp14
-rw-r--r--plugins/Import/src/main.cpp17
-rw-r--r--plugins/Import/src/mcontacts.cpp17
-rw-r--r--plugins/Import/src/miranda.cpp4
-rw-r--r--plugins/Import/src/patterns.cpp129
-rw-r--r--plugins/Import/src/resource.h6
-rw-r--r--plugins/Import/src/stdafx.h32
-rw-r--r--plugins/Import/src/ui.cpp143
-rw-r--r--plugins/Import/src/wizard.cpp4
9 files changed, 268 insertions, 98 deletions
diff --git a/plugins/Import/src/import.cpp b/plugins/Import/src/import.cpp
index 81d7a9d1f8..67c0d80fe2 100644
--- a/plugins/Import/src/import.cpp
+++ b/plugins/Import/src/import.cpp
@@ -1020,13 +1020,17 @@ void MirandaImport()
return;
}
- DATABASELINK *dblink = FindDatabasePlugin(importFile);
- if (dblink == nullptr) {
- AddMessage(LPGENW("There's no database driver to open the input file, exiting."));
- return;
+ DATABASELINK *dblink;
+ if (g_pActivePattern == nullptr) {
+ dblink = FindDatabasePlugin(g_wszImportFile);
+ if (dblink == nullptr) {
+ AddMessage(LPGENW("There's no database driver to open the input file, exiting."));
+ return;
+ }
}
+ else dblink = &g_patternDbLink;
- if ((srcDb = dblink->Load(importFile, TRUE)) == nullptr) {
+ if ((srcDb = dblink->Load(g_wszImportFile, TRUE)) == nullptr) {
AddMessage(LPGENW("Error loading source file, exiting."));
return;
}
diff --git a/plugins/Import/src/main.cpp b/plugins/Import/src/main.cpp
index 0477aae3f4..812c5aaa5d 100644
--- a/plugins/Import/src/main.cpp
+++ b/plugins/Import/src/main.cpp
@@ -24,6 +24,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
int g_iImportOptions;
MCONTACT g_hImportContact;
+CImportPattern *g_pActivePattern;
bool g_bServiceMode = false, g_bSendQuit = false;
HWND g_hwndWizard, g_hwndAccMerge;
@@ -101,7 +102,7 @@ static INT_PTR ServiceMode(WPARAM wParam, LPARAM)
ptrW wszFullName(Utils_ReplaceVarsW(L"%miranda_userdata%\\%miranda_profilename%.dat.bak"));
if (!_waccess(wszFullName, 0)) {
g_iImportOptions = IOPT_ADDUNKNOWN + IOPT_COMPLETE;
- wcsncpy_s(importFile, MAX_PATH, wszFullName, _TRUNCATE);
+ wcsncpy_s(g_wszImportFile, MAX_PATH, wszFullName, _TRUNCATE);
RunWizard(new CProgressPageDlg(), true);
}
return SERVICE_CONTINUE;
@@ -116,20 +117,10 @@ static INT_PTR ServiceMode(WPARAM wParam, LPARAM)
static INT_PTR CustomImport(WPARAM wParam, LPARAM)
{
MImportOptions *opts = (MImportOptions*)wParam;
- wcsncpy_s(importFile, MAX_PATH, opts->pwszFileName, _TRUNCATE);
+ wcsncpy_s(g_wszImportFile, MAX_PATH, opts->pwszFileName, _TRUNCATE);
g_iImportOptions = opts->dwFlags;
g_hImportContact = 0;
- return RunWizard(new CProgressPageDlg(), true);
-}
-
-static INT_PTR ImportContact(WPARAM hContact, LPARAM)
-{
- CContactImportDlg dlg(hContact);
- if (!dlg.DoModal())
- return 0;
-
- g_hImportContact = hContact;
- g_iImportOptions = IOPT_HISTORY + dlg.getFlags();
+ g_pActivePattern = nullptr;
return RunWizard(new CProgressPageDlg(), true);
}
diff --git a/plugins/Import/src/mcontacts.cpp b/plugins/Import/src/mcontacts.cpp
index 9cf8b12401..748881538b 100644
--- a/plugins/Import/src/mcontacts.cpp
+++ b/plugins/Import/src/mcontacts.cpp
@@ -27,11 +27,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#define HEADER_STR "HB"
-static int mc_makeDatabase(const wchar_t*)
-{
- return 1;
-}
-
/////////////////////////////////////////////////////////////////////////////////////////
// CDbxMcontacts database driver, read-only
@@ -126,11 +121,15 @@ public:
if (dwSize == sizeof(MC_MsgHeader32)) {
MC_MsgHeader32 hdr;
r = ReadFile(m_hFile, &hdr, sizeof(hdr), &dwRead, 0);
+ if (!r || dwRead < sizeof(hdr))
+ return;
SetFilePointer(m_hFile, hdr.cbBlob, 0, FILE_CURRENT);
}
else {
MC_MsgHeader64 hdr;
r = ReadFile(m_hFile, &hdr, sizeof(hdr), &dwRead, 0);
+ if (!r || dwRead < sizeof(hdr))
+ return;
SetFilePointer(m_hFile, hdr.cbBlob, 0, FILE_CURRENT);
}
pos += dwSize;
@@ -243,6 +242,14 @@ public:
}
};
+/////////////////////////////////////////////////////////////////////////////////////////
+// database link functions
+
+static int mc_makeDatabase(const wchar_t*)
+{
+ return 1;
+}
+
static int mc_grokHeader(const wchar_t *profile)
{
return CDbxMc().Open(profile);
diff --git a/plugins/Import/src/miranda.cpp b/plugins/Import/src/miranda.cpp
index 39559cdf4a..7864c78e9d 100644
--- a/plugins/Import/src/miranda.cpp
+++ b/plugins/Import/src/miranda.cpp
@@ -24,7 +24,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
time_t dwSinceDate = 0;
-wchar_t importFile[MAX_PATH];
+wchar_t g_wszImportFile[MAX_PATH];
//=======================================================================================
// Profile selection dialog
@@ -95,7 +95,7 @@ void CMirandaPageDlg::OnNext()
MessageBox(m_hwnd, TranslateT("The given file does not exist. Please check that you have entered the name correctly."), TranslateT("Miranda Import"), MB_OK);
return;
}
- mir_wstrcpy(importFile, filename);
+ mir_wstrcpy(g_wszImportFile, filename);
PostMessage(m_hwndParent, WIZM_GOTOPAGE, 0, (LPARAM)new CMirandaOptionsPageDlg());
}
diff --git a/plugins/Import/src/patterns.cpp b/plugins/Import/src/patterns.cpp
index 67da571557..f893ade734 100644
--- a/plugins/Import/src/patterns.cpp
+++ b/plugins/Import/src/patterns.cpp
@@ -22,12 +22,15 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "stdafx.h"
+#include <memory>
+#include <vector>
+
void CMPlugin::LoadPatterns()
{
wchar_t wszPath[MAX_PATH], wszFullPath[MAX_PATH];
GetModuleFileNameW(m_hInst, wszPath, _countof(wszPath));
if (auto *p = wcsrchr(wszPath, '\\'))
- p[1] = 0;
+ *p = 0;
mir_snwprintf(wszFullPath, L"%s\\Import\\*.ini", wszPath);
@@ -54,6 +57,9 @@ void CMPlugin::LoadPattern(const wchar_t *pwszFileName)
pNew->wszName = buf;
pNew->iType = GetPrivateProfileIntW(L"General", L"Type", 1, pwszFileName);
+ if (GetPrivateProfileStringW(L"General", L"DefaultExtension", L"", buf, _countof(buf), pwszFileName))
+ pNew->wszExt = buf;
+
if (GetPrivateProfileStringW(L"General", L"Charset", L"", buf, _countof(buf), pwszFileName)) {
if (!wcsicmp(buf, L"ANSI"))
pNew->iCodePage = GetPrivateProfileIntW(L"General", L"Codepage", CP_ACP, pwszFileName);
@@ -104,3 +110,124 @@ void CMPlugin::LoadPattern(const wchar_t *pwszFileName)
m_patterns.insert(pNew.release());
}
+
+/////////////////////////////////////////////////////////////////////////////////////////
+// pattern-based database driver
+
+class CDbxPattern : public MDatabaseReadonly, public MZeroedObject
+{
+ FILE *m_hFile;
+
+ std::vector<DWORD> m_events;
+ std::vector<DWORD>::iterator m_curr;
+
+public:
+ CDbxPattern()
+ {}
+
+ ~CDbxPattern()
+ {
+ if (m_hFile != INVALID_HANDLE_VALUE)
+ ::CloseHandle(m_hFile);
+ }
+
+ void Load()
+ {
+ wchar_t wszLine[2048];
+ do {
+ m_events.push_back(ftell(m_hFile));
+ }
+ while (fgetws(wszLine, _countof(wszLine), m_hFile));
+
+ fseek(m_hFile, 0, SEEK_SET);
+ }
+
+ int Open(const wchar_t *profile)
+ {
+ m_hFile = _wfopen(profile, L"rb");
+ return (m_hFile == nullptr) ? EGROKPRF_CANTREAD : EGROKPRF_NOERROR;
+ }
+
+ // mcontacts format always store history for one contact only
+ STDMETHODIMP_(LONG) GetContactCount(void) override
+ {
+ return 1;
+ }
+
+ STDMETHODIMP_(LONG) GetEventCount(MCONTACT) override
+ {
+ return (LONG)m_events.size();
+ }
+
+ STDMETHODIMP_(BOOL) GetEvent(MEVENT dwOffset, DBEVENTINFO *dbei) override
+ {
+ fseek(m_hFile, dwOffset, SEEK_SET);
+
+ wchar_t wszLine[2048];
+ fgetws(wszLine, _countof(wszLine), m_hFile);
+ return 1;
+ }
+
+ STDMETHODIMP_(MEVENT) FindFirstEvent(MCONTACT) override
+ {
+ m_curr = m_events.begin();
+ return *m_curr;
+ }
+
+ STDMETHODIMP_(MEVENT) FindNextEvent(MCONTACT, MEVENT) override
+ {
+ if (m_curr == m_events.end())
+ return 0;
+
+ ++m_curr;
+ return *m_curr;
+ }
+
+ STDMETHODIMP_(MEVENT) FindLastEvent(MCONTACT) override
+ {
+ m_curr = m_events.end();
+ return *m_curr;
+ }
+
+ STDMETHODIMP_(MEVENT) FindPrevEvent(MCONTACT, MEVENT) override
+ {
+ if (m_curr == m_events.begin())
+ return 0;
+
+ --m_curr;
+ return *m_curr;
+ }
+};
+
+/////////////////////////////////////////////////////////////////////////////////////////
+// database link functions
+
+static int pattern_makeDatabase(const wchar_t*)
+{
+ return 1;
+}
+
+static int pattern_grokHeader(const wchar_t *profile)
+{
+ return CDbxPattern().Open(profile);
+}
+
+static MDatabaseCommon* pattern_load(const wchar_t *profile, BOOL)
+{
+ std::auto_ptr<CDbxPattern> db(new CDbxPattern());
+ if (db->Open(profile))
+ return nullptr;
+
+ db->Load();
+ return db.release();
+}
+
+DATABASELINK g_patternDbLink =
+{
+ 0,
+ "pattern",
+ L"Pattern-based file driver",
+ pattern_makeDatabase,
+ pattern_grokHeader,
+ pattern_load
+};
diff --git a/plugins/Import/src/resource.h b/plugins/Import/src/resource.h
index c3d12ea54d..a2f6bb7088 100644
--- a/plugins/Import/src/resource.h
+++ b/plugins/Import/src/resource.h
@@ -51,6 +51,8 @@
#define IDC_FT 1040
#define IDC_OPEN_FILE 1042
#define IDC_CHECK_DUPS 1043
+#define IDC_COMBO1 1045
+#define IDC_FILETYPE 1045
#define IDM_COPY 40001
#define IDC_STATIC -1
@@ -58,9 +60,9 @@
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
-#define _APS_NEXT_RESOURCE_VALUE 116
+#define _APS_NEXT_RESOURCE_VALUE 117
#define _APS_NEXT_COMMAND_VALUE 40002
-#define _APS_NEXT_CONTROL_VALUE 1045
+#define _APS_NEXT_CONTROL_VALUE 1046
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif
diff --git a/plugins/Import/src/stdafx.h b/plugins/Import/src/stdafx.h
index 7ca8c6a310..ee80f02412 100644
--- a/plugins/Import/src/stdafx.h
+++ b/plugins/Import/src/stdafx.h
@@ -69,7 +69,7 @@ struct CImportPattern : public MZeroedObject
pcre16_extra *extra;
};
- CMStringW wszName;
+ CMStringW wszName, wszExt;
int iType = 1;
int iCodePage = CP_UTF8;
int iUseHeader, iUsePreMsg, iUseFilename;
@@ -85,6 +85,8 @@ struct CImportPattern : public MZeroedObject
struct CMPlugin : public PLUGIN<CMPlugin>
{
private:
+ friend class CContactImportDlg;
+
void LoadPattern(const wchar_t *pwszFileName);
void LoadPatterns();
@@ -225,35 +227,17 @@ bool IsDuplicateEvent(MCONTACT hContact, DBEVENTINFO dbei);
int CreateGroup(const wchar_t *name, MCONTACT hContact);
extern HWND g_hwndWizard, g_hwndAccMerge;
-extern wchar_t importFile[MAX_PATH];
+extern wchar_t g_wszImportFile[MAX_PATH];
extern time_t dwSinceDate;
extern bool g_bServiceMode, g_bSendQuit;
extern int g_iImportOptions;
extern MCONTACT g_hImportContact;
-void RegisterIcons(void);
+extern CImportPattern *g_pActivePattern;
+extern DATABASELINK g_patternDbLink;
+void RegisterIcons(void);
void RegisterMContacts();
void RegisterJson();
-/////////////////////////////////////////////////////////////////////////////////////////
-
-class CContactImportDlg : public CDlgBase
-{
- MCONTACT m_hContact;
- int m_flags = 0;
-
- CCtrlButton m_btnOpenFile, m_btnOk;
- CCtrlEdit edtFileName;
-
-public:
- CContactImportDlg(MCONTACT hContact);
-
- int getFlags() const { return m_flags; }
-
- bool OnInitDialog() override;
- bool OnApply() override;
-
- void onClick_Ok(CCtrlButton*);
- void onClick_OpenFile(CCtrlButton*);
-};
+INT_PTR ImportContact(WPARAM hContact, LPARAM);
diff --git a/plugins/Import/src/ui.cpp b/plugins/Import/src/ui.cpp
index b5cb46a5ff..18bfe73a10 100644
--- a/plugins/Import/src/ui.cpp
+++ b/plugins/Import/src/ui.cpp
@@ -22,56 +22,109 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "stdafx.h"
-CContactImportDlg::CContactImportDlg(MCONTACT hContact) :
- CDlgBase(g_plugin, IDD_IMPORT_CONTACT),
- m_hContact(hContact),
- edtFileName(this, IDC_FILENAME),
- m_btnOk(this, IDOK),
- m_btnOpenFile(this, IDC_OPEN_FILE)
+class CContactImportDlg : public CDlgBase
{
- m_btnOk.OnClick = Callback(this, &CContactImportDlg::onClick_Ok);
- m_btnOpenFile.OnClick = Callback(this, &CContactImportDlg::onClick_OpenFile);
-}
+ MCONTACT m_hContact;
+ int m_flags = 0;
-bool CContactImportDlg::OnInitDialog()
-{
- CMStringW wszTitle(FORMAT, TranslateT("Import history for %s"), Clist_GetContactDisplayName(m_hContact));
- SetWindowTextW(m_hwnd, wszTitle);
- return true;
-}
+ CCtrlButton m_btnOpenFile, m_btnOk;
+ CCtrlCombo m_cmbFileType;
+ CCtrlEdit edtFileName;
-bool CContactImportDlg::OnApply()
-{
- edtFileName.GetText(importFile, _countof(importFile));
- if (importFile[0] == 0)
- return false;
+public:
+ CContactImportDlg(MCONTACT hContact) :
+ CDlgBase(g_plugin, IDD_IMPORT_CONTACT),
+ m_hContact(hContact),
+ edtFileName(this, IDC_FILENAME),
+ m_cmbFileType(this, IDC_FILETYPE),
+ m_btnOk(this, IDOK),
+ m_btnOpenFile(this, IDC_OPEN_FILE)
+ {
+ m_btnOk.OnClick = Callback(this, &CContactImportDlg::onClick_Ok);
+ m_btnOpenFile.OnClick = Callback(this, &CContactImportDlg::onClick_OpenFile);
+ }
- if (IsDlgButtonChecked(m_hwnd, IDC_CHECK_DUPS))
- m_flags = IOPT_CHECKDUPS;
- return true;
-}
+ int getFlags() const { return m_flags; }
-void CContactImportDlg::onClick_Ok(CCtrlButton*)
-{
- EndModal(1);
-}
+ bool OnInitDialog() override
+ {
+ CMStringW wszTitle(FORMAT, TranslateT("Import history for %s"), Clist_GetContactDisplayName(m_hContact));
+ SetWindowTextW(m_hwnd, wszTitle);
+
+ m_cmbFileType.AddString(TranslateT("Miranda NG database"), -1);
+ m_cmbFileType.AddString(TranslateT("JSON file"), -2);
+
+ int iType = 1;
+ for (auto &it : g_plugin.m_patterns)
+ m_cmbFileType.AddString(it->wszName, iType++);
+
+ return true;
+ }
-void CContactImportDlg::onClick_OpenFile(CCtrlButton*)
+ bool OnApply() override
+ {
+ edtFileName.GetText(g_wszImportFile, _countof(g_wszImportFile));
+ if (g_wszImportFile[0] == 0)
+ return false;
+
+ if (IsDlgButtonChecked(m_hwnd, IDC_CHECK_DUPS))
+ m_flags = IOPT_CHECKDUPS;
+ return true;
+ }
+
+ void onClick_Ok(CCtrlButton*)
+ {
+ EndModal(1);
+ }
+
+ void onClick_OpenFile(CCtrlButton*)
+ {
+ int iCur = m_cmbFileType.GetCurSel();
+ if (iCur == -1)
+ return;
+
+ CMStringW text, cmbText(ptrW(m_cmbFileType.GetText()));
+ switch(int idx = m_cmbFileType.GetItemData(iCur)) {
+ case -1:
+ text.AppendFormat(L"%s (*.dat,*.bak)%c*.dat;*.bak%c", cmbText.c_str(), 0, 0);
+ g_pActivePattern = nullptr;
+ break;
+
+ case -2:
+ text.AppendFormat(L"%s (*.json)%c*.json%c", cmbText.c_str(), 0, 0);
+ g_pActivePattern = nullptr;
+ break;
+
+ default:
+ auto &p = g_plugin.m_patterns[idx-1];
+ text.AppendFormat(L"%s (*.%s)%c*.%s%c", cmbText.c_str(), p.wszExt.c_str(), 0, p.wszExt.c_str(), 0);
+ g_pActivePattern = &p;
+ break;
+ }
+ text.AppendFormat(L"%s (*.*)%c*.*%c%c", TranslateT("All Files"), 0, 0, 0);
+
+ OPENFILENAME ofn = { 0 };
+ ofn.lStructSize = OPENFILENAME_SIZE_VERSION_400;
+ ofn.lpstrFilter = text;
+ ofn.lpstrDefExt = L"dat";
+ ofn.Flags = OFN_FILEMUSTEXIST | OFN_EXPLORER | OFN_NOCHANGEDIR | OFN_DONTADDTORECENT;
+ ofn.lpstrFile = g_wszImportFile;
+ ofn.nMaxFile = _countof(g_wszImportFile);
+ if (!GetOpenFileNameW(&ofn)) {
+ g_wszImportFile[0] = 0;
+ g_pActivePattern = nullptr;
+ }
+ else edtFileName.SetText(g_wszImportFile);
+ }
+};
+
+INT_PTR ImportContact(WPARAM hContact, LPARAM)
{
- CMStringW text;
- text.AppendFormat(L"%s (*.dat,*.bak)%c*.dat;*.bak%c", TranslateT("Miranda NG database"), 0, 0);
- text.AppendFormat(L"%s (*.json)%c*.json%c", TranslateT("JSON file"), 0, 0);
- text.AppendFormat(L"%s (*.*)%c*.*%c%c", TranslateT("All Files"), 0, 0, 0);
-
- OPENFILENAME ofn = { 0 };
- ofn.lStructSize = OPENFILENAME_SIZE_VERSION_400;
- ofn.lpstrFilter = text;
- ofn.lpstrDefExt = L"dat";
- ofn.Flags = OFN_FILEMUSTEXIST | OFN_EXPLORER | OFN_NOCHANGEDIR | OFN_DONTADDTORECENT;
- ofn.lpstrFile = importFile;
- ofn.nMaxFile = _countof(importFile);
- if (!GetOpenFileName(&ofn))
- importFile[0] = 0;
- else
- edtFileName.SetText(importFile);
+ CContactImportDlg dlg(hContact);
+ if (!dlg.DoModal())
+ return 0;
+
+ g_hImportContact = hContact;
+ g_iImportOptions = IOPT_HISTORY + dlg.getFlags();
+ return RunWizard(new CProgressPageDlg(), true);
}
diff --git a/plugins/Import/src/wizard.cpp b/plugins/Import/src/wizard.cpp
index 2767a2ffc2..6efdeeb125 100644
--- a/plugins/Import/src/wizard.cpp
+++ b/plugins/Import/src/wizard.cpp
@@ -27,7 +27,9 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
CIntroPageDlg::CIntroPageDlg() :
CWizardPageDlg(IDD_WIZARDINTRO)
-{}
+{
+ g_pActivePattern = nullptr;
+}
bool CIntroPageDlg::OnInitDialog()
{