File indexing completed on 2025-03-23 11:10:20
0001 // SPDX-FileCopyrightText: 2009 Volker Krause <vkrause@kde.org> 0002 // SPDX-FileCopyrightText: 2010 Tom Albers <toma@kde.org> 0003 // SPDX-FileCopyrightText: 2012-2023 Laurent Montel <montel@kde.org> 0004 // SPDX-FileCopyrightText: 2022 Devin Lin <devin@kde.org> 0005 // SPDX-License-Identifier: LGPL-2.0-or-later 0006 0007 #include "newaccount.h" 0008 0009 #include "accounts/setup/resource.h" 0010 #include "accounts/setup/transport.h" 0011 0012 NewAccount::NewAccount(QObject* parent) 0013 : QObject(parent) 0014 , m_ispdb{nullptr} 0015 , m_setupManager{new SetupManager{this}} 0016 , m_receivingMailProtocol{ReceivingMailProtocol::Imap} 0017 , m_imapPort{993} 0018 , m_imapAuthenticationType{AuthenticationType::Plain} 0019 , m_pop3Port{995} 0020 , m_pop3AuthenticationType{AuthenticationType::Plain} 0021 , m_smtpPort{587} 0022 , m_smtpAuthenticationType{AuthenticationType::Plain} 0023 { 0024 connect(m_setupManager, &SetupManager::setupSucceeded, this, [this](const QString &msg){ Q_EMIT setupSucceeded(msg); }); 0025 connect(m_setupManager, &SetupManager::setupFailed, this, [this](const QString &msg){ Q_EMIT setupFailed(msg); }); 0026 connect(m_setupManager, &SetupManager::setupInfo, this, [this](const QString &msg){ Q_EMIT setupInfo(msg); }); 0027 } 0028 0029 NewAccount::~NewAccount() noexcept 0030 { 0031 } 0032 0033 QString &NewAccount::email() 0034 { 0035 return m_email; 0036 } 0037 0038 void NewAccount::setEmail(const QString& email) 0039 { 0040 if (m_email != email) { 0041 m_email = email; 0042 Q_EMIT emailChanged(); 0043 } 0044 } 0045 0046 QString &NewAccount::name() 0047 { 0048 return m_name; 0049 } 0050 0051 void NewAccount::setName(const QString& name) 0052 { 0053 if (m_name != name) { 0054 m_name = name; 0055 Q_EMIT nameChanged(); 0056 } 0057 } 0058 0059 QString &NewAccount::password() 0060 { 0061 return m_password; 0062 } 0063 0064 void NewAccount::setPassword(const QString &password) 0065 { 0066 if (m_password != password) { 0067 m_password = password; 0068 Q_EMIT passwordChanged(); 0069 } 0070 } 0071 0072 bool NewAccount::ispdbIsSearching() 0073 { 0074 return m_ispdbIsSearching; 0075 } 0076 0077 NewAccount::ReceivingMailProtocol NewAccount::receivingMailProtocol() 0078 { 0079 return m_receivingMailProtocol; 0080 } 0081 0082 void NewAccount::setReceivingMailProtocol(NewAccount::ReceivingMailProtocol receivingMailProtocol) 0083 { 0084 if (m_receivingMailProtocol != receivingMailProtocol) { 0085 m_receivingMailProtocol = receivingMailProtocol; 0086 Q_EMIT receivingMailProtocolChanged(); 0087 } 0088 } 0089 0090 QString &NewAccount::imapHost() 0091 { 0092 return m_imapHost; 0093 } 0094 0095 void NewAccount::setImapHost(QString host) 0096 { 0097 if (m_imapHost != host) { 0098 m_imapHost = host; 0099 Q_EMIT imapHostChanged(); 0100 } 0101 } 0102 0103 int NewAccount::imapPort() 0104 { 0105 return m_imapPort; 0106 } 0107 0108 void NewAccount::setImapPort(int port) 0109 { 0110 if (m_imapPort != port) { 0111 m_imapPort = port; 0112 Q_EMIT imapPortChanged(); 0113 } 0114 } 0115 0116 QString &NewAccount::imapUsername() 0117 { 0118 return m_imapUsername; 0119 } 0120 0121 void NewAccount::setImapUsername(QString username) 0122 { 0123 if (m_imapUsername != username) { 0124 m_imapUsername = username; 0125 Q_EMIT imapUsernameChanged(); 0126 } 0127 } 0128 0129 QString &NewAccount::imapPassword() 0130 { 0131 return m_imapPassword; 0132 } 0133 0134 void NewAccount::setImapPassword(QString password) 0135 { 0136 if (m_imapPassword != password) { 0137 m_imapPassword = password; 0138 Q_EMIT imapPasswordChanged(); 0139 } 0140 } 0141 0142 NewAccount::AuthenticationType NewAccount::imapAuthenticationType() 0143 { 0144 return m_imapAuthenticationType; 0145 } 0146 0147 void NewAccount::setImapAuthenticationType(AuthenticationType authenticationType) 0148 { 0149 if (m_imapAuthenticationType != authenticationType) { 0150 m_imapAuthenticationType = authenticationType; 0151 Q_EMIT imapAuthenticationTypeChanged(); 0152 } 0153 } 0154 0155 NewAccount::SocketType NewAccount::imapSocketType() 0156 { 0157 return m_imapSocketType; 0158 } 0159 0160 void NewAccount::setImapSocketType(SocketType socketType) 0161 { 0162 if (m_imapSocketType != socketType) { 0163 m_imapSocketType = socketType; 0164 Q_EMIT imapSocketTypeChanged(); 0165 } 0166 } 0167 0168 QString &NewAccount::pop3Host() 0169 { 0170 return m_pop3Host; 0171 } 0172 0173 void NewAccount::setPop3Host(QString host) 0174 { 0175 if (m_pop3Host != host) { 0176 m_pop3Host = host; 0177 Q_EMIT pop3HostChanged(); 0178 } 0179 } 0180 0181 int NewAccount::pop3Port() 0182 { 0183 return m_pop3Port; 0184 } 0185 0186 void NewAccount::setPop3Port(int port) 0187 { 0188 if (m_pop3Port != port) { 0189 m_pop3Port = port; 0190 Q_EMIT pop3PortChanged(); 0191 } 0192 } 0193 0194 QString &NewAccount::pop3Username() 0195 { 0196 return m_pop3Username; 0197 } 0198 0199 void NewAccount::setPop3Username(QString username) 0200 { 0201 if (m_pop3Username != username) { 0202 m_pop3Username = username; 0203 Q_EMIT pop3UsernameChanged(); 0204 } 0205 } 0206 0207 QString &NewAccount::pop3Password() 0208 { 0209 return m_pop3Password; 0210 } 0211 0212 void NewAccount::setPop3Password(QString password) 0213 { 0214 if (m_pop3Password != password) { 0215 m_pop3Password = password; 0216 Q_EMIT pop3PasswordChanged(); 0217 } 0218 } 0219 0220 NewAccount::AuthenticationType NewAccount::pop3AuthenticationType() 0221 { 0222 return m_pop3AuthenticationType; 0223 } 0224 0225 void NewAccount::setPop3AuthenticationType(AuthenticationType authenticationType) 0226 { 0227 if (m_pop3AuthenticationType != authenticationType) { 0228 m_pop3AuthenticationType = authenticationType; 0229 Q_EMIT pop3AuthenticationTypeChanged(); 0230 } 0231 } 0232 0233 NewAccount::SocketType NewAccount::pop3SocketType() 0234 { 0235 return m_pop3SocketType; 0236 } 0237 0238 void NewAccount::setPop3SocketType(SocketType socketType) 0239 { 0240 if (m_pop3SocketType != socketType) { 0241 m_pop3SocketType = socketType; 0242 Q_EMIT pop3SocketTypeChanged(); 0243 } 0244 } 0245 0246 QString &NewAccount::smtpHost() 0247 { 0248 return m_smtpHost; 0249 } 0250 0251 void NewAccount::setSmtpHost(QString host) 0252 { 0253 if (m_smtpHost != host) { 0254 m_smtpHost = host; 0255 Q_EMIT smtpHostChanged(); 0256 } 0257 } 0258 0259 int NewAccount::smtpPort() 0260 { 0261 return m_smtpPort; 0262 } 0263 0264 void NewAccount::setSmtpPort(int port) 0265 { 0266 if (m_smtpPort != port) { 0267 m_smtpPort = port; 0268 Q_EMIT smtpPortChanged(); 0269 } 0270 } 0271 0272 QString &NewAccount::smtpUsername() 0273 { 0274 return m_smtpUsername; 0275 } 0276 0277 void NewAccount::setSmtpUsername(QString username) 0278 { 0279 if (m_smtpUsername != username) { 0280 m_smtpUsername = username; 0281 Q_EMIT smtpUsernameChanged(); 0282 } 0283 } 0284 0285 QString &NewAccount::smtpPassword() 0286 { 0287 return m_smtpPassword; 0288 } 0289 0290 void NewAccount::setSmtpPassword(QString password) 0291 { 0292 if (m_smtpPassword != password) { 0293 m_smtpPassword = password; 0294 Q_EMIT smtpPasswordChanged(); 0295 } 0296 } 0297 0298 NewAccount::AuthenticationType NewAccount::smtpAuthenticationType() 0299 { 0300 return m_smtpAuthenticationType; 0301 } 0302 0303 void NewAccount::setSmtpAuthenticationType(AuthenticationType authenticationType) 0304 { 0305 if (m_smtpAuthenticationType != authenticationType) { 0306 m_smtpAuthenticationType = authenticationType; 0307 Q_EMIT smtpAuthenticationTypeChanged(); 0308 } 0309 } 0310 0311 NewAccount::SocketType NewAccount::smtpSocketType() 0312 { 0313 return m_smtpSocketType; 0314 } 0315 0316 void NewAccount::setSmtpSocketType(SocketType socketType) 0317 { 0318 if (m_smtpSocketType != socketType) { 0319 m_smtpSocketType = socketType; 0320 Q_EMIT smtpSocketTypeChanged(); 0321 } 0322 } 0323 0324 void NewAccount::searchIspdbForConfig() 0325 { 0326 delete m_ispdb; 0327 m_ispdb = new Ispdb(this); 0328 0329 m_ispdb->setEmail(m_email); 0330 m_ispdb->start(); 0331 0332 connect(m_ispdb, &Ispdb::finished, this, &NewAccount::ispdbFinishedSearchingSlot); 0333 0334 m_ispdbIsSearching = true; 0335 Q_EMIT ispdbIsSearchingChanged(); 0336 } 0337 0338 void NewAccount::addAccount() 0339 { 0340 if (m_receivingMailProtocol == ReceivingMailProtocol::Imap) { 0341 configureImap(m_imapHost, m_imapPort, m_imapUsername, m_imapPassword, m_imapAuthenticationType, m_imapSocketType); 0342 } else if (m_receivingMailProtocol == ReceivingMailProtocol::Pop3) { 0343 configurePop3(m_pop3Host, m_pop3Port, m_pop3Username, m_pop3Password, m_pop3AuthenticationType, m_pop3SocketType); 0344 } 0345 0346 configureSmtp(m_smtpHost, m_smtpPort, m_smtpUsername, m_smtpPassword, m_smtpAuthenticationType, m_smtpSocketType); 0347 0348 m_setupManager->execute(); 0349 } 0350 0351 void NewAccount::configureImap(QString host, int port, QString username, QString password, AuthenticationType authentication, SocketType socket) 0352 { 0353 QObject *object = m_setupManager->createResource(QStringLiteral("akonadi_imap_resource")); 0354 auto *t = qobject_cast<Resource *>(object); 0355 0356 t->setName(username); 0357 t->setOption(QStringLiteral("ImapServer"), host); 0358 t->setOption(QStringLiteral("ImapPort"), port); 0359 t->setOption(QStringLiteral("UserName"), username); 0360 t->setOption(QStringLiteral("Password"), password); 0361 0362 switch (authentication) { 0363 case AuthenticationType::Plain: 0364 t->setOption(QStringLiteral("Authentication"), MailTransport::Transport::EnumAuthenticationType::CLEAR); 0365 break; 0366 case AuthenticationType::CramMD5: 0367 t->setOption(QStringLiteral("Authentication"), MailTransport::Transport::EnumAuthenticationType::CRAM_MD5); 0368 break; 0369 case AuthenticationType::NTLM: 0370 t->setOption(QStringLiteral("Authentication"), MailTransport::Transport::EnumAuthenticationType::NTLM); 0371 break; 0372 case AuthenticationType::GSSAPI: 0373 t->setOption(QStringLiteral("Authentication"), MailTransport::Transport::EnumAuthenticationType::GSSAPI); 0374 break; 0375 case AuthenticationType::OAuth2: 0376 t->setOption(QStringLiteral("Authentication"), MailTransport::Transport::EnumAuthenticationType::XOAUTH2); 0377 break; 0378 case AuthenticationType::NoAuth: 0379 break; 0380 default: 0381 break; 0382 } 0383 0384 switch (socket) { 0385 case SocketType::None: 0386 t->setOption(QStringLiteral("Safety"), QStringLiteral("None")); 0387 break; 0388 case SocketType::SSL: 0389 t->setOption(QStringLiteral("Safety"), QStringLiteral("SSL")); 0390 break; 0391 case SocketType::StartTLS: 0392 t->setOption(QStringLiteral("Safety"), QStringLiteral("STARTTLS")); 0393 break; 0394 default: 0395 break; 0396 } 0397 } 0398 0399 void NewAccount::configurePop3(QString host, int port, QString username, QString password, AuthenticationType authentication, SocketType socket) 0400 { 0401 QObject *object = m_setupManager->createResource(QStringLiteral("akonadi_pop3_resource")); 0402 auto *t = qobject_cast<Resource *>(object); 0403 0404 t->setName(username); 0405 t->setOption(QStringLiteral("Host"), host); 0406 t->setOption(QStringLiteral("Port"), port); 0407 t->setOption(QStringLiteral("Login"), username); 0408 t->setOption(QStringLiteral("Password"), password); 0409 0410 switch (authentication) { 0411 case AuthenticationType::Plain: 0412 t->setOption(QStringLiteral("AuthenticationMethod"), MailTransport::Transport::EnumAuthenticationType::PLAIN); 0413 break; 0414 case AuthenticationType::CramMD5: 0415 t->setOption(QStringLiteral("AuthenticationMethod"), MailTransport::Transport::EnumAuthenticationType::CRAM_MD5); 0416 break; 0417 case AuthenticationType::NTLM: 0418 t->setOption(QStringLiteral("AuthenticationMethod"), MailTransport::Transport::EnumAuthenticationType::NTLM); 0419 break; 0420 case AuthenticationType::GSSAPI: 0421 t->setOption(QStringLiteral("AuthenticationMethod"), MailTransport::Transport::EnumAuthenticationType::GSSAPI); 0422 break; 0423 case AuthenticationType::NoAuth: 0424 default: 0425 t->setOption(QStringLiteral("AuthenticationMethod"), MailTransport::Transport::EnumAuthenticationType::CLEAR); 0426 break; 0427 } 0428 0429 switch (socket) { 0430 case SocketType::SSL: 0431 t->setOption(QStringLiteral("UseSSL"), 1); 0432 break; 0433 case SocketType::StartTLS: 0434 t->setOption(QStringLiteral("UseTLS"), 1); 0435 break; 0436 case SocketType::None: 0437 default: 0438 t->setOption(QStringLiteral("UseTLS"), 1); // TODO is this correct? 0439 break; 0440 } 0441 } 0442 0443 void NewAccount::configureSmtp(QString host, int port, QString username, QString password, AuthenticationType authentication, SocketType socket) 0444 { 0445 QObject *object = m_setupManager->createTransport(QStringLiteral("smtp")); 0446 auto *t = qobject_cast<Transport *>(object); 0447 0448 t->setName(username); 0449 t->setHost(host); 0450 t->setPort(port); 0451 t->setUsername(username); 0452 t->setPassword(password); 0453 0454 switch (authentication) { 0455 case AuthenticationType::Plain: 0456 t->setAuthenticationType(QStringLiteral("plain")); 0457 break; 0458 case AuthenticationType::CramMD5: 0459 t->setAuthenticationType(QStringLiteral("cram-md5")); 0460 break; 0461 case AuthenticationType::NTLM: 0462 t->setAuthenticationType(QStringLiteral("ntlm")); 0463 break; 0464 case AuthenticationType::GSSAPI: 0465 t->setAuthenticationType(QStringLiteral("gssapi")); 0466 break; 0467 case AuthenticationType::OAuth2: 0468 t->setAuthenticationType(QStringLiteral("oauth2")); 0469 break; 0470 case AuthenticationType::NoAuth: 0471 break; 0472 default: 0473 break; 0474 } 0475 0476 switch (socket) { 0477 case SocketType::None: 0478 t->setEncryption(QStringLiteral("none")); 0479 break; 0480 case SocketType::SSL: 0481 t->setEncryption(QStringLiteral("ssl")); 0482 break; 0483 case SocketType::StartTLS: 0484 t->setEncryption(QStringLiteral("tls")); 0485 break; 0486 default: 0487 break; 0488 } 0489 } 0490 0491 void NewAccount::ispdbFinishedSearchingSlot() 0492 { 0493 if (!m_ispdb) { 0494 return; 0495 } 0496 0497 m_ispdbIsSearching = false; 0498 Q_EMIT ispdbIsSearchingChanged(); 0499 0500 // add smtp settings 0501 if (!m_ispdb->smtpServers().isEmpty()) { 0502 const Server s = m_ispdb->smtpServers().at(0); 0503 setSmtpHost(s.hostname); 0504 setSmtpPort(s.port); 0505 setSmtpUsername(s.username); 0506 setSmtpPassword(m_password); 0507 setSmtpAuthenticationType(ispdbTypeToAuth(s.authentication)); 0508 setSmtpSocketType(ispdbTypeToSocket(s.socketType)); 0509 } 0510 0511 // add imap settings 0512 if (!m_ispdb->imapServers().isEmpty()) { 0513 const Server s = m_ispdb->imapServers().at(0); 0514 setImapHost(s.hostname); 0515 setImapPort(s.port); 0516 setImapUsername(s.username); 0517 setImapPassword(m_password); 0518 setImapAuthenticationType(ispdbTypeToAuth(s.authentication)); 0519 setImapSocketType(ispdbTypeToSocket(s.socketType)); 0520 } 0521 0522 // add pop3 settings 0523 if (!m_ispdb->pop3Servers().isEmpty()) { 0524 const Server s = m_ispdb->pop3Servers().at(0); 0525 setPop3Host(s.hostname); 0526 setPop3Port(s.port); 0527 setPop3Username(s.username); 0528 setPop3Password(m_password); 0529 setPop3AuthenticationType(ispdbTypeToAuth(s.authentication)); 0530 setPop3SocketType(ispdbTypeToSocket(s.socketType)); 0531 } 0532 } 0533 0534 NewAccount::AuthenticationType NewAccount::ispdbTypeToAuth(Ispdb::authType authType) 0535 { 0536 switch (authType) { 0537 case Ispdb::Plain: 0538 return AuthenticationType::Plain; 0539 case Ispdb::CramMD5: 0540 return AuthenticationType::CramMD5; 0541 case Ispdb::NTLM: 0542 return AuthenticationType::NTLM; 0543 case Ispdb::GSSAPI: 0544 return AuthenticationType::GSSAPI; 0545 case Ispdb::OAuth2: 0546 return AuthenticationType::OAuth2; 0547 case Ispdb::ClientIP: 0548 case Ispdb::NoAuth: 0549 default: 0550 return AuthenticationType::NoAuth; 0551 } 0552 } 0553 0554 NewAccount::SocketType NewAccount::ispdbTypeToSocket(Ispdb::socketType socketType) 0555 { 0556 switch (socketType) { 0557 case Ispdb::SSL: 0558 return SocketType::SSL; 0559 case Ispdb::StartTLS: 0560 return SocketType::StartTLS; 0561 case Ispdb::None: 0562 default: 0563 return SocketType::None; 0564 } 0565 } 0566