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

0001 /*
0002     This file is part of Akonadi KolabProxy.
0003     SPDX-FileCopyrightText: 2009 Kevin Krammer <kevin.krammer@gmx.at>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "distributionlist.h"
0009 #include "pimkolab_debug.h"
0010 
0011 #include <KContacts/ContactGroup>
0012 
0013 using namespace KolabV2;
0014 
0015 namespace
0016 {
0017 inline QString unhandledTagAppName()
0018 {
0019     return QStringLiteral("KOLABUNHANDLED");
0020 } // no hyphens in appnames!
0021 }
0022 // saving (contactgroup->xml)
0023 DistributionList::DistributionList(const KContacts::ContactGroup *contactGroup)
0024 {
0025     setFields(contactGroup);
0026 }
0027 
0028 // loading (xml->contactgroup)
0029 DistributionList::DistributionList(const QString &xml)
0030 {
0031     load(xml);
0032 }
0033 
0034 DistributionList::~DistributionList() = default;
0035 
0036 void DistributionList::setName(const QString &name)
0037 {
0038     mName = name;
0039 }
0040 
0041 QString DistributionList::name() const
0042 {
0043     return mName;
0044 }
0045 
0046 void KolabV2::DistributionList::loadDistrListMember(const QDomElement &element)
0047 {
0048     Member member;
0049     for (QDomNode n = element.firstChild(); !n.isNull(); n = n.nextSibling()) {
0050         if (n.isComment()) {
0051             continue;
0052         }
0053         if (n.isElement()) {
0054             QDomElement e = n.toElement();
0055             const QString tagName = e.tagName();
0056             if (tagName == QLatin1StringView("display-name")) {
0057                 member.displayName = e.text();
0058             } else if (tagName == QLatin1StringView("smtp-address")) {
0059                 member.email = e.text();
0060             } else if (tagName == QLatin1StringView("uid")) {
0061                 member.uid = e.text();
0062             }
0063         }
0064     }
0065     mDistrListMembers.append(member);
0066 }
0067 
0068 void DistributionList::saveDistrListMembers(QDomElement &element) const
0069 {
0070     QList<Member>::ConstIterator it = mDistrListMembers.constBegin();
0071     const QList<Member>::ConstIterator end = mDistrListMembers.constEnd();
0072     for (; it != end; ++it) {
0073         QDomElement e = element.ownerDocument().createElement(QStringLiteral("member"));
0074         element.appendChild(e);
0075         const Member &m = *it;
0076         if (!m.uid.isEmpty()) {
0077             writeString(e, QStringLiteral("uid"), m.uid);
0078         } else {
0079             writeString(e, QStringLiteral("display-name"), m.displayName);
0080             writeString(e, QStringLiteral("smtp-address"), m.email);
0081         }
0082     }
0083 }
0084 
0085 bool DistributionList::loadAttribute(QDomElement &element)
0086 {
0087     const QString tagName = element.tagName();
0088     switch (tagName[0].toLatin1()) {
0089     case 'd':
0090         if (tagName == QLatin1StringView("display-name")) {
0091             setName(element.text());
0092             return true;
0093         }
0094         break;
0095     case 'm':
0096         if (tagName == QLatin1StringView("member")) {
0097             loadDistrListMember(element);
0098             return true;
0099         }
0100         break;
0101     default:
0102         break;
0103     }
0104     return KolabBase::loadAttribute(element);
0105 }
0106 
0107 bool DistributionList::saveAttributes(QDomElement &element) const
0108 {
0109     // Save the base class elements
0110     KolabBase::saveAttributes(element);
0111     writeString(element, QStringLiteral("display-name"), name());
0112     saveDistrListMembers(element);
0113 
0114     return true;
0115 }
0116 
0117 bool DistributionList::loadXML(const QDomDocument &document)
0118 {
0119     QDomElement top = document.documentElement();
0120 
0121     if (top.tagName() != QLatin1StringView("distribution-list")) {
0122         qCWarning(PIMKOLAB_LOG) << QStringLiteral("XML error: Top tag was %1 instead of the expected distribution-list").arg(top.tagName());
0123         return false;
0124     }
0125 
0126     for (QDomNode n = top.firstChild(); !n.isNull(); n = n.nextSibling()) {
0127         if (n.isComment()) {
0128             continue;
0129         }
0130         if (n.isElement()) {
0131             QDomElement e = n.toElement();
0132             if (!loadAttribute(e)) {
0133                 // Unhandled tag - save for later storage
0134                 // qCDebug(PIMKOLAB_LOG) <<"Saving unhandled tag" << e.tagName();
0135                 Custom c;
0136                 c.app = unhandledTagAppName();
0137                 c.name = e.tagName();
0138                 c.value = e.text();
0139                 mCustomList.append(c);
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 DistributionList::saveXML() const
0150 {
0151     QDomDocument document = domTree();
0152     QDomElement element = document.createElement(QStringLiteral("distribution-list"));
0153     element.setAttribute(QStringLiteral("version"), QStringLiteral("1.0"));
0154     saveAttributes(element);
0155     document.appendChild(element);
0156     return document.toString();
0157 }
0158 
0159 QString DistributionList::productID() const
0160 {
0161     // TODO should we get name/version from desktop file?
0162     return QStringLiteral("Akonadi Kolab Proxy");
0163 }
0164 
0165 // The saving is contactgroup -> DistributionList -> xml, this is the first part
0166 void DistributionList::setFields(const KContacts::ContactGroup *contactGroup)
0167 {
0168     KolabBase::setFields(contactGroup);
0169 
0170     setName(contactGroup->name());
0171 
0172     // explicit contact data
0173     for (int index = 0; index < contactGroup->dataCount(); ++index) {
0174         const KContacts::ContactGroup::Data &data = contactGroup->data(index);
0175 
0176         Member m;
0177         m.displayName = data.name();
0178         m.email = data.email();
0179 
0180         mDistrListMembers.append(m);
0181     }
0182     for (int index = 0; index < contactGroup->contactReferenceCount(); ++index) {
0183         const KContacts::ContactGroup::ContactReference &data = contactGroup->contactReference(index);
0184 
0185         Member m;
0186         m.uid = data.uid();
0187 
0188         mDistrListMembers.append(m);
0189     }
0190     if (contactGroup->contactGroupReferenceCount() > 0) {
0191         qCWarning(PIMKOLAB_LOG) << "Tried to save contact group references, which should have been resolved already";
0192     }
0193 }
0194 
0195 // The loading is: xml -> DistributionList -> contactgroup, this is the second part
0196 void DistributionList::saveTo(KContacts::ContactGroup *contactGroup)
0197 {
0198     KolabBase::saveTo(contactGroup);
0199 
0200     contactGroup->setName(name());
0201 
0202     QList<Member>::ConstIterator mit = mDistrListMembers.constBegin();
0203     const QList<Member>::ConstIterator mEnd = mDistrListMembers.constEnd();
0204     for (; mit != mEnd; ++mit) {
0205         if (!(*mit).uid.isEmpty()) {
0206             contactGroup->append(KContacts::ContactGroup::ContactReference((*mit).uid));
0207         } else {
0208             contactGroup->append(KContacts::ContactGroup::Data((*mit).displayName, (*mit).email));
0209         }
0210     }
0211 }
0212 
0213 QString DistributionList::type() const
0214 {
0215     return QStringLiteral("DistributionList");
0216 }
0217 
0218 // kate: space-indent on; indent-width 2; replace-tabs on;