Source Code (Use browser search to find items of interest.)

Class Index

kdelibs'KMimeType (./kdelibs/kio/kmimetype.h:44)

class KMimeType : public KServiceType
{
  K_SYCOCATYPE( KST_KMimeType, KServiceType )

public:
  typedef KSharedPtr<KMimeType> Ptr;
  typedef QValueList<Ptr> List;
public: // KDoc seems to barf on those typedefs and generates no docs after them
  /**
   * Constructor.  You may pass in arguments to create a mimetype with
   * specific properties
   */
  KMimeType( const QString & _fullpath, const QString& _type, const QString& _icon,
	     const QString& _comment, const QStringList& _patterns );

  /**
   * Construct a mimetype and take all informations from a config file.
   */
  KMimeType( const QString & _fullpath );

  /**
   * Construct a mimetype and take all informations from a desktop file.
   */
  KMimeType( KDesktopFile *config );

  /**
   * @internal construct a service from a stream.
   * The stream must already be positionned at the correct offset
   */
  KMimeType( QDataStream& _str, int offset );

  virtual ~KMimeType();

  /**
   * Return the filename of the icon associated with the mimetype.
   * The arguments are unused, but provided so that KMimeType derived classes
   * can use them (e.g. KFolderType uses the URL to return one out of 2 icons)
   * @return the path to the icon associated with this MIME type.
   */
  virtual QString icon( const QString& , bool ) const { return m_strIcon; }
  /**
   * This function differs from the above only in that a KURL may be
   * provided instead of a QString for convenience.
   */
  virtual QString icon( const KURL& , bool ) const { return m_strIcon; }
  /**
   * Use this function only if you don't have a special URL
   * for which you search a pixmap.  This function is useful to find
   * out, which icon is usually chosen for a certain mime type. Since
   * no URL is passed, it is impossible to obey icon hints in desktop
   * entries for example.
   * @param _force_size Override globallly configured icon size. 
   * @param _path Output parameter to get the full path. Seldom needed.
   */
  virtual QPixmap pixmap( int _force_size, QString * _path = 0L ) const;
  /**
   * Find the pixmap for a given file of this mimetype
   * Convenience method that uses icon(), but also locates and load the pixmap
   * @param _url URL for the file
   * @param _force_size Override globallly configured icon size. 
   * @param _path Output parameter to get the full path. Seldom needed.
   */
  virtual QPixmap pixmap( const KURL& _url, int _force_size, QString * _path = 0L ) const;

  /**
   * Convenience method to find the pixmap for a URL
   * Call this one when you don't know the mimetype.
   */
  static QPixmap pixmapForURL( const KURL & _url, mode_t _mode = 0,
                               int force_size=0, QString * _path = 0L );

  /**
   * The arguments are unused, but provided so that KMimeType derived classes
   * can use them.
   * @return the descriptive comment associated with the MIME type, if any.
   */
  virtual QString comment( const QString&, bool ) const { return m_strComment; }
  /**
   * This function differs from the above only in that a KURL may be
   * provided instead of a QString for convenience.
   */
  virtual QString comment( const KURL&, bool ) const { return m_strComment; }

  /**
   * @return the list of patterns associated to the MIME Type
   */
  virtual const QStringList& patterns() const { return m_lstPatterns; }

  /**
   * Load the mimetype from a stream.
   * @param _parentLoaded internal (set by the constructor)
   */
  virtual void load( QDataStream&, bool _parentLoaded = false );
  /**
   * Save the mimetype to a stream
   */
  virtual void save( QDataStream& );

  virtual QVariant property( const QString& _name ) const;
  virtual QStringList propertyNames() const;

