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 <kde_file.h> 00026 #include <QtCore/QFile> 00027 #include <QtCore/QSet> 00028 #include <QtCore/QDirIterator> 00029 00030 class KFolderMimeTypePrivate : public KMimeTypePrivate 00031 { 00032 public: 00033 K_SYCOCATYPE( KST_KFolderMimeType, KMimeTypePrivate ) 00034 00035 KFolderMimeTypePrivate(const QString &s) 00036 : KMimeTypePrivate(s) 00037 {} 00038 00039 KFolderMimeTypePrivate(QDataStream& str, int offset) 00040 : KMimeTypePrivate(str, offset) 00041 {} 00042 00043 virtual QString comment(const KUrl &url) const; 00044 virtual QString iconName(const KUrl &url) const; 00045 00046 }; 00047 00048 /******************************************************* 00049 * 00050 * KFolderMimeType 00051 * 00052 ******************************************************/ 00053 00054 KFolderMimeType::KFolderMimeType( const QString& fullpath, const QString& name, const QString& comment ) 00055 : KMimeType(*new KFolderMimeTypePrivate(fullpath), name, comment ) 00056 { 00057 } 00058 00059 KFolderMimeType::KFolderMimeType( QDataStream& str, int offset ) 00060 : KMimeType( *new KFolderMimeTypePrivate(str, offset)) 00061 { 00062 } 00063 00064 KFolderMimeType::~KFolderMimeType() 00065 { 00066 } 00067 00068 QString KFolderMimeTypePrivate::iconName( const KUrl& _url ) const 00069 { 00070 if ( _url.isEmpty() || !_url.isLocalFile() ) 00071 return KMimeTypePrivate::iconName( _url ); 00072 00073 // Stating .directory files can cause long freezes when e.g. /home 00074 // uses autofs for every user's home directory, i.e. opening /home 00075 // in a file dialog will mount every single home directory. 00076 // These non-mounted directories can be identified by having 0 size. 00077 // There are also other directories with 0 size, such as /proc, that may 00078 // be mounted, but those are unlikely to contain .directory (and checking 00079 // this would require KMountPoint from kio). 00080 KDE_struct_stat buff; 00081 if (KDE_stat( QFile::encodeName( _url.toLocalFile()), &buff ) == 0 00082 && S_ISDIR( buff.st_mode ) && buff.st_size == 0 ) { 00083 return KMimeTypePrivate::iconName( _url ); 00084 } 00085 00086 KUrl u(_url); 00087 u.addPath(QString::fromLatin1(".directory")); 00088 00089 QString icon; 00090 // using KStandardDirs as this one checks for path being 00091 // a file instead of a directory 00092 if ( KStandardDirs::exists( u.toLocalFile() ) ) 00093 { 00094 KDesktopFile cfg( u.toLocalFile() ); 00095 KConfigGroup group = cfg.desktopGroup(); 00096 icon = group.readEntry( "Icon" ); 00097 QString empty_icon = group.readEntry( "EmptyIcon" ); 00098 00099 if ( !empty_icon.isEmpty() ) 00100 { 00101 bool isempty = true; 00102 QDirIterator dirIt( _url.toLocalFile(), QDir::Dirs|QDir::Files|QDir::NoDotAndDotDot ); 00103 while ( dirIt.hasNext() ) { 00104 dirIt.next(); 00105 if ( dirIt.fileName() != QLatin1String( ".directory" ) ) { 00106 isempty = false; 00107 break; 00108 } 00109 } 00110 if ( isempty ) 00111 return empty_icon; 00112 } 00113 } 00114 00115 if ( icon.isEmpty() ) 00116 return KMimeTypePrivate::iconName( _url ); 00117 00118 if ( icon.startsWith( QLatin1String( "./" ) ) ) { 00119 // path is relative with respect to the location 00120 // of the .directory file (#73463) 00121 KUrl v( _url ); 00122 v.addPath( icon.mid( 2 ) ); 00123 icon = v.toLocalFile(); 00124 } 00125 00126 return icon; 00127 } 00128 00129 QString KFolderMimeTypePrivate::comment( const KUrl& _url ) const 00130 { 00131 if ( _url.isEmpty() || !_url.isLocalFile() ) 00132 return KMimeTypePrivate::comment( _url ); 00133 00134 KUrl u(_url); 00135 u.addPath(QString::fromLatin1(".directory")); 00136 00137 const KDesktopFile cfg( u.toLocalFile() ); 00138 QString comment = cfg.readComment(); 00139 if ( comment.isEmpty() ) 00140 return KMimeTypePrivate::comment( _url ); 00141 00142 return comment; 00143 }
KDE 4.6 API Reference