Merge branch 'mingw-updates' of https://github.com/mojofunk/ardour into cairocanvas
[ardour.git] / libs / ardour / export_handler.cc
index 20abc80de1995866ff8befb89228d04bd1e83845..c9f20d182e7bc4dce63a82be58d39f4d8cdd2bcd 100644 (file)
 #include "ardour/export_status.h"
 #include "ardour/export_format_specification.h"
 #include "ardour/export_filename.h"
+#include "ardour/soundcloud_upload.h"
+#include "ardour/system_exec.h"
+#include "pbd/openuri.h"
+#include "pbd/basename.h"
 #include "ardour/session_metadata.h"
 
 #include "i18n.h"
@@ -277,6 +281,13 @@ ExportHandler::process_normalize ()
        return 0;
 }
 
+void
+ExportHandler::command_output(std::string output, size_t size)
+{
+       std::cerr << "command: " << size << ", " << output << std::endl;
+       info << output << endmsg;
+}
+
 void
 ExportHandler::finish_timespan ()
 {
@@ -297,13 +308,77 @@ ExportHandler::finish_timespan ()
                        AudiofileTagger::tag_file(filename, *SessionMetadata::Metadata());
                }
 
+               if (!fmt->command().empty()) {
+
+#if 0                  // would be nicer with C++11 initialiser...
+                       std::map<char, std::string> subs {
+                               { 'f', filename },
+                               { 'd', Glib::path_get_dirname(filename) },
+                               { 'b', PBD::basename_nosuffix(filename) },
+                               { 'u', upload_username },
+                               { 'p', upload_password}
+                       };
+#endif
+
+                       PBD::ScopedConnection command_connection;
+                       std::map<char, std::string> subs;
+                       subs.insert (std::pair<char, std::string> ('f', filename));
+                       subs.insert (std::pair<char, std::string> ('d', Glib::path_get_dirname(filename)));
+                       subs.insert (std::pair<char, std::string> ('b', PBD::basename_nosuffix(filename)));
+                       subs.insert (std::pair<char, std::string> ('u', upload_username));
+                       subs.insert (std::pair<char, std::string> ('p', upload_password));
+
+
+                       std::cerr << "running command: " << fmt->command() << "..." << std::endl;
+                       ARDOUR::SystemExec *se = new ARDOUR::SystemExec(fmt->command(), subs);
+                       se->ReadStdout.connect_same_thread(command_connection, boost::bind(&ExportHandler::command_output, this, _1, _2));
+                       if (se->start (2) == 0) {
+                               // successfully started
+                               std::cerr << "started!" << std::endl;
+                               while (se->is_running ()) {
+                                       // wait for system exec to terminate
+                                       // std::cerr << "waiting..." << std::endl;
+                                       usleep (1000);
+                               }
+                       }
+                       std::cerr << "done! deleting..." << std::endl;
+                       delete (se);
+               }
+
+               if (fmt->upload()) {
+                       SoundcloudUploader *soundcloud_uploader = new SoundcloudUploader;
+                       std::string token = soundcloud_uploader->Get_Auth_Token(upload_username, upload_password);
+                       std::cerr
+                               << "uploading "
+                               << filename << std::endl
+                               << "username = " << upload_username
+                               << ", password = " << upload_password
+                               << " - token = " << token << " ..."
+                               << std::endl;
+                       std::string path = soundcloud_uploader->Upload (
+                                       filename,
+                                       PBD::basename_nosuffix(filename), // title
+                                       token,
+                                       upload_public,
+                                       this);
+
+                       if (path.length() != 0) {
+                               if (upload_open) {
+                               std::cerr << "opening " << path << " ..." << std::endl;
+                               open_uri(path.c_str());  // open the soundcloud website to the new file
+                               }
+                       } else {
+                               error << _("upload to Soundcloud failed.  Perhaps your email or password are incorrect?\n") << endmsg;
+                       }
+                       delete soundcloud_uploader;
+               }
                config_map.erase (config_map.begin());
        }
 
        start_timespan ();
 }
 
-/*** CD Marker sutff ***/
+/*** CD Marker stuff ***/
 
 struct LocationSortByStart {
     bool operator() (Location *a, Location *b) {