+ if (preview.setup_labels (chooser.get_filename())) {
+ if (preview.autoplay()) {
+ Glib::signal_idle().connect (mem_fun (preview, &SoundFileBox::audition_oneshot));
+ }
+ }
+}
+
+void
+SoundFileBrowser::found_list_view_selected ()
+{
+ if (!reset_options ()) {
+ set_response_sensitive (RESPONSE_OK, false);
+ } else {
+ ustring file;
+
+ TreeView::Selection::ListHandle_Path rows = found_list_view.get_selection()->get_selected_rows ();
+
+ if (!rows.empty()) {
+ TreeIter iter = found_list->get_iter(*rows.begin());
+ file = (*iter)[found_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::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 ()
+{
+ string tag_string = found_entry.get_text ();
+
+ vector<string> tags;
+
+ if (!PBD::tokenize (tag_string, string(","), std::back_inserter (tags), true)) {
+ warning << _("SoundFileBrowser: 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) {
+ TreeModel::iterator new_row = found_list->append();
+ TreeModel::Row row = *new_row;
+ string path = Glib::filename_from_uri (string ("file:") + *i);
+ row[found_list_columns.pathname] = path;
+ }
+}
+
+void*
+freesound_search_thread_entry (void* arg)
+{
+ PBD::notify_gui_about_thread_creation (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 ()
+{
+ vector<ustring> results;
+
+ int n = notebook.get_current_page ();
+
+ if (n == 0) {
+ vector<ustring> filenames = chooser.get_filenames();
+ vector<ustring>::iterator i;
+
+ for (i = filenames.begin(); i != filenames.end(); ++i) {
+ struct stat buf;
+ if ((!stat((*i).c_str(), &buf)) && S_ISREG(buf.st_mode)) {
+ results.push_back (*i);
+ }
+ }
+
+ } else if (n==1){
+
+ typedef TreeView::Selection::ListHandle_Path ListPath;
+
+ ListPath rows = found_list_view.get_selection()->get_selected_rows ();
+ for (ListPath::iterator i = rows.begin() ; i != rows.end(); ++i) {
+ 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);
+ }
+ }
+
+ return results;
+}
+
+void
+SoundFileOmega::reset_options_noret ()
+{
+ if (!resetting_ourselves) {
+ (void) reset_options ();
+ }
+}
+
+bool
+SoundFileOmega::reset_options ()
+{
+ vector<ustring> paths = get_paths ();
+
+ if (paths.empty()) {
+
+ channel_combo.set_sensitive (false);
+ action_combo.set_sensitive (false);
+ where_combo.set_sensitive (false);
+ copy_files_btn.set_sensitive (false);
+
+ return false;
+
+ } else {
+
+ channel_combo.set_sensitive (true);
+ action_combo.set_sensitive (true);
+ where_combo.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;
+ bool src_needed;
+ bool selection_includes_multichannel;
+ bool selection_can_be_embedded_with_links = check_link_status (*session, paths);
+ ImportMode mode;
+
+ if (check_info (paths, same_size, src_needed, selection_includes_multichannel)) {
+ Glib::signal_idle().connect (mem_fun (*this, &SoundFileOmega::bad_file_message));
+ return false;
+ }
+
+ ustring existing_choice;
+ vector<string> action_strings;
+
+ if (selected_track_cnt > 0) {
+ if (channel_combo.get_active_text().length()) {
+ ImportDisposition id = get_channel_disposition();
+
+ switch (id) {
+ case Editing::ImportDistinctFiles:
+ if (selected_track_cnt == paths.size()) {
+ action_strings.push_back (importmode2string (ImportToTrack));
+ }
+ break;
+
+ case Editing::ImportDistinctChannels:
+ /* XXX it would be nice to allow channel-per-selected track
+ but its too hard we don't want to deal with all the
+ different per-file + per-track channel configurations.
+ */
+ break;
+
+ default:
+ action_strings.push_back (importmode2string (ImportToTrack));
+ break;
+ }
+ }
+ }
+
+ action_strings.push_back (importmode2string (ImportAsTrack));
+ action_strings.push_back (importmode2string (ImportAsRegion));
+ action_strings.push_back (importmode2string (ImportAsTapeTrack));
+
+ resetting_ourselves = true;
+
+ existing_choice = action_combo.get_active_text();
+
+ set_popdown_strings (action_combo, action_strings);
+
+ /* preserve any existing choice, if possible */
+
+
+ if (existing_choice.length()) {
+ vector<string>::iterator x;
+ for (x = action_strings.begin(); x != action_strings.end(); ++x) {
+ if (*x == existing_choice) {
+ action_combo.set_active_text (existing_choice);
+ break;
+ }
+ }
+ if (x == action_strings.end()) {
+ action_combo.set_active_text (action_strings.front());
+ }
+ } else {
+ action_combo.set_active_text (action_strings.front());
+ }
+
+ resetting_ourselves = false;
+
+ if ((mode = get_mode()) == ImportAsRegion) {
+ where_combo.set_sensitive (false);
+ } else {
+ where_combo.set_sensitive (true);
+ }
+
+ vector<string> channel_strings;
+
+ if (mode == ImportAsTrack || mode == ImportAsTapeTrack || mode == ImportToTrack) {
+ channel_strings.push_back (_("one track per file"));
+
+ if (selection_includes_multichannel) {
+ channel_strings.push_back (_("one track per channel"));
+ }
+
+ if (paths.size() > 1) {
+ /* tape tracks are a single region per track, so we cannot
+ sequence multiple files.
+ */
+ if (mode != ImportAsTapeTrack) {
+ channel_strings.push_back (_("sequence files"));
+ }
+ if (same_size) {
+ channel_strings.push_back (_("all files in one region"));
+ }
+
+ }
+
+ } else {
+ channel_strings.push_back (_("one region per file"));
+
+ if (selection_includes_multichannel) {
+ channel_strings.push_back (_("one region per channel"));
+ }
+
+ if (paths.size() > 1) {
+ if (same_size) {
+ channel_strings.push_back (_("all files in one region"));
+ }
+ }
+ }
+
+ existing_choice = channel_combo.get_active_text();
+
+ set_popdown_strings (channel_combo, channel_strings);
+
+ /* preserve any existing choice, if possible */
+
+ if (existing_choice.length()) {
+ vector<string>::iterator x;
+ for (x = channel_strings.begin(); x != channel_strings.end(); ++x) {
+ if (*x == existing_choice) {
+ channel_combo.set_active_text (existing_choice);
+ break;
+ }
+ }
+ if (x == channel_strings.end()) {
+ channel_combo.set_active_text (channel_strings.front());
+ }
+ } else {
+ channel_combo.set_active_text (channel_strings.front());
+ }
+
+ if (src_needed) {
+ src_combo.set_sensitive (true);
+ } else {
+ src_combo.set_sensitive (false);
+ }
+
+ if (Config->get_only_copy_imported_files()) {
+
+ if (selection_can_be_embedded_with_links) {
+ copy_files_btn.set_sensitive (true);
+ } else {
+ copy_files_btn.set_sensitive (false);
+ }
+
+ } else {
+
+ copy_files_btn.set_sensitive (true);
+ }
+
+ return true;
+}
+
+
+bool
+SoundFileOmega::bad_file_message()
+{
+ MessageDialog msg (*this,
+ _("One or more of the selected files\ncannot be used by Ardour"),
+ true,
+ Gtk::MESSAGE_INFO,
+ Gtk::BUTTONS_OK);
+ msg.run ();
+ resetting_ourselves = true;
+ chooser.unselect_uri (chooser.get_preview_uri());
+ resetting_ourselves = false;
+
+ return false;
+}
+
+bool
+SoundFileOmega::check_info (const vector<ustring>& paths, bool& same_size, bool& src_needed, bool& multichannel)
+{
+ SoundFileInfo info;
+ nframes64_t sz = 0;
+ bool err = false;
+ string errmsg;
+
+ same_size = true;
+ src_needed = false;
+ multichannel = false;
+
+ for (vector<ustring>::const_iterator i = paths.begin(); i != paths.end(); ++i) {
+
+ if (AudioFileSource::get_soundfile_info (*i, info, errmsg)) {
+ if (info.channels > 1) {
+ multichannel = true;
+ }
+ if (sz == 0) {
+ sz = info.length;
+ } else {
+ if (sz != info.length) {
+ same_size = false;
+ }
+ }
+
+ if ((nframes_t) info.samplerate != session->frame_rate()) {
+ src_needed = true;
+ }
+
+ } else if (SMFSource::safe_midi_file_extension (*i)) {
+
+ Evoral::SMF reader;
+ reader.open(*i);
+ if (reader.num_tracks() > 1) {
+ multichannel = true; // "channel" == track here...
+ }
+
+ /* XXX we need err = true handling here in case
+ we can't check the file
+ */
+
+ } else {
+ err = true;
+ }
+ }
+
+ return err;
+}
+
+
+bool
+SoundFileOmega::check_link_status (const Session& s, const vector<ustring>& paths)
+{
+ sys::path path = s.session_directory().sound_path() / "linktest";
+ string tmpdir = path.to_string();
+ bool ret = false;
+
+ if (mkdir (tmpdir.c_str(), 0744)) {
+ if (errno != EEXIST) {
+ return false;
+ }
+ }
+
+ for (vector<ustring>::const_iterator i = paths.begin(); i != paths.end(); ++i) {
+
+ char tmpc[MAXPATHLEN+1];
+
+ snprintf (tmpc, sizeof(tmpc), "%s/%s", tmpdir.c_str(), Glib::path_get_basename (*i).c_str());
+
+ /* can we link ? */
+
+ if (link ((*i).c_str(), tmpc)) {
+ goto out;
+ }
+
+ unlink (tmpc);
+ }
+
+ ret = true;
+
+ out:
+ rmdir (tmpdir.c_str());
+ return ret;
+}
+
+SoundFileChooser::SoundFileChooser (Gtk::Window& parent, string title, ARDOUR::Session* s)
+ : SoundFileBrowser (parent, title, s, false)
+{
+ chooser.set_select_multiple (false);
+ found_list_view.get_selection()->set_mode (SELECTION_SINGLE);
+ freesound_list_view.get_selection()->set_mode (SELECTION_SINGLE);