File indexing completed on 2024-05-12 16:35:03

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2007 Pierre Ducroquet <pinaraf@gmail.com>
0003  * Copyright (C) 2008 Thorsten Zachmann <zachmann@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 "InfoVariable.h"
0022 
0023 #include "VariablesDebug.h"
0024 
0025 #include <KoProperties.h>
0026 #include <KoShapeSavingContext.h>
0027 #include <KoXmlReader.h>
0028 #include <KoXmlWriter.h>
0029 
0030 #include <QGlobalStatic>
0031 
0032 static const struct {
0033     KoInlineObject::Property property;
0034     const char * tag;
0035     const char * saveTag;
0036 } propertyData[] = {
0037     { KoInlineObject::AuthorName, "creator", "text:creator" },
0038     { KoInlineObject::DocumentURL, "file-name", "text:file-name" },
0039     { KoInlineObject::Title, "title", "text:title" },
0040     { KoInlineObject::Subject, "subject", "text:subject" },
0041     { KoInlineObject::Keywords, "keywords", "text:keywords" },
0042     //{ KoInlineObject::Description, "description", "text:description" }
0043     { KoInlineObject::Comments, "comments", "text:comments" }
0044 };
0045 
0046 static const unsigned int numPropertyData = sizeof(propertyData) / sizeof(*propertyData);
0047 
0048 QStringList InfoVariable::tags()
0049 {
0050     QStringList tagList;
0051     for (unsigned int i = 0; i < numPropertyData; ++i) {
0052         tagList << propertyData[i].tag;
0053     }
0054     return tagList;
0055 }
0056 
0057 InfoVariable::InfoVariable()
0058         : KoVariable(true),
0059         m_type(KoInlineObject::AuthorName)
0060 {
0061 }
0062 
0063 void InfoVariable::readProperties(const KoProperties *props)
0064 {
0065     m_type = (Property) props->property("vartype").value<int>();
0066 }
0067 
0068 void InfoVariable::propertyChanged(Property property, const QVariant &value)
0069 {
0070     if (property == m_type) {
0071         setValue(value.toString());
0072     }
0073 }
0074 
0075 typedef QMap<KoInlineObject::Property, const char*> SaveMap;
0076 
0077 Q_GLOBAL_STATIC(SaveMap, s_saveInfo)
0078 
0079 void InfoVariable::saveOdf(KoShapeSavingContext & context)
0080 {
0081     KoXmlWriter *writer = &context.xmlWriter();
0082 
0083     if (!s_saveInfo.exists() ) {
0084         for (unsigned int i = 0; i < numPropertyData; ++i) {
0085             s_saveInfo->insert(propertyData[i].property, propertyData[i].saveTag);
0086         }
0087     }
0088     const char *nodeName = s_saveInfo->value(m_type, 0);
0089     if (nodeName) {
0090         writer->startElement(nodeName, false);
0091         writer->addTextNode(value());
0092         writer->endElement();
0093     }
0094 }
0095 
0096 typedef QMap<QString, KoInlineObject::Property> LoadMap;
0097 
0098 Q_GLOBAL_STATIC(LoadMap, s_loadInfo)
0099 
0100 bool InfoVariable::loadOdf(const KoXmlElement & element, KoShapeLoadingContext & /*context*/)
0101 {
0102     if (!s_loadInfo.exists() ) {
0103         for (unsigned int i = 0; i < numPropertyData; ++i) {
0104             s_loadInfo->insert(propertyData[i].tag, propertyData[i].property);
0105         }
0106     }
0107 
0108     const QString localName(element.localName());
0109     m_type = s_loadInfo->value(localName);
0110 
0111     for(KoXmlNode node = element.firstChild(); !node.isNull(); node = node.nextSibling() ) {
0112         if (node.isText()) {
0113             setValue(node.toText().data());
0114             break;
0115         }
0116     }
0117 
0118     return true;
0119 }