File indexing completed on 2024-12-29 04:44:29
0001 /* 0002 SPDX-FileCopyrightText: 2012-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "sylpheedaddressbook.h" 0008 #include <KContacts/Addressee> 0009 #include <KContacts/ContactGroup> 0010 0011 #include "sylpheedplugin_debug.h" 0012 #include <KLocalizedString> 0013 0014 #include <QDomDocument> 0015 0016 SylpheedAddressBook::SylpheedAddressBook(const QDir &dir) 0017 : mDir(dir) 0018 { 0019 } 0020 0021 SylpheedAddressBook::~SylpheedAddressBook() = default; 0022 0023 void SylpheedAddressBook::importAddressBook() 0024 { 0025 const QStringList files = mDir.entryList(QStringList(QStringLiteral("addrbook-[0-9]*.xml")), QDir::Files, QDir::Name); 0026 if (files.isEmpty()) { 0027 addAddressBookImportInfo(i18n("No addressbook found")); 0028 } else { 0029 for (const QString &file : files) { 0030 readAddressBook(mDir.path() + QLatin1Char('/') + file); 0031 } 0032 } 0033 cleanUp(); 0034 } 0035 0036 void SylpheedAddressBook::readAddressBook(const QString &filename) 0037 { 0038 QFile file(filename); 0039 // qCDebug(SYLPHEEDPLUGIN_LOG)<<" import filename :"<<filename; 0040 if (!file.open(QIODevice::ReadOnly)) { 0041 qCDebug(SYLPHEEDPLUGIN_LOG) << " We can't open file" << filename; 0042 return; 0043 } 0044 QDomDocument doc; 0045 const QDomDocument::ParseResult parseResult = doc.setContent(&file); 0046 if (!parseResult) { 0047 qCDebug(SYLPHEEDPLUGIN_LOG) << "Unable to load document.Parse error in line " << parseResult.errorLine << ", col " << parseResult.errorColumn << ": " 0048 << qPrintable(parseResult.errorMessage); 0049 return; 0050 } 0051 QDomElement domElement = doc.documentElement(); 0052 0053 if (domElement.isNull()) { 0054 qCDebug(SYLPHEEDPLUGIN_LOG) << "addressbook not found"; 0055 return; 0056 } 0057 0058 QDomElement e = domElement.firstChildElement(); 0059 if (e.isNull()) { 0060 addAddressBookImportError(i18n("No contact found in %1", filename)); 0061 return; 0062 } 0063 0064 for (; !e.isNull(); e = e.nextSiblingElement()) { 0065 // TODO ?? const QString name = e.attribute(QStringLiteral("name")); 0066 0067 const QString tag = e.tagName(); 0068 if (tag == QLatin1StringView("person")) { 0069 KContacts::Addressee contact; 0070 // uid="333304265" first-name="dd" last-name="ccc" nick-name="" cn="laurent" 0071 QString uidPerson; 0072 if (e.hasAttribute(QStringLiteral("uid"))) { 0073 uidPerson = e.attribute(QStringLiteral("uid")); 0074 } 0075 if (e.hasAttribute(QStringLiteral("first-name"))) { 0076 contact.setName(e.attribute(QStringLiteral("first-name"))); 0077 } 0078 if (e.hasAttribute(QStringLiteral("last-name"))) { 0079 contact.setFamilyName(e.attribute(QStringLiteral("last-name"))); 0080 } 0081 if (e.hasAttribute(QStringLiteral("nick-name"))) { 0082 contact.setNickName(e.attribute(QStringLiteral("nick-name"))); 0083 } 0084 if (e.hasAttribute(QStringLiteral("cn"))) { 0085 contact.setFormattedName(e.attribute(QStringLiteral("cn"))); 0086 } 0087 QStringList uidAddress; 0088 for (QDomElement addressElement = e.firstChildElement(); !addressElement.isNull(); addressElement = addressElement.nextSiblingElement()) { 0089 const QString addressTag = addressElement.tagName(); 0090 if (addressTag == QLatin1StringView("address-list")) { 0091 QStringList emails; 0092 for (QDomElement addresslist = addressElement.firstChildElement(); !addresslist.isNull(); addresslist = addresslist.nextSiblingElement()) { 0093 const QString tagAddressList = addresslist.tagName(); 0094 if (tagAddressList == QLatin1StringView("address")) { 0095 if (addresslist.hasAttribute(QStringLiteral("email"))) { 0096 emails << addresslist.attribute(QStringLiteral("email")); 0097 } else if (addresslist.hasAttribute(QStringLiteral("alias"))) { 0098 // TODO: 0099 } else if (addresslist.hasAttribute(QStringLiteral("uid"))) { 0100 uidAddress << addresslist.attribute(QStringLiteral("uid")); 0101 } 0102 } else { 0103 qCDebug(SYLPHEEDPLUGIN_LOG) << " tagAddressList unknown :" << tagAddressList; 0104 } 0105 } 0106 if (!emails.isEmpty()) { 0107 contact.setEmails(emails); 0108 } 0109 } else if (addressTag == QLatin1StringView("attribute-list")) { 0110 for (QDomElement attributelist = addressElement.firstChildElement(); !attributelist.isNull(); 0111 attributelist = attributelist.nextSiblingElement()) { 0112 const QString tagAttributeList = attributelist.tagName(); 0113 if (tagAttributeList == QLatin1StringView("attribute")) { 0114 if (attributelist.hasAttribute(QStringLiteral("name"))) { 0115 const QString name = attributelist.attribute(QStringLiteral("name")); 0116 const QString value = attributelist.text(); 0117 contact.insertCustom(QStringLiteral("KADDRESSBOOK"), name, value); 0118 } 0119 } else { 0120 qCDebug(SYLPHEEDPLUGIN_LOG) << "tagAttributeList not implemented " << tagAttributeList; 0121 } 0122 } 0123 } else { 0124 qCDebug(SYLPHEEDPLUGIN_LOG) << " addressTag unknown :" << addressTag; 0125 } 0126 } 0127 if (!mAddressBookUid.contains(uidPerson)) { 0128 mAddressBookUid.insert(uidPerson, uidAddress); 0129 } else { 0130 qCDebug(SYLPHEEDPLUGIN_LOG) << " problem uidPerson already stored" << uidPerson; 0131 } 0132 addImportContactNote(contact, QStringLiteral("Sylpheed")); 0133 createContact(contact); 0134 } else if (tag == QLatin1StringView("group")) { 0135 QString name = e.attribute(QStringLiteral("name")); 0136 KContacts::ContactGroup group(name); 0137 // TODO: create Group 0138 for (QDomElement groupElement = e.firstChildElement(); !groupElement.isNull(); groupElement = groupElement.nextSiblingElement()) { 0139 const QString groupTag = groupElement.tagName(); 0140 if (groupTag == QLatin1StringView("member-list")) { 0141 for (QDomElement memberlist = groupElement.firstChildElement(); !memberlist.isNull(); memberlist = memberlist.nextSiblingElement()) { 0142 const QString tagMemberList = memberlist.tagName(); 0143 if (tagMemberList == QLatin1StringView("member")) { 0144 QString pid; 0145 QString eid; 0146 if (memberlist.hasAttribute(QStringLiteral("pid"))) { 0147 pid = memberlist.attribute(QStringLiteral("pid")); 0148 } 0149 if (memberlist.hasAttribute(QStringLiteral("eid"))) { 0150 eid = memberlist.attribute(QStringLiteral("eid")); 0151 } 0152 if (!pid.isEmpty() && !eid.isEmpty()) { 0153 // TODO 0154 } else { 0155 qCDebug(SYLPHEEDPLUGIN_LOG) << " Problem with group" << name; 0156 } 0157 // TODO 0158 } 0159 } 0160 } 0161 } 0162 createGroup(group); 0163 } else { 0164 qCDebug(SYLPHEEDPLUGIN_LOG) << " SylpheedAddressBook::readAddressBook tag unknown :" << tag; 0165 } 0166 } 0167 } 0168 0169 #include "moc_sylpheedaddressbook.cpp"