• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • kio/kio
 

kio/kio

  • kio
  • kio
kfileitem.cpp
1 /* This file is part of the KDE project
2  Copyright (C) 1999 David Faure <faure@kde.org>
3  2001 Carsten Pfeiffer <pfeiffer@kde.org>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 // $Id$
21 
22 #include <sys/time.h>
23 #include <pwd.h>
24 #include <grp.h>
25 #include <sys/types.h>
26 
27 #include <assert.h>
28 #include <unistd.h>
29 
30 #include "kfileitem.h"
31 
32 #include <tqdir.h>
33 #include <tqfile.h>
34 #include <tqmap.h>
35 #include <tqstylesheet.h>
36 
37 #include <kdebug.h>
38 #include <kfilemetainfo.h>
39 #include <ksambashare.h>
40 #include <knfsshare.h>
41 #include <kglobal.h>
42 #include <kglobalsettings.h>
43 #include <kiconloader.h>
44 #include <klargefile.h>
45 #include <klocale.h>
46 #include <kmimetype.h>
47 #include <krun.h>
48 
49 class KFileItem::KFileItemPrivate {
50  public:
51  TQString iconName;
52 };
53 
54 KFileItem::KFileItem( const KIO::UDSEntry& _entry, const KURL& _url,
55  bool _determineMimeTypeOnDemand, bool _urlIsDirectory ) :
56  m_entry( _entry ),
57  m_url( _url ),
58  m_pMimeType( 0 ),
59  m_fileMode( KFileItem::Unknown ),
60  m_permissions( KFileItem::Unknown ),
61  m_bMarked( false ),
62  m_bLink( false ),
63  m_bIsLocalURL( _url.isLocalFile() ),
64  m_bMimeTypeKnown( false ),
65  m_hidden( Auto ),
66  d(0)
67 {
68  readUDSEntry( _urlIsDirectory );
69  init( _determineMimeTypeOnDemand );
70 }
71 
72 KFileItem::KFileItem( mode_t _mode, mode_t _permissions, const KURL& _url, bool _determineMimeTypeOnDemand ) :
73  m_entry(), // warning !
74  m_url( _url ),
75  m_strName( _url.fileName() ),
76  m_strText( KIO::decodeFileName( m_strName ) ),
77  m_pMimeType( 0 ),
78  m_fileMode ( _mode ),
79  m_permissions( _permissions ),
80  m_bMarked( false ),
81  m_bLink( false ),
82  m_bIsLocalURL( _url.isLocalFile() ),
83  m_bMimeTypeKnown( false ),
84  m_hidden( Auto ),
85  d(0)
86 {
87  init( _determineMimeTypeOnDemand );
88 }
89 
90 KFileItem::KFileItem( const KURL &url, const TQString &mimeType, mode_t mode )
91 : m_url( url ),
92  m_strName( url.fileName() ),
93  m_strText( KIO::decodeFileName( m_strName ) ),
94  m_pMimeType( 0 ),
95  m_fileMode( mode ),
96  m_permissions( KFileItem::Unknown ),
97  m_bMarked( false ),
98  m_bLink( false ),
99  m_bIsLocalURL( url.isLocalFile() ),
100  m_bMimeTypeKnown( !mimeType.isEmpty() ),
101  m_hidden( Auto ),
102  d(0)
103 {
104  if (m_bMimeTypeKnown)
105  m_pMimeType = KMimeType::mimeType( mimeType );
106 
107  init( false );
108 }
109 
110 KFileItem::KFileItem( const KFileItem & item ) :
111  d(0)
112 {
113  assign( item );
114 }
115 
116 KFileItem& KFileItem::operator=( const KFileItem & item )
117 {
118  assign( item );
119  return *this;
120 }
121 
122 KFileItem::~KFileItem()
123 {
124  delete d;
125 }
126 
127 void KFileItem::init( bool _determineMimeTypeOnDemand )
128 {
129  m_access = TQString::null;
130  m_size = (KIO::filesize_t) -1;
131  // metaInfo = KFileMetaInfo();
132  for ( int i = 0; i < NumFlags; i++ )
133  m_time[i] = (time_t) -1;
134 
135  // determine mode and/or permissions if unknown
136  if ( m_fileMode == KFileItem::Unknown || m_permissions == KFileItem::Unknown )
137  {
138  mode_t mode = 0;
139  if ( m_url.isLocalFile() )
140  {
141  /* directories may not have a slash at the end if
142  * we want to stat() them; it requires that we
143  * change into it .. which may not be allowed
144  * stat("/is/unaccessible") -> rwx------
145  * stat("/is/unaccessible/") -> EPERM H.Z.
146  * This is the reason for the -1
147  */
148  KDE_struct_stat buf;
149  TQCString path = TQFile::encodeName(m_url.path( -1 ));
150  if ( KDE_lstat( path.data(), &buf ) == 0 )
151  {
152  mode = buf.st_mode;
153  if ( S_ISLNK( mode ) )
154  {
155  m_bLink = true;
156  if ( KDE_stat( path.data(), &buf ) == 0 )
157  mode = buf.st_mode;
158  else // link pointing to nowhere (see kio/file/file.cc)
159  mode = (S_IFMT-1) | S_IRWXU | S_IRWXG | S_IRWXO;
160  }
161  // While we're at it, store the times
162  m_time[ Modification ] = buf.st_mtime;
163  m_time[ Access ] = buf.st_atime;
164  if ( m_fileMode == KFileItem::Unknown )
165  m_fileMode = mode & S_IFMT; // extract file type
166  if ( m_permissions == KFileItem::Unknown )
167  m_permissions = mode & 07777; // extract permissions
168  }
169  }
170  }
171 
172  // determine the mimetype
173  if (!m_pMimeType && !m_url.isEmpty())
174  {
175  bool accurate = false;
176  bool isLocalURL;
177  KURL url = mostLocalURL(isLocalURL);
178 
179  m_pMimeType = KMimeType::findByURL( url, m_fileMode, isLocalURL,
180  // use fast mode if not mimetype on demand
181  _determineMimeTypeOnDemand, &accurate );
182  //kdDebug() << "finding mimetype for " << url.url() << " : " << m_pMimeType->name() << endl;
183  // if we didn't use fast mode, or if we got a result, then this is the mimetype
184  // otherwise, determineMimeType will be able to do better.
185  m_bMimeTypeKnown = (!_determineMimeTypeOnDemand) || accurate;
186  }
187 }
188 
189 void KFileItem::readUDSEntry( bool _urlIsDirectory )
190 {
191  // extract the mode and the filename from the KIO::UDS Entry
192  bool UDS_URL_seen = false;
193 
194  if (&m_entry == NULL) return;
195 
196  KIO::UDSEntry::ConstIterator it = m_entry.begin();
197  for( ; it != m_entry.end(); ++it ) {
198  switch ((*it).m_uds) {
199 
200  case KIO::UDS_FILE_TYPE:
201  m_fileMode = (mode_t)((*it).m_long);
202  break;
203 
204  case KIO::UDS_ACCESS:
205  m_permissions = (mode_t)((*it).m_long);
206  break;
207 
208  case KIO::UDS_USER:
209  m_user = ((*it).m_str);
210  break;
211 
212  case KIO::UDS_GROUP:
213  m_group = ((*it).m_str);
214  break;
215 
216  case KIO::UDS_NAME:
217  m_strName = (*it).m_str;
218  m_strText = KIO::decodeFileName( m_strName );
219  break;
220 
221  case KIO::UDS_URL:
222  UDS_URL_seen = true;
223  m_url = KURL((*it).m_str);
224  if ( m_url.isLocalFile() )
225  m_bIsLocalURL = true;
226  break;
227 
228  case KIO::UDS_MIME_TYPE:
229  m_pMimeType = KMimeType::mimeType((*it).m_str);
230  m_bMimeTypeKnown = true;
231  break;
232 
233  case KIO::UDS_GUESSED_MIME_TYPE:
234  m_guessedMimeType = (*it).m_str;
235  break;
236 
237  case KIO::UDS_LINK_DEST:
238  m_bLink = !(*it).m_str.isEmpty(); // we don't store the link dest
239  break;
240 
241  case KIO::UDS_ICON_NAME:
242  if ( !d )
243  d = new KFileItemPrivate();
244  d->iconName = (*it).m_str;
245  break;
246 
247  case KIO::UDS_HIDDEN:
248  if ( (*it).m_long )
249  m_hidden = Hidden;
250  else
251  m_hidden = Shown;
252  break;
253  }
254  }
255 
256  // avoid creating these QStrings again and again
257  static const TQString& dot = KGlobal::staticQString(".");
258  if ( _urlIsDirectory && !UDS_URL_seen && !m_strName.isEmpty() && m_strName != dot )
259  m_url.addPath( m_strName );
260 }
261 
262 void KFileItem::refresh()
263 {
264  m_fileMode = KFileItem::Unknown;
265  m_permissions = KFileItem::Unknown;
266  m_pMimeType = 0L;
267  m_user = TQString::null;
268  m_group = TQString::null;
269  m_metaInfo = KFileMetaInfo();
270  m_hidden = Auto;
271 
272  // Basically, we can't trust any information we got while listing.
273  // Everything could have changed...
274  // Clearing m_entry makes it possible to detect changes in the size of the file,
275  // the time information, etc.
276  m_entry = KIO::UDSEntry();
277  init( false );
278 }
279 
280 void KFileItem::refreshMimeType()
281 {
282  m_pMimeType = 0L;
283  init( false ); // Will determine the mimetype
284 }
285 
286 void KFileItem::setURL( const KURL &url )
287 {
288  m_url = url;
289  setName( url.fileName() );
290 }
291 
292 void KFileItem::setName( const TQString& name )
293 {
294  m_strName = name;
295  m_strText = KIO::decodeFileName( m_strName );
296 }
297 
298 TQString KFileItem::linkDest() const
299 {
300  if (&m_entry == NULL) return TQString::null;
301 
302  // Extract it from the KIO::UDSEntry
303  KIO::UDSEntry::ConstIterator it = m_entry.begin();
304  for( ; it != m_entry.end(); ++it )
305  if ( (*it).m_uds == KIO::UDS_LINK_DEST )
306  return (*it).m_str;
307  // If not in the KIO::UDSEntry, or if UDSEntry empty, use readlink() [if local URL]
308  if ( m_bIsLocalURL )
309  {
310  char buf[1000];
311  int n = readlink( TQFile::encodeName(m_url.path( -1 )), buf, sizeof(buf)-1 );
312  if ( n != -1 )
313  {
314  buf[ n ] = 0;
315  return TQFile::decodeName( buf );
316  }
317  }
318  return TQString::null;
319 }
320 
321 TQString KFileItem::localPath() const
322 {
323  if ( m_bIsLocalURL )
324  {
325  return m_url.path();
326  }
327  else
328  {
329  if (&m_entry == NULL) return TQString::null;
330 
331  // Extract the local path from the KIO::UDSEntry
332  KIO::UDSEntry::ConstIterator it = m_entry.begin();
333  const KIO::UDSEntry::ConstIterator end = m_entry.end();
334  for( ; it != end; ++it )
335  if ( (*it).m_uds == KIO::UDS_LOCAL_PATH )
336  return (*it).m_str;
337  }
338 
339  return TQString::null;
340 }
341 
342 KIO::filesize_t KFileItem::size(bool &exists) const
343 {
344  exists = true;
345  if ( m_size != (KIO::filesize_t) -1 )
346  return m_size;
347 
348  if (&m_entry == NULL) return 0L;
349 
350  // Extract it from the KIO::UDSEntry
351  KIO::UDSEntry::ConstIterator it = m_entry.begin();
352  for( ; it != m_entry.end(); ++it )
353  if ( (*it).m_uds == KIO::UDS_SIZE ) {
354  m_size = (*it).m_long;
355  return m_size;
356  }
357  // If not in the KIO::UDSEntry, or if UDSEntry empty, use stat() [if local URL]
358  if ( m_bIsLocalURL )
359  {
360  KDE_struct_stat buf;
361  if ( KDE_stat( TQFile::encodeName(m_url.path( -1 )), &buf ) == 0 )
362  return buf.st_size;
363  }
364  exists = false;
365  return 0L;
366 }
367 
368 bool KFileItem::hasExtendedACL() const
369 {
370  if (&m_entry == NULL) return false;
371  KIO::UDSEntry::ConstIterator it = m_entry.begin();
372  for( ; it != m_entry.end(); it++ )
373  if ( (*it).m_uds == KIO::UDS_EXTENDED_ACL ) {
374  return true;
375  }
376  return false;
377 }
378 
379 KACL KFileItem::ACL() const
380 {
381  if ( hasExtendedACL() ) {
382  if (&m_entry == NULL) return KACL( m_permissions );
383 
384  // Extract it from the KIO::UDSEntry
385  KIO::UDSEntry::ConstIterator it = m_entry.begin();
386  for( ; it != m_entry.end(); ++it )
387  if ( (*it).m_uds == KIO::UDS_ACL_STRING )
388  return KACL((*it).m_str);
389  }
390  // create one from the basic permissions
391  return KACL( m_permissions );
392 }
393 
394 KACL KFileItem::defaultACL() const
395 {
396  if (&m_entry == NULL) return KACL();
397 
398  // Extract it from the KIO::UDSEntry
399  KIO::UDSEntry::ConstIterator it = m_entry.begin();
400  for( ; it != m_entry.end(); ++it )
401  if ( (*it).m_uds == KIO::UDS_DEFAULT_ACL_STRING )
402  return KACL((*it).m_str);
403  return KACL();
404 }
405 
406 KIO::filesize_t KFileItem::size() const
407 {
408  bool exists;
409  return size(exists);
410 }
411 
412 time_t KFileItem::time( unsigned int which ) const
413 {
414  bool hasTime;
415  return time(which, hasTime);
416 }
417 time_t KFileItem::time( unsigned int which, bool &hasTime ) const
418 {
419  hasTime = true;
420  unsigned int mappedWhich = 0;
421 
422  switch( which ) {
423  case KIO::UDS_MODIFICATION_TIME:
424  mappedWhich = Modification;
425  break;
426  case KIO::UDS_ACCESS_TIME:
427  mappedWhich = Access;
428  break;
429  case KIO::UDS_CREATION_TIME:
430  mappedWhich = Creation;
431  break;
432  }
433 
434  if ( m_time[mappedWhich] != (time_t) -1 )
435  return m_time[mappedWhich];
436 
437  if (&m_entry == NULL) return static_cast<time_t>(0);
438 
439  // Extract it from the KIO::UDSEntry
440  KIO::UDSEntry::ConstIterator it = m_entry.begin();
441  for( ; it != m_entry.end(); ++it )
442  if ( (*it).m_uds == which ) {
443  m_time[mappedWhich] = static_cast<time_t>((*it).m_long);
444  return m_time[mappedWhich];
445  }
446 
447  // If not in the KIO::UDSEntry, or if UDSEntry empty, use stat() [if local URL]
448  if ( m_bIsLocalURL )
449  {
450  KDE_struct_stat buf;
451  if ( KDE_stat( TQFile::encodeName(m_url.path(-1)), &buf ) == 0 )
452  {
453  if(which == KIO::UDS_CREATION_TIME) {
454  // We can't determine creation time for local files
455  hasTime = false;
456  m_time[mappedWhich] = static_cast<time_t>(0);
457  return m_time[mappedWhich];
458  }
459  m_time[mappedWhich] = (which == KIO::UDS_MODIFICATION_TIME) ?
460  buf.st_mtime :
461  /* which == KIO::UDS_ACCESS_TIME)*/
462  buf.st_atime;
463  return m_time[mappedWhich];
464  }
465  }
466  hasTime = false;
467  return static_cast<time_t>(0);
468 }
469 
470 
471 TQString KFileItem::user() const
472 {
473  if ( m_user.isEmpty() && m_bIsLocalURL )
474  {
475  KDE_struct_stat buff;
476  if ( KDE_lstat( TQFile::encodeName(m_url.path( -1 )), &buff ) == 0) // get uid/gid of the link, if it's a link
477  {
478  struct passwd *user = getpwuid( buff.st_uid );
479  if ( user != 0L )
480  m_user = TQString::fromLocal8Bit(user->pw_name);
481  }
482  }
483  return m_user;
484 }
485 
486 TQString KFileItem::group() const
487 {
488 #ifdef Q_OS_UNIX
489  if (m_group.isEmpty() && m_bIsLocalURL )
490  {
491  KDE_struct_stat buff;
492  if ( KDE_lstat( TQFile::encodeName(m_url.path( -1 )), &buff ) == 0) // get uid/gid of the link, if it's a link
493  {
494  struct group *ge = getgrgid( buff.st_gid );
495  if ( ge != 0L ) {
496  m_group = TQString::fromLocal8Bit(ge->gr_name);
497  if (m_group.isEmpty())
498  m_group.sprintf("%d",ge->gr_gid);
499  } else
500  m_group.sprintf("%d",buff.st_gid);
501  }
502  }
503 #endif
504  return m_group;
505 }
506 
507 TQString KFileItem::mimetype() const
508 {
509  KFileItem * that = const_cast<KFileItem *>(this);
510  return that->determineMimeType()->name();
511 }
512 
513 KMimeType::Ptr KFileItem::determineMimeType()
514 {
515  if ( !m_pMimeType || !m_bMimeTypeKnown )
516  {
517  bool isLocalURL;
518  KURL url = mostLocalURL(isLocalURL);
519 
520  m_pMimeType = KMimeType::findByURL( url, m_fileMode, isLocalURL );
521  //kdDebug() << "finding mimetype for " << url.url() << " : " << m_pMimeType->name() << endl;
522  m_bMimeTypeKnown = true;
523  }
524 
525  return m_pMimeType;
526 }
527 
528 bool KFileItem::isMimeTypeKnown() const
529 {
530  // The mimetype isn't known if determineMimeType was never called (on-demand determination)
531  // or if this fileitem has a guessed mimetype (e.g. ftp symlink) - in which case
532  // it always remains "not fully determined"
533  return m_bMimeTypeKnown && m_guessedMimeType.isEmpty();
534 }
535 
536 TQString KFileItem::mimeComment()
537 {
538  KMimeType::Ptr mType = determineMimeType();
539 
540  bool isLocalURL;
541  KURL url = mostLocalURL(isLocalURL);
542 
543  TQString comment = mType->comment( url, isLocalURL );
544  //kdDebug() << "finding comment for " << url.url() << " : " << m_pMimeType->name() << endl;
545  if (!comment.isEmpty())
546  return comment;
547  else
548  return mType->name();
549 }
550 
551 TQString KFileItem::iconName()
552 {
553  if (d && (!d->iconName.isEmpty())) return d->iconName;
554 
555  bool isLocalURL;
556  KURL url = mostLocalURL(isLocalURL);
557 
558  //kdDebug() << "finding icon for " << url.url() << " : " << m_pMimeType->name() << endl;
559  return determineMimeType()->icon(url, isLocalURL);
560 }
561 
562 int KFileItem::overlays() const
563 {
564  int _state = 0;
565  if ( m_bLink )
566  _state |= KIcon::LinkOverlay;
567 
568  if ( !S_ISDIR( m_fileMode ) // Locked dirs have a special icon, use the overlay for files only
569  && !isReadable())
570  _state |= KIcon::LockOverlay;
571 
572  if ( isHidden() )
573  _state |= KIcon::HiddenOverlay;
574 
575  if( S_ISDIR( m_fileMode ) && m_bIsLocalURL)
576  {
577  if (KSambaShare::instance()->isDirectoryShared( m_url.path() ) ||
578  KNFSShare::instance()->isDirectoryShared( m_url.path() ))
579  {
580  //kdDebug()<<"KFileShare::isDirectoryShared : "<<m_url.path()<<endl;
581  _state |= KIcon::ShareOverlay;
582  }
583  }
584 
585  if ( m_pMimeType->name() == "application/x-gzip" && m_url.fileName().right(3) == ".gz" )
586  _state |= KIcon::ZipOverlay;
587  return _state;
588 }
589 
590 TQPixmap KFileItem::pixmap( int _size, int _state ) const
591 {
592  if (d && (!d->iconName.isEmpty()))
593  return DesktopIcon(d->iconName,_size,_state);
594 
595  if ( !m_pMimeType )
596  {
597  static const TQString & defaultFolderIcon =
598  KGlobal::staticQString(KMimeType::mimeType( "inode/directory" )->KServiceType::icon());
599 
600  if ( S_ISDIR( m_fileMode ) )
601  return DesktopIcon( defaultFolderIcon, _size, _state );
602 
603  return DesktopIcon( "unknown", _size, _state );
604  }
605 
606  _state |= overlays();
607 
608  KMimeType::Ptr mime;
609  // Use guessed mimetype if the main one hasn't been determined for sure
610  if ( !m_bMimeTypeKnown && !m_guessedMimeType.isEmpty() )
611  mime = KMimeType::mimeType( m_guessedMimeType );
612  else
613  mime = m_pMimeType;
614 
615  // Support for gzipped files: extract mimetype of contained file
616  // See also the relevant code in overlays, which adds the zip overlay.
617  if ( mime->name() == "application/x-gzip" && m_url.fileName().right(3) == ".gz" )
618  {
619  KURL sf;
620  sf.setPath( m_url.path().left( m_url.path().length() - 3 ) );
621  //kdDebug() << "KFileItem::pixmap subFileName=" << subFileName << endl;
622  mime = KMimeType::findByURL( sf, 0, m_bIsLocalURL );
623  }
624 
625  bool isLocalURL;
626  KURL url = mostLocalURL(isLocalURL);
627 
628  TQPixmap p = mime->pixmap( url, KIcon::Desktop, _size, _state );
629  //kdDebug() << "finding pixmap for " << url.url() << " : " << mime->name() << endl;
630  if (p.isNull())
631  kdWarning() << "Pixmap not found for mimetype " << m_pMimeType->name() << endl;
632 
633  return p;
634 }
635 
636 bool KFileItem::isReadable() const
637 {
638  /*
639  struct passwd * user = getpwuid( geteuid() );
640  bool isMyFile = (TQString::fromLocal8Bit(user->pw_name) == m_user);
641  // This gets ugly for the group....
642  // Maybe we want a static TQString for the user and a static QStringList
643  // for the groups... then we need to handle the deletion properly...
644  */
645 
646  if ( m_permissions != KFileItem::Unknown ) {
647  // No read permission at all
648  if ( !(S_IRUSR & m_permissions) && !(S_IRGRP & m_permissions) && !(S_IROTH & m_permissions) )
649  return false;
650 
651  // Read permissions for all: save a stat call
652  if ( (S_IRUSR|S_IRGRP|S_IROTH) & m_permissions )
653  return true;
654  }
655 
656  // Or if we can't read it [using ::access()] - not network transparent
657  if ( m_bIsLocalURL && ::access( TQFile::encodeName(m_url.path()), R_OK ) == -1 )
658  return false;
659 
660  return true;
661 }
662 
663 bool KFileItem::isWritable() const
664 {
665  /*
666  struct passwd * user = getpwuid( geteuid() );
667  bool isMyFile = (TQString::fromLocal8Bit(user->pw_name) == m_user);
668  // This gets ugly for the group....
669  // Maybe we want a static TQString for the user and a static QStringList
670  // for the groups... then we need to handle the deletion properly...
671  */
672 
673  if ( m_permissions != KFileItem::Unknown ) {
674  // No write permission at all
675  if ( !(S_IWUSR & m_permissions) && !(S_IWGRP & m_permissions) && !(S_IWOTH & m_permissions) )
676  return false;
677  }
678 
679  // Or if we can't read it [using ::access()] - not network transparent
680  if ( m_bIsLocalURL && ::access( TQFile::encodeName(m_url.path()), W_OK ) == -1 )
681  return false;
682 
683  return true;
684 }
685 
686 bool KFileItem::isHidden() const
687 {
688  if ( m_hidden != Auto )
689  return m_hidden == Hidden;
690 
691  if ( !m_url.isEmpty() )
692  return m_url.fileName()[0] == '.';
693  else // should never happen
694  return m_strName[0] == '.';
695 }
696 
697 bool KFileItem::isDir() const
698 {
699  if ( m_fileMode == KFileItem::Unknown )
700  {
701  kdDebug() << " KFileItem::isDir can't say -> false " << endl;
702  return false; // can't say for sure, so no
703  }
704  return (S_ISDIR(m_fileMode));
705 /*
706  if (!S_ISDIR(m_fileMode)) {
707  if (m_url.isLocalFile()) {
708  KMimeType::Ptr ptr=KMimeType::findByURL(m_url,0,true,true);
709  if ((ptr!=0) && (ptr->is("directory/inode"))) return true;
710  }
711  return false
712  } else return true;*/
713 }
714 
715 bool KFileItem::acceptsDrops()
716 {
717  // A directory ?
718  if ( S_ISDIR( mode() ) ) {
719  return isWritable();
720  }
721 
722  // But only local .desktop files and executables
723  if ( !m_bIsLocalURL )
724  return false;
725 
726  if (( mimetype() == "application/x-desktop") ||
727  ( mimetype() == "media/builtin-mydocuments") ||
728  ( mimetype() == "media/builtin-mycomputer") ||
729  ( mimetype() == "media/builtin-mynetworkplaces") ||
730  ( mimetype() == "media/builtin-printers") ||
731  ( mimetype() == "media/builtin-trash") ||
732  ( mimetype() == "media/builtin-webbrowser"))
733  return true;
734 
735  // Executable, shell script ... ?
736  if ( ::access( TQFile::encodeName(m_url.path()), X_OK ) == 0 )
737  return true;
738 
739  return false;
740 }
741 
742 TQString KFileItem::getStatusBarInfo()
743 {
744  TQString text = m_strText;
745 
746  if ( m_bLink )
747  {
748  TQString comment = determineMimeType()->comment( m_url, m_bIsLocalURL );
749  TQString tmp;
750  if ( comment.isEmpty() )
751  tmp = i18n ( "Symbolic Link" );
752  else
753  tmp = i18n("%1 (Link)").arg(comment);
754  text += "->";
755  text += linkDest();
756  text += " ";
757  text += tmp;
758  }
759  else if ( S_ISREG( m_fileMode ) )
760  {
761  bool hasSize;
762  KIO::filesize_t sizeValue = size(hasSize);
763  if(hasSize)
764  text += TQString(" (%1) ").arg( KIO::convertSize( sizeValue ) );
765  text += mimeComment();
766  }
767  else if ( S_ISDIR ( m_fileMode ) )
768  {
769  text += "/ ";
770  text += mimeComment();
771  }
772  else
773  {
774  text += " ";
775  text += mimeComment();
776  }
777  text.replace('\n', " "); // replace any newlines with a space, so the statusbar doesn't get a two-line string which messes the display up, Alex
778  return text;
779 }
780 
781 TQString KFileItem::getToolTipText(int maxcount)
782 {
783  // we can return TQString::null if no tool tip should be shown
784  TQString tip;
785  KFileMetaInfo info = metaInfo();
786 
787  // the font tags are a workaround for the fact that the tool tip gets
788  // screwed if the color scheme uses white as default text color
789  const char* start = "<tr><td><nobr><font color=\"black\">";
790  const char* mid = "</font></nobr></td><td><nobr><font color=\"black\">";
791  const char* end = "</font></nobr></td></tr>";
792 
793  tip = "<table cellspacing=0 cellpadding=0>";
794 
795  tip += start + i18n("Name:") + mid + text() + end;
796  tip += start + i18n("Type:") + mid;
797 
798  TQString type = TQStyleSheet::escape(mimeComment());
799  if ( m_bLink ) {
800  tip += i18n("Link to %1 (%2)").arg(linkDest(), type) + end;
801  } else
802  tip += type + end;
803 
804  if ( !S_ISDIR ( m_fileMode ) ) {
805  bool hasSize;
806  KIO::filesize_t sizeValue = size(hasSize);
807  if(hasSize)
808  tip += start + i18n("Size:") + mid +
809  KIO::convertSizeWithBytes(sizeValue) + end;
810  }
811  TQString timeStr = timeString( KIO::UDS_MODIFICATION_TIME);
812  if(!timeStr.isEmpty())
813  tip += start + i18n("Modified:") + mid +
814  timeStr + end;
815 #ifndef Q_WS_WIN //TODO: show win32-specific permissions
816  TQString userStr = user();
817  TQString groupStr = group();
818  if(!userStr.isEmpty() || !groupStr.isEmpty())
819  tip += start + i18n("Owner:") + mid + userStr + " - " + groupStr + end +
820  start + i18n("Permissions:") + mid +
821  parsePermissions(m_permissions) + end;
822 #endif
823 
824  if (info.isValid() && !info.isEmpty() )
825  {
826  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>";
827  TQStringList keys = info.preferredKeys();
828 
829  // now the rest
830  TQStringList::Iterator it = keys.begin();
831  for (int count = 0; count<maxcount && it!=keys.end() ; ++it)
832  {
833  KFileMetaInfoItem item = info.item( *it );
834  if ( item.isValid() )
835  {
836  TQString s = item.string();
837  if ( ( item.attributes() & KFileMimeTypeInfo::SqueezeText )
838  && s.length() > 50) {
839  s.truncate(47);
840  s.append("...");
841  }
842  if ( !s.isEmpty() )
843  {
844  count++;
845  tip += start +
846  TQStyleSheet::escape( item.translatedKey() ) + ":" +
847  mid +
848  TQStyleSheet::escape( s ) +
849  end;
850  }
851 
852  }
853  }
854  }
855  tip += "</table>";
856 
857  //kdDebug() << "making this the tool tip rich text:\n";
858  //kdDebug() << tip << endl;
859 
860  return tip;
861 }
862 
863 void KFileItem::run()
864 {
865  // It might be faster to pass skip that when we know the mimetype,
866  // and just call KRun::runURL. But then we need to use mostLocalURL()
867  // for application/x-desktop files, to be able to execute them.
868  (void) new KRun( m_url, m_fileMode, m_bIsLocalURL );
869 }
870 
871 bool KFileItem::cmp( const KFileItem & item )
872 {
873  bool hasSize1,hasSize2,hasTime1,hasTime2;
874  hasSize1 = hasSize2 = hasTime1 = hasTime2 = false;
875  return ( m_strName == item.m_strName
876  && m_bIsLocalURL == item.m_bIsLocalURL
877  && m_fileMode == item.m_fileMode
878  && m_permissions == item.m_permissions
879  && m_user == item.m_user
880  && m_group == item.m_group
881  && m_bLink == item.m_bLink
882  && m_hidden == item.m_hidden
883  && size(hasSize1) == item.size(hasSize2)
884  && hasSize1 == hasSize2
885  && time(KIO::UDS_MODIFICATION_TIME, hasTime1) == item.time(KIO::UDS_MODIFICATION_TIME, hasTime2)
886  && hasTime1 == hasTime2
887  && (!d || !item.d || d->iconName == item.d->iconName) );
888 
889  // Don't compare the mimetypes here. They might not be known, and we don't want to
890  // do the slow operation of determining them here.
891 }
892 
893 void KFileItem::assign( const KFileItem & item )
894 {
895  if ( this == &item )
896  return;
897  m_entry = item.m_entry;
898  m_url = item.m_url;
899  m_bIsLocalURL = item.m_bIsLocalURL;
900  m_strName = item.m_strName;
901  m_strText = item.m_strText;
902  m_fileMode = item.m_fileMode;
903  m_permissions = item.m_permissions;
904  m_user = item.m_user;
905  m_group = item.m_group;
906  m_bLink = item.m_bLink;
907  m_pMimeType = item.m_pMimeType;
908  m_strLowerCaseName = item.m_strLowerCaseName;
909  m_bMimeTypeKnown = item.m_bMimeTypeKnown;
910  m_hidden = item.m_hidden;
911  m_guessedMimeType = item.m_guessedMimeType;
912  m_access = item.m_access;
913  m_metaInfo = item.m_metaInfo;
914  for ( int i = 0; i < NumFlags; i++ )
915  m_time[i] = item.m_time[i];
916  m_size = item.m_size;
917  // note: m_extra is NOT copied, as we'd have no control over who is
918  // deleting the data or not.
919 
920  // We had a mimetype previously (probably), so we need to re-determine it
921  determineMimeType();
922 
923  if ( item.d ) {
924  if ( !d )
925  d = new KFileItemPrivate;
926  d->iconName = item.d->iconName;
927  } else {
928  delete d;
929  d = 0;
930  }
931 }
932 
933 void KFileItem::setUDSEntry( const KIO::UDSEntry& _entry, const KURL& _url,
934  bool _determineMimeTypeOnDemand, bool _urlIsDirectory )
935 {
936  m_entry = _entry;
937  m_url = _url;
938  m_strName = TQString::null;
939  m_strText = TQString::null;
940  m_user = TQString::null;
941  m_group = TQString::null;
942  m_strLowerCaseName = TQString::null;
943  m_pMimeType = 0;
944  m_fileMode = KFileItem::Unknown;
945  m_permissions = KFileItem::Unknown;
946  m_bMarked = false;
947  m_bLink = false;
948  m_bIsLocalURL = _url.isLocalFile();
949  m_bMimeTypeKnown = false;
950  m_hidden = Auto;
951  m_guessedMimeType = TQString::null;
952  m_metaInfo = KFileMetaInfo();
953 
954  if ( d )
955  d->iconName = TQString::null;
956 
957  readUDSEntry( _urlIsDirectory );
958  init( _determineMimeTypeOnDemand );
959 }
960 
961 void KFileItem::setFileMode( mode_t m )
962 {
963  m_fileMode = m;
964 }
965 
966 void KFileItem::setMimeType( const TQString& mimetype )
967 {
968  m_pMimeType = KMimeType::mimeType( mimetype );
969 }
970 
971 void KFileItem::setExtraData( const void *key, void *value )
972 {
973  if ( !key )
974  return;
975 
976  m_extra.replace( key, value );
977 }
978 
979 const void * KFileItem::extraData( const void *key ) const
980 {
981  TQMapConstIterator<const void*,void*> it = m_extra.find( key );
982  if ( it != m_extra.end() )
983  return it.data();
984  return 0L;
985 }
986 
987 void * KFileItem::extraData( const void *key )
988 {
989  TQMapIterator<const void*,void*> it = m_extra.find( key );
990  if ( it != m_extra.end() )
991  return it.data();
992  return 0L;
993 }
994 
995 void KFileItem::removeExtraData( const void *key )
996 {
997  m_extra.remove( key );
998 }
999 
1000 TQString KFileItem::permissionsString() const
1001 {
1002  if (m_access.isNull())
1003  m_access = parsePermissions( m_permissions );
1004 
1005  return m_access;
1006 }
1007 
1008 TQString KFileItem::parsePermissions(mode_t perm) const
1009 {
1010  char p[] = "---------- ";
1011 
1012  if (isDir())
1013  p[0]='d';
1014  else if (isLink())
1015  p[0]='l';
1016 
1017  if (perm & TQFileInfo::ReadUser)
1018  p[1]='r';
1019  if (perm & TQFileInfo::WriteUser)
1020  p[2]='w';
1021  if ((perm & TQFileInfo::ExeUser) && !(perm & S_ISUID)) p[3]='x';
1022  else if ((perm & TQFileInfo::ExeUser) && (perm & S_ISUID)) p[3]='s';
1023  else if (!(perm & TQFileInfo::ExeUser) && (perm & S_ISUID)) p[3]='S';
1024 
1025  if (perm & TQFileInfo::ReadGroup)
1026  p[4]='r';
1027  if (perm & TQFileInfo::WriteGroup)
1028  p[5]='w';
1029  if ((perm & TQFileInfo::ExeGroup) && !(perm & S_ISGID)) p[6]='x';
1030  else if ((perm & TQFileInfo::ExeGroup) && (perm & S_ISGID)) p[6]='s';
1031  else if (!(perm & TQFileInfo::ExeGroup) && (perm & S_ISGID)) p[6]='S';
1032 
1033  if (perm & TQFileInfo::ReadOther)
1034  p[7]='r';
1035  if (perm & TQFileInfo::WriteOther)
1036  p[8]='w';
1037  if ((perm & TQFileInfo::ExeOther) && !(perm & S_ISVTX)) p[9]='x';
1038  else if ((perm & TQFileInfo::ExeOther) && (perm & S_ISVTX)) p[9]='t';
1039  else if (!(perm & TQFileInfo::ExeOther) && (perm & S_ISVTX)) p[9]='T';
1040 
1041  if (hasExtendedACL())
1042  p[10]='+';
1043 
1044  return TQString::fromLatin1(p);
1045 }
1046 
1047 // check if we need to cache this
1048 TQString KFileItem::timeString( unsigned int which ) const
1049 {
1050  bool hasTime;
1051  time_t time_ = time(which, hasTime);
1052  if(!hasTime) return TQString::null;
1053 
1054  TQDateTime t;
1055  t.setTime_t( time_);
1056  return KGlobal::locale()->formatDateTime( t );
1057 }
1058 
1059 void KFileItem::setMetaInfo( const KFileMetaInfo & info )
1060 {
1061  m_metaInfo = info;
1062 }
1063 
1064 const KFileMetaInfo & KFileItem::metaInfo(bool autoget, int) const
1065 {
1066  bool isLocalURL;
1067  KURL url = mostLocalURL(isLocalURL);
1068 
1069  if ( autoget && !m_metaInfo.isValid() &&
1070  KGlobalSettings::showFilePreview(url) )
1071  {
1072  m_metaInfo = KFileMetaInfo( url, mimetype() );
1073  }
1074 
1075  return m_metaInfo;
1076 }
1077 
1078 KURL KFileItem::mostLocalURL(bool &local) const
1079 {
1080  TQString local_path = localPath();
1081 
1082  if ( !local_path.isEmpty() )
1083  {
1084  local = true;
1085  KURL url;
1086  url.setPath(local_path);
1087  return url;
1088  }
1089  else
1090  {
1091  local = m_bIsLocalURL;
1092  return m_url;
1093  }
1094 }
1095 
1096 void KFileItem::virtual_hook( int, void* )
1097 { /*BASE::virtual_hook( id, data );*/ }
1098 
1099 TQDataStream & operator<< ( TQDataStream & s, const KFileItem & a )
1100 {
1101  // We don't need to save/restore anything that refresh() invalidates,
1102  // since that means we can re-determine those by ourselves.
1103  s << a.m_url;
1104  s << a.m_strName;
1105  s << a.m_strText;
1106  return s;
1107 }
1108 
1109 TQDataStream & operator>> ( TQDataStream & s, KFileItem & a )
1110 {
1111  s >> a.m_url;
1112  s >> a.m_strName;
1113  s >> a.m_strText;
1114  a.m_bIsLocalURL = a.m_url.isLocalFile();
1115  a.m_bMimeTypeKnown = false;
1116  a.refresh();
1117  return s;
1118 }

kio/kio

Skip menu "kio/kio"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kio/kio

Skip menu "kio/kio"
  • arts
  • dcop
  • dnssd
  • interfaces
  •     interface
  •     library
  •   kspeech
  •   ktexteditor
  • kabc
  • kate
  • kcmshell
  • kdecore
  • kded
  • kdefx
  • kdeprint
  • kdesu
  • kdeui
  • kdoctools
  • khtml
  • kimgio
  • kinit
  • kio
  •   bookmarks
  •   httpfilter
  •   kfile
  •   kio
  •   kioexec
  •   kpasswdserver
  •   kssl
  • kioslave
  •   http
  • kjs
  • kmdi
  •   kmdi
  • knewstuff
  • kparts
  • krandr
  • kresources
  • kspell2
  • kunittest
  • kutils
  • kwallet
  • libkmid
  • libkscreensaver
Generated for kio/kio by doxygen 1.8.3.1
This website is maintained by Timothy Pearson.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. |