r125: This commit was manufactured by cvs2svn to create tag 'r1_1_7-last'.
[cinelerra_cv/mob.git] / hvirtual / cinelerra / savefile.C
blob3cca96edf7e4ed756fc7307a367fde38d5030172
1 #include "confirmsave.h"
2 #include "defaults.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 "mainmenu.h"
10 #include "mwindow.h"
11 #include "mwindowgui.h"
12 #include "savefile.h"
13 #include "mainsession.h"
15 #include <string.h>
17 #include <libintl.h>
18 #define _(String) gettext(String)
19 #define gettext_noop(String) String
20 #define N_(String) gettext_noop (String)
30 SaveBackup::SaveBackup(MWindow *mwindow)
31  : BC_MenuItem(_("Save backup"))
33         this->mwindow = mwindow;
35 int SaveBackup::handle_event()
37         mwindow->save_backup();
38         mwindow->gui->show_message(_("Saved backup."));
39         return 1;
52 Save::Save(MWindow *mwindow) : BC_MenuItem(_("Save"), "s", 's')
53
54         this->mwindow = mwindow; 
55         quit_now = 0; 
58 int Save::create_objects(SaveAs *saveas)
60         this->saveas = saveas;
61         return 0;
64 int Save::handle_event()
66         if(mwindow->session->filename[0] == 0) 
67         {
68                 saveas->start();
69         }
70         else
71         {
72 // save it
73 // TODO: Move this into mwindow.
74                 FileXML file;
75                 mwindow->edl->save_xml(mwindow->plugindb, 
76                         &file, 
77                         mwindow->session->filename,
78                         0,
79                         0);
80                 file.terminate_string();
82                 if(file.write_to_file(mwindow->session->filename))
83                 {
84                         char string2[256];
85                         sprintf(string2, _("Couldn't open %s"), mwindow->session->filename);
86                         ErrorBox error(PROGRAM_NAME ": Error",
87                                 mwindow->gui->get_abs_cursor_x(),
88                                 mwindow->gui->get_abs_cursor_y());
89                         error.create_objects(string2);
90                         error.run_window();
91                         return 1;               
92                 }
93                 else
94                 {
95                         char string[BCTEXTLEN];
96                         sprintf(string, _("\"%s\" %dC written"), mwindow->session->filename, strlen(file.string));
97                         mwindow->gui->show_message(string, BLACK);
98                 }
99                 mwindow->session->changes_made = 0;
100                 if(saveas->quit_now) mwindow->gui->set_done(0);
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_path());
149                 strcpy(filename, window->get_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                 FILE *in;
163                 if(in = fopen(filename, "rb"))
164                 {
165                         fclose(in);
166                         ConfirmSaveWindow cwindow(mwindow, filename);
167                         cwindow.create_objects();
168                         int result2 = cwindow.run_window();
169                         if(result2) result = 1;
170                 }
171         }while(result);        // file exists so repeat
173 //printf("SaveAs::run 6 %s\n", filename);
178 // save it
179         FileXML file;
180         mwindow->set_filename(filename);      // update the project name
181         mwindow->edl->save_xml(mwindow->plugindb, 
182                 &file, 
183                 filename,
184                 0,
185                 0);
186         file.terminate_string();
188         if(file.write_to_file(filename))
189         {
190                 char string2[256];
191                 mwindow->set_filename("");      // update the project name
192                 sprintf(string2, _("Couldn't open %s."), filename);
193                 ErrorBox error(PROGRAM_NAME ": Error",
194                         mwindow->gui->get_abs_cursor_x(),
195                         mwindow->gui->get_abs_cursor_y());
196                 error.create_objects(string2);
197                 error.run_window();
198                 return;         
199         }
200         else
201         {
202                 char string[BCTEXTLEN];
203                 sprintf(string, _("\"%s\" %dC written"), filename, strlen(file.string));
204                 mwindow->gui->lock_window();
205                 mwindow->gui->show_message(string, BLACK);
206                 mwindow->gui->unlock_window();
207         }
210         mwindow->session->changes_made = 0;
211         mmenu->add_load(filename);
212         if(quit_now) mwindow->gui->set_done(0);
213         return;
223 SaveFileWindow::SaveFileWindow(MWindow *mwindow, char *init_directory)
224  : BC_FileBox(mwindow->gui->get_abs_cursor_x(),
225         mwindow->gui->get_abs_cursor_y() - BC_WindowBase::get_resources()->filebox_h / 2,
226         init_directory, 
227         PROGRAM_NAME ": Save", 
228         _("Enter a filename to save as"))
230         this->mwindow = mwindow; 
233 SaveFileWindow::~SaveFileWindow() {}