kio Library API Documentation

kfileitem.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 1999 David Faure <faure@kde.org>
00003                  2001 Carsten Pfeiffer <pfeiffer@kde.org>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018    Boston, MA 02111-1307, USA.
00019 */
00020 // $Id: kfileitem.cpp,v 1.158.2.1 2004/09/14 18:48:26 binner Exp $
00021 
00022 #include <sys/time.h>
00023 #include <pwd.h>
00024 #include <grp.h>
00025 #include <sys/types.h>
00026 
00027 #include <assert.h>
00028 #include <unistd.h>
00029 
00030 #include "kfileitem.h"
00031 
00032 #include <qdir.h>
00033 #include <qfile.h>
00034 #include <qmap.h>
00035 #include <qstylesheet.h>
00036 
00037 #include <kdebug.h>
00038 #include <kfilemetainfo.h>
00039 #include <ksambashare.h>
00040 #include <knfsshare.h>
00041 #include <kglobal.h>
00042 #include <kglobalsettings.h>
00043 #include <kiconloader.h>
00044 #include <klargefile.h>
00045 #include <klocale.h>
00046 #include <kmimetype.h>
00047 #include <krun.h>
00048 
00049 class KFileItem::KFileItemPrivate {
00050     public:
00051         QString iconName;
00052 };
00053 
00054 KFileItem::KFileItem( const KIO::UDSEntry& _entry, const KURL& _url,
00055                       bool _determineMimeTypeOnDemand, bool _urlIsDirectory ) :
00056   m_entry( _entry ),
00057   m_url( _url ),
00058   m_pMimeType( 0 ),
00059   m_fileMode( KFileItem::Unknown ),
00060   m_permissions( KFileItem::Unknown ),
00061   m_bMarked( false ),
00062   m_bLink( false ),
00063   m_bIsLocalURL( _url.isLocalFile() ),
00064   m_bMimeTypeKnown( false ),
00065   d(0L)
00066 {
00067   bool UDS_URL_seen = false;
00068   // extract the mode and the filename from the KIO::UDS Entry
00069   KIO::UDSEntry::ConstIterator it = m_entry.begin();
00070   for( ; it != m_entry.end(); it++ ) {
00071     switch ((*it).m_uds) {
00072 
00073         case KIO::UDS_FILE_TYPE:
00074           m_fileMode = (mode_t)((*it).m_long);
00075           break;
00076 
00077         case KIO::UDS_ACCESS:
00078           m_permissions = (mode_t)((*it).m_long);
00079           break;
00080 
00081         case KIO::UDS_USER:
00082           m_user = ((*it).m_str);
00083           break;
00084 
00085         case KIO::UDS_GROUP:
00086           m_group = ((*it).m_str);
00087           break;
00088 
00089         case KIO::UDS_NAME:
00090           m_strName = (*it).m_str;
00091           m_strText = KIO::decodeFileName( m_strName );
00092           break;
00093 
00094         case KIO::UDS_URL:
00095           UDS_URL_seen = true;
00096           m_url = KURL((*it).m_str);
00097           break;
00098 
00099         case KIO::UDS_MIME_TYPE:
00100           m_pMimeType = KMimeType::mimeType((*it).m_str);
00101           m_bMimeTypeKnown = true;
00102           break;
00103 
00104         case KIO::UDS_GUESSED_MIME_TYPE:
00105           m_guessedMimeType = (*it).m_str;
00106           break;
00107 
00108         case KIO::UDS_LINK_DEST:
00109           m_bLink = !(*it).m_str.isEmpty(); // we don't store the link dest
00110           break;
00111         case KIO::UDS_ICON_NAME:
00112       d=new KFileItemPrivate();
00113       d->iconName=(*it).m_str;
00114       break;
00115     }
00116   }
00117   // avoid creating these QStrings again and again
00118   static const QString& dot = KGlobal::staticQString(".");
00119   if ( _urlIsDirectory && !UDS_URL_seen && !m_strName.isEmpty() && m_strName != dot )
00120     m_url.addPath( m_strName );
00121   init( _determineMimeTypeOnDemand );
00122 }
00123 
00124 KFileItem::KFileItem( mode_t _mode, mode_t _permissions, const KURL& _url, bool _determineMimeTypeOnDemand ) :
00125   m_entry(), // warning !
00126   m_url( _url ),
00127   m_strName( _url.fileName() ),
00128   m_strText( KIO::decodeFileName( m_strName ) ),
00129   m_pMimeType( 0 ),
00130   m_fileMode ( _mode ),
00131   m_permissions( _permissions ),
00132   m_bMarked( false ),
00133   m_bLink( false ),
00134   m_bIsLocalURL( _url.isLocalFile() ),
00135   m_bMimeTypeKnown( false ),
00136   d(0L)
00137 {
00138   init( _determineMimeTypeOnDemand );
00139 }
00140 
00141 KFileItem::KFileItem( const KURL &url, const QString &mimeType, mode_t mode )
00142 :  m_url( url ),
00143   m_strName( url.fileName() ),
00144   m_strText( KIO::decodeFileName( m_strName ) ),
00145   m_pMimeType( 0 ),
00146   m_fileMode( mode ),
00147   m_permissions( KFileItem::Unknown ),
00148   m_bMarked( false ),
00149   m_bLink( false ),
00150   m_bIsLocalURL( url.isLocalFile() ),
00151   m_bMimeTypeKnown( !mimeType.isEmpty() ),
00152   d(0L)
00153 {
00154   if (m_bMimeTypeKnown)
00155     m_pMimeType = KMimeType::mimeType( mimeType );
00156 
00157   init( false );
00158 }
00159 
00160 KFileItem::KFileItem( const KFileItem & item ) :
00161   d(0L)
00162 {
00163     assign( item );
00164 }
00165 
00166 KFileItem::~KFileItem()
00167 {
00168   delete d;
00169 }
00170 
00171 void KFileItem::init( bool _determineMimeTypeOnDemand )
00172 {
00173   m_access = QString::null;
00174   m_size = (KIO::filesize_t) -1;
00175   //  metaInfo = KFileMetaInfo();
00176   for ( int i = 0; i < NumFlags; i++ )
00177       m_time[i] = (time_t) -1;
00178 
00179   // determine mode and/or permissions if unknown
00180   if ( m_fileMode == KFileItem::Unknown || m_permissions == KFileItem::Unknown )
00181   {
00182     mode_t mode = 0;
00183     if ( m_url.isLocalFile() )
00184     {
00185       /* directories may not have a slash at the end if
00186        * we want to stat() them; it requires that we
00187        * change into it .. which may not be allowed
00188        * stat("/is/unaccessible")  -> rwx------
00189        * stat("/is/unaccessible/") -> EPERM            H.Z.
00190        * This is the reason for the -1
00191        */
00192       KDE_struct_stat buf;
00193       QCString path = QFile::encodeName(m_url.path( -1 ));
00194       if ( KDE_lstat( path.data(), &buf ) == 0 )
00195       {
00196         mode = buf.st_mode;
00197         if ( S_ISLNK( mode ) )
00198         {
00199           m_bLink = true;
00200           if ( KDE_stat( path.data(), &buf ) == 0 )
00201               mode = buf.st_mode;
00202           else // link pointing to nowhere (see kio/file/file.cc)
00203               mode = (S_IFMT-1) | S_IRWXU | S_IRWXG | S_IRWXO;
00204         }
00205         // While we're at it, store the times
00206         m_time[ Modification ] = buf.st_mtime;
00207         m_time[ Access ] = buf.st_atime;
00208         if ( m_fileMode == KFileItem::Unknown )
00209           m_fileMode = mode & S_IFMT; // extract file type
00210         if ( m_permissions == KFileItem::Unknown )
00211           m_permissions = mode & 07777; // extract permissions
00212       }
00213     }
00214   }
00215 
00216   // determine the mimetype
00217   if (!m_pMimeType && !m_url.isEmpty())
00218   {
00219       bool accurate = false;
00220       m_pMimeType = KMimeType::findByURL( m_url, m_fileMode, m_bIsLocalURL,
00221                                           // use fast mode if not mimetype on demand
00222                                           _determineMimeTypeOnDemand, &accurate );
00223       // if we didn't use fast mode, or if we got a result, then this is the mimetype
00224       // otherwise, determineMimeType will be able to do better.
00225       m_bMimeTypeKnown = (!_determineMimeTypeOnDemand) || accurate;
00226   }
00227 
00228 }
00229 
00230 void KFileItem::refresh()
00231 {
00232   m_fileMode = KFileItem::Unknown;
00233   m_permissions = KFileItem::Unknown;
00234   m_pMimeType = 0L;
00235   m_user = QString::null;
00236   m_group = QString::null;
00237   m_access = QString::null;
00238   m_size = (KIO::filesize_t) -1;
00239   m_metaInfo = KFileMetaInfo();
00240   for ( int i = 0; i < NumFlags; i++ )
00241       m_time[i] = (time_t) -1;
00242 
00243   // Basically, we can't trust any information we got while listing.
00244   // Everything could have changed...
00245   // Clearing m_entry makes it possible to detect changes in the size of the file,
00246   // the time information, etc.
00247   m_entry = KIO::UDSEntry();
00248   init( false );
00249 }
00250 
00251 void KFileItem::refreshMimeType()
00252 {
00253   m_pMimeType = 0L;
00254   init( false ); // Will determine the mimetype
00255 }
00256 
00257 void KFileItem::setURL( const KURL &url )
00258 {
00259   m_url = url;
00260   setName( url.fileName() );
00261 }
00262 
00263 void KFileItem::setName( const QString& name )
00264 {
00265   m_strName = name;
00266   m_strText = KIO::decodeFileName( m_strName );
00267 }
00268 
00269 QString KFileItem::linkDest() const
00270 {
00271   // Extract it from the KIO::UDSEntry
00272   KIO::UDSEntry::ConstIterator it = m_entry.begin();
00273   for( ; it != m_entry.end(); it++ )
00274     if ( (*it).m_uds == KIO::UDS_LINK_DEST )
00275       return (*it).m_str;
00276   // If not in the KIO::UDSEntry, or if UDSEntry empty, use readlink() [if local URL]
00277   if ( m_bIsLocalURL )
00278   {
00279     char buf[1000];
00280     int n = readlink( QFile::encodeName(m_url.path( -1 )), buf, sizeof(buf)-1 );
00281     if ( n != -1 )
00282     {
00283       buf[ n ] = 0;
00284       return QFile::decodeName( buf );
00285     }
00286   }
00287   return QString::null;
00288 }
00289 
00290 KIO::filesize_t KFileItem::size() const
00291 {
00292   if ( m_size != (KIO::filesize_t) -1 )
00293     return m_size;
00294 
00295   // Extract it from the KIO::UDSEntry
00296   KIO::UDSEntry::ConstIterator it = m_entry.begin();
00297   for( ; it != m_entry.end(); it++ )
00298     if ( (*it).m_uds == KIO::UDS_SIZE ) {
00299       m_size = (*it).m_long;
00300       return m_size;
00301     }
00302   // If not in the KIO::UDSEntry, or if UDSEntry empty, use stat() [if local URL]
00303   if ( m_bIsLocalURL )
00304   {
00305     KDE_struct_stat buf;
00306     if ( KDE_stat( QFile::encodeName(m_url.path( -1 )), &buf ) == 0 )
00307         return buf.st_size;
00308   }
00309   return 0L;
00310 }
00311 
00312 time_t KFileItem::time( unsigned int which ) const
00313 {
00314   unsigned int mappedWhich = 0;
00315 
00316   switch( which ) {
00317     case KIO::UDS_MODIFICATION_TIME:
00318       mappedWhich = Modification;
00319       break;
00320     case KIO::UDS_ACCESS_TIME:
00321       mappedWhich = Access;
00322       break;
00323     case KIO::UDS_CREATION_TIME:
00324       mappedWhich = Creation;
00325       break;
00326   }
00327 
00328   if ( m_time[mappedWhich] != (time_t) -1 )
00329     return m_time[mappedWhich];
00330 
00331   // Extract it from the KIO::UDSEntry
00332   KIO::UDSEntry::ConstIterator it = m_entry.begin();
00333   for( ; it != m_entry.end(); it++ )
00334     if ( (*it).m_uds == which ) {
00335       m_time[mappedWhich] = static_cast<time_t>((*it).m_long);
00336       return m_time[mappedWhich];
00337     }
00338 
00339   // If not in the KIO::UDSEntry, or if UDSEntry empty, use stat() [if local URL]
00340   if ( m_bIsLocalURL )
00341   {
00342     KDE_struct_stat buf;
00343     if ( KDE_stat( QFile::encodeName(m_url.path(-1)), &buf ) == 0 )
00344     {
00345         m_time[mappedWhich] = (which == KIO::UDS_MODIFICATION_TIME) ?
00346                                buf.st_mtime :
00347                                (which == KIO::UDS_ACCESS_TIME) ? buf.st_atime :
00348                                static_cast<time_t>(0); // We can't determine creation time for local files
00349         return m_time[mappedWhich];
00350     }
00351   }
00352   return static_cast<time_t>(0);
00353 }
00354 
00355 
00356 QString KFileItem::user() const
00357 {
00358   if ( m_user.isEmpty() && m_bIsLocalURL )
00359   {
00360     KDE_struct_stat buff;
00361     if ( KDE_lstat( QFile::encodeName(m_url.path( -1 )), &buff ) == 0) // get uid/gid of the link, if it's a link
00362     {
00363       struct passwd *user = getpwuid( buff.st_uid );
00364       if ( user != 0L )
00365         m_user = QString::fromLocal8Bit(user->pw_name);
00366     }
00367   }
00368   return m_user;
00369 }
00370 
00371 QString KFileItem::group() const
00372 {
00373   if (m_group.isEmpty() && m_bIsLocalURL )
00374   {
00375     KDE_struct_stat buff;
00376     if ( KDE_lstat( QFile::encodeName(m_url.path( -1 )), &buff ) == 0) // get uid/gid of the link, if it's a link
00377     {
00378       struct group *ge = getgrgid( buff.st_gid );
00379       if ( ge != 0L ) {
00380         m_group = QString::fromLocal8Bit(ge->gr_name);
00381         if (m_group.isEmpty())
00382           m_group.sprintf("%d",ge->gr_gid);
00383       } else
00384         m_group.sprintf("%d",buff.st_gid);
00385     }
00386   }
00387   return m_group;
00388 }
00389 
00390 QString KFileItem::mimetype() const
00391 {
00392   KFileItem * that = const_cast<KFileItem *>(this);
00393   return that->determineMimeType()->name();
00394 }
00395 
00396 KMimeType::Ptr KFileItem::determineMimeType()
00397 {
00398   if ( !m_pMimeType || !m_bMimeTypeKnown )
00399   {
00400     m_pMimeType = KMimeType::findByURL( m_url, m_fileMode, m_bIsLocalURL );
00401     //kdDebug() << "finding mimetype for " << m_url.url() << " : " << m_pMimeType->name() << endl;
00402     m_bMimeTypeKnown = true;
00403   }
00404 
00405   return m_pMimeType;
00406 }
00407 
00408 bool KFileItem::isMimeTypeKnown() const
00409 {
00410   // The mimetype isn't known if determineMimeType was never called (on-demand determination)
00411   // or if this fileitem has a guessed mimetype (e.g. ftp symlink) - in which case
00412   // it always remains "not fully determined"
00413   return m_bMimeTypeKnown && m_guessedMimeType.isEmpty();
00414 }
00415 
00416 QString KFileItem::mimeComment()
00417 {
00418  KMimeType::Ptr mType = determineMimeType();
00419  QString comment = mType->comment( m_url, m_bIsLocalURL );
00420   if (!comment.isEmpty())
00421     return comment;
00422   else
00423     return mType->name();
00424 }
00425 
00426 QString KFileItem::iconName()
00427 {
00428   if (d && (!d->iconName.isEmpty())) return d->iconName;
00429   return determineMimeType()->icon(m_url, m_bIsLocalURL);
00430 }
00431 
00432 int KFileItem::overlays() const
00433 {
00434   int _state = 0;
00435   if ( m_bLink )
00436       _state |= KIcon::LinkOverlay;
00437 
00438   if ( !S_ISDIR( m_fileMode ) // Locked dirs have a special icon, use the overlay for files only
00439        && !isReadable())
00440      _state |= KIcon::LockOverlay;
00441 
00442   if ( isHidden() )
00443      _state |= KIcon::HiddenOverlay;
00444 
00445   if( S_ISDIR( m_fileMode ) && m_bIsLocalURL)
00446   {
00447     if (KSambaShare::instance()->isDirectoryShared( m_url.path() ) ||
00448         KNFSShare::instance()->isDirectoryShared( m_url.path() ))
00449     {
00450       //kdDebug()<<"KFileShare::isDirectoryShared : "<<m_url.path()<<endl;
00451       _state |= KIcon::ShareOverlay;
00452     }
00453   }
00454 
00455   if ( m_pMimeType->name() == "application/x-gzip" && m_url.fileName().right(3) == ".gz" )
00456      _state |= KIcon::ZipOverlay;
00457   return _state;
00458 }
00459 
00460 QPixmap KFileItem::pixmap( int _size, int _state ) const
00461 {
00462   if (d && (!d->iconName.isEmpty()))
00463      return DesktopIcon(d->iconName,_size,_state);
00464 
00465   if ( !m_pMimeType )
00466   {
00467     static const QString & defaultFolderIcon =
00468        KGlobal::staticQString(KMimeType::mimeType( "inode/directory" )->KServiceType::icon());
00469 
00470     if ( S_ISDIR( m_fileMode ) )
00471      return DesktopIcon( defaultFolderIcon, _size, _state );
00472 
00473     return DesktopIcon( "unknown", _size, _state );
00474   }
00475 
00476   _state |= overlays();
00477 
00478   KMimeType::Ptr mime;
00479   // Use guessed mimetype if the main one hasn't been determined for sure
00480   if ( !m_bMimeTypeKnown && !m_guessedMimeType.isEmpty() )
00481       mime = KMimeType::mimeType( m_guessedMimeType );
00482   else
00483       mime = m_pMimeType;
00484 
00485   // Support for gzipped files: extract mimetype of contained file
00486   // See also the relevant code in overlays, which adds the zip overlay.
00487   if ( mime->name() == "application/x-gzip" && m_url.fileName().right(3) == ".gz" )
00488   {
00489       KURL sf;
00490       sf.setPath( m_url.path().left( m_url.path().length() - 3 ) );
00491       //kdDebug() << "KFileItem::pixmap subFileName=" << subFileName << endl;
00492       mime = KMimeType::findByURL( sf, 0, m_bIsLocalURL );
00493   }
00494 
00495   QPixmap p = mime->pixmap( m_url, KIcon::Desktop, _size, _state );
00496   if (p.isNull())
00497       kdWarning() << "Pixmap not found for mimetype " << m_pMimeType->name() << endl;
00498 
00499   return p;
00500 }
00501 
00502 bool KFileItem::isReadable() const
00503 {
00504   /*
00505   struct passwd * user = getpwuid( geteuid() );
00506   bool isMyFile = (QString::fromLocal8Bit(user->pw_name) == m_user);
00507   // This gets ugly for the group....
00508   // Maybe we want a static QString for the user and a static QStringList
00509   // for the groups... then we need to handle the deletion properly...
00510   */
00511 
00512   // No read permission at all
00513   if ( !(S_IRUSR & m_permissions) && !(S_IRGRP & m_permissions) && !(S_IROTH & m_permissions) )
00514       return false;
00515 
00516   // Or if we can't read it [using ::access()] - not network transparent
00517   else if ( m_bIsLocalURL && ::access( QFile::encodeName(m_url.path()), R_OK ) == -1 )
00518       return false;
00519 
00520   return true;
00521 }
00522 
00523 bool KFileItem::isHidden() const
00524 {
00525   if ( !m_url.isEmpty() )
00526       return m_url.fileName()[0] == '.';
00527   else // should never happen
00528       return m_strName[0] == '.';
00529 }
00530 
00531 bool KFileItem::isDir() const
00532 {
00533   if ( m_fileMode == KFileItem::Unknown )
00534   {
00535     kdDebug() << " KFileItem::isDir can't say -> false " << endl;
00536     return false; // can't say for sure, so no
00537   }
00538   return S_ISDIR(m_fileMode);
00539 }
00540 
00541 bool KFileItem::acceptsDrops()
00542 {
00543   // A directory ?
00544   if ( S_ISDIR( mode() ) )
00545   {
00546     if ( m_bIsLocalURL ) // local -> check if we can enter it
00547        return (::access( QFile::encodeName(m_url.path()), X_OK ) == 0);
00548     else
00549        return true; // assume ok for remote urls
00550   }
00551 
00552   // But only local .desktop files and executables
00553   if ( !m_bIsLocalURL )
00554     return false;
00555 
00556   if ( mimetype() == "application/x-desktop")
00557     return true;
00558 
00559   // Executable, shell script ... ?
00560   if ( ::access( QFile::encodeName(m_url.path()), X_OK ) == 0 )
00561     return true;
00562 
00563   return false;
00564 }
00565 
00566 QString KFileItem::getStatusBarInfo()
00567 {
00568   QString comment = determineMimeType()->comment( m_url, m_bIsLocalURL );
00569   QString text = m_strText;
00570   // Extract from the KIO::UDSEntry the additional info we didn't get previously
00571   QString myLinkDest = linkDest();
00572   KIO::filesize_t mySize = size();
00573 
00574   if ( m_bLink )
00575   {
00576       QString tmp;
00577       if ( comment.isEmpty() )
00578         tmp = i18n ( "Symbolic Link" );
00579       else
00580         tmp = i18n("%1 (Link)").arg(comment);
00581       text += "->";
00582       text += myLinkDest;
00583       text += "  ";
00584       text += tmp;
00585   }
00586   else if ( S_ISREG( m_fileMode ) )
00587   {
00588       text += QString(" (%1)").arg( KIO::convertSize( mySize ) );
00589       text += "  ";
00590       text += comment;
00591   }
00592   else if ( S_ISDIR ( m_fileMode ) )
00593   {
00594       text += "/  ";
00595       text += comment;
00596     }
00597     else
00598     {
00599       text += "  ";
00600       text += comment;
00601     }
00602     return text;
00603 }
00604 
00605 QString KFileItem::getToolTipText(int maxcount)
00606 {
00607   // we can return QString::null if no tool tip should be shown
00608   QString tip;
00609   KFileMetaInfo info = metaInfo();
00610 
00611   // the font tags are a workaround for the fact that the tool tip gets
00612   // screwed if the color scheme uses white as default text color
00613   const char* start = "<tr><td><nobr><font color=\"black\">";
00614   const char* mid   = "</font></nobr></td><td><nobr><font color=\"black\">";
00615   const char* end   = "</font></nobr></td></tr>";
00616 
00617   tip = "<table cellspacing=0 cellpadding=0>";
00618 
00619   tip += start + i18n("Type:") + mid;
00620 
00621   QString type = QStyleSheet::escape(determineMimeType()->comment());
00622   if ( m_bLink ) {
00623    tip += i18n("Link to %1 (%2)").arg(linkDest(), type) + end;
00624   } else
00625     tip += type + end;
00626 
00627   if ( !S_ISDIR ( m_fileMode ) )
00628     tip += start + i18n("Size:") + mid +
00629            KIO::convertSize( size() ) + end;
00630 
00631   tip += start + i18n("Modified:") + mid +
00632          timeString( KIO::UDS_MODIFICATION_TIME) + end +
00633          start + i18n("Owner:") + mid + user() + " - " + group() + end +
00634          start + i18n("Permissions:") + mid +
00635          parsePermissions(m_permissions) + end;
00636 
00637   if (info.isValid() && !info.isEmpty() )
00638   {
00639     tip += "<tr><td colspan=2><center><s>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</s></center></td></tr>";
00640     QStringList keys = info.preferredKeys();
00641 
00642     // now the rest
00643     QStringList::Iterator it = keys.begin();
00644     for (int count = 0; count<maxcount && it!=keys.end() ; ++it)
00645     {
00646       KFileMetaInfoItem item = info.item( *it );
00647       if ( item.isValid() )
00648       {
00649         QString s = item.string();
00650         if (s.length() > 50) {
00651           s.truncate(47);
00652           s.append("...");
00653         }
00654         if ( !s.isEmpty() )
00655         {
00656           count++;
00657           tip += start +
00658                    QStyleSheet::escape( item.translatedKey() ) + ":" +
00659                  mid +
00660                    QStyleSheet::escape( s ) +
00661                  end;
00662         }
00663 
00664       }
00665     }
00666     tip += "</table>";
00667   }
00668 
00669   //kdDebug() << "making this the tool tip rich text:\n";
00670   //kdDebug() << tip << endl;
00671 
00672   return tip;
00673 }
00674 
00675 void KFileItem::run()
00676 {
00677   KURL url( m_url );
00678   // When clicking on a link to e.g. $HOME from the desktop, we want to open $HOME
00679   // But when following a link on the FTP site, the target be an absolute path
00680   // that doesn't work in the URL. So we resolve links only on the local filesystem.
00681   if ( m_bLink && m_bIsLocalURL )
00682     url = KURL( m_url, linkDest() );
00683   (void) new KRun( url, m_fileMode, m_bIsLocalURL );
00684 }
00685 
00686 bool KFileItem::cmp( const KFileItem & item )
00687 {
00688     return ( m_strName == item.m_strName
00689              && m_bIsLocalURL == item.m_bIsLocalURL
00690              && m_fileMode == item.m_fileMode
00691              && m_permissions == item.m_permissions
00692              && m_user == item.m_user
00693              && m_group == item.m_group
00694              && m_bLink == item.m_bLink
00695              && size() == item.size()
00696              && time(KIO::UDS_MODIFICATION_TIME) == item.time(KIO::UDS_MODIFICATION_TIME) );
00697 }
00698 
00699 void KFileItem::assign( const KFileItem & item )
00700 {
00701     if ( this == &item )
00702         return;
00703     m_entry = item.m_entry;
00704     m_url = item.m_url;
00705     m_bIsLocalURL = item.m_bIsLocalURL;
00706     m_strName = item.m_strName;
00707     m_strText = item.m_strText;
00708     m_fileMode = item.m_fileMode;
00709     m_permissions = item.m_permissions;
00710     m_user = item.m_user;
00711     m_group = item.m_group;
00712     m_bLink = item.m_bLink;
00713     m_pMimeType = item.m_pMimeType;
00714     m_strLowerCaseName = item.m_strLowerCaseName;
00715     m_bMimeTypeKnown = item.m_bMimeTypeKnown;
00716     m_guessedMimeType   = item.m_guessedMimeType;
00717     m_access            = item.m_access;
00718     m_metaInfo          = item.m_metaInfo;
00719     for ( int i = 0; i < NumFlags; i++ )
00720         m_time[i] = item.m_time[i];
00721     m_size = item.m_size;
00722     // note: m_extra is NOT copied, as we'd have no control over who is
00723     // deleting the data or not.
00724 
00725     delete d; d = 0;    
00726     // We had a mimetype previously (probably), so we need to re-determine it
00727     determineMimeType();
00728     if (item.d) {
00729     d=new KFileItemPrivate;
00730     d->iconName=item.d->iconName;
00731     }
00732 }
00733 
00734 void KFileItem::setExtraData( const void *key, void *value )
00735 {
00736     if ( !key )
00737         return;
00738 
00739     m_extra.replace( key, value );
00740 }
00741 
00742 const void * KFileItem::extraData( const void *key ) const
00743 {
00744     QMapConstIterator<const void*,void*> it = m_extra.find( key );
00745     if ( it != m_extra.end() )
00746         return it.data();
00747     return 0L;
00748 }
00749 
00750 void * KFileItem::extraData( const void *key )
00751 {
00752     QMapIterator<const void*,void*> it = m_extra.find( key );
00753     if ( it != m_extra.end() )
00754         return it.data();
00755     return 0L;
00756 }
00757 
00758 void KFileItem::removeExtraData( const void *key )
00759 {
00760     m_extra.remove( key );
00761 }
00762 
00763 QString KFileItem::permissionsString() const
00764 {
00765     if (m_access.isNull())
00766       m_access = parsePermissions( m_permissions );
00767 
00768     return m_access;
00769 }
00770 
00771 QString KFileItem::parsePermissions(mode_t perm) const
00772 {
00773     char p[] = "----------";
00774 
00775     if (isDir())
00776     p[0]='d';
00777     else if (isLink())
00778     p[0]='l';
00779 
00780     if (perm & QFileInfo::ReadUser)
00781     p[1]='r';
00782     if (perm & QFileInfo::WriteUser)
00783     p[2]='w';
00784     if (perm & QFileInfo::ExeUser)
00785     p[3]='x';
00786 
00787     if (perm & QFileInfo::ReadGroup)
00788     p[4]='r';
00789     if (perm & QFileInfo::WriteGroup)
00790     p[5]='w';
00791     if (perm & QFileInfo::ExeGroup)
00792     p[6]='x';
00793 
00794     if (perm & QFileInfo::ReadOther)
00795     p[7]='r';
00796     if (perm & QFileInfo::WriteOther)
00797     p[8]='w';
00798     if (perm & QFileInfo::ExeOther)
00799     p[9]='x';
00800 
00801     return QString::fromLatin1(p);
00802 }
00803 
00804 // check if we need to cache this
00805 QString KFileItem::timeString( unsigned int which ) const
00806 {
00807     QDateTime t;
00808     t.setTime_t( time(which) );
00809     return KGlobal::locale()->formatDateTime( t );
00810 }
00811 
00812 void KFileItem::setMetaInfo( const KFileMetaInfo & info )
00813 {
00814     m_metaInfo = info;
00815 }
00816 
00817 const KFileMetaInfo & KFileItem::metaInfo(bool autoget, int) const
00818 {
00819     if ( autoget && !m_metaInfo.isValid() &&
00820          KGlobalSettings::showFilePreview(m_url) )
00821     {
00822         m_metaInfo = KFileMetaInfo( m_url, mimetype() );
00823     }
00824 
00825     return m_metaInfo;
00826 }
00827 
00828 void KFileItem::virtual_hook( int, void* )
00829 { /*BASE::virtual_hook( id, data );*/ }
00830 
00831 QDataStream & operator<< ( QDataStream & s, const KFileItem & a )
00832 {
00833     // We don't need to save/restore anything that refresh() invalidates,
00834     // since that means we can re-determine those by ourselves.
00835     s << a.m_url;
00836     s << a.m_strName;
00837     s << a.m_strText;
00838     return s;
00839 }
00840 
00841 QDataStream & operator>> ( QDataStream & s, KFileItem & a )
00842 {
00843     s >> a.m_url;
00844     s >> a.m_strName;
00845     s >> a.m_strText;
00846     a.m_bIsLocalURL = a.m_url.isLocalFile();
00847     a.m_bMimeTypeKnown = false;
00848     a.refresh();
00849     return s;
00850 }
KDE Logo
This file is part of the documentation for kio Library Version 3.3.90.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Mar 30 10:15:28 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003