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
|
/*
Copyright © 2015-2016 Gluzskiy Alexandr (sss)
This file is part of Unknown Download Manager (UDM).
UDM 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, either version 2 of the License, or
(at your option) any later version.
UDM 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 UDM. If not, see <http://www.gnu.org/licenses/>.
*/
#include "curl_download.h"
#include <boost/thread.hpp>
size_t curl_w_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
{
curl_download *d = (curl_download*)userdata;
if(d->get_cancel_state())
return -1; //cancel transfer
//TODO: implement pause
size_t size_ = size * nmemb;
if(size_)
{
//TODO: write data
}
return size_;
}
curl_download::curl_download(std::map<int, std::string> params, core_api *a)
{
//for now we use single transfer connection for url
//TODO: support multiple connections in parallel for multithreaded download
easy_handle = curl_easy_init();
if(!easy_handle)
; //TODO: handle error
curl_easy_setopt(easy_handle, CURLOPT_URL, params[0].c_str()); //Url as set in module_info
curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, curl_w_callback);
curl_easy_setopt(easy_handle, CURLOPT_WRITEDATA, this);
if(!params[1].empty())
download_path = params[1];
else
download_path = a->get_core_settings()["download_dir"];
//curl_easy_setopt(h, CURLOPT_DEFAULT_PROTOCOL, "http"); //require curl >= 7.45
}
bool curl_download::start()
{
boost::thread(boost::bind(&curl_download::perform_internal, this));
state = core_events::download_running;
return true; //TODO:
}
bool curl_download::stop()
{
cancel_transfer = true;
state = core_events::download_stopped;
return true; //TODO:
}
bool curl_download::delete_download()
{
cancel_transfer = true;
curl_easy_cleanup(easy_handle);
return true; //TODO:
}
void curl_download::perform_internal()
{
auto status = curl_easy_perform(easy_handle);
if(status != CURLE_OK)
state = core_events::download_error;
else
state = core_events::download_completed;
//TODO: fire event
}
curl_download::~curl_download()
{
curl_easy_cleanup(easy_handle);
}
|