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 mwindow->gui->lock_window("Save::save_before_quit");
126 saveas->quit_now = 1;
128 mwindow->gui->unlock_window();
132 SaveAs::SaveAs(MWindow *mwindow)
133 : BC_MenuItem(_("Save as..."), "Shift-S", 'S'), Thread()
136 this->mwindow = mwindow;
140 int SaveAs::set_mainmenu(MainMenu *mmenu)
146 int SaveAs::handle_event()
155 // ======================================= get path from user
157 //printf("SaveAs::run 1\n");
158 char directory[1024], filename[1024];
159 sprintf(directory, "~");
160 mwindow->defaults->get("DIRECTORY", directory);
162 // Loop if file exists
164 SaveFileWindow *window;
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());
175 // Extend the filename with .xml
176 if(strlen(filename) < 4 ||
177 strcasecmp(&filename[strlen(filename) - 4], ".xml"))
179 strcat(filename, ".xml");
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
188 //printf("SaveAs::run 6 %s\n", filename);
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();
202 if(file.write_to_file(filename))
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();
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();
225 mwindow->session->changes_made = 0;
226 mmenu->add_load(filename);
227 // Last command in program
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,
238 _(PROGRAM_NAME ": Save"),
239 _("Enter a filename to save as"))
241 this->mwindow = mwindow;
244 SaveFileWindow::~SaveFileWindow() {}
248 int SaveProjectModeItem::handle_event()
250 ((SaveProjectMode *)get_popup_menu())->update(id);
254 SaveProjectMode::SaveProjectMode(SaveProjectWindow *gui, int x, int y)
255 : BC_PopupMenu(x, y, 100, "")
258 save_modes[SAVE_PROJECT_COPY] = _("Copy");
259 save_modes[SAVE_PROJECT_SYMLINK] = _("SymLink");
260 save_modes[SAVE_PROJECT_RELLINK] = _("RelLink");
263 SaveProjectMode::~SaveProjectMode()
267 void SaveProjectMode::create_objects()
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]);
275 void SaveProjectMode::update(int mode)
277 if( gui->save_mode == mode ) return;
278 set_text(save_modes[gui->save_mode = mode]);
281 SaveProjectTextBox::SaveProjectTextBox(SaveProjectWindow *gui, int x, int y, int w)
282 : BC_TextBox(x, y, w, 1, gui->dir_path)
286 SaveProjectTextBox::~SaveProjectTextBox()
290 int SaveProjectTextBox::handle_event()
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)
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;
309 SaveProjectWindow::~SaveProjectWindow()
313 void SaveProjectWindow::create_objects()
315 int x = 20, y = 20, x1 = get_w()-80;
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));
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;
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));
341 SaveProject::SaveProject(MWindow *mwindow)
342 : BC_MenuItem(_("Export Project..."), "Alt-s", 's'), Thread()
345 this->mwindow = mwindow;
348 int SaveProject::handle_event()
354 void SaveProject::run()
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);
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();
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);