diff options
author | Alexander Lantsev <aunsane@gmail.com> | 2014-03-07 20:30:38 +0000 |
---|---|---|
committer | Alexander Lantsev <aunsane@gmail.com> | 2014-03-07 20:30:38 +0000 |
commit | c2c7b69efb2eac474a191d25f4a609274e87249e (patch) | |
tree | 97344fdb8afffab8e0f0aa7ee83b62b294d42fa0 /plugins/Dropbox | |
parent | 6cf0c508c0291c0622221a48b91bd39684e70dcf (diff) |
Dropbox: added retrieving of account info
git-svn-id: http://svn.miranda-ng.org/main/trunk@8459 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'plugins/Dropbox')
-rw-r--r-- | plugins/Dropbox/src/dropbox.cpp | 181 | ||||
-rw-r--r-- | plugins/Dropbox/src/dropbox.h | 3 | ||||
-rw-r--r-- | plugins/Dropbox/src/dropbox_events.cpp | 2 | ||||
-rw-r--r-- | plugins/Dropbox/src/dropbox_services.cpp | 14 |
4 files changed, 139 insertions, 61 deletions
diff --git a/plugins/Dropbox/src/dropbox.cpp b/plugins/Dropbox/src/dropbox.cpp index e7dc92458c..f364597139 100644 --- a/plugins/Dropbox/src/dropbox.cpp +++ b/plugins/Dropbox/src/dropbox.cpp @@ -58,69 +58,154 @@ bool CDropbox::HasAccessToken() return db_get_sa(NULL, MODULE, "TokenSecret") != NULL;
}
-void CDropbox::RequestAcceessToken()
+//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);
+//
+// char request_token[128] = { 0 };
+//
+// if (DialogBoxParam(
+// g_hInstance,
+// MAKEINTRESOURCE(IDD_TOKEN_REQUEST),
+// NULL,
+// CDropbox::TokenRequestProc,
+// (LPARAM)&request_token) == IDOK)
+// {
+// char data[1024];
+// mir_snprintf(
+// data,
+// SIZEOF(data),
+// "grant_type=authorization_code&code=%s",
+// request_token);
+//
+// HttpRequest *request = new HttpRequest(hNetlibUser, REQUEST_POST, DROPBOX_API_URL "/oauth2/token");
+// request->pData = mir_strdup(data);
+// request->dataLength = (int)strlen(data);
+// request->AddHeader("Content-Type", "application/x-www-form-urlencoded");
+// request->AddBasicAuthHeader(DROPBOX_API_KEY, DROPBOX_API_SECRET);
+//
+// mir_ptr<NETLIBHTTPREQUEST> response(request->Send());
+//
+// delete request;
+//
+// MCONTACT hContact = CDropbox::GetDefaultContact();
+//
+// if (response)
+// {
+// JSONNODE *root = json_parse(response->pData);
+// if (root)
+// {
+// if (response->resultCode == HTTP_STATUS_OK)
+// {
+// JSONNODE *node = json_get(root, "access_token");
+// ptrA access_token = ptrA(mir_u2a(json_as_string(node)));
+// db_set_s(NULL, MODULE, "TokenSecret", access_token);
+//
+// if (hContact)
+// {
+// 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("You have been authorized"), MB_ICONINFORMATION);
+// }
+// else
+// {
+// JSONNODE *node = json_get(root, "error_description");
+// ptrW error_description(json_as_string(node));
+//
+// CDropbox::ShowNotification((wchar_t*)error_description, MB_ICONERROR);
+// }
+// }
+// }
+// else
+// HandleFileTransferError(response, hContact);
+// }
+//}
+
+void CDropbox::RequestAccountInfo()
{
- ShellExecuteA(NULL, "open", DROPBOX_WWW_URL DROPBOX_API_VER "/oauth2/authorize?response_type=code&client_id=" DROPBOX_API_KEY, NULL, NULL, SW_SHOWDEFAULT);
-
- char request_token[128] = { 0 };
-
- if (DialogBoxParam(
- g_hInstance,
- MAKEINTRESOURCE(IDD_TOKEN_REQUEST),
- NULL,
- CDropbox::TokenRequestProc,
- (LPARAM)&request_token) == IDOK)
- {
- char data[1024];
- mir_snprintf(
- data,
- SIZEOF(data),
- "grant_type=authorization_code&code=%s",
- request_token);
-
- HttpRequest *request = new HttpRequest(hNetlibUser, REQUEST_POST, DROPBOX_API_URL "/oauth2/token");
- request->pData = mir_strdup(data);
- request->dataLength = (int)strlen(data);
- request->AddHeader("Content-Type", "application/x-www-form-urlencoded");
- request->AddBasicAuthHeader(DROPBOX_API_KEY, DROPBOX_API_SECRET);
-
- mir_ptr<NETLIBHTTPREQUEST> response(request->Send());
+ HttpRequest *request = new HttpRequest(hNetlibUser, REQUEST_GET, DROPBOX_API_URL "/account/info");
+ request->AddBearerAuthHeader(db_get_sa(NULL, MODULE, "TokenSecret"));
+ mir_ptr<NETLIBHTTPREQUEST> response(request->Send());
- delete request;
+ delete request;
- MCONTACT hContact = CDropbox::GetDefaultContact();
+ MCONTACT hContact = CDropbox::GetDefaultContact();
- if (response)
+ if (response)
+ {
+ JSONNODE *root = json_parse(response->pData);
+ if (root)
{
- JSONNODE *root = json_parse(response->pData);
- if (root)
+ if (response->resultCode == HTTP_STATUS_OK)
{
- if (response->resultCode == HTTP_STATUS_OK)
+ JSONNODE *node = json_get(root, "referral_link");
+ if (node)
{
- JSONNODE *node = json_get(root, "access_token");
- ptrA access_token = ptrA(mir_u2a(json_as_string(node)));
- db_set_s(NULL, MODULE, "TokenSecret", access_token);
+ ptrW referral_link = ptrW(json_as_string(node));
+ db_set_ws(hContact, MODULE, "Homepage", referral_link);
+ }
- if (hContact)
+ node = json_get(root, "display_name");
+ if (node)
+ {
+ ptrW display_name = ptrW(json_as_string(node));
+ wchar_t *sep = wcsrchr(display_name, L' ');
+ if (sep)
{
- if (db_get_w(hContact, MODULE, "Status", ID_STATUS_OFFLINE) == ID_STATUS_OFFLINE)
- db_set_w(hContact, MODULE, "Status", ID_STATUS_ONLINE);
+ db_set_ws(hContact, MODULE, "LastName", sep + 1);
+ display_name[wcslen(display_name) - wcslen(sep)] = '\0';
+ db_set_ws(hContact, MODULE, "FirstName", display_name);
+ }
+ else
+ {
+ db_set_ws(hContact, MODULE, "FirstName", display_name);
+ db_unset(hContact, MODULE, "LastName");
}
-
- CDropbox::ShowNotification(TranslateT("You have been authorized"), MB_ICONINFORMATION);
}
- else
+
+ node = json_get(root, "country");
+ if (node)
{
- JSONNODE *node = json_get(root, "error_description");
- ptrW error_description(json_as_string(node));
+ ptrW isocodeW(json_as_string(node));
+ ptrA isocode(mir_u2a(isocodeW));
- CDropbox::ShowNotification((wchar_t*)error_description, MB_ICONERROR);
+ if (!strlen(isocode))
+ db_unset(hContact, MODULE, "Country");
+ else
+ {
+ char *country = (char *)CallService(MS_UTILS_GETCOUNTRYBYISOCODE, (WPARAM)isocode, 0);
+ db_set_s(hContact, MODULE, "Country", country);
+ }
+ }
+
+ node = json_get(root, "quota_info");
+ root = json_as_node(node);
+ if (root)
+ {
+ node = json_get(root, "shared");
+ if (node)
+ db_set_dw(hContact, MODULE, "SharedQuota", json_as_int(node));
+ node = json_get(root, "normal");
+ if (node)
+ db_set_dw(hContact, MODULE, "NormalQuota", json_as_int(node));
+ node = json_get(root, "quota");
+ if (node)
+ db_set_dw(hContact, MODULE, "TotalQuota", json_as_int(node));
}
}
+ /*else
+ {
+ JSONNODE *node = json_get(root, "error_description");
+ ptrW error_description(json_as_string(node));
+
+ CDropbox::ShowNotification((wchar_t*)error_description, MB_ICONERROR);
+ }*/
}
- else
- HandleFileTransferError(response, hContact);
}
+ //else
+ // HandleFileTransferError(response, hContact);
}
void CDropbox::DestroyAcceessToken()
@@ -190,6 +275,8 @@ UINT CDropbox::RequestAcceessTokenAsync(void *owner, void* param) db_set_w(hContact, MODULE, "Status", ID_STATUS_ONLINE);
}
+ instance->RequestAccountInfo();
+
if (hwndDlg)
SetDlgItemText(hwndDlg, IDC_AUTH_STATUS, TranslateT("you have been authorized"));
else
@@ -212,7 +299,7 @@ UINT CDropbox::RequestAcceessTokenAsync(void *owner, void* param) if (hwndDlg)
SetDlgItemText(hwndDlg, IDC_AUTH_STATUS, TranslateT("unknown error"));
else
- HandleFileTransferError(response, hContact);
+ HandleFileTransferError(response, hContact);
}
SetDlgItemTextA(hwndDlg, IDC_REQUEST_CODE, "");
diff --git a/plugins/Dropbox/src/dropbox.h b/plugins/Dropbox/src/dropbox.h index 3864dc3548..c920e79e7b 100644 --- a/plugins/Dropbox/src/dropbox.h +++ b/plugins/Dropbox/src/dropbox.h @@ -85,6 +85,9 @@ private: static UINT RequestAcceessTokenAsync(void *owner, void* param);
+ // account info
+ void RequestAccountInfo();
+
// transrers
static int HandleFileTransferError(NETLIBHTTPREQUEST *response, MCONTACT hContact);
diff --git a/plugins/Dropbox/src/dropbox_events.cpp b/plugins/Dropbox/src/dropbox_events.cpp index 325a328152..6562457131 100644 --- a/plugins/Dropbox/src/dropbox_events.cpp +++ b/plugins/Dropbox/src/dropbox_events.cpp @@ -15,6 +15,8 @@ int CDropbox::OnModulesLoaded(void *obj, WPARAM wParam, LPARAM lParam) instance->hNetlibUser = (HANDLE)CallService(MS_NETLIB_REGISTERUSER, 0, (LPARAM)&nlu);
+ instance->GetDefaultContact();
+
if (ServiceExists(MS_BB_ADDBUTTON))
{
BBButton bbd = { sizeof(bbd) };
diff --git a/plugins/Dropbox/src/dropbox_services.cpp b/plugins/Dropbox/src/dropbox_services.cpp index 4a28527423..84c9f6b5b7 100644 --- a/plugins/Dropbox/src/dropbox_services.cpp +++ b/plugins/Dropbox/src/dropbox_services.cpp @@ -172,20 +172,6 @@ INT_PTR CDropbox::ProtoReceiveMessage(void *obj, WPARAM, LPARAM lParam) return 0;
}
-//INT_PTR CDropbox::RequestApiAuthorization(WPARAM, LPARAM)
-//{
-// mir_forkthread(CDropbox::RequestApiAuthorizationAsync, 0);
-//
-// return 0;
-//}
-//
-//INT_PTR CDropbox::RevokeApiAuthorization(WPARAM, LPARAM)
-//{
-// mir_forkthread(CDropbox::RevokeApiAuthorizationAsync, 0);
-//
-// return 0;
-//}
-
INT_PTR CDropbox::SendFilesToDropbox(void *obj, WPARAM hContact, LPARAM)
{
CDropbox *instance = (CDropbox*)obj;
|