4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
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.
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.
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
22 #include "confirmsave.h"
24 #include "bcrecentlist.h"
28 #include "fileformat.h"
29 #include "indexfile.h"
31 #include "mainerror.h"
34 #include "mwindowgui.h"
35 #include "playback3d.h"
37 #include "mainsession.h"
49 SaveBackup::SaveBackup(MWindow *mwindow)
50 : BC_MenuItem(_("Save backup"), "b", 'b')
52 this->mwindow = mwindow;
54 int SaveBackup::handle_event()
56 mwindow->save_backup();
57 mwindow->gui->show_message(_("Saved backup."));
71 Save::Save(MWindow *mwindow) : BC_MenuItem(_("Save"), "s", 's')
73 this->mwindow = mwindow;
77 void Save::create_objects(SaveAs *saveas)
79 this->saveas = saveas;
82 int Save::handle_event()
84 if(mwindow->session->filename[0] == 0)
91 // TODO: Move this into mwindow.
93 mwindow->edl->save_xml(&file, mwindow->session->filename);
94 file.terminate_string();
96 if(file.write_to_file(mwindow->session->filename))
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();
110 char string[BCTEXTLEN];
111 sprintf(string, _("\"%s\" %dC written"),
112 mwindow->session->filename, (int)strlen(file.string()));
113 mwindow->gui->show_message(string);
115 mwindow->session->changes_made = 0;
116 // Last command in program
117 if( saveas->quit_now )
123 int Save::save_before_quit()
125 saveas->quit_now = 1;
130 SaveAs::SaveAs(MWindow *mwindow)
131 : BC_MenuItem(_("Save as..."), "Shift-S", 'S'), Thread()
134 this->mwindow = mwindow;
138 int SaveAs::set_mainmenu(MainMenu *mmenu)
144 int SaveAs::handle_event()
153 // ======================================= get path from user
155 //printf("SaveAs::run 1\n");
156 char directory[1024], filename[1024];
157 sprintf(directory, "~");
158 mwindow->defaults->get("DIRECTORY", directory);
160 // Loop if file exists
162 SaveFileWindow *window;
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());
173 // Extend the filename with .xml
174 if(strlen(filename) < 4 ||
175 strcasecmp(&filename[strlen(filename) - 4], ".xml"))
177 strcat(filename, ".xml");
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
186 //printf("SaveAs::run 6 %s\n", filename);
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();
200 if(file.write_to_file(filename))
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();
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();
223 mwindow->session->changes_made = 0;
224 mmenu->add_load(filename);
225 // Last command in program
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,
236 _(PROGRAM_NAME ": Save"),
237 _("Enter a filename to save as"))
239 this->mwindow = mwindow;
242 SaveFileWindow::~SaveFileWindow() {}
246 int SaveProjectModeItem::handle_event()
248 ((SaveProjectMode *)get_popup_menu())->update(id);
252 SaveProjectMode::SaveProjectMode(SaveProjectWindow *gui, int x, int y)
253 : BC_PopupMenu(x, y, 100, "")
256 save_modes[SAVE_PROJECT_COPY] = _("Copy");
257 save_modes[SAVE_PROJECT_SYMLINK] = _("SymLink");
258 save_modes[SAVE_PROJECT_RELLINK] = _("RelLink");
261 SaveProjectMode::~SaveProjectMode()
265 void SaveProjectMode::create_objects()
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]);
273 void SaveProjectMode::update(int mode)
275 if( gui->save_mode == mode ) return;
276 set_text(save_modes[gui->save_mode = mode]);
279 SaveProjectTextBox::SaveProjectTextBox(SaveProjectWindow *gui, int x, int y, int w)
280 : BC_TextBox(x, y, w, 1, gui->dir_path)
284 SaveProjectTextBox::~SaveProjectTextBox()
288 int SaveProjectTextBox::handle_event()
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)
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;
307 SaveProjectWindow::~SaveProjectWindow()
311 void SaveProjectWindow::create_objects()
313 int x = 20, y = 20, x1 = get_w()-80;
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));
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;
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));
339 SaveProject::SaveProject(MWindow *mwindow)
340 : BC_MenuItem(_("Export Project..."), "Alt-s", 's'), Thread()
343 this->mwindow = mwindow;
346 int SaveProject::handle_event()
352 void SaveProject::run()
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);
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();
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);