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