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->unlock_window();
54 result = filebox->run_window();
55 mwindow->defaults->update("DIRECTORY", filebox->get_submitted_path());
56 strcpy(filename, filebox->get_submitted_path());
58 if( result == 1 ) return 1; // user cancelled
59 if( !filename[0] ) return 1; // no filename given
60 // Extend the filename with .xml
61 if( strlen(filename) < 4 ||
62 strcasecmp(&filename[strlen(filename) - 4], ".xml") ) {
63 strcat(filename, ".xml");
65 result = ConfirmSave::test_file(mwindow, filename);
70 int ConfirmSave::test_file(MWindow *mwindow, char *path)
72 ArrayList<char*> paths;
74 int result = test_files(mwindow, &paths);
79 int ConfirmSave::test_files(MWindow *mwindow, ArrayList<char*> *paths)
81 ArrayList<BC_ListBoxItem*> list;
84 for(int i = 0; i < paths->size(); i++) {
85 char *path = paths->values[i];
86 if( !access(path, F_OK) ) {
87 list.append(new BC_ListBoxItem(path));
93 ConfirmSaveWindow window(mwindow, &list);
94 window.create_objects();
95 window.raise_window();
96 result = window.run_window();
99 printf(_("The following files exist:\n"));
100 for(int i = 0; i < list.total; i++) {
101 printf(" %s\n", list.values[i]->get_text());
103 printf(_("Won't overwrite existing files.\n"));
106 list.remove_all_objects();
110 list.remove_all_objects();
118 #define CSW_W xS(400)
119 #define CSW_H yS(150)
121 ConfirmSaveWindow::ConfirmSaveWindow(MWindow *mwindow,
122 ArrayList<BC_ListBoxItem*> *list)
123 : BC_Window(_(PROGRAM_NAME ": File Exists"),
124 mwindow->gui->get_abs_cursor_x(1) - CSW_W/2,
125 mwindow->gui->get_abs_cursor_y(1) - CSW_H/2,
131 ConfirmSaveWindow::~ConfirmSaveWindow()
136 void ConfirmSaveWindow::create_objects()
139 int ys10 = yS(10), ys30 = yS(30);
140 int x = xs10, y = ys10;
141 lock_window("ConfirmSaveWindow::create_objects");
142 add_subwindow(new BC_OKButton(this));
143 add_subwindow(new BC_CancelButton(this));
145 add_subwindow(title = new BC_Title(x,
147 _("The following files exist. Overwrite them?")));
149 add_subwindow(listbox = new BC_ListBox(x, y,
151 get_h() - y - BC_OKButton::calculate_h() - ys10,
154 add_subwindow(new BC_OKButton(this));
155 add_subwindow(new BC_CancelButton(this));
160 int ConfirmSaveWindow::resize_event(int w, int h)
163 int ys10 = yS(10), ys30 = yS(30);
164 int x = xs10, y = ys10;
165 title->reposition_window(x, y);
167 listbox->reposition_window(x, y,
169 h - y - BC_OKButton::calculate_h() - ys10);