00dee98145f3c773b7fad2c548765084972bf45c
[goodguy/history.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 "edl.h"
25 #include "errorbox.h"
26 #include "file.h"
27 #include "filexml.h"
28 #include "fileformat.h"
29 #include "indexfile.h"
30 #include "language.h"
31 #include "mainmenu.h"
32 #include "mwindow.h"
33 #include "mwindowgui.h"
34 #include "playback3d.h"
35 #include "savefile.h"
36 #include "mainsession.h"
37
38 #include <string.h>
39
40
41
42
43
44
45
46
47
48 SaveBackup::SaveBackup(MWindow *mwindow)
49  : BC_MenuItem(_("Save backup"))
50 {
51         this->mwindow = mwindow;
52 }
53 int SaveBackup::handle_event()
54 {
55         mwindow->save_backup();
56         mwindow->gui->show_message(_("Saved backup."));
57         return 1;
58 }
59
60
61
62
63
64
65
66
67
68
69
70 Save::Save(MWindow *mwindow) : BC_MenuItem(_("Save"), "s", 's')
71 {
72         this->mwindow = mwindow;
73         quit_now = 0;
74 }
75
76 void Save::create_objects(SaveAs *saveas)
77 {
78         this->saveas = saveas;
79 }
80
81 int Save::handle_event()
82 {
83         if(mwindow->session->filename[0] == 0)
84         {
85                 saveas->start();
86         }
87         else
88         {
89 // save it
90 // TODO: Move this into mwindow.
91                 FileXML file;
92                 mwindow->edl->save_xml(&file,
93                         mwindow->session->filename,
94                         0,
95                         0);
96                 file.terminate_string();
97
98                 if(file.write_to_file(mwindow->session->filename))
99                 {
100                         char string2[256];
101                         sprintf(string2, _("Couldn't open %s"), mwindow->session->filename);
102                         ErrorBox error(_(PROGRAM_NAME ": Error"),
103                                 mwindow->gui->get_abs_cursor_x(1),
104                                 mwindow->gui->get_abs_cursor_y(1));
105                         error.create_objects(string2);
106                         error.raise_window();
107                         error.run_window();
108                         return 1;
109                 }
110                 else
111                 {
112                         char string[BCTEXTLEN];
113                         sprintf(string, _("\"%s\" %dC written"),
114                                  mwindow->session->filename, (int)strlen(file.string()));
115                         mwindow->gui->show_message(string);
116                 }
117                 mwindow->session->changes_made = 0;
118 // Last command in program
119                 if( saveas->quit_now )
120                         mwindow->quit();
121         }
122         return 1;
123 }
124
125 int Save::save_before_quit()
126 {
127         saveas->quit_now = 1;
128         handle_event();
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,
199                 filename,
200                 0,
201                 0);
202         mwindow->gui->unlock_window();
203         file.terminate_string();
204
205         if(file.write_to_file(filename))
206         {
207                 char string2[256];
208                 mwindow->set_filename("");      // update the project name
209                 sprintf(string2, _("Couldn't open %s."), filename);
210                 ErrorBox error(_(PROGRAM_NAME ": Error"),
211                         mwindow->gui->get_abs_cursor_x(1),
212                         mwindow->gui->get_abs_cursor_y(1));
213                 error.create_objects(string2);
214                 error.raise_window();
215                 error.run_window();
216                 return;
217         }
218         else
219         {
220                 char string[BCTEXTLEN];
221                 sprintf(string, _("\"%s\" %dC written"), filename, (int)strlen(file.string()));
222                 mwindow->gui->lock_window("SaveAs::run 2");
223                 mwindow->gui->show_message(string);
224                 mwindow->gui->unlock_window();
225         }
226
227
228         mwindow->session->changes_made = 0;
229         mmenu->add_load(filename);
230 // Last command in program
231         if( quit_now )
232                 mwindow->quit();
233         return;
234 }
235
236
237
238
239
240
241
242
243 SaveFileWindow::SaveFileWindow(MWindow *mwindow, char *init_directory)
244  : BC_FileBox(mwindow->gui->get_abs_cursor_x(1),
245         mwindow->gui->get_abs_cursor_y(1) - BC_WindowBase::get_resources()->filebox_h / 2,
246         init_directory,
247         _(PROGRAM_NAME ": Save"),
248         _("Enter a filename to save as"))
249 {
250         this->mwindow = mwindow;
251 }
252
253 SaveFileWindow::~SaveFileWindow() {}
254