270 lines
5.6 KiB
C++
270 lines
5.6 KiB
C++
|
/*
|
||
|
Copyright 2010-2011 Grégory Soutadé
|
||
|
|
||
|
This file is part of KissCount.
|
||
|
|
||
|
KissCount is free software: you can redistribute it and/or modify
|
||
|
it under the terms of the GNU General Public License as published by
|
||
|
the Free Software Foundation, either version 3 of the License, or
|
||
|
(at your option) any later version.
|
||
|
|
||
|
KissCount is distributed in the hope that it will be useful,
|
||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
GNU General Public License for more details.
|
||
|
|
||
|
You should have received a copy of the GNU General Public License
|
||
|
along with KissCount. If not, see <http://www.gnu.org/licenses/>.
|
||
|
*/
|
||
|
|
||
|
#include "ImportEngine.h"
|
||
|
|
||
|
ImportEngine::ImportEngine()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
ImportEngine::~ImportEngine()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
bool ImportEngine::HandleFile(const wxString& path, User* user, Database* db, KissCount* kiss)
|
||
|
{
|
||
|
_path = path;
|
||
|
_user = user;
|
||
|
_db = db;
|
||
|
_kiss = kiss;
|
||
|
|
||
|
_accounts.clear();
|
||
|
_unresolvedAccounts.clear();
|
||
|
_operations.clear();
|
||
|
_descriptions.clear();
|
||
|
|
||
|
return path.EndsWith(_shortExt) || path.EndsWith(_shortExt.Upper());
|
||
|
}
|
||
|
|
||
|
wxString ImportEngine::GetFileExt()
|
||
|
{
|
||
|
return wxGetTranslation(_longExt);
|
||
|
}
|
||
|
|
||
|
std::vector<wxString> ExplodeString(wxString& s)
|
||
|
{
|
||
|
wxString tmp = s, cur = wxT("");
|
||
|
std::vector<wxString> res;
|
||
|
|
||
|
while (tmp.Len())
|
||
|
{
|
||
|
if (tmp.StartsWith(wxT(" ")) ||
|
||
|
tmp.StartsWith(wxT("\t")) ||
|
||
|
tmp.StartsWith(wxT("\n")) ||
|
||
|
tmp.StartsWith(wxT("\r")))
|
||
|
{
|
||
|
if (cur.Len())
|
||
|
{
|
||
|
res.push_back(cur);
|
||
|
cur = wxT("");
|
||
|
}
|
||
|
tmp = tmp.Mid(1);
|
||
|
continue;
|
||
|
}
|
||
|
cur += tmp.SubString(0, 0);
|
||
|
tmp = tmp.Mid(1);
|
||
|
}
|
||
|
|
||
|
if (cur.Len())
|
||
|
res.push_back(cur);
|
||
|
|
||
|
return res;
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
Remove :
|
||
|
- head spaces
|
||
|
- tail spaces
|
||
|
- every word starting by a number
|
||
|
*/
|
||
|
wxString ImportEngine::RemoveUnused(wxString& s)
|
||
|
{
|
||
|
wxString tmp = s, tmp2;
|
||
|
wxString res;
|
||
|
|
||
|
tmp = tmp.Trim(true);
|
||
|
tmp = tmp.Trim(false);
|
||
|
|
||
|
while (tmp.Len())
|
||
|
{
|
||
|
tmp2 = tmp.SubString(0, 0);
|
||
|
if (tmp2.IsNumber())
|
||
|
{
|
||
|
do
|
||
|
{
|
||
|
tmp = tmp.Mid(1);
|
||
|
tmp2 = tmp.SubString(0, 0);
|
||
|
} while (tmp.Len() && tmp2 != wxT(" ") &&
|
||
|
tmp2 != wxT("\t") &&
|
||
|
tmp2 != wxT("\r") &&
|
||
|
tmp2 != wxT("\n"));
|
||
|
}
|
||
|
res += tmp2;
|
||
|
tmp = tmp.Mid(1);
|
||
|
}
|
||
|
|
||
|
return res;
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
Find a pattern between the two strings:
|
||
|
%mX : lower string of orig[X]
|
||
|
%MX : upper string of orig[X]
|
||
|
%fX : First case in upper case of orig[X]
|
||
|
%wX : word orig[X]
|
||
|
other : constants
|
||
|
*/
|
||
|
wxString ImportEngine::FindPattern(wxString& orig, wxString& dest)
|
||
|
{
|
||
|
wxString pattern, cur_pat;
|
||
|
int i, a;
|
||
|
std::vector<wxString> tok1 = ExplodeString(orig);
|
||
|
std::vector<wxString> tok2 = ExplodeString(dest);
|
||
|
int size1 = tok1.size(), size2 = tok2.size();
|
||
|
|
||
|
for(i=0; i<size2; i++)
|
||
|
{
|
||
|
cur_pat = wxT("");
|
||
|
|
||
|
for (a=0; a<size1; a++)
|
||
|
{
|
||
|
if (tok2[i] == tok1[a])
|
||
|
{
|
||
|
cur_pat = wxT("%w");
|
||
|
break;
|
||
|
}
|
||
|
else if (tok2[i] == tok1[a].Lower())
|
||
|
{
|
||
|
cur_pat = wxT("%m");
|
||
|
break;
|
||
|
}
|
||
|
else if (tok2[i] == tok1[a].Upper())
|
||
|
{
|
||
|
cur_pat = wxT("%m");
|
||
|
break;
|
||
|
}
|
||
|
else if (tok2[i].Lower() == tok1[a].Lower() && tok2[i][0] == tok1[a][0])
|
||
|
{
|
||
|
cur_pat = wxT("%f");
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (cur_pat.Len())
|
||
|
pattern += cur_pat + wxString::Format(wxT("%d"), a);
|
||
|
else
|
||
|
pattern += tok2[i];
|
||
|
|
||
|
pattern += wxT(" ");
|
||
|
}
|
||
|
|
||
|
pattern = pattern.Trim();
|
||
|
|
||
|
return pattern;
|
||
|
}
|
||
|
|
||
|
void ImportEngine::ApplyPattern(ImportPattern& pattern, Operation& op)
|
||
|
{
|
||
|
std::vector<wxString> tok1 = ExplodeString(pattern.filter);
|
||
|
std::vector<wxString> tok2 = ExplodeString(op.description);
|
||
|
int size1 = tok1.size(), i;
|
||
|
long pos;
|
||
|
|
||
|
op.description = wxT("");
|
||
|
|
||
|
for(i=0; i<size1; i++)
|
||
|
{
|
||
|
pos = -1;
|
||
|
if (tok1[i].StartsWith(wxT("%")))
|
||
|
{
|
||
|
tok1[i].Mid(3).ToLong(&pos);
|
||
|
}
|
||
|
|
||
|
if (pos != -1)
|
||
|
{
|
||
|
if (tok1[i].SubString(1, 1) == wxT("w"))
|
||
|
op.description += tok2[pos];
|
||
|
else if (tok1[i].SubString(1, 1) == wxT("m"))
|
||
|
op.description += tok2[pos].Lower();
|
||
|
else if (tok1[i].SubString(1, 1) == wxT("M"))
|
||
|
op.description += tok2[pos].Upper();
|
||
|
else if (tok1[i].SubString(1, 1) == wxT("f"))
|
||
|
op.description += tok2[pos].SubString(0, 0).Upper() + tok2[pos].Mid(1).Lower();
|
||
|
}
|
||
|
else
|
||
|
op.description += tok1[i];
|
||
|
op.description += wxT(" ");
|
||
|
}
|
||
|
|
||
|
op.description.Trim();
|
||
|
op.account = pattern.account;
|
||
|
op.category = pattern.category;
|
||
|
}
|
||
|
|
||
|
int ImportEngine::UpdatePattern(int pos)
|
||
|
{
|
||
|
wxString key1, key2;
|
||
|
ImportPattern pattern;
|
||
|
Operation op;
|
||
|
int i, nbOpUpdated = 0;
|
||
|
|
||
|
if (!_user) return 0;
|
||
|
|
||
|
nbOpUpdated = 1;
|
||
|
|
||
|
op = _operations[pos];
|
||
|
|
||
|
key1 = RemoveUnused(_descriptions[op.id]);
|
||
|
|
||
|
pattern.filter = FindPattern(_descriptions[op.id], op.description);
|
||
|
pattern.account = op.account;
|
||
|
pattern.category = op.category;
|
||
|
|
||
|
_user->_importPatterns[key1] = pattern;
|
||
|
|
||
|
for(i=pos+1; i<(int)_operations.size(); i++)
|
||
|
{
|
||
|
key2 = RemoveUnused(_descriptions[_operations[i].id]);
|
||
|
|
||
|
if (key1 == key2)
|
||
|
{
|
||
|
ApplyPattern(_user->_importPatterns[key2], _operations[i]);
|
||
|
nbOpUpdated++;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return nbOpUpdated;
|
||
|
}
|
||
|
|
||
|
void ImportEngine::MatchPattern(wxString& originalKey, Operation& op)
|
||
|
{
|
||
|
wxString key1;
|
||
|
ImportPattern pattern;
|
||
|
|
||
|
if (!_user) return;
|
||
|
|
||
|
key1 = RemoveUnused(originalKey);
|
||
|
|
||
|
if (!_user->_importPatterns.count(key1))
|
||
|
{
|
||
|
pattern.filter = FindPattern(originalKey, op.description);
|
||
|
pattern.account = op.account;
|
||
|
pattern.category = op.category;
|
||
|
|
||
|
_user->_importPatterns[key1] = pattern;
|
||
|
|
||
|
// std::cout << "New pattern " << key1.mb_str() << "\t" << pattern.filter.mb_str() << std::endl;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
op.description = _descriptions[op.id];
|
||
|
ApplyPattern(_user->_importPatterns[key1], op);
|
||
|
}
|
||
|
}
|