enough with umpteen "i18n.h" files. Consolidate on pbd/i18n.h
[ardour.git] / gtk2_ardour / sfdb_freesound_mootcher.cc
index 1444660a88ef245af028edb33827283829af5cfe..74290df0a3dad079d0b72aff297f14ac1943e00c 100644 (file)
-/* sfdb_freesound_mootcher.cpp **********************************************************************\r
-\r
-       Adapted for Ardour by Ben Loftis, March 2008\r
-       Updated to new Freesound API by Colin Fletcher, November 2011\r
-\r
-       Mootcher 23-8-2005\r
-\r
-       Mootcher Online Access to thefreesoundproject website\r
-       http://freesound.iua.upf.edu/\r
-\r
-       GPL 2005 Jorn Lemon\r
-       mail for questions/remarks: mootcher@twistedlemon.nl\r
-       or go to the freesound website forum\r
-\r
-       -----------------------------------------------------------------\r
-\r
-       Includes:\r
-               curl.h    (version 7.14.0)\r
-       Librarys:\r
-               libcurl.lib\r
-\r
-       -----------------------------------------------------------------\r
-       Licence GPL:\r
-\r
-       This program is free software; you can redistribute it and/or\r
-       modify it under the terms of the GNU General Public License\r
-       as published by the Free Software Foundation; either version 2\r
-       of the License, or (at your option) any later version.\r
-\r
-       This program is distributed in the hope that it will be useful,\r
-       but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
-       GNU General Public License for more details.\r
-\r
-       You should have received a copy of the GNU General Public License\r
-       along with this program; if not, write to the Free Software\r
-       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.\r
-\r
-\r
-*************************************************************************************/\r
-#include "sfdb_freesound_mootcher.h"\r
-\r
-#include "pbd/xml++.h"\r
-#include "pbd/filesystem.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 "ardour/audio_library.h"\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()\r
-       : curl(curl_easy_init())\r
-{\r
-       std::string path;\r
-       path = Glib::get_home_dir() + "/Freesound/";\r
-       changeWorkingDir ( path.c_str() );\r
-};\r
-//------------------------------------------------------------------------\r
-Mootcher:: ~Mootcher()\r
-{\r
-}\r
-\r
-//------------------------------------------------------------------------\r
-void Mootcher::changeWorkingDir(const char *saveLocation)\r
-{\r
-       basePath = saveLocation;\r
-#ifdef __WIN32__\r
-       std::string replace = "/";\r
-       size_t pos = basePath.find("\\");\r
-       while( pos != std::string::npos ){\r
-               basePath.replace(pos, 1, replace);\r
-               pos = basePath.find("\\");\r
-       }\r
-#endif\r
-       //\r
-       size_t pos2 = basePath.find_last_of("/");\r
-       if(basePath.length() != (pos2+1)) basePath += "/";\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
-       }\r
-}\r
-       \r
-\r
-//------------------------------------------------------------------------\r
-size_t Mootcher::WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)\r
-{\r
-       register int realsize = (int)(size * nmemb);\r
-       struct MemoryStruct *mem = (struct MemoryStruct *)data;\r
-\r
-       mem->memory = (char *)realloc(mem->memory, mem->size + realsize + 1);\r
-\r
-       if (mem->memory) {\r
-               memcpy(&(mem->memory[mem->size]), ptr, realsize);\r
-               mem->size += realsize;\r
-               mem->memory[mem->size] = 0;\r
-       }\r
-       return realsize;\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
-               case sort_duration_asc:         return "duration_asc";\r
-               case sort_created_desc:         return "created_desc";\r
-               case sort_created_asc:          return "created_asc";\r
-               case sort_downloads_desc:       return "downloads_desc";\r
-               case sort_downloads_asc:        return "downloads_asc";\r
-               case sort_rating_desc:          return "rating_desc";\r
-               case sort_rating_asc:           return "rating_asc";\r
-               default:                        return "";      \r
-       }\r
-}\r
-\r
-//------------------------------------------------------------------------\r
-void Mootcher::setcUrlOptions()\r
-{\r
-       // basic init for curl\r
-       curl_global_init(CURL_GLOBAL_ALL);\r
-       // some servers don't like requests that are made without a user-agent field, so we provide one\r
-       curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");\r
-       // setup curl error buffer\r
-       curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);\r
-       // Allow redirection\r
-       curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);\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, 30);\r
-\r
-\r
-}\r
-\r
-std::string Mootcher::doRequest(std::string uri, std::string params)\r
-{\r
-       std::string result;\r
-       struct MemoryStruct xml_page;\r
-       xml_page.memory = NULL;\r
-       xml_page.size = 0;\r
-\r
-       setcUrlOptions();\r
-       curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);\r
-       curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) &xml_page);\r
-\r
-       // curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);\r
-       // curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postMessage.c_str());\r
-       // curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, -1);\r
-\r
-       // the url to get\r
-       std::string url = base_url + uri + "?";\r
-       if (params != "") {\r
-               url += params + "&api_key=" + api_key + "&format=xml";\r
-       } else {\r
-               url += "api_key=" + api_key + "&format=xml";\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
-               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
-       }\r
-\r
-       return result;\r
-\r
-}\r
-\r
-\r
-std::string Mootcher::searchText(std::string query, int page, std::string filter, enum sortMethod sort)\r
-{\r
-       std::string params = "";\r
-       char buf[24];\r
-\r
-       if (page > 1) {\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
-       \r
-       if (sort)\r
-               params += "&s=" + sortMethodString(sort);\r
-\r
-       return doRequest("/sounds/search", params);\r
-}\r
-\r
-//------------------------------------------------------------------------\r
-\r
-std::string Mootcher::getSoundResourceFile(std::string ID)\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
-       XMLTree doc;\r
-       doc.read_buffer( xml.c_str() );\r
-       XMLNode *freesound = doc.root();\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
-               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
-               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
-\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
-                       XMLNodeList children = tags->children();\r
-                       XMLNodeConstIterator niter;\r
-                       std::vector<std::string> strings;\r
-                       for (niter = children.begin(); niter != children.end(); ++niter) {\r
-                               XMLNode *node = *niter;\r
-                               if( strcmp( node->name().c_str(), "resource") == 0 ) {\r
-                                       XMLNode *text = node->child("text");\r
-                                       if (text) {\r
-                                               // std::cerr << "tag: " << text->content() << std::endl;\r
-                                               strings.push_back(text->content());\r
-                                       }\r
-                               }\r
-                       }\r
-                       ARDOUR::Library->set_tags (std::string("//")+audioFileName, strings);\r
-                       ARDOUR::Library->save_changes ();\r
-               }\r
-       }\r
-\r
-       return audioFileName;\r
-}\r
-\r
-int audioFileWrite(void *buffer, size_t size, size_t nmemb, void *file)\r
-{\r
-       return (int)fwrite(buffer, size, nmemb, (FILE*) file);\r
-};\r
-\r
-//------------------------------------------------------------------------\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 = 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
-               \r
-               // else file was small, probably an error, delete it and try again\r
-               fclose(testFile);\r
-               remove( audioFileName.c_str() );  \r
-       }\r
-\r
-       if (!curl) {\r
-               return "";\r
-       }\r
-\r
-       //now download the actual file\r
-       FILE* theFile;\r
-       theFile = g_fopen( audioFileName.c_str(), "wb" );\r
-\r
-       if (!theFile) {\r
-               return "";\r
-       }\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
-       /* hack to get rid of the barber-pole stripes */\r
-       caller->progress_bar.hide();\r
-       caller->progress_bar.show();\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->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
-       return audioFileName;\r
-}\r
-\r
-//---------\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_stop) {\r
-               return -1;\r
-       }\r
-       \r
-       \r
-       sfb->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
+/* sfdb_freesound_mootcher.cpp **********************************************************************
+
+       Adapted for Ardour by Ben Loftis, March 2008
+       Updated to new Freesound API by Colin Fletcher, November 2011
+
+       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"
+
+#include "pbd/xml++.h"
+#include "pbd/error.h"
+
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <iostream>
+
+#include <glib.h>
+#include "pbd/gstdio_compat.h"
+
+#include "pbd/i18n.h"
+
+#include "ardour/audio_library.h"
+#include "ardour/rc_configuration.h"
+#include "pbd/pthread_utils.h"
+#include "gui_thread.h"
+
+using namespace PBD;
+
+static const std::string base_url = "http://www.freesound.org/api";
+static const std::string api_key = "9d77cb8d841b4bcfa960e1aae62224eb"; // ardour3
+
+//------------------------------------------------------------------------
+Mootcher::Mootcher()
+       : curl(curl_easy_init())
+{
+       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));
+};
+//------------------------------------------------------------------------
+Mootcher:: ~Mootcher()
+{
+       curl_easy_cleanup(curl);
+}
+
+//------------------------------------------------------------------------
+
+void Mootcher::ensureWorkingDir ()
+{
+       std::string p = ARDOUR::Config->get_freesound_download_dir();
+
+       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;
+               }
+       }
+       basePath = p;
+#ifdef PLATFORM_WINDOWS
+       std::string replace = "/";
+       size_t pos = basePath.find("\\");
+       while( pos != std::string::npos ){
+               basePath.replace(pos, 1, replace);
+               pos = basePath.find("\\");
+       }
+#endif
+}
+
+
+//------------------------------------------------------------------------
+size_t Mootcher::WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
+{
+       int realsize = (int)(size * nmemb);
+       struct MemoryStruct *mem = (struct MemoryStruct *)data;
+
+       mem->memory = (char *)realloc(mem->memory, mem->size + realsize + 1);
+
+       if (mem->memory) {
+               memcpy(&(mem->memory[mem->size]), ptr, realsize);
+               mem->size += realsize;
+               mem->memory[mem->size] = 0;
+       }
+       return realsize;
+}
+
+
+//------------------------------------------------------------------------
+
+std::string Mootcher::sortMethodString(enum sortMethod sort)
+{
+// given a sort type, returns the string value to be passed to the API to
+// sort the results in the requested way.
+
+       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 "";
+       }
+}
+
+//------------------------------------------------------------------------
+void Mootcher::setcUrlOptions()
+{
+       // 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
+       curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);
+       // Allow redirection
+       curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
+
+       // Allow connections to time out (without using signals)
+       curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
+       curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 30);
+
+
+}
+
+std::string Mootcher::doRequest(std::string uri, std::string params)
+{
+       std::string result;
+       struct MemoryStruct xml_page;
+       xml_page.memory = NULL;
+       xml_page.size = 0;
+
+       setcUrlOptions();
+       curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
+       curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) &xml_page);
+
+       // curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
+       // curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postMessage.c_str());
+       // curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, -1);
+
+       // 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 ) {
+               error << string_compose (_("curl error %1 (%2)"), res, curl_easy_strerror(res)) << endmsg;
+               return "";
+       }
+
+       // free the memory
+       if (xml_page.memory) {
+               result = xml_page.memory;
+       }
+
+       free (xml_page.memory);
+       xml_page.memory = NULL;
+       xml_page.size = 0;
+
+       return result;
+}
+
+
+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);
+}
+
+//------------------------------------------------------------------------
+
+std::string Mootcher::searchText(std::string query, int page, std::string filter, enum sortMethod sort)
+{
+       std::string params = "";
+       char buf[24];
+
+       if (page > 1) {
+               snprintf(buf, 23, "p=%d&", page);
+               params += buf;
+       }
+
+       char *eq = curl_easy_escape(curl, query.c_str(), query.length());
+       params += "q=\"" + std::string(eq) + "\"";
+       free(eq);
+
+       if (filter != "") {
+               char *ef = curl_easy_escape(curl, filter.c_str(), filter.length());
+               params += "&f=" + std::string(ef);
+               free(ef);
+       }
+
+       if (sort)
+               params += "&s=" + sortMethodString(sort);
+
+       params += "&fields=id,original_filename,duration,filesize,samplerate,license,serve";
+       params += "&sounds_per_page=100";
+
+       return doRequest("/sounds/search", params);
+}
+
+//------------------------------------------------------------------------
+
+std::string Mootcher::getSoundResourceFile(std::string ID)
+{
+
+       std::string originalSoundURI;
+       std::string audioFileName;
+       std::string xml;
+
+
+       // 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) {
+               error << _("getSoundResourceFile: There is no valid root in the xml file") << endmsg;
+               return "";
+       }
+
+       if (strcmp(doc.root()->name().c_str(), "response") != 0) {
+               error << string_compose (_("getSoundResourceFile: root = %1, != response"), doc.root()->name()) << endmsg;
+               return "";
+       }
+
+       XMLNode *name = freesound->child("original_filename");
+
+       // get the file name and size from xml file
+       if (name) {
+
+               audioFileName = Glib::build_filename (basePath, ID + "-" + name->child("text")->content());
+
+               //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());
+                                       }
+                               }
+                       }
+                       ARDOUR::Library->set_tags (std::string("//")+audioFileName, strings);
+                       ARDOUR::Library->save_changes ();
+               }
+       }
+
+       return audioFileName;
+}
+
+int audioFileWrite(void *buffer, size_t size, size_t nmemb, void *file)
+{
+       return (int)fwrite(buffer, size, nmemb, (FILE*) file);
+};
+
+//------------------------------------------------------------------------
+
+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;
+}
+
+void
+Mootcher::doneWithMootcher()
+{
+
+       // update the sound info pane if the selection in the list box is still us
+       sfb->refresh_display(ID, audioFileName);
+
+       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
+}
+
+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();
+
+       thisMootcher->Finished(); /* EMIT SIGNAL */
+       return res;
+}
+
+
+//------------------------------------------------------------------------
+
+bool Mootcher::checkAudioFile(std::string originalFileName, std::string theID)
+{
+       ensureWorkingDir();
+       ID = theID;
+       audioFileName = Glib::build_filename (basePath, ID + "-" + originalFileName);
+
+       // check to see if audio file already exists
+       FILE *testFile = g_fopen(audioFileName.c_str(), "r");
+       if (testFile) {
+               fseek (testFile , 0 , SEEK_END);
+               if (ftell (testFile) > 256) {
+                       fclose (testFile);
+                       return true;
+               }
+
+               // else file was small, probably an error, delete it
+               fclose(testFile);
+               remove( audioFileName.c_str() );
+       }
+       return false;
+}
+
+
+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;
+       }
+       // now download the actual file
+       theFile = g_fopen( (audioFileName + ".part").c_str(), "wb" );
+
+       if (!theFile) {
+               return false;
+       }
+
+       // create the download url
+       audioURL += "?api_key=" + api_key;
+
+       setcUrlOptions();
+       curl_easy_setopt(curl, CURLOPT_URL, audioURL.c_str() );
+       curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, audioFileWrite);
+       curl_easy_setopt(curl, CURLOPT_WRITEDATA, theFile);
+
+       std::string prog;
+       prog = string_compose (_("%1"), originalFileName);
+       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;
+
+       curl_easy_setopt (curl, CURLOPT_NOPROGRESS, 0); // turn on the progress bar
+       curl_easy_setopt (curl, CURLOPT_PROGRESSFUNCTION, progress_callback);
+       curl_easy_setopt (curl, CURLOPT_PROGRESSDATA, this);
+
+       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());
+       pthread_t freesound_download_thread;
+       pthread_create_and_store("freesound_import", &freesound_download_thread, freesound_download_thread_func, this);
+
+       return true;
+}
+
+//---------
+
+void
+Mootcher::updateProgress(double dlnow, double dltotal)
+{
+       if (dltotal > 0) {
+               double fraction = dlnow / dltotal;
+               // 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;
+               }
+               progress_bar.set_fraction(fraction);
+       }
+}
+
+int
+Mootcher::progress_callback(void *caller, double dltotal, double dlnow, double /*ultotal*/, double /*ulnow*/)
+{
+       // 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.
+
+       Mootcher *thisMootcher = (Mootcher *) caller;
+
+       if (thisMootcher->cancel_download) {
+               return -1;
+       }
+
+       thisMootcher->Progress(dlnow, dltotal); /* EMIT SIGNAL */
+       return 0;
+}
+