File indexing completed on 2024-12-22 05:07:42

0001 /*
0002     SPDX-FileCopyrightText: 2014 Sandro Knauß <knauss@kolabsys.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 // This code was taken from kmail-account-wizard
0008 
0009 #include "setupautoconfigkolabfreebusy.h"
0010 #include "accounts/ispdb/autoconfigkolabfreebusy.h"
0011 
0012 #include "configfile.h"
0013 
0014 #include <QFileInfo>
0015 
0016 #include <KLocalizedString>
0017 
0018 SetupAutoconfigKolabFreebusy::SetupAutoconfigKolabFreebusy(QObject *parent)
0019     : SetupObject(parent)
0020     , mIspdb(new AutoconfigKolabFreebusy(this))
0021 {
0022     connect(mIspdb, &AutoconfigKolabFreebusy::finished, this, &SetupAutoconfigKolabFreebusy::onIspdbFinished);
0023 }
0024 
0025 SetupAutoconfigKolabFreebusy::~SetupAutoconfigKolabFreebusy()
0026 {
0027     delete mIspdb;
0028 }
0029 
0030 int SetupAutoconfigKolabFreebusy::countFreebusyServers() const
0031 {
0032     return mIspdb->freebusyServers().count();
0033 }
0034 
0035 void SetupAutoconfigKolabFreebusy::fillFreebusyServer(int i, QObject *o) const
0036 {
0037     const freebusy isp = mIspdb->freebusyServers().values().at(i);
0038     auto *korganizer = qobject_cast<ConfigFile *>(o);
0039     QFileInfo path(isp.path);
0040     QString url(QStringLiteral("https://"));
0041 
0042     if (isp.socketType == Ispdb::None) {
0043         url = QStringLiteral("http://");
0044     }
0045 
0046     url += isp.hostname;
0047 
0048     if (isp.port != 80) {
0049         url += QLatin1Char(':');
0050         url += QString::number(isp.port);
0051     }
0052 
0053     if (!isp.path.startsWith(QLatin1Char('/'))) {
0054         url += QLatin1Char('/');
0055     }
0056 
0057     url += path.path();
0058 
0059     bool fullDomainRetrieval = (path.baseName() == QLatin1String("$EMAIL$"));
0060 
0061     const QString group(QStringLiteral("FreeBusy Retrieve"));
0062 
0063     korganizer->setConfig(group, QStringLiteral("FreeBusyFullDomainRetrieval"), fullDomainRetrieval ? QStringLiteral("true") : QStringLiteral("false"));
0064     korganizer->setConfig(group, QStringLiteral("FreeBusyRetrieveAuto"), QStringLiteral("true"));
0065     korganizer->setConfig(group, QStringLiteral("FreeBusyRetrieveUrl"), url);
0066     korganizer->setConfig(group, QStringLiteral("FreeBusyRetrieverUser"), isp.username);
0067     korganizer->setConfig(group, QStringLiteral("FreeBusyRetrieverPassword"), isp.password);
0068     if (!isp.password.isEmpty()) {
0069         korganizer->setConfig(group, QStringLiteral("FreeBusyRetrieveSavePassword"), QStringLiteral("true"));
0070     }
0071 }
0072 
0073 void SetupAutoconfigKolabFreebusy::start()
0074 {
0075     mIspdb->start();
0076     Q_EMIT info(i18n("Searching for autoconfiguration..."));
0077 }
0078 
0079 void SetupAutoconfigKolabFreebusy::setEmail(const QString &email)
0080 {
0081     mIspdb->setEmail(email);
0082 }
0083 
0084 void SetupAutoconfigKolabFreebusy::setPassword(const QString &password)
0085 {
0086     mIspdb->setPassword(password);
0087 }
0088 
0089 void SetupAutoconfigKolabFreebusy::create()
0090 {
0091 }
0092 
0093 void SetupAutoconfigKolabFreebusy::destroy()
0094 {
0095 }
0096 
0097 void SetupAutoconfigKolabFreebusy::onIspdbFinished(bool status)
0098 {
0099     Q_EMIT ispdbFinished(status);
0100     if (status) {
0101         Q_EMIT info(i18n("Autoconfiguration found."));
0102     } else {
0103         Q_EMIT info(i18n("Autoconfiguration failed."));
0104     }
0105 }