  /**
   * @return a pointer to the mime type '_name' or a pointer to the default
   *         mime type "application/octet-stream". 0L is NEVER returned.
   *
   * VERY IMPORTANT : don't store the result in a KMimeType * !
   * @see KServiceType::serviceType
   */
  static Ptr mimeType( const QString& _name );
  /**
   * This function looks at mode_t first. If that does not help it
   * looks at the extension.  This is ok for FTP, FILE, TAR and
   * friends, but is not for HTTP ( cgi scripts! ). You should use
   * @ref KRun instead, but this function returns immediately while
   * @ref KRun is async. If no extension matches, then @ref
   * KMimeMagic is used if the URL a local file or
   * "application/octet-stream" is returned otherwise.
   *
   * @param _url is the right most URL with a filesystem protocol. It
   *        is up to you to find out about that if you have a nested
   *        URL.  For example
   *        "http://localhost/mist.gz#gzip:/decompress" would have to
   *        pass the "http://..." URL part, while
   *        "file:/tmp/x.tar#tar:/src/test.gz#gzip:/decompress" would
   *        have to pass the "tar:/..." part of the URL, since gzip is
   *        a filter protocol and not a filesystem protocol.
   *
   * @param _fast_mode If set to true no disk access is allowed to
   *        find out the mimetype. The result may be suboptimal, but
   *        it is * FAST.
   * @return a pointer to the matching mimetype. 0L is NEVER returned.
   * VERY IMPORTANT : don't store the result in a KMimeType * !
   */
  static Ptr findByURL( const KURL& _url, mode_t _mode = 0,
			       bool _is_local_file = false, bool _fast_mode = false );

  /**
   * Get all the mimetypes. Useful for showing the list of
   * available mimetypes.
   * Very memory consuming, don't use unless really necessary.
   */
  static List allMimeTypes();

protected:
  /**
   * Signal a missing mime type
   */
  static void errorMissingMimeType( const QString& _type );

  /**
   * This function makes sure that the default mime type exists
   */
  static void buildDefaultType();
  /**
   * This function makes sure that vital mime types are installed.
   */
  static void checkEssentialMimeTypes();
  /**
   * True if check for vital mime types has been done
   */
  static bool s_bChecked;

  QStringList m_lstPatterns;

  static Ptr s_pDefaultType;
};

/**
 * @short Mimetype for a folder (inode/directory)
 * Handles locked folders, for instance.
 */

kdelibs'KMimeType::buildDefaultType() (./kdelibs/kio/kmimetype.cpp:52)

void KMimeType::buildDefaultType()
{
  assert ( !s_pDefaultType );
  // Try to find the default type
  KServiceType * mime = KServiceTypeFactory::self()->
	findServiceTypeByName( "application/octet-stream" );

  if (mime && mime->isType( KST_KMimeType ))
  {
      s_pDefaultType = KMimeType::Ptr((KMimeType *) mime);
  }
  else
  {
     errorMissingMimeType( "application/octet-stream" );
     KStandardDirs stdDirs;
     QString sDefaultMimeType = stdDirs.resourceDirs("mime").first()+"application/octet-stream.desktop";
     s_pDefaultType = new KMimeType( sDefaultMimeType, "application/octet-stream",
                                     "unknown", "mime", QStringList() );
  }
}

// Check for essential mimetypes

kdelibs'KMimeType::checkEssentialMimeTypes() (./kdelibs/kio/kmimetype.cpp:74)

void KMimeType::checkEssentialMimeTypes()
{
  if ( s_bChecked ) // already done
    return;
  if ( !s_pDefaultType ) // we need a default type first
    buildDefaultType();

  s_bChecked = true; // must be done before building mimetypes

  // No Mime-Types installed ?
  // Lets do some rescue here.
  if ( !KServiceTypeFactory::self()->checkMimeTypes() )
  {
    KMessageBoxWrapper::error( 0L, i18n( "No mime types installed!" ) );
    return; // no point in going any further
  }
	
  if ( KMimeType::mimeType( "inode/directory" ) == s_pDefaultType )
    errorMissingMimeType( "inode/directory" );
  if ( KMimeType::mimeType( "inode/directory-locked" ) == s_pDefaultType )
    errorMissingMimeType( "inode/directory-locked" );
  if ( KMimeType::mimeType( "inode/blockdevice" ) == s_pDefaultType )
    errorMissingMimeType( "inode/blockdevice" );
  if ( KMimeType::mimeType( "inode/chardevice" ) == s_pDefaultType )
    errorMissingMimeType( "inode/chardevice" );
  if ( KMimeType::mimeType( "inode/socket" ) == s_pDefaultType )
    errorMissingMimeType( "inode/socket" );
  if ( KMimeType::mimeType( "inode/fifo" ) == s_pDefaultType )
    errorMissingMimeType( "inode/fifo" );
  if ( KMimeType::mimeType( "application/x-shellscript" ) == s_pDefaultType )
    errorMissingMimeType( "application/x-shellscript" );
  if ( KMimeType::mimeType( "application/x-executable" ) == s_pDefaultType )
    errorMissingMimeType( "application/x-executable" );
  if ( KMimeType::mimeType( "application/x-desktop" ) == s_pDefaultType )
    errorMissingMimeType( "application/x-desktop" );
}


