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