remove cruft from ArdourUI; improve keyboard icon(s); shrink other plugin GUI buttons...
[ardour.git] / gtk2_ardour / sfdb_ui.cc
index 21d56497fb21b14fb36dc9c9eb8287598d4d70ab..8492ae1e051e290b7867af77c7d4e99cc84a1e5b 100644 (file)
 #include <glibmm/fileutils.h>
 
 #include <pbd/convert.h>
-#include <pbd/fs.h>
 #include <pbd/tokenizer.h>
+#include <pbd/enumwriter.h>
+#include <pbd/pthread_utils.h>
+#include <pbd/xml++.h>
 
 #include <gtkmm2ext/utils.h>
 
 #include "utils.h"
 #include "gain_meter.h"
 
+#ifdef FREESOUND
+#include "sfdb_freesound_mootcher.h"
+#endif
+
 #include "i18n.h"
 
 using namespace ARDOUR;
@@ -65,11 +71,46 @@ using Glib::ustring;
 
 ustring SoundFileBrowser::persistent_folder;
 
-SoundFileBox::SoundFileBox ()
+static ImportMode
+string2importmode (string str)
+{
+       if (str == _("as new tracks")) {
+               return ImportAsTrack;
+       } else if (str == _("to selected tracks")) {
+               return ImportToTrack;
+       } else if (str == _("to region list")) {
+               return ImportAsRegion;
+       } else if (str == _("as new tape tracks")) {
+               return ImportAsTapeTrack;
+       }
+
+       warning << string_compose (_("programming error: unknown import mode string %1"), str) << endmsg;
+       
+       return ImportAsTrack;
+}
+
+static string
+importmode2string (ImportMode mode)
+{
+       switch (mode) {
+       case ImportAsTrack:
+               return _("as new tracks");
+       case ImportToTrack:
+               return _("to selected tracks");
+       case ImportAsRegion:
+               return _("to region list");
+       case ImportAsTapeTrack:
+               return _("as new tape tracks");
+       }
+       /*NOTREACHED*/
+       return _("as new tracks");
+}
+
+SoundFileBox::SoundFileBox (bool persistent)
        : _session(0),
          table (6, 2),
-         length_clock ("sfboxLengthClock", false, "EditCursorClock", false, true, false),
-         timecode_clock ("sfboxTimecodeClock", false, "EditCursorClock", false, false, false),
+         length_clock ("sfboxLengthClock", !persistent, "EditCursorClock", false, true, false),
+         timecode_clock ("sfboxTimecodeClock", !persistent, "EditCursorClock", false, false, false),
          main_box (false, 6),
          autoplay_btn (_("Auto-play"))
        
@@ -221,8 +262,10 @@ SoundFileBox::setup_labels (const ustring& filename)
                samplerate.set_name ("NewSessionSR2Label");
        }
 
-       length_clock.set (sf_info.length, true);
-       timecode_clock.set (sf_info.timecode, true);
+       double src_coef = (double) _session->nominal_frame_rate() / sf_info.samplerate;
+
+       length_clock.set (sf_info.length * src_coef + 0.5, true);
+       timecode_clock.set (sf_info.timecode * src_coef + 0.5, true);
 
        // this is a hack that is fixed in trunk, i think (august 26th, 2007)
 
@@ -348,66 +391,135 @@ SoundFileBox::save_tags (const vector<string>& tags)
        Library->save_changes ();
 }
 
-SoundFileBrowser::SoundFileBrowser (Gtk::Window& parent, string title, ARDOUR::Session* s)
+SoundFileBrowser::SoundFileBrowser (Gtk::Window& parent, string title, ARDOUR::Session* s, bool persistent)
        : ArdourDialog (parent, title, false, false),
          found_list (ListStore::create(found_list_columns)),
+         freesound_list (ListStore::create(freesound_list_columns)),
          chooser (FILE_CHOOSER_ACTION_OPEN),
+         preview (persistent),
+         found_search_btn (_("Search")),
          found_list_view (found_list),
-         found_search_btn (_("Search"))
+         freesound_search_btn (_("Start Downloading")),
+         freesound_list_view (freesound_list)
 {
-       VBox* vbox;
-       HBox* hbox;
-
+       resetting_ourselves = false;
        gm = 0;
 
-       set_session (s);
-       resetting_ourselves = false;
+       if (ARDOUR::Profile->get_sae()) {
+               chooser.add_shortcut_folder_uri("file:///Library/GarageBand/Apple Loops");
+               chooser.add_shortcut_folder_uri("file:///Library/Application Support/GarageBand/Instrument Library/Sampler/Sampler Files");
+       }
+       
+       //add the file chooser
+       {
+               chooser.set_border_width (12);
+               custom_filter.add_custom (FILE_FILTER_FILENAME, mem_fun(*this, &SoundFileBrowser::on_custom));
+               custom_filter.set_name (_("Audio files"));
+
+               matchall_filter.add_pattern ("*.*");
+               matchall_filter.set_name (_("All files"));
+
+               chooser.add_filter (custom_filter);
+               chooser.add_filter (matchall_filter);
+               chooser.set_select_multiple (true);
+               chooser.signal_update_preview().connect(mem_fun(*this, &SoundFileBrowser::update_preview));
+               chooser.signal_file_activated().connect (mem_fun (*this, &SoundFileBrowser::chooser_file_activated));
+
+               if (!persistent_folder.empty()) {
+                       chooser.set_current_folder (persistent_folder);
+               }
+               notebook.append_page (chooser, _("Browse Files"));
+       }
        
        hpacker.set_spacing (6);
        hpacker.pack_start (notebook, true, true);
        hpacker.pack_start (preview, false, false);
-
-       get_vbox()->pack_start (hpacker, true, true);
-
-       hbox = manage(new HBox);
-       hbox->pack_start (found_entry);
-       hbox->pack_start (found_search_btn);
-       
-       vbox = manage(new VBox);
-       vbox->pack_start (*hbox, PACK_SHRINK);
-       vbox->pack_start (found_list_view);
-       found_list_view.append_column(_("Paths"), found_list_columns.pathname);
        
-       chooser.set_border_width (12);
+       get_vbox()->pack_start (hpacker, true, true);
 
-       notebook.append_page (chooser, _("Browse Files"));
-       notebook.append_page (*vbox, _("Search Tags"));
+       //add tag search
+       {
+               VBox* vbox;
+               HBox* hbox;
 
-       notebook.set_size_request (500, -1);
 
-       found_list_view.get_selection()->set_mode (SELECTION_MULTIPLE);
-       found_list_view.signal_row_activated().connect (mem_fun (*this, &SoundFileBrowser::found_list_view_activated));
+               hbox = manage(new HBox);
+               hbox->pack_start (found_entry);
+               hbox->pack_start (found_search_btn);
+               
+               Gtk::ScrolledWindow *scroll = manage(new ScrolledWindow);
+               scroll->add(found_list_view);
+               scroll->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
 
-       custom_filter.add_custom (FILE_FILTER_FILENAME, mem_fun(*this, &SoundFileBrowser::on_custom));
-       custom_filter.set_name (_("Audio files"));
+               vbox = manage(new VBox);
+               vbox->pack_start (*hbox, PACK_SHRINK);
+               vbox->pack_start (*scroll);
+               
+               found_list_view.append_column(_("Paths"), found_list_columns.pathname);
 
-       matchall_filter.add_pattern ("*.*");
-       matchall_filter.set_name (_("All files"));
+               found_list_view.get_selection()->signal_changed().connect(mem_fun(*this, &SoundFileBrowser::found_list_view_selected));
+               
+               found_list_view.signal_row_activated().connect (mem_fun (*this, &SoundFileBrowser::found_list_view_activated));
 
-       chooser.add_filter (custom_filter);
-       chooser.add_filter (matchall_filter);
-       chooser.set_select_multiple (true);
-       chooser.signal_update_preview().connect(mem_fun(*this, &SoundFileBrowser::update_preview));
-       chooser.signal_file_activated().connect (mem_fun (*this, &SoundFileBrowser::chooser_file_activated));
+               found_search_btn.signal_clicked().connect(mem_fun(*this, &SoundFileBrowser::found_search_clicked));
+               found_entry.signal_activate().connect(mem_fun(*this, &SoundFileBrowser::found_search_clicked));
 
-       if (!persistent_folder.empty()) {
-               chooser.set_current_folder (persistent_folder);
+               notebook.append_page (*vbox, _("Search Tags"));
        }
+       
+       //add freesound search
+#ifdef FREESOUND
+       {
+               VBox* vbox;
+               HBox* passbox;
+               Label* label;
+
+               passbox = manage(new HBox);
+               passbox->set_border_width (12);
+               passbox->set_spacing (6);
+
+               label = manage (new Label);
+               label->set_text (_("User:"));
+               passbox->pack_start (*label, false, false);
+               passbox->pack_start (freesound_name_entry);
+               label = manage (new Label);
+               label->set_text (_("Password:"));
+               passbox->pack_start (*label, false, false);
+               passbox->pack_start (freesound_pass_entry);
+               label = manage (new Label);
+               label->set_text (_("Tags:"));
+               passbox->pack_start (*label, false, false);
+               passbox->pack_start (freesound_entry, false, false);
+               passbox->pack_start (freesound_search_btn, false, false);
+               
+               Gtk::ScrolledWindow *scroll = manage(new ScrolledWindow);
+               scroll->add(freesound_list_view);
+               scroll->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
+
+               vbox = manage(new VBox);
+               vbox->pack_start (*passbox, PACK_SHRINK);
+               vbox->pack_start(*scroll);
+               
+               //vbox->pack_start (freesound_list_view);
+
+               freesound_list_view.append_column(_("Paths"), freesound_list_columns.pathname);
+
+               freesound_list_view.get_selection()->signal_changed().connect(mem_fun(*this, &SoundFileBrowser::freesound_list_view_selected));
+               
+               //freesound_list_view.get_selection()->set_mode (SELECTION_MULTIPLE);
+               freesound_list_view.signal_row_activated().connect (mem_fun (*this, &SoundFileBrowser::freesound_list_view_activated));
+
+               freesound_search_btn.signal_clicked().connect(mem_fun(*this, &SoundFileBrowser::freesound_search_clicked));
+               freesound_entry.signal_activate().connect(mem_fun(*this, &SoundFileBrowser::freesound_search_clicked));
 
-       found_list_view.get_selection()->signal_changed().connect(mem_fun(*this, &SoundFileBrowser::found_list_view_selected));
+               notebook.append_page (*vbox, _("Search Freesound"));
+       }
+#endif
        
-       found_search_btn.signal_clicked().connect(mem_fun(*this, &SoundFileBrowser::found_search_clicked));
-       found_entry.signal_activate().connect(mem_fun(*this, &SoundFileBrowser::found_search_clicked));
+
+       notebook.set_size_request (500, -1);
+
+       set_session (s);
 
        add_button (Stock::CANCEL, RESPONSE_CANCEL);
        add_button (Stock::APPLY, RESPONSE_APPLY);
@@ -447,6 +559,12 @@ SoundFileBrowser::found_list_view_activated (const TreeModel::Path& path, TreeVi
        preview.audition ();
 }
 
+void
+SoundFileBrowser::freesound_list_view_activated (const TreeModel::Path& path, TreeViewColumn* col)
+{
+       preview.audition ();
+}
+
 void
 SoundFileBrowser::set_session (Session* s)
 {
@@ -466,7 +584,8 @@ SoundFileBrowser::add_gain_meter ()
                delete gm;
        }
 
-       gm = new GainMeter (session->the_auditioner(), *session);
+       gm = new GainMeter (*session);
+       gm->set_io (session->the_auditioner());
 
        meter_packer.set_border_width (12);
        meter_packer.pack_start (*gm, false, true);
@@ -515,10 +634,10 @@ SoundFileBrowser::on_custom (const FileFilter::Info& filter_info)
 void
 SoundFileBrowser::update_preview ()
 {
-       preview.setup_labels (chooser.get_filename());
-
-       if (preview.autoplay()) {
-               Glib::signal_idle().connect (mem_fun (preview, &SoundFileBox::audition_oneshot));
+       if (preview.setup_labels (chooser.get_filename())) {
+               if (preview.autoplay()) {
+                       Glib::signal_idle().connect (mem_fun (preview, &SoundFileBox::audition_oneshot));
+               }
        }
 }
 
@@ -545,6 +664,29 @@ SoundFileBrowser::found_list_view_selected ()
        }
 }
 
+void
+SoundFileBrowser::freesound_list_view_selected ()
+{
+       if (!reset_options ()) {
+               set_response_sensitive (RESPONSE_OK, false);
+       } else {
+               ustring file;
+
+               TreeView::Selection::ListHandle_Path rows = freesound_list_view.get_selection()->get_selected_rows ();
+               
+               if (!rows.empty()) {
+                       TreeIter iter = freesound_list->get_iter(*rows.begin());
+                       file = (*iter)[freesound_list_columns.pathname];
+                       chooser.set_filename (file);
+                       set_response_sensitive (RESPONSE_OK, true);
+               } else {
+                       set_response_sensitive (RESPONSE_OK, false);
+               }
+               
+               preview.setup_labels (file);
+       }
+}
+
 void
 SoundFileBrowser::found_search_clicked ()
 {
@@ -569,6 +711,88 @@ SoundFileBrowser::found_search_clicked ()
        }
 }
 
