From 2e51a3103f26ca7a9fec8f96baf56f4d51fd0112 Mon Sep 17 00:00:00 2001 From: Alexander Lantsev Date: Thu, 27 Feb 2014 09:59:53 +0000 Subject: Dropbox: - added error notifications - fixed menu items behavior git-svn-id: http://svn.miranda-ng.org/main/trunk@8283 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/Dropbox/src/dropbox.cpp | 52 ++---- plugins/Dropbox/src/dropbox.h | 14 +- plugins/Dropbox/src/dropbox_events.cpp | 39 +++- plugins/Dropbox/src/dropbox_menus.cpp | 34 ++-- plugins/Dropbox/src/dropbox_services.cpp | 29 +-- plugins/Dropbox/src/dropbox_transfers.cpp | 293 +++++++++++++++++------------- plugins/Dropbox/src/http_request.h | 2 + 7 files changed, 254 insertions(+), 209 deletions(-) (limited to 'plugins/Dropbox') diff --git a/plugins/Dropbox/src/dropbox.cpp b/plugins/Dropbox/src/dropbox.cpp index 41c53d22a4..3498680c24 100644 --- a/plugins/Dropbox/src/dropbox.cpp +++ b/plugins/Dropbox/src/dropbox.cpp @@ -37,7 +37,7 @@ bool CDropbox::HasAccessToken() return db_get_sa(NULL, MODULE, "TokenSecret") != NULL; } -void CDropbox::RequestAcceessToken(MCONTACT hContact) +void CDropbox::RequestAcceessToken() { ShellExecuteA(NULL, "open", DROPBOX_WWW_URL DROPBOX_API_VER "/oauth2/authorize?response_type=code&client_id=" DROPBOX_API_KEY, NULL, NULL, SW_SHOWDEFAULT); @@ -63,16 +63,18 @@ void CDropbox::RequestAcceessToken(MCONTACT hContact) request->AddHeader("Content-Type", "application/x-www-form-urlencoded"); request->AddBasicAuthHeader(DROPBOX_API_KEY, DROPBOX_API_SECRET); - NETLIBHTTPREQUEST *response = request->Send(); + mir_ptr response(request->Send()); delete request; + MCONTACT hContact = CDropbox::GetDefaultContact(); + if (response) { - if (response->resultCode == HTTP_STATUS::OK) + JSONNODE *root = json_parse(response->pData); + if (root) { - JSONNODE *root = json_parse(response->pData); - if (root != NULL) + if (response->resultCode == HTTP_STATUS::OK) { JSONNODE *node = json_get(root, "access_token"); ptrA access_token = ptrA(mir_u2a(json_as_string(node))); @@ -80,46 +82,34 @@ void CDropbox::RequestAcceessToken(MCONTACT hContact) if (hContact) { - node = json_get(root, "uid"); - wchar_t *uid = json_as_string(node); - db_set_ws(hContact, MODULE, "uid", uid); if (db_get_w(hContact, MODULE, "Status", ID_STATUS_OFFLINE) == ID_STATUS_OFFLINE) db_set_w(hContact, MODULE, "Status", ID_STATUS_ONLINE); } - CDropbox::ShowNotification(TranslateT("Access request"), TranslateT("Access granted"), MB_ICONINFORMATION); - - delete node; - delete root; + CDropbox::ShowNotification(TranslateT("You have been authorized"), MB_ICONINFORMATION); } - } - else - { - JSONNODE *root = json_parse(response->pData); - if (root != NULL) + else { JSONNODE *node = json_get(root, "error_description"); - wchar_t *error_description = json_as_string(node); + ptrW error_description(json_as_string(node)); - CDropbox::ShowNotification(TranslateT("Access request"), error_description, MB_ICONERROR); - - delete node; - delete root; + CDropbox::ShowNotification((wchar_t*)error_description, MB_ICONERROR); } } - - mir_free(response); } + else + HandleFileTransferError(response, hContact); } } -void CDropbox::DestroyAcceessToken(MCONTACT hContact) +void CDropbox::DestroyAcceessToken() { HttpRequest *request = new HttpRequest(hNetlibUser, REQUEST_POST, DROPBOX_API_URL "/disable_access_token"); - NETLIBHTTPREQUEST *response = request->Send(); + mir_ptr response(request->Send()); delete request; - mir_free(response); + + MCONTACT hContact = CDropbox::GetDefaultContact(); db_unset(NULL, MODULE, "TokenSecret"); if (hContact) @@ -131,17 +121,15 @@ void CDropbox::DestroyAcceessToken(MCONTACT hContact) void CDropbox::RequestApiAuthorizationAsync(void *arg) { - MCONTACT hContact = (MCONTACT)arg; - if (HasAccessToken() && MessageBox( NULL, TranslateT("Are you sure you want to request athorization?"), TranslateT("Request athorization"), MB_YESNO | MB_ICONQUESTION) == IDYES) { - INSTANCE->DestroyAcceessToken(hContact); - INSTANCE->RequestAcceessToken(hContact); + INSTANCE->DestroyAcceessToken(); + INSTANCE->RequestAcceessToken(); } else - INSTANCE->RequestAcceessToken(hContact); + INSTANCE->RequestAcceessToken(); } \ No newline at end of file diff --git a/plugins/Dropbox/src/dropbox.h b/plugins/Dropbox/src/dropbox.h index 72af0114ee..89afb8635d 100644 --- a/plugins/Dropbox/src/dropbox.h +++ b/plugins/Dropbox/src/dropbox.h @@ -66,17 +66,19 @@ private: // access token static bool HasAccessToken(); - void RequestAcceessToken(MCONTACT hContact); - void DestroyAcceessToken(MCONTACT hContact); + void RequestAcceessToken(); + void DestroyAcceessToken(); static void RequestApiAuthorizationAsync(void *arg); // transrers - void SendFileChunkedFirst(const char *data, int length, char *uploadId, int &offset); - void SendFileChunkedNext(const char *data, int length, const char *uploadId, int &offset); - void SendFileChunkedLast(const char *fileName, const char *uploadId, MCONTACT hContact); + int HandleFileTransferError(NETLIBHTTPREQUEST *response, MCONTACT hContact); - void CreateFolder(const char *folderName, MCONTACT hContact); + int SendFileChunkedFirst(const char *data, int length, char *uploadId, int &offset, MCONTACT hContact); + int SendFileChunkedNext(const char *data, int length, const char *uploadId, int &offset, MCONTACT hContact); + int SendFileChunkedLast(const char *fileName, const char *uploadId, MCONTACT hContact); + + int CreateFolder(const char *folderName, MCONTACT hContact); static void _cdecl SendFileAsync(void *arg); diff --git a/plugins/Dropbox/src/dropbox_events.cpp b/plugins/Dropbox/src/dropbox_events.cpp index 8f234b770d..fd2d67d26a 100644 --- a/plugins/Dropbox/src/dropbox_events.cpp +++ b/plugins/Dropbox/src/dropbox_events.cpp @@ -20,6 +20,12 @@ int CDropbox::OnModulesLoaded(WPARAM wParam, LPARAM lParam) if (!CallService(MS_PROTO_ADDTOCONTACT, hContact, (LPARAM)MODULE)) { db_set_s(hContact, MODULE, "Nick", MODULE); + db_set_ws(hContact, "CList", "MyHandle", L"Dropbox"); + + if (HasAccessToken() && db_get_w(hContact, MODULE, "Status", ID_STATUS_OFFLINE) == ID_STATUS_OFFLINE) + { + db_set_w(hContact, MODULE, "Status", ID_STATUS_ONLINE); + } } } @@ -62,8 +68,8 @@ int CDropbox::OnContactDeleted(WPARAM hContact, LPARAM lParam) { if (lstrcmpiA(GetContactProto(hContact), MODULE) == 0) { - if (INSTANCE->HasAccessToken()) - INSTANCE->DestroyAcceessToken(hContact); + if (HasAccessToken()) + INSTANCE->DestroyAcceessToken(); } return 0; @@ -78,13 +84,13 @@ int CDropbox::OnSrmmWindowOpened(WPARAM wParam, LPARAM lParam) BBButton bbd = { sizeof(bbd) }; bbd.pszModuleName = MODULE; - if (ev->hContact == INSTANCE->GetDefaultContact()) + if (ev->hContact == GetDefaultContact() || !HasAccessToken() || status == ID_STATUS_OFFLINE) bbd.bbbFlags = BBSF_HIDDEN | BBSF_DISABLED; - else if (status == ID_STATUS_OFFLINE) + else if (INSTANCE->hContactTransfer) bbd.bbbFlags = BBSF_DISABLED; bbd.dwButtonID = BBB_ID_FILE_SEND; - CallService(MS_BB_SETBUTTONSTATE, (WPARAM)ev->hContact, (LPARAM)&bbd); + CallService(MS_BB_SETBUTTONSTATE, ev->hContact, (LPARAM)&bbd); } return 0; @@ -97,10 +103,31 @@ int CDropbox::OnSrmmButtonPressed(WPARAM wParam, LPARAM lParam) { INSTANCE->hContactTransfer = cbc->hContact; - HWND hwnd = (HWND)CallService(MS_FILE_SENDFILE, INSTANCE->GetDefaultContact(), 0); + HWND hwnd = (HWND)CallService(MS_FILE_SENDFILE, GetDefaultContact(), 0); dcftp[hwnd] = cbc->hContact; } return 0; +} + +int CDropbox::OnFileDoalogCancelled(WPARAM hContact, LPARAM lParam) +{ + HWND hwnd = (HWND)lParam; + if (INSTANCE->hContactTransfer == dcftp[hwnd]) + { + dcftp.erase((HWND)lParam); + INSTANCE->hContactTransfer = 0; + } + + return 0; +} + +int CDropbox::OnFileDoalogSuccessed(WPARAM hContact, LPARAM lParam) +{ + HWND hwnd = (HWND)lParam; + if (INSTANCE->hContactTransfer == dcftp[hwnd]) + dcftp.erase((HWND)lParam); + + return 0; } \ No newline at end of file diff --git a/plugins/Dropbox/src/dropbox_menus.cpp b/plugins/Dropbox/src/dropbox_menus.cpp index a4047d4be8..ea885779ef 100644 --- a/plugins/Dropbox/src/dropbox_menus.cpp +++ b/plugins/Dropbox/src/dropbox_menus.cpp @@ -10,14 +10,14 @@ void CDropbox::InitMenus() mi.pszService = MODULE"/SendFilesToDropbox"; mi.ptszName = LPGENT("Send files to Dropbox"); - mi.position = -201002000 + CMI_SEND_FILES; + mi.position = -2000020000 + CMI_SEND_FILES; mi.icolibItem = LoadSkinnedIconHandle(SKINICON_EVENT_FILE); ContactMenuItems[CMI_SEND_FILES] = Menu_AddContactMenuItem(&mi); CreateServiceFunction(mi.pszService, SendFilesToDropbox); mi.pszService = MODULE"/RequestAuthorization"; mi.ptszName = LPGENT("Request Authorization"); - mi.position = -201001000 + CMI_API_REQUEST_AUTH; + mi.position = -2000001000 + CMI_API_REQUEST_AUTH; mi.icolibItem = LoadSkinnedIconHandle(SKINICON_AUTH_REQUEST); ContactMenuItems[CMI_API_REQUEST_AUTH] = Menu_AddContactMenuItem(&mi); CreateServiceFunction(mi.pszService, RequestApiAuthorization); @@ -36,23 +36,23 @@ void CDropbox::Menu_DisableItem(HGENMENU hMenuItem, BOOL bDisable) int CDropbox::OnPrebuildContactMenu(WPARAM hContact, LPARAM lParam) { - if ( !hContact) + if (!hContact) return 0; - //bool ctrlPressed = (GetKeyState(VK_CONTROL) & 0x8000) != 0; - char *module = GetContactProto(hContact); - WORD status = db_get_w(hContact, module, "Status", ID_STATUS_OFFLINE); - CallContactService(hContact, PS_GETSTATUS, 0, 0); - - if (hContact == INSTANCE->GetDefaultContact() || status == ID_STATUS_OFFLINE) - Menu_ShowItem(INSTANCE->ContactMenuItems[CMI_SEND_FILES], FALSE); - else - Menu_DisableItem(INSTANCE->ContactMenuItems[CMI_SEND_FILES], INSTANCE->hContactTransfer); - - if (strcmp(module, MODULE)) - { - Menu_ShowItem(INSTANCE->ContactMenuItems[CMI_API_REQUEST_AUTH], FALSE); - } + Menu_DisableItem(ContactMenuItems[CMI_SEND_FILES], FALSE); + + Menu_ShowItem(ContactMenuItems[CMI_SEND_FILES], FALSE); + Menu_ShowItem(ContactMenuItems[CMI_API_REQUEST_AUTH], FALSE); + + WORD status = db_get_w(hContact, GetContactProto(hContact), "Status", ID_STATUS_OFFLINE); + + if (hContact == GetDefaultContact()) + Menu_ShowItem(ContactMenuItems[CMI_API_REQUEST_AUTH], TRUE); + else if (status != ID_STATUS_OFFLINE && HasAccessToken()) + Menu_ShowItem(ContactMenuItems[CMI_SEND_FILES], TRUE); + + if (INSTANCE->hContactTransfer) + Menu_DisableItem(ContactMenuItems[CMI_SEND_FILES], TRUE); return 0; } \ No newline at end of file diff --git a/plugins/Dropbox/src/dropbox_services.cpp b/plugins/Dropbox/src/dropbox_services.cpp index 1ce493c25a..bae1d1d548 100644 --- a/plugins/Dropbox/src/dropbox_services.cpp +++ b/plugins/Dropbox/src/dropbox_services.cpp @@ -21,6 +21,9 @@ INT_PTR CDropbox::ProtoGetCaps(WPARAM wParam, LPARAM lParam) INT_PTR CDropbox::ProtoSendFile(WPARAM wParam, LPARAM lParam) { + if (!HasAccessToken()) + return ACKRESULT_FAILED; + CCSDATA *pccsd = (CCSDATA*)lParam; FileTransfer *ftp = new FileTransfer(); @@ -84,7 +87,7 @@ INT_PTR CDropbox::ProtoSendFile(WPARAM wParam, LPARAM lParam) return fileId; } -INT_PTR CDropbox::ProtoSendMessage( WPARAM wParam, LPARAM lParam) +INT_PTR CDropbox::ProtoSendMessage(WPARAM wParam, LPARAM lParam) { CCSDATA *pccsd = (CCSDATA*)lParam; @@ -95,8 +98,8 @@ INT_PTR CDropbox::ProtoSendMessage( WPARAM wParam, LPARAM lParam) dbei.timestamp = time(NULL); dbei.eventType = EVENTTYPE_MESSAGE; dbei.cbBlob = strlen(message); - dbei.pBlob = (PBYTE)mir_strdup(message); - //dbei.flags = DBEF_UTF; + dbei.pBlob = (PBYTE)message; + dbei.flags = DBEF_SENT | DBEF_UTF; db_event_add(pccsd->hContact, &dbei); return 0; @@ -104,7 +107,7 @@ INT_PTR CDropbox::ProtoSendMessage( WPARAM wParam, LPARAM lParam) INT_PTR CDropbox::RequestApiAuthorization(WPARAM wParam, LPARAM lParam) { - mir_forkthread(CDropbox::RequestApiAuthorizationAsync, (void*)wParam); + mir_forkthread(CDropbox::RequestApiAuthorizationAsync, 0); return 0; } @@ -119,23 +122,5 @@ INT_PTR CDropbox::SendFilesToDropbox(WPARAM hContact, LPARAM lParam) dcftp[hwnd] = hContact; - return 0; -} - -int CDropbox::OnFileDoalogCancelled(WPARAM hContact, LPARAM lParam) -{ - HWND hwnd = (HWND)lParam; - if (INSTANCE->hContactTransfer == dcftp[hwnd]) - dcftp.erase((HWND)lParam); - - return 0; -} - -int CDropbox::OnFileDoalogSuccessed(WPARAM hContact, LPARAM lParam) -{ - HWND hwnd = (HWND)lParam; - if (INSTANCE->hContactTransfer == dcftp[hwnd]) - dcftp.erase((HWND)lParam); - return 0; } \ No newline at end of file diff --git a/plugins/Dropbox/src/dropbox_transfers.cpp b/plugins/Dropbox/src/dropbox_transfers.cpp index c87d06fe15..3c3e949430 100644 --- a/plugins/Dropbox/src/dropbox_transfers.cpp +++ b/plugins/Dropbox/src/dropbox_transfers.cpp @@ -1,41 +1,56 @@ #include "common.h" -void CDropbox::SendFileChunkedFirst(const char *data, int length, char *uploadId, int &offset) +int CDropbox::HandleFileTransferError(NETLIBHTTPREQUEST *response, MCONTACT hContact) +{ + if (!response) + { + CDropbox::ShowNotification(TranslateT("Server does not respond"), MB_ICONERROR, hContact); + return ACKRESULT_FAILED; + } + + if (response->resultCode != HTTP_STATUS::OK) + { + CDropbox::ShowNotification(HttpStatusToText((HTTP_STATUS)response->resultCode), MB_ICONERROR, hContact); + return response->resultCode; + } + + return 0; +} + +int CDropbox::SendFileChunkedFirst(const char *data, int length, char *uploadId, int &offset, MCONTACT hContact) { HttpRequest *request = new HttpRequest(hNetlibUser, REQUEST_PUT, DROPBOX_APICONTENT_URL "/chunked_upload"); request->AddBearerAuthHeader(db_get_sa(NULL, MODULE, "TokenSecret")); request->AddHeader("Content-Type", "application/octet-stream"); - request->pData = (char*)mir_alloc(sizeof(char) * length ); + request->pData = (char*)mir_alloc(sizeof(char) * length); memcpy(request->pData, data, length); request->dataLength = length; - NETLIBHTTPREQUEST *response = request->Send(); + mir_ptr response(request->Send()); delete request; - if (response) + if (response && response->resultCode == HTTP_STATUS::OK) { - if (response->resultCode == HTTP_STATUS::OK) + JSONNODE *root = json_parse(response->pData); + if (root) { - JSONNODE *root = json_parse(response->pData); - if (root != NULL) - { - JSONNODE *node = json_get(root, "upload_id"); - strcpy(uploadId, mir_u2a(json_as_string(node))); + JSONNODE *node = json_get(root, "upload_id"); + strcpy(uploadId, mir_u2a(json_as_string(node))); - node = json_get(root, "offset"); - offset = json_as_int(node); + node = json_get(root, "offset"); + offset = json_as_int(node); - delete node; - delete root; - } + return 0; } - mir_free(response); + return HandleFileTransferError(response, hContact); } + + return HandleFileTransferError(response, hContact); } -void CDropbox::SendFileChunkedNext(const char *data, int length, const char *uploadId, int &offset) +int CDropbox::SendFileChunkedNext(const char *data, int length, const char *uploadId, int &offset, MCONTACT hContact) { CMStringA url = DROPBOX_APICONTENT_URL "/chunked_upload"; url.AppendFormat("?upload_id=%s&offset=%i", uploadId, offset); @@ -47,30 +62,28 @@ void CDropbox::SendFileChunkedNext(const char *data, int length, const char *upl memcpy(request->pData, data, length); request->dataLength = length; - NETLIBHTTPREQUEST *response = request->Send(); + mir_ptr response(request->Send()); delete request; - if (response) + if (response && response->resultCode == HTTP_STATUS::OK) { - if (response->resultCode == HTTP_STATUS::OK) + JSONNODE *root = json_parse(response->pData); + if (root) { - JSONNODE *root = json_parse(response->pData); - if (root != NULL) - { - JSONNODE *node = json_get(root, "offset"); - offset = json_as_int(node); + JSONNODE *node = json_get(root, "offset"); + offset = json_as_int(node); - delete node; - delete root; - } + return 0; } - mir_free(response); + return HandleFileTransferError(response, hContact); } + + return HandleFileTransferError(response, hContact); } -void CDropbox::SendFileChunkedLast(const char *fileName, const char *uploadId, MCONTACT hContact) +int CDropbox::SendFileChunkedLast(const char *fileName, const char *uploadId, MCONTACT hContact) { CMStringA url; url.AppendFormat( @@ -88,13 +101,13 @@ void CDropbox::SendFileChunkedLast(const char *fileName, const char *uploadId, M request->pData = mir_strdup(param); request->dataLength = param.GetLength(); - NETLIBHTTPREQUEST *response = request->Send(); + mir_ptr response(request->Send()); delete request; - if (response) + if (response && response->resultCode == HTTP_STATUS::OK) { - if (response->resultCode == HTTP_STATUS::OK && !strchr(fileName, '\\')) + if (!strchr(fileName, '\\')) { url.Replace(DROPBOX_APICONTENT_URL, DROPBOX_API_URL); url.Replace("commit_chunked_upload", "shares"); @@ -102,43 +115,41 @@ void CDropbox::SendFileChunkedLast(const char *fileName, const char *uploadId, M request = new HttpRequest(hNetlibUser, REQUEST_POST, url); request->AddBearerAuthHeader(db_get_sa(NULL, MODULE, "TokenSecret")); - mir_free(response); - response = request->Send(); - if (response) + delete request; + + if (response &&response->resultCode == HTTP_STATUS::OK) { - if (response->resultCode == HTTP_STATUS::OK) + JSONNODE *root = json_parse(response->pData); + if (root) { - JSONNODE *root = json_parse(response->pData); - if (root != NULL) - { - JSONNODE *node = json_get(root, "url"); + JSONNODE *node = json_get(root, "url"); - char message[1024]; - mir_snprintf( - message, - SIZEOF(message), - Translate("Link to download file \"%s\": %s"), - fileName, - mir_utf8encodeW(json_as_string(node))); + char message[1024]; + mir_snprintf( + message, + SIZEOF(message), + Translate("Link to download file \"%s\": %s"), + fileName, + mir_utf8encodeW(json_as_string(node))); - CallContactService(hContact, PSS_MESSAGE, 0, (LPARAM)&message); + CallContactService(hContact, PSS_MESSAGE, PREF_UTF, (LPARAM)&message); - delete node; - delete root; - } + return 0; } - mir_free(response); + return HandleFileTransferError(response, hContact); } + + return HandleFileTransferError(response, hContact); } - else - mir_free(response); } + + return HandleFileTransferError(response, hContact); } -void CDropbox::CreateFolder(const char *folderName, MCONTACT hContact) +int CDropbox::CreateFolder(const char *folderName, MCONTACT hContact) { CMStringA folder = folderName; folder.Replace('\\', '/'); @@ -148,19 +159,17 @@ void CDropbox::CreateFolder(const char *folderName, MCONTACT hContact) DROPBOX_API_ROOT, folder); - HttpRequest *request = new HttpRequest(hNetlibUser, REQUEST_POST, DROPBOX_API_URL "/fileops/create_folder"); + mir_ptr request(new HttpRequest(hNetlibUser, REQUEST_POST, DROPBOX_API_URL "/fileops/create_folder")); request->AddBearerAuthHeader(db_get_sa(NULL, MODULE, "TokenSecret")); request->AddHeader("Content-Type", "application/x-www-form-urlencoded"); request->pData = mir_strdup(param); request->dataLength = param.GetLength(); - NETLIBHTTPREQUEST *response = request->Send(); + mir_ptr response(request->Send()); - delete request; - - if (response) + if (response && response->resultCode == HTTP_STATUS::OK) { - if (response->resultCode == HTTP_STATUS::OK && !strchr(folderName, '\\')) + if (!strchr(folderName, '\\')) { CMStringA url = DROPBOX_API_URL; url.AppendFormat("/shares/%s/%s", @@ -174,107 +183,139 @@ void CDropbox::CreateFolder(const char *folderName, MCONTACT hContact) response = request->Send(); - if (response) + if (response && response->resultCode == HTTP_STATUS::OK) { - if (response->resultCode == HTTP_STATUS::OK) + JSONNODE *root = json_parse(response->pData); + if (root != NULL) { - JSONNODE *root = json_parse(response->pData); - if (root != NULL) - { - JSONNODE *node = json_get(root, "url"); - char message[1024]; - mir_snprintf( - message, - SIZEOF(message), - Translate("Link to download folder \"%s\": %s"), - folderName, - mir_utf8encodeW(json_as_string(node))); - - CallContactService(hContact, PSS_MESSAGE, 0, (LPARAM)&message); - - delete node; - delete root; - } + JSONNODE *node = json_get(root, "url"); + char message[1024]; + mir_snprintf( + message, + SIZEOF(message), + Translate("Link to download folder \"%s\": %s"), + folderName, + mir_utf8encodeW(json_as_string(node))); + + CallContactService(hContact, PSS_MESSAGE, DBEF_UTF, (LPARAM)&message); + + return 0; } - mir_free(response); + return HandleFileTransferError(response, hContact); } + + return HandleFileTransferError(response, hContact); } - else - mir_free(response); } + + return HandleFileTransferError(response, hContact); } void _cdecl CDropbox::SendFileAsync(void *arg) { + bool error = false; FileTransfer *ftp = (FileTransfer*)arg; ProtoBroadcastAck(MODULE, ftp->pfts.hContact, ACKTYPE_FILE, ACKRESULT_INITIALISING, ftp->hProcess, 0); for (int i = 0; ftp->pszFolders[i]; i++) - INSTANCE->CreateFolder(ftp->pszFolders[i], ftp->hContact); + { + if (INSTANCE->CreateFolder(ftp->pszFolders[i], ftp->hContact)) + { + error = true; + break; + } + } - for (int i = 0; ftp->pfts.pszFiles[i]; i++) + if (!error) { - FILE *file = fopen(ftp->pfts.pszFiles[i], "rb"); - if (file != NULL) + for (int i = 0; ftp->pfts.pszFiles[i]; i++) { - int offset = 0; - char *uploadId = new char[32]; + FILE *file = fopen(ftp->pfts.pszFiles[i], "rb"); + if (file != NULL) + { + int offset = 0; + char *uploadId = new char[32]; - const char *fileName = NULL; - if (!ftp->relativePathStart) - fileName = strrchr(ftp->pfts.pszFiles[i], '\\') + 1; - else - fileName = &ftp->pfts.pszFiles[i][ftp->relativePathStart]; + const char *fileName = NULL; + if (!ftp->relativePathStart) + fileName = strrchr(ftp->pfts.pszFiles[i], '\\') + 1; + else + fileName = &ftp->pfts.pszFiles[i][ftp->relativePathStart]; - fseek(file, 0, SEEK_END); - DWORD fileSize = ftell(file); - fseek(file, 0, SEEK_SET); + fseek(file, 0, SEEK_END); + DWORD fileSize = ftell(file); + fseek(file, 0, SEEK_SET); - ftp->pfts.currentFileNumber = i; - ftp->pfts.currentFileSize = fileSize; - ftp->pfts.currentFileProgress = 0; - ftp->pfts.szCurrentFile = strrchr(ftp->pfts.pszFiles[i], '\\') + 1; + ftp->pfts.currentFileNumber = i; + ftp->pfts.currentFileSize = fileSize; + ftp->pfts.currentFileProgress = 0; + ftp->pfts.szCurrentFile = strrchr(ftp->pfts.pszFiles[i], '\\') + 1; - ProtoBroadcastAck(MODULE, ftp->pfts.hContact, ACKTYPE_FILE, ACKRESULT_DATA, ftp->hProcess, (LPARAM)&ftp->pfts); + ProtoBroadcastAck(MODULE, ftp->pfts.hContact, ACKTYPE_FILE, ACKRESULT_DATA, ftp->hProcess, (LPARAM)&ftp->pfts); - while (!feof(file) && !ferror(file)) - { - int chunkSize = DROPBOX_FILE_CHUNK_SIZE; - if (fileSize < 1024*1024) - chunkSize = DROPBOX_FILE_CHUNK_SIZE / 5; - else if (fileSize > 20*1024*1024) - chunkSize = DROPBOX_FILE_CHUNK_SIZE * 4; + while (!feof(file) && !ferror(file)) + { + int chunkSize = DROPBOX_FILE_CHUNK_SIZE; + if (fileSize < 1024*1024) + chunkSize = DROPBOX_FILE_CHUNK_SIZE / 5; + else if (fileSize > 20*1024*1024) + chunkSize = DROPBOX_FILE_CHUNK_SIZE * 4; - char *data = new char[chunkSize + 1]; - int count = fread(data, sizeof(char), chunkSize, file); + char *data = new char[chunkSize + 1]; + int count = fread(data, sizeof(char), chunkSize, file); - if (!offset) - INSTANCE->SendFileChunkedFirst(data, count, uploadId, offset); - else - INSTANCE->SendFileChunkedNext(data, count, uploadId, offset); + if (!offset) + { + if (INSTANCE->SendFileChunkedFirst(data, count, uploadId, offset, ftp->hContact)) + { + error = true; + break; + } + } + else + { + if (INSTANCE->SendFileChunkedNext(data, count, uploadId, offset, ftp->hContact)) + { + error = true; + break; + } + } - ftp->pfts.currentFileProgress += count; - ftp->pfts.totalProgress += count; + ftp->pfts.currentFileProgress += count; + ftp->pfts.totalProgress += count; - ProtoBroadcastAck(MODULE, ftp->pfts.hContact, ACKTYPE_FILE, ACKRESULT_DATA, ftp->hProcess, (LPARAM)&ftp->pfts); - } + ProtoBroadcastAck(MODULE, ftp->pfts.hContact, ACKTYPE_FILE, ACKRESULT_DATA, ftp->hProcess, (LPARAM)&ftp->pfts); + } - fclose(file); + fclose(file); - INSTANCE->SendFileChunkedLast(fileName, uploadId, ftp->hContact); - ftp->pfts.currentFileProgress = ftp->pfts.currentFileSize; + if (!error) + { + if (INSTANCE->SendFileChunkedLast(fileName, uploadId, ftp->hContact)) + { + error = true; + } + else + { + ftp->pfts.currentFileProgress = ftp->pfts.currentFileSize; - if (i < ftp->pfts.totalFiles - 1) - ProtoBroadcastAck(MODULE, ftp->pfts.hContact, ACKTYPE_FILE, ACKRESULT_NEXTFILE, ftp->hProcess, 0); + if (i < ftp->pfts.totalFiles - 1) + ProtoBroadcastAck(MODULE, ftp->pfts.hContact, ACKTYPE_FILE, ACKRESULT_NEXTFILE, ftp->hProcess, 0); + } + } + } } } if (INSTANCE->hContactTransfer) INSTANCE->hContactTransfer = 0; - ProtoBroadcastAck(MODULE, ftp->pfts.hContact, ACKTYPE_FILE, ACKRESULT_SUCCESS, ftp->hProcess, 0); + if (!error) + ProtoBroadcastAck(MODULE, ftp->pfts.hContact, ACKTYPE_FILE, ACKRESULT_SUCCESS, ftp->hProcess, 0); + else + ProtoBroadcastAck(MODULE, ftp->pfts.hContact, ACKTYPE_FILE, ACKRESULT_FAILED, ftp->hProcess, 0); delete ftp; } \ No newline at end of file diff --git a/plugins/Dropbox/src/http_request.h b/plugins/Dropbox/src/http_request.h index 79882053e3..45e270993e 100644 --- a/plugins/Dropbox/src/http_request.h +++ b/plugins/Dropbox/src/http_request.h @@ -110,6 +110,8 @@ public: NETLIBHTTPREQUEST *Send() { szUrl = m_szUrl.GetBuffer(); + /*CMStringA message; message.AppendFormat("Send request to %s", szUrl); + CallService(MS_NETLIB_LOG, (WPARAM)m_hNetlibUser, (LPARAM)message.GetBuffer());*/ return (NETLIBHTTPREQUEST*)CallService(MS_NETLIB_HTTPTRANSACTION, (WPARAM)m_hNetlibUser, (LPARAM)this); } -- cgit v1.2.3