File indexing completed on 2024-04-28 04:18:50

0001 // vim: set tabstop=4 shiftwidth=4 expandtab
0002 /*
0003 Gwenview - A simple image viewer for KDE
0004 Copyright 2000-2007 Aurélien Gâteau <agateau@kde.org>
0005 
0006 This program is free software; you can redistribute it and/or
0007 modify it under the terms of the GNU General Public License
0008 as published by the Free Software Foundation; either version 2
0009 of the License, or (at your option) any later version.
0010 
0011 This program is distributed in the hope that it will be useful,
0012 but WITHOUT ANY WARRANTY; without even the implied warranty of
0013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014 GNU General Public License for more details.
0015 
0016 You should have received a copy of the GNU General Public License
0017 along with this program; if not, write to the Free Software
0018 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
0019 
0020 */
0021 // Self
0022 #include "archiveutils.h"
0023 
0024 // Qt
0025 #include <QMimeDatabase>
0026 
0027 // KF
0028 #include <KFileItem>
0029 #include <KProtocolManager>
0030 
0031 // Local
0032 #include "gwenview_lib_debug.h"
0033 
0034 namespace Gwenview
0035 {
0036 namespace ArchiveUtils
0037 {
0038 bool fileItemIsArchive(const KFileItem &item)
0039 {
0040     const QMimeType mimeType = item.determineMimeType();
0041     if (!mimeType.isValid()) {
0042         qCWarning(GWENVIEW_LIB_LOG) << "determineMimeType() returned a null pointer";
0043         return false;
0044     }
0045     return !ArchiveUtils::protocolForMimeType(mimeType.name()).isEmpty();
0046 }
0047 
0048 bool fileItemIsDirOrArchive(const KFileItem &item)
0049 {
0050     return item.isDir() || fileItemIsArchive(item);
0051 }
0052 
0053 QString protocolForMimeType(const QString &mimeType)
0054 {
0055     static QHash<QString, QString> cache;
0056     QHash<QString, QString>::ConstIterator it = cache.constFind(mimeType);
0057     if (it != cache.constEnd()) {
0058         return it.value();
0059     }
0060 
0061     if (mimeType == QLatin1String("image/svg+xml-compressed")) {
0062         // We don't want .svgz to be considered as archives because QtSvg knows
0063         // how to decode gzip-ed svg files
0064         cache.insert(mimeType, QString());
0065         return {};
0066     }
0067 
0068     QString protocol = KProtocolManager::protocolForArchiveMimetype(mimeType);
0069     if (protocol.isEmpty()) {
0070         // No protocol, try with mimeType parents. This is useful for .cbz for
0071         // example
0072         const QMimeType mime = QMimeDatabase().mimeTypeForName(mimeType);
0073         const QStringList allAncestors = mime.allAncestors();
0074         for (const QString &parentMimeType : allAncestors) {
0075             protocol = KProtocolManager::protocolForArchiveMimetype(parentMimeType);
0076             if (!protocol.isEmpty()) {
0077                 break;
0078             }
0079         }
0080     }
0081 
0082     cache.insert(mimeType, protocol);
0083     return protocol;
0084 }
0085 
0086 } // namespace ArchiveUtils
0087 
0088 } // namespace Gwenview