27 #include <sys/types.h>
32 #include "tdefileitem.h"
37 #include <tqstylesheet.h>
41 #include <tdefilemetainfo.h>
42 #include <ksambashare.h>
43 #include <knfsshare.h>
44 #include <tdeglobal.h>
45 #include <tdeglobalsettings.h>
46 #include <kiconloader.h>
47 #include <klargefile.h>
48 #include <tdelocale.h>
49 #include <kmimetype.h>
53 #include "tdelficon.h"
54 #endif // HAVE_ELFICON
56 class KFileItem::KFileItemPrivate {
62 bool _determineMimeTypeOnDemand,
bool _urlIsDirectory ) :
70 m_bIsLocalURL( _url.isLocalFile() ),
71 m_bMimeTypeKnown( false ),
76 init( _determineMimeTypeOnDemand );
79 KFileItem::KFileItem( mode_t _mode, mode_t _permissions,
const KURL& _url,
bool _determineMimeTypeOnDemand ) :
82 m_strName( _url.fileName() ),
83 m_strText( TDEIO::decodeFileName( m_strName ) ),
86 m_permissions( _permissions ),
89 m_bIsLocalURL( _url.isLocalFile() ),
90 m_bMimeTypeKnown( false ),
94 init( _determineMimeTypeOnDemand );
99 m_strName( url.fileName() ),
100 m_strText( TDEIO::decodeFileName( m_strName ) ),
106 m_bIsLocalURL( url.isLocalFile() ),
107 m_bMimeTypeKnown( !mimeType.isEmpty() ),
111 if (m_bMimeTypeKnown) {
137 m_access = TQString::null;
140 for (
int i = 0; i < NumFlags; i++ ) {
141 m_time[i] = (time_t) -1;
145 if ( m_fileMode == KFileItem::Unknown || m_permissions == KFileItem::Unknown )
148 if ( m_url.isLocalFile() )
158 TQCString path = TQFile::encodeName(m_url.path( -1 ));
159 if ( KDE_lstat( path.data(), &buf ) == 0 )
162 if ( S_ISLNK( mode ) )
165 if ( KDE_stat( path.data(), &buf ) == 0 ) {
169 mode = (S_IFMT-1) | S_IRWXU | S_IRWXG | S_IRWXO;
173 m_time[ Modification ] = buf.st_mtime;
174 m_time[ Access ] = buf.st_atime;
175 if ( m_fileMode == KFileItem::Unknown ) {
176 m_fileMode = mode & S_IFMT;
178 if ( m_permissions == KFileItem::Unknown ) {
179 m_permissions = mode & 07777;
186 if (!m_pMimeType && !m_url.isEmpty())
188 bool accurate =
false;
194 _determineMimeTypeOnDemand, &accurate );
198 m_bMimeTypeKnown = (!_determineMimeTypeOnDemand) || accurate;
205 bool UDS_URL_seen =
false;
207 if (&m_entry == NULL)
return;
209 TDEIO::UDSEntry::ConstIterator it = m_entry.begin();
210 for( ; it != m_entry.end(); ++it ) {
211 switch ((*it).m_uds) {
214 m_fileMode = (mode_t)((*it).m_long);
218 m_permissions = (mode_t)((*it).m_long);
222 m_user = ((*it).m_str);
226 m_group = ((*it).m_str);
230 m_strName = (*it).m_str;
236 m_url = KURL((*it).m_str);
237 if ( m_url.isLocalFile() )
238 m_bIsLocalURL =
true;
243 m_bMimeTypeKnown =
true;
247 m_guessedMimeType = (*it).m_str;
251 m_bLink = !(*it).m_str.isEmpty();
256 d =
new KFileItemPrivate();
257 d->iconName = (*it).m_str;
270 static const TQString& dot = TDEGlobal::staticQString(
".");
271 if ( _urlIsDirectory && !UDS_URL_seen && !m_strName.isEmpty() && m_strName != dot ) {
272 m_url.addPath( m_strName );
278 m_fileMode = KFileItem::Unknown;
279 m_permissions = KFileItem::Unknown;
281 m_user = TQString::null;
282 m_group = TQString::null;
319 if (&m_entry == NULL)
return TQString::null;
322 TDEIO::UDSEntry::ConstIterator it = m_entry.begin();
323 for( ; it != m_entry.end(); ++it )
330 int n = readlink( TQFile::encodeName(m_url.path( -1 )), buf,
sizeof(buf)-1 );
334 return TQFile::decodeName( buf );
337 return TQString::null;
348 if (&m_entry == NULL)
return TQString::null;
351 TDEIO::UDSEntry::ConstIterator it = m_entry.begin();
352 const TDEIO::UDSEntry::ConstIterator end = m_entry.end();
353 for( ; it != end; ++it )
358 return TQString::null;
367 if (&m_entry == NULL)
return 0L;
370 TDEIO::UDSEntry::ConstIterator it = m_entry.begin();
371 for( ; it != m_entry.end(); ++it )
373 m_size = (*it).m_long;
380 if ( KDE_stat( TQFile::encodeName(m_url.path( -1 )), &buf ) == 0 )
389 if (&m_entry == NULL)
return false;
390 TDEIO::UDSEntry::ConstIterator it = m_entry.begin();
391 for( ; it != m_entry.end(); it++ )
401 if (&m_entry == NULL)
return KACL( m_permissions );
404 TDEIO::UDSEntry::ConstIterator it = m_entry.begin();
405 for( ; it != m_entry.end(); ++it )
407 return KACL((*it).m_str);
410 return KACL( m_permissions );
415 if (&m_entry == NULL)
return KACL();
418 TDEIO::UDSEntry::ConstIterator it = m_entry.begin();
419 for( ; it != m_entry.end(); ++it )
421 return KACL((*it).m_str);
434 return time(which, hasTime);
439 unsigned int mappedWhich = 0;
443 mappedWhich = Modification;
446 mappedWhich = Access;
449 mappedWhich = Creation;
453 if ( m_time[mappedWhich] != (time_t) -1 )
454 return m_time[mappedWhich];
456 if (&m_entry == NULL)
return static_cast<time_t
>(0);
459 TDEIO::UDSEntry::ConstIterator it = m_entry.begin();
460 for( ; it != m_entry.end(); ++it )
461 if ( (*it).m_uds == which ) {
462 m_time[mappedWhich] =
static_cast<time_t
>((*it).m_long);
463 return m_time[mappedWhich];
470 if ( KDE_stat( TQFile::encodeName(m_url.path(-1)), &buf ) == 0 )
475 m_time[mappedWhich] =
static_cast<time_t
>(0);
476 return m_time[mappedWhich];
482 return m_time[mappedWhich];
486 return static_cast<time_t
>(0);
492 if ( m_user.isEmpty() && m_bIsLocalURL )
494 KDE_struct_stat buff;
495 if ( KDE_lstat( TQFile::encodeName(m_url.path( -1 )), &buff ) == 0)
497 struct passwd *
user = getpwuid( buff.st_uid );
499 m_user = TQString::fromLocal8Bit(user->pw_name);
508 if (m_group.isEmpty() && m_bIsLocalURL )
510 KDE_struct_stat buff;
511 if ( KDE_lstat( TQFile::encodeName(m_url.path( -1 )), &buff ) == 0)
513 struct group *ge = getgrgid( buff.st_gid );
515 m_group = TQString::fromLocal8Bit(ge->gr_name);
516 if (m_group.isEmpty())
517 m_group.sprintf(
"%d",ge->gr_gid);
519 m_group.sprintf(
"%d",buff.st_gid);
534 if ( !m_pMimeType || !m_bMimeTypeKnown )
541 m_bMimeTypeKnown =
true;
547 bool KFileItem::isMimeTypeKnown()
const
552 return m_bMimeTypeKnown && m_guessedMimeType.isEmpty();
562 TQString comment = mType->comment( url, isLocalURL );
564 if (!comment.isEmpty())
567 return mType->name();
572 if (d && (!d->iconName.isEmpty()))
return d->iconName;
585 _state |= TDEIcon::LinkOverlay;
587 if ( !S_ISDIR( m_fileMode )
589 _state |= TDEIcon::LockOverlay;
592 _state |= TDEIcon::HiddenOverlay;
594 if( S_ISDIR( m_fileMode ) && m_bIsLocalURL)
600 _state |= TDEIcon::ShareOverlay;
604 if ( m_pMimeType->name() ==
"application/x-gzip" && m_url.fileName().right(3) ==
".gz" )
605 _state |= TDEIcon::ZipOverlay;
611 if (d && (!d->iconName.isEmpty()))
612 return DesktopIcon(d->iconName,_size,_state);
616 static const TQString & defaultFolderIcon =
619 if ( S_ISDIR( m_fileMode ) )
620 return DesktopIcon( defaultFolderIcon, _size, _state );
622 return DesktopIcon(
"unknown", _size, _state );
629 if ( !m_bMimeTypeKnown && !m_guessedMimeType.isEmpty() )
636 if ( mime->name() ==
"application/x-gzip" && m_url.fileName().right(3) ==
".gz" )
639 sf.setPath( m_url.path().left( m_url.path().length() - 3 ) );
647 TQPixmap p = mime->pixmap( url, TDEIcon::Desktop, _size, _state );
650 kdWarning() <<
"Pixmap not found for mimetype " << m_pMimeType->name() << endl;
652 if ( mime->name() ==
"application/x-executable" ) {
660 unsigned int icon_size;
661 libr_icon *icon = NULL;
662 libr_file *handle = NULL;
663 libr_access_t access = LIBR_READ;
665 if((handle = libr_open(const_cast<char*>(url.path().ascii()), access)) == NULL)
667 kdWarning() <<
"failed to open file" << url.path() << endl;
672 icon = libr_icon_geticon_bysize(handle, icon_size);
676 int iconresnamefound = 0;
677 iconentry *
entry = NULL;
679 if(!get_iconlist(handle, &icons))
682 kdWarning() <<
"failed to obtain ELF icon: " << libr_errmsg() << endl;
685 TQString sysIconName = elf_get_resource(handle,
".metadata_sysicon");
686 if (!sysIconName.isEmpty()) {
687 if (TDEGlobal::iconLoader()->iconPath(sysIconName.ascii(), 0,
true) !=
"") {
688 p = DesktopIcon( sysIconName.ascii(), _size, _state );
696 while((entry = get_nexticon(&icons, entry)) != NULL)
701 icon = libr_icon_geticon_byname(handle, entry->name);
703 if (TDEGlobal::iconLoader()->iconPath(entry->name, 0,
true) !=
"") {
704 iconresnamefound = 1;
705 p = DesktopIcon( entry->name, _size, _state );
711 if ((iconresnamefound == 0) && (icon)) {
713 size_t icon_data_length;
714 char* icondata = libr_icon_malloc(icon, &icon_data_length);
715 p.loadFromData(static_cast<uchar*>(static_cast<void*>(icondata)), icon_data_length);
716 if (icon_size != 0) {
717 TQImage ip = p.convertToImage();
718 ip = ip.smoothScale(icon_size, icon_size);
719 p.convertFromImage(ip);
722 libr_icon_close(icon);
726 #endif // HAVE_ELFICON
742 if ( m_permissions != KFileItem::Unknown ) {
744 if ( !(S_IRUSR & m_permissions) && !(S_IRGRP & m_permissions) && !(S_IROTH & m_permissions) )
748 if ( (S_IRUSR|S_IRGRP|S_IROTH) & m_permissions )
753 if ( m_bIsLocalURL && ::access( TQFile::encodeName(m_url.path()), R_OK ) == -1 )
769 if ( m_permissions != KFileItem::Unknown ) {
771 if ( !(S_IWUSR & m_permissions) && !(S_IWGRP & m_permissions) && !(S_IWOTH & m_permissions) )
776 if ( m_bIsLocalURL && ::access( TQFile::encodeName(m_url.path()), W_OK ) == -1 )
784 if ( m_hidden != Auto )
785 return m_hidden == Hidden;
787 if ( !m_url.isEmpty() )
788 return m_url.fileName()[0] ==
'.';
790 return m_strName[0] ==
'.';
795 if ( m_fileMode == KFileItem::Unknown )
797 kdDebug() <<
" KFileItem::isDir can't say -> false " << endl;
800 return (S_ISDIR(m_fileMode));
814 if ( S_ISDIR(
mode() ) ) {
819 if ( !m_bIsLocalURL )
822 if ((
mimetype() ==
"application/x-desktop") ||
823 (
mimetype() ==
"media/builtin-mydocuments") ||
824 (
mimetype() ==
"media/builtin-mycomputer") ||
825 (
mimetype() ==
"media/builtin-mynetworkplaces") ||
826 (
mimetype() ==
"media/builtin-printers") ||
827 (
mimetype() ==
"media/builtin-trash") ||
828 (
mimetype() ==
"media/builtin-webbrowser"))
832 if ( ::access( TQFile::encodeName(m_url.path()), X_OK ) == 0 )
840 TQString
text = m_strText;
846 if ( comment.isEmpty() )
847 tmp = i18n (
"Symbolic Link" );
849 tmp = i18n(
"%1 (Link)").arg(comment);
855 else if ( S_ISREG( m_fileMode ) )
863 else if ( S_ISDIR ( m_fileMode ) )
873 text.replace(
'\n',
" ");
885 const char* start =
"<tr><td><nobr><font color=\"black\">";
886 const char* mid =
"</font></nobr></td><td><nobr><font color=\"black\">";
887 const char* end =
"</font></nobr></td></tr>";
889 tip =
"<table cellspacing=0 cellpadding=0>";
891 tip += start + i18n(
"Name:") + mid +
text() + end;
892 tip += start + i18n(
"Type:") + mid;
894 TQString type = TQStyleSheet::escape(
mimeComment());
896 tip += i18n(
"Link to %1 (%2)").arg(
linkDest(), type) + end;
900 if ( !S_ISDIR ( m_fileMode ) ) {
904 tip += start + i18n(
"Size:") + mid +
908 if(!timeStr.isEmpty())
909 tip += start + i18n(
"Modified:") + mid +
911 #ifndef Q_WS_WIN //TODO: show win32-specific permissions
912 TQString userStr =
user();
913 TQString groupStr =
group();
914 if(!userStr.isEmpty() || !groupStr.isEmpty())
915 tip += start + i18n(
"Owner:") + mid + userStr +
" - " + groupStr + end +
916 start + i18n(
"Permissions:") + mid +
922 tip +=
"<tr><td colspan=2><center><s> </s></center></td></tr>";
926 TQStringList::Iterator it = keys.begin();
927 for (
int count = 0; count<maxcount && it!=keys.end() ; ++it)
932 TQString s = item.
string();
934 && s.length() > 50) {
944 TQStyleSheet::escape( s ) +
964 (void)
new KRun( m_url, m_fileMode, m_bIsLocalURL );
969 bool hasSize1,hasSize2,hasTime1,hasTime2;
970 hasSize1 = hasSize2 = hasTime1 = hasTime2 =
false;
971 return ( m_strName == item.m_strName
972 && m_bIsLocalURL == item.m_bIsLocalURL
973 && m_fileMode == item.m_fileMode
974 && m_permissions == item.m_permissions
975 && m_user == item.m_user
976 && m_group == item.m_group
977 && m_bLink == item.m_bLink
978 && m_hidden == item.m_hidden
979 &&
size(hasSize1) == item.
size(hasSize2)
980 && hasSize1 == hasSize2
982 && hasTime1 == hasTime2
983 && (!d || !item.d || d->iconName == item.d->iconName) );
993 m_entry = item.m_entry;
995 m_bIsLocalURL = item.m_bIsLocalURL;
996 m_strName = item.m_strName;
997 m_strText = item.m_strText;
998 m_fileMode = item.m_fileMode;
999 m_permissions = item.m_permissions;
1000 m_user = item.m_user;
1001 m_group = item.m_group;
1002 m_bLink = item.m_bLink;
1003 m_pMimeType = item.m_pMimeType;
1004 m_strLowerCaseName = item.m_strLowerCaseName;
1005 m_bMimeTypeKnown = item.m_bMimeTypeKnown;
1006 m_hidden = item.m_hidden;
1007 m_guessedMimeType = item.m_guessedMimeType;
1008 m_access = item.m_access;
1009 m_metaInfo = item.m_metaInfo;
1010 for (
int i = 0; i < NumFlags; i++ )
1011 m_time[i] = item.m_time[i];
1012 m_size = item.m_size;
1021 d =
new KFileItemPrivate;
1022 d->iconName = item.d->iconName;
1030 bool _determineMimeTypeOnDemand,
bool _urlIsDirectory )
1034 m_strName = TQString::null;
1035 m_strText = TQString::null;
1036 m_user = TQString::null;
1037 m_group = TQString::null;
1038 m_strLowerCaseName = TQString::null;
1040 m_fileMode = KFileItem::Unknown;
1041 m_permissions = KFileItem::Unknown;
1044 m_bIsLocalURL = _url.isLocalFile();
1045 m_bMimeTypeKnown =
false;
1047 m_guessedMimeType = TQString::null;
1051 d->iconName = TQString::null;
1054 init( _determineMimeTypeOnDemand );
1072 m_extra.replace( key, value );
1077 TQMapConstIterator<const void*,void*> it = m_extra.find( key );
1078 if ( it != m_extra.end() )
1085 TQMapIterator<const void*,void*> it = m_extra.find( key );
1086 if ( it != m_extra.end() )
1093 m_extra.remove( key );
1098 if (m_access.isNull())
1106 char p[] =
"---------- ";
1113 if (perm & TQFileInfo::ReadUser)
1115 if (perm & TQFileInfo::WriteUser)
1117 if ((perm & TQFileInfo::ExeUser) && !(perm & S_ISUID)) p[3]=
'x';
1118 else if ((perm & TQFileInfo::ExeUser) && (perm & S_ISUID)) p[3]=
's';
1119 else if (!(perm & TQFileInfo::ExeUser) && (perm & S_ISUID)) p[3]=
'S';
1121 if (perm & TQFileInfo::ReadGroup)
1123 if (perm & TQFileInfo::WriteGroup)
1125 if ((perm & TQFileInfo::ExeGroup) && !(perm & S_ISGID)) p[6]=
'x';
1126 else if ((perm & TQFileInfo::ExeGroup) && (perm & S_ISGID)) p[6]=
's';
1127 else if (!(perm & TQFileInfo::ExeGroup) && (perm & S_ISGID)) p[6]=
'S';
1129 if (perm & TQFileInfo::ReadOther)
1131 if (perm & TQFileInfo::WriteOther)
1133 if ((perm & TQFileInfo::ExeOther) && !(perm & S_ISVTX)) p[9]=
'x';
1134 else if ((perm & TQFileInfo::ExeOther) && (perm & S_ISVTX)) p[9]=
't';
1135 else if (!(perm & TQFileInfo::ExeOther) && (perm & S_ISVTX)) p[9]=
'T';
1140 return TQString::fromLatin1(p);
1147 time_t time_ =
time(which, hasTime);
1148 if(!hasTime)
return TQString::null;
1151 t.setTime_t( time_);
1152 return TDEGlobal::locale()->formatDateTime( t );
1165 if ( autoget && !m_metaInfo.
isValid() &&
1166 TDEGlobalSettings::showFilePreview(url) )
1178 if ( !local_path.isEmpty() )
1182 url.setPath(local_path);
1187 local = m_bIsLocalURL;
1192 void KFileItem::virtual_hook(
int,
void* )
1195 TQDataStream & operator<< ( TQDataStream & s,
const KFileItem & a )
1205 TQDataStream & operator>> ( TQDataStream & s,
KFileItem & a )
1210 a.m_bIsLocalURL = a.m_url.isLocalFile();
1211 a.m_bMimeTypeKnown =
false;