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_user = QString::null;
00235   m_group = QString::null;
00236   m_access = QString::null;
00237   m_size = (KIO::filesize_t) -1;
00238   m_metaInfo = KFileMetaInfo();
00239   for ( int i = 0; i < NumFlags; i++ )
00240       m_time[i] = (time_t) -1;
00241 
00242   // Basically, we can't trust any information we got while listing.
00243   // Everything could have changed...
00244   // Clearing m_entry makes it possible to detect changes in the size of the file,
00245   // the time information, etc.
00246   m_entry = KIO::UDSEntry();
00247   init( false );
00248 }
00249 
00250 void KFileItem::refreshMimeType()
00251 {
00252   m_pMimeType = 0L;
00253   init( false ); // Will determine the mimetype
00254 }
00255 
00256 void KFileItem::setURL( const KURL &url )
00257 {
00258   m_url = url;
00259   setName( url.fileName() );
00260 }
00261 
00262 void KFileItem::setName( const QString& name )
00263 {
00264   m_strName = name;
00265   m_strText = KIO::decodeFileName( m_strName );
00266 }
00267 
00268 QString KFileItem::linkDest() const
00269 {
00270   // Extract it from the KIO::UDSEntry
00271   KIO::UDSEntry::ConstIterator it = m_entry.begin();
00272   for( ; it != m_entry.end(); it++ )
00273     if ( (*it).m_uds == KIO::UDS_LINK_DEST )
00274       return (*it).m_str;
00275   // If not in the KIO::UDSEntry, or if UDSEntry empty, use readlink() [if local URL]
00276   if ( m_bIsLocalURL )
00277   {
00278     char buf[1000];
00279     int n = readlink( QFile::encodeName(m_url.path( -1 )), buf, sizeof(buf)-1 );
00280     if ( n != -1 )
00281     {
00282       buf[ n ] = 0;
00283       return QFile::decodeName( buf );
00284     }
00285   }
00286   return QString::null;
00287 }
00288 
00289 KIO::filesize_t KFileItem::size() const
00290 {
00291   if ( m_size != (KIO::filesize_t) -1 )
00292     return m_size;
00293 
00294   // Extract it from the KIO::UDSEntry
00295   KIO::UDSEntry::ConstIterator it = m_entry.begin();
00296   for( ; it != m_entry.end(); it++ )
00297     if ( (*it).m_uds == KIO::UDS_SIZE ) {
00298       m_size = (*it).m_long;
00299       return m_size;
00300     }
00301   // If not in the KIO::UDSEntry, or if UDSEntry empty, use stat() [if local URL]
00302   if ( m_bIsLocalURL )
00303   {
00304     KDE_struct_stat buf;
00305     if ( KDE_stat( QFile::encodeName(m_url.path( -1 )), &buf ) == 0 )
00306         return buf.st_size;
00307   }
00308   return 0L;
00309 }
00310 
00311 time_t KFileItem::time( unsigned int which ) const
00312 {
00313   unsigned int mappedWhich = 0;
00314 
00315   switch( which ) {
00316     case KIO::UDS_MODIFICATION_TIME:
00317       mappedWhich = Modification;
00318       break;
00319     case KIO::UDS_ACCESS_TIME:
00320       mappedWhich = Access;
00321       break;
00322     case KIO::UDS_CREATION_TIME:
00323       mappedWhich = Creation;
00324       break;
00325   }
00326 
00327   if ( m_time[mappedWhich] != (time_t) -1 )
00328     return m_time[mappedWhich];
00329 
00330   // Extract it from the KIO::UDSEntry
00331   KIO::UDSEntry::ConstIterator it = m_entry.begin();
00332   for( ; it != m_entry.end(); it++ )
00333     if ( (*it).m_uds == which ) {
00334       m_time[mappedWhich] = static_cast<time_t>((*it).m_long);
00335       return m_time[mappedWhich];
00336     }
00337 
00338   // If not in the KIO::UDSEntry, or if UDSEntry empty, use stat() [if local URL]
00339   if ( m_bIsLocalURL )
00340   {
00341     KDE_struct_stat buf;
00342     if ( KDE_stat( QFile::encodeName(m_url.path(-1)), &buf ) == 0 )
00343     {
00344         m_time[mappedWhich] = (which == KIO::UDS_MODIFICATION_TIME) ?
00345                                buf.st_mtime :
00346                                (which == KIO::UDS_ACCESS_TIME) ? buf.st_atime :
00347                                static_cast<time_t>(0); // We can't determine creation time for local files
00348         return m_time[mappedWhich];
00349     }
00350   }
00351   return static_cast<time_t>(0);
00352 }
00353 
00354 
00355 QString KFileItem::user() const
00356 {
00357   if ( m_user.isEmpty() && m_bIsLocalURL )
00358   {
00359     KDE_struct_stat buff;
00360     if ( KDE_lstat( QFile::encodeName(m_url.path( -1 )), &buff ) == 0) // get uid/gid of the link, if it's a link
00361     {
00362       struct passwd *user = getpwuid( buff.st_uid );
00363       if ( user != 0L )
00364         m_user = QString::fromLocal8Bit(user->pw_name);
00365     }
00366   }
00367   return m_user;
00368 }
00369 
00370 QString KFileItem::group() const
00371 {
00372   if (m_group.isEmpty() && m_bIsLocalURL )
00373   {
00374     KDE_struct_stat buff;
00375     if ( KDE_lstat( QFile::encodeName(m_url.path( -1 )), &buff ) == 0) // get uid/gid of the link, if it's a link
00376     {
00377       struct group *ge = getgrgid( buff.st_gid );
00378       if ( ge != 0L ) {
00379         m_group = QString::fromLocal8Bit(ge->gr_name);
00380         if (m_group.isEmpty())
00381           m_group.sprintf("%d",ge->gr_gid);
00382       } else
00383         m_group.sprintf("%d",buff.st_gid);
00384     }
00385   }
00386   return m_group;
00387 }
00388 
00389 QString KFileItem::mimetype() const
00390 {
00391   KFileItem * that = const_cast<KFileItem *>(this);
00392   return that->determineMimeType()->name();
00393 }
00394 
00395 KMimeType::Ptr KFileItem::determineMimeType()
00396 {
00397   if ( !m_pMimeType || !m_bMimeTypeKnown )
00398   {
00399     m_pMimeType = KMimeType::findByURL( m_url, m_fileMode, m_bIsLocalURL );
00400     //kdDebug() << "finding mimetype for " << m_url.url() << " : " << m_pMimeType->name() << endl;
00401     m_bMimeTypeKnown = true;
00402   }
00403 
00404   return m_pMimeType;
00405 }
00406 
00407 bool KFileItem::isMimeTypeKnown() const
00408 {
00409   // The mimetype isn't known if determineMimeType was never called (on-demand determination)
00410   // or if this fileitem has a guessed mimetype (e.g. ftp symlink) - in which case
00411   // it always remains "not fully determined"
00412   return m_bMimeTypeKnown && m_guessedMimeType.isEmpty();
00413 }
00414 
00415 QString KFileItem::mimeComment()
00416 {
00417  KMimeType::Ptr mType = determineMimeType();
00418  QString comment = mType->comment( m_url, m_bIsLocalURL );
00419   if (!comment.isEmpty())
00420     return comment;
00421   else
00422     return mType->name();
00423 }
00424 
00425 QString KFileItem::iconName()
00426 {
00427   if (d && (!d->iconName.isEmpty())) return d->iconName;
00428   return determineMimeType()->icon(m_url, m_bIsLocalURL);
00429 }
00430 
00431 int KFileItem::overlays() const
00432 {
00433   int _state = 0;
00434   if ( m_bLink )
00435       _state |= KIcon::LinkOverlay;
00436 
00437   if ( !S_ISDIR( m_fileMode ) // Locked dirs have a special icon, use the overlay for files only
00438        && !isReadable())
00439      _state |= KIcon::LockOverlay;
00440 
00441   if ( isHidden() )
00442      _state |= KIcon::HiddenOverlay;
00443 
00444   if( S_ISDIR( m_fileMode ) && m_bIsLocalURL)
00445   {
00446     if (KSambaShare::instance()->isDirectoryShared( m_url.path() ) ||
00447         KNFSShare::instance()->isDirectoryShared( m_url.path() ))
00448     {
00449       //kdDebug()<<"KFileShare::isDirectoryShared : "<<m_url.path()<<endl;
00450       _state |= KIcon::ShareOverlay;
00451     }
00452   }
00453 
00454   if ( m_pMimeType->name() == "application/x-gzip" && m_url.fileName().right(3) == ".gz" )
00455      _state |= KIcon::ZipOverlay;
00456   return _state;
00457 }
00458 
00459 QPixmap KFileItem::pixmap( int _size, int _state ) const
00460 {
00461   if (d && (!d->iconName.isEmpty()))
00462      return DesktopIcon(d->iconName,_size,_state);
00463 
00464   if ( !m_pMimeType )
00465   {
00466     static const QString & defaultFolderIcon =
00467        KGlobal::staticQString(KMimeType::mimeType( "inode/directory" )->KServiceType::icon());
00468 
00469     if ( S_ISDIR( m_fileMode ) )
00470      return DesktopIcon( defaultFolderIcon, _size, _state );
00471 
00472     return DesktopIcon( "unknown", _size, _state );
00473   }
00474 
00475   _state |= overlays();
00476 
00477   KMimeType::Ptr mime;
00478   // Use guessed mimetype if the main one hasn't been determined for sure
00479   if ( !m_bMimeTypeKnown && !m_guessedMimeType.isEmpty() )
00480       mime = KMimeType::mimeType( m_guessedMimeType );
00481   else
00482       mime = m_pMimeType;
00483 
00484   // Support for gzipped files: extract mimetype of contained file
00485   // See also the relevant code in overlays, which adds the zip overlay.
00486   if ( mime->name() == "application/x-gzip" && m_url.fileName().right(3) == ".gz" )
00487   {
00488       KURL sf;
00489       sf.setPath( m_url.path().left( m_url.path().length() - 3 ) );
00490       //kdDebug() << "KFileItem::pixmap subFileName=" << subFileName << endl;
00491       mime = KMimeType::findByURL( sf, 0, m_bIsLocalURL );
00492   }
00493 
00494   QPixmap p = mime->pixmap( m_url, KIcon::Desktop, _size, _state );
00495   if (p.isNull())
00496       kdWarning() << "Pixmap not found for mimetype " << m_pMimeType->name() << endl;
00497 
00498   return p;
00499 }
00500 
00501 bool KFileItem::isReadable() const
00502 {
00503   /*
00504   struct passwd * user = getpwuid( geteuid() );
00505   bool isMyFile = (QString::fromLocal8Bit(user->pw_name) == m_user);
00506   // This gets ugly for the group....
00507   // Maybe we want a static QString for the user and a static QStringList
00508   // for the groups... then we need to handle the deletion properly...
00509   */
00510 
00511   // No read permission at all
00512   if ( !(S_IRUSR & m_permissions) && !(S_IRGRP & m_permissions) && !(S_IROTH & m_permissions) )
00513       return false;
00514 
00515   // Or if we can't read it [using ::access()] - not network transparent
00516   else if ( m_bIsLocalURL && ::access( QFile::encodeName(m_url.path()), R_OK ) == -1 )
00517       return false;
00518 
00519   return true;
00520 }
00521 
00522 bool KFileItem::isHidden() const
00523 {
00524   if ( !m_url.isEmpty() )
00525       return m_url.fileName()[0] == '.';
00526   else // should never happen
00527       return m_strName[0] == '.';
00528 }
00529 
00530 bool KFileItem::isDir() const
00531 {
00532   if ( m_fileMode == KFileItem::Unknown )
00533   {
00534     kdDebug() << " KFileItem::isDir can't say -> false " << endl;
00535     return false; // can't say for sure, so no
00536   }
00537   return S_ISDIR(m_fileMode);
00538 }
00539 
00540 bool KFileItem::acceptsDrops()
00541 {
00542   // A directory ?
00543   if ( S_ISDIR( mode() ) )
00544   {
00545     if ( m_bIsLocalURL ) // local -> check if we can enter it
00546        return (::access( QFile::encodeName(m_url.path()), X_OK ) == 0);
00547     else
00548        return true; // assume ok for remote urls
00549   }
00550 
00551   // But only local .desktop files and executables
00552   if ( !m_bIsLocalURL )
00553     return false;
00554 
00555   if ( mimetype() == "application/x-desktop")
00556     return true;
00557 
00558   // Executable, shell script ... ?
00559   if ( ::access( QFile::encodeName(m_url.path()), X_OK ) == 0 )
00560     return true;
00561 
00562   return false;
00563 }
00564 
00565 QString KFileItem::getStatusBarInfo()
00566 {
00567   QString comment = determineMimeType()->comment( m_url, m_bIsLocalURL );
00568   QString text = m_strText;
00569   // Extract from the KIO::UDSEntry the additional info we didn't get previously
00570   QString myLinkDest = linkDest();
00571   KIO::filesize_t mySize = size();
00572 
00573   if ( m_bLink )
00574   {
00575       QString tmp;
00576       if ( comment.isEmpty() )
00577         tmp = i18n ( "Symbolic Link" );
00578       else
00579         tmp = i18n("%1 (Link)").arg(comment);
00580       text += "->";
00581       text += myLinkDest;
00582       text += "  ";
00583       text += tmp;
00584   }
00585   else if ( S_ISREG( m_fileMode ) )
00586   {
00587       text += QString(" (%1)").arg( KIO::convertSize( mySize ) );
00588       text += "  ";
00589       text += comment;
00590   }
00591   else if ( S_ISDIR ( m_fileMode ) )
00592   {
00593       text += "/  ";
00594       text += comment;
00595     }
00596     else
00597     {
00598       text += "  ";
00599       text += comment;
00600     }
00601     return text;
00602 }
00603 
00604 QString KFileItem::getToolTipText(int maxcount)
00605 {
00606   // we can return QString::null if no tool tip should be shown
00607   QString tip;
00608   KFileMetaInfo info = metaInfo();
00609 
00610   // the font tags are a workaround for the fact that the tool tip gets
00611   // screwed if the color scheme uses white as default text color
00612   const char* start = "<tr><td><nobr><font color=\"black\">";
00613   const char* mid   = "</font></nobr></td><td><nobr><font color=\"black\">";
00614   const char* end   = "</font></nobr></td></tr>";
00615 
00616   tip = "<table cellspacing=0 cellpadding=0>";
00617 
00618   tip += start + i18n("Type:") + mid;
00619 
00620   QString type = QStyleSheet::escape(determineMimeType()->comment());
00621   if ( m_bLink ) {
00622    tip += i18n("Link to %1 (%2)").arg(linkDest(), type) + end;
00623   } else
00624     tip += type + end;
00625 
00626   if ( !S_ISDIR ( m_fileMode ) )
00627     tip += start + i18n("Size:") + mid +
00628            KIO::convertSize( size() ) + end;
00629 
00630   tip += start + i18n("Modified:") + mid +
00631          timeString( KIO::UDS_MODIFICATION_TIME) + end +
00632          start + i18n("Owner:") + mid + user() + " - " + group() + end +
00633          start + i18n("Permissions:") + mid +
00634          parsePermissions(m_permissions) + end;
00635 
00636   if (info.isValid() && !info.isEmpty() )
00637   {
00638     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>";
00639     QStringList keys = info.preferredKeys();
00640 
00641     // now the rest
00642     QStringList::Iterator it = keys.begin();
00643     for (int count = 0; count<maxcount && it!=keys.end() ; ++it)
00644     {
00645       KFileMetaInfoItem item = info.item( *it );
00646       if ( item.isValid() )
00647       {
00648         QString s = item.string();
00649         if (s.length() > 50) {
00650           s.truncate(47);
00651           s.append("...");
00652         }
00653         if ( !s.isEmpty() )
00654         {
00655           count++;
00656           tip += start +
00657                    QStyleSheet::escape( item.translatedKey() ) + ":" +
00658                  mid +
00659                    QStyleSheet::escape( s ) +
00660                  end;
00661         }
00662 
00663       }
00664     }
00665     tip += "</table>";
00666   }
00667 
00668   //kdDebug() << "making this the tool tip rich text:\n";
00669   //kdDebug() << tip << endl;
00670 
00671   return tip;
00672 }
00673 
00674 void KFileItem::run()
00675 {
00676   KURL url( m_url );
00677   // When clicking on a link to e.g. $HOME from the desktop, we want to open $HOME
00678   // But when following a link on the FTP site, the target be an absolute path
00679   // that doesn't work in the URL. So we resolve links only on the local filesystem.
00680   if ( m_bLink && m_bIsLocalURL )
00681     url = KURL( m_url, linkDest() );
00682   (void) new KRun( url, m_fileMode, m_bIsLocalURL );
00683 }
00684 
00685 bool KFileItem::cmp( const KFileItem & item )
00686 {
00687     return ( m_strName == item.m_strName
00688              && m_bIsLocalURL == item.m_bIsLocalURL
00689              && m_fileMode == item.m_fileMode
00690              && m_permissions == item.m_permissions
00691              && m_user == item.m_user
00692              && m_group == item.m_group
00693              && m_bLink == item.m_bLink
00694              && size() == item.size()
00695              && time(KIO::UDS_MODIFICATION_TIME) == item.time(KIO::UDS_MODIFICATION_TIME) );
00696 }
00697 
00698 void KFileItem::assign( const KFileItem & item )
00699 {
00700     m_entry = item.m_entry;
00701     m_url = item.m_url;
00702     m_bIsLocalURL = item.m_bIsLocalURL;
00703     m_strName = item.m_strName;
00704     m_strText = item.m_strText;
00705     m_fileMode = item.m_fileMode;
00706     m_permissions = item.m_permissions;
00707     m_user = item.m_user;
00708     m_group = item.m_group;
00709     m_bLink = item.m_bLink;
00710     m_pMimeType = item.m_pMimeType;
00711     m_strLowerCaseName = item.m_strLowerCaseName;
00712     m_bMimeTypeKnown = item.m_bMimeTypeKnown;
00713     m_guessedMimeType   = item.m_guessedMimeType;
00714     m_access            = item.m_access;
00715     m_metaInfo          = item.m_metaInfo;
00716     for ( int i = 0; i < NumFlags; i++ )
00717         m_time[i] = item.m_time[i];
00718     m_size = item.m_size;
00719     // note: m_extra is NOT copied, as we'd have no control over who is
00720     // deleting the data or not.
00721 
00722     // We had a mimetype previously (probably), so we need to re-determine it
00723     determineMimeType();
00724     if (item.d) {
00725     d=new KFileItemPrivate;
00726     d->iconName=item.d->iconName;
00727     }
00728 }
00729 
00730 void KFileItem::setExtraData( const void *key, void *value )
00731 {
00732     if ( !key )
00733         return;
00734 
00735     m_extra.replace( key, value );
00736 }
00737 
00738 const void * KFileItem::extraData( const void *key ) const
00739 {
00740     QMapConstIterator<const void*,void*> it = m_extra.find( key );
00741     if ( it != m_extra.end() )
00742         return it.data();
00743     return 0L;
00744 }
00745 
00746 void * KFileItem::extraData( const void *key )
00747 {
00748     QMapIterator<const void*,void*> it = m_extra.find( key );
00749     if ( it != m_extra.end() )
00750         return it.data();
00751     return 0L;
00752 }
00753 
00754 void KFileItem::removeExtraData( const void *key )
00755 {
00756     m_extra.remove( key );
00757 }
00758 
00759 QString KFileItem::permissionsString() const
00760 {
00761     if (m_access.isNull())
00762       m_access = parsePermissions( m_permissions );
00763 
00764     return m_access;
00765 }
00766 
00767 QString KFileItem::parsePermissions(mode_t perm) const
00768 {
00769     char p[] = "----------";
00770 
00771     if (isDir())
00772     p[0]='d';
00773     else if (isLink())
00774     p[0]='l';
00775 
00776     if (perm & QFileInfo::ReadUser)
00777     p[1]='r';
00778     if (perm & QFileInfo::WriteUser)
00779     p[2]='w';
00780     if (perm & QFileInfo::ExeUser)
00781     p[3]='x';
00782 
00783     if (perm & QFileInfo::ReadGroup)
00784     p[4]='r';
00785     if (perm & QFileInfo::WriteGroup)
00786     p[5]='w';
00787     if (perm & QFileInfo::ExeGroup)
00788     p[6]='x';
00789 
00790     if (perm & QFileInfo::ReadOther)
00791     p[7]='r';
00792     if (perm & QFileInfo::WriteOther)
00793     p[8]='w';
00794     if (perm & QFileInfo::ExeOther)
00795     p[9]='x';
00796 
00797     return QString::fromLatin1(p);
00798 }
00799 
00800 // check if we need to cache this
00801 QString KFileItem::timeString( unsigned int which ) const
00802 {
00803     QDateTime t;
00804     t.setTime_t( time(which) );
00805     return KGlobal::locale()->formatDateTime( t );
00806 }
00807 
00808 void KFileItem::setMetaInfo( const KFileMetaInfo & info )
00809 {
00810     m_metaInfo = info;
00811 }
00812 
00813 const KFileMetaInfo & KFileItem::metaInfo(bool autoget, int) const
00814 {
00815     if ( autoget && !m_metaInfo.isValid() &&
00816          KGlobalSettings::showFilePreview(m_url) )
00817     {
00818         m_metaInfo = KFileMetaInfo( m_url, mimetype() );
00819     }
00820 
00821     return m_metaInfo;
00822 }
00823 
00824 void KFileItem::virtual_hook( int, void* )
00825 { /*BASE::virtual_hook( id, data );*/ }
00826 
00827 QDataStream & operator<< ( QDataStream & s, const KFileItem & a )
00828 {
00829     // We don't need to save/restore anything that refresh() invalidates,
00830     // since that means we can re-determine those by ourselves.
00831     s << a.m_url;
00832     s << a.m_strName;
00833     s << a.m_strText;
00834     return s;
00835 }
00836 
00837 QDataStream & operator>> ( QDataStream & s, KFileItem & a )
00838 {
00839     s >> a.m_url;
00840     s >> a.m_strName;
00841     s >> a.m_strText;
00842     a.m_bIsLocalURL = a.m_url.isLocalFile();
00843     a.m_bMimeTypeKnown = false;
00844     a.refresh();
00845     return s;
00846 }
KDE Logo
This file is part of the documentation for kio Library Version 3.3.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Feb 18 15:10:42 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003