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

Class Index

kdelibs'KDEDesktopMimeType (./kdelibs/kio/kmimetype.h:236)

class KDEDesktopMimeType : public KMimeType
{
  K_SYCOCATYPE( KST_KDEDesktopMimeType, KMimeType )

public:
  enum ServiceType { ST_MOUNT, ST_UNMOUNT, /* ST_PROPERTIES, */ ST_USER_DEFINED };
		
  struct Service
  {
    Service() { m_display = true; }
    QString m_strName;
    QString m_strIcon;
    QString m_strExec;
    ServiceType m_type;
    bool m_display;
  };
  // KDEDesktopMimeType( const QString & _fullpath, const QString& _type, const QString& _icon,
  //                     const QString& _comment, const QStringList& _patterns );
  // KDEDesktopMimeType( const QString & _fullpath ) : KMimeType( _fullpath ) { }
  KDEDesktopMimeType( KDesktopFile *config) : KMimeType( config ) { }
  KDEDesktopMimeType( QDataStream& _str, int offset ) : KMimeType( _str, offset ) { }

  virtual QString icon( const QString& _url, bool _is_local ) const;
  virtual QString icon( const KURL& _url, bool _is_local ) const;
  virtual QPixmap pixmap( const KURL& _url, int force_size, QString * _path = 0L ) const;
  virtual QString comment( const QString& _url, bool _is_local ) const;
  virtual QString comment( const KURL& _url, bool _is_local ) const;

  /**
   * @return a list of services for the given .desktop file that are handled
   * by kio itself. Namely mount/unmount for FSDevice files.
   */
  static QValueList<Service> builtinServices( const KURL& _url );
  /**
   * @return a list of services defined by the user as possible actions
   * on the given .desktop file.
   */
  static QValueList<Service> userDefinedServices( const KURL& _url );

  /**
   * @param _url is the URL of the desktop entry. The URL must be
   *        local, otherwise nothing will happen.
   */
  static void executeService( const QString& _url, KDEDesktopMimeType::Service& _service );

  /**
   * Invokes the default action for the desktop entry. If the desktop
   * entry is not local, then only false is returned. Otherwise we
   * would create a security problem. Only types Link and Mimetype
   * could be followed.
   *
   * @return true on success and false on failure.
   *
   * @see KRun::runURL */

  static bool run( const KURL& _url, bool _is_local );

protected:
  static bool runFSDevice( const KURL& _url, const KSimpleConfig &cfg );
  static bool runApplication( const KURL& _url, const QString & _serviceFile );
  static bool runLink( const KURL& _url, const KSimpleConfig &cfg );
  static bool runMimeType( const KURL& _url, const KSimpleConfig &cfg );
};

/**
 * @short MimeType for any executable, like /bin/ls
 */

kdelibs'KDEDesktopMimeType::icon() (./kdelibs/kio/kmimetype.cpp:405)

QString KDEDesktopMimeType::icon( const QString& _url, bool _is_local ) const
{
  if ( !_is_local || _url.isEmpty() )
    return KMimeType::icon( _url, _is_local );

  KURL u( _url );
  return icon( u, _is_local );
}


kdelibs'KDEDesktopMimeType::icon() (./kdelibs/kio/kmimetype.cpp:414)

QString KDEDesktopMimeType::icon( const KURL& _url, bool _is_local ) const
{
  if ( !_is_local )
    return KMimeType::icon( _url, _is_local );

  KSimpleConfig cfg( _url.path(), true );
  cfg.setDesktopGroup();
  QString icon = cfg.readEntry( "Icon" );
  QString type = cfg.readEntry( "Type" );

  if ( type == "FSDevice" || type == "FSDev") // need to provide FSDev for
                                              // backwards compatibility
  {
    QString unmount_icon = cfg.readEntry( "UnmountIcon" );
    QString dev = cfg.readEntry( "Dev" );
    if ( !icon.isEmpty() && !unmount_icon.isEmpty() && !dev.isEmpty() )
    {
      QString mp = KIO::findDeviceMountPoint( dev.ascii() );
      // Is the device not mounted ?
      if ( mp.isNull() )
        return unmount_icon;
    }
  }

  if ( icon.isEmpty() )
    return KMimeType::icon( _url, _is_local );

  return icon;
}


