no longer need ffmpeg patch0 which was for Termux
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / confirmsave.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #include "asset.h"
23 #include "bchash.h"
24 #include "confirmsave.h"
25 #include "language.h"
26 #include "mwindow.h"
27 #include "mwindowgui.h"
28
29
30 ConfirmSave::ConfirmSave()
31 {
32 }
33
34 ConfirmSave::~ConfirmSave()
35 {
36 }
37
38 int ConfirmSave::get_save_path(MWindow *mwindow, char *filename)
39 {
40         int result = 1;
41         char directory[BCTEXTLEN];
42         sprintf(directory, "~");
43         mwindow->defaults->get("DIRECTORY", directory);
44         while( result ) {
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());
58                 delete filebox;
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");
65                 }
66                 result = ConfirmSave::test_file(mwindow, filename);
67         }
68         return result;
69 }
70
71 int ConfirmSave::test_file(MWindow *mwindow, char *path)
72 {
73         ArrayList<char*> paths;
74         paths.append(path);
75         int result = test_files(mwindow, &paths);
76         paths.remove_all();
77         return result;
78 }
79
80 int ConfirmSave::test_files(MWindow *mwindow, ArrayList<char*> *paths)
81 {
82         ArrayList<BC_ListBoxItem*> list;
83         int result = 0;
84
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));
89                 }
90         }
91
92         if(list.total) {
93                 if(mwindow) {
94                         ConfirmSaveWindow window(mwindow, &list);
95                         window.create_objects();
96                         window.raise_window();
97                         result = window.run_window();
98                 }
99                 else {
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());
103                         }
104                         printf(_("Won't overwrite existing files.\n"));
105                         result = 1;
106                 }
107                 list.remove_all_objects();
108                 return result;
109         }
110         else {
111                 list.remove_all_objects();
112                 return 0;
113         }
114
115         return result;
116 }
117
118
119 #define CSW_W xS(400)
120 #define CSW_H yS(150)
121
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,
127                 CSW_W, CSW_H)
128 {
129         this->list = list;
130 }
131
132 ConfirmSaveWindow::~ConfirmSaveWindow()
133 {
134 }
135
136
137 void ConfirmSaveWindow::create_objects()
138 {
139         int xs10 = xS(10);
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));
145
146         add_subwindow(title = new BC_Title(x,
147                 y,
148                 _("The following files exist.  Overwrite them?")));
149         y += ys30;
150         add_subwindow(listbox = new BC_ListBox(x, y,
151                 get_w() - x - xs10,
152                 get_h() - y - BC_OKButton::calculate_h() - ys10,
153                 LISTBOX_TEXT,
154                 list));
155         add_subwindow(new BC_OKButton(this));
156         add_subwindow(new BC_CancelButton(this));
157         show_window(1);
158         unlock_window();
159 }
160
161 int ConfirmSaveWindow::resize_event(int w, int h)
162 {
163         int xs10 = xS(10);
164         int ys10 = yS(10), ys30 = yS(30);
165         int x = xs10, y = ys10;
166         title->reposition_window(x, y);
167         y += ys30;
168         listbox->reposition_window(x, y,
169                 w - x - xs10,
170                 h - y - BC_OKButton::calculate_h() - ys10);
171         return 1;
172 }
173