File indexing completed on 2024-04-28 15:39:07

0001 // SPDX-FileCopyrightText: 2020-2022 Tobias Leupold <tl at stonemx dot de>
0002 //
0003 // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0004 
0005 // Local includes
0006 #include "MimeHelper.h"
0007 #include "Logging.h"
0008 
0009 // Qt includes
0010 #include <QHash>
0011 #include <QMimeDatabase>
0012 #include <QMimeData>
0013 #include <QUrl>
0014 
0015 namespace MimeHelper
0016 {
0017 
0018 static const QVector<QString> s_usableImages {
0019     QStringLiteral("image/jpeg"),
0020     QStringLiteral("image/png"),
0021     QStringLiteral("image/webp"),
0022     QStringLiteral("image/tiff"),
0023     QStringLiteral("image/openraster"),
0024     QStringLiteral("application/x-krita")
0025 };
0026 
0027 static const QVector<QString> s_rawImageMimeTypes {
0028     QStringLiteral("image/x-canon-cr2"),
0029     QStringLiteral("image/x-nikon-nef"),
0030     QStringLiteral("image/x-adobe-dng")
0031 };
0032 
0033 static const QVector<QString> s_usableGeoData {
0034     QStringLiteral("application/x-gpx+xml"),
0035     QStringLiteral("application/xml+gpx")
0036 };
0037 
0038 static const QHash<KGeoTag::DropTarget, QVector<QString>> s_usableTypes {
0039     { KGeoTag::DroppedOnImageList, s_usableImages },
0040     { KGeoTag::DroppedOnMap,       s_usableGeoData },
0041     { KGeoTag::DroppedOnTrackList, s_usableGeoData }
0042 };
0043 
0044 static const QMimeDatabase s_mimeDB;
0045 
0046 QVector<QString> getUsablePaths(KGeoTag::DropTarget dropTarget, const QMimeData *data)
0047 {
0048     if (! data->hasUrls()) {
0049         return { };
0050     }
0051 
0052     QVector<QString> usablePaths;
0053     const auto urls = data->urls();
0054     for (const auto &url : urls) {
0055         if (! url.isLocalFile()) {
0056             continue;
0057         }
0058 
0059         const auto path = url.toLocalFile();
0060         const auto type = s_mimeDB.mimeTypeForFile(path);
0061         for (const auto &possibleType : s_usableTypes.value(dropTarget)) {
0062             if (type.inherits(possibleType)) {
0063                 usablePaths.append(path);
0064                 break;
0065             }
0066         }
0067     }
0068 
0069     return usablePaths;
0070 }
0071 
0072 QString mimeType(const QString &path)
0073 {
0074     return s_mimeDB.mimeTypeForFile(path).name();
0075 }
0076 
0077 KGeoTag::FileType classifyFile(const QString &path)
0078 {
0079     const auto type = s_mimeDB.mimeTypeForFile(path);
0080     qCDebug(KGeoTagLog) << "MIME type for" << path << "is" << type.name();
0081 
0082     for (const auto &possibleType : s_usableImages) {
0083         if (type.inherits(possibleType)) {
0084             qCDebug(KGeoTagLog) << type.name() << "inherits or is" << possibleType
0085                                 << "--> image file";
0086             return KGeoTag::ImageFile;
0087         }
0088     }
0089 
0090     for (const auto &possibleType : s_usableGeoData) {
0091         if (type.inherits(possibleType)) {
0092             qCDebug(KGeoTagLog) << type.name() << "inherits or is" << possibleType
0093                                 << "--> geodata file";
0094             return KGeoTag::GeoDataFile;
0095         }
0096     }
0097 
0098     qCDebug(KGeoTagLog) << type.name() << "can't be used";
0099     return KGeoTag::UnsupportedFile;
0100 }
0101 
0102 bool isRawImage(const QString &path)
0103 {
0104     return s_rawImageMimeTypes.contains(mimeType(path));
0105 }
0106 
0107 }