First pass of sfdb tag searching. Not functional, but very very close.
[ardour.git] / gtk2_ardour / sfdb_ui.cc
index d737d409fddc42699dc06fcdfc7b234841b0cad5..d3c61859919e31cc8b5a71dd4fb399fc4b1d084b 100644 (file)
@@ -1,6 +1,5 @@
 /*
-    Copyright (C) 2005 Paul Davis 
-    Written by Taybin Rutkin
+    Copyright (C) 2005-2006 Paul Davis
 
     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
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-
 */
 
 #include <map>
 #include <cerrno>
-
-#include <sndfile.h>
-
-#include <pbd/basename.h>
+#include <sstream>
 
 #include <gtkmm/box.h>
 #include <gtkmm/stock.h>
 
+#include <pbd/convert.h>
+#include <pbd/tokenizer.h>
+#include <pbd/whitespace.h>
+
+#include <gtkmm2ext/utils.h>
+
 #include <ardour/audio_library.h>
 #include <ardour/audioregion.h>
-#include <ardour/sndfile_helpers.h>
-#include <ardour/sndfilesource.h>
+#include <ardour/audiofilesource.h>
+#include <ardour/region_factory.h>
+#include <ardour/source_factory.h>
 
+#include "ardour_ui.h"
+#include "editing.h"
 #include "gui_thread.h"
 #include "prompter.h"
 #include "sfdb_ui.h"
+#include "utils.h"
 
 #include "i18n.h"
 
 using namespace ARDOUR;
-
-std::string length2string (const int32_t frames, const int32_t sample_rate);
+using namespace PBD;
+using namespace std;
 
 SoundFileBox::SoundFileBox ()
        :
        _session(0),
        current_pid(0),
-       fields(Gtk::ListStore::create(label_columns)),
        main_box (false, 3),
-       top_box (true, 4),
        bottom_box (true, 4),
        play_btn(_("Play")),
        stop_btn(_("Stop")),
-       add_field_btn(_("Add Field...")),
-       remove_field_btn(_("Remove Field"))
+       apply_btn(_("Apply"))
 {
        set_name (X_("SoundFileBox"));
+       
+       set_size_request (250, 500);
+       
        border_frame.set_label (_("Soundfile Info"));
        border_frame.add (main_box);
 
@@ -70,38 +75,30 @@ SoundFileBox::SoundFileBox ()
        main_box.pack_start(format, false, false);
        main_box.pack_start(channels, false, false);
        main_box.pack_start(samplerate, false, false);
-       main_box.pack_start(field_view, true, true);
-       main_box.pack_start(top_box, false, false);
+       main_box.pack_start(timecode, false, false);
+       main_box.pack_start(tags_entry, true, true);
+       main_box.pack_start(apply_btn, false, false);
        main_box.pack_start(bottom_box, false, false);
 
-       field_view.set_model (fields);
-       field_view.set_size_request(200, 150);
-       field_view.append_column (_("Field"), label_columns.field);
-       field_view.append_column_editable (_("Value"), label_columns.data);
-
-       top_box.set_homogeneous(true);
-       top_box.pack_start(add_field_btn);
-       top_box.pack_start(remove_field_btn);
-
-       remove_field_btn.set_sensitive(false);
-
        bottom_box.set_homogeneous(true);
        bottom_box.pack_start(play_btn);
        bottom_box.pack_start(stop_btn);
 
+//     tags_entry.signal_focus_out_event().connect (mem_fun (*this, &SoundFileBox::tags_entry_left));
        play_btn.signal_clicked().connect (mem_fun (*this, &SoundFileBox::play_btn_clicked));
        stop_btn.signal_clicked().connect (mem_fun (*this, &SoundFileBox::stop_btn_clicked));
+       apply_btn.signal_clicked().connect (mem_fun (*this, &SoundFileBox::apply_btn_clicked));
 
-       add_field_btn.signal_clicked().connect
-                       (mem_fun (*this, &SoundFileBox::add_field_clicked));
-       remove_field_btn.signal_clicked().connect
-                       (mem_fun (*this, &SoundFileBox::remove_field_clicked));
-
-       field_view.get_selection()->signal_changed().connect (mem_fun (*this, &SoundFileBox::field_selected));
-       Library->fields_changed.connect (mem_fun (*this, &SoundFileBox::setup_fields));
+       length.set_alignment (0.0f, 0.0f);
+       format.set_alignment (0.0f, 0.0f);
+       channels.set_alignment (0.0f, 0.0f);
+       samplerate.set_alignment (0.0f, 0.0f);
+       timecode.set_alignment (0.0f, 0.0f);
 
-       show_all();
+       stop_btn.set_no_show_all (true);
        stop_btn.hide();
+       
+       show_all();
 }
 
 void
