25 #define YUILogComponent "ncurses"
26 #include <yui/YUILog.h>
27 #include "NCFileSelection.h"
35 #include <sys/types.h>
49 struct stat64 * statInfo,
53 _mode = statInfo->st_mode;
54 _device = statInfo->st_dev;
55 _links = statInfo->st_nlink;
56 _size = statInfo->st_size;
57 _mtime = statInfo->st_mtime;
61 char tmpName[PATH_MAX+1];
63 int len = readlink( fileName.c_str(), tmpName, PATH_MAX );
73 else if ( S_ISREG( _mode )
74 && ( _mode & S_IXUSR ) )
81 struct passwd * pwdInfo = getpwuid( statInfo->st_uid );
84 _user = pwdInfo->pw_name;
86 struct group * groupInfo = getgrgid( statInfo->st_gid );
89 _group = groupInfo->gr_name;
91 if ( _mode & S_IRUSR )
96 if ( _mode & S_IWUSR )
101 if ( _mode & S_IXUSR )
106 if ( _mode & S_IRGRP )
111 if ( _mode & S_IWGRP )
116 if ( _mode & S_IXGRP )
121 if ( _mode & S_IROTH )
126 if ( _mode & S_IWOTH )
131 if ( _mode & S_IXOTH )
138 NCFileInfo::NCFileInfo()
147 _device = ( dev_t )0;
148 _links = ( nlink_t )0;
149 _size = ( off64_t )0;
150 _mtime = ( time_t )0;
154 NCFileSelectionTag::NCFileSelectionTag(
NCFileInfo * info )
158 setLabel( fileInfo->_tag );
161 NCFileSelectionTag::~NCFileSelectionTag()
169 NCFileSelection::NCFileSelection( YWidget * parent,
170 YTableHeader * tableHeader,
171 NCFileSelectionType type,
172 const string & iniDir )
173 :
NCTable( parent, tableHeader )
175 , currentDir( iniDir )
180 struct stat64 statInfo;
182 if ( !iniDir.empty() )
184 stat64( iniDir.c_str(), &statInfo );
188 || !S_ISDIR( statInfo.st_mode ) )
190 char wDir[PATH_MAX+1];
194 if ( getcwd( wDir, PATH_MAX ) )
210 NCFileSelection::~NCFileSelection()
216 string NCFileSelection::getCurrentLine()
232 void NCFileSelection::setCurrentDir()
234 string selected = getCurrentLine();
235 yuiDebug() <<
"Current directory: " << selected << endl;
237 if ( selected !=
".." )
239 if ( startDir !=
"/" )
241 currentDir = startDir +
"/" + selected;
245 currentDir = startDir + selected;
252 if (( pos = currentDir.find_last_of(
"/" ) ) != 0 )
254 currentDir = currentDir.substr( 0, pos );
264 void NCFileSelection::addLine(
const vector<string> & elements,
267 YTableItem *tabItem =
new YTableItem();
271 for (
unsigned i = 1; i < elements.size()+1; ++i ) {
272 tabItem->addCell( elements[i-1] );
295 data.push_back( fileInfo->_name );
301 data.push_back( fileInfo->_name );
303 sprintf( size_buf,
"%lld", (
long long int ) fileInfo->_size );
304 data.push_back( size_buf );
305 data.push_back( fileInfo->_perm );
306 data.push_back( fileInfo->_user );
307 data.push_back( fileInfo->_group );
313 data.push_back(
" " );
314 data.push_back(
" " );
319 addLine( data, fileInfo );
334 data.push_back( fileInfo->_name );
340 data.push_back( fileInfo->_name );
341 data.push_back( fileInfo->_perm );
342 data.push_back( fileInfo->_user );
343 data.push_back( fileInfo->_group );
349 data.push_back(
" " );
350 data.push_back(
" " );
355 addLine( data, fileInfo );
369 return cc->getFileInfo();
382 YTableItem *it =
dynamic_cast<YTableItem*
> (cl->
origItem() );
383 YTableCell *tcell = it->cell(0);
395 YTableHeader * tableHeader,
396 NCFileSelectionType type,
397 const string & filter,
398 const string & iniDir )
404 string filterStr = filter;
405 const string delims(
" \t" );
406 string::size_type begin, end;
408 begin = filterStr.find_first_not_of( delims );
410 while ( begin != string::npos )
412 end = filterStr.find_first_of( delims, begin );
414 if ( end == string::npos )
415 end = filterStr.length();
417 pattern.push_back( filterStr.substr( begin, end - begin ) );
418 begin = filterStr.find_first_not_of( delims, end );
428 vector<string> header;
429 string old_textdomain = textdomain(NULL);
430 setTextdomain(
"ncurses" );
437 header.push_back(
string(
" " ) );
439 header.push_back(
string( _(
"File name" ) ) );
446 header.push_back(
string(
" " ) );
448 header.push_back(
string( _(
"File name" ) ) );
450 header.push_back(
string( _(
"Size" ) ) );
452 header.push_back(
string( _(
"Permissions" ) ) );
454 header.push_back(
string( _(
"User" ) ) );
456 header.push_back(
string( _(
"Group" ) ) );
463 header.push_back(
string(
" " ) );
464 header.push_back(
string( _(
"File name" ) ) );
471 setTextdomain( old_textdomain.c_str() );
475 bool NCFileTable::filterMatch(
const string & fileEntry )
477 if ( pattern.empty() )
482 list<string>::iterator it = pattern.begin();
484 while ( it != pattern.end() )
486 if ( fnmatch(( *it ).c_str(), fileEntry.c_str(), FNM_PATHNAME ) == 0 )
496 NCursesEvent NCFileSelection::handleKeyEvents( wint_t key )
500 if ( sendKeyEvents() &&
501 ( key == KEY_LEFT || key == KEY_RIGHT ) )
503 ret = NCursesEvent::key;
508 ret.keySymbol =
"CursorLeft";
512 ret.keySymbol =
"CursorRight";
527 if ( ret == NCursesEvent::key )
533 currentFile = getCurrentLine();
544 ret = NCursesEvent::SelectionChanged;
545 ret.result = currentFile;
550 ret = NCursesEvent::none;
562 struct stat64 statInfo;
563 struct stat64 linkInfo;
564 struct dirent * entry;
565 list<string> tmpList;
566 list<string>::iterator it;
570 DIR * diskDir = opendir( currentDir.c_str() );
576 while (( entry = readdir( diskDir ) ) )
578 string entryName = entry->d_name;
580 if ( entryName !=
"."
581 && filterMatch( entryName ) )
583 tmpList.push_back( entryName );
589 it = tmpList.begin();
591 while ( it != tmpList.end() )
593 string fullName = currentDir +
"/" + ( *it );
595 if ( lstat64( fullName.c_str(), &statInfo ) == 0 )
597 if ( S_ISREG( statInfo.st_mode ) || S_ISBLK( statInfo.st_mode ) )
599 if ((( *it ) ==
".." && currentDir !=
"/" )
605 else if ( S_ISLNK( statInfo.st_mode ) )
607 if ( stat64( fullName.c_str(), &linkInfo ) == 0 )
609 if ( S_ISREG( linkInfo.st_mode ) || S_ISBLK( linkInfo.st_mode ) )
625 currentFile = getCurrentLine();
636 yuiError() <<
"ERROR opening directory: " << currentDir <<
" errno: "
637 << strerror( errno ) << endl;
645 NCDirectoryTable::NCDirectoryTable( YWidget * parent,
646 YTableHeader * tableHeader,
647 NCFileSelectionType type,
648 const string & iniDir )
660 vector<string> header;
661 string old_textdomain = textdomain(NULL);
662 setTextdomain(
"ncurses" );
669 header.push_back(
string(
" " ) );
671 header.push_back(
string( _(
"Directory Name" ) ) );
678 header.push_back(
string(
" " ) );
680 header.push_back(
string( _(
"Directory Name" ) ) );
681 header.push_back(
string( _(
"Permissions" ) ) );
682 header.push_back(
string( _(
"User" ) ) );
683 header.push_back(
string( _(
"Group" ) ) );
690 header.push_back(
string(
" " ) );
691 header.push_back(
string( _(
"Directory Name" ) ) );
698 setTextdomain( old_textdomain.c_str() );
704 struct stat64 statInfo;
705 struct stat64 linkInfo;
706 struct dirent * entry;
707 list<string> tmpList;
708 list<string>::iterator it;
712 DIR * diskDir = opendir( currentDir.c_str() );
718 while (( entry = readdir( diskDir ) ) )
720 string entryName = entry->d_name;
722 if ( entryName !=
"." )
724 tmpList.push_back( entryName );
731 it = tmpList.begin();
733 while ( it != tmpList.end() )
735 string fullName = currentDir +
"/" + ( *it );
737 if ( lstat64( fullName.c_str(), &statInfo ) == 0 )
739 if ( S_ISDIR( statInfo.st_mode ) )
741 if ((( *it ) ==
".." && currentDir !=
"/" )
747 else if ( S_ISLNK( statInfo.st_mode ) )
749 if ( stat64( fullName.c_str(), &linkInfo ) == 0 )
751 if ( S_ISDIR( linkInfo.st_mode ) )
763 startDir = currentDir;
772 yuiError() <<
"ERROR opening directory: " << currentDir <<
" errno: "
773 << strerror( errno ) << endl;
788 if ( ret == NCursesEvent::key )
805 ret = NCursesEvent::SelectionChanged;
806 ret.result = currentDir;
817 ret = NCursesEvent::SelectionChanged;
818 ret.result = currentDir;
826 ret = NCursesEvent::Activated;
827 ret.result = currentDir;
832 ret = NCursesEvent::none;