Add option to limit automatable control parmaters
[ardour.git] / libs / ardour / soundcloud_upload.cc
index d725614ea752bc08219e45e70ec00298789701e2..291e4d2fe6951cefb9a9442dec9d173b64b3a16a 100644 (file)
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <iostream>
-#include <pbd/gstdio_compat.h>
+#include "pbd/gstdio_compat.h"
 
-#include "i18n.h"
+#include "pbd/i18n.h"
 
 using namespace PBD;
 
-size_t
+static size_t
 WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
 {
        register int realsize = (int)(size * nmemb);
@@ -52,6 +52,8 @@ WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
 }
 
 SoundcloudUploader::SoundcloudUploader()
+: errorBuffer()
+, caller(0)
 {
        curl_handle = curl_easy_init();
        multi_handle = curl_multi_init();
@@ -72,7 +74,7 @@ SoundcloudUploader::Get_Auth_Token( std::string username, std::string password )
        struct curl_httppost *formpost=NULL;
        struct curl_httppost *lastptr=NULL;
 
-       /* Fill in the filename field */ 
+       /* Fill in the filename field */
        curl_formadd(&formpost,
                        &lastptr,
                        CURLFORM_COPYNAME, "client_id",
@@ -108,7 +110,7 @@ SoundcloudUploader::Get_Auth_Token( std::string username, std::string password )
        headerlist = curl_slist_append(headerlist, "Accept: application/xml");
        curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, headerlist);
 
-       /* what URL that receives this POST */ 
+       /* what URL that receives this POST */
        std::string url = "https://api.soundcloud.com/oauth2/token";
        curl_easy_setopt(curl_handle, CURLOPT_URL, url.c_str());
        curl_easy_setopt(curl_handle, CURLOPT_HTTPPOST, formpost);
@@ -118,7 +120,7 @@ SoundcloudUploader::Get_Auth_Token( std::string username, std::string password )
        // perform online request
        CURLcode res = curl_easy_perform(curl_handle);
        if (res != 0) {
-               DEBUG_TRACE (DEBUG::Soundcloud, string_compose ("curl error %1 (%2)", res, curl_easy_strerror(res) ) );
+               DEBUG_TRACE (DEBUG::Soundcloud, string_compose ("curl error %1 (%2)\n", res, curl_easy_strerror(res) ) );
                return "";
        }
 
@@ -146,7 +148,7 @@ int
 SoundcloudUploader::progress_callback(void *caller, double dltotal, double dlnow, double ultotal, double ulnow)
 {
        SoundcloudUploader *scu = (SoundcloudUploader *) caller;
-       DEBUG_TRACE (DEBUG::Soundcloud, string_compose ("%1: uploaded %2 of %3", scu->title, ulnow, ultotal) );
+       DEBUG_TRACE (DEBUG::Soundcloud, string_compose ("%1: uploaded %2 of %3\n", scu->title, ulnow, ultotal) );
        scu->caller->SoundcloudProgress(ultotal, ulnow, scu->title); /* EMIT SIGNAL */
        return 0;
 }
@@ -170,14 +172,14 @@ SoundcloudUploader::Upload(std::string file_path, std::string title, std::string
        struct curl_httppost *lastptr=NULL;
 
        /* Fill in the file upload field. This makes libcurl load data from
-          the given file name when curl_easy_perform() is called. */ 
+          the given file name when curl_easy_perform() is called. */
        curl_formadd(&formpost,
                        &lastptr,
                        CURLFORM_COPYNAME, "track[asset_data]",
                        CURLFORM_FILE, file_path.c_str(),
                        CURLFORM_END);
 
-       /* Fill in the filename field */ 
+       /* Fill in the filename field */
        curl_formadd(&formpost,
                        &lastptr,
                        CURLFORM_COPYNAME, "oauth_token",
@@ -205,7 +207,7 @@ SoundcloudUploader::Upload(std::string file_path, std::string title, std::string
 
 
        /* initalize custom header list (stating that Expect: 100-continue is not
-          wanted */ 
+          wanted */
        struct curl_slist *headerlist=NULL;
        static const char buf[] = "Expect:";
        headerlist = curl_slist_append(headerlist, buf);
@@ -213,7 +215,7 @@ SoundcloudUploader::Upload(std::string file_path, std::string title, std::string
 
        if (curl_handle && multi_handle) {
 
-               /* what URL that receives this POST */ 
+               /* what URL that receives this POST */
                std::string url = "https://api.soundcloud.com/tracks";
                curl_easy_setopt(curl_handle, CURLOPT_URL, url.c_str());
                // curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L);
@@ -235,7 +237,7 @@ SoundcloudUploader::Upload(std::string file_path, std::string title, std::string
 
                while(still_running) {
                        struct timeval timeout;
-                       int rc; /* select() return code */ 
+                       int rc; /* select() return code */
 
                        fd_set fdread;
                        fd_set fdwrite;
@@ -248,7 +250,7 @@ SoundcloudUploader::Upload(std::string file_path, std::string title, std::string
                        FD_ZERO(&fdwrite);
                        FD_ZERO(&fdexcep);
 
-                       /* set a suitable timeout to play around with */ 
+                       /* set a suitable timeout to play around with */
                        timeout.tv_sec = 1;
                        timeout.tv_usec = 0;
 
@@ -261,33 +263,33 @@ SoundcloudUploader::Upload(std::string file_path, std::string title, std::string
                                        timeout.tv_usec = (curl_timeo % 1000) * 1000;
                        }
 
-                       /* get file descriptors from the transfers */ 
+                       /* get file descriptors from the transfers */
                        curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
 
                        /* In a real-world program you OF COURSE check the return code of the
                           function calls.  On success, the value of maxfd is guaranteed to be
                           greater or equal than -1.  We call select(maxfd + 1, ...), specially in
                           case of (maxfd == -1), we call select(0, ...), which is basically equal
-                          to sleep. */ 
+                          to sleep. */
 
                        rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
 
                        switch(rc) {
                                case -1:
-                                       /* select error */ 
+                                       /* select error */
                                        break;
                                case 0:
                                default:
-                                       /* timeout or readable/writable sockets */ 
+                                       /* timeout or readable/writable sockets */
                                        curl_multi_perform(multi_handle, &still_running);
                                        break;
                        }
-               } 
+               }
 
-               /* then cleanup the formpost chain */ 
+               /* then cleanup the formpost chain */
                curl_formfree(formpost);
 
-               /* free slist */ 
+               /* free slist */
                curl_slist_free_all (headerlist);
        }
 
@@ -302,19 +304,19 @@ SoundcloudUploader::Upload(std::string file_path, std::string title, std::string
                XMLNode *root = doc.root();
 
                if (!root) {
-                       DEBUG_TRACE (DEBUG::Soundcloud, "no root XML node!");
+                       DEBUG_TRACE (DEBUG::Soundcloud, "no root XML node!\n");
                        return "";
                }
 
                XMLNode *url_node = root->child("permalink-url");
                if (!url_node) {
-                       DEBUG_TRACE (DEBUG::Soundcloud, "no child node \"permalink-url\" found!");
+                       DEBUG_TRACE (DEBUG::Soundcloud, "no child node \"permalink-url\" found!\n");
                        return "";
                }
 
                XMLNode *text_node = url_node->child("text");
                if (!text_node) {
-                       DEBUG_TRACE (DEBUG::Soundcloud, "no text node found!");
+                       DEBUG_TRACE (DEBUG::Soundcloud, "no text node found!\n");
                        return "";
                }
 
@@ -336,15 +338,13 @@ SoundcloudUploader:: ~SoundcloudUploader()
 void
 SoundcloudUploader::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_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
        // setup curl error buffer
        curl_easy_setopt(curl_handle, CURLOPT_ERRORBUFFER, errorBuffer);
        // Allow redirection
        curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1);
-       
+
        // Allow connections to time out (without using signals)
        curl_easy_setopt(curl_handle, CURLOPT_NOSIGNAL, 1);
        curl_easy_setopt(curl_handle, CURLOPT_CONNECTTIMEOUT, 30);