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"
42 SaveBackup::SaveBackup(MWindow *mwindow)
43 : BC_MenuItem(_("Save backup"), "b", 'b')
45 this->mwindow = mwindow;
47 int SaveBackup::handle_event()
49 mwindow->save_backup();
50 mwindow->gui->show_message(_("Saved backup."));
55 Save::Save(MWindow *mwindow) : BC_MenuItem(_("Save"), "s", 's')
57 this->mwindow = mwindow;
61 void Save::create_objects(SaveAs *saveas)
63 this->saveas = saveas;
66 int Save::handle_event()
68 if(mwindow->session->filename[0] == 0)
75 // TODO: Move this into mwindow.
77 mwindow->edl->save_xml(&file, mwindow->session->filename);
78 file.terminate_string();
80 if(file.write_to_file(mwindow->session->filename))
83 sprintf(string2, _("Couldn't open %s"), mwindow->session->filename);
84 ErrorBox error(_(PROGRAM_NAME ": Error"),
85 mwindow->gui->get_abs_cursor_x(1),
86 mwindow->gui->get_abs_cursor_y(1));
87 error.create_objects(string2);
94 char string[BCTEXTLEN];
95 sprintf(string, _("\"%s\" %dC written"),
96 mwindow->session->filename, (int)strlen(file.string()));
97 mwindow->gui->show_message(string);
99 mwindow->session->changes_made = 0;
100 // Last command in program
101 if( saveas->quit_now )
107 int Save::save_before_quit()
109 mwindow->gui->lock_window("Save::save_before_quit");
110 saveas->quit_now = 1;
112 mwindow->gui->unlock_window();
116 SaveAs::SaveAs(MWindow *mwindow)
117 : BC_MenuItem(_("Save as..."), "Shift-S", 'S'), Thread()
120 this->mwindow = mwindow;
124 int SaveAs::set_mainmenu(MainMenu *mmenu)
130 int SaveAs::handle_event()
139 // ======================================= get path from user
141 //printf("SaveAs::run 1\n");
142 char directory[1024], filename[1024];
143 sprintf(directory, "~");
144 mwindow->defaults->get("DIRECTORY", directory);
146 // Loop if file exists
148 SaveFileWindow *window;
150 window = new SaveFileWindow(mwindow, directory);
151 window->lock_window("SaveAs::run");
152 window->create_objects();
153 window->unlock_window();
154 result = window->run_window();
155 mwindow->defaults->update("DIRECTORY", window->get_submitted_path());
156 strcpy(filename, window->get_submitted_path());
159 // Extend the filename with .xml
160 if(strlen(filename) < 4 ||
161 strcasecmp(&filename[strlen(filename) - 4], ".xml"))
163 strcat(filename, ".xml");
166 // ======================================= try to save it
167 if(filename[0] == 0) return; // no filename given
168 if(result == 1) return; // user cancelled
169 result = ConfirmSave::test_file(mwindow, filename);
170 }while(result); // file exists so repeat
172 //printf("SaveAs::run 6 %s\n", filename);
179 mwindow->gui->lock_window("SaveAs::run 1");
180 // update the project name
181 mwindow->set_filename(filename);
182 mwindow->edl->save_xml(&file, filename);
183 mwindow->gui->unlock_window();
184 file.terminate_string();
186 if(file.write_to_file(filename))
189 mwindow->set_filename(""); // update the project name
190 sprintf(string2, _("Couldn't open %s."), filename);
191 ErrorBox error(_(PROGRAM_NAME ": Error"),
192 mwindow->gui->get_abs_cursor_x(1),
193 mwindow->gui->get_abs_cursor_y(1));
194 error.create_objects(string2);
195 error.raise_window();
201 char string[BCTEXTLEN];
202 sprintf(string, _("\"%s\" %dC written"), filename, (int)strlen(file.string()));
203 mwindow->gui->lock_window("SaveAs::run 2");
204 mwindow->gui->show_message(string);
205 mwindow->gui->unlock_window();
209 mwindow->session->changes_made = 0;
210 mmenu->add_load(filename);
211 // Last command in program
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,
222 _(PROGRAM_NAME ": Save"),
223 _("Enter a filename to save as"))
225 this->mwindow = mwindow;
228 SaveFileWindow::~SaveFileWindow() {}
232 int SaveProjectModeItem::handle_event()
234 ((SaveProjectMode *)get_popup_menu())->update(id);
238 SaveProjectMode::SaveProjectMode(SaveProjectWindow *gui, int x, int y)
239 : BC_PopupMenu(x, y, xS(100), "")
242 save_modes[SAVE_PROJECT_COPY] = _("Copy");
243 save_modes[SAVE_PROJECT_SYMLINK] = _("SymLink");
244 save_modes[SAVE_PROJECT_RELLINK] = _("RelLink");
247 SaveProjectMode::~SaveProjectMode()
251 void SaveProjectMode::create_objects()
253 add_item(new SaveProjectModeItem(save_modes[SAVE_PROJECT_COPY], SAVE_PROJECT_COPY));
254 add_item(new SaveProjectModeItem(save_modes[SAVE_PROJECT_SYMLINK], SAVE_PROJECT_SYMLINK));
255 add_item(new SaveProjectModeItem(save_modes[SAVE_PROJECT_RELLINK], SAVE_PROJECT_RELLINK));
256 set_text(save_modes[gui->save_mode]);
259 void SaveProjectMode::update(int mode)
261 if( gui->save_mode == mode ) return;
262 set_text(save_modes[gui->save_mode = mode]);
265 SaveProjectTextBox::SaveProjectTextBox(SaveProjectWindow *gui, int x, int y, int w)
266 : BC_TextBox(x, y, w, 1, gui->dir_path)
270 SaveProjectTextBox::~SaveProjectTextBox()
274 int SaveProjectTextBox::handle_event()
279 SaveProjectWindow::SaveProjectWindow(MWindow *mwindow, const char *dir_path,
280 int save_mode, int overwrite, int reload)
281 : BC_Window(_(PROGRAM_NAME ": Export Project"),
282 mwindow->gui->get_abs_cursor_x(1),
283 mwindow->gui->get_abs_cursor_y(1) -
284 BC_WindowBase::get_resources()->filebox_h / 2,
285 xS(540), yS(220), xS(540), yS(220), 0)
287 this->mwindow = mwindow;
288 strcpy(this->dir_path, dir_path);
289 this->overwrite = overwrite;
290 this->save_mode = save_mode;
291 this->reload = reload;
293 SaveProjectWindow::~SaveProjectWindow()
297 void SaveProjectWindow::create_objects()
299 int xs10 = xS(10), xs20 = xS(20);
300 int ys10 = yS(10), ys20 = yS(20);
301 int x = xs20, y = ys20, x1 = get_w()-xS(80);
303 add_subwindow(title = new BC_Title(x, y, _("Project Directory:")));
304 y += title->get_h() + ys10;
305 add_subwindow(textbox = new SaveProjectTextBox(this, x, y, x1-x));
307 add_subwindow(recent_project = new BC_RecentList("RECENT_PROJECT",
308 mwindow->defaults, textbox, 10, x1, y, xS(300), yS(100)));
309 recent_project->load_items("RECENT_PROJECT");
310 x1 += recent_project->get_w() + xs10;
311 add_subwindow(browse_button = new BrowseButton(mwindow->theme, this,
312 textbox, x1, y-yS(5), "", "", "", 1));
313 y += textbox->get_h() + ys20;
314 add_subwindow(mode_popup = new SaveProjectMode(this, x, y));
315 mode_popup->create_objects();
316 y += mode_popup->get_h() + ys10;
318 BC_CheckBox *overwrite_files, *reload_project;
319 add_subwindow(overwrite_files = new BC_CheckBox(x1, y, &overwrite, _("Overwrite files")));
320 x1 += overwrite_files->get_w() + xs20;
321 add_subwindow(reload_project = new BC_CheckBox(x1, y, &reload, _("Reload project")));
322 add_subwindow(new BC_OKButton(this));
323 add_subwindow(new BC_CancelButton(this));
327 SaveProject::SaveProject(MWindow *mwindow)
328 : BC_MenuItem(_("Export Project..."), "Alt-s", 's'), Thread()
331 this->mwindow = mwindow;
334 int SaveProject::handle_event()
340 void SaveProject::run()
342 char dir_path[1024]; sprintf(dir_path, "~");
343 mwindow->defaults->get("PROJECT_DIRECTORY", dir_path);
344 int reload = mwindow->defaults->get("PROJECT_RELOAD", 0);
345 int overwrite = mwindow->defaults->get("PROJECT_OVERWRITE", 0);
346 int save_mode = mwindow->defaults->get("PROJECT_SAVE_MODE", 0);
348 SaveProjectWindow window(mwindow, dir_path, save_mode, overwrite, reload);
349 window.lock_window("SaveProject::run");
350 window.create_objects();
351 window.unlock_window();
352 int result = window.run_window();
355 strcpy(dir_path, window.textbox->get_text());
356 window.recent_project->add_item("RECENT_PROJECT", dir_path);
357 reload = window.get_reload();
358 overwrite = window.get_overwrite();
359 save_mode = window.get_save_mode();
360 mwindow->defaults->update("PROJECT_DIRECTORY", dir_path);
361 mwindow->defaults->update("PROJECT_RELOAD", reload);
362 mwindow->defaults->update("PROJECT_OVERWRITE", overwrite);
363 mwindow->defaults->update("PROJECT_SAVE_MODE", save_mode);
364 mwindow->save_project(dir_path, save_mode, overwrite, reload);