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"
26 #include "filesystem.h"
39 BC_Rename::BC_Rename(BC_RenameThread *thread, int x, int y, BC_FileBox *filebox)
40 : BC_Window(filebox->get_rename_title(),
51 this->thread = thread;
54 BC_Rename::~BC_Rename()
59 void BC_Rename::create_objects()
62 lock_window("BC_Rename::create_objects");
63 add_tool(new BC_Title(x, y, _("Enter a new name for the file:")));
65 add_subwindow(textbox = new BC_TextBox(x, y, 300, 1, thread->orig_name));
67 add_subwindow(new BC_OKButton(this));
69 add_subwindow(new BC_CancelButton(this));
74 const char* BC_Rename::get_text()
76 return textbox->get_text();
80 BC_RenameThread::BC_RenameThread(BC_FileBox *filebox)
83 this->filebox = filebox;
85 change_lock = new Mutex("BC_RenameThread::change_lock");
86 completion_lock = new Condition(1, "BC_RenameThread::completion_lock");
89 BC_RenameThread::~BC_RenameThread()
93 delete completion_lock;
96 void BC_RenameThread::run()
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,
105 window->create_objects();
106 change_lock->unlock();
109 int result = window->run_window();
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());
118 //printf("BC_RenameThread::run %d %s -> %s\n", __LINE__, old_name, new_name);
119 rename(old_name, new_name);
122 filebox->lock_window("BC_RenameThread::run");
124 filebox->unlock_window();
127 change_lock->lock("BC_RenameThread::run 2");
130 change_lock->unlock();
132 completion_lock->unlock();
135 int BC_RenameThread::interrupt()
137 change_lock->lock("BC_RenameThread::interrupt");
140 window->lock_window("BC_RenameThread::interrupt");
142 window->unlock_window();
145 change_lock->unlock();
147 completion_lock->lock("BC_RenameThread::interrupt");
148 completion_lock->unlock();
152 int BC_RenameThread::start_rename()
158 window->lock_window("BC_RenameThread::start_rename");
159 window->raise_window();
160 window->unlock_window();
161 change_lock->unlock();
165 char string[BCTEXTLEN];
167 strcpy(string, filebox->get_current_path());
169 if(fs.is_dir(string))
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
186 // Extract filename from path to rename it
187 fs.extract_dir(orig_path, string);
188 fs.extract_name(orig_name, string);
192 //printf("BC_RenameThread::start_rename %d %s %s %s\n", __LINE__, string, orig_path, orig_name);
195 change_lock->unlock();
196 completion_lock->lock("BC_RenameThread::start_rename");