Skip to content

Commit a57d7a5

Browse files
committed
[demo] Cache server object so that the same one is used
1 parent d21e4c1 commit a57d7a5

2 files changed

Lines changed: 28 additions & 5 deletions

File tree

demos/demo2/sendemail.cpp

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,29 @@ void SendEmail::on_sendEmail_clicked()
9494
void SendEmail::sendMailAsync(const MimeMessage &msg)
9595
{
9696
qDebug() << "sendMailAsync";
97-
auto server = new Server(this);
98-
server->setHost(ui->host->text());
99-
server->setPort(quint16(ui->port->value()));
100-
server->setConnectionType(ui->security->currentIndex() == 0 ? Server::TcpConnection :
101-
ui->security->currentIndex() == 1 ? Server::SslConnection : Server::TlsConnection);
97+
98+
const QString host = ui->host->text();
99+
const quint16 port(ui->port->value());
100+
const Server::ConnectionType ct = ui->security->currentIndex() == 0 ?
101+
Server::TcpConnection : ui->security->currentIndex() == 1 ?
102+
Server::SslConnection : Server::TlsConnection;
103+
104+
Server *server = nullptr;
105+
for (auto srv : m_aServers) {
106+
if (srv->host() == host && srv->port() == port & srv->connectionType() == ct) {
107+
server = srv;
108+
break;
109+
}
110+
}
111+
112+
if (!server) {
113+
server = new Server(this);
114+
server->setHost(host);
115+
server->setPort(port);
116+
server->setConnectionType(ct);
117+
m_aServers.push_back(server);
118+
}
119+
102120
const QString user = ui->username->text();
103121
if (!user.isEmpty()) {
104122
server->setAuthMethod(Server::AuthLogin);

demos/demo2/sendemail.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ namespace Ui {
2626
class SendEmail;
2727
}
2828

29+
namespace SimpleMail {
30+
class Server;
31+
}
32+
2933
using namespace SimpleMail;
3034

3135
class SendEmail : public QWidget
@@ -45,6 +49,7 @@ private Q_SLOTS:
4549

4650
private:
4751
QSettings m_settings;
52+
std::vector<Server *> m_aServers;
4853
Ui::SendEmail *ui;
4954

5055
void errorMessage(const QString & message);

0 commit comments

Comments
 (0)