From 9fbb540928c2584128bdd6361d60ee57f315a0f6 Mon Sep 17 00:00:00 2001 From: Alexander Lantsev Date: Fri, 21 Feb 2014 19:37:00 +0000 Subject: insignificant changes git-svn-id: http://svn.miranda-ng.org/main/trunk@8203 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/Dropbox/src/dropbox.cpp | 31 ++++------- plugins/Dropbox/src/dropbox.h | 15 +++--- plugins/Dropbox/src/dropbox_menus.cpp | 17 ++++-- plugins/Dropbox/src/dropbox_services.cpp | 88 +++++++++++++++++++++++++++---- plugins/Dropbox/src/dropbox_transfers.cpp | 19 ++++--- plugins/Dropbox/src/http_request.h | 42 +++++++++++++++ 6 files changed, 162 insertions(+), 50 deletions(-) (limited to 'plugins/Dropbox') diff --git a/plugins/Dropbox/src/dropbox.cpp b/plugins/Dropbox/src/dropbox.cpp index d6a6bd65d9..9bbee6b9f5 100644 --- a/plugins/Dropbox/src/dropbox.cpp +++ b/plugins/Dropbox/src/dropbox.cpp @@ -10,9 +10,9 @@ void CDropbox::Init() HookEvent(ME_OPT_INITIALISE, OnOptionsInit); HookEvent(ME_SYSTEM_MODULESLOADED, CDropbox::OnModulesLoaded); - CreateProtoServiceFunction(MODULE, PS_GETCAPS, CDropbox::GetCaps); - CreateProtoServiceFunction(MODULE, PSS_FILE, CDropbox::SendFile); - CreateProtoServiceFunction(MODULE, PSS_MESSAGE, CDropbox::SendMessage); + CreateProtoServiceFunction(MODULE, PS_GETCAPS, CDropbox::ProtoGetCaps); + CreateProtoServiceFunction(MODULE, PSS_FILE, CDropbox::ProtoSendFile); + CreateProtoServiceFunction(MODULE, PSS_MESSAGE, CDropbox::ProtoSendMessage); InitIcons(); InitMenus(); @@ -41,20 +41,10 @@ void CDropbox::RequestAcceessToken(MCONTACT hContact) CDropbox::TokenRequestProc, (LPARAM)&request_token) == IDOK) { - char data[64]; - mir_snprintf( - data, - SIZEOF(data), - "client_id=%s&client_secret=%s", - DROPBOX_API_KEY, - DROPBOX_API_SECRET); - HttpRequest *request = new HttpRequest(hNetlibUser, REQUEST_POST, DROPBOX_API_URL "/oauth2/token"); request->AddParameter("grant_type", "authorization_code"); request->AddParameter("code", request_token); - request->AddHeader("Content-Type", "application/x-www-form-urlencoded"); - request->pData = mir_strdup(data); - request->dataLength = strlen(data); + request->AddBasicAuthHeader(DROPBOX_API_KEY, DROPBOX_API_SECRET); NETLIBHTTPREQUEST *response = request->Send(); @@ -122,18 +112,19 @@ void CDropbox::DestroyAcceessToken(MCONTACT hContact) } } -void CDropbox::RequeriedAccessAsync(void *arg) +void CDropbox::RequestApiAuthorizationAsync(void *arg) { MCONTACT hContact = (MCONTACT)arg; - if (hContact && MessageBox( + if (HasAccessToken() && MessageBox( NULL, - TranslateT("Are you sure you want to requeried access?"), - TranslateT("Requeried access"), + TranslateT("Are you sure you want to request athorization?"), + TranslateT("Request athorization"), MB_YESNO | MB_ICONQUESTION) == IDYES) { - if (HasAccessToken()) - Singleton::GetInstance()->DestroyAcceessToken(hContact); + Singleton::GetInstance()->DestroyAcceessToken(hContact); Singleton::GetInstance()->RequestAcceessToken(hContact); } + else + Singleton::GetInstance()->RequestAcceessToken(hContact); } \ No newline at end of file diff --git a/plugins/Dropbox/src/dropbox.h b/plugins/Dropbox/src/dropbox.h index c2aed06ec8..09f162302f 100644 --- a/plugins/Dropbox/src/dropbox.h +++ b/plugins/Dropbox/src/dropbox.h @@ -17,7 +17,8 @@ enum { - CMI_API_ACCESS_REQUERIED, + CMI_API_REQUEST_AUTH, + CMI_SEND_FILES, CMI_MAX // this item shall be the last one }; @@ -88,11 +89,13 @@ private: static int OnOptionsInit(WPARAM wParam, LPARAM lParam); // services - static INT_PTR GetCaps(WPARAM wParam, LPARAM lParam); - static INT_PTR SendFile(WPARAM wParam, LPARAM lParam); - static INT_PTR SendMessage(WPARAM wParam, LPARAM lParam); + static INT_PTR ProtoGetCaps(WPARAM wParam, LPARAM lParam); + static INT_PTR ProtoSendFile(WPARAM wParam, LPARAM lParam); + static INT_PTR ProtoSendMessage(WPARAM wParam, LPARAM lParam); - static INT_PTR RequeriedApiAccess(WPARAM wParam, LPARAM lParam); + static INT_PTR RequestApiAuthorization(WPARAM wParam, LPARAM lParam); + + static INT_PTR SendFilesToDropbox(WPARAM wParam, LPARAM lParam); // access token static bool HasAccessToken(); @@ -100,7 +103,7 @@ private: void RequestAcceessToken(MCONTACT hContact); void DestroyAcceessToken(MCONTACT hContact); - static void RequeriedAccessAsync(void *arg); + static void RequestApiAuthorizationAsync(void *arg); // transrers HttpRequest *CreateFileSendChunkedRequest(const char *data, int length); diff --git a/plugins/Dropbox/src/dropbox_menus.cpp b/plugins/Dropbox/src/dropbox_menus.cpp index 7ae155bcf5..aff00f4187 100644 --- a/plugins/Dropbox/src/dropbox_menus.cpp +++ b/plugins/Dropbox/src/dropbox_menus.cpp @@ -8,10 +8,17 @@ void CDropbox::InitMenus() mi.cbSize = sizeof(CLISTMENUITEM); mi.flags = CMIF_TCHAR; - mi.pszService = MODULE"/RequeriedAccess"; - mi.ptszName = LPGENT("Requeried access"); - mi.position = -201001000 + CMI_API_ACCESS_REQUERIED; + /*mi.pszService = MODULE"/SendFilesToDropbox"; + mi.ptszName = LPGENT("Send files to Dropbox"); + mi.position = -201000000 + 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.icolibItem = LoadSkinnedIconHandle(SKINICON_AUTH_REQUEST); - ContactMenuItems[CMI_API_ACCESS_REQUERIED] = Menu_AddContactMenuItem(&mi); - CreateServiceFunction(mi.pszService, RequeriedApiAccess); + ContactMenuItems[CMI_API_REQUEST_AUTH] = Menu_AddContactMenuItem(&mi); + CreateServiceFunction(mi.pszService, RequestApiAuthorization); } \ No newline at end of file diff --git a/plugins/Dropbox/src/dropbox_services.cpp b/plugins/Dropbox/src/dropbox_services.cpp index 124a76b4e3..c485779737 100644 --- a/plugins/Dropbox/src/dropbox_services.cpp +++ b/plugins/Dropbox/src/dropbox_services.cpp @@ -1,6 +1,6 @@ #include "common.h" -INT_PTR CDropbox::GetCaps(WPARAM wParam, LPARAM lParam) +INT_PTR CDropbox::ProtoGetCaps(WPARAM wParam, LPARAM lParam) { switch(wParam) { @@ -19,7 +19,7 @@ INT_PTR CDropbox::GetCaps(WPARAM wParam, LPARAM lParam) return 0; } -INT_PTR CDropbox::SendFile(WPARAM wParam, LPARAM lParam) +INT_PTR CDropbox::ProtoSendFile(WPARAM wParam, LPARAM lParam) { CCSDATA *pccsd = (CCSDATA*)lParam; @@ -60,12 +60,7 @@ INT_PTR CDropbox::SendFile(WPARAM wParam, LPARAM lParam) } else { - //if (!relativePathStart) - ftp->pfts.pszFiles[k] = mir_strdup(files[i]); - /*else - { - ftp->pfts.pszFiles[k] = mir_strdup(&files[i][relativePathStart]); - }*/ + ftp->pfts.pszFiles[k] = mir_strdup(files[i]); FILE *file = fopen(files[i], "rb"); if (file != NULL) @@ -86,14 +81,85 @@ INT_PTR CDropbox::SendFile(WPARAM wParam, LPARAM lParam) return fileId; } -INT_PTR CDropbox::SendMessage( WPARAM wParam, LPARAM lParam) +INT_PTR CDropbox::ProtoSendMessage( WPARAM wParam, LPARAM lParam) +{ + return 0; +} + +INT_PTR CDropbox::RequestApiAuthorization(WPARAM wParam, LPARAM lParam) { + mir_forkthread(CDropbox::RequestApiAuthorizationAsync, (void*)wParam); + return 0; } -INT_PTR CDropbox::RequeriedApiAccess(WPARAM wParam, LPARAM lParam) +INT_PTR CDropbox::SendFilesToDropbox(WPARAM wParam, LPARAM lParam) { - mir_forkthread(CDropbox::RequeriedAccessAsync, (void*)wParam); + TCHAR filter[128], *pfilter; + wchar_t buffer[4096] = {0}; + + OPENFILENAME ofn = {0}; + ofn.lStructSize = OPENFILENAME_SIZE_VERSION_400; + //ofn.hwndOwner = hwndDlg; + lstrcpy(filter, TranslateT("All files")); + lstrcat(filter, _T(" (*)")); + pfilter = filter + lstrlen(filter)+1; + lstrcpy(pfilter, _T("*")); + pfilter = filter + lstrlen(filter)+1; + pfilter[ 0 ] = '\0'; + ofn.lpstrFilter = filter; + ofn.lpstrFile = buffer; + ofn.nMaxFile = 4096; + ofn.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_ALLOWMULTISELECT | OFN_EXPLORER | OFN_NOCHANGEDIR | OFN_HIDEREADONLY | OFN_DONTADDTORECENT; + if (GetOpenFileName(&ofn)) + { + wchar_t **files = NULL; + + if (buffer[ofn.nFileOffset - 1] != '\0') + { + // Single-Select + files = (wchar_t**)mir_alloc(sizeof(wchar_t*) * 2); + + files[0] = mir_wstrdup(buffer); + files[1] = NULL; + } + else + { + // Multi-Select + int i = 0; + wchar_t *p = buffer; + + while (*p) + { + p += lstrlen(p) + 1; + i++; + } + + files = (wchar_t**)mir_alloc(sizeof(wchar_t*) * i); + p = buffer; i = 0; + + while (*p) + { + p += lstrlen(p) + 1; + if (lstrlen(p) > 0) + { + int len = lstrlen(buffer) + lstrlen(p) + 1; + files[i] = (wchar_t*)mir_alloc(sizeof(wchar_t) * len); + lstrcpy(files[i], buffer); + lstrcat(files[i], L"\\"); + lstrcat(files[i], p); + files[i++][len] = '\0'; + } + } + files[i] = NULL; + } + + /*char *cHandle = (char*)wParam; + char hHandle[16]= { 0 }; + strcat(&hHandle[1], cHandle);*/ + + CallContactService(wParam, PSS_FILET, wParam, (LPARAM)files); + } 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 519e191479..4d5dd7c2e6 100644 --- a/plugins/Dropbox/src/dropbox_transfers.cpp +++ b/plugins/Dropbox/src/dropbox_transfers.cpp @@ -3,7 +3,7 @@ HttpRequest *CDropbox::CreateFileSendChunkedRequest(const char *data, int length) { HttpRequest *request = new HttpRequest(hNetlibUser, REQUEST_PUT, DROPBOX_APICONTENT_URL "/chunked_upload"); - request->AddParameter("access_token", db_get_sa(NULL, MODULE, "TokenSecret")); + request->AddBearerAuthHeader(db_get_sa(NULL, MODULE, "TokenSecret")); if (length > 0) { request->AddHeader("Content-Type", "application/octet-stream"); @@ -86,7 +86,7 @@ void CDropbox::SendFileChunkedLast(const char *fileName, const char *uploadId, M HttpRequest *request = new HttpRequest(hNetlibUser, REQUEST_POST, url); request->AddParameter("upload_id", uploadId); - request->AddParameter("access_token", db_get_sa(NULL, MODULE, "TokenSecret")); + request->AddBearerAuthHeader(db_get_sa(NULL, MODULE, "TokenSecret")); NETLIBHTTPREQUEST *response = request->Send(); @@ -100,7 +100,7 @@ void CDropbox::SendFileChunkedLast(const char *fileName, const char *uploadId, M url.Replace("commit_chunked_upload", "shares"); request = new HttpRequest(hNetlibUser, REQUEST_POST, url); - request->AddParameter("access_token", db_get_sa(NULL, MODULE, "TokenSecret")); + request->AddBearerAuthHeader(db_get_sa(NULL, MODULE, "TokenSecret")); mir_free(response); @@ -152,7 +152,7 @@ void CDropbox::CreateFolder(const char *folderName, MCONTACT hContact) HttpRequest *request = new HttpRequest(hNetlibUser, REQUEST_POST, DROPBOX_API_URL "/fileops/create_folder"); request->AddParameter("root", DROPBOX_API_ROOT); request->AddParameter("path", folderName); - request->AddParameter("access_token", db_get_sa(NULL, MODULE, "TokenSecret")); + request->AddBearerAuthHeader(db_get_sa(NULL, MODULE, "TokenSecret")); NETLIBHTTPREQUEST *response = request->Send(); @@ -172,7 +172,7 @@ void CDropbox::CreateFolder(const char *folderName, MCONTACT hContact) folder.GetBuffer()); request = new HttpRequest(hNetlibUser, REQUEST_POST, url); - request->AddParameter("access_token", db_get_sa(NULL, MODULE, "TokenSecret")); + request->AddBearerAuthHeader(db_get_sa(NULL, MODULE, "TokenSecret")); mir_free(response); @@ -233,8 +233,11 @@ void _cdecl CDropbox::SendFileAsync(void *arg) int offset = 0; char *uploadId = new char[32]; - //const char *fileName = strrchr(ftp->pfts.pszFiles[i], '\\') + 1; - const char *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); @@ -256,7 +259,7 @@ void _cdecl CDropbox::SendFileAsync(void *arg) chunkSize = DROPBOX_FILE_CHUNK_SIZE * 4; char *data = new char[chunkSize + 1]; - size_t count = fread(data, sizeof(char), chunkSize, file); + int count = fread(data, sizeof(char), chunkSize, file); if (!offset) Singleton::GetInstance()->SendFileChunkedFirst(data, count, uploadId, offset); diff --git a/plugins/Dropbox/src/http_request.h b/plugins/Dropbox/src/http_request.h index 43b1b311ec..81a6dcd99d 100644 --- a/plugins/Dropbox/src/http_request.h +++ b/plugins/Dropbox/src/http_request.h @@ -41,6 +41,48 @@ public: headersCount++; } + void AddBasicAuthHeader(LPCSTR szLogin, LPCSTR szPassword) + { + char cPair[128]; + mir_snprintf( + cPair, + SIZEOF(cPair), + "%s:%s", + szLogin, + szPassword); + + char *ePair = (char *)mir_base64_encode((BYTE*)cPair, strlen(cPair)); + + char value[128]; + mir_snprintf( + value, + SIZEOF(value), + "Basic %s", + ePair); + + mir_free(ePair); + + headers = (NETLIBHTTPHEADER*)mir_realloc(headers, sizeof(NETLIBHTTPHEADER)*(headersCount+1)); + headers[headersCount].szName = mir_strdup("Authorization"); + headers[headersCount].szValue = mir_strdup(value); + headersCount++; + } + + void AddBearerAuthHeader(LPCSTR szValue) + { + char value[128]; + mir_snprintf( + value, + SIZEOF(value), + "Bearer %s", + szValue); + + headers = (NETLIBHTTPHEADER*)mir_realloc(headers, sizeof(NETLIBHTTPHEADER)*(headersCount+1)); + headers[headersCount].szName = mir_strdup("Authorization"); + headers[headersCount].szValue = mir_strdup(value); + headersCount++; + } + void AddParameter(LPCSTR szName, LPCSTR szValue) { if (m_szUrl.Find('?') == -1) -- cgit v1.2.3