kdelibs'KMimeType::errorMissingMimeType() (./kdelibs/kio/kmimetype.cpp:111)

void KMimeType::errorMissingMimeType( const QString& _type )
{
  QString tmp = i18n( "Could not find mime type\n%1" ).arg( _type );

  KMessageBoxWrapper::sorry( 0, tmp );
}

KMimeType::Ptr KMimeType::mimeType( const QString& _name )
{
  KServiceType * mime = KServiceTypeFactory::self()->findServiceTypeByName( _name );

  if ( !mime || !mime->isType( KST_KMimeType ) )
  {
    if ( !s_pDefaultType )
      buildDefaultType();
    return s_pDefaultType;
  }

  // We got a mimetype
  return KMimeType::Ptr((KMimeType *) mime);
}

KMimeType::List KMimeType::allMimeTypes()
{
  return KServiceTypeFactory::self()->allMimeTypes();
}

KMimeType::Ptr KMimeType::findByURL( const KURL& _url, mode_t _mode,
				 bool _is_local_file, bool _fast_mode )
{
  checkEssentialMimeTypes();

  if ( !_fast_mode && !_is_local_file && _url.isLocalFile() )
    _is_local_file = true;

  if ( !_fast_mode && _is_local_file && _mode == 0 )
  {
    struct stat buff;
    if ( stat( _url.path().ascii(), &buff ) != -1 )
      _mode = buff.st_mode;
  }

  // Look at mode_t first
  if ( S_ISDIR( _mode ) )
  {
    // Special hack for local files. We want to see whether we
    // are allowed to enter the directory
    if ( _is_local_file )
    {
      QString path ( _url.path( 0 ) );
      if ( access( path.data(), R_OK ) == -1 )
	return mimeType( "inode/directory-locked" );
    }
    return mimeType( "inode/directory" );
  }
  if ( S_ISCHR( _mode ) )
    return mimeType( "inode/chardevice" );
  if ( S_ISBLK( _mode ) )
    return mimeType( "inode/blockdevice" );
  if ( S_ISFIFO( _mode ) )
    return mimeType( "inode/fifo" );
  if ( S_ISSOCK( _mode ) )
    return mimeType( "inode/socket" );
  // KMimeMagic can do that better for local files
  if ( !_is_local_file && S_ISREG( _mode ) && ( _mode & ( S_IXUSR | S_IXGRP | S_IXOTH ) ) )
    return mimeType( "application/x-executable" );

  QString fileName ( _url.filename() );

  if ( ! fileName.isNull() )
    {
      // Try to find it out by looking at the filename
      KMimeType * mime = KServiceTypeFactory::self()->findFromPattern( fileName );
      if ( mime )
         return KMimeType::Ptr( mime );

      // Another filename binding, hardcoded, is .desktop:
      if ( fileName.right(8) == ".desktop" )
	return mimeType( "application/x-desktop" );
      // Another filename binding, hardcoded, is .kdelnk;
      // this is preserved for backwards compatibility
      if ( fileName.right(7) == ".kdelnk" )
	return mimeType( "application/x-desktop" );
      // .directory files are detected as x-desktop by mimemagic
      // but don't have a Type= entry. Better cheat and say they are text files
      if ( fileName == ".directory" )
	return mimeType( "text/plain" );
    }

  if ( !_is_local_file || _fast_mode )
  {
    QString path = _url.path();
    if ( path.right(1) == "/" || path.isEmpty() )
    {
      // Assume HTML for http/https protocol
      return (_url.protocol().left(4) == "http") ? mimeType( "text/html" )
                                                 : mimeType( "inode/directory" );
    }
  }

  // No more chances for non local URLs
  if ( !_is_local_file || _fast_mode )
    return mimeType( "application/octet-stream" );

  // Do some magic for local files
  QString path = _url.path( 0 );
  kdDebug(7009) << QString("Mime Type finding for '%1'").arg(path) << endl;
  KMimeMagicResult* result = KMimeMagic::self()->findFileType( path );

  // If we still did not find it, we must assume the default mime type
  if ( !result || !result->isValid() )  /* !result->mimeType() || result->mimeType()[0] == 0 ) */
    return mimeType( "application/octet-stream" );

  // The mimemagic stuff was successful
  return mimeType( result->mimeType() );
}


