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

tdeio/tdeio

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

tdeio/tdeio

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

tdeio/tdeio

Skip menu "tdeio/tdeio"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • knewstuff
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeio/tdeio by doxygen 1.8.3.1
This website is maintained by Timothy Pearson.