diff options
Diffstat (limited to 'plugins/Dropbox/src/dropbox_transfers.cpp')
-rw-r--r-- | plugins/Dropbox/src/dropbox_transfers.cpp | 155 |
1 files changed, 85 insertions, 70 deletions
diff --git a/plugins/Dropbox/src/dropbox_transfers.cpp b/plugins/Dropbox/src/dropbox_transfers.cpp index 8c520c69c3..f9bcce0bb4 100644 --- a/plugins/Dropbox/src/dropbox_transfers.cpp +++ b/plugins/Dropbox/src/dropbox_transfers.cpp @@ -1,27 +1,27 @@ #include "stdafx.h"
-/*void CDropbox::SendFile(const char *fileName, const char *data, int length)
+void CDropbox::SendFile(const char *fileName, const char *data, int length)
{
-CMStringA url(FORMAT, DROPBOX_APICONTENT_URL "/files_put/%s/%s", DROPBOX_API_ROOT, ptrA(mir_utf8encode(fileName)));
-url.Replace('\\', '/');
+ CMStringA url(FORMAT, DROPBOX_APICONTENT_URL "/files_put/%s/%s", DROPBOX_API_ROOT, ptrA(mir_utf8encode(fileName)));
+ url.Replace('\\', '/');
-HttpRequest *request = new HttpRequest(hNetlibConnection, REQUEST_PUT, DROPBOX_APICONTENT_URL "/files_put");
-request->AddBearerAuthHeader(db_get_sa(NULL, MODULE, "TokenSecret"));
-request->pData = (char*)mir_alloc(sizeof(char)* length);
-memcpy(request->pData, data, length);
-request->dataLength = length;
+ HttpRequest *request = new HttpRequest(hNetlibConnection, REQUEST_PUT, DROPBOX_APICONTENT_URL "/files_put");
+ request->AddBearerAuthHeader(ptrA(db_get_sa(NULL, MODULE, "TokenSecret")));
+ request->pData = (char*)mir_alloc(sizeof(char)* length);
+ memcpy(request->pData, data, length);
+ request->dataLength = length;
-mir_ptr<NETLIBHTTPREQUEST> response(request->Send());
+ mir_ptr<NETLIBHTTPREQUEST> response(request->Send());
-delete request;
+ delete request;
-HandleHttpResponseError(hNetlibConnection, response);
-}*/
+ HandleHttpResponseError(response);
+}
void CDropbox::SendFileChunkedFirst(const char *data, int length, char *uploadId, size_t &offset)
{
HttpRequest *request = new HttpRequest(hNetlibConnection, REQUEST_PUT, DROPBOX_APICONTENT_URL "/chunked_upload");
- request->AddBearerAuthHeader(db_get_sa(NULL, MODULE, "TokenSecret"));
+ request->AddBearerAuthHeader(ptrA(db_get_sa(NULL, MODULE, "TokenSecret")));
request->AddHeader("Content-Type", "application/octet-stream");
request->pData = (char*)mir_alloc(sizeof(char)* length);
memcpy(request->pData, data, length);
@@ -50,7 +50,7 @@ void CDropbox::SendFileChunkedNext(const char *data, int length, const char *upl url.AppendFormat("?upload_id=%s&offset=%i", uploadId, offset);
HttpRequest *request = new HttpRequest(hNetlibConnection, REQUEST_PUT, url);
- request->AddBearerAuthHeader(db_get_sa(NULL, MODULE, "TokenSecret"));
+ request->AddBearerAuthHeader(ptrA(db_get_sa(NULL, MODULE, "TokenSecret")));
request->AddHeader("Content-Type", "application/octet-stream");
request->pData = (char*)mir_alloc(sizeof(char)* length);
memcpy(request->pData, data, length);
@@ -75,13 +75,13 @@ void CDropbox::SendFileChunkedLast(const char *fileName, const char *uploadId) CMStringA url(FORMAT, "%s/commit_chunked_upload/%s/%s", DROPBOX_APICONTENT_URL, DROPBOX_API_ROOT, fileName);
url.Replace('\\', '/');
- CMStringA param = CMStringA("upload_id=") + uploadId;
+ CMStringA data(FORMAT, "upload_id=%s", uploadId);
HttpRequest *request = new HttpRequest(hNetlibConnection, REQUEST_POST, url);
- request->AddBearerAuthHeader(db_get_sa(NULL, MODULE, "TokenSecret"));
+ request->AddBearerAuthHeader(ptrA(db_get_sa(NULL, MODULE, "TokenSecret")));
request->AddHeader("Content-Type", "application/x-www-form-urlencoded");
- request->pData = mir_strdup(param);
- request->dataLength = param.GetLength();
+ request->pData = data.GetBuffer();
+ request->dataLength = data.GetLength();
mir_ptr<NETLIBHTTPREQUEST> response(request->Send());
@@ -95,13 +95,13 @@ void CDropbox::CreateFolder(const char *folderName) CMStringA folder(folderName);
folder.Replace('\\', '/');
- CMStringA param(FORMAT, "root=%s&path=%s", DROPBOX_API_ROOT, folder);
+ CMStringA data(FORMAT, "root=%s&path=%s", DROPBOX_API_ROOT, folder);
HttpRequest *request = new HttpRequest(hNetlibConnection, REQUEST_POST, DROPBOX_API_URL "/fileops/create_folder");
- request->AddBearerAuthHeader(db_get_sa(NULL, MODULE, "TokenSecret"));
+ request->AddBearerAuthHeader(ptrA(db_get_sa(NULL, MODULE, "TokenSecret")));
request->AddHeader("Content-Type", "application/x-www-form-urlencoded");
- request->pData = mir_strdup(param);
- request->dataLength = param.GetLength();
+ request->pData = data.GetBuffer();
+ request->dataLength = data.GetLength();
mir_ptr<NETLIBHTTPREQUEST> response(request->Send());
@@ -123,7 +123,7 @@ void CDropbox::CreateDownloadUrl(const char *path, char *url) api_url += "?short_url=false";
HttpRequest *request = new HttpRequest(hNetlibConnection, REQUEST_POST, api_url);
- request->AddBearerAuthHeader(db_get_sa(NULL, MODULE, "TokenSecret"));
+ request->AddBearerAuthHeader(ptrA(db_get_sa(NULL, MODULE, "TokenSecret")));
mir_ptr<NETLIBHTTPREQUEST> response(request->Send());
@@ -135,7 +135,7 @@ void CDropbox::CreateDownloadUrl(const char *path, char *url) if (root)
{
JSONNODE *node = json_get(root, "url");
- mir_strcpy(url, _T2A(json_as_string(node)));
+ mir_strcpy(url, ptrA(mir_urlEncode(_T2A(json_as_string(node)))));
}
}
@@ -148,11 +148,14 @@ UINT CDropbox::SendFilesAsync(void *owner, void *arg) try
{
- if (ftp->pwszFolders)
+ if (ftp->ptszFolders)
{
- for (int i = 0; ftp->pwszFolders[i]; i++)
+ for (int i = 0; ftp->ptszFolders[i]; i++)
{
- ptrA utf8_folderName(mir_utf8encodeW(ftp->pwszFolders[i]));
+ if(ftp->isTerminated)
+ throw TransferException("Transfer was terminated");
+
+ ptrA utf8_folderName(mir_utf8encodeW(ftp->ptszFolders[i]));
instance->CreateFolder(utf8_folderName);
if (!strchr(utf8_folderName, '\\'))
@@ -166,6 +169,9 @@ UINT CDropbox::SendFilesAsync(void *owner, void *arg) for (int i = 0; ftp->pfts.ptszFiles[i]; i++)
{
+ if (ftp->isTerminated)
+ throw TransferException("Transfer was terminated");
+
FILE *hFile = _tfopen(ftp->pfts.ptszFiles[i], _T("rb"));
if (hFile == NULL)
throw TransferException("Unable to open file");
@@ -183,7 +189,7 @@ UINT CDropbox::SendFilesAsync(void *owner, void *arg) ftp->pfts.currentFileNumber = i;
ftp->pfts.currentFileSize = fileSize;
ftp->pfts.currentFileProgress = 0;
- ftp->pfts.wszCurrentFile = wcsrchr(ftp->pfts.ptszFiles[i], '\\') + 1;
+ ftp->pfts.tszCurrentFile = wcsrchr(ftp->pfts.ptszFiles[i], '\\') + 1;
ProtoBroadcastAck(MODULE, ftp->pfts.hContact, ACKTYPE_FILE, ACKRESULT_DATA, ftp->hProcess, (LPARAM)&ftp->pfts);
@@ -202,6 +208,9 @@ UINT CDropbox::SendFilesAsync(void *owner, void *arg) if (ferror(hFile))
throw TransferException("Error while file sending");
+ if (ftp->isTerminated)
+ throw TransferException("Transfer was terminated");
+
char *data = new char[chunkSize + 1];
int count = (int)fread(data, sizeof(char), chunkSize, hFile);
@@ -226,7 +235,7 @@ UINT CDropbox::SendFilesAsync(void *owner, void *arg) {
char url[MAX_PATH];
instance->CreateDownloadUrl(utf8_fileName, url);
- ftp->AddUrl(url);
+ ftp->AddUrl(url);
}
ftp->pfts.currentFileProgress = ftp->pfts.currentFileSize;
@@ -239,7 +248,7 @@ UINT CDropbox::SendFilesAsync(void *owner, void *arg) {
Netlib_Logf(instance->hNetlibConnection, "%s: %s", MODULE, ex.what());
ProtoBroadcastAck(MODULE, ftp->pfts.hContact, ACKTYPE_FILE, ACKRESULT_FAILED, ftp->hProcess, 0);
- return 0;
+ return ACKRESULT_FAILED;
}
ProtoBroadcastAck(MODULE, ftp->pfts.hContact, ACKTYPE_FILE, ACKRESULT_SUCCESS, ftp->hProcess, 0);
@@ -253,35 +262,27 @@ UINT CDropbox::SendFilesAndReportAsync(void *owner, void *arg) FileTransferParam *ftp = (FileTransferParam*)arg;
int res = SendFilesAsync(owner, arg);
- if (!res)
+ if (res)
{
- CMStringA urls;
- for (int i = 0; i < ftp->urlList.getCount(); i++)
- urls.AppendFormat("%s\r\n", ftp->urlList[i]);
- char *data = urls.GetBuffer();
+ instance->transfers.remove(ftp);
+ delete ftp;
+ return res;
+ }
+
+ CMStringA urls;
+ for (int i = 0; i < ftp->urlList.getCount(); i++)
+ urls.AppendFormat("%s\r\n", ftp->urlList[i]);
+ char *data = urls.GetBuffer();
- if (db_get_b(NULL, MODULE, "UrlAutoSend", 1))
+ if (db_get_b(NULL, MODULE, "UrlAutoSend", 1))
+ {
+ char *message = mir_utf8encode(data);
+ if (ftp->hContact != instance->GetDefaultContact())
{
- char *message = mir_utf8encode(data);
- if (ftp->hContact != instance->GetDefaultContact())
- {
- if (CallContactService(ftp->hContact, PSS_MESSAGE, PREF_UTF, (LPARAM)message) != ACKRESULT_FAILED)
- {
- DBEVENTINFO dbei = { sizeof(dbei) };
- dbei.flags = DBEF_UTF | DBEF_SENT/* | DBEF_READ*/;
- dbei.szModule = MODULE;
- dbei.timestamp = time(NULL);
- dbei.eventType = EVENTTYPE_MESSAGE;
- dbei.cbBlob = (int)mir_strlen(data);
- dbei.pBlob = (PBYTE)message;
- db_event_add(ftp->hContact, &dbei);
- }
- else CallServiceSync(MS_MSG_SENDMESSAGEW, (WPARAM)ftp->hContact, (LPARAM)data);
- }
- else
+ if (CallContactService(ftp->hContact, PSS_MESSAGE, PREF_UTF, (LPARAM)message) != ACKRESULT_FAILED)
{
DBEVENTINFO dbei = { sizeof(dbei) };
- dbei.flags = DBEF_UTF;
+ dbei.flags = DBEF_UTF | DBEF_SENT/* | DBEF_READ*/;
dbei.szModule = MODULE;
dbei.timestamp = time(NULL);
dbei.eventType = EVENTTYPE_MESSAGE;
@@ -289,33 +290,46 @@ UINT CDropbox::SendFilesAndReportAsync(void *owner, void *arg) dbei.pBlob = (PBYTE)message;
db_event_add(ftp->hContact, &dbei);
}
+ else CallServiceSync(MS_MSG_SENDMESSAGE, (WPARAM)ftp->hContact, (LPARAM)data);
+ }
+ else
+ {
+ DBEVENTINFO dbei = { sizeof(dbei) };
+ dbei.flags = DBEF_UTF;
+ dbei.szModule = MODULE;
+ dbei.timestamp = time(NULL);
+ dbei.eventType = EVENTTYPE_MESSAGE;
+ dbei.cbBlob = (int)mir_strlen(data);
+ dbei.pBlob = (PBYTE)message;
+ db_event_add(ftp->hContact, &dbei);
}
+ }
- if (db_get_b(NULL, MODULE, "UrlPasteToMessageInputArea", 0))
- CallServiceSync(MS_MSG_SENDMESSAGE, (WPARAM)ftp->hContact, (LPARAM)data);
+ if (db_get_b(NULL, MODULE, "UrlPasteToMessageInputArea", 0))
+ CallServiceSync(MS_MSG_SENDMESSAGE, (WPARAM)ftp->hContact, (LPARAM)data);
- if (db_get_b(NULL, MODULE, "UrlCopyToClipboard", 0))
+ if (db_get_b(NULL, MODULE, "UrlCopyToClipboard", 0))
+ {
+ if (OpenClipboard(NULL))
{
- if (OpenClipboard(NULL))
+ EmptyClipboard();
+ size_t size = sizeof(TCHAR) * (urls.GetLength() + 1);
+ HGLOBAL hClipboardData = GlobalAlloc(NULL, size);
+ if (hClipboardData)
{
- EmptyClipboard();
- size_t size = sizeof(TCHAR) * (urls.GetLength() + 1);
- HGLOBAL hClipboardData = GlobalAlloc(NULL, size);
- if (hClipboardData)
+ TCHAR *pchData = (TCHAR*)GlobalLock(hClipboardData);
+ if (pchData)
{
- TCHAR *pchData = (TCHAR*)GlobalLock(hClipboardData);
- if (pchData)
- {
- memcpy(pchData, (TCHAR*)data, size);
- GlobalUnlock(hClipboardData);
- SetClipboardData(CF_TEXT, hClipboardData);
- }
+ memcpy(pchData, (TCHAR*)data, size);
+ GlobalUnlock(hClipboardData);
+ SetClipboardData(CF_TEXT, hClipboardData);
}
- CloseClipboard();
}
+ CloseClipboard();
}
}
+ instance->transfers.remove(ftp);
delete ftp;
return res;
@@ -335,6 +349,7 @@ UINT CDropbox::SendFilesAndEventAsync(void *owner, void *arg) NotifyEventHooks(instance->hFileSentEventHook, ftp->hContact, (LPARAM)&ti);
+ instance->transfers.remove(ftp);
delete ftp;
return res;
|