/*****************************************************************************
 *
 *  softwareupdatedialog.cpp -- GUI for SoftwareUpdate class.
 *
 *  Copyright (C) 2010 Starbuck <starbuck@labdv.com>
 *  All rights reserved.
 *
 *  This file is NOT open source.
 *
 *****************************************************************************/

#include "../../src/gui4/macros.h"
#include "softwareupdatedialog.h"
#include <QDesktopServices>

SoftwareUpdateDialog::SoftwareUpdateDialog(QWidget *parent)
	: QDialog(parent)
{
	// Software update handling
	m_softwareUpdate = new SoftwareUpdate(&m_serverResponse, &m_whatYouGet,
		&m_downloadOpenFileUrl, &m_downloadSmartFileUrl, this);

	connect(m_softwareUpdate, SIGNAL(checkUpdateFailed(QString)), this, SLOT(checkUpdateFailed(QString)));
	connect(m_softwareUpdate, SIGNAL(checkUpdateFinished()), this, SLOT(checkUpdateDone()));
	connect(m_softwareUpdate, SIGNAL(checkUpdateSucceeded()), this, SLOT(checkUpdateDone()));

	// Activate widgets
	m_checkUpdateBox = new QGroupBox();
	m_checkUpdateLayout = new QVBoxLayout();
	m_checkUpdateText = new QLabel();

	m_checkUpdateBox->setTitle(tr("Check for %1 %2 new release").arg(APPNAME).arg(APPMAJORVERSION));
	m_checkUpdateBox->setLayout(m_checkUpdateLayout);
	m_checkUpdateLayout->addWidget(m_checkUpdateText);
	m_checkUpdateLayout->addStretch(5);
	m_checkUpdateText->setTextFormat(Qt::RichText);
	m_checkUpdateText->setWordWrap(true);
	m_checkUpdateText->setText(
		tr(	"Checking %1 %2 update server for new release...<br><br><br><br>")
			.arg(APPNAME).arg(APPMAJORVERSION));

	m_downloadBox = new QGroupBox();
	m_downloadLayout = new QHBoxLayout();
	m_downloadOpenButton = new QPushButton();
	m_downloadSmartButton = new QPushButton();
	m_downloadHomeButton = new QPushButton();

	m_downloadBox->setTitle(tr("Download %1 %2").arg(APPNAME).arg(APPMAJORVERSION));
	m_downloadBox->setLayout(m_downloadLayout);
	m_downloadOpenButton->setText(tr(" %1 %2 Open Edition").arg(APPNAME).arg(APPMAJORVERSION));
	m_downloadOpenButton->setToolTip(tr("Download %1 %2 Open Edition current release")
		.arg(APPNAME).arg(APPMAJORVERSION));
	m_downloadOpenButton->setEnabled(false);
	m_downloadSmartButton->setText(tr(" %1 %2 Smart Edition").arg(APPNAME).arg(APPMAJORVERSION));
	m_downloadSmartButton->setToolTip(tr("Download %1 %2 Smart Edition current release")
		.arg(APPNAME).arg(APPMAJORVERSION));
	m_downloadSmartButton->setEnabled(false);
	m_downloadHomeButton->setText(tr("Download server"));
	m_downloadHomeButton->setToolTip(tr("Visit %1 %2 download page")
		.arg(APPNAME).arg(APPMAJORVERSION));

	m_downloadLayout->addWidget(m_downloadOpenButton);
	m_downloadLayout->addWidget(m_downloadSmartButton);
	m_downloadLayout->addWidget(m_downloadHomeButton);

	connect(m_downloadHomeButton, SIGNAL(clicked()), this, SLOT(downloadHome()));
	connect(m_downloadOpenButton, SIGNAL(clicked()), this, SLOT(downloadOpen()));
	connect(m_downloadSmartButton, SIGNAL(clicked()), this, SLOT(downloadSmart()));

	m_whatyougetBox = new QGroupBox();
	m_whatyougetLayout = new QVBoxLayout();
	m_whatyougetText = new QLabel();

	m_whatyougetBox->setTitle(tr("What you get activating %1 %2 Smart Edition").arg(APPNAME).arg(APPMAJORVERSION));
	m_whatyougetBox->setLayout(m_whatyougetLayout);
	m_whatyougetBox->setVisible(false);
	m_whatyougetLayout->addWidget(m_whatyougetText);
	m_whatyougetText->setTextFormat(Qt::RichText);
	m_whatyougetText->setOpenExternalLinks(true);
	m_whatyougetText->setWordWrap(true);

	m_windowButtonAccept = new QPushButton(tr("Close"), this);
	m_windowButtonLayout = new QHBoxLayout();
#ifdef Q_OS_MAC
	m_windowButtonLayout->setSpacing(10);
#endif
	m_windowButtonLayout->addStretch(3);
	m_windowButtonLayout->addWidget(m_windowButtonAccept);
	
	connect(m_windowButtonAccept, SIGNAL(clicked()), this, SLOT(accept()));

	// Window stuff
	m_windowLayout = new QVBoxLayout();
	m_windowLayout->addWidget(m_checkUpdateBox);// the ownership of item is transferred to the layout
	m_windowLayout->addWidget(m_whatyougetBox);
	m_windowLayout->addWidget(m_downloadBox);
	m_windowLayout->addItem(m_windowButtonLayout);
	setLayout(m_windowLayout);					// the QWidget takes ownership of layout
	setAttribute(Qt::WA_AlwaysShowToolTips);
	setWindowTitle(tr("%1 %2 - Check for update").arg(APPNAME).arg(APPMAJORVERSION));
	setFixedWidth(480);
}