kdelibs'KDEDesktopMimeType::pixmap() (./kdelibs/kio/kmimetype.cpp:444)

QPixmap KDEDesktopMimeType::pixmap( const KURL& _url, int size, QString * _path ) const
{
  QString _icon = icon( _url, _url.isLocalFile() );
  QPixmap pix = KGlobal::iconLoader()->loadIcon( _icon, KIcon::Desktop, 
	size, KIcon::DefaultState, _path, false );
  #if 0
  if (pix.isNull())
  {
    KSimpleConfig cfg( _url.path(), true );
    cfg.setDesktopGroup();
    QString type = cfg.readEntry( "Type" );
    if ( type == "FSDevice" )
    {
      //kdDebug(7009) << "trying to load devices/" << _icon << endl;
      // KDE-1.x kdelnks contain "cdrom_mount.xpm" instead of "devices/cdrom_mount"
      return KGlobal::iconLoader()->loadIcon( QString("devices/"+_icon), _group_or_size, _path, false );
    }
    return KGlobal::iconLoader()->loadIcon("unknown", _group_or_size, _path, false);
  }
  #endif
  return pix;
}


kdelibs'KDEDesktopMimeType::comment() (./kdelibs/kio/kmimetype.cpp:467)

QString KDEDesktopMimeType::comment( const QString& _url, bool _is_local ) const
{
  if ( !_is_local || _url.isEmpty() )
    return KMimeType::comment( _url, _is_local );

  KURL u( _url );
  return comment( u, _is_local );
}


kdelibs'KDEDesktopMimeType::comment() (./kdelibs/kio/kmimetype.cpp:476)

QString KDEDesktopMimeType::comment( const KURL& _url, bool _is_local ) const
{
  if ( !_is_local )
    return KMimeType::comment( _url, _is_local );

  KSimpleConfig cfg( _url.path(), true );
  cfg.setDesktopGroup();
  QString comment = cfg.readEntry( "Comment" );
  if ( comment.isEmpty() )
    return KMimeType::comment( _url, _is_local );

  return comment;
}


kdelibs'KDEDesktopMimeType::run() (./kdelibs/kio/kmimetype.cpp:490)

bool KDEDesktopMimeType::run( const KURL& u, bool _is_local )
{
  // It might be a security problem to run external untrusted desktop
  // entry files
  if ( !_is_local )
    return false;

  KSimpleConfig cfg( u.path(), true );
  cfg.setDesktopGroup();
  QString type = cfg.readEntry( "Type" );
  if ( type.isEmpty() )
  {
    QString tmp = i18n("The desktop entry file\n%1\n"
		       "has no Type=... entry").arg(u.path() );
    KMessageBoxWrapper::error( 0, tmp);
    return false;
  }

  //kdDebug(7009) << "TYPE = " << type.data() << endl;

  if ( type == "FSDevice" )
    return runFSDevice( u, cfg );
  else if ( type == "Application" )
    return runApplication( u, u.path() );
  else if ( type == "Link" )
    return runLink( u, cfg );
  else if ( type == "MimeType" )
    return runMimeType( u, cfg );


  QString tmp = i18n("The desktop entry of type\n%1\nis unknown").arg( type );
  KMessageBoxWrapper::error( 0, tmp);

  return false;
}


kdelibs'KDEDesktopMimeType::runFSDevice() (./kdelibs/kio/kmimetype.cpp:526)

