summaryrefslogtreecommitdiff
path: root/plugins/CloudFile/src/services.cpp
blob: 826554ca97f5634b09841d263c3b55d9cc48ffb5 (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
#include "stdafx.h"

static int CompareServices(const CCloudService *p1, const CCloudService *p2)
{
	return mir_strcmp(p1->GetAccountName(), p2->GetAccountName());
}

LIST<CCloudService> Services(10, CompareServices);

CCloudService* FindService(const char *szProto)
{
	for (auto &it : Services)
		if (!mir_strcmp(it->GetAccountName(), szProto))
			return it;
	
	return nullptr;
}

static INT_PTR GetService(WPARAM wParam, LPARAM lParam)
{
	CFSERVICEINFO *info = (CFSERVICEINFO*)lParam;
	if (info == nullptr)
		return 1;

	ptrA accountName(mir_strdup((char*)wParam));
	if (!accountName || !mir_strlen(accountName))
		accountName = db_get_sa(NULL, MODULE, "DefaultService");
	if (accountName == nullptr)
		return 2;

	CCloudService *service = FindService(accountName);
	if (service == nullptr)
		return 3;

	info->accountName = service->GetAccountName();
	info->userName = service->GetUserName();

	return 0;
}

static INT_PTR EnumServices(WPARAM wParam, LPARAM lParam)
{
	CFSERVICEINFO info = {};
	enumCFServiceFunc enumFunc = (enumCFServiceFunc)wParam;
	void *param = (void*)lParam;

	for (auto &service : Services) {
		info.accountName = service->GetAccountName();
		info.userName = service->GetUserName();
		int res = enumFunc(&info, param);
		if (res != 0)
			return res;
	}

	return 0;
}

INT_PTR Upload(WPARAM wParam, LPARAM lParam)
{
	CFUPLOADDATA *uploadData = (CFUPLOADDATA*)wParam;
	if (uploadData == nullptr)
		return 1;

	ptrA accountName(mir_strdup(uploadData->accountName));
	if (!mir_strlen(accountName))
		accountName = db_get_sa(NULL, MODULE, "DefaultService");
	if (accountName == nullptr)
		return 2;

	CCloudService *service = FindService(uploadData->accountName);
	if (service == nullptr)
		return 3;

	if (PathIsDirectory(uploadData->localPath)) {
		// temporary unsupported
		return 4;
	}

	FileTransferParam ftp(0);
	ftp.SetWorkingDirectory(uploadData->localPath);
	ftp.SetServerDirectory(uploadData->serverFolder);
	ftp.AddFile(uploadData->localPath);

	int res = CCloudService::Upload(service, &ftp);
	if (res == ACKRESULT_SUCCESS && lParam) {
		size_t linkCount = 0;
		const char **links = ftp.GetSharedLinks(linkCount);
		if (linkCount > 0)
			lParam = (LPARAM)mir_strdup(links[linkCount - 1]);
	}

	return res;
}

void InitializeServices()
{
	Proto_RegisterModule(PROTOTYPE_FILTER, MODULE);

	CreateServiceFunction(MODULE PSS_FILE, SendFileInterceptor);

	CreateServiceFunction(MS_CLOUDFILE_GETSERVICE, GetService);
	CreateServiceFunction(MS_CLOUDFILE_ENUMSERVICES, EnumServices);
	CreateServiceFunction(MS_CLOUDFILE_UPLOAD, Upload);
}