4 * Copyright (C) 2010 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
22 #include "condition.h"
23 #include "bcfilebox.h"
25 #include "bcresources.h"
27 #include "filesystem.h"
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)
39 this->thread = thread;
42 BC_Rename::~BC_Rename()
47 void BC_Rename::create_objects()
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:")));
53 add_subwindow(textbox = new BC_TextBox(x, y, xS(300), 1, thread->orig_name));
55 add_subwindow(new BC_OKButton(this));
56 x = get_w() - xS(100);
57 add_subwindow(new BC_CancelButton(this));
62 const char* BC_Rename::get_text()
64 return textbox->get_text();
68 BC_RenameThread::BC_RenameThread(BC_FileBox *filebox)
71 this->filebox = filebox;
73 change_lock = new Mutex("BC_RenameThread::change_lock");
74 completion_lock = new Condition(1, "BC_RenameThread::completion_lock");
77 BC_RenameThread::~BC_RenameThread()
81 delete completion_lock;
84 void BC_RenameThread::run()
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,
93 window->create_objects();
94 change_lock->unlock();
97 int result = window->run_window();
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());
106 //printf("BC_RenameThread::run %d %s -> %s\n", __LINE__, old_name, new_name);
107 rename(old_name, new_name);
110 filebox->lock_window("BC_RenameThread::run");
112 filebox->unlock_window();
115 change_lock->lock("BC_RenameThread::run 2");
118 change_lock->unlock();
120 completion_lock->unlock();
123 int BC_RenameThread::interrupt()
125 change_lock->lock("BC_RenameThread::interrupt");
128 window->lock_window("BC_RenameThread::interrupt");
130 window->unlock_window();
133 change_lock->unlock();
135 completion_lock->lock("BC_RenameThread::interrupt");
136 completion_lock->unlock();
140 int BC_RenameThread::start_rename()
146 window->lock_window("BC_RenameThread::start_rename");
147 window->raise_window();
148 window->unlock_window();
149 change_lock->unlock();
153 char string[BCTEXTLEN];
155 strcpy(string, filebox->get_current_path());
157 if(fs.is_dir(string))
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
174 // Extract filename from path to rename it
175 fs.extract_dir(orig_path, string);
176 fs.extract_name(orig_name, string);
180 //printf("BC_RenameThread::start_rename %d %s %s %s\n", __LINE__, string, orig_path, orig_name);
183 change_lock->unlock();
184 completion_lock->lock("BC_RenameThread::start_rename");