25 #define YUILogComponent "ncurses"
26 #include <yui/YUILog.h>
27 #include "NCFileSelection.h"
35 #include <sys/types.h>
44 struct stat64 * statInfo,
48 _mode = statInfo->st_mode;
49 _device = statInfo->st_dev;
50 _links = statInfo->st_nlink;
51 _size = statInfo->st_size;
52 _mtime = statInfo->st_mtime;
56 char tmpName[PATH_MAX+1];
58 int len = readlink( fileName.c_str(), tmpName, PATH_MAX );
68 else if ( S_ISREG( _mode )
69 && ( _mode & S_IXUSR ) )
76 struct passwd * pwdInfo = getpwuid( statInfo->st_uid );
79 _user = pwdInfo->pw_name;
81 struct group * groupInfo = getgrgid( statInfo->st_gid );
84 _group = groupInfo->gr_name;
86 if ( _mode & S_IRUSR )
91 if ( _mode & S_IWUSR )
96 if ( _mode & S_IXUSR )
101 if ( _mode & S_IRGRP )
106 if ( _mode & S_IWGRP )
111 if ( _mode & S_IXGRP )
116 if ( _mode & S_IROTH )
121 if ( _mode & S_IWOTH )
126 if ( _mode & S_IXOTH )
133 NCFileInfo::NCFileInfo( )
142 _device = ( dev_t )0;
143 _links = ( nlink_t )0;
144 _size = ( off64_t )0;
145 _mtime = ( time_t )0;
149 NCFileSelectionTag::NCFileSelectionTag(
NCFileInfo * info )
153 setLabel( fileInfo->_tag );
156 NCFileSelectionTag::~NCFileSelectionTag()
164 NCFileSelection::NCFileSelection( YWidget * parent,
165 YTableHeader * tableHeader,
166 NCFileSelectionType type,
167 const std::string & iniDir )
168 :
NCTable( parent, tableHeader )
170 , currentDir( iniDir )
176 struct stat64 statInfo;
178 if ( !iniDir.empty() )
180 stat64( iniDir.c_str(), &statInfo );
184 || !S_ISDIR( statInfo.st_mode ) )
186 char wDir[PATH_MAX+1];
190 if ( getcwd( wDir, PATH_MAX ) )
202 yuiDebug() << std::endl;
206 NCFileSelection::~NCFileSelection()
208 yuiDebug() << std::endl;
212 std::string NCFileSelection::getCurrentLine( )
214 int index = getCurrentItem();
228 void NCFileSelection::setCurrentDir()
230 std::string selected = getCurrentLine();
231 yuiMilestone() <<
"Current directory: " << selected << std::endl;
233 if ( selected !=
".." )
235 if ( startDir !=
"/" )
237 currentDir = startDir +
"/" + selected;
241 currentDir = startDir + selected;
248 if (( pos = currentDir.find_last_of(
"/" ) ) != 0 )
250 currentDir = currentDir.substr( 0, pos );
260 void NCFileSelection::addLine(
const std::vector<std::string> & elements,
263 YTableItem *tabItem =
new YTableItem();
267 for (
unsigned i = 1; i < elements.size()+1; ++i ) {
268 tabItem->addCell( elements[i-1] );
272 addItem(tabItem,
true);
278 return NCTable::deleteAllItems();
284 std::vector<std::string> data;
291 data.push_back( fileInfo->_name );
297 data.push_back( fileInfo->_name );
299 sprintf( size_buf,
"%lld", (
long long int ) fileInfo->_size );
300 data.push_back( size_buf );
301 data.push_back( fileInfo->_perm );
302 data.push_back( fileInfo->_user );
303 data.push_back( fileInfo->_group );
309 data.push_back(
" " );
310 data.push_back(
" " );
315 addLine( data, fileInfo );
323 std::vector<std::string> data;
330 data.push_back( fileInfo->_name );
336 data.push_back( fileInfo->_name );
337 data.push_back( fileInfo->_perm );
338 data.push_back( fileInfo->_user );
339 data.push_back( fileInfo->_group );
345 data.push_back(
" " );
346 data.push_back(
" " );
351 addLine( data, fileInfo );
365 return cc->getFileInfo();
378 YTableItem *it =
dynamic_cast<YTableItem*
> (cl->origItem() );
379 YTableCell *tcell = it->cell(0);
391 YTableHeader * tableHeader,
392 NCFileSelectionType type,
393 const std::string & filter,
394 const std::string & iniDir )
400 std::string filterStr = filter;
401 const std::string delims(
" \t" );
402 std::string::size_type begin, end;
404 begin = filterStr.find_first_not_of( delims );
406 while ( begin != std::string::npos )
408 end = filterStr.find_first_of( delims, begin );
410 if ( end == std::string::npos )
411 end = filterStr.length();
413 pattern.push_back( filterStr.substr( begin, end - begin ) );
414 begin = filterStr.find_first_not_of( delims, end );
424 std::vector<std::string> header;
425 std::string old_textdomain = textdomain(NULL);
426 setTextdomain(
"ncurses" );
433 header.push_back(
"L" + std::string(
" " ) );
435 header.push_back(
"L" + std::string( _(
"File name" ) ) );
442 header.push_back(
"L" + std::string(
" " ) );
444 header.push_back(
"L" + std::string( _(
"File name" ) ) );
446 header.push_back(
"L" + std::string( _(
"Size" ) ) );
448 header.push_back(
"L" + std::string( _(
"Permissions" ) ) );
450 header.push_back(
"L" + std::string( _(
"User" ) ) );
452 header.push_back(
"L" + std::string( _(
"Group" ) ) );
459 header.push_back(
"L" + std::string(
" " ) );
460 header.push_back(
"L" + std::string( _(
"File name" ) ) );
467 setTextdomain( old_textdomain.c_str() );
471 bool NCFileTable::filterMatch(
const std::string & fileEntry )
473 if ( pattern.empty() )
478 std::list<std::string>::iterator it = pattern.begin();
480 while ( it != pattern.end() )
482 if ( fnmatch(( *it ).c_str(), fileEntry.c_str(), FNM_PATHNAME ) == 0 )
492 NCursesEvent NCFileSelection::handleKeyEvents( wint_t key )
496 if ( sendKeyEvents() &&
497 ( key == KEY_LEFT || key == KEY_RIGHT ) )
499 ret = NCursesEvent::key;
504 ret.keySymbol =
"CursorLeft";
508 ret.keySymbol =
"CursorRight";
523 if ( ret == NCursesEvent::key )
529 currentFile = getCurrentLine();
540 ret = NCursesEvent::SelectionChanged;
541 ret.result = currentFile;
546 ret = NCursesEvent::none;
549 yuiDebug() <<
"CURRENT_FILE: " << currentFile << std::endl;
558 struct stat64 statInfo;
559 struct stat64 linkInfo;
560 struct dirent * entry;
561 std::list<std::string> tmpList;
562 std::list<std::string>::iterator it;
566 DIR * diskDir = opendir( currentDir.c_str() );
572 while (( entry = readdir( diskDir ) ) )
574 std::string entryName = entry->d_name;
576 if ( entryName !=
"."
577 && filterMatch( entryName ) )
579 tmpList.push_back( entryName );
585 it = tmpList.begin();
587 while ( it != tmpList.end() )
589 std::string fullName = currentDir +
"/" + ( *it );
591 if ( lstat64( fullName.c_str(), &statInfo ) == 0 )
593 if ( S_ISREG( statInfo.st_mode ) || S_ISBLK( statInfo.st_mode ) )
595 if ((( *it ) ==
".." && currentDir !=
"/" )
601 else if ( S_ISLNK( statInfo.st_mode ) )
603 if ( stat64( fullName.c_str(), &linkInfo ) == 0 )
605 if ( S_ISREG( linkInfo.st_mode ) || S_ISBLK( linkInfo.st_mode ) )
621 currentFile = getCurrentLine();
632 yuiError() <<
"ERROR opening directory: " << currentDir <<
" errno: "
633 << strerror( errno ) << std::endl;
641 NCDirectoryTable::NCDirectoryTable( YWidget * parent,
642 YTableHeader * tableHeader,
643 NCFileSelectionType type,
644 const std::string & iniDir )
656 std::vector<std::string> header;
657 std::string old_textdomain = textdomain(NULL);
658 setTextdomain(
"ncurses" );
665 header.push_back(
"L" + std::string(
" " ) );
667 header.push_back(
"L" + std::string( _(
"Directory Name" ) ) );
674 header.push_back(
"L" + std::string(
" " ) );
676 header.push_back(
"L" + std::string( _(
"Directory Name" ) ) );
677 header.push_back(
"L" + std::string( _(
"Permissions" ) ) );
678 header.push_back(
"L" + std::string( _(
"User" ) ) );
679 header.push_back(
"L" + std::string( _(
"Group" ) ) );
686 header.push_back(
"L" + std::string(
" " ) );
687 header.push_back(
"L" + std::string( _(
"Directory Name" ) ) );
694 setTextdomain( old_textdomain.c_str() );
700 struct stat64 statInfo;
701 struct stat64 linkInfo;
702 struct dirent * entry;
703 std::list<std::string> tmpList;
704 std::list<std::string>::iterator it;
708 DIR * diskDir = opendir( currentDir.c_str() );
714 while (( entry = readdir( diskDir ) ) )
716 std::string entryName = entry->d_name;
718 if ( entryName !=
"." )
720 tmpList.push_back( entryName );
727 it = tmpList.begin();
729 while ( it != tmpList.end() )
731 std::string fullName = currentDir +
"/" + ( *it );
733 if ( lstat64( fullName.c_str(), &statInfo ) == 0 )
735 if ( S_ISDIR( statInfo.st_mode ) )
737 if ((( *it ) ==
".." && currentDir !=
"/" )
743 else if ( S_ISLNK( statInfo.st_mode ) )
745 if ( stat64( fullName.c_str(), &linkInfo ) == 0 )
747 if ( S_ISDIR( linkInfo.st_mode ) )
759 startDir = currentDir;
768 yuiError() <<
"ERROR opening directory: " << currentDir <<
" errno: "
769 << strerror( errno ) << std::endl;
778 NCursesEvent NCDirectoryTable::wHandleInput( wint_t key )
784 if ( ret == NCursesEvent::key )
787 unsigned int old_pos = getCurrentItem();
801 ret = NCursesEvent::SelectionChanged;
802 ret.result = currentDir;
813 ret = NCursesEvent::SelectionChanged;
814 ret.result = currentDir;
822 ret = NCursesEvent::Activated;
823 ret.result = currentDir;
828 ret = NCursesEvent::none;
831 yuiDebug() <<
"CURRENT: " << currentDir <<
" START DIR: " << startDir << std::endl;
NCFileInfo * getFileInfo(int index)
virtual NCTablePad * myPad() const
virtual void fillHeader()
NCFileTable(YWidget *parent, YTableHeader *tableHeader, NCFileSelectionType type, const std::string &filter, const std::string &iniDir)
NCFileInfo(std::string fileName, struct stat64 *statInfo, bool link=false)
virtual bool createListEntry(NCFileInfo *fileInfo)
virtual void fillHeader()
virtual bool createListEntry(NCFileInfo *fileInfo)
virtual void deleteAllItems()
unsigned int getNumLines()