Fix --template commandline option
[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
26 #include "pbd/gstdio_compat.h"
27 #include "pbd/i18n.h"
28 #include "pbd/file_utils.h"
29
30 #include "ptformat/ptformat.h"
31
32 #include "ardour/session_handle.h"
33
34 #include "gtkmm2ext/utils.h"
35
36 #include "pt_import_selector.h"
37
38 using namespace Gtk;
39 using namespace ARDOUR;
40 using namespace PBD;
41
42 PTImportSelector::PTImportSelector (PTFFormat& ptf) :
43         ArdourDialog (_("Import PT Session")),
44         ptimport_ptf_chooser (FILE_CHOOSER_ACTION_OPEN),
45         ptimport_import_button (_("Import")),
46         ptimport_cancel_button (_("Cancel"))
47 {
48         _ptf = &ptf;
49
50         if (!_session_rate) {
51                 Gtk::Dialog::response(RESPONSE_CANCEL);
52         }
53
54         set_size_request (800, 450);
55         ptimport_import_button.set_size_request (90, 35);
56         ptimport_cancel_button.set_size_request (90, 35);
57
58         Gtk::FileFilter match_pt_filter;
59
60         ptimport_info_text.set_editable (false);
61         ptimport_info_text.set_wrap_mode (Gtk::WRAP_NONE);
62         ptimport_info_text.get_buffer ()->set_text ("Select a PT session\n");
63         ptimport_info_text.set_sensitive (false);
64
65         match_pt_filter.add_pattern ("*.pt5");
66         match_pt_filter.add_pattern ("*.pt6");
67         match_pt_filter.add_pattern ("*.pt7");
68         match_pt_filter.add_pattern ("*.pts");
69         match_pt_filter.add_pattern ("*.ptf");
70         match_pt_filter.add_pattern ("*.ptx");
71         match_pt_filter.set_name (_("All PT sessions"));
72
73         Gtkmm2ext::add_volume_shortcuts (ptimport_ptf_chooser);
74         ptimport_ptf_chooser.add_filter (match_pt_filter);
75         ptimport_ptf_chooser.set_select_multiple (false);
76         //XXX ptimport_ptf_chooser.set_current_folder (dstdir);
77
78
79         HBox* buttons = manage (new HBox);
80         buttons->set_spacing (2);
81         buttons->set_border_width (10);
82         buttons->pack_start (ptimport_import_button, false, false);
83         buttons->pack_start (ptimport_cancel_button, false, false);
84
85         HBox* infobox = manage (new HBox);
86         infobox->set_spacing (1);
87         infobox->set_border_width (50);
88         infobox->pack_start (ptimport_info_text, false, false);
89
90         HBox* toplevel = manage (new HBox);
91         toplevel->set_spacing (2);
92         toplevel->set_border_width (10);
93         toplevel->pack_start (ptimport_ptf_chooser, true, true);
94         toplevel->pack_start (*infobox, false, false);
95
96         get_vbox()->pack_start (*toplevel, true, true);
97         get_vbox()->pack_start (*buttons, false, false);
98
99         ptimport_ptf_chooser.signal_selection_changed ().connect (sigc::mem_fun (*this, &PTImportSelector::update_ptf));
100
101         ptimport_import_button.set_sensitive(false);
102         ptimport_cancel_button.set_sensitive(true);
103
104         ptimport_cancel_button.signal_clicked ().connect (sigc::bind (sigc::mem_fun (*this, &Gtk::Dialog::response), RESPONSE_CANCEL));
105         ptimport_import_button.signal_clicked ().connect (sigc::bind (sigc::mem_fun (*this, &Gtk::Dialog::response), RESPONSE_ACCEPT));
106
107         show_all ();
108 }
109
110 void
111 PTImportSelector::update_ptf()
112 {
113         if (ptimport_ptf_chooser.get_filename ().size () > 0) {
114                 int err = 0;
115                 std::string path = ptimport_ptf_chooser.get_filename ();
116                 bool ok = Glib::file_test(path.c_str(), Glib::FILE_TEST_IS_REGULAR | Glib::FILE_TEST_IS_SYMLINK)
117                                 && !Glib::file_test(path.c_str(), Glib::FILE_TEST_IS_DIR);
118                 if (ok) {
119                         err = _ptf->load (path, _session_rate);
120                         if (err == -1) {
121                                 ptimport_info_text.get_buffer ()->set_text ("Cannot decrypt PT session\n");
122                                 ptimport_import_button.set_sensitive(false);
123                         } else if (err == -2) {
124                                 ptimport_info_text.get_buffer ()->set_text ("Cannot detect PT session\n");
125                                 ptimport_import_button.set_sensitive(false);
126                         } else if (err == -3) {
127                                 ptimport_info_text.get_buffer ()->set_text ("Incompatible PT version\n");
128                                 ptimport_import_button.set_sensitive(false);
129                         } else if (err == -4) {
130                                 ptimport_info_text.get_buffer ()->set_text ("Cannot parse PT session\n");
131                                 ptimport_import_button.set_sensitive(false);
132                         } else {
133                                 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"),
134                                         (int)_ptf->version (),
135                                         _ptf->sessionrate (),
136                                         _ptf->audiofiles ().size (),
137                                         _ptf->regions ().size (),
138                                         _ptf->tracks ().size (),
139                                         _ptf->midiregions ().size (),
140                                         _ptf->miditracks ().size ()
141                                 );
142                                 if (_session_rate != _ptf->sessionrate ()) {
143                                         ptinfo = string_compose (_("%1WARNING:\n\nSample rate mismatch,\nwill be resampling\n"), ptinfo);
144                                 }
145                                 ptimport_info_text.get_buffer ()->set_text (ptinfo);
146                                 ptimport_import_button.set_sensitive(true);
147                         }
148                 }
149         }
150 }
151
152 void
153 PTImportSelector::set_session (Session* s)
154 {
155         ArdourDialog::set_session (s);
156         _session_rate = s->sample_rate ();
157 }