Only show user-presets in favorite sidebar
[ardour.git] / gtk2_ardour / editor_pt_import.cc
index 3639fcf877f5fc468b794b69184e11b9bbe8365e..12b6484b783f82907651e60ac9cfaa81f9876a11 100644 (file)
 #include "pbd/shortpath.h"
 #include "pbd/stateful_diff_command.h"
 
-#include <gtkmm2ext/choice.h>
-
 #include "ardour/audio_track.h"
 #include "ardour/audiofilesource.h"
 #include "ardour/audioregion.h"
 #include "ardour/midi_region.h"
 #include "ardour/midi_track.h"
+#include "ardour/midi_model.h"
 #include "ardour/operations.h"
 #include "ardour/region_factory.h"
 #include "ardour/smf_source.h"
@@ -59,8 +58,9 @@
 #include "interthread_progress_window.h"
 #include "mouse_cursors.h"
 #include "editor_cursors.h"
+#include "pt_import_selector.h"
 
-#include "i18n.h"
+#include "pbd/i18n.h"
 
 using namespace std;
 using namespace ARDOUR;
@@ -70,7 +70,7 @@ using namespace Gtkmm2ext;
 using namespace Editing;
 using std::string;
 
-/* Functions supporting the incorporation of PT sessions into ardour */
+/* Editor dialogs supporting the incorporation of PT sessions into ardour */
 
 void
 Editor::external_pt_dialog ()
@@ -83,189 +83,57 @@ Editor::external_pt_dialog ()
                return;
        }
 
