File indexing completed on 2024-12-01 04:47:51

0001 /*
0002     This file is part of the kolab resource - the implementation of the
0003     Kolab storage format. See www.kolab.org for documentation on this.
0004 
0005     SPDX-FileCopyrightText: 2004 Bo Thorsen <bo@sonofthor.dk>
0006 
0007     SPDX-License-Identifier: LGPL-2.0-or-later
0008 */
0009 
0010 #include "journal.h"
0011 #include "libkolab-version.h"
0012 #include "pimkolab_debug.h"
0013 
0014 using namespace KolabV2;
0015 
0016 KCalendarCore::Journal::Ptr Journal::fromXml(const QDomDocument &xmlDoc, const QString &tz)
0017 {
0018     Journal journal(tz);
0019     journal.loadXML(xmlDoc);
0020     KCalendarCore::Journal::Ptr kcalJournal(new KCalendarCore::Journal());
0021     journal.saveTo(kcalJournal);
0022     return kcalJournal;
0023 }
0024 
0025 QString Journal::journalToXML(const KCalendarCore::Journal::Ptr &kcalJournal, const QString &tz)
0026 {
0027     Journal journal(tz, kcalJournal);
0028     return journal.saveXML();
0029 }
0030 
0031 Journal::Journal(const QString &tz, const KCalendarCore::Journal::Ptr &journal)
0032     : KolabBase(tz)
0033 {
0034     if (journal) {
0035         setFields(journal);
0036     }
0037 }
0038 
0039 Journal::~Journal() = default;
0040 
0041 void Journal::setSummary(const QString &summary)
0042 {
0043     mSummary = summary;
0044 }
0045 
0046 QString Journal::summary() const
0047 {
0048     return mSummary;
0049 }
0050 
0051 void Journal::setStartDate(const QDateTime &startDate)
0052 {
0053     mStartDate = startDate;
0054 }
0055 
0056 QDateTime Journal::startDate() const
0057 {
0058     return mStartDate;
0059 }
0060 
0061 void Journal::setEndDate(const QDateTime &endDate)
0062 {
0063     mEndDate = endDate;
0064 }
0065 
0066 QDateTime Journal::endDate() const
0067 {
0068     return mEndDate;
0069 }
0070 
0071 bool Journal::loadAttribute(QDomElement &element)
0072 {
0073     const QString tagName = element.tagName();
0074 
0075     if (tagName == QLatin1StringView("summary")) {
0076         setSummary(element.text());
0077     } else if (tagName == QLatin1StringView("start-date")) {
0078         const auto t = element.text();
0079         setStartDate(stringToDateTime(t));
0080         mDateOnly = t.size() <= 10;
0081     } else {
0082         // Not handled here
0083         return KolabBase::loadAttribute(element);
0084     }
0085 
0086     // We handled this
0087     return true;
0088 }
0089 
0090 bool Journal::saveAttributes(QDomElement &element) const
0091 {
0092     // Save the base class elements
0093     KolabBase::saveAttributes(element);
0094 
0095     writeString(element, QStringLiteral("summary"), summary());
0096     if (mDateOnly) {
0097         writeString(element, QStringLiteral("start-date"), dateToString(startDate().date()));
0098     } else {
0099         writeString(element, QStringLiteral("start-date"), dateTimeToString(startDate()));
0100     }
0101 
0102     return true;
0103 }
0104 
0105 bool Journal::loadXML(const QDomDocument &document)
0106 {
0107     QDomElement top = document.documentElement();
0108 
0109     if (top.tagName() != QLatin1StringView("journal")) {
0110         qCWarning(PIMKOLAB_LOG) << QStringLiteral("XML error: Top tag was %1 instead of the expected Journal").arg(top.tagName());
0111         return false;
0112     }
0113 
0114     for (QDomNode n = top.firstChild(); !n.isNull(); n = n.nextSibling()) {
0115         if (n.isComment()) {
0116             continue;
0117         }
0118         if (n.isElement()) {
0119             QDomElement e = n.toElement();
0120             if (!loadAttribute(e)) {
0121                 // Unhandled tag - save for later storage
0122                 // qDebug( "Unhandled tag: %s", e.toCString().data() );
0123             }
0124         } else {
0125             qCDebug(PIMKOLAB_LOG) << "Node is not a comment or an element???";
0126         }
0127     }
0128 
0129     return true;
0130 }
0131 
0132 QString Journal::saveXML() const
0133 {
0134     QDomDocument document = domTree();
0135     QDomElement element = document.createElement(QStringLiteral("journal"));
0136     element.setAttribute(QStringLiteral("version"), QStringLiteral("1.0"));
0137     saveAttributes(element);
0138     document.appendChild(element);
0139     return document.toString();
0140 }
0141 
0142 void Journal::saveTo(const KCalendarCore::Journal::Ptr &journal) const
0143 {
0144     KolabBase::saveTo(journal);
0145 
0146     journal->setSummary(summary());
0147     journal->setDtStart(utcToLocal(startDate()));
0148     journal->setAllDay(mDateOnly);
0149 }
0150 
0151 void Journal::setFields(const KCalendarCore::Journal::Ptr &journal)
0152 {
0153     // Set baseclass fields
0154     KolabBase::setFields(journal);
0155 
0156     // Set our own fields
0157     setSummary(journal->summary());
0158     setStartDate(localToUTC(journal->dtStart()));
0159 }
0160 
0161 QString Journal::productID() const
0162 {
0163     return QLatin1StringView(LIBKOLAB_LIB_VERSION_STRING) + QLatin1StringView(", Kolab resource");
0164 }