bool KDEDesktopMimeType::runFSDevice( const KURL& _url, const KSimpleConfig &cfg )
{
  QString point = cfg.readEntry( "MountPoint" );
  QString dev = cfg.readEntry( "Dev" );

  if ( dev.isEmpty() )
  {
    QString tmp = i18n("The desktop entry file\n%1\nis of type FSDevice but has no Dev=... entry").arg( _url.path() );
    KMessageBoxWrapper::error( 0, tmp);
    return false;
  }

  QString mp = KIO::findDeviceMountPoint( dev.ascii() );
  // Is the device already mounted ?
  if ( !mp.isNull() )
  {
    QString mp2 = "file:";
    mp2 += mp;
    // Open a new window
    KFileManager::getFileManager()->openFileManagerWindow( mp2 );
  }
  else
  {
    bool ro = cfg.readBoolEntry( "ReadOnly", false );
		
    KURL u( _url );
    (void) new KAutoMount( ro, 0L, dev.ascii(), 0L, u.path() );
  }

  return true;
}


kdelibs'KDEDesktopMimeType::runApplication() (./kdelibs/kio/kmimetype.cpp:558)

bool KDEDesktopMimeType::runApplication( const KURL& , const QString & _serviceFile )
{
  KService s( _serviceFile );
  if ( !s.isValid() )
    // The error message was already displayed, so we can just quit here
    return false;

  QString user = s.username();
  QString opts = s.terminalOptions();
  if (opts.isNull())
    opts = "";

  QString cmd = s.exec();
  if (s.substituteUid() && !user.isEmpty())
  {
    if (s.terminal()) 
      cmd = QString("konsole %1 -e su -- %2 -c \"%3\"").arg(opts).arg(user).arg(cmd);
    else
      cmd = QString("kdesu -u %1 -- %2").arg(user).arg(cmd);
  } else if (s.terminal())
    cmd = QString("konsole %1 -e /bin/sh -- -c \"%2\"").arg(opts).arg(cmd);

  KURL::List empty;
  bool res = KRun::run( cmd, empty, s.name(), s.icon(), s.icon() );

  return res;
}


kdelibs'KDEDesktopMimeType::runLink() (./kdelibs/kio/kmimetype.cpp:586)

bool KDEDesktopMimeType::runLink( const KURL& _url, const KSimpleConfig &cfg )
{
  QString url = cfg.readEntry( "URL" );
  if ( url.isEmpty() )
  {
    QString tmp = i18n("The desktop entry file\n%1\nis of type Link but has no URL=... entry").arg( _url.url() );
    KMessageBoxWrapper::error( 0, tmp );
    return false;
  }

  (void)new KRun( url );

  return true;
}


kdelibs'KDEDesktopMimeType::runMimeType() (./kdelibs/kio/kmimetype.cpp:601)

bool KDEDesktopMimeType::runMimeType( const KURL& , const KSimpleConfig & )
{
  // HACK: TODO
  // How ? (David) Showing up the properties dialog ? That's in libkonq !
  return false;
}


kdelibs'KDEDesktopMimeType::builtinServices() (./kdelibs/kio/kmimetype.cpp:608)

QValueList<KDEDesktopMimeType::Service> KDEDesktopMimeType::builtinServices( const KURL& _url )
{
  QValueList<Service> result;

  if ( !_url.isLocalFile() )
    return result;

  KSimpleConfig cfg( _url.path(), true );
  cfg.setDesktopGroup();
  QString type = cfg.readEntry( "Type" );

  if ( type.isEmpty() )
    return result;

  if ( type == "FSDevice" )
  {
    QString dev = cfg.readEntry( "Dev" );
    if ( dev.isEmpty() )
    {
      QString tmp = i18n("The desktop entry file\n%1\nis of type FSDevice but has no Dev=... entry").arg( _url.path() );
      KMessageBoxWrapper::error( 0, tmp);
    }
    else
    {
      QString mp = KIO::findDeviceMountPoint( dev.ascii() );
      // not mounted ?
      if ( mp.isEmpty() )
      {
	Service mount;
	mount.m_strName = i18n("Mount");
	mount.m_type = ST_MOUNT;
	result.append( mount );
      }
      else
      {
	Service unmount;
	unmount.m_strName = i18n("Unmount");
	unmount.m_type = ST_UNMOUNT;
	result.append( unmount );
      }
    }
  }

  return result;
}


