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
24 #include "confirmsave.h"
27 #include "mwindowgui.h"
30 ConfirmSave::ConfirmSave()
34 ConfirmSave::~ConfirmSave()
38 int ConfirmSave::get_save_path(MWindow *mwindow, char *filename)
41 char directory[BCTEXTLEN];
42 sprintf(directory, "~");
43 mwindow->defaults->get("DIRECTORY", directory);
45 int mx, my; mwindow->gui->get_abs_cursor(mx, my);
46 my -= BC_WindowBase::get_resources()->filebox_h / 2;
47 char string[BCTEXTLEN];
48 sprintf(string, _("Enter a filename to save as"));
49 BC_FileBox *filebox = new BC_FileBox(mx, my, directory,
50 _(PROGRAM_NAME ": Save"), string);
51 filebox->lock_window("ConfirmSave::get_save_path");
52 filebox->create_objects();
53 filebox->context_help_set_keyword("Saving Project Files");
54 filebox->unlock_window();
55 result = filebox->run_window();
56 mwindow->defaults->update("DIRECTORY", filebox->get_submitted_path());
57 strcpy(filename, filebox->get_submitted_path());
59 if( result == 1 ) return 1; // user cancelled
60 if( !filename[0] ) return 1; // no filename given
61 // Extend the filename with .xml
62 if( strlen(filename) < 4 ||
63 strcasecmp(&filename[strlen(filename) - 4], ".xml") ) {
64 strcat(filename, ".xml");
66 result = ConfirmSave::test_file(mwindow, filename);
71 int ConfirmSave::test_file(MWindow *mwindow, char *path)
73 ArrayList<char*> paths;
75 int result = test_files(mwindow, &paths);
80 int ConfirmSave::test_files(MWindow *mwindow, ArrayList<char*> *paths)
82 ArrayList<BC_ListBoxItem*> list;
85 for(int i = 0; i < paths->size(); i++) {
86 char *path = paths->values[i];
87 if( !access(path, F_OK) ) {
88 list.append(new BC_ListBoxItem(path));
94 ConfirmSaveWindow window(mwindow, &list);
95 window.create_objects();
96 window.raise_window();
97 result = window.run_window();
100 printf(_("The following files exist:\n"));
101 for(int i = 0; i < list.total; i++) {
102 printf(" %s\n", list.values[i]->get_text());
104 printf(_("Won't overwrite existing files.\n"));
107 list.remove_all_objects();
111 list.remove_all_objects();
119 #define CSW_W xS(400)
120 #define CSW_H yS(150)
122 ConfirmSaveWindow::ConfirmSaveWindow(MWindow *mwindow,
123 ArrayList<BC_ListBoxItem*> *list)
124 : BC_Window(_(PROGRAM_NAME ": File Exists"),
125 mwindow->gui->get_abs_cursor_x(1) - CSW_W/2,
126 mwindow->gui->get_abs_cursor_y(1) - CSW_H/2,
132 ConfirmSaveWindow::~ConfirmSaveWindow()
137 void ConfirmSaveWindow::create_objects()
140 int ys10 = yS(10), ys30 = yS(30);
141 int x = xs10, y = ys10;
142 lock_window("ConfirmSaveWindow::create_objects");
143 add_subwindow(new BC_OKButton(this));
144 add_subwindow(new BC_CancelButton(this));
146 add_subwindow(title = new BC_Title(x,
148 _("The following files exist. Overwrite them?")));
150 add_subwindow(listbox = new BC_ListBox(x, y,
152 get_h() - y - BC_OKButton::calculate_h() - ys10,
155 add_subwindow(new BC_OKButton(this));
156 add_subwindow(new BC_CancelButton(this));
161 int ConfirmSaveWindow::resize_event(int w, int h)
164 int ys10 = yS(10), ys30 = yS(30);
165 int x = xs10, y = ys10;
166 title->reposition_window(x, y);
168 listbox->reposition_window(x, y,
170 h - y - BC_OKButton::calculate_h() - ys10);