summaryrefslogtreecommitdiff
path: root/protocols/Tox/src
diff options
context:
space:
mode:
authorAlexander Lantsev <aunsane@gmail.com>2014-09-29 17:47:24 +0000
committerAlexander Lantsev <aunsane@gmail.com>2014-09-29 17:47:24 +0000
commit6beb2d44b3ab2811417507be4c78c19626c9ee7e (patch)
tree0292dd1424658b2a34850dfaebafe2bdde36bfdc /protocols/Tox/src
parent8f994ff592a5501018ba601d33d59a558a2d9840 (diff)
Tox:
- profile manager is now profile import - tox profile renaming without reloading of tox core - updated tox core git-svn-id: http://svn.miranda-ng.org/main/trunk@10632 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'protocols/Tox/src')
-rw-r--r--protocols/Tox/src/resource.h3
-rw-r--r--protocols/Tox/src/tox_account.cpp26
-rw-r--r--protocols/Tox/src/tox_accounts.cpp9
-rw-r--r--protocols/Tox/src/tox_avatars.cpp2
-rw-r--r--protocols/Tox/src/tox_contacts.cpp2
-rw-r--r--protocols/Tox/src/tox_events.cpp2
-rw-r--r--protocols/Tox/src/tox_options.cpp9
-rw-r--r--protocols/Tox/src/tox_profile.cpp144
-rw-r--r--protocols/Tox/src/tox_proto.cpp9
-rw-r--r--protocols/Tox/src/tox_proto.h9
-rw-r--r--protocols/Tox/src/tox_utils.cpp89
11 files changed, 153 insertions, 151 deletions
diff --git a/protocols/Tox/src/resource.h b/protocols/Tox/src/resource.h
index a59da4ee67..3d68c6c90a 100644
--- a/protocols/Tox/src/resource.h
+++ b/protocols/Tox/src/resource.h
@@ -4,15 +4,14 @@
//
#define IDI_TOX 1000
#define IDD_PROFILE_MANAGER 1001
+#define IDD_PROFILE_IMPORT 1001
#define IDC_TOXID 1002
#define IDD_ACCOUNT_MANAGER 1003
#define IDC_CLIPBOARD 1004
#define IDD_OPTIONS_MAIN 1005
-#define IDC_CREATE_NEW 1006
#define IDC_PROFILE_PATH 1007
#define IDC_SEARCH 1008
#define IDC_PASSWORD 1008
-#define IDC_USE_EXISTING 1009
#define IDC_BROWSE_PROFILE 1010
#define IDC_NAME 1011
#define IDC_GROUP 1012
diff --git a/protocols/Tox/src/tox_account.cpp b/protocols/Tox/src/tox_account.cpp
index 8921a5c437..5cd08f59b7 100644
--- a/protocols/Tox/src/tox_account.cpp
+++ b/protocols/Tox/src/tox_account.cpp
@@ -20,6 +20,21 @@ int CToxProto::OnAccountLoaded(WPARAM, LPARAM)
void CToxProto::InitToxCore()
{
+ std::tstring profilePath = GetToxProfilePath();
+ if (!IsFileExists(profilePath))
+ {
+ return;
+ }
+
+ hProfile = CreateFile(
+ profilePath.c_str(),
+ GENERIC_READ | GENERIC_WRITE,
+ FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
+ NULL,
+ OPEN_EXISTING,
+ FILE_ATTRIBUTE_NORMAL,
+ NULL);
+
Tox_Options options = { 0 };
options.udp_disabled = getByte("DisableUDP", 0);
options.ipv6enabled = !getByte("DisableIPv6", 0);
@@ -59,7 +74,7 @@ void CToxProto::InitToxCore()
tox_callback_avatar_info(tox, OnGotFriendAvatarInfo, this);
tox_callback_avatar_data(tox, OnGotFriendAvatarData, this);
- LoadToxData();
+ LoadToxProfile();
int size = tox_get_self_name_size(tox);
std::vector<uint8_t> username(size);
@@ -81,8 +96,15 @@ void CToxProto::InitToxCore()
void CToxProto::UninitToxCore()
{
- SaveToxData();
+ std::tstring profilePath = GetToxProfilePath();
+ if (!IsFileExists(profilePath))
+ {
+ return;
+ }
+
+ SaveToxProfile();
tox_kill(tox);
+ CloseHandle(hProfile);
}
void CToxProto::DoBootstrap()
diff --git a/protocols/Tox/src/tox_accounts.cpp b/protocols/Tox/src/tox_accounts.cpp
index acab9db206..97b6f9b345 100644
--- a/protocols/Tox/src/tox_accounts.cpp
+++ b/protocols/Tox/src/tox_accounts.cpp
@@ -32,26 +32,23 @@ int CToxProto::OnAccountListChanged(WPARAM wParam, LPARAM lParam)
switch (wParam)
{
case PRAC_ADDED:
- if (this->getStringA("ToxID"))
- UninitToxCore();
DialogBoxParam(
g_hInstance,
- MAKEINTRESOURCE(IDD_PROFILE_MANAGER),
+ MAKEINTRESOURCE(IDD_PROFILE_IMPORT),
account->hwndAccMgrUI,
CToxProto::ToxProfileManagerProc,
(LPARAM)this);
InitToxCore();
+ SaveToxProfile();
break;
case PRAC_CHANGED:
- UninitToxCore();
std::tstring newPath = GetToxProfilePath();
TCHAR oldPath[MAX_PATH];
mir_sntprintf(oldPath, MAX_PATH, _T("%s\\%s.tox"), VARST(_T("%miranda_userdata%")), accountName);
- MoveFileEx(oldPath, newPath.c_str(), MOVEFILE_REPLACE_EXISTING);
+ MoveFileEx(oldPath, newPath.c_str(), MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED);
mir_free(accountName);
accountName = mir_tstrdup(m_tszUserName);
- InitToxCore();
break;
}
}
diff --git a/protocols/Tox/src/tox_avatars.cpp b/protocols/Tox/src/tox_avatars.cpp
index 747ce8f643..e4f6d4afbb 100644
--- a/protocols/Tox/src/tox_avatars.cpp
+++ b/protocols/Tox/src/tox_avatars.cpp
@@ -164,7 +164,7 @@ INT_PTR CToxProto::SetMyAvatar(WPARAM wParam, LPARAM lParam)
}
else
{
- if (tox_set_avatar(tox, TOX_AVATAR_FORMAT_NONE, NULL, 0) == TOX_ERROR)
+ if (tox_unset_avatar(tox) == TOX_ERROR)
{
debugLogA("CToxProto::SetMyAvatar: failed to unset avatar");
return -1;
diff --git a/protocols/Tox/src/tox_contacts.cpp b/protocols/Tox/src/tox_contacts.cpp
index a2f6573713..b23c4341f9 100644
--- a/protocols/Tox/src/tox_contacts.cpp
+++ b/protocols/Tox/src/tox_contacts.cpp
@@ -166,7 +166,7 @@ int CToxProto::OnContactDeleted(MCONTACT hContact, LPARAM lParam)
return 1;
}
- SaveToxData();
+ SaveToxProfile();
return 0;
}
diff --git a/protocols/Tox/src/tox_events.cpp b/protocols/Tox/src/tox_events.cpp
index 8a803d8144..a2a4b83a7e 100644
--- a/protocols/Tox/src/tox_events.cpp
+++ b/protocols/Tox/src/tox_events.cpp
@@ -47,7 +47,7 @@ int CToxProto::OnSettingsChanged(WPARAM hContact, LPARAM lParam)
{
if (tox_set_name(tox, (uint8_t*)dbcws->value.pszVal, (uint16_t)strlen(dbcws->value.pszVal)))
{
- SaveToxData();
+ SaveToxProfile();
}
}
diff --git a/protocols/Tox/src/tox_options.cpp b/protocols/Tox/src/tox_options.cpp
index 0b24416308..934f0b9ba1 100644
--- a/protocols/Tox/src/tox_options.cpp
+++ b/protocols/Tox/src/tox_options.cpp
@@ -18,8 +18,11 @@ INT_PTR CToxProto::MainOptionsProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM l
ptrT pass(proto->getTStringA("Password"));
SetDlgItemText(hwnd, IDC_PASSWORD, pass);
- std::string address = proto->getStringA(NULL, TOX_SETTINGS_ID);
- SetDlgItemTextA(hwnd, IDC_TOXID, address.c_str());
+ ptrA address(proto->getStringA(TOX_SETTINGS_ID));
+ if (address != NULL)
+ {
+ SetDlgItemTextA(hwnd, IDC_TOXID, address);
+ }
ptrT group(proto->getTStringA(TOX_SETTINGS_GROUP));
SetDlgItemText(hwnd, IDC_GROUP, group);
@@ -93,7 +96,7 @@ INT_PTR CToxProto::MainOptionsProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM l
proto->delSetting(NULL, TOX_SETTINGS_GROUP);
}
- proto->SaveToxData();
+ proto->SaveToxProfile();
return TRUE;
}
diff --git a/protocols/Tox/src/tox_profile.cpp b/protocols/Tox/src/tox_profile.cpp
index 13f801ebb9..168e998d8c 100644
--- a/protocols/Tox/src/tox_profile.cpp
+++ b/protocols/Tox/src/tox_profile.cpp
@@ -10,6 +10,101 @@ std::tstring CToxProto::GetToxProfilePath()
return profilePath;
}
+void CToxProto::LoadToxProfile()
+{
+ 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);
+ if (size == 0)
+ {
+ debugLogA("CToxProto::LoadToxData: tox profile is empty");
+ fclose(hFile);
+ return;
+ }
+
+ uint8_t *data = (uint8_t*)mir_alloc(size);
+ if (fread(data, sizeof(uint8_t), size, hFile) != size)
+ {
+ debugLogA("CToxProto::LoadToxData: could not read tox profile");
+ fclose(hFile);
+ mir_free(data);
+ return;
+ }
+
+ if (tox_is_data_encrypted(data))
+ {
+ ptrT password(getTStringA("Password"));
+ char *password_utf8 = mir_utf8encodeW(password);
+ if (tox_encrypted_load(tox, data, size, (uint8_t*)password_utf8, strlen(password_utf8)) == TOX_ERROR)
+ {
+ debugLogA("CToxProto::LoadToxData: could not decrypt tox profile");
+ }
+ mir_free(password_utf8);
+ }
+ else
+ {
+ if (tox_load(tox, data, size) == TOX_ERROR)
+ {
+ debugLogA("CToxProto::LoadToxData: could not load tox profile");
+ }
+ }
+
+ mir_free(data);
+ fclose(hFile);
+}
+
+void CToxProto::SaveToxProfile()
+{
+ 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);
+ uint8_t *data = (uint8_t*)mir_alloc(size);
+ if (password && _tcslen(password))
+ {
+ char *password_utf8 = mir_utf8encodeW(password);
+ if (tox_encrypted_save(tox, data, (uint8_t*)password_utf8, strlen(password_utf8)) == TOX_ERROR)
+ {
+ debugLogA("CToxProto::LoadToxData: could not encrypt tox profile");
+ mir_free(password_utf8);
+ mir_free(data);
+ fclose(hFile);
+ return;
+ }
+ mir_free(password_utf8);
+ }
+ else
+ {
+ tox_save(tox, data);
+ }
+
+ if (fwrite(data, sizeof(uint8_t), size, hFile) != size)
+ {
+ 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)
{
CToxProto *proto = (CToxProto*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
@@ -25,21 +120,6 @@ INT_PTR CToxProto::ToxProfileManagerProc(HWND hwnd, UINT uMsg, WPARAM wParam, LP
profilePath = (TCHAR*)mir_calloc(sizeof(TCHAR)*MAX_PATH);
SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)profilePath);
-
- std::tstring toxProfilePath = proto->GetToxProfilePath();
- if (proto->IsFileExists(toxProfilePath))
- {
- _tcscpy(profilePath, proto->GetToxProfilePath().c_str());
- SetDlgItemText(hwnd, IDC_PROFILE_PATH, profilePath);
-
- CheckDlgButton(hwnd, IDC_USE_EXISTING, TRUE);
- EnableWindow(GetDlgItem(hwnd, IDC_PROFILE_PATH), IsDlgButtonChecked(hwnd, IDC_USE_EXISTING));
- EnableWindow(GetDlgItem(hwnd, IDC_BROWSE_PROFILE), IsDlgButtonChecked(hwnd, IDC_USE_EXISTING));
- }
- else
- {
- CheckDlgButton(hwnd, IDC_CREATE_NEW, TRUE);
- }
}
return TRUE;
@@ -54,12 +134,6 @@ INT_PTR CToxProto::ToxProfileManagerProc(HWND hwnd, UINT uMsg, WPARAM wParam, LP
case WM_COMMAND:
switch (LOWORD(wParam))
{
- case IDC_CREATE_NEW:
- case IDC_USE_EXISTING:
- EnableWindow(GetDlgItem(hwnd, IDC_PROFILE_PATH), IsDlgButtonChecked(hwnd, IDC_USE_EXISTING));
- EnableWindow(GetDlgItem(hwnd, IDC_BROWSE_PROFILE), IsDlgButtonChecked(hwnd, IDC_USE_EXISTING));
- break;
-
case IDC_BROWSE_PROFILE:
{
TCHAR filter[MAX_PATH] = { 0 };
@@ -76,6 +150,7 @@ INT_PTR CToxProto::ToxProfileManagerProc(HWND hwnd, UINT uMsg, WPARAM wParam, LP
if (GetOpenFileName(&ofn) && profilePath)
{
+ EnableWindow(GetDlgItem(hwnd, IDOK), TRUE);
SetDlgItemText(hwnd, IDC_PROFILE_PATH, profilePath);
}
}
@@ -83,32 +158,29 @@ INT_PTR CToxProto::ToxProfileManagerProc(HWND hwnd, UINT uMsg, WPARAM wParam, LP
case IDOK:
{
- std::tstring toxProfilePath = proto->GetToxProfilePath();
- if (IsDlgButtonChecked(hwnd, IDC_USE_EXISTING))
+ std::tstring defaultProfilePath = proto->GetToxProfilePath();
+ if (profilePath && _tcslen(profilePath))
{
- if (profilePath[0] != 0 && toxProfilePath != profilePath)
+ if (_tcsicmp(profilePath, defaultProfilePath.c_str()) != 0)
{
- CopyFile(profilePath, toxProfilePath.c_str(), FALSE);
+ CopyFile(profilePath, defaultProfilePath.c_str(), FALSE);
}
}
else
{
- if (proto->IsFileExists(toxProfilePath))
- {
- if (MessageBox(NULL,
- TranslateT("Tox profile with same name already exists.\r\nIf you continue, the profile will be lost!"),
- TranslateT("Tox profile"),
- MB_OKCANCEL | MB_ICONWARNING) == IDCANCEL)
- {
- break;
- }
- DeleteFile(profilePath);
- }
+ fclose(_wfopen(defaultProfilePath.c_str(), _T("w")));
}
EndDialog(hwnd, 1);
}
break;
+ case IDCANCEL:
+ {
+ std::tstring defaultProfilePath = proto->GetToxProfilePath();
+ fclose(_wfopen(defaultProfilePath.c_str(), _T("w")));
+ EndDialog(hwnd, 0);
+ }
+ break;
}
break;
}
diff --git a/protocols/Tox/src/tox_proto.cpp b/protocols/Tox/src/tox_proto.cpp
index 0d3ab920f6..be66970f60 100644
--- a/protocols/Tox/src/tox_proto.cpp
+++ b/protocols/Tox/src/tox_proto.cpp
@@ -5,8 +5,7 @@ PROTO<CToxProto>(protoName, userName)
{
accountName = mir_tstrdup(userName);
- if (this->getStringA("ToxID"))
- InitToxCore();
+ InitToxCore();
CreateProtoService(PS_CREATEACCMGRUI, &CToxProto::OnAccountManagerInit);
@@ -111,7 +110,7 @@ int __cdecl CToxProto::Authorize(HANDLE hDbEvent)
db_unset(hContact, "CList", "NotOnList");
delSetting(hContact, "Auth");
- SaveToxData();
+ SaveToxProfile();
return 0;
}
@@ -132,7 +131,7 @@ int __cdecl CToxProto::AuthRequest(MCONTACT hContact, const PROTOCHAR* szMessage
int32_t number = tox_add_friend(tox, pubKey.data(), (uint8_t*)(char*)reason, (uint16_t)strlen(reason));
if (number > TOX_ERROR)
{
- SaveToxData();
+ SaveToxProfile();
// change tox address in contact id by tox id
std::string id = ToxAddressToId(address);
@@ -217,7 +216,7 @@ int __cdecl CToxProto::SetStatus(int iNewStatus)
m_iStatus = iNewStatus;
if (tox_set_user_status(tox, MirandaToToxStatus(iNewStatus)) == 0)
{
- SaveToxData();
+ SaveToxProfile();
}
}
}
diff --git a/protocols/Tox/src/tox_proto.h b/protocols/Tox/src/tox_proto.h
index 13ca949b93..547954bea9 100644
--- a/protocols/Tox/src/tox_proto.h
+++ b/protocols/Tox/src/tox_proto.h
@@ -124,8 +124,12 @@ private:
void __cdecl PollingThread(void*);
// tox profile
+ HANDLE hProfile;
std::tstring GetToxProfilePath();
+ void LoadToxProfile();
+ void SaveToxProfile();
+
static INT_PTR CALLBACK ToxProfileManagerProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
// accounts
@@ -225,11 +229,6 @@ private:
std::string ToxAddressToId(std::string);
static bool IsFileExists(std::tstring path);
-
-
-
- void LoadToxData();
- void SaveToxData();
};
#endif //_TOX_PROTO_H_ \ No newline at end of file
diff --git a/protocols/Tox/src/tox_utils.cpp b/protocols/Tox/src/tox_utils.cpp
index 854d59f40d..a290e6a76e 100644
--- a/protocols/Tox/src/tox_utils.cpp
+++ b/protocols/Tox/src/tox_utils.cpp
@@ -120,92 +120,3 @@ bool CToxProto::IsFileExists(std::tstring path)
}
return false;
}
-
-void CToxProto::LoadToxData()
-{
- 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);
-
- uint8_t *data = (uint8_t*)mir_alloc(size);
- if (fread(data, sizeof(uint8_t), size, hFile) != size)
- {
- debugLogA("CToxProto::LoadToxData: could not read tox profile");
- fclose(hFile);
- mir_free(data);
- return;
- }
-
- if (tox_is_data_encrypted(data))
- {
- ptrT password(getTStringA("Password"));
- char *password_utf8 = mir_utf8encodeW(password);
- if (tox_encrypted_load(tox, data, size, (uint8_t*)password_utf8, strlen(password_utf8)) == TOX_ERROR)
- {
- debugLogA("CToxProto::LoadToxData: could not decrypt tox profile");
- }
- mir_free(password_utf8);
- }
- else
- {
- if (tox_load(tox, data, size) == TOX_ERROR)
- {
- debugLogA("CToxProto::LoadToxData: could not load tox profile");
- }
- }
-
- mir_free(data);
- fclose(hFile);
-}
-
-void CToxProto::SaveToxData()
-{
- std::tstring toxProfilePath = GetToxProfilePath();
- FILE *hFile = _wfopen(toxProfilePath.c_str(), _T("wb"));
- if (!hFile)
- {
- debugLogA("CToxProto::LoadToxData: could not open tox profile");
- return;
- }
-
- ptrT password(getTStringA("Password"));
- uint32_t size;
- if (password && _tcslen(password))
- size = tox_encrypted_size(tox);
- else
- size = tox_size(tox);
- uint8_t *data = (uint8_t*)mir_alloc(size);
- if (password && _tcslen(password))
- {
- char *password_utf8 = mir_utf8encodeW(password);
- if (tox_encrypted_save(tox, data, (uint8_t*)password_utf8, strlen(password_utf8)) == TOX_ERROR)
- {
- debugLogA("CToxProto::LoadToxData: could not encrypt tox profile");
- mir_free(password_utf8);
- mir_free(data);
- fclose(hFile);
- return;
- }
- mir_free(password_utf8);
- }
- else
- {
- tox_save(tox, data);
- }
-
- if (fwrite(data, sizeof(uint8_t), size, hFile) != size)
- {
- debugLogA("CToxProto::LoadToxData: could not write tox profile");
- }
-
- mir_free(data);
- fclose(hFile);
-}