@@ -109,7 +106,7 @@ SoundFileBox::set_session(Session* s)
 {
        _session = s;
 
-    if (!_session) {
+       if (!_session) {
                play_btn.set_sensitive(false);
        } else {
                _session->AuditionActive.connect(mem_fun (*this, &SoundFileBox::audition_status_changed));
@@ -121,66 +118,55 @@ SoundFileBox::setup_labels (string filename)
 {
        path = filename;
 
-    SNDFILE *sf;
-
-       sf_info.format = 0; // libsndfile says to clear this before sf_open().
-
-    if ((sf = sf_open ((char *) filename.c_str(), SFM_READ, &sf_info)) < 0) {
-        return false;
-    }
-
-       sf_close (sf);
-
-    if (sf_info.frames == 0 && sf_info.channels == 0 &&
-               sf_info.samplerate == 0 && sf_info.format == 0 &&
-               sf_info.sections == 0) {
-               /* .. ok, it's not a sound file */
-           return false;
+       string error_msg;
+
+       if(!AudioFileSource::get_soundfile_info (filename, sf_info, error_msg)) {
+               length.set_text (_("Length: n/a"));
+               format.set_text (_("Format: n/a"));
+               channels.set_text (_("Channels: n/a"));
+               samplerate.set_text (_("Samplerate: n/a"));
+               timecode.set_text (_("Timecode: n/a"));
+               tags_entry.set_text ("");
+               
+               tags_entry.set_sensitive (false);
+               play_btn.set_sensitive (false);
+               apply_btn.set_sensitive (false);
+               
+               return false;
        }
 
-       length.set_alignment (0.0f, 0.0f);
-       length.set_text (string_compose("Length: %1", length2string(sf_info.frames, sf_info.samplerate)));
-
-       format.set_alignment (0.0f, 0.0f);
-       format.set_text (string_compose("Format: %1, %2", 
-                               sndfile_major_format(sf_info.format),
-                               sndfile_minor_format(sf_info.format)));
-
-       channels.set_alignment (0.0f, 0.0f);
-       channels.set_text (string_compose("Channels: %1", sf_info.channels));
-
-       samplerate.set_alignment (0.0f, 0.0f);
-       samplerate.set_text (string_compose("Samplerate: %1", sf_info.samplerate));
-
-       setup_fields ();
-
+       length.set_text (string_compose(_("Length: %1"), length2string(sf_info.length, sf_info.samplerate)));
+       format.set_text (sf_info.format_name);
+       channels.set_text (string_compose(_("Channels: %1"), sf_info.channels));
+       samplerate.set_text (string_compose(_("Samplerate: %1"), sf_info.samplerate));
+       timecode.set_text (string_compose (_("Timecode: %1"), length2string(sf_info.timecode, sf_info.samplerate)));
+
+       vector<string> tags = Library->get_tags (filename);
+       
+       stringstream tag_string;
+       for (vector<string>::iterator i = tags.begin(); i != tags.end(); ++i) {
+               if (i != tags.begin()) {
+                       tag_string << ", ";
+               }
+               tag_string << *i;
+       }
+       tags_entry.set_text (tag_string.str());
+       
+       tags_entry.set_sensitive (true);
+       if (_session) {
+               play_btn.set_sensitive (true);
+       }
+       apply_btn.set_sensitive (true);
+       
        return true;
 }
 
-void
-SoundFileBox::setup_fields ()
-{
-       ENSURE_GUI_THREAD(mem_fun (*this, &SoundFileBox::setup_fields));
-
-       fields->clear ();
-
-       vector<string> field_list;
-       Library->get_fields(field_list);
-
-       vector<string>::iterator i;
-       Gtk::TreeModel::iterator iter;
-       Gtk::TreeModel::Row row;
-       for (i = field_list.begin(); i != field_list.end(); ++i) {
-               if (!(*i == _("channels") || *i == _("samplerate") ||
-                       *i == _("resolution") || *i == _("format"))) {
-                       iter = fields->append();
-                       row = *iter;
-
-                       string value = Library->get_field(path, *i);
-                       row[label_columns.field] = *i;
-                       row[label_columns.data]  = value;
-               }
-       }
+bool
+SoundFileBox::tags_entry_left (GdkEventFocus* event)
+{      
+       apply_btn_clicked ();
+       
+       return true;
 }
 
 void
@@ -197,16 +183,18 @@ SoundFileBox::play_btn_clicked ()
                return;
        }
 
-       static std::map<string, AudioRegion*> region_cache;
-
-       if (region_cache.find (path) == region_cache.end()) {
-               AudioRegion::SourceList srclist;
-               SndFileSource* sfs;
+       typedef std::map<string, boost::shared_ptr<AudioRegion> > RegionCache; 
+       static  RegionCache region_cache;
+       RegionCache::iterator the_region;
 
+       if ((the_region = region_cache.find (path)) == region_cache.end()) {
+               SourceList srclist;
+               boost::shared_ptr<AudioFileSource> afs;
+               
                for (int n = 0; n < sf_info.channels; ++n) {
                        try {
-                               sfs = new SndFileSource(path+":"+string_compose("%1", n), false);
-                               srclist.push_back(sfs);
+                               afs = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createReadable (*_session, path+":"+string_compose("%1", n), AudioFileSource::Flag (0)));
+                               srclist.push_back(afs);
 
                        } catch (failed_constructor& err) {
                                error << _("Could not access soundfile: ") << path << endmsg;
@@ -218,16 +206,26 @@ SoundFileBox::play_btn_clicked ()
                        return;
                }
 
-               string result;
-               _session->region_name (result, PBD::basename(srclist[0]->name()), false);
-               AudioRegion* a_region = new AudioRegion(srclist, 0, srclist[0]->length(), result, 0, Region::DefaultFlags, false);
-               region_cache[path] = a_region;
+               string rname;
+
+               _session->region_name (rname, Glib::path_get_basename(srclist[0]->name()), false);
+
+               pair<string,boost::shared_ptr<AudioRegion> > newpair;
+               pair<RegionCache::iterator,bool> res;
+
+               newpair.first = path;
+               newpair.second = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (srclist, 0, srclist[0]->length(), rname, 0, Region::DefaultFlags, false));
+
+               res = region_cache.insert (newpair);
+               the_region = res.first;
        }
 
        play_btn.hide();
        stop_btn.show();
 
-       _session->audition_region(*region_cache[path]);
+       boost::shared_ptr<Region> r = boost::static_pointer_cast<Region> (the_region->second);
+
+       _session->audition_region(r);
 }
 
 void
@@ -241,54 +239,76 @@ SoundFileBox::stop_btn_clicked ()
 }
 
 void
-SoundFileBox::add_field_clicked ()
+SoundFileBox::apply_btn_clicked ()
 {
-    ArdourPrompter prompter (true);
-    string name;
-
-    prompter.set_prompt (_("Name for field"));
+       string tag_string = tags_entry.get_text ();
 
-    switch (prompter.run ()) {
-               case Gtk::RESPONSE_ACCEPT:
-               prompter.get_result (name);
-                       if (name.length()) {
-                               Library->add_field (name);
-                               Library->save_changes ();
-                       }
-               break;
+       vector<string> tags;
 
-           default:
-               break;
-    }
+    if (!PBD::tokenize (tag_string, string(","), std::back_inserter (tags), true)) {
+               warning << _("SoundFileBox: Could not tokenize string: ") << tag_string << endmsg;
+               return;
+       }
+       
+       Library->set_tags (path, tags);
+       Library->save_changes ();
 }
 
-void
-SoundFileBox::remove_field_clicked ()
-{}
-
 void
 SoundFileBox::audition_status_changed (bool active)
 {
-    ENSURE_GUI_THREAD(bind (mem_fun (*this, &SoundFileBox::audition_status_changed), active));
-
+       ENSURE_GUI_THREAD(bind (mem_fun (*this, &SoundFileBox::audition_status_changed), active));
+       
        if (!active) {
                stop_btn_clicked ();
        }
 }
 
-void
-SoundFileBox::field_selected ()
-{}
-
-SoundFileBrowser::SoundFileBrowser (std::string title)
-       :
-       ArdourDialog(title),
-       chooser(Gtk::FILE_CHOOSER_ACTION_OPEN)
+// this needs to be kept in sync with the ImportMode enum defined in editing.h and editing_syms.h.
+static const char *import_mode_strings[] = {
+       N_("Add to Region list"),
+       N_("Add to selected Track(s)"),
+       N_("Add as new Track(s)"),
+       N_("Add as new Tape Track(s)"),
+       0
+};
+
+SoundFileBrowser::SoundFileBrowser (string title, ARDOUR::Session* s)
+       : ArdourDialog (title, false),
+         chooser (Gtk::FILE_CHOOSER_ACTION_OPEN),
+         found_list (Gtk::ListStore::create(found_list_columns)),
+         found_list_view (found_list),
+         found_search_btn (_("Search"))
 {
-       get_vbox()->pack_start(chooser);
-       chooser.set_preview_widget(preview);
-
+       set_default_size (700, 500);
+       Gtk::HBox* hbox = manage(new Gtk::HBox);
+       hbox->pack_start(notebook);
+       hbox->pack_start(preview, Gtk::PACK_SHRINK);
+       get_vbox()->pack_start(*hbox);
+
+       hbox = manage(new Gtk::HBox);
+       hbox->pack_start (found_entry);
+       hbox->pack_start (found_search_btn);
+       
+       Gtk::VBox* vbox = manage(new Gtk::VBox);
+       vbox->pack_start (*hbox, Gtk::PACK_SHRINK);
+       vbox->pack_start (found_list_view);
+       found_list_view.append_column(_("Paths"), found_list_columns.pathname);
+       
+       notebook.append_page (chooser, _("Files"));
+       notebook.append_page (*vbox, _("Tags"));
+
+       filter.add_custom (Gtk::FILE_FILTER_FILENAME, mem_fun(*this, &SoundFileBrowser::on_custom));
+       chooser.set_filter (filter);
+       chooser.set_select_multiple (true);
        chooser.signal_update_preview().connect(mem_fun(*this, &SoundFileBrowser::update_preview));
+       found_list_view.get_selection()->signal_changed().connect(mem_fun(*this, &SoundFileBrowser::found_list_view_selected));
+       
+       found_search_btn.signal_clicked().connect(mem_fun(*this, &SoundFileBrowser::found_search_clicked));
+
+       show_all ();
+       
+       set_session (s);
 }
 
 void
@@ -297,69 +317,163 @@ SoundFileBrowser::set_session (Session* s)
        preview.set_session(s);
 }
 
+bool
+SoundFileBrowser::on_custom (const Gtk::FileFilter::Info& filter_info)
+{
+       return AudioFileSource::safe_file_extension(filter_info.filename);
+}
+
 void
 SoundFileBrowser::update_preview ()
 {
-       chooser.set_preview_widget_active(preview.setup_labels(chooser.get_filename()));
+       preview.setup_labels(chooser.get_filename());
+}
+
+void
+SoundFileBrowser::found_list_view_selected ()
+{
+       
 }
 
-SoundFileChooser::SoundFileChooser (std::string title)
+void
+SoundFileBrowser::found_search_clicked ()
+{
+       string tag_string = found_entry.get_text ();
+
+       vector<string> tags;
+
+    if (!PBD::tokenize (tag_string, string(","), std::back_inserter (tags), true)) {
+               warning << _("SoundFileBox: Could not tokenize string: ") << tag_string << endmsg;
+               return;
+       }
+
+       vector<string> results;
+       Library->search_members_and (results, tags);
+       
+       found_list->clear();
+       for (vector<string>::iterator i = results.begin(); i != results.end(); ++i) {
+               Gtk::TreeModel::iterator new_row = found_list->append();
+               Gtk::TreeModel::Row row = *new_row;
+               cout << *i << endl;
+               row[found_list_columns.pathname] = *i;
+       }
+}
+
+SoundFileChooser::SoundFileChooser (string title, ARDOUR::Session* s)
        :
-       SoundFileBrowser(title)
+       SoundFileBrowser(title, s)
 {
        add_button (Gtk::Stock::OPEN, Gtk::RESPONSE_OK);
        add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
-
+       
        show_all ();
 }
 
-SoundFileOmega::SoundFileOmega (std::string title)
-       :
-       SoundFileBrowser(title),
-       embed_btn (_("Embed")),
-       import_btn (_("Import")),
-       split_check (_("Split Channels"))
+vector<string> SoundFileOmega::mode_strings;
+
+SoundFileOmega::SoundFileOmega (string title, ARDOUR::Session* s)
+       : SoundFileBrowser (title, s),
+         split_check (_("Split Channels"))
 {
-       get_action_area()->pack_start(embed_btn);
-       get_action_area()->pack_start(import_btn);
-       add_button (Gtk::Stock::CLOSE, Gtk::RESPONSE_CLOSE);
+//     add_button (Gtk::Stock::CLOSE, Gtk::RESPONSE_CLOSE);
 
-       chooser.set_extra_widget(split_check);
+       ARDOUR_UI::instance()->tooltips().set_tip(split_check, 
+                       _("Create a region for each channel"));
 
-       embed_btn.signal_clicked().connect (mem_fun (*this, &SoundFileOmega::embed_clicked));
-       import_btn.signal_clicked().connect (mem_fun (*this, &SoundFileOmega::import_clicked));
+       Gtk::Button* btn = add_button (_("Embed"), ResponseEmbed);
+       ARDOUR_UI::instance()->tooltips().set_tip(*btn, 
+                       _("Link to an external file"));
 
+       btn = add_button (_("Import"), ResponseImport);
+       ARDOUR_UI::instance()->tooltips().set_tip(*btn, 
+                       _("Copy a file to the session folder"));
+       
+       if (mode_strings.empty()) {
+               mode_strings = I18N (import_mode_strings);
+       }
+       Gtkmm2ext::set_popdown_strings (mode_combo, mode_strings);
+
+       set_mode (Editing::ImportAsRegion);
+
+       get_action_area()->pack_start (split_check);
+       get_action_area()->pack_start (mode_combo);
+
+       mode_combo.signal_changed().connect (mem_fun (*this, &SoundFileOmega::mode_changed));
+       
        show_all ();
 }
 
-void
-SoundFileOmega::embed_clicked ()
+bool
+SoundFileOmega::get_split ()
 {
-       Embedded (chooser.get_filenames(), split_check.get_active());
+       return split_check.get_active();
+}
+
+vector<Glib::ustring>
+SoundFileOmega::get_paths ()
+{
+       return chooser.get_filenames();
 }
 
 void
-SoundFileOmega::import_clicked ()
+SoundFileOmega::set_mode (Editing::ImportMode mode)
 {
-       Imported (chooser.get_filenames(), split_check.get_active());
+       mode_combo.set_active_text (mode_strings[(int)mode]);
+
+       switch (mode) {
+       case Editing::ImportAsRegion:
+               split_check.set_sensitive (true);
+               break;
+       case Editing::ImportAsTrack:
+               split_check.set_sensitive (true);
+               break;
+       case Editing::ImportToTrack:
+               split_check.set_sensitive (false);
+               break;
+       case Editing::ImportAsTapeTrack:
+               split_check.set_sensitive (true);
+               break;
+       }
 }
 
-std::string
-length2string (const int32_t frames, const int32_t sample_rate)
+Editing::ImportMode
+SoundFileOmega::get_mode ()
 {
-    int secs = (int) (frames / (float) sample_rate);
-    int hrs =  secs / 3600;
-    secs -= (hrs * 3600);
-    int mins = secs / 60;
-    secs -= (mins * 60);
+       vector<string>::iterator i;
+       uint32_t n;
+       string str = mode_combo.get_active_text ();
 
-    int total_secs = (hrs * 3600) + (mins * 60) + secs;
-    int frames_remaining = frames - (total_secs * sample_rate);
-    float fractional_secs = (float) frames_remaining / sample_rate;
+       for (n = 0, i = mode_strings.begin (); i != mode_strings.end(); ++i, ++n) {
+               if (str == (*i)) {
+                       break;
+               }
+       }
 
-    char duration_str[32];
-    sprintf (duration_str, "%02d:%02d:%05.2f", hrs, mins, (float) secs + fractional_secs);
+       if (i == mode_strings.end()) {
+               fatal << string_compose (_("programming error: %1"), X_("unknown import mode string")) << endmsg;
+               /*NOTREACHED*/
+       }
 
-    return duration_str;
+       return (Editing::ImportMode) (n);
 }
 
+void
+SoundFileOmega::mode_changed ()
+{
+       Editing::ImportMode mode = get_mode();
+
+       switch (mode) {
+       case Editing::ImportAsRegion:
+               split_check.set_sensitive (true);
+               break;
+       case Editing::ImportAsTrack:
+               split_check.set_sensitive (true);
+               break;
+       case Editing::ImportToTrack:
+               split_check.set_sensitive (false);
+               break;
+       case Editing::ImportAsTapeTrack:
+               split_check.set_sensitive (true);
+               break;
+       }
+}