summaryrefslogtreecommitdiff
path: root/plugins/Dropbox/src/dropbox.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/Dropbox/src/dropbox.cpp')
-rw-r--r--plugins/Dropbox/src/dropbox.cpp149
1 files changed, 107 insertions, 42 deletions
diff --git a/plugins/Dropbox/src/dropbox.cpp b/plugins/Dropbox/src/dropbox.cpp
index b52e80c166..e7dc92458c 100644
--- a/plugins/Dropbox/src/dropbox.cpp
+++ b/plugins/Dropbox/src/dropbox.cpp
@@ -1,46 +1,56 @@
#include "common.h"
-std::map<HWND, MCONTACT> CDropbox::dcftp;
-std::map<std::string, pThreadFunc> CDropbox::commands;
-HGENMENU CDropbox::ContactMenuItems[CMI_MAX];
-
-void CDropbox::Init()
+CDropbox::CDropbox()
{
PROTOCOLDESCRIPTOR pd = { PROTOCOLDESCRIPTOR_V3_SIZE };
pd.szName = MODULE;
pd.type = PROTOTYPE_VIRTUAL;
CallService(MS_PROTO_REGISTERMODULE, 0, (LPARAM)&pd);
- HookEvent(ME_OPT_INITIALISE, OnOptionsInit);
- HookEvent(ME_SYSTEM_PRESHUTDOWN, OnPreShutdown);
- HookEvent(ME_SYSTEM_MODULESLOADED, CDropbox::OnModulesLoaded);
- HookEvent(ME_DB_CONTACT_DELETED, CDropbox::OnContactDeleted);
- HookEvent(ME_CLIST_PREBUILDCONTACTMENU, CDropbox::OnPrebuildContactMenu);
+ HookEventObj(ME_SYSTEM_PRESHUTDOWN, OnPreShutdown, this);
+ HookEventObj(ME_SYSTEM_MODULESLOADED, OnModulesLoaded, this);
+ HookEventObj(ME_DB_CONTACT_DELETED, OnContactDeleted, this);
+ HookEventObj(ME_OPT_INITIALISE, OnOptionsInitialized, this);
+ HookEventObj(ME_CLIST_PREBUILDCONTACTMENU, OnPrebuildContactMenu, this);
- CreateProtoServiceFunction(MODULE, PS_GETCAPS, CDropbox::ProtoGetCaps);
- CreateProtoServiceFunction(MODULE, PSS_FILE, CDropbox::ProtoSendFile);
- CreateProtoServiceFunction(MODULE, PSS_MESSAGE, CDropbox::ProtoSendMessage);
- CreateProtoServiceFunction(MODULE, PSR_MESSAGE, CDropbox::ProtoReceiveMessage);
+ CreateProtoServiceFunction(MODULE, PS_GETCAPS, ProtoGetCaps);
+ CreateProtoServiceFunctionObj(PSS_FILE, ProtoSendFile, this);
+ CreateProtoServiceFunctionObj(PSS_MESSAGE, ProtoSendMessage, this);
+ CreateProtoServiceFunctionObj(PSR_MESSAGE, ProtoReceiveMessage, this);
- InitIcons();
- InitMenus();
+ InitializeIcons();
+ InitializeMenus();
+ commands["help"] = CDropbox::CommandHelp;
commands["content"] = CDropbox::CommandContent;
commands["share"] = CDropbox::CommandShare;
commands["delete"] = CDropbox::CommandDelete;
- hContactTransfer = 0;
hFileProcess = hMessageProcess = 1;
+ hDefaultContact = hTransferContact = 0;
}
-MCONTACT CDropbox::hContactDefault = 0;
-
MCONTACT CDropbox::GetDefaultContact()
{
- if (!hContactDefault)
- hContactDefault = db_find_first(MODULE);
+ if (!hDefaultContact)
+ hDefaultContact = db_find_first(MODULE);
+
+ if (!hDefaultContact)
+ {
+ hDefaultContact = (MCONTACT)CallService(MS_DB_CONTACT_ADD, 0, 0);
+ if (!CallService(MS_PROTO_ADDTOCONTACT, hDefaultContact, (LPARAM)MODULE))
+ {
+ db_set_s(NULL, MODULE, "Nick", MODULE);
+ db_set_s(hDefaultContact, MODULE, "Nick", MODULE);
+ db_set_ws(hDefaultContact, "CList", "MyHandle", L"Dropbox");
- return hContactDefault;
+ int status = db_get_w(hDefaultContact, MODULE, "Status", ID_STATUS_OFFLINE);
+ if (HasAccessToken() && status == ID_STATUS_OFFLINE)
+ db_set_w(hDefaultContact, MODULE, "Status", ID_STATUS_ONLINE);
+ }
+ }
+
+ return hDefaultContact;
}
bool CDropbox::HasAccessToken()
@@ -57,7 +67,7 @@ void CDropbox::RequestAcceessToken()
if (DialogBoxParam(
g_hInstance,
MAKEINTRESOURCE(IDD_TOKEN_REQUEST),
- NULL,
+ NULL,
CDropbox::TokenRequestProc,
(LPARAM)&request_token) == IDOK)
{
@@ -85,7 +95,7 @@ void CDropbox::RequestAcceessToken()
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, "access_token");
ptrA access_token = ptrA(mir_u2a(json_as_string(node)));
@@ -130,27 +140,82 @@ void CDropbox::DestroyAcceessToken()
}
}
-void CDropbox::RequestApiAuthorizationAsync(void *arg)
+UINT CDropbox::RequestAcceessTokenAsync(void *owner, void* param)
{
- if (HasAccessToken() && MessageBox(
- NULL,
- TranslateT("Are you sure you want to request authorization?"),
- TranslateT("Request authorization"),
- MB_YESNO | MB_ICONQUESTION) == IDYES)
+ HWND hwndDlg = (HWND)param;
+ CDropbox *instance = (CDropbox*)owner;
+
+ EnableWindow(GetDlgItem(hwndDlg, IDC_AUTHORIZE), FALSE);
+ SetDlgItemText(hwndDlg, IDC_AUTH_STATUS, TranslateT("in process..."));
+
+ if (instance->HasAccessToken())
+ instance->DestroyAcceessToken();
+
+ char requestToken[128];
+ GetDlgItemTextA(hwndDlg, IDC_REQUEST_CODE, requestToken, SIZEOF(requestToken));
+
+ char data[1024];
+ mir_snprintf(
+ data,
+ SIZEOF(data),
+ "grant_type=authorization_code&code=%s",
+ requestToken);
+
+ HttpRequest *request = new HttpRequest(instance->hNetlibUser, REQUEST_POST, DROPBOX_API_URL "/oauth2/token");
+ request->AddBasicAuthHeader(DROPBOX_API_KEY, DROPBOX_API_SECRET);
+ request->AddHeader("Content-Type", "application/x-www-form-urlencoded");
+ request->pData = mir_strdup(data);
+ request->dataLength = (int)strlen(data);
+
+ mir_ptr<NETLIBHTTPREQUEST> response(request->Send());
+
+ delete request;
+
+ MCONTACT hContact = instance->GetDefaultContact();
+
+ if (response)
{
- INSTANCE->DestroyAcceessToken();
- INSTANCE->RequestAcceessToken();
+ 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);
+ }
+
+ if (hwndDlg)
+ SetDlgItemText(hwndDlg, IDC_AUTH_STATUS, TranslateT("you have been authorized"));
+ else
+ ShowNotification(TranslateT("you have been authorized"), MB_ICONINFORMATION);
+ }
+ else
+ {
+ JSONNODE *node = json_get(root, "error_description");
+ ptrW error_description(json_as_string(node));
+
+ if (hwndDlg)
+ SetDlgItemText(hwndDlg, IDC_AUTH_STATUS, error_description);
+ else
+ ShowNotification((wchar_t*)error_description, MB_ICONERROR);
+ }
+ }
}
else
- INSTANCE->RequestAcceessToken();
-}
+ {
+ if (hwndDlg)
+ SetDlgItemText(hwndDlg, IDC_AUTH_STATUS, TranslateT("unknown error"));
+ else
+ HandleFileTransferError(response, hContact);
+ }
-void CDropbox::RevokeApiAuthorizationAsync(void *arg)
-{
- if (HasAccessToken() && MessageBox(
- NULL,
- TranslateT("Are you sure you want to revoke authorization?"),
- TranslateT("Revoke authorization"),
- MB_YESNO | MB_ICONQUESTION) == IDYES)
- INSTANCE->DestroyAcceessToken();
+ SetDlgItemTextA(hwndDlg, IDC_REQUEST_CODE, "");
+
+ return 0;
} \ No newline at end of file