summaryrefslogtreecommitdiff
path: root/protocols/Tox/src/file_transfer.h
blob: 6f30b90241ef34b2e5af387e674b9e34e0c528f4 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#ifndef _FILE_TRANSFER_H_
#define _FILE_TRANSFER_H_

class CFileTransfer;

class CFile
{
private:
	int number;
	char *name;
	const TCHAR *path;
	size_t size;

	const CFileTransfer *transfer;

public:
	CFile(const CFileTransfer *fileTransfer, const TCHAR *filePath, size_t fileSize) :
		number(-1)
	{
		transfer = fileTransfer;

		path = filePath;
		name = strrchr(ptrA(mir_utf8encodeT(path)), '\\');
		size = fileSize;
	}

	CFile(int number) : number(number), name(NULL) { }

	~CFile()
	{
		number = -1;
		if (name != NULL)
		{
			mir_free(name);
		}
	}

	const CFileTransfer *GetTransfer() const
	{
		return this->transfer;
	}

	void SetNumber(int fileNumber)
	{
		number = fileNumber;
	}

	const TCHAR* GetPath() const
	{
		return path;
	}

	const char* GetName() const
	{
		return name;
	}

	size_t GetSize() const
	{
		return size;
	}
};

class CFileTransfer
{
private:
	ULONG number;

	HANDLE hWait;
	LIST<CFile> files;
	PROTOFILETRANSFERSTATUS pfts;
	const PROTO_INTERFACE *proto;

public:
	CFileTransfer(const PROTO_INTERFACE *proto, MCONTACT hContact, ULONG transferNumber, DWORD flags) :
		files(1, NumericKeySortT)
	{
		this->proto = proto;

		pfts.cbSize = sizeof(pfts);
		pfts.flags = PFTS_TCHAR | flags;
		pfts.hContact = hContact;
		pfts.currentFileNumber = 0;
		pfts.currentFileProgress = 0;
		pfts.currentFileSize = 0;
		pfts.totalBytes = 0;
		pfts.totalFiles = 0;
		pfts.totalProgress = 0;
		pfts.pszFiles = NULL;
		pfts.tszWorkingDir = NULL;
		pfts.tszCurrentFile = NULL;

		number = transferNumber;
	}

	~CFileTransfer()
	{
		if (pfts.tszWorkingDir)
			mir_free(pfts.tszWorkingDir);
		if (pfts.ptszFiles)
		{
			for (int i = 0; pfts.ptszFiles[i]; i++)
			{
				if (pfts.ptszFiles[i]) mir_free(pfts.ptszFiles[i]);
			}
			mir_free(pfts.ptszFiles);
		}
	}

	void ProcessTransferedFiles(TCHAR** ppszFiles)
	{
		for (pfts.totalFiles = 0; ppszFiles[pfts.totalFiles]; pfts.totalFiles++);
		pfts.ptszFiles = (TCHAR**)mir_alloc(sizeof(TCHAR*)*(pfts.totalFiles + 1));
		pfts.ptszFiles[pfts.totalFiles] = NULL;
		for (int i = 0; ppszFiles[i]; i++)
		{
			if (!pfts.tszWorkingDir)
			{
				wchar_t *path = ppszFiles[i];
				int length = wcsrchr(path, '\\') - path;
				pfts.tszWorkingDir = (TCHAR*)mir_alloc(sizeof(TCHAR)*(length + 1));
				lstrcpyn(pfts.tszWorkingDir, ppszFiles[i], length + 1);
				pfts.tszWorkingDir[length] = '\0';
			}

			pfts.ptszFiles[i] = mir_tstrdup(ppszFiles[i]);

			size_t fileSize = 0;
			FILE *file = _tfopen(ppszFiles[i], _T("rb"));
			if (file != NULL)
			{
				fseek(file, 0, SEEK_END);
				pfts.totalBytes += fileSize = ftell(file);
				fseek(file, 0, SEEK_SET);
				fclose(file);
			}

			files.insert(new CFile(this, ppszFiles[i], fileSize));
		}
	}

	const PROTO_INTERFACE *GetProtoInstance() const
	{
		return proto;
	}

	ULONG GetTransferNumber() const
	{
		return number;
	}

	MCONTACT GetContactHandle() const
	{
		return pfts.hContact;
	}

	int GetFileCount() const
	{
		return pfts.totalFiles;
	}

	CFile *GetFileAt(int idx) const
	{
		return files[idx];
	}

	CFile *GetFileByNumber(int number) const
	{
		CFile *search = new CFile(number);
		CFile *file = files.find(search);
		delete search;

		return file;
	}

	bool HasFile(int number) const
	{
		const CFile *file = GetFileByNumber(number);

		return file != NULL;
	}

	void Wait()
	{
		WaitForSingleObject(hWait, INFINITE);
	}
};

class CFileSendTransfer : public CFileTransfer
{
public:
	CFileSendTransfer(MCONTACT hContact, ULONG hProcess) : CFileTransfer(NULL, hContact, hProcess, PFTS_SENDING)
	{
	}
};

#endif //_FILE_TRANSFER_H_