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