remove Features5, rework gradient plugin, fix paste_edl track title bug, gl_probe...
[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         mwindow->gui->lock_window("Save::save_before_quit");
126         saveas->quit_now = 1;
127         handle_event();
128         mwindow->gui->unlock_window();
129         return 0;
130 }
131
132 SaveAs::SaveAs(MWindow *mwindow)
133  : BC_MenuItem(_("Save as..."), "Shift-S", 'S'), Thread()
134 {
135         set_shift(1);
136         this->mwindow = mwindow;
137         quit_now = 0;
138 }
139
140 int SaveAs::set_mainmenu(MainMenu *mmenu)
141 {
142         this->mmenu = mmenu;
143         return 0;
144 }
145
146 int SaveAs::handle_event()
147 {
148         quit_now = 0;
149         start();
150         return 1;
151 }
152
153 void SaveAs::run()
154 {
155 // ======================================= get path from user
156         int result;
157 //printf("SaveAs::run 1\n");
158         char directory[1024], filename[1024];
159         sprintf(directory, "~");
160         mwindow->defaults->get("DIRECTORY", directory);
161
162 // Loop if file exists
163         do{
164                 SaveFileWindow *window;
165
166                 window = new SaveFileWindow(mwindow, directory);
167                 window->lock_window("SaveAs::run");
168                 window->create_objects();
169                 window->unlock_window();
170                 result = window->run_window();
171                 mwindow->defaults->update("DIRECTORY", window->get_submitted_path());
172                 strcpy(filename, window->get_submitted_path());
173                 delete window;
174
175 // Extend the filename with .xml
176                 if(strlen(filename) < 4 ||
177                         strcasecmp(&filename[strlen(filename) - 4], ".xml"))
178                 {
179                         strcat(filename, ".xml");
180                 }
181
182 // ======================================= try to save it
183                 if(filename[0] == 0) return;              // no filename given
184                 if(result == 1) return;          // user cancelled
185                 result = ConfirmSave::test_file(mwindow, filename);
186         }while(result);        // file exists so repeat
187
188 //printf("SaveAs::run 6 %s\n", filename);
189
190
191
192
193 // save it
194         FileXML file;
195         mwindow->gui->lock_window("SaveAs::run 1");
196 // update the project name
197         mwindow->set_filename(filename);
198         mwindow->edl->save_xml(&file, filename);
199         mwindow->gui->unlock_window();
200         file.terminate_string();
201
202         if(file.write_to_file(filename))
203         {
204                 char string2[256];
205                 mwindow->set_filename("");      // update the project name
206                 sprintf(string2, _("Couldn't open %s."), filename);
207                 ErrorBox error(_(PROGRAM_NAME ": Error"),
208                         mwindow->gui->get_abs_cursor_x(1),
209                         mwindow->gui->get_abs_cursor_y(1));
210                 error.create_objects(string2);
211                 error.raise_window();
212                 error.run_window();
213                 return;
214         }
215         else
216         {
217                 char string[BCTEXTLEN];
218                 sprintf(string, _("\"%s\" %dC written"), filename, (int)strlen(file.string()));
219                 mwindow->gui->lock_window("SaveAs::run 2");
220                 mwindow->gui->show_message(string);
221                 mwindow->gui->unlock_window();
222         }
223
224
225         mwindow->session->changes_made = 0;
226         mmenu->add_load(filename);
227 // Last command in program
228         if( quit_now )
229                 mwindow->quit();
230         return;
231 }
232
233
234 SaveFileWindow::SaveFileWindow(MWindow *mwindow, char *init_directory)
235  : BC_FileBox(mwindow->gui->get_abs_cursor_x(1),
236         mwindow->gui->get_abs_cursor_y(1) - BC_WindowBase::get_resources()->filebox_h / 2,
237         init_directory,
238         _(PROGRAM_NAME ": Save"),
239         _("Enter a filename to save as"))
240 {
241         this->mwindow = mwindow;
242 }
243
244 SaveFileWindow::~SaveFileWindow() {}
245
246
247
248 int SaveProjectModeItem::handle_event()
249 {
250         ((SaveProjectMode *)get_popup_menu())->update(id);
251         return 1;
252 }
253
254 SaveProjectMode::SaveProjectMode(SaveProjectWindow *gui, int x, int y)
255  : BC_PopupMenu(x, y, 100, "")
256 {
257         this->gui = gui;
258         save_modes[SAVE_PROJECT_COPY]     = _("Copy");
259         save_modes[SAVE_PROJECT_SYMLINK]  = _("SymLink");
260         save_modes[SAVE_PROJECT_RELLINK]  = _("RelLink");
261 }
262
263 SaveProjectMode::~SaveProjectMode()
264 {
265 }
266
267 void SaveProjectMode::create_objects()
268 {
269         add_item(new SaveProjectModeItem(save_modes[SAVE_PROJECT_COPY],    SAVE_PROJECT_COPY));
270         add_item(new SaveProjectModeItem(save_modes[SAVE_PROJECT_SYMLINK], SAVE_PROJECT_SYMLINK));
271         add_item(new SaveProjectModeItem(save_modes[SAVE_PROJECT_RELLINK], SAVE_PROJECT_RELLINK));
272         set_text(save_modes[gui->save_mode]);
273 }
274
275 void SaveProjectMode::update(int mode)
276 {
277         if( gui->save_mode == mode ) return;
278         set_text(save_modes[gui->save_mode = mode]);
279 }
280
281 SaveProjectTextBox::SaveProjectTextBox(SaveProjectWindow *gui, int x, int y, int w)
282  : BC_TextBox(x, y, w, 1, gui->dir_path)
283 {
284         this->gui = gui;
285 }
286 SaveProjectTextBox::~SaveProjectTextBox()
287 {
288 }
289
290 int SaveProjectTextBox::handle_event()
291 {
292         return 1;
293 }
294
295 SaveProjectWindow::SaveProjectWindow(MWindow *mwindow, const char *dir_path,
296                         int save_mode, int overwrite, int reload)
297  : BC_Window(_(PROGRAM_NAME ": Export Project"),
298                 mwindow->gui->get_abs_cursor_x(1),
299                 mwindow->gui->get_abs_cursor_y(1) -
300                         BC_WindowBase::get_resources()->filebox_h / 2,
301                 540, 220, 540, 220, 0)
302 {
303         this->mwindow = mwindow;
304         strcpy(this->dir_path, dir_path);
305         this->overwrite = overwrite;
306         this->save_mode = save_mode;
307         this->reload = reload;
308 }
309 SaveProjectWindow::~SaveProjectWindow()
310 {
311 }
312
313 void SaveProjectWindow::create_objects()
314 {
315         int x = 20, y = 20, x1 = get_w()-80;
316         BC_Title *title;
317         add_subwindow(title = new BC_Title(x, y, _("Project Directory:")));
318         y += title->get_h() + 10;
319         add_subwindow(textbox = new SaveProjectTextBox(this, x, y, x1-x));
320         x1 += 10;
321         add_subwindow(recent_project = new BC_RecentList("RECENT_PROJECT",
322                 mwindow->defaults, textbox, 10, x1, y, 300, 100));
323         recent_project->load_items("RECENT_PROJECT");
324         x1 += recent_project->get_w() + 10;
325         add_subwindow(browse_button = new BrowseButton(mwindow->theme, this,
326                 textbox, x1, y-5, "", "", "", 1));
327         y += textbox->get_h() + 20;
328         add_subwindow(mode_popup = new SaveProjectMode(this, x, y));
329         mode_popup->create_objects();
330         y += mode_popup->get_h() + 10;
331         x1 = x;
332         BC_CheckBox *overwrite_files, *reload_project;
333         add_subwindow(overwrite_files = new BC_CheckBox(x1, y, &overwrite, _("Overwrite files")));
334         x1 += overwrite_files->get_w() + 20;
335         add_subwindow(reload_project = new BC_CheckBox(x1, y, &reload, _("Reload project")));
336         add_subwindow(new BC_OKButton(this));
337         add_subwindow(new BC_CancelButton(this));
338         show_window(1);
339 }
340
341 SaveProject::SaveProject(MWindow *mwindow)
342  : BC_MenuItem(_("Export Project..."), "Alt-s", 's'), Thread()
343 {
344         set_alt(1);
345         this->mwindow = mwindow;
346 }
347
348 int SaveProject::handle_event()
349 {
350         start();
351         return 1;
352 }
353
354 void SaveProject::run()
355 {
356         char dir_path[1024];  sprintf(dir_path, "~");
357         mwindow->defaults->get("PROJECT_DIRECTORY", dir_path);
358         int reload = mwindow->defaults->get("PROJECT_RELOAD", 0);
359         int overwrite = mwindow->defaults->get("PROJECT_OVERWRITE", 0);
360         int save_mode = mwindow->defaults->get("PROJECT_SAVE_MODE", 0);
361
362         SaveProjectWindow window(mwindow, dir_path, save_mode, overwrite, reload);
363         window.lock_window("SaveProject::run");
364         window.create_objects();
365         window.unlock_window();
366         int result = window.run_window();
367         if( result ) return;
368
369         strcpy(dir_path, window.textbox->get_text());
370         window.recent_project->add_item("RECENT_PROJECT", dir_path);
371         reload = window.get_reload();
372         overwrite = window.get_overwrite();
373         save_mode = window.get_save_mode();
374         mwindow->defaults->update("PROJECT_DIRECTORY", dir_path);
375         mwindow->defaults->update("PROJECT_RELOAD", reload);
376         mwindow->defaults->update("PROJECT_OVERWRITE", overwrite);
377         mwindow->defaults->update("PROJECT_SAVE_MODE", save_mode);
378         mwindow->save_project(dir_path, save_mode, overwrite, reload);
379 }
380