#include "sfdb_freesound_mootcher.h"\r
\r
#include "pbd/xml++.h"\r
-#include "pbd/filesystem.h"\r
+#include "pbd/error.h"\r
\r
#include <sys/stat.h>\r
#include <sys/types.h>\r
#include <iostream>\r
\r
+#include <glib.h>\r
+#include <glib/gstdio.h>\r
+\r
+#include "i18n.h"\r
+\r
#include "ardour/audio_library.h"\r
\r
+using namespace PBD;\r
+\r
static const std::string base_url = "http://www.freesound.org/api";\r
static const std::string api_key = "9d77cb8d841b4bcfa960e1aae62224eb"; // ardour3\r
\r
-\r
//------------------------------------------------------------------------\r
-Mootcher::Mootcher(const char *saveLocation)\r
+Mootcher::Mootcher()\r
: curl(curl_easy_init())\r
{\r
- changeWorkingDir(saveLocation);\r
+ std::string path;\r
+ path = Glib::get_home_dir() + "/Freesound/";\r
+ changeWorkingDir ( path.c_str() );\r
};\r
//------------------------------------------------------------------------\r
Mootcher:: ~Mootcher()\r
{\r
+ curl_easy_cleanup(curl);\r
}\r
\r
//------------------------------------------------------------------------\r
\r
void Mootcher::ensureWorkingDir ()\r
{\r
- PBD::sys::path p = basePath;\r
- p /= "snd";\r
- if (!PBD::sys::is_directory (p)) {\r
- PBD::sys::create_directories (p);\r
+ std::string p = Glib::build_filename (basePath, "snd");\r
+\r
+ if (!Glib::file_test (p, Glib::FILE_TEST_IS_DIR)) {\r
+ if (g_mkdir_with_parents (p.c_str(), 0775) != 0) {\r
+ PBD::error << "Unable to create Mootcher working dir" << endmsg;\r
+ }\r
}\r
}\r
\r
//------------------------------------------------------------------------\r
\r
std::string Mootcher::sortMethodString(enum sortMethod sort) {\r
+// given a sort type, returns the string value to be passed to the API to\r
+// sort the results in the requested way.\r
\r
switch (sort) {\r
case sort_duration_desc: return "duration_desc"; \r
\r
// Allow connections to time out (without using signals)\r
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);\r
- curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10);\r
+ curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 30);\r
\r
\r
}\r
}\r
\r
curl_easy_setopt(curl, CURLOPT_URL, url.c_str() );\r
- std::cerr << "doRequest: " << url << std::endl;\r
\r
// perform online request\r
CURLcode res = curl_easy_perform(curl);\r
if( res != 0 ) {\r
- std::cerr << "curl error " << res << " (" << curl_easy_strerror(res) << ")" << std::endl;\r
+ error << string_compose (_("curl error %1 (%2)"), res, curl_easy_strerror(res)) << endmsg;\r
return "";\r
}\r
\r
- result = xml_page.memory;\r
// free the memory\r
- if(xml_page.memory){\r
- free( xml_page.memory );\r
- xml_page.memory = NULL;\r
- xml_page.size = 0;\r
+ if (xml_page.memory) {\r
+ result = xml_page.memory;\r
}\r
+ \r
+ free (xml_page.memory);\r
+ xml_page.memory = NULL;\r
+ xml_page.size = 0;\r
\r
return result;\r
-\r
}\r
\r
\r
snprintf(buf, 23, "p=%d&", page);\r
params += buf;\r
}\r
- \r
- params += "q=" + query; \r
\r
- if (filter != "")\r
- params += "&f=" + filter;\r
+ char *eq = curl_easy_escape(curl, query.c_str(), query.length());\r
+ params += "q=\"" + std::string(eq) + "\"";\r
+ free(eq);\r
+\r
+ if (filter != "") {\r
+ char *ef = curl_easy_escape(curl, filter.c_str(), filter.length());\r
+ params += "&f=" + std::string(ef);\r
+ free(ef);\r
+ }\r
\r
if (sort)\r
params += "&s=" + sortMethodString(sort);\r
\r
+ params += "&fields=id,original_filename,duration,filesize,samplerate,license,serve";\r
+\r
return doRequest("/sounds/search", params);\r
}\r
\r
\r
std::string originalSoundURI;\r
std::string audioFileName;\r
- std::string xmlFileName;\r
std::string xml;\r
\r
\r
- std::cerr << "getSoundResourceFile(" << ID << ")" << std::endl;\r
-\r
// download the xmlfile into xml_page\r
xml = doRequest("/sounds/" + ID, "");\r
\r
\r
// if the page is not a valid xml document with a 'freesound' root\r
if (freesound == NULL) {\r
- std::cerr << "getSoundResourceFile: There is no valid root in the xml file" << std::endl;\r
+ error << _("getSoundResourceFile: There is no valid root in the xml file") << endmsg;\r
return "";\r
}\r
\r
if (strcmp(doc.root()->name().c_str(), "response") != 0) {\r
- std::cerr << "getSoundResourceFile: root =" << doc.root()->name() << ", != response" << std::endl;\r
+ error << string_compose (_("getSoundResourceFile: root = %1, != response"), doc.root()->name()) << endmsg;\r
return "";\r
}\r
\r
XMLNode *name = freesound->child("original_filename");\r
- XMLNode *filesize = freesound->child("filesize");\r
-\r
\r
// get the file name and size from xml file\r
- if (name && filesize) {\r
+ if (name) {\r
\r
audioFileName = basePath + "snd/" + ID + "-" + name->child("text")->content();\r
\r
- // create new filename with the ID number\r
- xmlFileName = basePath;\r
- xmlFileName += "snd/";\r
- xmlFileName += freesound->child("id")->child("text")->content();\r
- xmlFileName += "-";\r
- xmlFileName += name->child("text")->content();\r
- xmlFileName += ".xml";\r
-\r
- // std::cerr << "getSoundResourceFile: saving XML: " << xmlFileName << std::endl;\r
-\r
- // save the xml file to disk\r
- ensureWorkingDir();\r
- doc.write(xmlFileName.c_str());\r
-\r
//store all the tags in the database\r
XMLNode *tags = freesound->child("tags");\r
if (tags) {\r
};\r
\r
//------------------------------------------------------------------------\r
-std::string Mootcher::getAudioFile(std::string originalFileName, std::string ID, std::string audioURL, Gtk::ProgressBar *progress_bar)\r
+std::string Mootcher::getAudioFile(std::string originalFileName, std::string ID, std::string audioURL, SoundFileBrowser *caller)\r
{\r
ensureWorkingDir();\r
std::string audioFileName = basePath + "snd/" + ID + "-" + originalFileName;\r
\r
- //check to see if audio file already exists\r
- FILE *testFile = fopen(audioFileName.c_str(), "r");\r
+ // check to see if audio file already exists\r
+ FILE *testFile = g_fopen(audioFileName.c_str(), "r");\r
if (testFile) { \r
fseek (testFile , 0 , SEEK_END);\r
if (ftell (testFile) > 256) {\r
- std::cerr << "audio file " << audioFileName << " already exists" << std::endl;\r
fclose (testFile);\r
return audioFileName;\r
}\r
remove( audioFileName.c_str() ); \r
}\r
\r
+ if (!curl) {\r
+ return "";\r
+ }\r
+\r
+ //if already canceling a previous download, bail out here ( this can happen b/c getAudioFile gets called by various UI update funcs )\r
+ if ( caller->freesound_download_cancel ) {\r
+ return "";\r
+ }\r
+ \r
//now download the actual file\r
- if (curl) {\r
+ FILE* theFile;\r
+ theFile = g_fopen( audioFileName.c_str(), "wb" );\r
\r
- FILE* theFile;\r
- theFile = fopen( audioFileName.c_str(), "wb" );\r
+ if (!theFile) {\r
+ return "";\r
+ }\r
+ \r
+ // create the download url\r
+ audioURL += "?api_key=" + api_key;\r
\r
- if (theFile) {\r
- \r
- // create the download url\r
- audioURL += "?api_key=" + api_key;\r
- \r
- setcUrlOptions();\r
- curl_easy_setopt(curl, CURLOPT_URL, audioURL.c_str() );\r
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, audioFileWrite);\r
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, theFile);\r
-\r
- std::cerr << "downloading " << audioFileName << " from " << audioURL << "..." << std::endl;\r
-\r
- curl_easy_setopt (curl, CURLOPT_NOPROGRESS, 0); // turn on the progress bar\r
- curl_easy_setopt (curl, CURLOPT_PROGRESSFUNCTION, progress_callback);\r
- curl_easy_setopt (curl, CURLOPT_PROGRESSDATA, progress_bar);\r
-\r
- CURLcode res = curl_easy_perform(curl);\r
- fclose(theFile);\r
-\r
- curl_easy_setopt (curl, CURLOPT_NOPROGRESS, 1); // turn off the progress bar\r
- progress_bar->set_fraction(0.0);\r
-\r
- if( res != 0 ) {\r
- std::cerr << "curl error " << res << " (" << curl_easy_strerror(res) << ")" << std::endl;\r
- remove( audioFileName.c_str() ); \r
- return "";\r
- } else {\r
- std::cerr << "done!" << std::endl;\r
- // now download the tags &c.\r
- getSoundResourceFile(ID);\r
- }\r
- }\r
+ setcUrlOptions();\r
+ curl_easy_setopt(curl, CURLOPT_URL, audioURL.c_str() );\r
+ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, audioFileWrite);\r
+ curl_easy_setopt(curl, CURLOPT_WRITEDATA, theFile);\r
+\r
+ /* hack to get rid of the barber-pole stripes */\r
+ caller->freesound_progress_bar.hide();\r
+ caller->freesound_progress_bar.show();\r
+\r
+ std::string prog;\r
+ prog = string_compose (_("%1"), originalFileName);\r
+ caller->freesound_progress_bar.set_text(prog);\r
+\r
+ curl_easy_setopt (curl, CURLOPT_NOPROGRESS, 0); // turn on the progress bar\r
+ curl_easy_setopt (curl, CURLOPT_PROGRESSFUNCTION, progress_callback);\r
+ curl_easy_setopt (curl, CURLOPT_PROGRESSDATA, caller);\r
+\r
+ CURLcode res = curl_easy_perform(curl);\r
+ fclose(theFile);\r
+\r
+ curl_easy_setopt (curl, CURLOPT_NOPROGRESS, 1); // turn off the progress bar\r
+ caller->freesound_progress_bar.set_fraction(0.0);\r
+ caller->freesound_progress_bar.set_text("");\r
+ \r
+ if( res != 0 ) {\r
+ error << string_compose (_("curl error %1 (%2)"), res, curl_easy_strerror(res)) << endmsg;\r
+ remove( audioFileName.c_str() ); \r
+ return "";\r
+ } else {\r
+ // now download the tags &c.\r
+ getSoundResourceFile(ID);\r
}\r
\r
return audioFileName;\r
}\r
\r
//---------\r
-int Mootcher::progress_callback(void *bar, double dltotal, double dlnow, double ultotal, double ulnow)\r
+int Mootcher::progress_callback(void *caller, double dltotal, double dlnow, double /*ultotal*/, double /*ulnow*/)\r
{\r
\r
+SoundFileBrowser *sfb = (SoundFileBrowser *) caller;\r
//XXX I hope it's OK to do GTK things in this callback. Otherwise\r
// I'll have to do stuff like in interthread_progress_window.\r
+ if (sfb->freesound_download_cancel) {\r
+ return -1;\r
+ }\r
+ \r
\r
- Gtk::ProgressBar *progress_bar = (Gtk::ProgressBar *) bar;\r
- progress_bar->set_fraction(dlnow/dltotal);\r
+ sfb->freesound_progress_bar.set_fraction(dlnow/dltotal);\r
/* Make sure the progress widget gets updated */\r
while (Glib::MainContext::get_default()->iteration (false)) {\r
/* do nothing */\r
}\r
- std::cerr << "progress: " << dlnow << " of " << dltotal << " \r";\r
return 0;\r
}\r
\r