put man pages here at top level
[ardour2.git] / gtk2_ardour / export_range_markers_dialog.cc
blob329493e195ea447bbf718b7f455a9897044761eb
1 /*
2 Copyright (C) 2006 Paul Davis
3 Author: Andre Raue
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.
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.
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.
21 #include <sys/stat.h>
23 #include <sstream>
25 #include <ardour/audioengine.h>
26 #include <ardour/sndfile_helpers.h>
28 #include "ardour_ui.h"
29 #include "export_range_markers_dialog.h"
31 #include "i18n.h"
33 using namespace Gtk;
34 using namespace ARDOUR;
35 using namespace PBD;
36 using namespace std;
38 ExportRangeMarkersDialog::ExportRangeMarkersDialog (PublicEditor& editor)
39 : ExportDialog(editor)
41 set_title (_("ardour: export ranges"));
42 file_frame.set_label (_("Export to Directory"));
44 do_not_allow_export_cd_markers();
46 total_duration = 0;
47 current_range_marker_index = 0;
50 Gtk::FileChooserAction
51 ExportRangeMarkersDialog::browse_action () const
53 return Gtk::FILE_CHOOSER_ACTION_CREATE_FOLDER;
56 void
57 ExportRangeMarkersDialog::export_audio_data ()
59 getSession().locations()->apply(*this, &ExportRangeMarkersDialog::process_range_markers_export);
62 void
63 ExportRangeMarkersDialog::process_range_markers_export(Locations::LocationList& locations)
65 Locations::LocationList::iterator locationIter;
66 current_range_marker_index = 0;
67 init_progress_computing(locations);
69 for (locationIter = locations.begin(); locationIter != locations.end(); ++locationIter) {
70 Location *currentLocation = (*locationIter);
72 if(currentLocation->is_range_marker()){
73 // init filename
74 string filepath = get_target_filepath(
75 get_selected_file_name(),
76 currentLocation->name(),
77 sndfile_file_ending_from_string(get_selected_header_format()));
79 initSpec(filepath);
81 spec.start_frame = currentLocation->start();
82 spec.end_frame = currentLocation->end();
84 if (getSession().start_audio_export(spec)){
85 // if export fails
86 return;
89 // wait until export of this range finished
90 gtk_main_iteration();
92 while (spec.running){
93 if(gtk_events_pending()){
94 gtk_main_iteration();
95 }else {
96 usleep(10000);
100 current_range_marker_index++;
102 getSession().stop_audio_export (spec);
106 spec.running = false;
110 string
111 ExportRangeMarkersDialog::get_target_filepath(string path, string filename, string postfix)
113 string target_path = path;
114 if ((target_path.find_last_of ('/')) != string::npos) {
115 target_path += '/';
118 string target_filepath = target_path + filename + postfix;
119 struct stat statbuf;
121 for(int counter=1; (stat (target_filepath.c_str(), &statbuf) == 0); counter++){
122 // while file exists
123 ostringstream scounter;
124 scounter.flush();
125 scounter << counter;
127 target_filepath =
128 target_path + filename + "_" + scounter.str() + postfix;
131 return target_filepath;
135 bool
136 ExportRangeMarkersDialog::is_filepath_valid(string &filepath)
138 // sanity check file name first
139 struct stat statbuf;
141 if (filepath.empty()) {
142 // warning dialog
143 string txt = _("Please enter a valid target directory.");
144 MessageDialog msg (*this, txt, false, MESSAGE_ERROR, BUTTONS_OK, true);
145 msg.run();
146 return false;
149 if ( (stat (filepath.c_str(), &statbuf) != 0) ||
150 (!S_ISDIR (statbuf.st_mode)) ) {
151 string txt = _("Please select an existing target directory. Files are not allowed!");
152 MessageDialog msg (*this, txt, false, MESSAGE_ERROR, BUTTONS_OK, true);
153 msg.run();
154 return false;
157 // directory needs to exist and be writable
158 string dirpath = Glib::path_get_dirname (filepath);
159 if (::access (dirpath.c_str(), W_OK) != 0) {
160 string txt = _("Cannot write file in: ") + dirpath;
161 MessageDialog msg (*this, txt, false, MESSAGE_ERROR, BUTTONS_OK, true);
162 msg.run();
163 return false;
166 return true;
170 void
171 ExportRangeMarkersDialog::init_progress_computing(Locations::LocationList& locations)
173 // flush vector
174 range_markers_durations_aggregated.resize(0);
176 nframes_t duration_before_current_location = 0;
177 Locations::LocationList::iterator locationIter;
179 for (locationIter = locations.begin(); locationIter != locations.end(); ++locationIter) {
180 Location *currentLocation = (*locationIter);
182 if(currentLocation->is_range_marker()){
183 range_markers_durations_aggregated.push_back (duration_before_current_location);
185 nframes_t duration = currentLocation->end() - currentLocation->start();
187 range_markers_durations.push_back (duration);
188 duration_before_current_location += duration;
192 total_duration = duration_before_current_location;
196 gint
197 ExportRangeMarkersDialog::progress_timeout ()
199 double progress = 0.0;
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;
209 set_progress_fraction( progress );
210 return TRUE;