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 "note.h" 0011 #include "libkolab-version.h" 0012 #include "pimkolab_debug.h" 0013 0014 using namespace KolabV2; 0015 0016 KCalendarCore::Journal::Ptr Note::xmlToJournal(const QString &xml) 0017 { 0018 Note note; 0019 note.load(xml); 0020 KCalendarCore::Journal::Ptr journal(new KCalendarCore::Journal()); 0021 note.saveTo(journal); 0022 return journal; 0023 } 0024 0025 QString Note::journalToXML(const KCalendarCore::Journal::Ptr &journal) 0026 { 0027 Note note(journal); 0028 return note.saveXML(); 0029 } 0030 0031 Note::Note(const KCalendarCore::Journal::Ptr &journal) 0032 { 0033 if (journal) { 0034 setFields(journal); 0035 } 0036 } 0037 0038 Note::~Note() = default; 0039 0040 void Note::setSummary(const QString &summary) 0041 { 0042 mSummary = summary; 0043 } 0044 0045 QString Note::summary() const 0046 { 0047 return mSummary; 0048 } 0049 0050 void Note::setBackgroundColor(const QColor &bgColor) 0051 { 0052 mBackgroundColor = bgColor; 0053 } 0054 0055 QColor Note::backgroundColor() const 0056 { 0057 return mBackgroundColor; 0058 } 0059 0060 void Note::setForegroundColor(const QColor &fgColor) 0061 { 0062 mForegroundColor = fgColor; 0063 } 0064 0065 QColor Note::foregroundColor() const 0066 { 0067 return mForegroundColor; 0068 } 0069 0070 void Note::setRichText(bool richText) 0071 { 0072 mRichText = richText; 0073 } 0074 0075 bool Note::richText() const 0076 { 0077 return mRichText; 0078 } 0079 0080 bool Note::loadAttribute(QDomElement &element) 0081 { 0082 const QString tagName = element.tagName(); 0083 if (tagName == QLatin1StringView("summary")) { 0084 setSummary(element.text()); 0085 } else if (tagName == QLatin1StringView("foreground-color")) { 0086 setForegroundColor(stringToColor(element.text())); 0087 } else if (tagName == QLatin1StringView("background-color")) { 0088 setBackgroundColor(stringToColor(element.text())); 0089 } else if (tagName == QLatin1StringView("knotes-richtext")) { 0090 mRichText = (element.text() == QLatin1StringView("true")); 0091 } else { 0092 return KolabBase::loadAttribute(element); 0093 } 0094 0095 // We handled this 0096 return true; 0097 } 0098 0099 bool Note::saveAttributes(QDomElement &element) const 0100 { 0101 // Save the base class elements 0102 KolabBase::saveAttributes(element); 0103 0104 // Save the elements 0105 #if 0 0106 QDomComment c = element.ownerDocument().createComment("Note specific attributes"); 0107 element.appendChild(c); 0108 #endif 0109 0110 writeString(element, QStringLiteral("summary"), summary()); 0111 if (foregroundColor().isValid()) { 0112 writeString(element, QStringLiteral("foreground-color"), colorToString(foregroundColor())); 0113 } 0114 if (backgroundColor().isValid()) { 0115 writeString(element, QStringLiteral("background-color"), colorToString(backgroundColor())); 0116 } 0117 writeString(element, QStringLiteral("knotes-richtext"), mRichText ? QStringLiteral("true") : QStringLiteral("false")); 0118 0119 return true; 0120 } 0121 0122 bool Note::loadXML(const QDomDocument &document) 0123 { 0124 QDomElement top = document.documentElement(); 0125 0126 if (top.tagName() != QLatin1StringView("note")) { 0127 qCWarning(PIMKOLAB_LOG) << QStringLiteral("XML error: Top tag was %1 instead of the expected note").arg(top.tagName()); 0128 return false; 0129 } 0130 0131 for (QDomNode n = top.firstChild(); !n.isNull(); n = n.nextSibling()) { 0132 if (n.isComment()) { 0133 continue; 0134 } 0135 if (n.isElement()) { 0136 QDomElement e = n.toElement(); 0137 if (!loadAttribute(e)) { 0138 // TODO: Unhandled tag - save for later storage 0139 qCDebug(PIMKOLAB_LOG) << "Warning: Unhandled tag" << e.tagName(); 0140 } 0141 } else { 0142 qCDebug(PIMKOLAB_LOG) << "Node is not a comment or an element???"; 0143 } 0144 } 0145 0146 return true; 0147 } 0148 0149 QString Note::saveXML() const 0150 { 0151 QDomDocument document = domTree(); 0152 QDomElement element = document.createElement(QStringLiteral("note")); 0153 element.setAttribute(QStringLiteral("version"), QStringLiteral("1.0")); 0154 saveAttributes(element); 0155 document.appendChild(element); 0156 return document.toString(); 0157 } 0158 0159 void Note::setFields(const KCalendarCore::Journal::Ptr &journal) 0160 { 0161 KolabBase::setFields(journal); 0162 0163 setSummary(journal->summary()); 0164 0165 QString property = journal->customProperty("KNotes", "BgColor"); 0166 if (!property.isEmpty()) { 0167 setBackgroundColor(property); 0168 } else { 0169 setBackgroundColor(QStringLiteral("yellow")); 0170 } 0171 0172 property = journal->customProperty("KNotes", "FgColor"); 0173 if (!property.isEmpty()) { 0174 setForegroundColor(property); 0175 } else { 0176 setForegroundColor(QStringLiteral("black")); 0177 } 0178 0179 property = journal->customProperty("KNotes", "RichText"); 0180 if (!property.isEmpty()) { 0181 setRichText(property == QLatin1StringView("true") ? true : false); 0182 } else { 0183 setRichText(false); 0184 } 0185 } 0186 0187 void Note::saveTo(const KCalendarCore::Journal::Ptr &journal) const 0188 { 0189 KolabBase::saveTo(journal); 0190 0191 // TODO: background and foreground 0192 journal->setSummary(summary()); 0193 if (foregroundColor().isValid()) { 0194 journal->setCustomProperty("KNotes", "FgColor", colorToString(foregroundColor())); 0195 } 0196 if (backgroundColor().isValid()) { 0197 journal->setCustomProperty("KNotes", "BgColor", colorToString(backgroundColor())); 0198 } 0199 journal->setCustomProperty("KNotes", "RichText", richText() ? QStringLiteral("true") : QStringLiteral("false")); 0200 } 0201 0202 QString Note::productID() const 0203 { 0204 return QStringLiteral("KNotes %1, Kolab resource").arg(QLatin1StringView(LIBKOLAB_LIB_VERSION_STRING)); 0205 }