summaryrefslogtreecommitdiff
path: root/plugins/FTPFileYM/src/ftpfile.cpp
blob: 18230a7b243c39dd034b6ea5a048f59f7b5b635e (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
FTP File YM plugin
Copyright (C) 2007-2010 Jan Holub

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation version 2
of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "stdafx.h"

CMPlugin g_plugin;

HGENMENU hMenu, hMainMenu, hSubMenu[ServerList::FTP_COUNT], hMainSubMenu[ServerList::FTP_COUNT];

extern UploadDialog *uDlg;
extern Manager *manDlg;

extern DeleteTimer &deleteTimer;
extern ServerList &ftpList;
extern Options &opt;

int PrebuildContactMenu(WPARAM wParam, LPARAM lParam);
void PrebuildMainMenu();
int TabsrmmButtonPressed(WPARAM wParam, LPARAM lParam);
int UploadFile(MCONTACT hContact, int m_iFtpNum, UploadJob::EMode mode);

/////////////////////////////////////////////////////////////////////////////////////////

static PLUGININFOEX pluginInfoEx =
{
	sizeof(PLUGININFOEX),
	__PLUGIN_NAME,
	PLUGIN_MAKE_VERSION(__MAJOR_VERSION, __MINOR_VERSION, __RELEASE_NUM, __BUILD_NUM),
	__DESCRIPTION,
	__AUTHOR,
	__COPYRIGHT,
	__AUTHORWEB,
	UNICODE_AWARE,
	// {9502E511-7E5D-49A1-8BA5-B1AEE70FA5BF}
	{0x9502e511, 0x7e5d, 0x49a1, {0x8b, 0xa5, 0xb1, 0xae, 0xe7, 0xf, 0xa5, 0xbf}}
};

CMPlugin::CMPlugin() :
	PLUGIN<CMPlugin>(MODULENAME, pluginInfoEx)
{}

/////////////////////////////////////////////////////////////////////////////////////////

static IconItem iconList[] =
{
	{ LPGEN("FTP Server 1"),     "ftp1", 		IDI_FTP0       },
	{ LPGEN("FTP Server 2"),     "ftp2", 		IDI_FTP1       },
	{ LPGEN("FTP Server 3"),     "ftp3", 		IDI_FTP2       },
	{ LPGEN("FTP Server 4"),     "ftp4", 		IDI_FTP3       },
	{ LPGEN("FTP Server 5"),     "ftp5", 		IDI_FTP4       },
	{ LPGEN("Send file"),        "main",       IDI_MENU       },
	{ LPGEN("Clipboard"),        "clipboard",  IDI_CLIPBOARD  },
	{ LPGEN("Pause"),            "pause",      IDI_PAUSE      },
	{ LPGEN("Resume"),           "resume",     IDI_RESUME     },
	{ LPGEN("Delete from list"), "clear",      IDI_CLEAR      },
	{ LPGEN("Delete from FTP"),  "delete",     IDI_DELETE     }
};

static void InitIcolib()
{
	g_plugin.registerIcon(LPGEN("FTP File"), iconList, MODULENAME);
}

void InitMenuItems()
{
	wchar_t stzName[256];

	CMenuItem mi(&g_plugin);
	SET_UID(mi, 0xB7132F5A, 0x65FC, 0x42C5, 0xB4, 0xCB, 0x54, 0xBC, 0xAC, 0x58, 0x34, 0xE9);
	mi.flags = CMIF_UNICODE;
	mi.hIcolibItem = iconList[ServerList::FTP_COUNT].hIcolib;
	mi.position = 3000090001;
	mi.name.w = LPGENW("FTP File");

	hMainMenu = Menu_AddMainMenuItem(&mi);
	if (opt.bUseSubmenu)
		hMenu = Menu_AddContactMenuItem(&mi);

	memset(&mi, 0, sizeof(mi));
	mi.name.w = stzName;
	mi.flags = CMIF_UNICODE | CMIF_SYSTEM;

	CMenuItem mi2(&g_plugin);
	mi2.flags = CMIF_UNICODE | CMIF_SYSTEM;
	mi2.pszService = MS_FTPFILE_CONTACTMENU;

	CMStringA frmt;
	for (int i = 0; i < ServerList::FTP_COUNT; i++) {
		ptrA Name(g_plugin.getStringA(frmt.Format("Name%d", i)));
		if (Name)
			mir_snwprintf(stzName, TranslateT("FTP Server %d"), i + 1);

		mi.root = (opt.bUseSubmenu) ? hMenu : nullptr;
		mi.hIcolibItem = iconList[i].hIcolib;
		hSubMenu[i] = Menu_AddContactMenuItem(&mi);
		Menu_ConfigureItem(hSubMenu[i], MCI_OPT_EXECPARAM, i + 1000);

		mi.root = hMainMenu;
		hMainSubMenu[i] = Menu_AddMainMenuItem(&mi);

		mi2.root = hSubMenu[i];
		mi2.pszService = MS_FTPFILE_CONTACTMENU;
		mi2.name.w = LPGENW("Upload file(s)");
		HGENMENU tmp = Menu_AddContactMenuItem(&mi2);
		Menu_ConfigureItem(tmp, MCI_OPT_EXECPARAM, mi2.position = i + UploadJob::FTP_RAWFILE);

		mi2.pszService = MS_FTPFILE_MAINMENU;
		mi2.root = hMainSubMenu[i];
		Menu_AddMainMenuItem(&mi2);

		mi2.root = hSubMenu[i];
		mi2.pszService = MS_FTPFILE_CONTACTMENU;
		mi2.name.w = LPGENW("Zip and upload file(s)");
		tmp = Menu_AddContactMenuItem(&mi2);
		Menu_ConfigureItem(tmp, MCI_OPT_EXECPARAM, i + UploadJob::FTP_ZIPFILE);

		mi2.pszService = MS_FTPFILE_MAINMENU;
		mi2.root = hMainSubMenu[i];
		Menu_AddMainMenuItem(&mi2);

		mi2.root = hSubMenu[i];
		mi2.pszService = MS_FTPFILE_CONTACTMENU;
		mi2.name.w = LPGENW("Zip and upload folder");
		tmp = Menu_AddContactMenuItem(&mi2);
		Menu_ConfigureItem(tmp, MCI_OPT_EXECPARAM, i + UploadJob::FTP_ZIPFOLDER);

		mi2.pszService = MS_FTPFILE_MAINMENU;
		mi2.root = hMainSubMenu[i];
		Menu_AddMainMenuItem(&mi2);
	}

	memset(&mi, 0, sizeof(mi));
	SET_UID(mi, 0x0C17CAD7, 0x7F23, 0x449B, 0xB9, 0xCD, 0xFF, 0x50, 0xDA, 0x69, 0xF3, 0x6F);
	mi.flags = CMIF_UNICODE;
	mi.hIcolibItem = iconList[ServerList::FTP_COUNT].hIcolib;
	mi.position = 3000090001;
	mi.name.w = LPGENW("FTP File manager");
	mi.pszService = MS_FTPFILE_SHOWMANAGER;
	mi.root = hMainMenu;
	Menu_AddMainMenuItem(&mi);

	PrebuildMainMenu();

	HookEvent(ME_CLIST_PREBUILDCONTACTMENU, PrebuildContactMenu);
}

void InitHotkeys()
{
	HOTKEYDESC hk = {};
	hk.szSection.a = MODULENAME;
	hk.szDescription.a = LPGEN("Show FTPFile manager");
	hk.pszName = "FTP_ShowManager";
	hk.pszService = MS_FTPFILE_SHOWMANAGER;
	g_plugin.addHotkey(&hk);
}

void InitTabsrmmButton()
{
	BBButton btn = {};
	btn.dwButtonID = 1;
	btn.pszModuleName = MODULENAME;
	btn.dwDefPos = 105;
	btn.hIcon = iconList[ServerList::FTP_COUNT].hIcolib;
	btn.bbbFlags = BBBF_ISARROWBUTTON | BBBF_ISIMBUTTON | BBBF_CANBEHIDDEN;
	btn.pwszTooltip = TranslateT("FTP File");
	Srmm_AddButton(&btn, &g_plugin);
	HookEvent(ME_MSG_BUTTONPRESSED, TabsrmmButtonPressed);
}

//------------ MENU & BUTTON HANDLERS ------------//

int PrebuildContactMenu(WPARAM wParam, LPARAM)
{
	bool bIsContact = false;

	char *szProto = GetContactProto(wParam);
	if (szProto) bIsContact = (CallProtoService(szProto, PS_GETCAPS, PFLAGNUM_1, 0) & PF1_IM) ? true : false;

	bool bHideRoot = opt.bHideInactive;
	for (int i = 0; i < ServerList::FTP_COUNT; i++)
		if (ftpList[i]->m_bEnabled)
			bHideRoot = false;

	if (opt.bUseSubmenu)
		Menu_ShowItem(hMenu, bIsContact && !bHideRoot);

	for (int i = 0; i < ServerList::FTP_COUNT; i++)
		Menu_ShowItem(hSubMenu[i], bIsContact && ftpList[i]->m_bEnabled);
	return 0;
}

void PrebuildMainMenu()
{
	for (int i = 0; i < ServerList::FTP_COUNT; i++)
		if (ftpList[i])
			Menu_ShowItem(hMainSubMenu[i], ftpList[i]->m_bEnabled);
}

int TabsrmmButtonPressed(WPARAM hContact, LPARAM lParam)
{
	CustomButtonClickData *cbc = (CustomButtonClickData *)lParam;

	if (!strcmp(cbc->pszModule, MODULENAME) && cbc->dwButtonId == 1 && hContact) {
		if (cbc->flags == BBCF_ARROWCLICKED) {
			HMENU hPopupMenu = CreatePopupMenu();
			if (hPopupMenu) {
				int iCount = 0;
				for (UINT i = 0; i < ServerList::FTP_COUNT; i++) {
					if (ftpList[i]->m_bEnabled) {
						HMENU hModeMenu = CreatePopupMenu();
						AppendMenu(hModeMenu, MF_STRING, i + UploadJob::FTP_RAWFILE, TranslateT("Upload file"));
						AppendMenu(hModeMenu, MF_STRING, i + UploadJob::FTP_ZIPFILE, TranslateT("Zip and upload file"));
						AppendMenu(hModeMenu, MF_STRING, i + UploadJob::FTP_ZIPFOLDER, TranslateT("Zip and upload folder"));
						AppendMenu(hPopupMenu, MF_STRING | MF_POPUP, (UINT_PTR)hModeMenu, ftpList[i]->m_stzName);
						DestroyMenu(hModeMenu);
						iCount++;
					}
				}

				if (iCount != 0) {
					POINT pt;
					GetCursorPos(&pt);
					HWND hwndBtn = WindowFromPoint(pt);
					if (hwndBtn) {
						RECT rc;
						GetWindowRect(hwndBtn, &rc);
						SetForegroundWindow(cbc->hwndFrom);
						int selected = TrackPopupMenu(hPopupMenu, TPM_RETURNCMD, rc.left, rc.bottom, 0, cbc->hwndFrom, nullptr);
						if (selected != 0) {
							int ftpNum = selected & (1 | 2 | 4);
							int mode = selected & (UploadJob::FTP_RAWFILE | UploadJob::FTP_ZIPFILE | UploadJob::FTP_ZIPFOLDER);
							UploadFile(hContact, ftpNum, (UploadJob::EMode)mode);
						}
					}
				}

				DestroyMenu(hPopupMenu);
			}
		}
		else UploadFile(hContact, 0, UploadJob::FTP_RAWFILE);
	}

	return 0;
}

int UploadFile(MCONTACT hContact, int m_iFtpNum, GenericJob::EMode mode, void **objects, int objCount, DWORD flags)
{
	if (!ftpList[m_iFtpNum]->isValid()) {
		Utils::msgBox(TranslateT("You have to fill FTP server setting before upload a file."), MB_OK | MB_ICONERROR);
		return 1;
	}

	GenericJob *job;
	if (mode == GenericJob::FTP_RAWFILE)
		job = new UploadJob(hContact, m_iFtpNum, mode);
	else
		job = new PackerJob(hContact, m_iFtpNum, mode);

	int result;
	if (objects != nullptr)
		result = job->getFiles(objects, objCount, flags);
	else
		result = job->getFiles();

	if (result != 0) {
		uDlg = UploadDialog::getInstance();
		if (!uDlg->m_hwnd || !uDlg->m_hwndTabs) {
			Utils::msgBox(TranslateT("Error has occurred while trying to create a dialog!"), MB_OK | MB_ICONERROR);
			delete uDlg;
			return 1;
		}

		job->addToUploadDlg();
		uDlg->show();
	}
	else {
		delete job;
		return 1;
	}

	return 0;
}

int UploadFile(MCONTACT hContact, int m_iFtpNum, GenericJob::EMode mode)
{
	return UploadFile(hContact, m_iFtpNum, mode, nullptr, 0, 0);
}

//------------ MIRANDA SERVICES ------------//

INT_PTR UploadService(WPARAM, LPARAM lParam)
{
	FTPUPLOAD *ftpu = (FTPUPLOAD *)lParam;
	if (ftpu == nullptr || ftpu->cbSize != sizeof(FTPUPLOAD))
		return 1;

	int ftpNum = (ftpu->ftpNum == FNUM_DEFAULT) ? opt.defaultFTP : ftpu->ftpNum - 1;
	int mode = (ftpu->mode * GenericJob::FTP_RAWFILE);

	UploadFile(ftpu->hContact, ftpNum, (GenericJob::EMode)mode, (void**)ftpu->pstzObjects, ftpu->objectCount, ftpu->flags);
	return 0;
}

INT_PTR ShowManagerService(WPARAM, LPARAM)
{
	manDlg = Manager::getInstance();
	manDlg->init();
	return 0;
}

INT_PTR ContactMenuService(WPARAM hContact, LPARAM lParam)
{
	int ftpNum = lParam & (1 | 2 | 4);
	int mode = lParam & (UploadJob::FTP_RAWFILE | UploadJob::FTP_ZIPFILE | UploadJob::FTP_ZIPFOLDER);
	return UploadFile(hContact, ftpNum, (UploadJob::EMode)mode);
}

INT_PTR MainMenuService(WPARAM wParam, LPARAM)
{
	int ftpNum = wParam & (1 | 2 | 4);
	int mode = wParam & (UploadJob::FTP_RAWFILE | UploadJob::FTP_ZIPFILE | UploadJob::FTP_ZIPFOLDER);
	return UploadFile(0, ftpNum, (UploadJob::EMode)mode);
}

//------------ START & EXIT STUFF ------------//

static int ModulesLoaded(WPARAM, LPARAM)
{
	InitMenuItems();
	InitTabsrmmButton();

	g_plugin.addSound(SOUND_UPCOMPLETE, LPGENW("FTP File"), LPGENW("File upload complete"));
	g_plugin.addSound(SOUND_CANCEL, LPGENW("FTP File"), LPGENW("Upload canceled"));

	curl_global_init(CURL_GLOBAL_ALL);

	return 0;
}

static int Shutdown(WPARAM, LPARAM)
{
	deleteTimer.deinit();

	delete manDlg;
	if (uDlg)
		SendMessage(uDlg->m_hwnd, WM_CLOSE, 0, 0);

	UploadJob::jobDone.release();
	DeleteJob::jobDone.release();
	DBEntry::cleanupDB();

	curl_global_cleanup();

	ftpList.deinit();
	opt.deinit();

	return 0;
}

int CMPlugin::Load()
{
#ifdef _DEBUG
	_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
#endif

	CoInitialize(nullptr);

	HookEvent(ME_SYSTEM_MODULESLOADED, ModulesLoaded);
	HookEvent(ME_SYSTEM_PRESHUTDOWN, Shutdown);
	HookEvent(ME_OPT_INITIALISE, Options::InitOptions);

	CreateServiceFunction(MS_FTPFILE_UPLOAD, UploadService);
	CreateServiceFunction(MS_FTPFILE_SHOWMANAGER, ShowManagerService);
	CreateServiceFunction(MS_FTPFILE_CONTACTMENU, ContactMenuService);
	CreateServiceFunction(MS_FTPFILE_MAINMENU, MainMenuService);

	InitIcolib();
	InitHotkeys();

	opt.loadOptions();
	deleteTimer.init();
	ftpList.init();
	return 0;
}