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