Remove unused variable and the accessor methods for it from ARDOUR_UI
[ardour.git] / gtk2_ardour / editor_audio_import.cc
index 7067d9dce09a735a9079fca959f300caa1da3e82..2eb8b9140a08cd2801d3f088b038ce439be55b31 100644 (file)
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id$
 */
 
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <errno.h>
+#include <unistd.h>
+
 #include <pbd/pthread_utils.h>
 #include <pbd/basename.h>
+#include <pbd/shortpath.h>
 
 #include <gtkmm2ext/choice.h>
+#include <gtkmm2ext/window_title.h>
 
 #include <ardour/session.h>
 #include <ardour/audioplaylist.h>
@@ -40,6 +46,7 @@
 #include "sfdb_ui.h"
 #include "editing.h"
 #include "audio_time_axis.h"
+#include "utils.h"
 
 #include "i18n.h"
 
@@ -48,6 +55,7 @@ using namespace ARDOUR;
 using namespace PBD;
 using namespace sigc;
 using namespace Gtk;
+using namespace Gtkmm2ext;
 using namespace Editing;
 using Glib::ustring;
 
@@ -130,6 +138,8 @@ Editor::do_embed (vector<ustring> paths, bool split, ImportMode mode, AudioTrack
        vector<ustring>::iterator a;
 
        for (a = paths.begin(); a != paths.end(); ) {
+
+               cerr << "Considering embed of " << (*a) << endl;
        
                Glib::ustring path = *a;
                Glib::ustring pair_base;
@@ -167,7 +177,7 @@ Editor::do_embed (vector<ustring> paths, bool split, ImportMode mode, AudioTrack
                                                           to_embed.size() > 2 ? _("multichannel") : _("stereo")));
                        choices.push_back (_("Import as multiple regions"));
                        
-                       Gtkmm2ext::Choice chooser (string_compose (_("Paired files detected (%1, %2 ...).\nDo you want to:"),
+                       Choice chooser (string_compose (_("Paired files detected (%1, %2 ...).\nDo you want to:"),
                                                                   to_embed[0],
                                                                   to_embed[1]),
                                                   choices);
@@ -213,7 +223,9 @@ Editor::do_embed (vector<ustring> paths, bool split, ImportMode mode, AudioTrack
 int
 Editor::import_sndfile (vector<ustring> paths, ImportMode mode, AudioTrack* track, nframes_t& pos)
 {
-       interthread_progress_window->set_title (string_compose (_("ardour: importing %1"), paths.front()));
+       WindowTitle title = string_compose (_("importing %1"), paths.front());
+
+       interthread_progress_window->set_title (title.get_string());
        interthread_progress_window->set_position (Gtk::WIN_POS_MOUSE);
        interthread_progress_window->show_all ();
        interthread_progress_bar.set_fraction (0.0f);
@@ -242,6 +254,8 @@ Editor::import_sndfile (vector<ustring> paths, ImportMode mode, AudioTrack* trac
        while (!(import_status.done || import_status.cancel)) {
                gtk_main_iteration ();
        }
+
+       interthread_progress_window->hide ();
        
        import_status.done = true;
        interthread_progress_connection.disconnect ();
@@ -249,7 +263,7 @@ Editor::import_sndfile (vector<ustring> paths, ImportMode mode, AudioTrack* trac
        /* import thread finished - see if we should build a new track */
        
        if (!import_status.new_regions.empty()) {
-               boost::shared_ptr<AudioRegion> region (import_status.new_regions.front());
+               boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion>(import_status.new_regions.front());
                finish_bringing_in_audio (region, region->n_channels(), region->n_channels(), track, pos, mode);
        }
 
@@ -264,12 +278,12 @@ Editor::embed_sndfile (vector<Glib::ustring> paths, bool split, bool multiple_fi
        boost::shared_ptr<AudioFileSource> source;
        SourceList sources;
        boost::shared_ptr<AudioRegion> region;
-       string idspec;
        string linked_path;
        SoundFileInfo finfo;
        ustring region_name;
        uint32_t input_chan = 0;
        uint32_t output_chan = 0;
+       int ret = 0;
 
        track_canvas.get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
        ARDOUR_UI::instance()->flush_pending ();
@@ -285,7 +299,7 @@ Editor::embed_sndfile (vector<Glib::ustring> paths, bool split, bool multiple_fi
                linked_path += Glib::path_get_basename (path);
                
                if (link (path.c_str(), linked_path.c_str()) == 0) {
-                       
+
                        /* there are many reasons why link(2) might have failed.
                           but if it succeeds, we now have a link in the
                           session sound dir that will protect against
@@ -293,6 +307,21 @@ Editor::embed_sndfile (vector<Glib::ustring> paths, bool split, bool multiple_fi
                        */
                        
                        path = linked_path;
+
+               } else {
+
+                       /* one possible reason is that its already linked */
+
+                       if (errno == EEXIST) {
+                               struct stat sb;
+
+                               if (stat (linked_path.c_str(), &sb) == 0) {
+                                       if (sb.st_nlink > 1) { // its a hard link, assume its the one we want
+                                               path = linked_path;
+                                       }
+                               }
+                       }
+
                }
                
                /* note that we temporarily truncated _id at the colon */
@@ -301,7 +330,7 @@ Editor::embed_sndfile (vector<Glib::ustring> paths, bool split, bool multiple_fi
                
                if (!AudioFileSource::get_soundfile_info (path, finfo, error_msg)) {
                        error << string_compose(_("Editor: cannot open file \"%1\", (%2)"), selection, error_msg ) << endmsg;
-                       return 0;
+                       goto out;
                }
                
                if (check_sample_rate  && (finfo.samplerate != (int) session->frame_rate())) {
@@ -311,27 +340,48 @@ Editor::embed_sndfile (vector<Glib::ustring> paths, bool split, bool multiple_fi
                                choices.push_back (_("Cancel entire import"));
                                choices.push_back (_("Don't embed it"));
                                choices.push_back (_("Embed all without questions"));
+                       
+                               Gtkmm2ext::Choice rate_choice (
+                                       string_compose (_("%1\nThis audiofile's sample rate doesn't match the session sample rate!"), 
+                                                       short_path (path, 40)),
+                                       choices, false);
+                               
+                               int resx = rate_choice.run ();
+                               
+                               switch (resx) {
+                               case 0: /* stop a multi-file import */
+                               case 1: /* don't import this one */
+                                       ret = -1;
+                                       goto out;
+                               case 2: /* do it, and the rest without asking */
+                                       check_sample_rate = false;
+                                       break;
+                               case 3: /* do it */
+                                       break;
+                               default:
+                                       ret = -2;
+                                       goto out;
+                               }
                        } else {
                                choices.push_back (_("Cancel"));
-                       }
-                       
-                       choices.push_back (_("Embed it anyway"));
-                       
-                       Gtkmm2ext::Choice rate_choice (
-                               string_compose (_("%1\nThis audiofile's sample rate doesn't match the session sample rate!"), path),
-                               choices, false);
+                               choices.push_back (_("Embed it anyway"));
                        
-                       switch (rate_choice.run()) {
-                       case 0: /* stop a multi-file import */
-                       case 1: /* don't import this one */
-                               return -1;
-                       case 2: /* do it, and the rest without asking */
-                               check_sample_rate = false;
-                               break;
-                       case 3: /* do it */
-                               break;
-                       default:
-                               return -2;
+                               Gtkmm2ext::Choice rate_choice (
+                                       string_compose (_("%1\nThis audiofile's sample rate doesn't match the session sample rate!"), path),
+                                       choices, false);
+                               
+                               int resx = rate_choice.run ();
+                               
+                               switch (resx) {
+                               case 0: /* don't import */
+                                       ret = -1;
+                                       goto out;
+                               case 1: /* do it */
+                                       break;
+                               default:
+                                       ret = -2;
+                                       goto out;
+                               }
                        }
                }
                
@@ -341,18 +391,25 @@ Editor::embed_sndfile (vector<Glib::ustring> paths, bool split, bool multiple_fi
                /* make the proper number of channels in the region */
                
                input_chan += finfo.channels;
-       
+
                for (int n = 0; n < finfo.channels; ++n)
                {
-                       idspec = path;
-                       idspec += string_compose(":%1", n);
-                       
                        try {
-                               source = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createReadable 
-                                                                                      (*session, idspec, 
-                                                                                       (mode == ImportAsTapeTrack ? 
-                                                                                        AudioFileSource::Destructive : 
-                                                                                        AudioFileSource::Flag (0))));
+
+                               /* check if we have this thing embedded already */
+
+                               boost::shared_ptr<Source> s;
+
+                               if ((s = session->source_by_path_and_channel (path, n)) == 0) {
+                                       source = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createReadable 
+                                                                                              (DataType::AUDIO, *session, path,  n,
+                                                                                               (mode == ImportAsTapeTrack ? 
+                                                                                                AudioFileSource::Destructive : 
+                                                                                                AudioFileSource::Flag (0))));
+                               } else {
+                                       source = boost::dynamic_pointer_cast<AudioFileSource> (s);
+                               }
+
                                sources.push_back(source);
                        } 
                        
@@ -377,9 +434,9 @@ Editor::embed_sndfile (vector<Glib::ustring> paths, bool split, bool multiple_fi
        
        region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (sources, 0, sources[0]->length(), region_name, 0,
                                                                                  Region::Flag (Region::DefaultFlags|Region::WholeFile|Region::External)));
-       
+
        if (Config->get_output_auto_connect() & AutoConnectMaster) {
-               output_chan = (session->master_out() ? session->master_out()->n_inputs() : input_chan);
+               output_chan = (session->master_out() ? session->master_out()->n_inputs().n_audio() : input_chan);
        } else {
                output_chan = input_chan;
        }
@@ -388,7 +445,7 @@ Editor::embed_sndfile (vector<Glib::ustring> paths, bool split, bool multiple_fi
        
   out:
        track_canvas.get_window()->set_cursor (*current_canvas_cursor);
-       return 0;
+       return ret;
 }
 
 int