Fix MIDI disk-writer flush
[ardour.git] / gtk2_ardour / session_archive_dialog.cc
1 /*
2  * Copyright (C) 2016-2019 Robin Gareus <robin@gareus.org>
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 <gtkmm/stock.h>
20 #include <gtkmm/table.h>
21
22 #include "ardour/filename_extensions.h"
23
24 #include "gtkmm2ext/utils.h"
25
26 #include "session_archive_dialog.h"
27
28 #include "pbd/i18n.h"
29
30 using namespace std;
31 using namespace Gtk;
32 using namespace ARDOUR;
33
34 SessionArchiveDialog::SessionArchiveDialog ()
35         : ArdourDialog (_("Zip/Archive Current Session"))
36         , ProgressReporter ()
37         , only_used_checkbox (_("Exclude unused audio sources"))
38 {
39         VBox* vbox = get_vbox();
40
41         vbox->set_spacing (6);
42
43         format_selector.append_text (ARDOUR::session_archive_suffix);
44         format_selector.set_active_text (ARDOUR::session_archive_suffix);
45
46         encode_selector.append_text (_("None"));
47         encode_selector.append_text (_("FLAC 16bit"));
48         encode_selector.append_text (_("FLAC 24bit"));
49         encode_selector.set_active_text (_("FLAC 16bit")); // TODO remember
50
51         compression_selector.append_text (_("None"));
52         compression_selector.append_text (_("Fast"));
53         compression_selector.append_text (_("Good"));
54         compression_selector.set_active_text (_("Good")); // TODO remember
55
56         Gtk::Table* table = manage (new Gtk::Table ());
57         table->set_col_spacings (10);
58         table->set_row_spacings (8);
59
60         Label* label;
61         int row = 0;
62
63         label = manage (new Label (_("Archive Name:"), Gtk::ALIGN_END, Gtk::ALIGN_CENTER, false));
64         table->attach (*label, 0, 1, row, row + 1, Gtk::FILL, Gtk::SHRINK);
65
66         HBox* hbox = manage (new HBox);
67         hbox->set_spacing (6);
68         hbox->pack_start (name_entry, true, true);
69         hbox->pack_start (format_selector, false, false);
70         table->attach (*hbox, 1, 2, row, row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::SHRINK);
71
72         ++row;
73
74         label = manage (new Label (_("Target directory/folder:"), Gtk::ALIGN_END, Gtk::ALIGN_CENTER, false));
75         table->attach (*label, 0, 1, row, row + 1, Gtk::FILL, Gtk::SHRINK);
76         table->attach (target_folder_selector, 1, 2, row, row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::SHRINK);
77
78         ++row;
79
80         label = manage (new Label (_("Audio Compression:"), Gtk::ALIGN_END, Gtk::ALIGN_CENTER, false));
81         table->attach (*label, 0, 1, row, row + 1, Gtk::FILL, Gtk::SHRINK);
82         table->attach (encode_selector, 1, 2, row, row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::SHRINK);
83
84         ++row;
85
86         label = manage (new Label (_("Archive Compression:"), Gtk::ALIGN_END, Gtk::ALIGN_CENTER, false));
87         table->attach (*label, 0, 1, row, row + 1, Gtk::FILL, Gtk::SHRINK);
88         table->attach (compression_selector, 1, 2, row, row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::SHRINK);
89
90         ++row;
91
92         table->attach (only_used_checkbox, 0, 2, row, row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::SHRINK);
93
94         ++row;
95
96         label = manage (new Label (_("Note: This archives only the current session state, snapshots are not included."), ALIGN_START));
97         label->set_line_wrap (true);
98         table->attach (*label, 0, 2, row, row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::SHRINK);
99
100         vbox->pack_start (*table, false, false);
101         vbox->pack_start (progress_bar, true, true, 12);
102
103         vbox->show_all ();
104         progress_bar.hide ();
105
106         add_button (Stock::CANCEL, RESPONSE_CANCEL);
107         add_button (Stock::OK, RESPONSE_OK);
108
109         Gtkmm2ext::add_volume_shortcuts (target_folder_selector);
110         target_folder_selector.set_action (FILE_CHOOSER_ACTION_SELECT_FOLDER);
111         target_folder_selector.set_current_folder (Config->get_default_session_parent_dir ()); // TODO get/set default_archive_dir
112         name_entry.signal_changed().connect (sigc::mem_fun (*this, &SessionArchiveDialog::name_entry_changed));
113         target_folder_selector.signal_current_folder_changed().connect (sigc::mem_fun (*this, &SessionArchiveDialog::name_entry_changed));
114         target_folder_selector.signal_selection_changed().connect (sigc::mem_fun (*this, &SessionArchiveDialog::name_entry_changed));
115         set_response_sensitive (RESPONSE_OK, false);
116 }
117
118
119 void
120 SessionArchiveDialog::name_entry_changed ()
121 {
122         if (name_entry.get_text().empty()) {
123                 set_response_sensitive (RESPONSE_OK, false);
124                 return;
125         }
126
127         std::string dir = Glib::build_filename (target_folder(), name_entry.get_text() + format_selector.get_active_text ());
128
129         if (Glib::file_test (dir, Glib::FILE_TEST_EXISTS)) {
130                 set_response_sensitive (RESPONSE_OK, false);
131                 return;
132         }
133
134         set_response_sensitive (RESPONSE_OK);
135 }
136
137 string
138 SessionArchiveDialog::target_folder () const
139 {
140         return target_folder_selector.get_filename ();
141 }
142
143 void
144 SessionArchiveDialog::set_target_folder (const std::string& name)
145 {
146         target_folder_selector.set_current_folder (name);
147         name_entry_changed ();
148 }
149
150 string
151 SessionArchiveDialog::name () const
152 {
153         return name_entry.get_text ();
154 }
155
156 void
157 SessionArchiveDialog::set_name (const std::string& name)
158 {
159         name_entry.set_text (name);
160         name_entry_changed ();
161 }
162
163 bool
164 SessionArchiveDialog::only_used_sources () const
165 {
166         return only_used_checkbox.get_active ();
167 }
168
169 void
170 SessionArchiveDialog::set_only_used_sources (bool en)
171 {
172         only_used_checkbox.set_active (en);
173 }
174
175 ARDOUR::Session::ArchiveEncode
176 SessionArchiveDialog::encode_option () const
177 {
178         string codec = encode_selector.get_active_text ();
179         if (codec == _("FLAC 16bit")) {
180                 return ARDOUR::Session::FLAC_16BIT;
181         }
182         if (codec == _("FLAC 24bit")) {
183                 return ARDOUR::Session::FLAC_24BIT;
184         }
185         return ARDOUR::Session::NO_ENCODE;
186 }
187
188 void
189 SessionArchiveDialog::set_encode_option (ARDOUR::Session::ArchiveEncode e)
190 {
191         switch (e) {
192                 case ARDOUR::Session::FLAC_16BIT:
193                         encode_selector.set_active_text (_("FLAC 16bit"));
194                         break;
195                 case ARDOUR::Session::FLAC_24BIT:
196                         encode_selector.set_active_text (_("FLAC 24bit"));
197                         break;
198                 default:
199                         encode_selector.set_active_text (_("None"));
200                         break;
201         }
202 }
203
204 PBD::FileArchive::CompressionLevel
205 SessionArchiveDialog::compression_level () const
206 {
207         string codec = compression_selector.get_active_text ();
208         if (codec == _("Fast")) {
209                 return PBD::FileArchive::CompressFast;
210         } else if (codec == _("None")) {
211                 return PBD::FileArchive::CompressNone;
212         }
213         return PBD::FileArchive::CompressGood;
214 }
215
216 void
217 SessionArchiveDialog::set_compression_level (PBD::FileArchive::CompressionLevel l)
218 {
219         switch (l) {
220                 case PBD::FileArchive::CompressFast:
221                         encode_selector.set_active_text (_("Fast"));
222                         break;
223                 case PBD::FileArchive::CompressNone:
224                         encode_selector.set_active_text (_("None"));
225                         break;
226                 case PBD::FileArchive::CompressGood:
227                         encode_selector.set_active_text (_("Good"));
228                         break;
229         }
230 }
231
232 void
233 SessionArchiveDialog::update_progress_gui (float p)
234 {
235         set_response_sensitive (RESPONSE_OK, false);
236         set_response_sensitive (RESPONSE_CANCEL, false);
237         progress_bar.show ();
238         if (p < 0) {
239                 progress_bar.set_text (_("Archiving Session"));
240                 return;
241         }
242         if (p > 1.0) {
243                 progress_bar.set_text (_("Encoding Audio"));
244                 return;
245         }
246         progress_bar.set_fraction (p);
247 }