Extend LV2UI-Request-Parameter File/Path GUI
[ardour.git] / gtk2_ardour / pt_import_selector.cc
1 /*
2     Copyright (C) 2018 Paul Davis
3     Author: Damien Zammit
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #include <pbd/error.h>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 #include <iostream>
25 #include "pbd/gstdio_compat.h"
26 #include "pbd/i18n.h"
27 #include "pbd/file_utils.h"
28
29 #include "ptformat/ptfformat.h"
30
31 #include "ardour/session_handle.h"
32 #include "pt_import_selector.h"
33
34 using namespace Gtk;
35 using namespace ARDOUR;
36 using namespace PBD;
37
38 PTImportSelector::PTImportSelector (PTFFormat& ptf) :
39         ArdourDialog (_("Import PT Session")),
40         ptimport_ptf_chooser (FILE_CHOOSER_ACTION_OPEN),
41         ptimport_import_button (_("Import")),
42         ptimport_cancel_button (_("Cancel"))
43 {
44         _ptf = &ptf;
45
46         if (!_session_rate) {
47                 Gtk::Dialog::response(RESPONSE_CANCEL);
48         }
49
50         set_size_request (800, 450);
51         ptimport_import_button.set_size_request (90, 35);
52         ptimport_cancel_button.set_size_request (90, 35);
53
54         Gtk::FileFilter match_pt_filter;
55
56         ptimport_info_text.set_editable (false);
57         ptimport_info_text.set_wrap_mode (Gtk::WRAP_NONE);
58         ptimport_info_text.get_buffer ()->set_text ("Select a PT session\n");
59         ptimport_info_text.set_sensitive (false);
60
61         match_pt_filter.add_pattern ("*.pt5");
62         match_pt_filter.add_pattern ("*.pt6");
63         match_pt_filter.add_pattern ("*.pt7");
64         match_pt_filter.add_pattern ("*.pts");
65         match_pt_filter.add_pattern ("*.ptf");
66         match_pt_filter.add_pattern ("*.ptx");
67         match_pt_filter.set_name (_("All PT sessions"));
68
69         ptimport_ptf_chooser.add_filter (match_pt_filter);
70         ptimport_ptf_chooser.set_select_multiple (false);
71         //XXX ptimport_ptf_chooser.set_current_folder (dstdir);
72
73
74         HBox* buttons = manage (new HBox);
75         buttons->set_spacing (2);
76         buttons->set_border_width (10);
77         buttons->pack_start (ptimport_import_button, false, false);
78         buttons->pack_start (ptimport_cancel_button, false, false);
79
80         HBox* infobox = manage (new HBox);
81         infobox->set_spacing (1);
82         infobox->set_border_width (50);
83         infobox->pack_start (ptimport_info_text, false, false);
84
85         HBox* toplevel = manage (new HBox);
86         toplevel->set_spacing (2);
87         toplevel->set_border_width (10);
88         toplevel->pack_start (ptimport_ptf_chooser, true, true);
89         toplevel->pack_start (*infobox, false, false);
90
91         get_vbox()->pack_start (*toplevel, true, true);
92         get_vbox()->pack_start (*buttons, false, false);
93
94         ptimport_ptf_chooser.signal_selection_changed ().connect (sigc::mem_fun (*this, &PTImportSelector::update_ptf));
95
96         ptimport_import_button.set_sensitive(false);
97         ptimport_cancel_button.set_sensitive(true);
98
99         ptimport_cancel_button.signal_clicked ().connect (sigc::bind (sigc::mem_fun (*this, &Gtk::Dialog::response), RESPONSE_CANCEL));
100         ptimport_import_button.signal_clicked ().connect (sigc::bind (sigc::mem_fun (*this, &Gtk::Dialog::response), RESPONSE_ACCEPT));
101
102         show_all ();
103 }
104
105 void
106 PTImportSelector::update_ptf()
107 {
108         if (ptimport_ptf_chooser.get_filename ().size () > 0) {
109                 std::string path = ptimport_ptf_chooser.get_filename ();
110                 bool ok = Glib::file_test(path.c_str(), Glib::FILE_TEST_IS_REGULAR | Glib::FILE_TEST_IS_SYMLINK)
111                                 && !Glib::file_test(path.c_str(), Glib::FILE_TEST_IS_DIR);
112                 if (ok) {
113                         if (_ptf->load (path, _session_rate) == -1) {
114                                 ptimport_info_text.get_buffer ()->set_text ("Cannot detect PT session\n");
115                                 ptimport_import_button.set_sensitive(false);
116                         } else {
117                                 std::string ptinfo = string_compose (_("PT Session [ VALID ]\n\nSession Info:\n\n\nPT v%1 Session @ %2Hz\n\n%3 audio files\n%4 audio regions\n%5 active audio regions\n%6 midi regions\n%7 active midi regions\n\n"),
118                                         (int)_ptf->version,
119                                         _ptf->sessionrate,
120                                         _ptf->audiofiles.size (),
121                                         _ptf->regions.size (),
122                                         _ptf->tracks.size (),
123                                         _ptf->midiregions.size (),
124                                         _ptf->miditracks.size ()
125                                 );
126                                 if (_session_rate != _ptf->sessionrate) {
127                                         ptinfo = string_compose (_("%1WARNING:\n\nSample rate mismatch,\nwill be resampling\n"), ptinfo);
128                                 }
129                                 ptimport_info_text.get_buffer ()->set_text (ptinfo);
130                                 ptimport_import_button.set_sensitive(true);
131                         }
132                 }
133         }
134 }
135
136 void
137 PTImportSelector::set_session (Session* s)
138 {
139         ArdourDialog::set_session (s);
140         _session_rate = s->sample_rate ();
141 }