Warning, file /office/calligra/libs/main/KoDocumentEntry.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* This file is part of the KDE project
0002    Copyright (C) 1998, 1999 Torben Weis <weis@kde.org>
0003    Copyright     2007       David Faure <faure@kde.org>
0004 
0005    This library is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU Library General Public
0007    License as published by the Free Software Foundation; either
0008    version 2 of the License, or (at your option) any later version.
0009 
0010    This library is distributed in the hope that it will be useful,
0011    but WITHOUT ANY WARRANTY; without even the implied warranty of
0012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013    Library General Public License for more details.
0014 
0015    You should have received a copy of the GNU Library General Public License
0016    along with this library; see the file COPYING.LIB.  If not, write to
0017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019 */
0020 
0021 #include "KoDocumentEntry.h"
0022 
0023 #include "KoPart.h"
0024 #include "KoDocument.h"
0025 #include "KoFilter.h"
0026 #include <MainDebug.h>
0027 
0028 #include <KoPluginLoader.h>
0029 #include <KoConfig.h> // CALLIGRA_OLD_PLUGIN_METADATA
0030 
0031 #include <kservicetype.h>
0032 #include <kpluginfactory.h>
0033 
0034 #include <QCoreApplication>
0035 
0036 #include <limits.h> // UINT_MAX
0037 
0038 KoDocumentEntry::KoDocumentEntry()
0039         : m_loader(0)
0040 {
0041 }
0042 
0043 KoDocumentEntry::KoDocumentEntry(QPluginLoader *loader)
0044         : m_loader(loader)
0045 {
0046 }
0047 
0048 KoDocumentEntry::~KoDocumentEntry()
0049 {
0050 }
0051 
0052 
0053 QJsonObject KoDocumentEntry::metaData() const
0054 {
0055     return m_loader ? m_loader->metaData().value("MetaData").toObject() : QJsonObject();
0056 }
0057 
0058 QString KoDocumentEntry::fileName() const
0059 {
0060     return m_loader ? m_loader->fileName() : QString();
0061 }
0062 
0063 /**
0064  * @return TRUE if the service pointer is null
0065  */
0066 bool KoDocumentEntry::isEmpty() const {
0067     return (m_loader == 0);
0068 }
0069 
0070 /**
0071  * @return name of the associated service
0072  */
0073 QString KoDocumentEntry::name() const {
0074     QJsonObject json = metaData();
0075     json = json.value("KPlugin").toObject();
0076     return json.value("Name").toString();
0077 }
0078 
0079 /**
0080  *  Mimetypes (and other service types) which this document can handle.
0081  */
0082 QStringList KoDocumentEntry::mimeTypes() const {
0083     QJsonObject json = metaData();
0084 #ifdef CALLIGRA_OLD_PLUGIN_METADATA
0085     return json.value("MimeType").toString().split(';', QString::SkipEmptyParts);
0086 #else
0087     QJsonObject pluginData = json.value("KPlugin").toObject();
0088     return pluginData.value("MimeTypes").toVariant().toStringList();
0089 #endif
0090 }
0091 
0092 /**
0093  *  @return TRUE if the document can handle the requested mimetype.
0094  */
0095 bool KoDocumentEntry::supportsMimeType(const QString & _mimetype) const {
0096     return mimeTypes().contains(_mimetype);
0097 }
0098 
0099 KoPart *KoDocumentEntry::createKoPart(QString* errorMsg) const
0100 {
0101     if (!m_loader) {
0102         return 0;
0103     }
0104 
0105     QObject *obj = m_loader->instance();
0106     KPluginFactory *factory = qobject_cast<KPluginFactory *>(obj);
0107     KoPart *part = factory->create<KoPart>(0, QVariantList());
0108 
0109     if (!part) {
0110         if (errorMsg)
0111             *errorMsg = m_loader->errorString();
0112         return 0;
0113     }
0114 
0115     return part;
0116 }
0117 
0118 KoDocumentEntry KoDocumentEntry::queryByMimeType(const QString & mimetype)
0119 {
0120     QList<KoDocumentEntry> vec = query(mimetype);
0121 
0122     if (vec.isEmpty()) {
0123         warnMain << "Got no results with " << mimetype;
0124         // Fallback to the old way (which was probably wrong, but better be safe)
0125         vec = query(mimetype);
0126 
0127         if (vec.isEmpty()) {
0128             // Still no match. Either the mimetype itself is unknown, or we have no service for it.
0129             // Help the user debugging stuff by providing some more diagnostics
0130             if (!KServiceType::serviceType(mimetype)) {
0131                 errorMain << "Unknown Calligra MimeType " << mimetype << "." << endl;
0132                 errorMain << "Check your installation (for instance, run 'kde4-config --path mime' and check the result)." << endl;
0133             } else {
0134                 errorMain << "Found no Calligra part able to handle " << mimetype << "!" << endl;
0135                 errorMain << "Check your installation (does the desktop file have X-KDE-NativeMimeType and Calligra/Part, did you install Calligra in a different prefix than KDE, without adding the prefix to /etc/kderc ?)" << endl;
0136             }
0137             return KoDocumentEntry();
0138         }
0139     }
0140 
0141     // Filthy hack alert -- this'll be properly fixed in the mvc branch.
0142     if (qApp->applicationName() == "flow" && vec.size() == 2) {
0143         return KoDocumentEntry(vec[1]);
0144     }
0145 
0146     return KoDocumentEntry(vec[0]);
0147 }
0148 
0149 QList<KoDocumentEntry> KoDocumentEntry::query(const QString & mimetype)
0150 {
0151 
0152     QList<KoDocumentEntry> lst;
0153 
0154     // Query the trader
0155     const QList<QPluginLoader *> offers = KoPluginLoader::pluginLoaders(QStringLiteral("calligra/parts"), mimetype);
0156 
0157     foreach(QPluginLoader *pluginLoader, offers) {
0158         lst.append(KoDocumentEntry(pluginLoader));
0159     }
0160 
0161     if (lst.count() > 1 && !mimetype.isEmpty()) {
0162         warnMain << "KoDocumentEntry::query " << mimetype << " got " << lst.count() << " offers!";
0163         foreach(const KoDocumentEntry &entry, lst) {
0164             warnMain << entry.name();
0165         }
0166     }
0167 
0168     return lst;
0169 }