File indexing completed on 2025-01-05 04:45:51

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 "evolutioncalendar.h"
0008 #include "evolutionutil.h"
0009 
0010 #include "evolutionv3plugin_debug.h"
0011 
0012 #include <QDir>
0013 #include <QDomDocument>
0014 #include <QDomElement>
0015 #include <QFile>
0016 
0017 EvolutionCalendar::EvolutionCalendar() = default;
0018 
0019 EvolutionCalendar::~EvolutionCalendar() = default;
0020 
0021 void EvolutionCalendar::loadCalendar(const QString &filename)
0022 {
0023     // Read gconf file
0024     QFile file(filename);
0025     if (!file.open(QIODevice::ReadOnly)) {
0026         qCDebug(EVOLUTIONPLUGIN_LOG) << " We can't open file" << filename;
0027         return;
0028     }
0029     QDomDocument doc;
0030     if (!EvolutionUtil::loadInDomDocument(&file, doc)) {
0031         return;
0032     }
0033     QDomElement config = doc.documentElement();
0034 
0035     if (config.isNull()) {
0036         qCDebug(EVOLUTIONPLUGIN_LOG) << "No config found";
0037         return;
0038     }
0039     mCalendarPath = QDir::homePath() + QLatin1StringView("/.local/share/evolution/calendar/");
0040     for (QDomElement e = config.firstChildElement(); !e.isNull(); e = e.nextSiblingElement()) {
0041         const QString tag = e.tagName();
0042         if (tag == QLatin1StringView("entry")) {
0043             if (e.hasAttribute(QStringLiteral("name"))) {
0044                 const QString attr = e.attribute(QStringLiteral("name"));
0045                 if (attr == QLatin1StringView("sources")) {
0046                     readCalendar(e);
0047                 } else {
0048                     qCDebug(EVOLUTIONPLUGIN_LOG) << " attr unknown " << attr;
0049                 }
0050             }
0051         }
0052     }
0053 }
0054 
0055 void EvolutionCalendar::readCalendar(const QDomElement &calendar)
0056 {
0057     for (QDomElement calendarConfig = calendar.firstChildElement(); !calendarConfig.isNull(); calendarConfig = calendarConfig.nextSiblingElement()) {
0058         if (calendarConfig.tagName() == QLatin1StringView("li")) {
0059             const QDomElement stringValue = calendarConfig.firstChildElement();
0060             extractCalendarInfo(stringValue.text());
0061         }
0062     }
0063 }
0064 
0065 void EvolutionCalendar::extractCalendarInfo(const QString &info)
0066 {
0067     // qCDebug(IMPORTWIZARD_LOG)<<" info "<<info;
0068     // Read QDomElement
0069     QDomDocument cal;
0070     if (!EvolutionUtil::loadInDomDocument(info, cal)) {
0071         return;
0072     }
0073     QDomElement domElement = cal.documentElement();
0074 
0075     if (domElement.isNull()) {
0076         qCDebug(EVOLUTIONPLUGIN_LOG) << "Account not found";
0077         return;
0078     }
0079     QString base_uri;
0080     if (domElement.hasAttribute(QStringLiteral("base_uri"))) {
0081         base_uri = domElement.attribute(QStringLiteral("base_uri"));
0082     }
0083     if (base_uri == QLatin1StringView("local:")) {
0084         for (QDomElement e = domElement.firstChildElement(); !e.isNull(); e = e.nextSiblingElement()) {
0085             const QString tag = e.tagName();
0086             if (tag == QLatin1StringView("source")) {
0087                 QString name;
0088                 QMap<QString, QVariant> settings;
0089                 if (e.hasAttribute(QStringLiteral("uid"))) { }
0090                 if (e.hasAttribute(QStringLiteral("name"))) {
0091                     name = e.attribute(QStringLiteral("name"));
0092                     settings.insert(QStringLiteral("DisplayName"), name);
0093                 }
0094                 if (e.hasAttribute(QStringLiteral("relative_uri"))) {
0095                     const QString path = mCalendarPath + e.attribute(QStringLiteral("relative_uri")) + QLatin1StringView("/calendar.ics");
0096                     settings.insert(QStringLiteral("Path"), path);
0097                 }
0098                 if (e.hasAttribute(QStringLiteral("color_spec"))) {
0099                     // const QString color = e.attribute(QStringLiteral("color_spec"));
0100                     // Need id.
0101                     // TODO: Need to get id for collection to add color.
0102                 }
0103                 QDomElement propertiesElement = e.firstChildElement();
0104                 if (!propertiesElement.isNull()) {
0105                     for (QDomElement property = propertiesElement.firstChildElement(); !property.isNull(); property = property.nextSiblingElement()) {
0106                         const QString propertyTag = property.tagName();
0107                         if (propertyTag == QLatin1StringView("property")) {
0108                             if (property.hasAttribute(QStringLiteral("name"))) {
0109                                 const QString propertyName = property.attribute(QStringLiteral("name"));
0110                                 if (propertyName == QLatin1StringView("custom-file-readonly")) {
0111                                     if (property.hasAttribute(QStringLiteral("value"))) {
0112                                         if (property.attribute(QStringLiteral("value")) == QLatin1Char('1')) {
0113                                             settings.insert(QStringLiteral("ReadOnly"), true);
0114                                         }
0115                                     }
0116                                 } else if (propertyName == QLatin1StringView("alarm")) {
0117                                     qCDebug(EVOLUTIONPLUGIN_LOG) << " need to implement alarm property";
0118                                 } else {
0119                                     qCDebug(EVOLUTIONPLUGIN_LOG) << " property unknown :" << propertyName;
0120                                 }
0121                             }
0122                         } else {
0123                             qCDebug(EVOLUTIONPLUGIN_LOG) << " tag unknown :" << propertyTag;
0124                         }
0125                     }
0126                 }
0127                 LibImportWizard::AbstractBase::createResource(QStringLiteral("akonadi_ical_resource"), name, settings);
0128             } else {
0129                 qCDebug(EVOLUTIONPLUGIN_LOG) << " tag unknown :" << tag;
0130             }
0131         }
0132     } else if (base_uri == QLatin1StringView("webcal://")) {
0133         qCDebug(EVOLUTIONPLUGIN_LOG) << " need to implement webcal protocol";
0134     } else if (base_uri == QLatin1StringView("google://")) {
0135         qCDebug(EVOLUTIONPLUGIN_LOG) << " need to implement google protocol";
0136     } else if (base_uri == QLatin1StringView("caldav://")) {
0137         qCDebug(EVOLUTIONPLUGIN_LOG) << " need to implement caldav protocol";
0138     } else {
0139         qCDebug(EVOLUTIONPLUGIN_LOG) << " base_uri unknown" << base_uri;
0140     }
0141 }