File indexing completed on 2024-04-28 16:13:35

0001 /*
0002  * This file is part of the KDE project
0003  *
0004  * Copyright (C) 2013 Arjen Hiemstra <ahiemstra@heimr.nl>
0005  *
0006  * This library is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU Library General Public
0008  * License as published by the Free Software Foundation; either
0009  * version 2 of the License, or (at your option) any later version.
0010  *
0011  * This library 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 GNU
0014  * Library General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU Library General Public License
0017  * along with this library; see the file COPYING.LIB.  If not, write to
0018  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0019  * Boston, MA 02110-1301, USA.
0020  *
0021  */
0022 
0023 #include "Global.h"
0024 
0025 #include <QDebug>
0026 
0027 #include <QUrl>
0028 #include <QUrlQuery>
0029 #include <QMimeDatabase>
0030 #include <QPluginLoader>
0031 
0032 #include <KoPluginLoader.h>
0033 
0034 // For the mimetype names
0035 #include <KWDocument.h>
0036 #include <DocBase.h>
0037 #include <KPrDocument.h>
0038 
0039 using namespace Calligra::Components;
0040 
0041 static const QStringList staticTextTypes{ "application/pdf" };
0042 
0043 Calligra::Components::Global::Global(QObject* parent)
0044     : QObject{parent}
0045 {
0046 }
0047 
0048 int Global::documentType(const QUrl& document)
0049 {
0050     int result = DocumentType::Unknown;
0051 
0052     if (!document.isValid()) {
0053         return result;
0054     }
0055 
0056     const QUrlQuery query(document);
0057 
0058     // First, check if the URL gives us specific information on this topic (such as asking for a new file)
0059     if(query.hasQueryItem("mimetype")) {
0060         QString mime = query.queryItemValue("mimetype");
0061         if(mime == WORDS_MIME_TYPE) {
0062             result = DocumentType::TextDocument;
0063         }
0064         else if(mime == SHEETS_MIME_TYPE) {
0065             result = DocumentType::Spreadsheet;
0066         }
0067         else if(mime == STAGE_MIME_TYPE) {
0068             result = DocumentType::Presentation;
0069         }
0070     }
0071     else {
0072         QMimeType mime = QMimeDatabase{}.mimeTypeForUrl(document);
0073 
0074         // TODO: see if KoPluginLoader could provide this info via some metadata query instead
0075         QList<QPluginLoader*> plugins = KoPluginLoader::pluginLoaders(QStringLiteral("calligra/parts"), mime.name());
0076 
0077         for (int i = 0; i < plugins.count(); i++) {
0078             QPluginLoader* loader = plugins.at(i);
0079 
0080             if(loader->fileName().contains("words")) {
0081                 result = DocumentType::TextDocument;
0082                 break;
0083             } else if(loader->fileName().contains("sheets")) {
0084                 result = DocumentType::Spreadsheet;
0085                 break;
0086             } else if(loader->fileName().contains("stage")) {
0087                 result = DocumentType::Presentation;
0088                 break;
0089             }
0090         }
0091 
0092         // cleanup
0093         qDeleteAll(plugins);
0094 
0095         // Since we don't actually have a Calligra plugin that handles these...
0096         if ((result == DocumentType::Unknown) && staticTextTypes.contains(mime.name())) {
0097             result = DocumentType::StaticTextDocument;
0098         }
0099     }
0100 
0101     return result;
0102 }