fix for vframe get_temp blunder, vicon zoom tweak
[goodguy/cinelerra.git] / cinelerra-5.1 / guicast / bcrename.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2010 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 "condition.h"
23 #include "bcfilebox.h"
24 #include "bcrename.h"
25 #include "bctitle.h"
26 #include "filesystem.h"
27 #include "language.h"
28 #include "mutex.h"
29 #include <string.h>
30
31 #include <sys/stat.h>
32
33
34
35
36
37
38
39 BC_Rename::BC_Rename(BC_RenameThread *thread, int x, int y, BC_FileBox *filebox)
40  : BC_Window(filebox->get_rename_title(),
41         x,
42         y,
43         320,
44         120,
45         0,
46         0,
47         0,
48         0,
49         1)
50 {
51         this->thread = thread;
52 }
53
54 BC_Rename::~BC_Rename()
55 {
56 }
57
58
59 void BC_Rename::create_objects()
60 {
61         int x = 10, y = 10;
62         lock_window("BC_Rename::create_objects");
63         add_tool(new BC_Title(x, y, _("Enter a new name for the file:")));
64         y += 20;
65         add_subwindow(textbox = new BC_TextBox(x, y, 300, 1, thread->orig_name));
66         y += 30;
67         add_subwindow(new BC_OKButton(this));
68         x = get_w() - 100;
69         add_subwindow(new BC_CancelButton(this));
70         show_window();
71         unlock_window();
72 }
73
74 const char* BC_Rename::get_text()
75 {
76         return textbox->get_text();
77 }
78
79
80 BC_RenameThread::BC_RenameThread(BC_FileBox *filebox)
81  : Thread(0, 0, 0)
82 {
83         this->filebox = filebox;
84         window = 0;
85         change_lock = new Mutex("BC_RenameThread::change_lock");
86         completion_lock = new Condition(1, "BC_RenameThread::completion_lock");
87 }
88
89 BC_RenameThread::~BC_RenameThread()
90 {
91         interrupt();
92         delete change_lock;
93         delete completion_lock;
94 }
95
96 void BC_RenameThread::run()
97 {
98         int x = filebox->get_abs_cursor_x(1);
99         int y = filebox->get_abs_cursor_y(1);
100         change_lock->lock("BC_RenameThread::run 1");
101         window = new BC_Rename(this,
102                 x,
103                 y,
104                 filebox);
105         window->create_objects();
106         change_lock->unlock();
107
108
109         int result = window->run_window();
110
111         if(!result)
112         {
113                 char old_name[BCTEXTLEN];
114                 char new_name[BCTEXTLEN];
115                 filebox->fs->join_names(old_name, orig_path, orig_name);
116                 filebox->fs->join_names(new_name, orig_path, window->get_text());
117
118 //printf("BC_RenameThread::run %d %s -> %s\n", __LINE__, old_name, new_name);
119                 rename(old_name, new_name);
120
121
122                 filebox->lock_window("BC_RenameThread::run");
123                 filebox->refresh();
124                 filebox->unlock_window();
125         }
126
127         change_lock->lock("BC_RenameThread::run 2");
128         delete window;
129         window = 0;
130         change_lock->unlock();
131
132         completion_lock->unlock();
133 }
134
135 int BC_RenameThread::interrupt()
136 {
137         change_lock->lock("BC_RenameThread::interrupt");
138         if(window)
139         {
140                 window->lock_window("BC_RenameThread::interrupt");
141                 window->set_done(1);
142                 window->unlock_window();
143         }
144
145         change_lock->unlock();
146
147         completion_lock->lock("BC_RenameThread::interrupt");
148         completion_lock->unlock();
149         return 0;
150 }
151
152 int BC_RenameThread::start_rename()
153 {
154         change_lock->lock();
155
156         if(window)
157         {
158                 window->lock_window("BC_RenameThread::start_rename");
159                 window->raise_window();
160                 window->unlock_window();
161                 change_lock->unlock();
162         }
163         else
164         {
165                 char string[BCTEXTLEN];
166                 FileSystem fs;
167                 strcpy(string, filebox->get_current_path());
168
169                 if(fs.is_dir(string))
170                 {
171 // Extract last directory from path to rename it
172                         strcpy(orig_path, string);
173                         char *ptr = orig_path + strlen(orig_path) - 1;
174 // Scan for end of path
175                         while(*ptr == '/' && ptr > orig_path) ptr--;
176 // Scan for previous separator
177                         while(*ptr != '/' && ptr > orig_path) ptr--;
178 // Get final path segment
179                         if(*ptr == '/') ptr++;
180                         strcpy(orig_name, ptr);
181 // Terminate original path
182                         *ptr = 0;
183                 }
184                 else
185                 {
186 // Extract filename from path to rename it
187                         fs.extract_dir(orig_path, string);
188                         fs.extract_name(orig_name, string);
189                 }
190
191
192 //printf("BC_RenameThread::start_rename %d %s %s %s\n", __LINE__, string, orig_path, orig_name);
193
194
195                 change_lock->unlock();
196                 completion_lock->lock("BC_RenameThread::start_rename");
197
198                 Thread::start();
199         }
200
201
202         return 0;
203 }
204
205