-       Gtk::FileChooserDialog dialog(_("Import PT Session"), FILE_CHOOSER_ACTION_OPEN);
-       dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
-       dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
+       PTImportSelector dialog (import_ptf);
+       dialog.set_session (_session);
 
        while (true) {
-               int result = dialog.run();
+               int result = dialog.run ();
 
-               if (result == Gtk::RESPONSE_OK) {
-                       ptpath = dialog.get_filename ();
+               if (result == Gtk::RESPONSE_ACCEPT) {
 
-                       if (!Glib::file_test (ptpath, Glib::FILE_TEST_IS_DIR|Glib::FILE_TEST_EXISTS)) {
-                               Gtk::MessageDialog msg (string_compose (_("%1: this is only the directory/folder name, not the filename.\n"), ptpath));
-                               msg.run ();
-                               continue;
-                       }
-               }
+                       import_pt_status.all_done = false;
 
-               if (ptpath.length()) {
-                       do_ptimport(ptpath, SrcBest);
-                       break;
-               }
+                       ImportProgressWindow ipw (&import_pt_status, _("PT Import"), _("Cancel Import"));
+                       pthread_create_and_store ("import_pt", &import_pt_status.thread, _import_pt_thread, this);
+                       pthread_detach (import_pt_status.thread);
 
-               if (result == Gtk::RESPONSE_CANCEL) {
-                       break;
-               }
-       }
-}
-
-void
-Editor::do_ptimport (std::string ptpath,
-                      SrcQuality  quality)
-{
-       vector<boost::shared_ptr<Region> > regions;
-       boost::shared_ptr<ARDOUR::Track> track;
-       ARDOUR::PluginInfoPtr instrument;
-       vector<string> to_import;
-       string fullpath;
-       bool ok = false;
-       bool onefailed = false;
-       PTFFormat ptf;
-       framepos_t pos = -1;
+                       ipw.show();
 
-       vector<ptflookup_t> ptfwavpair;
-       vector<ptflookup_t> ptfregpair;
-
-       if (ptf.load(ptpath, _session->frame_rate()) == -1) {
-               MessageDialog msg (_("Doesn't seem to be a valid PT session file"));
-               msg.run ();
-               return;
-       } else {
-               MessageDialog msg (string_compose (_("PT v%1 Session @ %2Hz\n\n%3 audio files\n%4 regions\n%5 active regions\n\nContinue..."), (int)ptf.version, ptf.sessionrate, ptf.audiofiles.size(), ptf.regions.size(), ptf.tracks.size()));
-               msg.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
-
-               int result = msg.run ();
-               if (result != Gtk::RESPONSE_OK) {
-                       return;
-               }
-       }
-       current_interthread_info = &import_status;
-       import_status.current = 1;
-       import_status.total = ptf.audiofiles.size ();
-       import_status.all_done = false;
-
-       ImportProgressWindow ipw (&import_status, _("Import"), _("Cancel Import"));
-
-       SourceList just_one;
-       SourceList imported;
-
-       for (vector<PTFFormat::wav_t>::iterator a = ptf.audiofiles.begin(); a != ptf.audiofiles.end(); ++a) {
-               ptflookup_t p;
-
-               fullpath = Glib::build_filename (Glib::path_get_dirname(ptpath), "Audio Files");
-               fullpath = Glib::build_filename (fullpath, a->filename);
-               to_import.clear ();
-               to_import.push_back (fullpath);
-               ipw.show ();
-               ok = import_sndfiles (to_import, Editing::ImportDistinctFiles, Editing::ImportAsRegion, quality, pos, 1, -1, track, false, instrument);
-               if (!import_status.sources.empty()) {
-                       p.index1 = a->index;
-                       p.id = import_status.sources.back()->id();
-
-                       ptfwavpair.push_back(p);
-                       imported.push_back(import_status.sources.back());
-               } else {
-                       onefailed = true;
-               }
-       }
-
-       if (onefailed) {
-               MessageDialog msg (_("Failed to load one or more of the audio files, but continuing to attempt import."));
-               msg.run ();
-       } else {
-               MessageDialog msg (_("Success! Import should complete soon."));
-               msg.run ();
-       }
-
-       for (vector<PTFFormat::region_t>::iterator a = ptf.regions.begin();
-                       a != ptf.regions.end(); ++a) {
-               for (vector<ptflookup_t>::iterator p = ptfwavpair.begin();
-                               p != ptfwavpair.end(); ++p) {
-                       if (p->index1 == a->wave.index) {
-                               for (SourceList::iterator x = imported.begin();
-                                               x != imported.end(); ++x) {
-                                       if ((*x)->id() == p->id) {
-                                               // Matched an uncreated ptf region to ardour region
-                                               ptflookup_t rp;
-                                               PropertyList plist;
-
-                                               plist.add (ARDOUR::Properties::start, a->sampleoffset);
-                                               plist.add (ARDOUR::Properties::position, 0);
-                                               plist.add (ARDOUR::Properties::length, a->length);
-                                               plist.add (ARDOUR::Properties::name, a->name);
-                                               plist.add (ARDOUR::Properties::layer, 0);
-                                               plist.add (ARDOUR::Properties::whole_file, false);
-                                               plist.add (ARDOUR::Properties::external, true);
-
-                                               just_one.clear();
-                                               just_one.push_back(*x);
-
-                                               boost::shared_ptr<Region> r = RegionFactory::create (just_one, plist);
-                                               regions.push_back(r);
-
-                                               rp.id = regions.back()->id();
-                                               rp.index1 = a->index;
-                                               ptfregpair.push_back(rp);
-                                       }
-                               }
+                       while (!import_pt_status.all_done) {
+                               gtk_main_iteration ();
                        }
-               }
-       }
 
-       boost::shared_ptr<AudioTrack> existing_track;
-       uint16_t nth = 0;
-       vector<ptflookup_t> usedtracks;
-       ptflookup_t utr;
-
-       for (vector<PTFFormat::track_t>::iterator a = ptf.tracks.begin();
-                       a != ptf.tracks.end(); ++a) {
-               for (vector<ptflookup_t>::iterator p = ptfregpair.begin();
-                               p != ptfregpair.end(); ++p) {
+                       // wait for thread to terminate
+                       while (!import_pt_status.done) {
+                               gtk_main_iteration ();
+                       }
 
-                       if (p->index1 == a->reg.index)  {
-                               // Matched a ptf active region to an ardour region
-                               utr.index1 = a->index;
-                               utr.index2 = nth;
-                               utr.id = p->id;
-                               boost::shared_ptr<Region> r = RegionFactory::region_by_id (p->id);
-                               vector<ptflookup_t>::iterator lookuptr = usedtracks.begin();
-                               vector<ptflookup_t>::iterator found;
-                               if ((found = std::find(lookuptr, usedtracks.end(), utr)) != usedtracks.end()) {
-                                       DEBUG_TRACE (DEBUG::FileUtils, string_compose ("\twav(%1) reg(%2) ptf_tr(%3) ard_tr(%4)\n", a->reg.wave.filename.c_str(), a->reg.index, found->index1, found->index2));
-                                       existing_track =  get_nth_selected_audio_track(found->index2);
-                                       // Put on existing track
-                                       boost::shared_ptr<Playlist> playlist = existing_track->playlist();
-                                       boost::shared_ptr<Region> copy (RegionFactory::create (r, true));
-                                       playlist->clear_changes ();
-                                       playlist->add_region (copy, a->reg.startpos);
-                                       //_session->add_command (new StatefulDiffCommand (playlist));
-                               } else {
-                                       // Put on a new track
-                                       DEBUG_TRACE (DEBUG::FileUtils, string_compose ("\twav(%1) reg(%2) new_tr(%3)\n", a->reg.wave.filename.c_str(), a->reg.index, nth));
-                                       list<boost::shared_ptr<AudioTrack> > at (_session->new_audio_track (1, 2, Normal, 0, 1));
-                                       if (at.empty()) {
-                                               return;
-                                       }
-                                       existing_track = at.back();
-                                       existing_track->set_name (a->name);
-                                       boost::shared_ptr<Playlist> playlist = existing_track->playlist();
-                                       boost::shared_ptr<Region> copy (RegionFactory::create (r, true));
-                                       playlist->clear_changes ();
-                                       playlist->add_region (copy, a->reg.startpos);
-                                       //_session->add_command (new StatefulDiffCommand (playlist));
-                                       nth++;
-                               }
-                               usedtracks.push_back(utr);
+                       if (import_pt_status.cancel) {
+                               MessageDialog msg (_("PT import may have missing files, check session log for details"));
+                               msg.run ();
+                       } else {
+                               MessageDialog msg (_("PT import complete!"));
+                               msg.run ();
                        }
+                       break;
+               } else if (result == Gtk::RESPONSE_CANCEL) {
+                       break;
                }
        }
+}
 
-       import_status.sources.clear();
+void *
+Editor::_import_pt_thread (void *arg)
+{
+       SessionEvent::create_per_thread_pool ("import pt events", 64);
 
-       if (ok) {
-               _session->save_state ("");
-       }
-       import_status.all_done = true;
+       Editor *ed = (Editor *) arg;
+       return ed->import_pt_thread ();
+}
+
+void *
+Editor::import_pt_thread ()
+{
+       _session->import_pt (import_ptf, import_pt_status);
+       return 0;
 }