/*
 *  Copyright (C) 2007-2008 CMM Sigma Andrzej Chmielowiec 
<cmmsigma@cmmsigma.eu>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2
 *  as published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 */

// SendMailDialog.cpp: implementation of the SendMailDialog class.
//
//////////////////////////////////////////////////////////////////////

#include "SendMailDialog.h"

SendMailDialog::SendMailDialog(QWidget *parent, Qt::WindowFlags f) : QDialog(parent, f)
{
	QGridLayout *layout = new QGridLayout();
	
	setWindowTitle(tr("Sending email"));

	mBreak = new QPushButton(tr("&Abort"));
	connect(mBreak, SIGNAL(clicked()), this, SLOT(reject()));
	mLabel = new QLabel(tr("Sending request in e-mail message"));
	mProgress = new QProgressBar();
	mProgress->setRange(0, 7);
	mProgress->setValue(0);

	layout->addWidget(mLabel, 0, 0, Qt::AlignTop | Qt::AlignLeft);
	layout->addWidget(mProgress, 1, 0, Qt::AlignTop);
	layout->addWidget(new QWidget(), 2, 0);
	layout->addWidget(mBreak, 3, 0, Qt::AlignRight | Qt::AlignBottom);
	layout->setRowStretch(0, 0);
	layout->setRowStretch(1, 0);
	layout->setRowStretch(2, 1);
	layout->setRowStretch(3, 0);

	setLayout(layout);

	mSmtp = new QtSmtp(this);
	connect(mSmtp, SIGNAL(errorOccured(const QString &)), this, SLOT(smtpErrorOccured(const QString &)));
	connect(mSmtp, SIGNAL(messageSend()), this, SLOT(smtpMessageSend()));
	connect(mSmtp, SIGNAL(progressStep()), this, SLOT(smtpProgressStep()));
}

SendMailDialog::~SendMailDialog()
{

}

void
SendMailDialog::smtpMessageSend()
{
	QMessageBox::information(this, tr("Information"), tr("Message was send."));
	mProgress->setValue(7);
	reject();
}

void
SendMailDialog::smtpErrorOccured(const QString &error)
{
	QMessageBox::critical(this, QObject::tr("Error"), error);
	reject();
}

void
SendMailDialog::smtpProgressStep()
{
	mProgress->setValue(mProgress->value() + 1);
}

QtSmtp *SendMailDialog::getSmtp()
{
	return mSmtp;
}
