• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.20 API Reference
  • KDE Home
  • Contact Us
 

KDECore

  • kdecore
  • services
kfoldermimetype.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries
2  * Copyright (C) 1999 Waldo Bastian <bastian@kde.org>
3  * 2000, 2007 David Faure <faure@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 version 2 as published by the Free Software Foundation;
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB. If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  **/
19 
20 #include "kfoldermimetype.h"
21 #include "kmimetype_p.h"
22 #include <kdesktopfile.h>
23 #include <kstandarddirs.h>
24 #include <kconfiggroup.h>
25 #include <kde_file.h>
26 #include <QtCore/QFile>
27 #include <QtCore/QSet>
28 #include <QtCore/QDirIterator>
29 
30 class KFolderMimeTypePrivate : public KMimeTypePrivate
31 {
32 public:
33  K_SYCOCATYPE( KST_KFolderMimeType, KMimeTypePrivate )
34 
35  KFolderMimeTypePrivate(const QString &s)
36  : KMimeTypePrivate(s)
37  {}
38 
39  KFolderMimeTypePrivate(QDataStream& str, int offset)
40  : KMimeTypePrivate(str, offset)
41  {}
42 
43  virtual QString comment(const KUrl &url) const;
44  virtual QString iconName(const KUrl &url) const;
45 
46 };
47 
48 /*******************************************************
49  *
50  * KFolderMimeType
51  *
52  ******************************************************/
53 
54 KFolderMimeType::KFolderMimeType( const QString& fullpath, const QString& name, const QString& comment )
55  : KMimeType(*new KFolderMimeTypePrivate(fullpath), name, comment )
56 {
57 }
58 
59 KFolderMimeType::KFolderMimeType( QDataStream& str, int offset )
60  : KMimeType( *new KFolderMimeTypePrivate(str, offset))
61 {
62 }
63 
64 KFolderMimeType::~KFolderMimeType()
65 {
66 }
67 
68 QString KFolderMimeTypePrivate::iconName( const KUrl& _url ) const
69 {
70  if ( _url.isEmpty() || !_url.isLocalFile() )
71  return KMimeTypePrivate::iconName( _url );
72 
73  // Stating .directory files can cause long freezes when e.g. /home
74  // uses autofs for every user's home directory, i.e. opening /home
75  // in a file dialog will mount every single home directory.
76  // These non-mounted directories can be identified by having 0 size.
77  // There are also other directories with 0 size, such as /proc, that may
78  // be mounted, but those are unlikely to contain .directory (and checking
79  // this would require KMountPoint from kio).
80  KDE_struct_stat buff;
81  if (KDE_stat( QFile::encodeName( _url.toLocalFile()), &buff ) == 0
82  && S_ISDIR( buff.st_mode ) && buff.st_size == 0 ) {
83  return KMimeTypePrivate::iconName( _url );
84  }
85 
86  KUrl u(_url);
87  u.addPath(QString::fromLatin1(".directory"));
88 
89  QString icon;
90  // using KStandardDirs as this one checks for path being
91  // a file instead of a directory
92  if ( KStandardDirs::exists( u.toLocalFile() ) )
93  {
94  KDesktopFile cfg( u.toLocalFile() );
95  KConfigGroup group = cfg.desktopGroup();
96  icon = group.readEntry( "Icon" );
97  QString empty_icon = group.readEntry( "EmptyIcon" );
98 
99  if ( !empty_icon.isEmpty() )
100  {
101  bool isempty = true;
102  QDirIterator dirIt( _url.toLocalFile(), QDir::Dirs|QDir::Files|QDir::NoDotAndDotDot );
103  while ( dirIt.hasNext() ) {
104  dirIt.next();
105  if ( dirIt.fileName() != QLatin1String( ".directory" ) ) {
106  isempty = false;
107  break;
108  }
109  }
110  if ( isempty )
111  return empty_icon;
112  }
113  }
114 
115  if ( icon.isEmpty() )
116  return KMimeTypePrivate::iconName( _url );
117 
118  if ( icon.startsWith( QLatin1String( "./" ) ) ) {
119  // path is relative with respect to the location
120  // of the .directory file (#73463)
121  KUrl v( _url );
122  v.addPath( icon.mid( 2 ) );
123  icon = v.toLocalFile();
124  }
125 
126  return icon;
127 }
128 
129 QString KFolderMimeTypePrivate::comment( const KUrl& _url ) const
130 {
131  if ( _url.isEmpty() || !_url.isLocalFile() )
132  return KMimeTypePrivate::comment( _url );
133 
134  KUrl u(_url);
135  u.addPath(QString::fromLatin1(".directory"));
136 
137  const KDesktopFile cfg( u.toLocalFile() );
138  QString comment = cfg.readComment();
139  if ( comment.isEmpty() )
140  return KMimeTypePrivate::comment( _url );
141 
142  return comment;
143 }
KMacroExpander::group
Definition: kmacroexpander_unix.cpp:34
KST_KFolderMimeType
Definition: ksycocatype.h:32
KMimeType
Represent a mime type, like "text/plain", and the data that is associated with it.
Definition: kmimetype.h:46
KMimeTypePrivate::iconName
virtual QString iconName(const KUrl &) const
Definition: kmimetype.cpp:785
KUrl::toLocalFile
QString toLocalFile(AdjustPathOption trailing=LeaveTrailingSlash) const
Definition: kurl.cpp:885
QString
kdesktopfile.h
KStandardDirs::exists
static bool exists(const QString &fullPath)
Checks for existence and accessability of a file or directory.
Definition: kstandarddirs.cpp:587
KUrl
Represents and parses a URL.
Definition: kurl.h:111
kmimetype_p.h
KUrl::addPath
void addPath(const QString &txt)
Adds to the current path.
Definition: kurl.cpp:1344
K_SYCOCATYPE
#define K_SYCOCATYPE(type, baseclass)
Definition: ksycocaentry_p.h:24
KFolderMimeType::KFolderMimeType
KFolderMimeType(const QString &fullpath, const QString &name, const QString &comment)
Definition: kfoldermimetype.cpp:54
KMimeTypePrivate::comment
virtual QString comment(const KUrl &=KUrl()) const
Definition: kmimetype_p.h:42
KFolderMimeType::~KFolderMimeType
~KFolderMimeType()
Destructor.
Definition: kfoldermimetype.cpp:64
KDesktopFile
KDE Desktop File Management.
Definition: kdesktopfile.h:38
KConfigGroup
A class for one specific group in a KConfig object.
Definition: kconfiggroup.h:53
kstandarddirs.h
KServiceType::comment
QString comment() const
Returns the descriptive comment associated, if any.
Definition: kservicetype.cpp:232
KUrl::isLocalFile
bool isLocalFile() const
Checks whether the file is local.
Definition: kurl.cpp:924
KMimeTypePrivate
Definition: kmimetype_p.h:24
kconfiggroup.h
kfoldermimetype.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2016 The KDE developers.
Generated on Sun May 8 2016 19:34:17 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDECore

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

kdelibs-4.14.20 API Reference

Skip menu "kdelibs-4.14.20 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal