part two of allow markup to be used in BoolOption items in an option editor
[ardour.git] / gtk2_ardour / sfdb_freesound_mootcher.cc
index 7e13c4f68b285818ec90b23ae13e95898d1654d6..ed1e2abfebbe7b0496bf106188b73486c1501c0a 100644 (file)
@@ -41,7 +41,7 @@
 #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
 \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()\r
        : curl(curl_easy_init())\r
@@ -91,10 +92,12 @@ void Mootcher::changeWorkingDir(const char *saveLocation)
 \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
@@ -178,12 +181,11 @@ std::string Mootcher::doRequest(std::string uri, std::string params)
        }\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
@@ -209,16 +211,21 @@ std::string Mootcher::searchText(std::string query, int page, std::string filter
                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,serve";        \r
+       params += "&fields=id,original_filename,duration,filesize,samplerate,license,serve";\r
 \r
        return doRequest("/sounds/search", params);\r
 }\r
@@ -233,8 +240,6 @@ std::string Mootcher::getSoundResourceFile(std::string ID)
        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
@@ -244,12 +249,12 @@ std::string Mootcher::getSoundResourceFile(std::string ID)
 \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
@@ -300,7 +305,6 @@ std::string Mootcher::getAudioFile(std::string originalFileName, std::string ID,
        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
@@ -335,13 +339,12 @@ std::string Mootcher::getAudioFile(std::string originalFileName, std::string ID,
        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->freesound_progress_bar.hide();\r
        caller->freesound_progress_bar.show();\r
 \r
        std::string prog;\r
-       prog = string_compose (_("%1: [Stop]->"), originalFileName);\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
@@ -356,11 +359,10 @@ std::string Mootcher::getAudioFile(std::string originalFileName, std::string ID,
        caller->freesound_progress_bar.set_text("");\r
        \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
                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
@@ -369,7 +371,7 @@ std::string Mootcher::getAudioFile(std::string originalFileName, std::string ID,
 }\r
 \r
 //---------\r
-int Mootcher::progress_callback(void *caller, 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
@@ -385,7 +387,6 @@ SoundFileBrowser *sfb = (SoundFileBrowser *) caller;
        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