File indexing completed on 2024-05-19 12:42:34

0001 /*
0002  * This file is part of the KDE project
0003  *
0004  * SPDX-FileCopyrightText: 2013 Arjen Hiemstra <ahiemstra@heimr.nl>
0005  *
0006  * SPDX-License-Identifier: LGPL-2.0-or-later
0007  *
0008  */
0009 
0010 #include "Global.h"
0011 
0012 #include <QDebug>
0013 
0014 #include <QUrl>
0015 #include <QUrlQuery>
0016 #include <QMimeDatabase>
0017 #include <QPluginLoader>
0018 
0019 #include <KoPluginLoader.h>
0020 
0021 // For the mimetype names
0022 #include <KWDocument.h>
0023 #include <sheets/core/DocBase.h>
0024 #include <KPrDocument.h>
0025 
0026 using namespace Calligra::Components;
0027 
0028 static const QStringList staticTextTypes{ "application/pdf" };
0029 
0030 Calligra::Components::Global::Global(QObject* parent)
0031     : QObject{parent}
0032 {
0033 }
0034 
0035 int Global::documentType(const QUrl& document)
0036 {
0037     int result = DocumentType::Unknown;
0038 
0039     if (!document.isValid()) {
0040         return result;
0041     }
0042 
0043     const QUrlQuery query(document);
0044 
0045     // First, check if the URL gives us specific information on this topic (such as asking for a new file)
0046     if(query.hasQueryItem("mimetype")) {
0047         QString mime = query.queryItemValue("mimetype");
0048         if(mime == WORDS_MIME_TYPE) {
0049             result = DocumentType::TextDocument;
0050         }
0051         else if(mime == SHEETS_MIME_TYPE) {
0052             result = DocumentType::Spreadsheet;
0053         }
0054         else if(mime == STAGE_MIME_TYPE) {
0055             result = DocumentType::Presentation;
0056         }
0057     }
0058     else {
0059         QMimeType mime = QMimeDatabase{}.mimeTypeForUrl(document);
0060 
0061         // TODO: see if KoPluginLoader could provide this info via some metadata query instead
0062         QList<QPluginLoader*> plugins = KoPluginLoader::pluginLoaders(QStringLiteral("calligra/parts"), mime.name());
0063 
0064         for (int i = 0; i < plugins.count(); i++) {
0065             QPluginLoader* loader = plugins.at(i);
0066 
0067             if(loader->fileName().contains("words")) {
0068                 result = DocumentType::TextDocument;
0069                 break;
0070             } else if(loader->fileName().contains("sheets")) {
0071                 result = DocumentType::Spreadsheet;
0072                 break;
0073             } else if(loader->fileName().contains("stage")) {
0074                 result = DocumentType::Presentation;
0075                 break;
0076             }
0077         }
0078 
0079         // cleanup
0080         qDeleteAll(plugins);
0081 
0082         // Since we don't actually have a Calligra plugin that handles these...
0083         if ((result == DocumentType::Unknown) && staticTextTypes.contains(mime.name())) {
0084             result = DocumentType::StaticTextDocument;
0085         }
0086     }
0087 
0088     return result;
0089 }