summaryrefslogtreecommitdiff
path: root/plugins/Dropbox/src/dropbox.cpp
blob: 9bbee6b9f5560524dccb6a0433c1d07fe9f7ee99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "common.h"

void CDropbox::Init()
{
	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_MODULESLOADED, CDropbox::OnModulesLoaded);

	CreateProtoServiceFunction(MODULE, PS_GETCAPS, CDropbox::ProtoGetCaps);
	CreateProtoServiceFunction(MODULE, PSS_FILE, CDropbox::ProtoSendFile);
	CreateProtoServiceFunction(MODULE, PSS_MESSAGE, CDropbox::ProtoSendMessage);

	InitIcons();
	InitMenus();
}

MCONTACT CDropbox::GetDefaultContact()
{
	return db_find_first(MODULE);
}

bool CDropbox::HasAccessToken()
{
	return db_get_sa(NULL, MODULE, "TokenSecret") != NULL;
}

void CDropbox::RequestAcceessToken(MCONTACT hContact)
{
	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)
	{
		HttpRequest *request = new HttpRequest(hNetlibUser, REQUEST_POST, DROPBOX_API_URL "/oauth2/token");
		request->AddParameter("grant_type", "authorization_code");
		request->AddParameter("code", request_token);
		request->AddBasicAuthHeader(DROPBOX_API_KEY, DROPBOX_API_SECRET);

		NETLIBHTTPREQUEST *response = request->Send();

		delete request;

		if (response)
		{
			if (response->resultCode == HttpStatus::OK)
			{
				JSONNODE *root = json_parse(response->pData);
				if (root != NULL)
				{
					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)
					{
						node = json_get(root, "uid");
						wchar_t *uid = json_as_string(node);
						db_set_ws(hContact, MODULE, "uid", uid);
						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("Access request"), TranslateT("Access granted"), MB_ICONINFORMATION);

					delete node;
					delete root;
				}
			}
			else
			{
				JSONNODE *root = json_parse(response->pData);
				if (root != NULL)
				{
					JSONNODE *node = json_get(root, "error_description");
					wchar_t *error_description = json_as_string(node);

					CDropbox::ShowNotification(TranslateT("Access request"), error_description, MB_ICONERROR);

					delete node;
					delete root;
				}
			}

			mir_free(response);
		}
	}
}

void CDropbox::DestroyAcceessToken(MCONTACT hContact)
{
	HttpRequest *request = new HttpRequest(hNetlibUser, REQUEST_POST, DROPBOX_API_URL "/disable_access_token");
	NETLIBHTTPREQUEST *response = request->Send();

	delete request;
	mir_free(response);

	db_unset(NULL, MODULE, "TokenSecret");
	if (hContact)
	{
		if (db_get_w(hContact, MODULE, "Status", ID_STATUS_ONLINE) == ID_STATUS_ONLINE)
			db_set_w(hContact, MODULE, "Status", ID_STATUS_OFFLINE);
	}
}

void CDropbox::RequestApiAuthorizationAsync(void *arg)
{
	MCONTACT hContact = (MCONTACT)arg;

	if (HasAccessToken() && MessageBox(
		NULL, 
		TranslateT("Are you sure you want to request athorization?"), 
		TranslateT("Request athorization"), 
		MB_YESNO | MB_ICONQUESTION) == IDYES)
	{
		Singleton<CDropbox>::GetInstance()->DestroyAcceessToken(hContact);
		Singleton<CDropbox>::GetInstance()->RequestAcceessToken(hContact);
	}
	else
		Singleton<CDropbox>::GetInstance()->RequestAcceessToken(hContact);
}