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 )
175 struct stat64 statInfo;
177 if ( !iniDir.empty() )
179 stat64( iniDir.c_str(), &statInfo );
183 || !S_ISDIR( statInfo.st_mode ) )
185 char wDir[PATH_MAX+1];
189 if ( getcwd( wDir, PATH_MAX ) )
201 yuiDebug() << std::endl;
205 NCFileSelection::~NCFileSelection()
207 yuiDebug() << std::endl;
211 std::string NCFileSelection::getCurrentLine( )
213 int index = getCurrentItem();
227 void NCFileSelection::setCurrentDir()
229 std::string selected = getCurrentLine();
230 yuiMilestone() <<
"Current directory: " << selected << std::endl;
232 if ( selected !=
".." )
234 if ( startDir !=
"/" )
236 currentDir = startDir +
"/" + selected;
240 currentDir = startDir + selected;
247 if (( pos = currentDir.find_last_of(
"/" ) ) != 0 )
249 currentDir = currentDir.substr( 0, pos );
259 void NCFileSelection::addLine(
const std::vector<std::string> & elements,
262 YTableItem *tabItem =
new YTableItem();
266 for (
unsigned i = 1; i < elements.size()+1; ++i ) {
267 tabItem->addCell( elements[i-1] );
271 addItem(tabItem,
true);
277 return NCTable::deleteAllItems();
283 std::vector<std::string> data;
290 data.push_back( fileInfo->_name );
296 data.push_back( fileInfo->_name );
298 sprintf( size_buf,
"%lld", (
long long int ) fileInfo->_size );
299 data.push_back( size_buf );
300 data.push_back( fileInfo->_perm );
301 data.push_back( fileInfo->_user );
302 data.push_back( fileInfo->_group );
308 data.push_back(
" " );
309 data.push_back(
" " );
314 addLine( data, fileInfo );
322 std::vector<std::string> data;
329 data.push_back( fileInfo->_name );
335 data.push_back( fileInfo->_name );
336 data.push_back( fileInfo->_perm );
337 data.push_back( fileInfo->_user );
338 data.push_back( fileInfo->_group );
344 data.push_back(
" " );
345 data.push_back(
" " );
350 addLine( data, fileInfo );
364 return cc->getFileInfo();
377 YTableItem *it =
dynamic_cast<YTableItem*
> (cl->origItem() );
378 YTableCell *tcell = it->cell(0);
390 YTableHeader * tableHeader,
391 NCFileSelectionType type,
392 const std::string & filter,
393 const std::string & iniDir )
399 std::string filterStr = filter;
400 const std::string delims(
" \t" );
401 std::string::size_type begin, end;
403 begin = filterStr.find_first_not_of( delims );
405 while ( begin != std::string::npos )
407 end = filterStr.find_first_of( delims, begin );
409 if ( end == std::string::npos )
410 end = filterStr.length();
412 pattern.push_back( filterStr.substr( begin, end - begin ) );
413 begin = filterStr.find_first_not_of( delims, end );
423 std::vector<std::string> header;
424 std::string old_textdomain = textdomain(NULL);
425 setTextdomain(
"ncurses" );
432 header.push_back(
"L" + std::string(
" " ) );
434 header.push_back(
"L" + std::string( _(
"File name" ) ) );
441 header.push_back(
"L" + std::string(
" " ) );
443 header.push_back(
"L" + std::string( _(
"File name" ) ) );
445 header.push_back(
"L" + std::string( _(
"Size" ) ) );
447 header.push_back(
"L" + std::string( _(
"Permissions" ) ) );
449 header.push_back(
"L" + std::string( _(
"User" ) ) );
451 header.push_back(
"L" + std::string( _(
"Group" ) ) );
458 header.push_back(
"L" + std::string(
" " ) );
459 header.push_back(
"L" + std::string( _(
"File name" ) ) );
466 setTextdomain( old_textdomain.c_str() );
470 bool NCFileTable::filterMatch(
const std::string & fileEntry )
472 if ( pattern.empty() )
477 std::list<std::string>::iterator it = pattern.begin();
479 while ( it != pattern.end() )
481 if ( fnmatch(( *it ).c_str(), fileEntry.c_str(), FNM_PATHNAME ) == 0 )
491 NCursesEvent NCFileSelection::handleKeyEvents( wint_t key )
495 if ( sendKeyEvents() &&
496 ( key == KEY_LEFT || key == KEY_RIGHT ) )
498 ret = NCursesEvent::key;
503 ret.keySymbol =
"CursorLeft";
507 ret.keySymbol =
"CursorRight";
522 if ( ret == NCursesEvent::key )
528 currentFile = getCurrentLine();
539 ret = NCursesEvent::SelectionChanged;
540 ret.result = currentFile;
545 ret = NCursesEvent::none;
548 yuiDebug() <<
"CURRENT_FILE: " << currentFile << std::endl;
557 struct stat64 statInfo;
558 struct stat64 linkInfo;
559 struct dirent * entry;
560 std::list<std::string> tmpList;
561 std::list<std::string>::iterator it;
565 DIR * diskDir = opendir( currentDir.c_str() );
571 while (( entry = readdir( diskDir ) ) )
573 std::string entryName = entry->d_name;
575 if ( entryName !=
"."
576 && filterMatch( entryName ) )
578 tmpList.push_back( entryName );
584 it = tmpList.begin();
586 while ( it != tmpList.end() )
588 std::string fullName = currentDir +
"/" + ( *it );
590 if ( lstat64( fullName.c_str(), &statInfo ) == 0 )
592 if ( S_ISREG( statInfo.st_mode ) || S_ISBLK( statInfo.st_mode ) )
594 if ((( *it ) ==
".." && currentDir !=
"/" )
600 else if ( S_ISLNK( statInfo.st_mode ) )
602 if ( stat64( fullName.c_str(), &linkInfo ) == 0 )
604 if ( S_ISREG( linkInfo.st_mode ) || S_ISBLK( linkInfo.st_mode ) )
620 currentFile = getCurrentLine();
631 yuiError() <<
"ERROR opening directory: " << currentDir <<
" errno: "
632 << strerror( errno ) << std::endl;
640 NCDirectoryTable::NCDirectoryTable( YWidget * parent,
641 YTableHeader * tableHeader,
642 NCFileSelectionType type,
643 const std::string & iniDir )
655 std::vector<std::string> header;
656 std::string old_textdomain = textdomain(NULL);
657 setTextdomain(
"ncurses" );
664 header.push_back(
"L" + std::string(
" " ) );
666 header.push_back(
"L" + std::string( _(
"Directory Name" ) ) );
673 header.push_back(
"L" + std::string(
" " ) );
675 header.push_back(
"L" + std::string( _(
"Directory Name" ) ) );
676 header.push_back(
"L" + std::string( _(
"Permissions" ) ) );
677 header.push_back(
"L" + std::string( _(
"User" ) ) );
678 header.push_back(
"L" + std::string( _(
"Group" ) ) );
685 header.push_back(
"L" + std::string(
" " ) );
686 header.push_back(
"L" + std::string( _(
"Directory Name" ) ) );
693 setTextdomain( old_textdomain.c_str() );
699 struct stat64 statInfo;
700 struct stat64 linkInfo;
701 struct dirent * entry;
702 std::list<std::string> tmpList;
703 std::list<std::string>::iterator it;
707 DIR * diskDir = opendir( currentDir.c_str() );
713 while (( entry = readdir( diskDir ) ) )
715 std::string entryName = entry->d_name;
717 if ( entryName !=
"." )
719 tmpList.push_back( entryName );
726 it = tmpList.begin();
728 while ( it != tmpList.end() )
730 std::string fullName = currentDir +
"/" + ( *it );
732 if ( lstat64( fullName.c_str(), &statInfo ) == 0 )
734 if ( S_ISDIR( statInfo.st_mode ) )
736 if ((( *it ) ==
".." && currentDir !=
"/" )
742 else if ( S_ISLNK( statInfo.st_mode ) )
744 if ( stat64( fullName.c_str(), &linkInfo ) == 0 )
746 if ( S_ISDIR( linkInfo.st_mode ) )
758 startDir = currentDir;
767 yuiError() <<
"ERROR opening directory: " << currentDir <<
" errno: "
768 << strerror( errno ) << std::endl;
777 NCursesEvent NCDirectoryTable::wHandleInput( wint_t key )
783 if ( ret == NCursesEvent::key )
786 unsigned int old_pos = getCurrentItem();
800 ret = NCursesEvent::SelectionChanged;
801 ret.result = currentDir;
812 ret = NCursesEvent::SelectionChanged;
813 ret.result = currentDir;
821 ret = NCursesEvent::Activated;
822 ret.result = currentDir;
827 ret = NCursesEvent::none;
830 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()