(native) Linux VST support from LinuxDSP
[ardour.git] / gtk2_ardour / export_range_markers_dialog.cc
1 /*
2     Copyright (C) 2006 Paul Davis
3     Author: Andre Raue
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 <sys/stat.h>
22
23 #include <sstream>
24
25 #include "pbd/filesystem.h"
26
27 #include "ardour/audioengine.h"
28 #include "ardour/sndfile_helpers.h"
29
30 #include "ardour_ui.h"
31 #include "export_range_markers_dialog.h"
32
33 #include "i18n.h"
34
35 using namespace Gtk;
36 using namespace ARDOUR;
37 using namespace PBD;
38 using namespace std;
39
40 ExportRangeMarkersDialog::ExportRangeMarkersDialog (PublicEditor& editor)
41         : ExportDialog(editor)
42 {
43         set_title (_("Export Ranges"));
44         file_frame.set_label (_("Export to Directory"));
45
46         do_not_allow_export_cd_markers();
47
48         total_duration = 0;
49         current_range_marker_index = 0;
50 }
51
52 Gtk::FileChooserAction
53 ExportRangeMarkersDialog::browse_action () const
54 {
55         return Gtk::FILE_CHOOSER_ACTION_CREATE_FOLDER;
56 }
57
58 void
59 ExportRangeMarkersDialog::export_data ()
60 {
61         getSession().locations()->apply(*this, &ExportRangeMarkersDialog::process_range_markers_export);
62 }
63
64 void
65 ExportRangeMarkersDialog::process_range_markers_export(Locations::LocationList& locations)
66 {
67         Locations::LocationList::iterator locationIter;
68         current_range_marker_index = 0;
69         init_progress_computing(locations);
70
71         for (locationIter = locations.begin(); locationIter != locations.end(); ++locationIter) {
72                 Location *currentLocation = (*locationIter);
73
74                 if(currentLocation->is_range_marker()){
75                         // init filename
76                         string filepath = get_target_filepath(
77                                 get_selected_file_name(),
78                                 currentLocation->name(),
79                                 get_selected_header_format());
80
81                         initSpec(filepath);
82
83                         spec.start_frame = currentLocation->start();
84                         spec.end_frame = currentLocation->end();
85
86                         if (getSession().start_export(spec)){
87                                 // if export fails
88                                 return;
89                         }
90
91                         // wait until export of this range finished
92                         gtk_main_iteration();
93
94                         while (spec.running){
95                                 if(gtk_events_pending()){
96                                         gtk_main_iteration();
97                                 }else {
98                                         usleep(10000);
99                                 }
100                         }
101
102                         current_range_marker_index++;
103
104                         getSession().stop_export (spec);
105                 }
106         }
107
108         spec.running = false;
109 }
110
111
112 string
113 ExportRangeMarkersDialog::get_target_filepath(string path, string filename, string postfix)
114 {
115         string target_path = path;
116         if ((target_path.find_last_of ('/')) != string::npos) {
117                 target_path += '/';
118         }
119
120         string target_filepath = target_path + filename + postfix;
121         struct stat statbuf;
122
123         for(int counter=1; (stat (target_filepath.c_str(), &statbuf) == 0); counter++){
124                 // while file exists
125                 ostringstream scounter;
126                 scounter.flush();
127                 scounter << counter;
128
129                 target_filepath =
130                         target_path + filename + "_" + scounter.str() + postfix;
131         }
132
133         return target_filepath;
134 }
135
136 bool
137 ExportRangeMarkersDialog::is_filepath_valid(string &filepath)
138 {
139         // sanity check file name first
140         struct stat statbuf;
141
142         if (filepath.empty()) {
143                 // warning dialog
144                 string txt = _("Please enter a valid target directory.");
145                 MessageDialog msg (*this, txt, false, MESSAGE_ERROR, BUTTONS_OK, true);
146                 msg.run();
147                 return false;
148         }
149
150         if ( (stat (filepath.c_str(), &statbuf) != 0) ||
151                 (!S_ISDIR (statbuf.st_mode)) ) {
152                 string txt = _("Please select an existing target directory. Files are not allowed!");
153                 MessageDialog msg (*this, txt, false, MESSAGE_ERROR, BUTTONS_OK, true);
154                 msg.run();
155                 return false;
156         }
157
158         // directory needs to exist and be writable
159         string dirpath = Glib::path_get_dirname (filepath);
160         if (!exists_and_writable (sys::path (dirpath))) {
161                 string txt = _("Cannot write file in: ") + dirpath;
162                 MessageDialog msg (*this, txt, false, MESSAGE_ERROR, BUTTONS_OK, true);
163                 msg.run();
164                 return false;
165         }
166
167         return true;
168 }
169
170 void
171 ExportRangeMarkersDialog::init_progress_computing(Locations::LocationList& locations)
172 {
173         // flush vector
174         range_markers_durations_aggregated.resize(0);
175
176         framecnt_t duration_before_current_location = 0;
177         Locations::LocationList::iterator locationIter;
178
179         for (locationIter = locations.begin(); locationIter != locations.end(); ++locationIter) {
180                 Location *currentLocation = (*locationIter);
181
182                 if(currentLocation->is_range_marker()){
183                         range_markers_durations_aggregated.push_back (duration_before_current_location);
184
185                         framecnt_t duration = currentLocation->end() - currentLocation->start();
186
187                         range_markers_durations.push_back (duration);
188                         duration_before_current_location += duration;
189                 }
190         }
191
192         total_duration = duration_before_current_location;
193 }
194
195
196 gint
197 ExportRangeMarkersDialog::progress_timeout ()
198 {
199         double progress = 0.0;
200
201         if (current_range_marker_index >= range_markers_durations.size()){
202                 progress = 1.0;
203         } else{
204                 progress = ((double) range_markers_durations_aggregated[current_range_marker_index] +
205                             (spec.progress * (double) range_markers_durations[current_range_marker_index])) /
206                         (double) total_duration;
207         }
208
209         set_progress_fraction( progress );
210         return TRUE;
211 }