SoftwareUpdateDialog::~SoftwareUpdateDialog()
{
	Q_CHECK_PTR(m_softwareUpdate);

	m_softwareUpdate->disconnect();
}

void SoftwareUpdateDialog::checkUpdate()
{
	m_softwareUpdate->checkUpdate();
}

void SoftwareUpdateDialog::checkUpdateDone()
{
	m_downloadOpenButton->setDisabled(m_downloadOpenFileUrl.isEmpty());
	m_downloadSmartButton->setDisabled(m_downloadSmartFileUrl.isEmpty());
	
	if (m_serverResponse.isEmpty())
	{
		m_checkUpdateText->setText(
			tr(	"<i>%1 %2 update server is not responding.</i><br><br>Please try later.")
				.arg(APPNAME).arg(APPMAJORVERSION));
	}
	else
	{
		m_checkUpdateText->setText(m_serverResponse);
	}

	if (m_whatYouGet.isEmpty())
	{	
		m_whatyougetBox->setVisible(false);
	}
	else
	{
		m_whatyougetText->setText(m_whatYouGet);
		m_whatyougetBox->setVisible(true);
	}
}


void SoftwareUpdateDialog::checkUpdateFailed(QString errorMessage)
{
	if(errorMessage.isEmpty()) errorMessage = tr("unknown error");

	errorMessage.replace(0, 1, errorMessage[0].toUpper());

	m_checkUpdateText->setText(
		tr(	"<i>%1 %2 update server error.</i><br><br><b>%1.</b>")
			.arg(APPNAME).arg(APPMAJORVERSION).arg(errorMessage));

	// Uncomment to show servert error reply
	//m_serverResponse = errorMessage;
}

void SoftwareUpdateDialog::downloadHome()
{
	QDesktopServices::openUrl( QUrl("http://www.labdv.com/dvdx/download.php") );
}

void SoftwareUpdateDialog::downloadOpen()
{
	if(!m_downloadOpenFileUrl.isEmpty()) QDesktopServices::openUrl( QUrl(m_downloadOpenFileUrl) );
}

void SoftwareUpdateDialog::downloadSmart()
{
	if(!m_downloadSmartFileUrl.isEmpty()) QDesktopServices::openUrl( QUrl(m_downloadSmartFileUrl) );
}