kdelibs'KDEDesktopMimeType::userDefinedServices() (./kdelibs/kio/kmimetype.cpp:654)

QValueList<KDEDesktopMimeType::Service> KDEDesktopMimeType::userDefinedServices( const KURL& _url )
{
  QValueList<Service> result;

  if ( !_url.isLocalFile() )
    return result;

  KSimpleConfig cfg( _url.path(), true );

  cfg.setDesktopGroup();

  if ( !cfg.hasKey( "Actions" ) )
    return result;

  QStringList keys = cfg.readListEntry( "Actions", ';' ); //the desktop standard defines ";" as separator!

  if ( keys.count() == 0 )
    return result;

  QStringList::ConstIterator it = keys.begin();
  QStringList::ConstIterator end = keys.end();
  for ( ; it != end; ++it )
  {
    kdDebug(7009) << "CURRENT KEY = " << (*it) << endl;

    QString group = *it;
    group.prepend( "Desktop Action " );

    bool bInvalidMenu = false;

    if ( cfg.hasGroup( group ) )
    {
      cfg.setGroup( group );

      if ( !cfg.hasKey( "Name" ) || !cfg.hasKey( "Exec" ) )
        bInvalidMenu = true;
      else
      {
        Service s;
        s.m_strName = cfg.readEntry( "Name" );
        s.m_strIcon = cfg.readEntry( "Icon" );
        s.m_strExec = cfg.readEntry( "Exec" );
	s.m_type = ST_USER_DEFINED;
        s.m_display = !cfg.readBoolEntry( "NoDisplay" );
	result.append( s );
      }
    }
    else
      bInvalidMenu = true;

    if ( bInvalidMenu )
    {
      QString tmp = i18n("The desktop entry file\n%1\n has an invalid menu entry\n%2").arg( _url.path()).arg( *it );
      KMessageBoxWrapper::error( 0, tmp );
    }
  }

  return result;
}


kdelibs'KDEDesktopMimeType::executeService() (./kdelibs/kio/kmimetype.cpp:714)

void KDEDesktopMimeType::executeService( const QString& _url, KDEDesktopMimeType::Service& _service )
{
  kdDebug(7009) << "EXECUTING Service " << _service.m_strName.data() << endl;

  KURL u( _url );

  if ( _service.m_type == ST_USER_DEFINED )
  {
    KURL::List lst;
    lst.append( u );
    KRun::run( _service.m_strExec, lst, _service.m_strName, _service.m_strIcon,
	       _service.m_strIcon );
    return;
  }
  else if ( _service.m_type == ST_MOUNT || _service.m_type == ST_UNMOUNT )
  {
    //kdDebug(7009) << "MOUNT&UNMOUNT" << endl;

    KSimpleConfig cfg( u.path(), true );
    cfg.setDesktopGroup();
    QString dev = cfg.readEntry( "Dev" );
    if ( dev.isEmpty() )
    {
      QString tmp = i18n("The desktop entry file\n%1\nis of type FSDevice but has no Dev=... entry").arg( u.path() );
      KMessageBoxWrapper::error( 0, tmp );
      return;
    }
    QString mp = KIO::findDeviceMountPoint( dev.ascii() );

    if ( _service.m_type == ST_MOUNT )
    {
      // Already mounted? Strange, but who knows ...
      if ( !mp.isEmpty() )
      {
	kdDebug(7009) << "ALREADY Mounted" << endl;
	return;
      }

      bool ro = cfg.readBoolEntry( "ReadOnly", false );

      (void)new KAutoMount( ro, 0L, dev.ascii(), 0L, u.path(), false );
    }
    else if ( _service.m_type == ST_UNMOUNT )
    {
      // Not mounted? Strange, but who knows ...
      if ( mp.isEmpty() )
	return;

      (void)new KAutoUnmount( mp, u.path() );
    }
  }
  else
    assert( 0 );
}