lv2_blklst additions, ffmpeg est/bad times, proxy preview, cin.svg icon
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / savefile.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "confirmsave.h"
23 #include "bchash.h"
24 #include "bcrecentlist.h"
25 #include "edl.h"
26 #include "file.h"
27 #include "filexml.h"
28 #include "fileformat.h"
29 #include "indexfile.h"
30 #include "language.h"
31 #include "mainerror.h"
32 #include "mainmenu.h"
33 #include "mwindow.h"
34 #include "mwindowgui.h"
35 #include "playback3d.h"
36 #include "savefile.h"
37 #include "mainsession.h"
38
39 #include <string.h>
40
41
42
43
44
45
46
47
48
49 SaveBackup::SaveBackup(MWindow *mwindow)
50  : BC_MenuItem(_("Save backup"), "b", 'b')
51 {
52         this->mwindow = mwindow;
53 }
54 int SaveBackup::handle_event()
55 {
56         mwindow->save_backup();
57         mwindow->gui->show_message(_("Saved backup."));
58         return 1;
59 }
60
61
62
63
64
65
66
67
68
69
70
71 Save::Save(MWindow *mwindow) : BC_MenuItem(_("Save"), "s", 's')
72 {
73         this->mwindow = mwindow;
74         quit_now = 0;
75 }
76
77 void Save::create_objects(SaveAs *saveas)
78 {
79         this->saveas = saveas;
80 }
81
82 int Save::handle_event()
83 {
84         if(mwindow->session->filename[0] == 0)
85         {
86                 saveas->start();
87         }
88         else
89         {
90 // save it
91 // TODO: Move this into mwindow.
92                 FileXML file;
93                 mwindow->edl->save_xml(&file, mwindow->session->filename);
94                 file.terminate_string();
95
96                 if(file.write_to_file(mwindow->session->filename))
97                 {
98                         char string2[256];
99                         sprintf(string2, _("Couldn't open %s"), mwindow->session->filename);
100                         ErrorBox error(_(PROGRAM_NAME ": Error"),
101                                 mwindow->gui->get_abs_cursor_x(1),
102                                 mwindow->gui->get_abs_cursor_y(1));
103                         error.create_objects(string2);
104                         error.raise_window();
105                         error.run_window();
106                         return 1;
107                 }
108                 else
109                 {
110                         char string[BCTEXTLEN];
111                         sprintf(string, _("\"%s\" %dC written"),
112                                  mwindow->session->filename, (int)strlen(file.string()));
113                         mwindow->gui->show_message(string);
114                 }
115                 mwindow->session->changes_made = 0;
116 // Last command in program
117                 if( saveas->quit_now )
118                         mwindow->quit();
119         }
120         return 1;
121 }
122
123 int Save::save_before_quit()
124 {
125         saveas->quit_now = 1;
126         handle_event();
127         return 0;
128 }
129
130 SaveAs::SaveAs(MWindow *mwindow)
131  : BC_MenuItem(_("Save as..."), "Shift-S", 'S'), Thread()
132 {
133         set_shift(1);
134         this->mwindow = mwindow;
135         quit_now = 0;
136 }
137
138 int SaveAs::set_mainmenu(MainMenu *mmenu)
139 {
140         this->mmenu = mmenu;
141         return 0;
142 }
143
144 int SaveAs::handle_event()
145 {
146         quit_now = 0;
147         start();
148         return 1;
149 }
150
151 void SaveAs::run()
152 {
153 // ======================================= get path from user
154         int result;
155 //printf("SaveAs::run 1\n");
156         char directory[1024], filename[1024];
157         sprintf(directory, "~");
158         mwindow->defaults->get("DIRECTORY", directory);
159
160 // Loop if file exists
161         do{
162                 SaveFileWindow *window;
163
164                 window = new SaveFileWindow(mwindow, directory);
165                 window->lock_window("SaveAs::run");
166                 window->create_objects();
167                 window->unlock_window();
168                 result = window->run_window();
169                 mwindow->defaults->update("DIRECTORY", window->get_submitted_path());
170                 strcpy(filename, window->get_submitted_path());
171                 delete window;
172
173 // Extend the filename with .xml
174                 if(strlen(filename) < 4 ||
175                         strcasecmp(&filename[strlen(filename) - 4], ".xml"))
176                 {
177                         strcat(filename, ".xml");
178                 }
179
180 // ======================================= try to save it
181                 if(filename[0] == 0) return;              // no filename given
182                 if(result == 1) return;          // user cancelled
183                 result = ConfirmSave::test_file(mwindow, filename);
184         }while(result);        // file exists so repeat
185
186 //printf("SaveAs::run 6 %s\n", filename);
187
188
189
190
191 // save it
192         FileXML file;
193         mwindow->gui->lock_window("SaveAs::run 1");
194 // update the project name
195         mwindow->set_filename(filename);
196         mwindow->edl->save_xml(&file, filename);
197         mwindow->gui->unlock_window();
198         file.terminate_string();
199
200         if(file.write_to_file(filename))
201         {
202                 char string2[256];
203                 mwindow->set_filename("");      // update the project name
204                 sprintf(string2, _("Couldn't open %s."), filename);
205                 ErrorBox error(_(PROGRAM_NAME ": Error"),
206                         mwindow->gui->get_abs_cursor_x(1),
207                         mwindow->gui->get_abs_cursor_y(1));
208                 error.create_objects(string2);
209                 error.raise_window();
210                 error.run_window();
211                 return;
212         }
213         else
214         {
215                 char string[BCTEXTLEN];
216                 sprintf(string, _("\"%s\" %dC written"), filename, (int)strlen(file.string()));
217                 mwindow->gui->lock_window("SaveAs::run 2");
218                 mwindow->gui->show_message(string);
219                 mwindow->gui->unlock_window();
220         }
221
222
223         mwindow->session->changes_made = 0;
224         mmenu->add_load(filename);
225 // Last command in program
226         if( quit_now )
227                 mwindow->quit();
228         return;
229 }
230
231
232 SaveFileWindow::SaveFileWindow(MWindow *mwindow, char *init_directory)
233  : BC_FileBox(mwindow->gui->get_abs_cursor_x(1),
234         mwindow->gui->get_abs_cursor_y(1) - BC_WindowBase::get_resources()->filebox_h / 2,
235         init_directory,
236         _(PROGRAM_NAME ": Save"),
237         _("Enter a filename to save as"))
238 {
239         this->mwindow = mwindow;
240 }
241
242 SaveFileWindow::~SaveFileWindow() {}
243
244
245
246 int SaveProjectModeItem::handle_event()
247 {
248         ((SaveProjectMode *)get_popup_menu())->update(id);
249         return 1;
250 }
251
252 SaveProjectMode::SaveProjectMode(SaveProjectWindow *gui, int x, int y)
253  : BC_PopupMenu(x, y, 100, "")
254 {
255         this->gui = gui;
256         save_modes[SAVE_PROJECT_COPY]     = _("Copy");
257         save_modes[SAVE_PROJECT_SYMLINK]  = _("SymLink");
258         save_modes[SAVE_PROJECT_RELLINK]  = _("RelLink");
259 }
260
261 SaveProjectMode::~SaveProjectMode()
262 {
263 }
264
265 void SaveProjectMode::create_objects()
266 {
267         add_item(new SaveProjectModeItem(save_modes[SAVE_PROJECT_COPY],    SAVE_PROJECT_COPY));
268         add_item(new SaveProjectModeItem(save_modes[SAVE_PROJECT_SYMLINK], SAVE_PROJECT_SYMLINK));
269         add_item(new SaveProjectModeItem(save_modes[SAVE_PROJECT_RELLINK], SAVE_PROJECT_RELLINK));
270         set_text(save_modes[gui->save_mode]);
271 }
272
273 void SaveProjectMode::update(int mode)
274 {
275         if( gui->save_mode == mode ) return;
276         set_text(save_modes[gui->save_mode = mode]);
277 }
278
279 SaveProjectTextBox::SaveProjectTextBox(SaveProjectWindow *gui, int x, int y, int w)
280  : BC_TextBox(x, y, w, 1, gui->dir_path)
281 {
282         this->gui = gui;
283 }
284 SaveProjectTextBox::~SaveProjectTextBox()
285 {
286 }
287
288 int SaveProjectTextBox::handle_event()
289 {
290         return 1;
291 }
292
293 SaveProjectWindow::SaveProjectWindow(MWindow *mwindow, const char *dir_path,
294                         int save_mode, int overwrite, int reload)
295  : BC_Window(_(PROGRAM_NAME ": Export Project"),
296                 mwindow->gui->get_abs_cursor_x(1),
297                 mwindow->gui->get_abs_cursor_y(1) -
298                         BC_WindowBase::get_resources()->filebox_h / 2,
299                 540, 220, 540, 220, 0)
300 {
301         this->mwindow = mwindow;
302         strcpy(this->dir_path, dir_path);
303         this->overwrite = overwrite;
304         this->save_mode = save_mode;
305         this->reload = reload;
306 }
307 SaveProjectWindow::~SaveProjectWindow()
308 {
309 }
310
311 void SaveProjectWindow::create_objects()
312 {
313         int x = 20, y = 20, x1 = get_w()-80;
314         BC_Title *title;
315         add_subwindow(title = new BC_Title(x, y, _("Project Directory:")));
316         y += title->get_h() + 10;
317         add_subwindow(textbox = new SaveProjectTextBox(this, x, y, x1-x));
318         x1 += 10;
319         add_subwindow(recent_project = new BC_RecentList("RECENT_PROJECT",
320                 mwindow->defaults, textbox, 10, x1, y, 300, 100));
321         recent_project->load_items("RECENT_PROJECT");
322         x1 += recent_project->get_w() + 10;
323         add_subwindow(browse_button = new BrowseButton(mwindow->theme, this,
324                 textbox, x1, y-5, "", "", "", 1));
325         y += textbox->get_h() + 20;
326         add_subwindow(mode_popup = new SaveProjectMode(this, x, y));
327         mode_popup->create_objects();
328         y += mode_popup->get_h() + 10;
329         x1 = x;
330         BC_CheckBox *overwrite_files, *reload_project;
331         add_subwindow(overwrite_files = new BC_CheckBox(x1, y, &overwrite, _("Overwrite files")));
332         x1 += overwrite_files->get_w() + 20;
333         add_subwindow(reload_project = new BC_CheckBox(x1, y, &reload, _("Reload project")));
334         add_subwindow(new BC_OKButton(this));
335         add_subwindow(new BC_CancelButton(this));
336         show_window(1);
337 }
338
339 SaveProject::SaveProject(MWindow *mwindow)
340  : BC_MenuItem(_("Export Project..."), "Alt-s", 's'), Thread()
341 {
342         set_alt(1);
343         this->mwindow = mwindow;
344 }
345
346 int SaveProject::handle_event()
347 {
348         start();
349         return 1;
350 }
351
352 void SaveProject::run()
353 {
354         char dir_path[1024];  sprintf(dir_path, "~");
355         mwindow->defaults->get("PROJECT_DIRECTORY", dir_path);
356         int reload = mwindow->defaults->get("PROJECT_RELOAD", 0);
357         int overwrite = mwindow->defaults->get("PROJECT_OVERWRITE", 0);
358         int save_mode = mwindow->defaults->get("PROJECT_SAVE_MODE", 0);
359
360         SaveProjectWindow window(mwindow, dir_path, save_mode, overwrite, reload);
361         window.lock_window("SaveProject::run");
362         window.create_objects();
363         window.unlock_window();
364         int result = window.run_window();
365         if( result ) return;
366
367         strcpy(dir_path, window.textbox->get_text());
368         window.recent_project->add_item("RECENT_PROJECT", dir_path);
369         reload = window.get_reload();
370         overwrite = window.get_overwrite();
371         save_mode = window.get_save_mode();
372         mwindow->defaults->update("PROJECT_DIRECTORY", dir_path);
373         mwindow->defaults->update("PROJECT_RELOAD", reload);
374         mwindow->defaults->update("PROJECT_OVERWRITE", overwrite);
375         mwindow->defaults->update("PROJECT_SAVE_MODE", save_mode);
376         mwindow->save_project(dir_path, save_mode, overwrite, reload);
377 }
378