summaryrefslogtreecommitdiff
path: root/protocols/Tox/src/tox_profile.cpp
diff options
context:
space:
mode:
authorAlexander Lantsev <aunsane@gmail.com>2014-10-04 01:14:11 +0000
committerAlexander Lantsev <aunsane@gmail.com>2014-10-04 01:14:11 +0000
commit928158d25b533037df6ba4a1b1daedeefb05bbbb (patch)
treeff6d48fffb7182bda42cbd1d057496e78bed350d /protocols/Tox/src/tox_profile.cpp
parent334c3a9d18f177f1b6c71d754219f56057d315c0 (diff)
Tox:
- tox id is key again - reworked tox profile logic git-svn-id: http://svn.miranda-ng.org/main/trunk@10679 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'protocols/Tox/src/tox_profile.cpp')
-rw-r--r--protocols/Tox/src/tox_profile.cpp85
1 files changed, 30 insertions, 55 deletions
diff --git a/protocols/Tox/src/tox_profile.cpp b/protocols/Tox/src/tox_profile.cpp
index 88248f7c27..8131164053 100644
--- a/protocols/Tox/src/tox_profile.cpp
+++ b/protocols/Tox/src/tox_profile.cpp
@@ -2,9 +2,14 @@
std::tstring CToxProto::GetToxProfilePath()
{
+ return GetToxProfilePath(m_tszUserName);
+}
+
+std::tstring CToxProto::GetToxProfilePath(const TCHAR *accountName)
+{
std::tstring profilePath;
TCHAR defaultPath[MAX_PATH];
- mir_sntprintf(defaultPath, MAX_PATH, _T("%s\\%s.tox"), VARST(_T("%miranda_userdata%")), m_tszUserName);
+ mir_sntprintf(defaultPath, MAX_PATH, _T("%s\\%s.tox"), VARST(_T("%miranda_userdata%")), accountName);
profilePath = defaultPath;
return profilePath;
@@ -14,32 +19,22 @@ void CToxProto::LoadToxProfile()
{
if (tox == NULL)
{
+ debugLogA("CToxProto::SaveToxProfile: tox core was not initialized");
return;
}
- std::tstring toxProfilePath = GetToxProfilePath();
- FILE *hFile = _wfopen(toxProfilePath.c_str(), _T("rb"));
- if (!hFile)
- {
- debugLogA("CToxProto::LoadToxData: could not open tox profile");
- return;
- }
-
- fseek(hFile, 0, SEEK_END);
- uint32_t size = ftell(hFile);
- rewind(hFile);
+ DWORD size = GetFileSize(hProfile, NULL);
if (size == 0)
{
debugLogA("CToxProto::LoadToxData: tox profile is empty");
- fclose(hFile);
return;
}
+ DWORD read = 0;
uint8_t *data = (uint8_t*)mir_alloc(size);
- if (fread(data, sizeof(uint8_t), size, hFile) != size)
+ if (!ReadFile(hProfile, data, size, &read, NULL) || size != read)
{
debugLogA("CToxProto::LoadToxData: could not read tox profile");
- fclose(hFile);
mir_free(data);
return;
}
@@ -63,61 +58,53 @@ void CToxProto::LoadToxProfile()
}
mir_free(data);
- fclose(hFile);
}
void CToxProto::SaveToxProfile()
{
if (tox == NULL)
{
+ debugLogA("CToxProto::SaveToxProfile: tox core was not initialized");
return;
}
- std::tstring toxProfilePath = GetToxProfilePath();
- FILE *hFile = _wfopen(toxProfilePath.c_str(), _T("wb"));
- if (!hFile)
- {
- debugLogA("CToxProto::LoadToxData: could not open tox profile");
- return;
- }
-
- uint32_t size;
ptrT password(getTStringA("Password"));
- if (password && _tcslen(password))
- size = tox_encrypted_size(tox);
- else
- size = tox_size(tox);
+ bool needToEncrypt = password && _tcslen(password);
+
+ DWORD size = needToEncrypt
+ ? size = tox_encrypted_size(tox)
+ : size = tox_size(tox);
+
uint8_t *data = (uint8_t*)mir_alloc(size);
- if (password && _tcslen(password))
+ if (needToEncrypt)
{
- char *password_utf8 = mir_utf8encodeW(password);
- if (tox_encrypted_save(tox, data, (uint8_t*)password_utf8, strlen(password_utf8)) == TOX_ERROR)
+ char *passwordInUtf8 = mir_utf8encodeW(password);
+ if (tox_encrypted_save(tox, data, (uint8_t*)passwordInUtf8, strlen(passwordInUtf8)) == TOX_ERROR)
{
debugLogA("CToxProto::LoadToxData: could not encrypt tox profile");
- mir_free(password_utf8);
+ mir_free(passwordInUtf8);
mir_free(data);
- fclose(hFile);
return;
}
- mir_free(password_utf8);
+ mir_free(passwordInUtf8);
}
else
{
tox_save(tox, data);
}
- if (fwrite(data, sizeof(uint8_t), size, hFile) != size)
+ DWORD written = 0;
+ if (!WriteFile(hProfile, data, size, &written, NULL) || size != written)
{
debugLogA("CToxProto::LoadToxData: could not write tox profile");
}
mir_free(data);
- fclose(hFile);
}
-INT_PTR CToxProto::ToxProfileManagerProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
+INT_PTR CToxProto::ToxProfileImportProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
- CToxProto *proto = (CToxProto*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
+ TCHAR *accountName = (TCHAR*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
TCHAR *profilePath = (TCHAR*)GetWindowLongPtr(hwnd, DWLP_USER);
switch (uMsg)
@@ -125,7 +112,7 @@ INT_PTR CToxProto::ToxProfileManagerProc(HWND hwnd, UINT uMsg, WPARAM wParam, LP
case WM_INITDIALOG:
TranslateDialogDefault(hwnd);
{
- proto = (CToxProto*)lParam;
+ accountName = (TCHAR*)lParam;
SetWindowLongPtr(hwnd, GWLP_USERDATA, lParam);
profilePath = (TCHAR*)mir_calloc(sizeof(TCHAR)*MAX_PATH);
@@ -133,17 +120,9 @@ INT_PTR CToxProto::ToxProfileManagerProc(HWND hwnd, UINT uMsg, WPARAM wParam, LP
}
return TRUE;
- case WM_CLOSE:
- {
- std::tstring defaultProfilePath = proto->GetToxProfilePath();
- fclose(_wfopen(defaultProfilePath.c_str(), _T("w")));
- EndDialog(hwnd, 0);
- }
- break;
-
case WM_DESTROY:
mir_free(profilePath);
- break;
+ return TRUE;
case WM_COMMAND:
switch (LOWORD(wParam))
@@ -180,7 +159,7 @@ INT_PTR CToxProto::ToxProfileManagerProc(HWND hwnd, UINT uMsg, WPARAM wParam, LP
case IDOK:
{
- std::tstring defaultProfilePath = proto->GetToxProfilePath();
+ std::tstring defaultProfilePath = GetToxProfilePath(accountName);
if (profilePath && _tcslen(profilePath))
{
if (_tcsicmp(profilePath, defaultProfilePath.c_str()) != 0)
@@ -197,11 +176,7 @@ INT_PTR CToxProto::ToxProfileManagerProc(HWND hwnd, UINT uMsg, WPARAM wParam, LP
break;
case IDCANCEL:
- {
- std::tstring defaultProfilePath = proto->GetToxProfilePath();
- fclose(_wfopen(defaultProfilePath.c_str(), _T("w")));
- EndDialog(hwnd, 0);
- }
+ EndDialog(hwnd, 0);
break;
}
break;