Fixes DateVariable saving to ODF (no "0-00-00T" prefix, use date/time-only or datetime)

REVIEW: 105463

thanks thorsten and pierre for review
parent 1791fc78
......@@ -2,6 +2,7 @@
Copyright (C) 2009 Thorsten Zachmann <zachmann@kde.org>
Copyright (C) 2009 Johannes Simon <johannes.simon@gmail.com>
Copyright (C) 2010,2011 Jan Hambrecht <jaham@gmx.net>
Copyright 2012 Friedrich W. H. Kossebau <kossebau@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
......@@ -276,3 +277,10 @@ void KoOdfWorkaround::fixBadFormulaHiddenForStyleCellProtect(QString& value)
value[length-7] = QLatin1Char('-');
}
}
void KoOdfWorkaround::fixBadDateForTextTime(QString &value)
{
if (value.startsWith(QLatin1String("0-00-00T"))) {
value.remove(0, 8);
}
}
......@@ -120,6 +120,12 @@ namespace KoOdfWorkaround
* This method fixes the bad strings to the correct ones.
*/
FLAKE_EXPORT void fixBadFormulaHiddenForStyleCellProtect(QString &value);
/**
* Calligra used to store text:time-value with a "0-00-00T" prefix
* This method removes that prefix.
*/
FLAKE_EXPORT void fixBadDateForTextTime(QString &value);
}
#endif /* KOODFWORKAROUND_H */
/* This file is part of the KDE project
* Copyright (C) 2006-2007 Thomas Zander <zander@kde.org>
* Copyright (C) 2008,2010 Thorsten Zachmann <zachmann@kde.org>
* Copyright 2012 Friedrich W. H. Kossebau <kossebau@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
......@@ -29,17 +30,19 @@
#include <KoShapeSavingContext.h>
#include <KoOdfLoadingContext.h>
#include <KoOdfStylesReader.h>
#include <KoOdfWorkaround.h>
DateVariable::DateVariable(DateType type)
: KoVariable()
, m_type(type)
, m_displayType(Date)
, m_valueType(DateTime)
, m_daysOffset(0)
, m_monthsOffset(0)
, m_yearsOffset(0)
, m_secsOffset(0)
{
m_time = QDateTime::currentDateTime();
m_datetime = QDateTime::currentDateTime();
}
DateVariable::~DateVariable()
......@@ -63,11 +66,17 @@ void DateVariable::saveOdf(KoShapeSavingContext & context)
if (m_type == Fixed) {
writer->addAttribute("text:fixed", "true");
// only write as much information as we read: just date/time or datetime
if (m_displayType == Time) {
// if only the time is set QDateTime::toString returns an empty so add the data by hand
writer->addAttribute("text:time-value", "0-00-00T" + m_time.time().toString(Qt::ISODate));
const QString timeValue = (m_valueType == DateTime) ?
m_datetime.toString(Qt::ISODate) :
m_datetime.time().toString(Qt::ISODate);
writer->addAttribute("text:time-value", timeValue);
} else {
writer->addAttribute("text:date-value", m_time.toString(Qt::ISODate));
const QString dateValue = (m_valueType == DateTime) ?
m_datetime.toString(Qt::ISODate) :
m_datetime.date().toString(Qt::ISODate);
writer->addAttribute("text:date-value", dateValue);
}
} else {
writer->addAttribute("text:fixed", "false");
......@@ -95,13 +104,34 @@ bool DateVariable::loadOdf(const KoXmlElement & element, KoShapeLoadingContext &
m_type = AutoUpdate;
}
if (localName == "time") {
m_displayType = Time;
} else {
m_displayType = Date;
}
//dateProperties.setProperty("time", element.attributeNS(KoXmlNS::text, localName + "-value"));
QString value(element.attributeNS(KoXmlNS::text, localName + "-value", ""));
if (!value.isEmpty()) {
if (value.startsWith("0-00-00T")) {
value.prepend("000");
#ifndef NWORKAROUND_ODF_BUGS
KoOdfWorkaround::fixBadDateForTextTime(value);
#endif
// hopefully this simple detection works in all cases
const bool isDateTime = (value.indexOf(QLatin1Char('T')) != -1);
if (isDateTime) {
m_datetime = QDateTime::fromString(value, Qt::ISODate);
m_valueType = DateTime;
} else {
if (m_displayType == Time) {
const QTime time = QTime::fromString(value, Qt::ISODate);
m_datetime = QDateTime(QDate::currentDate(), time);
} else {
const QDate date = QDate::fromString(value, Qt::ISODate);
m_datetime = QDateTime(date);
}
m_valueType = DateOrTime;
}
m_time = QDateTime::fromString(value, Qt::ISODate);
} else {
// if value is not set current time is assumed ODF 19.881 text:time-value
m_type = AutoUpdate;
......@@ -110,12 +140,6 @@ bool DateVariable::loadOdf(const KoXmlElement & element, KoShapeLoadingContext &
//dateProperties.setProperty("definition", dateFormat);
m_definition = dateFormat;
if (localName == "time") {
m_displayType = Time;
} else {
m_displayType = Date;
}
//dateProperties.setProperty("adjust", element.attributeNS(KoXmlNS::text, localName + "-adjust"));
const QString adjust(element.attributeNS(KoXmlNS::text, localName + "-adjust", ""));
adjustTime(adjust);
......@@ -127,7 +151,7 @@ void DateVariable::readProperties(const KoProperties *props)
{
m_definition = props->stringProperty("definition");
if (!props->stringProperty("time").isEmpty())
m_time = QDateTime::fromString(props->stringProperty("time"), Qt::ISODate);
m_datetime = QDateTime::fromString(props->stringProperty("time"), Qt::ISODate);
if (props->intProperty("id") == Fixed)
m_type = Fixed;
else
......@@ -139,6 +163,7 @@ void DateVariable::readProperties(const KoProperties *props)
else {
m_displayType = Date;
}
m_valueType = DateTime;
adjustTime(props->stringProperty("adjust"));
update();
}
......@@ -188,7 +213,7 @@ void DateVariable::update()
QDateTime target;
switch (m_type) {
case Fixed:
target = m_time;
target = m_datetime;
break;
case AutoUpdate:
target = QDateTime::currentDateTime();
......
......@@ -44,6 +44,11 @@ public:
Time
};
enum ValueType {
DateOrTime,
DateTime
};
/**
* Constructor.
*/
......@@ -60,7 +65,7 @@ public:
QWidget *createOptionsWidget();
QString definition() const {
const QString & definition() const {
return m_definition;
}
void setDefinition(const QString &definition);
......@@ -91,8 +96,9 @@ private:
DateType m_type;
DisplayType m_displayType;
ValueType m_valueType;
QString m_definition;
QDateTime m_time;
QDateTime m_datetime;
int m_daysOffset;
int m_monthsOffset;
int m_yearsOffset;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment