13
0

tweaks to library & downloader API

This commit is contained in:
Paul Davis 2022-09-07 08:52:22 -06:00
parent 78d96ed0c6
commit bd9fe31778
2 changed files with 31 additions and 11 deletions

View File

@ -15,11 +15,12 @@ namespace ARDOUR {
class LibraryDescription
{
public:
LibraryDescription (std::string const & n, std::string const & d, std::string const & u, std::string const & l, std::string const & td)
: _name (n), _description (d), _url (u), _license (l), _toplevel_dir (td), _installed (false) {}
LibraryDescription (std::string const & n, std::string const & a, std::string const & d, std::string const & u, std::string const & l, std::string const & td)
: _name (n), _author (a), _description (d), _url (u), _license (l), _toplevel_dir (td), _installed (false) {}
std::string const & name() const { return _name; }
std::string const & description() const { return _description; }
std::string const & author() const { return _author; }
std::string const & url() const { return _url; }
std::string const & license() const { return _license; }
std::string const & toplevel_dir() const { return _toplevel_dir; }
@ -29,6 +30,7 @@ class LibraryDescription
private:
std::string _name;
std::string _author;
std::string _description;
std::string _url;
std::string _license;
@ -61,7 +63,6 @@ class Downloader {
FILE* file;
CURL* curl;
bool _cancel;
double dsize; /* temporary to match CURL API */
std::atomic<uint64_t> _download_size; /* read-only from requestor thread */
std::atomic<uint64_t> _downloaded; /* read-only from requestor thread */
std::atomic<int> _status;

View File

@ -61,11 +61,15 @@ LibraryFetcher::get_descriptions ()
XMLNode const & root (*tree.root());
for (auto const & node : root.children()) {
string n, d, u, l, td;
string n, d, u, l, td, a;
if (!node->get_property (X_("name"), n)) {
std::cerr << "no name\n";
continue;
}
if (!node->get_property (X_("author"), a)) {
std::cerr << "no author\n";
continue;
}
if (!node->get_property (X_("url"), u)) {
std::cerr << "no urln";
continue;
@ -82,7 +86,7 @@ LibraryFetcher::get_descriptions ()
d = node->content();
_descriptions.push_back (LibraryDescription (n, d, u, l, td));
_descriptions.push_back (LibraryDescription (n, a, d, u, l, td));
_descriptions.back().set_installed (installed (_descriptions.back()));
std::cerr << "got description for " << _descriptions.back().name() << std::endl;
@ -183,7 +187,7 @@ LibraryFetcher::installed (LibraryDescription const & desc)
}
static size_t
CurlFILEWrite_CallbackFunc_StdString(void *contents, size_t size, size_t nmemb, Downloader* dl)
CurlWrite_CallbackFunc_Downloader(void *contents, size_t size, size_t nmemb, Downloader* dl)
{
return dl->write (contents, size, nmemb);
}
@ -220,7 +224,7 @@ Downloader::Downloader (string const & u, string const & dir)
int
Downloader::start ()
{
file_path = Glib::build_filename (Config->get_clip_library_dir(), destdir, Glib::path_get_basename (url));
file_path = Glib::build_filename (destdir, Glib::path_get_basename (url));
file = fopen (file_path.c_str(), "w");
if (!file) {
@ -256,6 +260,10 @@ void
Downloader::download ()
{
{
/* First curl fetch to get the data size so that we can offer a
* progress meter
*/
curl = curl_easy_init ();
if (!curl) {
_status = -1;
@ -267,10 +275,21 @@ Downloader::download ()
curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
curl_easy_setopt(curl, CURLOPT_HEADER, 0L);
curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_perform (curl);
curl_easy_getinfo (curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &dsize);
_download_size = dsize;
CURLcode res = curl_easy_perform (curl);
if (res == CURLE_OK) {
double dsize;
curl_easy_getinfo (curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &dsize);
_download_size = dsize;
}
curl_easy_cleanup (curl);
if (res != CURLE_OK ) {
_status = -2;
return;
}
}
curl = curl_easy_init ();
@ -281,7 +300,7 @@ Downloader::download ()
curl_easy_setopt (curl, CURLOPT_URL, url.c_str());
curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlFILEWrite_CallbackFunc_StdString);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlWrite_CallbackFunc_Downloader);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, this);
CURLcode res = curl_easy_perform (curl);
curl_easy_cleanup (curl);