• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KDECore

kfoldermimetype.cpp

Go to the documentation of this file.
00001 /*  This file is part of the KDE libraries
00002  *  Copyright (C) 1999 Waldo Bastian <bastian@kde.org>
00003  *                2000, 2007 David Faure   <faure@kde.org>
00004  *
00005  *  This library is free software; you can redistribute it and/or
00006  *  modify it under the terms of the GNU Library General Public
00007  *  License version 2 as published by the Free Software Foundation;
00008  *
00009  *  This library is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  *  Library General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU Library General Public License
00015  *  along with this library; see the file COPYING.LIB.  If not, write to
00016  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  *  Boston, MA 02110-1301, USA.
00018  **/
00019 
00020 #include "kfoldermimetype.h"
00021 #include "kmimetype_p.h"
00022 #include <kdesktopfile.h>
00023 #include <kstandarddirs.h>
00024 #include <kconfiggroup.h>
00025 #include <dirent.h>
00026 #include <QtCore/QFile>
00027 #include <QtCore/QSet>
00028 
00029 class KFolderMimeTypePrivate : public KMimeTypePrivate
00030 {
00031 public:
00032     K_SYCOCATYPE( KST_KFolderMimeType, KMimeTypePrivate )
00033 
00034     KFolderMimeTypePrivate(const QString &s)
00035         : KMimeTypePrivate(s)
00036     {}
00037 
00038     KFolderMimeTypePrivate(QDataStream& str, int offset)
00039         : KMimeTypePrivate(str, offset)
00040     {}
00041 
00042     virtual QString comment(const KUrl &url) const;
00043     virtual QString iconName(const KUrl &url) const;
00044 
00045 };
00046 
00047 /*******************************************************
00048  *
00049  * KFolderMimeType
00050  *
00051  ******************************************************/
00052 
00053 KFolderMimeType::KFolderMimeType( const QString& fullpath, const QString& name, const QString& comment )
00054     : KMimeType(*new KFolderMimeTypePrivate(fullpath), name, comment )
00055 {
00056 }
00057 
00058 KFolderMimeType::KFolderMimeType( QDataStream& str, int offset )
00059     : KMimeType( *new KFolderMimeTypePrivate(str, offset))
00060 {
00061 }
00062 
00063 KFolderMimeType::~KFolderMimeType()
00064 {
00065 }
00066 
00067 QString KFolderMimeTypePrivate::iconName( const KUrl& _url ) const
00068 {
00069   if ( _url.isEmpty() || !_url.isLocalFile() )
00070     return KMimeTypePrivate::iconName( _url );
00071 
00072   KUrl u( _url );
00073   u.addPath( ".directory" );
00074 
00075   QString icon;
00076   // using KStandardDirs as this one checks for path being
00077   // a file instead of a directory
00078   if ( KStandardDirs::exists( u.path() ) )
00079   {
00080     KDesktopFile cfg( u.path() );
00081     KConfigGroup group = cfg.desktopGroup();
00082     icon = group.readEntry( "Icon" );
00083     QString empty_icon = group.readEntry( "EmptyIcon" );
00084 
00085     if ( !empty_icon.isEmpty() )
00086     {
00087       bool isempty = false;
00088       DIR *dp = 0L;
00089       struct dirent *ep;
00090       dp = opendir( QFile::encodeName(_url.path()) );
00091       if ( dp )
00092       {
00093         QSet<QByteArray> entries;
00094         // Note that readdir isn't guaranteed to return "." and ".." first (#79826)
00095         ep=readdir( dp ); if ( ep ) entries.insert( ep->d_name );
00096         ep=readdir( dp ); if ( ep ) entries.insert( ep->d_name );
00097         if ( (ep=readdir( dp )) == 0L ) // third file is NULL entry -> empty directory
00098           isempty = true;
00099         else {
00100           entries.insert( ep->d_name );
00101           if ( readdir( dp ) == 0 ) { // only three
00102             // check if we got "." ".." and ".directory"
00103             isempty = entries.contains( "." ) &&
00104                       entries.contains( ".." ) &&
00105                       entries.contains( ".directory" );
00106           }
00107         }
00108         if (!isempty && !strcmp(ep->d_name, ".directory"))
00109           isempty = (readdir(dp) == 0L);
00110         closedir( dp );
00111       }
00112 
00113       if ( isempty )
00114         return empty_icon;
00115     }
00116   }
00117 
00118   if ( icon.isEmpty() )
00119     return KMimeTypePrivate::iconName( _url );
00120 
00121   if ( icon.startsWith( "./" ) ) {
00122     // path is relative with respect to the location
00123     // of the .directory file (#73463)
00124     KUrl v( _url );
00125     v.addPath( icon.mid( 2 ) );
00126     icon = v.path();
00127   }
00128 
00129   return icon;
00130 }
00131 
00132 QString KFolderMimeTypePrivate::comment( const KUrl& _url ) const
00133 {
00134     if ( _url.isEmpty() || !_url.isLocalFile() )
00135         return KMimeTypePrivate::comment( _url );
00136 
00137     KUrl u( _url );
00138     u.addPath( ".directory" );
00139 
00140     const KDesktopFile cfg( u.path() );
00141     QString comment = cfg.readComment();
00142     if ( comment.isEmpty() )
00143         return KMimeTypePrivate::comment( _url );
00144 
00145     return comment;
00146 }

KDECore

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

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal