Warning, file /office/calligra/libs/text/KoInlineObjectRegistry.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) 2006-2010 Thomas Zander <zander@kde.org>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Library General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2 of the License, or (at your option) any later version.
0008  *
0009  * This library is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012  * Library General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU Library General Public License
0015  * along with this library; see the file COPYING.LIB.  If not, write to
0016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018  */
0019 
0020 #include "KoInlineObjectRegistry.h"
0021 #include "KoInlineObjectFactoryBase.h"
0022 #include "InsertVariableAction_p.h"
0023 
0024 #include <KoCanvasBase.h>
0025 #include <KoInlineObject.h>
0026 #include <KoXmlReader.h>
0027 #include <KoPluginLoader.h>
0028 
0029 #include <QGlobalStatic>
0030 
0031 #include "TextDebug.h"
0032 
0033 Q_GLOBAL_STATIC(KoInlineObjectRegistry, s_instance)
0034 
0035 
0036 class Q_DECL_HIDDEN KoInlineObjectRegistry::Private
0037 {
0038 public:
0039     void insertFactory(KoInlineObjectFactoryBase *factory);
0040     void init(KoInlineObjectRegistry *q);
0041 
0042     QHash<QPair<QString, QString>, KoInlineObjectFactoryBase *> factories;
0043 };
0044 
0045 void KoInlineObjectRegistry::Private::init(KoInlineObjectRegistry *q)
0046 {
0047     KoPluginLoader::PluginsConfig config;
0048     config.whiteList = "TextInlinePlugins";
0049     config.blacklist = "TextInlinePluginsDisabled";
0050     config.group = "calligra";
0051     KoPluginLoader::load(QStringLiteral("calligra/textinlineobjects"), config);
0052 
0053     foreach (KoInlineObjectFactoryBase *factory, q->values()) {
0054         QString nameSpace = factory->odfNameSpace();
0055         if (nameSpace.isEmpty() || factory->odfElementNames().isEmpty()) {
0056             debugText << "Variable factory" << factory->id() << " does not have odfNameSpace defined, ignoring";
0057         } else {
0058             foreach (const QString &elementName, factory->odfElementNames()) {
0059                 factories.insert(QPair<QString, QString>(nameSpace, elementName), factory);
0060 
0061                 debugText << "Inserting variable factory" << factory->id() << " for"
0062                     << nameSpace << ":" << elementName;
0063             }
0064         }
0065     }
0066 }
0067 
0068 KoInlineObjectRegistry* KoInlineObjectRegistry::instance()
0069 {
0070     if (!s_instance.exists()) {
0071         s_instance->d->init(s_instance);
0072     }
0073     return s_instance;
0074 }
0075 
0076 QList<QAction*> KoInlineObjectRegistry::createInsertVariableActions(KoCanvasBase *host) const
0077 {
0078     QList<QAction*> answer;
0079     foreach (const QString &key, keys()) {
0080         KoInlineObjectFactoryBase *factory = value(key);
0081         if (factory->type() == KoInlineObjectFactoryBase::TextVariable) {
0082             foreach (const KoInlineObjectTemplate &templ, factory->templates()) {
0083                 answer.append(new InsertVariableAction(host, factory, templ));
0084             }
0085 #ifndef NDEBUG
0086            if (factory->templates().isEmpty()) {
0087                 warnText << "Variable factory" << factory->id() << "has no templates, skipping.";
0088            }
0089 #endif
0090         }
0091     }
0092     return answer;
0093 }
0094 
0095 KoInlineObject *KoInlineObjectRegistry::createFromOdf(const KoXmlElement &element, KoShapeLoadingContext &context) const
0096 {
0097     KoInlineObjectFactoryBase *factory = d->factories.value(
0098             QPair<QString, QString>(element.namespaceURI(), element.tagName()));
0099     if (factory == 0) {
0100         debugText << "No factory for" << element.namespaceURI() << ":" << element.tagName();
0101         return 0;
0102     }
0103 
0104     KoInlineObject *object = factory->createInlineObject(0);
0105     if (object) {
0106         object->loadOdf(element, context);
0107     }
0108 
0109     return object;
0110 }
0111 
0112 KoInlineObjectRegistry::~KoInlineObjectRegistry()
0113 {
0114     qDeleteAll(doubleEntries());
0115     qDeleteAll(values());
0116     delete d;
0117 }
0118 
0119 KoInlineObjectRegistry::KoInlineObjectRegistry()
0120         : d(new Private())
0121 {
0122 }