summaryrefslogtreecommitdiff
path: root/plugins/FTPFileYM/src/job_delete.cpp
blob: 6bb44b32ddf27527df748a33b9b266facf84db90 (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
/*
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"
#include "job_delete.h"

Event DeleteJob::jobDone;
mir_cs DeleteJob::mutexJobCount;
int DeleteJob::iRunningJobCount = 0;

extern ServerList &ftpList;
extern Manager *manDlg;

DeleteJob::DeleteJob(DBEntry *entry, Manager::TreeItem *item) :
	m_entry(entry),
	m_treeItem(item),
	m_ftp(ftpList[m_entry->m_iFtpNum])
{
}

DeleteJob::~DeleteJob()
{
	delete m_entry;
}

void DeleteJob::waitingThread(void *arg)
{
	DeleteJob *job = (DeleteJob *)arg;

	while (!Miranda_IsTerminated()) {
		mir_cslockfull lock(mutexJobCount);
		if (iRunningJobCount < MAX_RUNNING_JOBS) {
			iRunningJobCount++;
			lock.unlock();
			job->run();
			delete job;

			lock.lock();
			iRunningJobCount--;
			lock.unlock();

			jobDone.release();
			return;
		}

		lock.unlock();
		jobDone.wait();
	}

	delete job;
}

void DeleteJob::start()
{
	mir_forkthread(&DeleteJob::waitingThread, this);
}

void DeleteJob::run()
{
	char szError[1024];

	CURL *hCurl = curl_easy_init();
	if (hCurl) {
		struct curl_slist *headerList = nullptr;
		headerList = curl_slist_append(headerList, getDelFileString());

		Utils::curlSetOpt(hCurl, m_ftp, getDelUrlString(), headerList, szError);

		int result = curl_easy_perform(hCurl);
		if (result == CURLE_OK) {
			if (manDlg != nullptr && m_treeItem)
				m_treeItem->remove();
			else
				DBEntry::remove(m_entry->m_fileID);
		}
		else if (manDlg != nullptr && m_treeItem) {
			wchar_t *error = mir_a2u(szError);
			wcsncpy_s(m_treeItem->m_tszToolTip, error, _TRUNCATE);
			m_treeItem->setState(Manager::TreeItem::_ERROR());
			FREE(error);
		}

		curl_slist_free_all(headerList);
		curl_easy_cleanup(hCurl);
	}
}

char* DeleteJob::getDelFileString()
{
	if (m_ftp->m_ftpProto == ServerList::FTP::FT_SSH)
		mir_snprintf(m_buff, "rm \"%s/%s\"", m_ftp->m_szDir, m_entry->m_szFileName);
	else
		mir_snprintf(m_buff, "DELE %s", m_entry->m_szFileName);

	return m_buff;
}

char* DeleteJob::getDelUrlString()
{
	if (m_ftp->m_szDir[0] && m_ftp->m_ftpProto != ServerList::FTP::FT_SSH)
		mir_snprintf(m_buff, "%s%s/%s/", m_ftp->getProtoString(), m_ftp->m_szServer, m_ftp->m_szDir);
	else
		mir_snprintf(m_buff, "%s%s/", m_ftp->getProtoString(), m_ftp->m_szServer);

	return m_buff;
}