r863: Merge 2.1:
[cinelerra_cv.git] / cinelerra / savefile.C
blob8a9453ce878da5a979ab43f02a560a593c9a05e6
1 #include "confirmsave.h"
2 #include "bchash.h"
3 #include "edl.h"
4 #include "errorbox.h"
5 #include "file.h"
6 #include "filexml.h"
7 #include "fileformat.h"
8 #include "indexfile.h"
9 #include "language.h"
10 #include "mainmenu.h"
11 #include "mwindow.h"
12 #include "mwindowgui.h"
13 #include "playback3d.h"
14 #include "savefile.h"
15 #include "mainsession.h"
17 #include <string.h>
27 SaveBackup::SaveBackup(MWindow *mwindow)
28  : BC_MenuItem(_("Save backup"))
30         this->mwindow = mwindow;
32 int SaveBackup::handle_event()
34         mwindow->save_backup();
35         mwindow->gui->show_message(_("Saved backup."));
36         return 1;
49 Save::Save(MWindow *mwindow) : BC_MenuItem(_("Save"), "s", 's')
50
51         this->mwindow = mwindow; 
52         quit_now = 0; 
55 int Save::create_objects(SaveAs *saveas)
57         this->saveas = saveas;
58         return 0;
61 int Save::handle_event()
63         if(mwindow->session->filename[0] == 0) 
64         {
65                 saveas->start();
66         }
67         else
68         {
69 // save it
70 // TODO: Move this into mwindow.
71                 FileXML file;
72                 mwindow->edl->save_xml(mwindow->plugindb, 
73                         &file, 
74                         mwindow->session->filename,
75                         0,
76                         0);
77                 file.terminate_string();
79                 if(file.write_to_file(mwindow->session->filename))
80                 {
81                         char string2[256];
82                         sprintf(string2, _("Couldn't open %s"), mwindow->session->filename);
83                         ErrorBox error(PROGRAM_NAME ": Error",
84                                 mwindow->gui->get_abs_cursor_x(1),
85                                 mwindow->gui->get_abs_cursor_y(1));
86                         error.create_objects(string2);
87                         error.raise_window();
88                         error.run_window();
89                         return 1;               
90                 }
91                 else
92                 {
93                         char string[BCTEXTLEN];
94                         sprintf(string, _("\"%s\" %dC written"), mwindow->session->filename, strlen(file.string));
95                         mwindow->gui->show_message(string);
96                 }
97                 mwindow->session->changes_made = 0;
98 // Last command in program
99 //              if(saveas->quit_now) mwindow->gui->set_done(0);
100                 if(saveas->quit_now) mwindow->playback_3d->quit();
101         }
102         return 1;
105 int Save::save_before_quit()
107         saveas->quit_now = 1;
108         handle_event();
109         return 0;
112 SaveAs::SaveAs(MWindow *mwindow)
113  : BC_MenuItem(_("Save as..."), ""), Thread()
115         this->mwindow = mwindow; 
116         quit_now = 0;
119 int SaveAs::set_mainmenu(MainMenu *mmenu)
121         this->mmenu = mmenu;
122         return 0;
125 int SaveAs::handle_event() 
127         quit_now = 0;
128         start();
129         return 1;
132 void SaveAs::run()
134 // ======================================= get path from user
135         int result;
136 //printf("SaveAs::run 1\n");
137         char directory[1024], filename[1024];
138         sprintf(directory, "~");
139         mwindow->defaults->get("DIRECTORY", directory);
141 // Loop if file exists
142         do{
143                 SaveFileWindow *window;
145                 window = new SaveFileWindow(mwindow, directory);
146                 window->create_objects();
147                 result = window->run_window();
148                 mwindow->defaults->update("DIRECTORY", window->get_submitted_path());
149                 strcpy(filename, window->get_submitted_path());
150                 delete window;
152 // Extend the filename with .xml
153                 if(strlen(filename) < 4 || 
154                         strcasecmp(&filename[strlen(filename) - 4], ".xml"))
155                 {
156                         strcat(filename, ".xml");
157                 }
159 // ======================================= try to save it
160                 if(filename[0] == 0) return;              // no filename given
161                 if(result == 1) return;          // user cancelled
162                 result = ConfirmSave::test_file(mwindow, filename);
163         }while(result);        // file exists so repeat
165 //printf("SaveAs::run 6 %s\n", filename);
170 // save it
171         FileXML file;
172         mwindow->set_filename(filename);      // update the project name
173         mwindow->edl->save_xml(mwindow->plugindb, 
174                 &file, 
175                 filename,
176                 0,
177                 0);
178         file.terminate_string();
180         if(file.write_to_file(filename))
181         {
182                 char string2[256];
183                 mwindow->set_filename("");      // update the project name
184                 sprintf(string2, _("Couldn't open %s."), filename);
185                 ErrorBox error(PROGRAM_NAME ": Error",
186                         mwindow->gui->get_abs_cursor_x(1),
187                         mwindow->gui->get_abs_cursor_y(1));
188                 error.create_objects(string2);
189                 error.raise_window();
190                 error.run_window();
191                 return;         
192         }
193         else
194         {
195                 char string[BCTEXTLEN];
196                 sprintf(string, _("\"%s\" %dC written"), filename, strlen(file.string));
197                 mwindow->gui->lock_window();
198                 mwindow->gui->show_message(string);
199                 mwindow->gui->unlock_window();
200         }
203         mwindow->session->changes_made = 0;
204         mmenu->add_load(filename);
205 // Last command in program
206 //      if(quit_now) mwindow->gui->set_done(0);
207         if(quit_now) mwindow->playback_3d->quit();
208         return;
218 SaveFileWindow::SaveFileWindow(MWindow *mwindow, char *init_directory)
219  : BC_FileBox(mwindow->gui->get_abs_cursor_x(1),
220         mwindow->gui->get_abs_cursor_y(1) - BC_WindowBase::get_resources()->filebox_h / 2,
221         init_directory, 
222         PROGRAM_NAME ": Save", 
223         _("Enter a filename to save as"))
225         this->mwindow = mwindow; 
228 SaveFileWindow::~SaveFileWindow() {}