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