/*
 *  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 3
 *  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.
 */

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

#include "ReceiveMailDialog.h"

ReceiveMailDialog::ReceiveMailDialog(const MailConfig &config, const QString &receivedPath,
	QWidget *parent, Qt::WindowFlags f) : QDialog(parent, f)
{
	QGridLayout *layout = new QGridLayout();
	
	setWindowTitle(tr("Receiving requests"));

	mBreak = new QPushButton(tr("&Abort"));
	connect(mBreak, SIGNAL(clicked()), this, SLOT(reject()));
	mLabel = new QLabel(tr("Receiving requests in e-mail messages"));
	mProgress = new QProgressBar();

	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);

	mProgress->setRange(0, 1);
	
	setLayout(layout);

	mReqFilter = config.mReqFilter;
	mPop3 = new QtPop3();
	connect(mPop3, SIGNAL(errorOccured(const QString &)), this, SLOT(pop3ErrorOccured(const QString &)));
	connect(mPop3, SIGNAL(noNewMessages()), this, SLOT(pop3NoNewMessages()));
	connect(mPop3, SIGNAL(messageReceived(const MessageItem &)), this, SLOT(pop3MessageReceived(const MessageItem &)));
	connect(mPop3, SIGNAL(newMessagesCount(const int &)), this, SLOT(pop3NewMessagesCount(const int &)));
	connect(mPop3, SIGNAL(newMessages(const QStringList &)), this, SLOT(pop3NewMessages(const QStringList &)));

	mPop3->setDownloadPath(receivedPath);
	mPop3->setLeaveOnServer();
	mPop3->setLocalWriteMode(false);
	mPop3->setMessageLimit(10 * 1024);
	mPop3->setServer(config.mPop3Server);
	mPop3->setUserPassword(config.mPop3User, config.mPop3Pass);
	mPop3->receiveAll();
}

ReceiveMailDialog::~ReceiveMailDialog()
{

}

void
ReceiveMailDialog::pop3ErrorOccured(const QString &error)
{
	QMessageBox::critical(this, tr("Error"), error);
	reject();
}

void
ReceiveMailDialog::pop3NoNewMessages()
{
	mProgress->setValue(1);
	displayMessage(false);
	reject();
}

void
ReceiveMailDialog::pop3MessageReceived(const MessageItem &msg)
{
	if((msg.subject.contains(mReqFilter, Qt::CaseInsensitive) ||
		msg.msgId.contains(QString(QByteArray("sToken").toBase64()))) &&
		!msg.msgId.contains("sToken_certificate")) // do not receive certificates
	{
		processMessage(msg);
	}

	mProgress->setValue(mProgress->value() + 1);
}

void
ReceiveMailDialog::processMessage(const MessageItem &msg)
{
	foreach(AttachmentItem att, msg.atts)
	{
//		if(att.file.contains(".req", Qt::CaseInsensitive))
			mRequestFiles.append(att);
	}
	
	foreach(MessageItem sub, msg.subMessages)
	{
		processMessage(sub);
	}
}

void
ReceiveMailDialog::pop3NewMessagesCount(const int &count)
{
	if(count > 0)
	{
		mProgress->setRange(0, count);
		mProgress->setValue(0);
	}
}

void
ReceiveMailDialog::pop3NewMessages(const QStringList &)
{
	bool received = mRequestFiles.size() > 0;
	displayMessage(received);
	if(received)
		accept();
	else
		reject();
}

void
ReceiveMailDialog::displayMessage(bool received)
{
	if(received)
		QMessageBox::information(this, tr("Information"), tr("New requests received."));
	else
		QMessageBox::information(this, tr("Information"), tr("No new requests."));
}

QList<AttachmentItem> & ReceiveMailDialog::getRequestFiles()
{
	return mRequestFiles;
}
