2008-04-11 10:06:50 -04:00
|
|
|
/* sfdb_freesound_mootcher.cpp **********************************************************************
|
2011-06-01 13:00:29 -04:00
|
|
|
|
2008-04-11 10:06:50 -04:00
|
|
|
Adapted for Ardour by Ben Loftis, March 2008
|
2011-11-14 17:04:14 -05:00
|
|
|
Updated to new Freesound API by Colin Fletcher, November 2011
|
2011-06-01 13:00:29 -04:00
|
|
|
|
2008-04-11 10:06:50 -04:00
|
|
|
Mootcher 23-8-2005
|
|
|
|
|
|
|
|
Mootcher Online Access to thefreesoundproject website
|
|
|
|
http://freesound.iua.upf.edu/
|
|
|
|
|
|
|
|
GPL 2005 Jorn Lemon
|
|
|
|
mail for questions/remarks: mootcher@twistedlemon.nl
|
|
|
|
or go to the freesound website forum
|
|
|
|
|
|
|
|
-----------------------------------------------------------------
|
|
|
|
|
|
|
|
Includes:
|
|
|
|
curl.h (version 7.14.0)
|
|
|
|
Librarys:
|
|
|
|
libcurl.lib
|
|
|
|
|
|
|
|
-----------------------------------------------------------------
|
|
|
|
Licence GPL:
|
|
|
|
|
|
|
|
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; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
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, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
|
|
|
|
*************************************************************************************/
|
|
|
|
#include "sfdb_freesound_mootcher.h"
|
|
|
|
|
2009-02-25 13:26:51 -05:00
|
|
|
#include "pbd/xml++.h"
|
2012-06-23 01:09:08 -04:00
|
|
|
#include "pbd/error.h"
|
2008-04-11 10:06:50 -04:00
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2011-11-14 17:04:14 -05:00
|
|
|
#include <iostream>
|
2008-04-11 10:06:50 -04:00
|
|
|
|
2012-03-13 16:15:37 -04:00
|
|
|
#include <glib.h>
|
|
|
|
#include <glib/gstdio.h>
|
|
|
|
|
2012-04-05 17:31:55 -04:00
|
|
|
#include "i18n.h"
|
|
|
|
|
2009-02-25 13:26:51 -05:00
|
|
|
#include "ardour/audio_library.h"
|
2013-06-14 14:09:40 -04:00
|
|
|
#include "ardour/rc_configuration.h"
|
2013-06-14 14:17:24 -04:00
|
|
|
#include "pbd/pthread_utils.h"
|
2013-06-16 10:43:43 -04:00
|
|
|
#include "gui_thread.h"
|
2008-04-11 10:06:50 -04:00
|
|
|
|
2012-11-13 10:11:07 -05:00
|
|
|
using namespace PBD;
|
|
|
|
|
2011-11-14 17:04:14 -05:00
|
|
|
static const std::string base_url = "http://www.freesound.org/api";
|
|
|
|
static const std::string api_key = "9d77cb8d841b4bcfa960e1aae62224eb"; // ardour3
|
|
|
|
|
2008-04-11 10:06:50 -04:00
|
|
|
//------------------------------------------------------------------------
|
2012-03-09 17:02:48 -05:00
|
|
|
Mootcher::Mootcher()
|
2011-11-14 17:04:14 -05:00
|
|
|
: curl(curl_easy_init())
|
2008-04-11 10:06:50 -04:00
|
|
|
{
|
2013-06-14 14:09:40 -04:00
|
|
|
cancel_download_btn.set_label (_("Cancel"));
|
|
|
|
progress_hbox.pack_start (progress_bar, true, true);
|
|
|
|
progress_hbox.pack_end (cancel_download_btn, false, false);
|
|
|
|
progress_bar.show();
|
|
|
|
cancel_download_btn.show();
|
|
|
|
cancel_download_btn.signal_clicked().connect(sigc::mem_fun (*this, &Mootcher::cancelDownload));
|
2008-04-11 10:06:50 -04:00
|
|
|
};
|
|
|
|
//------------------------------------------------------------------------
|
2011-11-14 17:04:14 -05:00
|
|
|
Mootcher:: ~Mootcher()
|
2008-04-11 10:06:50 -04:00
|
|
|
{
|
2012-04-05 17:31:55 -04:00
|
|
|
curl_easy_cleanup(curl);
|
2008-04-11 10:06:50 -04:00
|
|
|
}
|
2011-11-14 17:04:14 -05:00
|
|
|
|
2008-04-11 10:06:50 -04:00
|
|
|
//------------------------------------------------------------------------
|
2011-06-01 13:00:29 -04:00
|
|
|
|
2011-12-27 09:36:14 -05:00
|
|
|
void Mootcher::ensureWorkingDir ()
|
|
|
|
{
|
2013-06-14 14:09:40 -04:00
|
|
|
std::string p = ARDOUR::Config->get_freesound_download_dir();
|
2012-06-23 01:09:08 -04:00
|
|
|
|
|
|
|
if (!Glib::file_test (p, Glib::FILE_TEST_IS_DIR)) {
|
|
|
|
if (g_mkdir_with_parents (p.c_str(), 0775) != 0) {
|
|
|
|
PBD::error << "Unable to create Mootcher working dir" << endmsg;
|
|
|
|
}
|
2011-12-27 09:36:14 -05:00
|
|
|
}
|
2013-06-14 14:09:40 -04:00
|
|
|
basePath = p;
|
2013-10-14 12:00:28 -04:00
|
|
|
#ifdef PLATFORM_WINDOWS
|
2013-06-14 14:09:40 -04:00
|
|
|
std::string replace = "/";
|
|
|
|
size_t pos = basePath.find("\\");
|
|
|
|
while( pos != std::string::npos ){
|
|
|
|
basePath.replace(pos, 1, replace);
|
|
|
|
pos = basePath.find("\\");
|
|
|
|
}
|
|
|
|
#endif
|
2008-04-11 10:06:50 -04:00
|
|
|
}
|
2011-12-27 09:36:14 -05:00
|
|
|
|
2008-04-11 10:06:50 -04:00
|
|
|
|
|
|
|
//------------------------------------------------------------------------
|
2011-11-14 17:04:14 -05:00
|
|
|
size_t Mootcher::WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
|
2008-04-11 10:06:50 -04:00
|
|
|
{
|
|
|
|
register int realsize = (int)(size * nmemb);
|
|
|
|
struct MemoryStruct *mem = (struct MemoryStruct *)data;
|
|
|
|
|
2011-11-14 17:04:14 -05:00
|
|
|
mem->memory = (char *)realloc(mem->memory, mem->size + realsize + 1);
|
2008-04-11 10:06:50 -04:00
|
|
|
|
|
|
|
if (mem->memory) {
|
|
|
|
memcpy(&(mem->memory[mem->size]), ptr, realsize);
|
|
|
|
mem->size += realsize;
|
|
|
|
mem->memory[mem->size] = 0;
|
|
|
|
}
|
|
|
|
return realsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------
|
|
|
|
|
2013-06-14 14:17:24 -04:00
|
|
|
std::string Mootcher::sortMethodString(enum sortMethod sort)
|
|
|
|
{
|
2012-03-09 17:02:48 -05:00
|
|
|
// given a sort type, returns the string value to be passed to the API to
|
|
|
|
// sort the results in the requested way.
|
2011-11-14 17:04:14 -05:00
|
|
|
|
|
|
|
switch (sort) {
|
|
|
|
case sort_duration_desc: return "duration_desc";
|
|
|
|
case sort_duration_asc: return "duration_asc";
|
|
|
|
case sort_created_desc: return "created_desc";
|
|
|
|
case sort_created_asc: return "created_asc";
|
|
|
|
case sort_downloads_desc: return "downloads_desc";
|
|
|
|
case sort_downloads_asc: return "downloads_asc";
|
|
|
|
case sort_rating_desc: return "rating_desc";
|
|
|
|
case sort_rating_asc: return "rating_asc";
|
|
|
|
default: return "";
|
|
|
|
}
|
|
|
|
}
|
2008-04-11 10:06:50 -04:00
|
|
|
|
|
|
|
//------------------------------------------------------------------------
|
2011-11-14 17:04:14 -05:00
|
|
|
void Mootcher::setcUrlOptions()
|
2008-04-11 10:06:50 -04:00
|
|
|
{
|
|
|
|
// basic init for curl
|
|
|
|
curl_global_init(CURL_GLOBAL_ALL);
|
|
|
|
// some servers don't like requests that are made without a user-agent field, so we provide one
|
|
|
|
curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
|
|
|
|
// setup curl error buffer
|
2011-11-14 17:04:14 -05:00
|
|
|
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);
|
2008-04-11 10:06:50 -04:00
|
|
|
// Allow redirection
|
|
|
|
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
|
2011-11-14 17:04:14 -05:00
|
|
|
|
|
|
|
// Allow connections to time out (without using signals)
|
|
|
|
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
|
2012-01-18 13:43:43 -05:00
|
|
|
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 30);
|
2011-11-14 17:04:14 -05:00
|
|
|
|
|
|
|
|
2008-04-11 10:06:50 -04:00
|
|
|
}
|
|
|
|
|
2011-11-14 17:04:14 -05:00
|
|
|
std::string Mootcher::doRequest(std::string uri, std::string params)
|
2008-04-11 10:06:50 -04:00
|
|
|
{
|
2011-11-14 17:04:14 -05:00
|
|
|
std::string result;
|
2008-04-11 10:06:50 -04:00
|
|
|
struct MemoryStruct xml_page;
|
|
|
|
xml_page.memory = NULL;
|
|
|
|
xml_page.size = 0;
|
|
|
|
|
2011-11-14 17:04:14 -05:00
|
|
|
setcUrlOptions();
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) &xml_page);
|
2011-06-01 13:00:29 -04:00
|
|
|
|
2011-11-14 17:04:14 -05:00
|
|
|
// curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
|
|
|
|
// curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postMessage.c_str());
|
|
|
|
// curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, -1);
|
2008-04-11 10:06:50 -04:00
|
|
|
|
2011-11-14 17:04:14 -05:00
|
|
|
// the url to get
|
|
|
|
std::string url = base_url + uri + "?";
|
|
|
|
if (params != "") {
|
|
|
|
url += params + "&api_key=" + api_key + "&format=xml";
|
|
|
|
} else {
|
|
|
|
url += "api_key=" + api_key + "&format=xml";
|
|
|
|
}
|
|
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, url.c_str() );
|
|
|
|
|
|
|
|
// perform online request
|
|
|
|
CURLcode res = curl_easy_perform(curl);
|
|
|
|
if( res != 0 ) {
|
2012-11-13 10:11:07 -05:00
|
|
|
error << string_compose (_("curl error %1 (%2)"), res, curl_easy_strerror(res)) << endmsg;
|
2011-11-14 17:04:14 -05:00
|
|
|
return "";
|
|
|
|
}
|
2008-04-11 10:06:50 -04:00
|
|
|
|
2011-11-14 17:04:14 -05:00
|
|
|
// free the memory
|
2012-04-06 22:08:18 -04:00
|
|
|
if (xml_page.memory) {
|
|
|
|
result = xml_page.memory;
|
2008-04-11 10:06:50 -04:00
|
|
|
}
|
2012-04-06 22:08:18 -04:00
|
|
|
|
|
|
|
free (xml_page.memory);
|
|
|
|
xml_page.memory = NULL;
|
|
|
|
xml_page.size = 0;
|
2011-06-01 13:00:29 -04:00
|
|
|
|
2011-11-14 17:04:14 -05:00
|
|
|
return result;
|
|
|
|
}
|
2011-06-01 13:00:29 -04:00
|
|
|
|
2008-04-11 10:06:50 -04:00
|
|
|
|
2013-06-14 14:17:24 -04:00
|
|
|
std::string Mootcher::searchSimilar(std::string id)
|
|
|
|
{
|
|
|
|
std::string params = "";
|
|
|
|
|
|
|
|
params += "&fields=id,original_filename,duration,filesize,samplerate,license,serve";
|
|
|
|
params += "&num_results=100";
|
|
|
|
|
|
|
|
return doRequest("/sounds/" + id + "/similar", params);
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------
|
|
|
|
|
2011-11-14 17:04:14 -05:00
|
|
|
std::string Mootcher::searchText(std::string query, int page, std::string filter, enum sortMethod sort)
|
|
|
|
{
|
|
|
|
std::string params = "";
|
|
|
|
char buf[24];
|
2008-04-11 10:06:50 -04:00
|
|
|
|
2011-11-14 17:04:14 -05:00
|
|
|
if (page > 1) {
|
|
|
|
snprintf(buf, 23, "p=%d&", page);
|
|
|
|
params += buf;
|
2008-04-11 10:06:50 -04:00
|
|
|
}
|
|
|
|
|
2012-12-21 08:44:53 -05:00
|
|
|
char *eq = curl_easy_escape(curl, query.c_str(), query.length());
|
2012-12-21 12:28:44 -05:00
|
|
|
params += "q=\"" + std::string(eq) + "\"";
|
2012-12-21 08:44:53 -05:00
|
|
|
free(eq);
|
|
|
|
|
|
|
|
if (filter != "") {
|
|
|
|
char *ef = curl_easy_escape(curl, filter.c_str(), filter.length());
|
|
|
|
params += "&f=" + std::string(ef);
|
|
|
|
free(ef);
|
|
|
|
}
|
2011-11-14 17:04:14 -05:00
|
|
|
|
|
|
|
if (sort)
|
|
|
|
params += "&s=" + sortMethodString(sort);
|
2008-04-11 10:06:50 -04:00
|
|
|
|
2012-12-21 14:32:19 -05:00
|
|
|
params += "&fields=id,original_filename,duration,filesize,samplerate,license,serve";
|
2013-03-15 15:59:19 -04:00
|
|
|
params += "&sounds_per_page=100";
|
2012-04-05 17:31:55 -04:00
|
|
|
|
2011-11-14 17:04:14 -05:00
|
|
|
return doRequest("/sounds/search", params);
|
2008-04-11 10:06:50 -04:00
|
|
|
}
|
2011-11-14 17:04:14 -05:00
|
|
|
|
2008-04-11 10:06:50 -04:00
|
|
|
//------------------------------------------------------------------------
|
2011-11-14 17:04:14 -05:00
|
|
|
|
|
|
|
std::string Mootcher::getSoundResourceFile(std::string ID)
|
2008-04-11 10:06:50 -04:00
|
|
|
{
|
|
|
|
|
2011-11-14 17:04:14 -05:00
|
|
|
std::string originalSoundURI;
|
|
|
|
std::string audioFileName;
|
|
|
|
std::string xml;
|
2008-04-11 10:06:50 -04:00
|
|
|
|
|
|
|
|
2011-11-14 17:04:14 -05:00
|
|
|
// download the xmlfile into xml_page
|
|
|
|
xml = doRequest("/sounds/" + ID, "");
|
|
|
|
|
|
|
|
XMLTree doc;
|
|
|
|
doc.read_buffer( xml.c_str() );
|
|
|
|
XMLNode *freesound = doc.root();
|
|
|
|
|
|
|
|
// if the page is not a valid xml document with a 'freesound' root
|
|
|
|
if (freesound == NULL) {
|
2012-11-13 10:11:07 -05:00
|
|
|
error << _("getSoundResourceFile: There is no valid root in the xml file") << endmsg;
|
2011-11-14 17:04:14 -05:00
|
|
|
return "";
|
2008-04-11 10:06:50 -04:00
|
|
|
}
|
|
|
|
|
2011-11-14 17:04:14 -05:00
|
|
|
if (strcmp(doc.root()->name().c_str(), "response") != 0) {
|
2012-11-13 10:11:07 -05:00
|
|
|
error << string_compose (_("getSoundResourceFile: root = %1, != response"), doc.root()->name()) << endmsg;
|
2011-11-14 17:04:14 -05:00
|
|
|
return "";
|
|
|
|
}
|
2011-06-01 13:00:29 -04:00
|
|
|
|
2011-11-14 17:04:14 -05:00
|
|
|
XMLNode *name = freesound->child("original_filename");
|
|
|
|
|
|
|
|
// get the file name and size from xml file
|
2012-04-05 17:31:55 -04:00
|
|
|
if (name) {
|
2011-11-14 17:04:14 -05:00
|
|
|
|
2013-06-14 14:09:40 -04:00
|
|
|
audioFileName = Glib::build_filename (basePath, ID + "-" + name->child("text")->content());
|
2011-11-14 17:04:14 -05:00
|
|
|
|
|
|
|
//store all the tags in the database
|
|
|
|
XMLNode *tags = freesound->child("tags");
|
|
|
|
if (tags) {
|
|
|
|
XMLNodeList children = tags->children();
|
|
|
|
XMLNodeConstIterator niter;
|
|
|
|
std::vector<std::string> strings;
|
|
|
|
for (niter = children.begin(); niter != children.end(); ++niter) {
|
|
|
|
XMLNode *node = *niter;
|
|
|
|
if( strcmp( node->name().c_str(), "resource") == 0 ) {
|
|
|
|
XMLNode *text = node->child("text");
|
|
|
|
if (text) {
|
|
|
|
// std::cerr << "tag: " << text->content() << std::endl;
|
|
|
|
strings.push_back(text->content());
|
2008-04-11 10:06:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-11-14 17:04:14 -05:00
|
|
|
ARDOUR::Library->set_tags (std::string("//")+audioFileName, strings);
|
|
|
|
ARDOUR::Library->save_changes ();
|
2008-04-11 10:06:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-14 17:04:14 -05:00
|
|
|
return audioFileName;
|
2008-04-11 10:06:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int audioFileWrite(void *buffer, size_t size, size_t nmemb, void *file)
|
|
|
|
{
|
|
|
|
return (int)fwrite(buffer, size, nmemb, (FILE*) file);
|
|
|
|
};
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------
|
2013-06-14 14:17:24 -04:00
|
|
|
|
|
|
|
void *
|
|
|
|
Mootcher::threadFunc() {
|
|
|
|
CURLcode res;
|
|
|
|
|
|
|
|
res = curl_easy_perform (curl);
|
|
|
|
fclose (theFile);
|
|
|
|
curl_easy_setopt (curl, CURLOPT_NOPROGRESS, 1); // turn off the progress bar
|
|
|
|
|
|
|
|
if (res != CURLE_OK) {
|
|
|
|
/* it's not an error if the user pressed the stop button */
|
|
|
|
if (res != CURLE_ABORTED_BY_CALLBACK) {
|
|
|
|
error << string_compose (_("curl error %1 (%2)"), res, curl_easy_strerror(res)) << endmsg;
|
|
|
|
}
|
|
|
|
remove ( (audioFileName+".part").c_str() );
|
|
|
|
} else {
|
|
|
|
rename ( (audioFileName+".part").c_str(), audioFileName.c_str() );
|
|
|
|
// now download the tags &c.
|
|
|
|
getSoundResourceFile(ID);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (void *) res;
|
|
|
|
}
|
2013-06-16 10:43:43 -04:00
|
|
|
|
|
|
|
void
|
|
|
|
Mootcher::doneWithMootcher()
|
2013-06-14 14:17:24 -04:00
|
|
|
{
|
|
|
|
|
|
|
|
// update the sound info pane if the selection in the list box is still us
|
2013-06-16 10:43:43 -04:00
|
|
|
sfb->refresh_display(ID, audioFileName);
|
2013-06-14 14:17:24 -04:00
|
|
|
|
2013-06-17 11:46:18 -04:00
|
|
|
delete this; // this should be OK to do as long as Progress and Finished signals are always received in the order in which they are emitted
|
2013-06-14 14:17:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
|
|
|
freesound_download_thread_func(void *arg)
|
|
|
|
{
|
|
|
|
Mootcher *thisMootcher = (Mootcher *) arg;
|
|
|
|
void *res;
|
|
|
|
|
|
|
|
// std::cerr << "freesound_download_thread_func(" << arg << ")" << std::endl;
|
|
|
|
res = thisMootcher->threadFunc();
|
|
|
|
|
2013-06-16 10:43:43 -04:00
|
|
|
thisMootcher->Finished(); /* EMIT SIGNAL */
|
2013-06-14 14:17:24 -04:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------
|
|
|
|
|
|
|
|
bool Mootcher::checkAudioFile(std::string originalFileName, std::string theID)
|
2008-04-11 10:06:50 -04:00
|
|
|
{
|
2011-12-27 09:36:14 -05:00
|
|
|
ensureWorkingDir();
|
2013-06-14 14:17:24 -04:00
|
|
|
ID = theID;
|
2013-06-14 14:09:40 -04:00
|
|
|
audioFileName = Glib::build_filename (basePath, ID + "-" + originalFileName);
|
2008-04-11 10:06:50 -04:00
|
|
|
|
2012-03-09 17:02:48 -05:00
|
|
|
// check to see if audio file already exists
|
2012-03-13 16:15:37 -04:00
|
|
|
FILE *testFile = g_fopen(audioFileName.c_str(), "r");
|
2011-11-14 17:04:14 -05:00
|
|
|
if (testFile) {
|
|
|
|
fseek (testFile , 0 , SEEK_END);
|
|
|
|
if (ftell (testFile) > 256) {
|
|
|
|
fclose (testFile);
|
2013-06-14 14:17:24 -04:00
|
|
|
return true;
|
2011-11-14 17:04:14 -05:00
|
|
|
}
|
|
|
|
|
2013-06-14 14:17:24 -04:00
|
|
|
// else file was small, probably an error, delete it
|
2011-11-14 17:04:14 -05:00
|
|
|
fclose(testFile);
|
|
|
|
remove( audioFileName.c_str() );
|
|
|
|
}
|
2013-06-14 14:17:24 -04:00
|
|
|
return false;
|
|
|
|
}
|
2011-06-01 13:00:29 -04:00
|
|
|
|
2012-03-09 17:02:48 -05:00
|
|
|
|
2013-06-14 14:17:24 -04:00
|
|
|
bool Mootcher::fetchAudioFile(std::string originalFileName, std::string theID, std::string audioURL, SoundFileBrowser *caller)
|
|
|
|
{
|
|
|
|
ensureWorkingDir();
|
|
|
|
ID = theID;
|
|
|
|
audioFileName = Glib::build_filename (basePath, ID + "-" + originalFileName);
|
|
|
|
|
|
|
|
if (!curl) {
|
|
|
|
return false;
|
2012-04-05 17:31:55 -04:00
|
|
|
}
|
2013-03-15 15:59:19 -04:00
|
|
|
// now download the actual file
|
2013-06-14 14:17:24 -04:00
|
|
|
theFile = g_fopen( (audioFileName + ".part").c_str(), "wb" );
|
2008-04-11 10:06:50 -04:00
|
|
|
|
2012-03-09 17:02:48 -05:00
|
|
|
if (!theFile) {
|
2013-06-14 14:17:24 -04:00
|
|
|
return false;
|
2012-03-09 17:02:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// create the download url
|
|
|
|
audioURL += "?api_key=" + api_key;
|
2008-04-11 10:06:50 -04:00
|
|
|
|
2012-03-09 17:02:48 -05:00
|
|
|
setcUrlOptions();
|
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, audioURL.c_str() );
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, audioFileWrite);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, theFile);
|
|
|
|
|
2012-04-05 17:31:55 -04:00
|
|
|
std::string prog;
|
2012-12-21 12:28:44 -05:00
|
|
|
prog = string_compose (_("%1"), originalFileName);
|
2013-06-14 14:17:24 -04:00
|
|
|
progress_bar.set_text(prog);
|
|
|
|
|
|
|
|
Gtk::VBox *freesound_vbox = dynamic_cast<Gtk::VBox *> (caller->notebook.get_nth_page(2));
|
|
|
|
freesound_vbox->pack_start(progress_hbox, Gtk::PACK_SHRINK);
|
|
|
|
progress_hbox.show();
|
|
|
|
cancel_download = false;
|
|
|
|
sfb = caller;
|
2012-03-09 17:02:48 -05:00
|
|
|
|
|
|
|
curl_easy_setopt (curl, CURLOPT_NOPROGRESS, 0); // turn on the progress bar
|
|
|
|
curl_easy_setopt (curl, CURLOPT_PROGRESSFUNCTION, progress_callback);
|
2013-06-14 14:17:24 -04:00
|
|
|
curl_easy_setopt (curl, CURLOPT_PROGRESSDATA, this);
|
2012-03-09 17:02:48 -05:00
|
|
|
|
2013-06-16 10:43:43 -04:00
|
|
|
Progress.connect(*this, invalidator (*this), boost::bind(&Mootcher::updateProgress, this, _1, _2), gui_context());
|
|
|
|
Finished.connect(*this, invalidator (*this), boost::bind(&Mootcher::doneWithMootcher, this), gui_context());
|
2013-06-14 14:17:24 -04:00
|
|
|
pthread_t freesound_download_thread;
|
|
|
|
pthread_create_and_store("freesound_import", &freesound_download_thread, freesound_download_thread_func, this);
|
2012-03-09 17:02:48 -05:00
|
|
|
|
2013-06-14 14:17:24 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------
|
|
|
|
|
2013-06-16 10:43:43 -04:00
|
|
|
void
|
|
|
|
Mootcher::updateProgress(double dlnow, double dltotal)
|
2013-06-14 14:17:24 -04:00
|
|
|
{
|
2013-06-16 10:43:43 -04:00
|
|
|
if (dltotal > 0) {
|
|
|
|
double fraction = dlnow / dltotal;
|
2013-06-14 14:17:24 -04:00
|
|
|
// std::cerr << "progress idle: " << progress->bar->get_text() << ". " << progress->dlnow << " / " << progress->dltotal << " = " << fraction << std::endl;
|
|
|
|
if (fraction > 1.0) {
|
|
|
|
fraction = 1.0;
|
|
|
|
} else if (fraction < 0.0) {
|
|
|
|
fraction = 0.0;
|
2013-03-15 15:59:19 -04:00
|
|
|
}
|
2013-06-16 10:43:43 -04:00
|
|
|
progress_bar.set_fraction(fraction);
|
2008-04-11 10:06:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-14 14:17:24 -04:00
|
|
|
int
|
|
|
|
Mootcher::progress_callback(void *caller, double dltotal, double dlnow, double /*ultotal*/, double /*ulnow*/)
|
2008-04-11 10:06:50 -04:00
|
|
|
{
|
2013-06-14 14:17:24 -04:00
|
|
|
// It may seem curious to pass a pointer to an instance of an object to a static
|
|
|
|
// member function, but we can't use a normal member function as a curl progress callback,
|
|
|
|
// and we want access to some private members of Mootcher.
|
2011-11-14 17:04:14 -05:00
|
|
|
|
2013-06-14 14:17:24 -04:00
|
|
|
Mootcher *thisMootcher = (Mootcher *) caller;
|
2012-01-18 13:43:43 -05:00
|
|
|
|
2013-06-14 14:17:24 -04:00
|
|
|
if (thisMootcher->cancel_download) {
|
|
|
|
return -1;
|
2011-11-14 17:04:14 -05:00
|
|
|
}
|
2013-06-14 14:17:24 -04:00
|
|
|
|
2013-06-16 10:43:43 -04:00
|
|
|
thisMootcher->Progress(dlnow, dltotal); /* EMIT SIGNAL */
|
2008-04-11 10:06:50 -04:00
|
|
|
return 0;
|
|
|
|
}
|
2011-11-14 17:04:14 -05:00
|
|
|
|