summaryrefslogtreecommitdiff
path: root/plugins/Dropbox/src/dropbox_transfers.cpp
diff options
context:
space:
mode:
authorAlexander Lantsev <aunsane@gmail.com>2014-02-27 09:59:53 +0000
committerAlexander Lantsev <aunsane@gmail.com>2014-02-27 09:59:53 +0000
commit2e51a3103f26ca7a9fec8f96baf56f4d51fd0112 (patch)
tree239feacbe6263d41b7bb021f4e226e2687775904 /plugins/Dropbox/src/dropbox_transfers.cpp
parent9b5210ac07c6e4e00af25eb90fdf653da791299a (diff)
Dropbox:
- added error notifications - fixed menu items behavior git-svn-id: http://svn.miranda-ng.org/main/trunk@8283 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'plugins/Dropbox/src/dropbox_transfers.cpp')
-rw-r--r--plugins/Dropbox/src/dropbox_transfers.cpp293
1 files changed, 167 insertions, 126 deletions
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<NETLIBHTTPREQUEST> 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<NETLIBHTTPREQUEST> 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<NETLIBHTTPREQUEST> 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<HttpRequest> 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<NETLIBHTTPREQUEST> 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