• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 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
30class KFolderMimeTypePrivate : public KMimeTypePrivate
31{
32public:
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
54KFolderMimeType::KFolderMimeType( const QString& fullpath, const QString& name, const QString& comment )
55 : KMimeType(*new KFolderMimeTypePrivate(fullpath), name, comment )
56{
57}
58
59KFolderMimeType::KFolderMimeType( QDataStream& str, int offset )
60 : KMimeType( *new KFolderMimeTypePrivate(str, offset))
61{
62}
63
64KFolderMimeType::~KFolderMimeType()
65{
66}
67
68QString 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
129QString 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}
KConfigGroup
A class for one specific group in a KConfig object.
Definition: kconfiggroup.h:54
KDesktopFile
KDE Desktop File Management.
Definition: kdesktopfile.h:39
KFolderMimeType::KFolderMimeType
KFolderMimeType(const QString &fullpath, const QString &name, const QString &comment)
Definition: kfoldermimetype.cpp:54
KFolderMimeType::~KFolderMimeType
~KFolderMimeType()
Destructor.
Definition: kfoldermimetype.cpp:64
KMimeTypePrivate
Definition: kmimetype_p.h:25
KMimeTypePrivate::iconName
virtual QString iconName(const KUrl &) const
Definition: kmimetype.cpp:785
KMimeTypePrivate::comment
virtual QString comment(const KUrl &=KUrl()) const
Definition: kmimetype_p.h:42
KMimeType
Represent a mime type, like "text/plain", and the data that is associated with it.
Definition: kmimetype.h:47
KStandardDirs::exists
static bool exists(const QString &fullPath)
Checks for existence and accessability of a file or directory.
Definition: kstandarddirs.cpp:587
KSycocaEntryPrivate::offset
int offset
Definition: ksycocaentry_p.h:75
KUrl
Represents and parses a URL.
Definition: kurl.h:112
KUrl::isLocalFile
bool isLocalFile() const
Checks whether the file is local.
Definition: kurl.cpp:924
KUrl::toLocalFile
QString toLocalFile(AdjustPathOption trailing=LeaveTrailingSlash) const
Definition: kurl.cpp:885
QString
kconfiggroup.h
kdesktopfile.h
kfoldermimetype.h
kmimetype_p.h
kstandarddirs.h
K_SYCOCATYPE
#define K_SYCOCATYPE(type, baseclass)
Definition: ksycocaentry_p.h:24
KST_KFolderMimeType
@ KST_KFolderMimeType
Definition: ksycocatype.h:32
KMacroExpander::group
@ group
Definition: kmacroexpander_unix.cpp:34
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 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.38 API Reference

Skip menu "kdelibs-4.14.38 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