Dialog uruchamiany przy pomocy przycisku z okna głównego:
dialog_send.h
#ifndef DIALOG_SEND_H
#define DIALOG_SEND_H
#include <QDialog>
#include <wiadomosc.h>
#include <startsend.h>
namespace Ui {
class Dialog_Send;
}
class Dialog_Send : public QDialog
{
Q_OBJECT
public:
explicit Dialog_Send(wiadomosc *Wiadomosc,QWidget *parent = 0);
~Dialog_Send();
void PrzedSend();
private slots:
void on_pushButton_clicked();
void slo_connect();
void slo_not_connect();
private:
Ui::Dialog_Send *ui;
bool OK_adres(QString &adres);
wiadomosc *myWiadomosc;
StartSend *cStartSend;
};
#endif // DIALOG_SEND_H
dialog_send.cpp
#include "dialog_send.h"
#include "ui_dialog_send.h"
Dialog_Send::Dialog_Send(wiadomosc *Wiadomosc,QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog_Send),
myWiadomosc(Wiadomosc)
{
ui->setupUi(this);
ui->progressBar->setValue(0);
cStartSend = new StartSend(myWiadomosc);
connect(cStartSend,SIGNAL(sig_connect()),this,SLOT(slo_connect()));
connect(cStartSend,SIGNAL(sig_not_connect()),this,SLOT(slo_not_connect()));
}
Dialog_Send::~Dialog_Send()
{
delete this->cStartSend;
delete ui;
}
void Dialog_Send::PrzedSend()
{
// ...
instrukcje sprawdzające poprawność adresów itp.
// ...
cStartSend->start(QThread::HighPriority);
}
bool Dialog_Send::OK_adres(QString &adres)
{
// ...
}
void Dialog_Send::on_pushButton_clicked()
{
if(ui->pushButton->text()=="Start")
{
ui->pushButton->setText("Anuluj");
ui->pushButton_2->setEnabled(false);
PrzedSend();
}
else
{
this->cStartSend->exit(-1); // czy tutaj ok?
ui->pushButton->setEnabled(false);
ui->plainTextEdit->appendPlainText("\nAnulowanie procedury wysyłania e-maili...");
ui->pushButton_2->setEnabled(true);
}
}
void Dialog_Send::slo_connect()
{
ui->plainTextEdit->appendPlainText("[+] Nawiązano połączenie z serwerem SMTP!");
}
void Dialog_Send::slo_not_connect()
{
ui->plainTextEdit->appendPlainText("[x] Nie można nawiązać połączenia z serwerem SMTP!\n Przyczyną może być brak dostępu do Internetu lub źle skonfigurowane konto.");
ui->pushButton->setText("Start");
ui->pushButton_2->setEnabled(true);
}
startsend.h //watek
#ifndef STARTSEND_H
#define STARTSEND_H
#include <QObject>
#include <QThread>
#include <QSslSocket>
#include <wiadomosc.h>
class StartSend : public QThread
{
Q_OBJECT
public:
explicit StartSend(wiadomosc *myWiadomosc,QThread *parent = 0);
void run();
private:
wiadomosc *myWiadomosc;
signals:
void sig_connect();
void sig_not_connect();
};
#endif // STARTSEND_H
startsend.cpp
StartSend::StartSend(wiadomosc *Wiadomosc,QThread *parent) :
QThread(parent),
myWiadomosc(Wiadomosc)
{
}
void StartSend::run()
{
//CONNECT
bool Connect = false;
QSslSocket mySocket;
if((int)myWiadomosc->type_encryption == 0)
{
mySocket.connectToHost(myWiadomosc->smtp, myWiadomosc->port);
if(mySocket.waitForConnected()) Connect = true;
}
else
{
if((int)myWiadomosc->type_encryption == 1) mySocket.setProtocol(QSsl::SslV3);
mySocket.connectToHostEncrypted(myWiadomosc->smtp, myWiadomosc->port);
if(mySocket.waitForConnected()) Connect = true;
}
if(!Connect)
{
this->sig_not_connect();
}
else
{
this->sig_connect();
}
while(true){};
exec();
}