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