kdelibs'KMimeType::KMimeType() (./kdelibs/kio/kmimetype.cpp:228)

KMimeType::KMimeType( const QString & _fullpath, const QString& _type, const QString& _icon,
                      const QString& _comment, const QStringList& _patterns )
  : KServiceType( _fullpath, _type, _icon, _comment )
{
  m_lstPatterns = _patterns;
}


kdelibs'KMimeType::KMimeType() (./kdelibs/kio/kmimetype.cpp:235)

KMimeType::KMimeType( const QString & _fullpath ) : KServiceType( _fullpath )
{
  KDesktopFile _cfg( _fullpath, true );
  m_lstPatterns = _cfg.readListEntry( "Patterns", ';' );

  if ( !isValid() )
    kdWarning(7009) << "mimetype not valid '" << m_strName << "' (missing entry in the file ?)" << endl;
}


kdelibs'KMimeType::KMimeType() (./kdelibs/kio/kmimetype.cpp:244)

KMimeType::KMimeType( KDesktopFile *config ) : KServiceType( config )
{
  config->setDesktopGroup();
  m_lstPatterns = config->readListEntry( "Patterns", ';' );

  if ( !isValid() )
    kdWarning(7009) << "mimetype not valid '" << m_strName << "' (missing entry in the file ?)" << endl;
}


kdelibs'KMimeType::KMimeType() (./kdelibs/kio/kmimetype.cpp:253)

KMimeType::KMimeType( QDataStream& _str, int offset ) : KServiceType( _str, offset )
{
  load( _str, true ); // load our specific stuff
}


kdelibs'KMimeType::load() (./kdelibs/kio/kmimetype.cpp:258)

void KMimeType::load( QDataStream& _str, bool _parentLoaded )
{
  if ( !_parentLoaded )
    KServiceType::load( _str );

  // kdDebug(7009) << "KMimeType::load( QDataStream& ) : loading list of patterns" << endl;
  _str >> m_lstPatterns;
  // kdDebug(7009) << "KMimeType::load( QDataStream& ) : done" << endl;
}


kdelibs'KMimeType::save() (./kdelibs/kio/kmimetype.cpp:268)

void KMimeType::save( QDataStream& _str )
{
  KServiceType::save( _str );
  // Warning adding/removing fields here involves a binary incompatible change - update version
  // number in ksycoca.h
  _str << m_lstPatterns;
}


kdelibs'KMimeType::property() (./kdelibs/kio/kmimetype.cpp:276)

QVariant KMimeType::property( const QString& _name ) const
{
  if ( _name == "Patterns" )
    return QVariant( m_lstPatterns );

  return KServiceType::property( _name );
}


kdelibs'KMimeType::propertyNames() (./kdelibs/kio/kmimetype.cpp:284)

QStringList KMimeType::propertyNames() const
{
  QStringList res = KServiceType::propertyNames();
  res.append( "Patterns" );

  return res;
}


kdelibs'KMimeType::~KMimeType() (./kdelibs/kio/kmimetype.cpp:292)

KMimeType::~KMimeType()
{
}


kdelibs'KMimeType::pixmap() (./kdelibs/kio/kmimetype.cpp:296)

QPixmap KMimeType::pixmap( int _size, QString * _path ) const
{
  return KGlobal::iconLoader()->loadIcon( icon( QString::null, false ), 
	KIcon::Desktop, _size, KIcon::DefaultState, _path, false );
}


kdelibs'KMimeType::pixmap() (./kdelibs/kio/kmimetype.cpp:302)

QPixmap KMimeType::pixmap( const KURL& _url, int _size, QString * _path ) const
{
  return KGlobal::iconLoader()->loadIcon( icon( _url, _url.isLocalFile() ), 
	KIcon::Desktop, _size, KIcon::DefaultState, _path, false );
}


kdelibs'KMimeType::pixmapForURL() (./kdelibs/kio/kmimetype.cpp:308)

QPixmap KMimeType::pixmapForURL( const KURL & _url, mode_t _mode,
                                 int _group_or_size, QString * _path )
{
  return KMimeType::findByURL( _url, _mode, _url.isLocalFile(), false /*HACK*/)->
    pixmap( _url, _group_or_size, _path );
}

/*******************************************************
 *
 * KFolderType
 *
 ******************************************************/