File indexing completed on 2024-04-21 04:58:20
0001 /* This file is part of the KDE project 0002 SPDX-FileCopyrightText: 1999, 2007 David Faure <faure@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "konqsettings.h" 0008 #include <kprotocolmanager.h> 0009 0010 #include <kdesktopfile.h> 0011 #include "konqdebug.h" 0012 #include <kconfiggroup.h> 0013 #include <QMimeDatabase> 0014 #include <QMimeType> 0015 0016 class KonqEmbedSettingsSingleton 0017 { 0018 public: 0019 KonqFMSettings self; 0020 }; 0021 Q_GLOBAL_STATIC(KonqEmbedSettingsSingleton, globalEmbedSettings) 0022 0023 KonqFMSettings *KonqFMSettings::settings() 0024 { 0025 return &globalEmbedSettings->self; 0026 } 0027 0028 //static 0029 void KonqFMSettings::reparseConfiguration() 0030 { 0031 if (globalEmbedSettings.exists()) { 0032 globalEmbedSettings->self.init(true); 0033 } 0034 } 0035 KonqFMSettings::KonqFMSettings() 0036 { 0037 init(false); 0038 } 0039 0040 KonqFMSettings::~KonqFMSettings() 0041 { 0042 } 0043 0044 void KonqFMSettings::init(bool reparse) 0045 { 0046 if (reparse) { 0047 fileTypesConfig()->reparseConfiguration(); 0048 } 0049 m_embedMap = fileTypesConfig()->entryMap(QStringLiteral("EmbedSettings")); 0050 } 0051 0052 KSharedConfig::Ptr KonqFMSettings::fileTypesConfig() 0053 { 0054 if (!m_fileTypesConfig) { 0055 m_fileTypesConfig = KSharedConfig::openConfig(QStringLiteral("filetypesrc"), KConfig::NoGlobals); 0056 } 0057 return m_fileTypesConfig; 0058 } 0059 0060 static bool alwaysEmbedMimeTypeGroup(const QString &mimeType) 0061 { 0062 if (mimeType.startsWith(QLatin1String("inode")) || mimeType.startsWith(QLatin1String("Browser")) || mimeType.startsWith(QLatin1String("Konqueror"))) { 0063 return true; //always embed mimetype inode/*, Browser/* and Konqueror/* 0064 } 0065 return false; 0066 } 0067 0068 bool KonqFMSettings::shouldEmbed(const QString &_mimeType) const 0069 { 0070 QMimeDatabase db; 0071 QMimeType mime = db.mimeTypeForName(_mimeType); 0072 if (!mime.isValid()) { 0073 qCWarning(KONQUEROR_LOG) << "Unknown mimetype" << _mimeType; 0074 return false; // unknown mimetype! 0075 } 0076 const QString mimeType = mime.name(); 0077 0078 // First check in user's settings whether to embed or not 0079 // 1 - in the filetypesrc config file (written by the configuration module) 0080 QMap<QString, QString>::const_iterator it = m_embedMap.find(QLatin1String("embed-") + mimeType); 0081 if (it != m_embedMap.end()) { 0082 qCDebug(KONQUEROR_LOG) << mimeType << it.value(); 0083 return it.value() == QLatin1String("true"); 0084 } 0085 // 2 - in the configuration for the group if nothing was found in the mimetype 0086 if (alwaysEmbedMimeTypeGroup(mimeType)) { 0087 return true; //always embed mimetype inode/*, Browser/* and Konqueror/* 0088 } 0089 const QString mimeTypeGroup = mimeType.left(mimeType.indexOf('/')); 0090 it = m_embedMap.find(QLatin1String("embed-") + mimeTypeGroup); 0091 if (it != m_embedMap.end()) { 0092 qCDebug(KONQUEROR_LOG) << mimeType << "group setting:" << it.value(); 0093 return it.value() == QLatin1String("true"); 0094 } 0095 // 2 bis - configuration for group of parent mimetype, if different 0096 if (mimeType[0].isLower()) { 0097 QStringList parents; 0098 parents.append(mimeType); 0099 while (!parents.isEmpty()) { 0100 const QString parent = parents.takeFirst(); 0101 if (alwaysEmbedMimeTypeGroup(parent)) { 0102 return true; 0103 } 0104 QMimeType mime = db.mimeTypeForName(parent); 0105 Q_ASSERT(mime.isValid()); // how could the -parent- be invalid? 0106 if (mime.isValid()) { 0107 parents += mime.parentMimeTypes(); 0108 } 0109 } 0110 } 0111 0112 // 3 - if no config found, use default. 0113 // Note: if you change those defaults, also change keditfiletype/mimetypedata.cpp ! 0114 // Embedding is false by default except for image/*, text/html and for zip, tar etc. 0115 const bool hasLocalProtocolRedirect = !KProtocolManager::protocolForArchiveMimetype(mimeType).isEmpty(); 0116 if (mimeTypeGroup == QLatin1String("image") 0117 || mime.inherits(QLatin1String("text/html")) || mime.inherits(QLatin1String("application/xhtml+xml")) 0118 || mimeTypeGroup == QLatin1String("multipart") 0119 || hasLocalProtocolRedirect) { 0120 return true; 0121 } 0122 return false; 0123 }