+void*
+freesound_search_thread_entry (void* arg)
+{
+       PBD::ThreadCreated (pthread_self(), X_("Freesound Search"));
+
+       static_cast<SoundFileBrowser*>(arg)->freesound_search_thread ();
+       
+       return 0;
+}
+
+bool searching = false;
+bool canceling = false;
+
+void
+SoundFileBrowser::freesound_search_clicked ()
+{
+       if (canceling)  //already canceling, button does nothing
+               return;
+       
+       if ( searching ) {
+               freesound_search_btn.set_label(_("Cancelling.."));
+               canceling = true;
+       } else {
+               searching = true;
+               freesound_search_btn.set_label(_("Cancel"));
+               pthread_t freesound_thr;
+               pthread_create_and_store ("freesound_search", &freesound_thr, 0, freesound_search_thread_entry, this);
+       }
+}
+
+void
+SoundFileBrowser::freesound_search_thread()
+{
+#ifdef FREESOUND
+       freesound_list->clear();
+
+       string path;
+       path = Glib::get_home_dir();
+       path += "/Freesound/";
+       Mootcher theMootcher(path.c_str());
+       
+       string name_string = freesound_name_entry.get_text ();
+       string pass_string = freesound_pass_entry.get_text ();
+       string search_string = freesound_entry.get_text ();
+
+       if ( theMootcher.doLogin( name_string, pass_string ) ) {
+
+               string theString = theMootcher.searchText(search_string);
+
+               XMLTree doc;
+               doc.read_buffer( theString );
+               XMLNode *root = doc.root();
+
+               if (root==NULL) return;
+
+               if ( strcmp(root->name().c_str(), "freesound") == 0) {
+
+                       XMLNode *node = 0;
+                       XMLNodeList children = root->children();
+                       XMLNodeConstIterator niter;
+                       for (niter = children.begin(); niter != children.end() && !canceling; ++niter) {
+                               node = *niter;
+                               if( strcmp( node->name().c_str(), "sample") == 0 ){
+                                       XMLProperty *prop=node->property ("id");
+                                       string filename = theMootcher.getFile( prop->value().c_str() );
+                                       if ( filename != "" ) {
+                                               TreeModel::iterator new_row = freesound_list->append();
+                                               TreeModel::Row row = *new_row;
+                                               string path = Glib::filename_from_uri (string ("file:") + filename);
+                                               row[freesound_list_columns.pathname] = path;                                            
+                                       }
+                               }
+                       }
+               }
+       }
+
+       searching = false;
+       canceling = false;
+       freesound_search_btn.set_label(_("Start Downloading"));
+#endif
+}
+
 vector<ustring>
 SoundFileBrowser::get_paths ()
 {
@@ -587,7 +811,7 @@ SoundFileBrowser::get_paths ()
                        }
                }
                
-       } else {
+       } else if (n==1){
                
                typedef TreeView::Selection::ListHandle_Path ListPath;
                
@@ -596,6 +820,17 @@ SoundFileBrowser::get_paths ()
                        TreeIter iter = found_list->get_iter(*i);
                        ustring str = (*iter)[found_list_columns.pathname];
                        
+                       results.push_back (str);
+               }
+       } else {
+               
+               typedef TreeView::Selection::ListHandle_Path ListPath;
+               
+               ListPath rows = freesound_list_view.get_selection()->get_selected_rows ();
+               for (ListPath::iterator i = rows.begin() ; i != rows.end(); ++i) {
+                       TreeIter iter = freesound_list->get_iter(*i);
+                       ustring str = (*iter)[freesound_list_columns.pathname];
+                       
                        results.push_back (str);
                }
        }
@@ -630,8 +865,17 @@ SoundFileOmega::reset_options ()
                channel_combo.set_sensitive (true);
                action_combo.set_sensitive (true);
                where_combo.set_sensitive (true);
-               copy_files_btn.set_sensitive (true);
 
+               /* if we get through this function successfully, this may be
+                  reset at the end, once we know if we can use hard links
+                  to do embedding
+               */
+
+               if (Config->get_only_copy_imported_files()) {
+                       copy_files_btn.set_sensitive (false);
+               } else {
+                       copy_files_btn.set_sensitive (false);
+               }
        }
 
        bool same_size;
