00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <fcntl.h>
00022 #include <sys/types.h>
00023 #include <sys/stat.h>
00024 #include <errno.h>
00025 #include <string.h>
00026 #include <stdlib.h>
00027 #include <unistd.h>
00028 #include <dirent.h>
00029
00030 #ifndef HAVE_PATHCONF
00031 #if HAVE_LIMITS_H
00032 # include <limits.h>
00033 #elif HAVE_SYS_PARAM_H
00034 # include <sys/param.h>
00035 #endif
00036 #ifndef PATH_MAX
00037 # if defined(_POSIX_PATH_MAX)
00038 # define PATH_MAX _POSIX_PATH_MAX
00039 # elif defined(MAXPATHLEN)
00040 # define PATH_MAX MAXPATHLEN
00041 # else
00042 # define PATH_MAX 255
00043 # endif
00044 #endif
00045 #endif // HAVE_PATHCONF
00046
00047 #ifdef HAVE_ALGORITHM
00048 # include <algorithm>
00049 #endif
00050
00051 #include "../intl.h"
00052 #include <messagebox.h>
00053 #include <colors.h>
00054 #include "fileopen.h"
00055
00056 void
00057 FileOpen::createWindows() throw (YAPETUI::UIException) {
00058 if (window != NULL) return;
00059
00060 window = newwin (windowHeight(), windowWidth(), startY(), startX());
00061 if (window == NULL)
00062 throw YAPETUI::UIException (_("Error creating file open window"));
00063
00064 std::list<YAPETUI::secstring> dir_list;
00065 std::list<YAPETUI::secstring> file_list;
00066 getEntries(dir_list, file_list);
00067 dir = new YAPETUI::ListWidget<YAPETUI::secstring>(dir_list,
00068 startX()+1,
00069 startY()+2,
00070 windowWidth()/2-1,
00071 windowHeight()-6);
00072
00073 files = new YAPETUI::ListWidget<YAPETUI::secstring>(file_list,
00074 startX() + windowWidth()/2 ,
00075 startY() + 2,
00076 windowWidth()/2-1,
00077 windowHeight()-6);
00078
00079 input = new YAPETUI::InputWidget(startX()+1,
00080 startY()+windowHeight()-3,
00081 windowWidth()-2);
00082
00083 okbutton = new YAPETUI::Button(_("OK"),
00084 startX()+1,
00085 startY()+windowHeight()-2);
00086
00087 cancelbutton = new YAPETUI::Button(_("Cancel"),
00088 startX()+8,
00089 startY()+windowHeight()-2);
00090
00091 }
00092
00093 void
00094 FileOpen::getEntries(std::list<YAPETUI::secstring>& d,
00095 std::list<YAPETUI::secstring>& f)
00096 throw(YAPETUI::UIException) {
00097
00098 DIR* dir_ptr = opendir(directory.c_str());
00099 if (dir_ptr == NULL)
00100 throw YAPETUI::UIException(strerror(errno));
00101
00102 struct dirent* de;
00103 struct stat st;
00104
00105 while ( (de = readdir(dir_ptr)) != NULL) {
00106 int retval = stat( YAPETUI::secstring(directory +"/"+de->d_name).c_str(), &st);
00107 if (retval != 0) {
00108 continue;
00109 }
00110
00111 if (S_ISDIR(st.st_mode)) {
00112 d.push_back(de->d_name);
00113 } else if (S_ISREG(st.st_mode)) {
00114 std::string tmp(de->d_name);
00115 if (endswith(tmp, ".pet") )
00116 f.push_back(de->d_name);
00117 }
00118 }
00119 closedir(dir_ptr);
00120
00121 d.sort();
00122 f.sort();
00123 }
00124
00125 void
00126 FileOpen::printTitle() throw(YAPETUI::UIException) {
00127 int retval = mymvwaddstr(window, 0, 2, title.c_str());
00128 if (retval == ERR)
00129 throw YAPETUI::UIException(_("Error printing title"));
00130 }
00131
00132 void
00133 FileOpen::printCWD() throw(YAPETUI::UIException) {
00134 char format[50];
00135 snprintf(format, 50, "%%-%ds", (windowWidth()-2));
00136
00137 int retval = mvwprintw(window, 1, 1, format, " ");
00138 if (retval == ERR)
00139 throw YAPETUI::UIException(_("Error clearing line"));
00140
00141 retval = mymvwaddstr(window, 1, 1, directory.c_str());
00142 if (retval == ERR)
00143 throw YAPETUI::UIException(_("Error printing cwd"));
00144
00145 retval = wrefresh(window);
00146 if (retval == ERR)
00147 throw YAPETUI::UIException(_("Error refreshing cwd"));
00148 }
00149
00150 void
00151 FileOpen::getcwd() throw (YAPETUI::UIException) {
00152 long size = 0;
00153 #ifdef HAVE_PATHCONF
00154 size = pathconf (".", _PC_PATH_MAX);
00155 size = size < 1 ? FALLBACK_PATH_MAX : size;
00156 #else
00157 size = MAX_PATH;
00158 #endif
00159 char* buf = (char *) malloc ( (size_t) size);
00160 if (buf == NULL)
00161 throw YAPETUI::UIException (_("Error allocating memory"));
00162
00163 char* ptr = ::getcwd (buf, (size_t) size);
00164 if (ptr == NULL) {
00165 free (buf);
00166 throw YAPETUI::UIException (strerror (errno));
00167 }
00168
00169 directory = ptr;
00170 free (ptr);
00171 }
00172
00173 void
00174 FileOpen::cd (const YAPETUI::secstring d) throw (YAPETUI::UIException) {
00175 int retval = chdir (d.c_str());
00176 if (retval != 0)
00177 throw YAPETUI::UIException (strerror (errno));
00178
00179 getcwd();
00180 }
00181
00182 FileOpen::FileOpen(std::string t) throw(YAPETUI::UIException) : BaseWindow(),
00183 title(t),
00184 window (NULL),
00185 dir (NULL),
00186 files (NULL),
00187 input(NULL),
00188 okbutton(NULL),
00189 cancelbutton(NULL),
00190 canceled(true) {
00191 getcwd();
00192 createWindows();
00193
00194 }
00195
00196 FileOpen::~FileOpen() {
00197 wclear(window);
00198 delwin (window);
00199 delete dir;
00200 delete files;
00201 delete input;
00202 delete okbutton;
00203 delete cancelbutton;
00204 }
00205
00206 void
00207 FileOpen::run() throw (YAPETUI::UIException) {
00208 std::list<YAPETUI::secstring> file_list;
00209 std::list<YAPETUI::secstring> dir_list;
00210
00211 refresh();
00212
00213 int ch;
00214
00215 while(true){
00216
00217
00218 while ( (ch= dir->focus()) != '\t') {
00219 switch (ch) {
00220 #ifdef HAVE_WRESIZE
00221 case KEY_RESIZE:
00222 YAPETUI::BaseWindow::resizeAll();
00223 break;
00224 #endif // HAVE_WRESIZE
00225 case KEY_ENTER:
00226 case '\n': {
00227 try {
00228 file_list.clear();
00229 dir_list.clear();
00230 cd(dir->getSelectedItem());
00231 getEntries(dir_list, file_list);
00232 files->setList(file_list);
00233 dir->setList(dir_list);
00234 printCWD();
00235 } catch (YAPETUI::UIException& ex) {
00236 YAPETUI::MessageBox* tmp =
00237 new YAPETUI::MessageBox(_("E R R O R"), ex.what());
00238 tmp->run();
00239 delete tmp;
00240 this->refresh();
00241 }
00242 }
00243 break;
00244 }
00245 }
00246
00247
00248 while ( (ch = files->focus()) != '\t') {
00249 switch (ch) {
00250 #ifdef HAVE_WRESIZE
00251 case KEY_RESIZE:
00252 YAPETUI::BaseWindow::resizeAll();
00253 break;
00254 #endif // HAVE_WRESIZE
00255 case KEY_ENTER:
00256 case '\n':
00257 filename = files->getSelectedItem();
00258 input->setText(filename);
00259 break;
00260 }
00261 }
00262
00263 #ifdef HAVE_WRESIZE
00264 while ( (ch = input->focus()) == KEY_RESIZE)
00265 YAPETUI::BaseWindow::resizeAll();
00266 #else // HAVE_WRESIZE
00267 ch = input->focus();
00268 #endif // HAVE_WRESIZE
00269 filename = input->getText();
00270
00271
00272
00273 #ifdef HAVE_WRESIZE
00274 while ( (ch = okbutton->focus()) == KEY_RESIZE)
00275 YAPETUI::BaseWindow::resizeAll();
00276 #else // HAVE_WRESIZE
00277 ch = okbutton->focus();
00278 #endif // HAVE_WRESIZE
00279 if (ch == '\n' || ch == KEY_ENTER) {
00280 canceled = false;
00281 return;
00282 }
00283
00284
00285 #ifdef HAVE_WRESIZE
00286 while ( (ch = cancelbutton->focus()) == KEY_RESIZE)
00287 YAPETUI::BaseWindow::resizeAll();
00288 #else // HAVE_WRESIZE
00289 ch = cancelbutton->focus();
00290 #endif // HAVE_WRESIZE
00291 if (ch == '\n' || ch == KEY_ENTER) {
00292 canceled = true;
00293 return;
00294 }
00295 }
00296 }
00297
00298 void
00299 FileOpen::refresh() throw (YAPETUI::UIException) {
00300 YAPETUI::Colors::setcolor(window, YAPETUI::MESSAGEBOX);
00301
00302
00303
00304 int retval = box(window, 0, 0);
00305 if (retval == ERR)
00306 throw YAPETUI::UIException(_("Error drawing box"));
00307
00308 printTitle();
00309 printCWD();
00310
00311 retval = wrefresh (window);
00312 if (retval == ERR)
00313 throw YAPETUI::UIException (_("Error refreshing window"));
00314
00315 dir->refresh();
00316 files->refresh();
00317 input->refresh();
00318 okbutton->refresh();
00319 cancelbutton->refresh();
00320 }
00321
00322 void
00323 FileOpen::resize() throw (YAPETUI::UIException) {
00324 delete dir;
00325 delete files;
00326 delete input;
00327 delete okbutton;
00328 delete cancelbutton;
00329
00330 int retval = delwin(window);
00331 if (retval == ERR)
00332 throw YAPETUI::UIException(_("Error deleting window"));
00333
00334 window = NULL;
00335 dir = NULL;
00336 files = NULL;
00337 input = NULL;
00338 okbutton = NULL;
00339 cancelbutton = NULL;
00340
00341 createWindows();
00342 }
00343
00344 std::string
00345 FileOpen::getFilepath() {
00346 if (!endswith(filename, ".pet"))
00347 filename+=".pet";
00348
00349 std::string tmp_filename(filename.c_str());
00350 std::string tmp_directory(directory.c_str());
00351 if (tmp_directory == "/")
00352 return tmp_directory + tmp_filename;
00353
00354 return tmp_directory + "/" + tmp_filename;
00355 }