@@ -655,7 +899,7 @@ SoundFileOmega::reset_options ()
                        switch (id) {
                        case Editing::ImportDistinctFiles:
                                if (selected_track_cnt == paths.size()) {
-                                       action_strings.push_back (_("to selected tracks"));
+                                       action_strings.push_back (importmode2string (ImportToTrack));
                                }
                                break;
                                
@@ -667,15 +911,15 @@ SoundFileOmega::reset_options ()
                                break;
                                
                        default:
-                               action_strings.push_back (_("to selected tracks"));
+                               action_strings.push_back (importmode2string (ImportToTrack));
                                break;
                        }
                } 
        }
 
-       action_strings.push_back (_("as new tracks"));
-       action_strings.push_back (_("to the region list"));
-       action_strings.push_back (_("as new tape tracks"));
+       action_strings.push_back (importmode2string (ImportAsTrack));
+       action_strings.push_back (importmode2string (ImportAsRegion));
+       action_strings.push_back (importmode2string (ImportAsTapeTrack));
 
        resetting_ourselves = true;
 
@@ -808,10 +1052,10 @@ SoundFileOmega::bad_file_message()
 bool
 SoundFileOmega::check_info (const vector<ustring>& paths, bool& same_size, bool& src_needed, bool& multichannel)
 {
-       SNDFILE* sf;
-       SF_INFO info;
+       SoundFileInfo info;
        nframes64_t sz = 0;
        bool err = false;
+       string errmsg;
 
        same_size = true;
        src_needed = false;
@@ -819,29 +1063,24 @@ SoundFileOmega::check_info (const vector<ustring>& paths, bool& same_size, bool&
 
        for (vector<ustring>::const_iterator i = paths.begin(); i != paths.end(); ++i) {
 
-               info.format = 0; // libsndfile says to clear this before sf_open().
+               if (!AudioFileSource::get_soundfile_info (*i, info, errmsg)) {
+                       err = true;
+               }
                
-               if ((sf = sf_open ((char*) (*i).c_str(), SFM_READ, &info)) != 0) { 
-                       sf_close (sf);
-
-                       if (info.channels > 1) {
-                               multichannel = true;
-                       }
-
-                       if (sz == 0) {
-                               sz = info.frames;
-                       } else {
-                               if (sz != info.frames) {
-                                       same_size = false;
-                               }
-                       }
-
-                       if ((nframes_t) info.samplerate != session->frame_rate()) {
-                               src_needed = true;
+               if (info.channels > 1) {
+                       multichannel = true;
+               }
+               
+               if (sz == 0) {
+                       sz = info.length;
+               } else {
+                       if (sz != info.length) {
+                               same_size = false;
                        }
+               }
 
-               } else {
-                       err = true;
+               if ((nframes_t) info.samplerate != session->frame_rate()) {
+                       src_needed = true;
                }
        }
 
@@ -886,11 +1125,11 @@ SoundFileOmega::check_link_status (const Session& s, const vector<ustring>& path
 }
 
 SoundFileChooser::SoundFileChooser (Gtk::Window& parent, string title, ARDOUR::Session* s)
-       : SoundFileBrowser (parent, title, s)
+       : SoundFileBrowser (parent, title, s, false)
 {
-       set_size_request (780, 300);
        chooser.set_select_multiple (false);
        found_list_view.get_selection()->set_mode (SELECTION_SINGLE);
+       freesound_list_view.get_selection()->set_mode (SELECTION_SINGLE);
 }
 
 void
@@ -922,8 +1161,9 @@ SoundFileChooser::get_filename ()
        return paths.front();
 }
 
-SoundFileOmega::SoundFileOmega (Gtk::Window& parent, string title, ARDOUR::Session* s, int selected_tracks)
-       : SoundFileBrowser (parent, title, s),
+SoundFileOmega::SoundFileOmega (Gtk::Window& parent, string title, ARDOUR::Session* s, int selected_tracks, bool persistent,
+                               Editing::ImportMode mode_hint)
+       : SoundFileBrowser (parent, title, s, persistent),
          copy_files_btn ( _("Copy files to session")),
          selected_track_cnt (selected_tracks)
 {
@@ -941,7 +1181,7 @@ SoundFileOmega::SoundFileOmega (Gtk::Window& parent, string title, ARDOUR::Sessi
 
        str.clear ();
        str.push_back (_("use file timestamp"));
-       str.push_back (_("at edit cursor"));
+       str.push_back (_("at edit point"));
        str.push_back (_("at playhead"));
        str.push_back (_("at session start"));
        set_popdown_strings (where_combo, str);
@@ -964,7 +1204,7 @@ SoundFileOmega::SoundFileOmega (Gtk::Window& parent, string title, ARDOUR::Sessi
        */
 
        str.clear ();
-       str.push_back (_("as new tracks"));
+       str.push_back (importmode2string (mode_hint));
        set_popdown_strings (action_combo, str);
        action_combo.set_active_text (str.front());
        action_combo.set_sensitive (false);
@@ -1049,20 +1289,16 @@ SoundFileOmega::SoundFileOmega (Gtk::Window& parent, string title, ARDOUR::Sessi
        chooser.signal_selection_changed().connect (mem_fun (*this, &SoundFileOmega::file_selection_changed));
 }
 
+void
+SoundFileOmega::set_mode (ImportMode mode)
+{
+       action_combo.set_active_text (importmode2string (mode));
+}
+
 ImportMode
 SoundFileOmega::get_mode () const
 {
-       ustring str = action_combo.get_active_text();
-
-       if (str == _("as new tracks")) {
-               return ImportAsTrack;
-       } else if (str == _("to the region list")) {
-               return ImportAsRegion;
-       } else if (str == _("to selected tracks")) {
-               return ImportToTrack;
-       } else {
-               return ImportAsTapeTrack;
-       }
+       return string2importmode (action_combo.get_active_text());
 }
 
 void
@@ -1081,8 +1317,8 @@ SoundFileOmega::get_position() const
 
        if (str == _("use file timestamp")) {
                return ImportAtTimestamp;
-       } else if (str == _("at edit cursor")) {
-               return ImportAtEditCursor;
+       } else if (str == _("at edit point")) {
+               return ImportAtEditPoint;
        } else if (str == _("at playhead")) {
                return ImportAtPlayhead;
        } else {