/*
Copyright (C) 2006 - 2009 Evan Teran
                   eteran@alum.rit.edu

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

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.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include "CheckVersion.h"
#include "Debugger.h"
#include "CheckVersionOptionsPage.h"
#include <QMenu>
#include <QMessageBox>
#include <QHttp> 
#include <QUrl>
#include <QSettings>

//------------------------------------------------------------------------------
// Name: CheckVersion()
// Desc: 
//------------------------------------------------------------------------------
CheckVersion::CheckVersion() : m_Menu(0), m_Http(0), m_InitialCheck(true) {

	QSettings settings;
	if(settings.value("CheckVersion/check_on_start.enabled", true).toBool()) {
		doCheck();
	}
}

//------------------------------------------------------------------------------
// Name: ~CheckVersion()
// Desc: 
//------------------------------------------------------------------------------
CheckVersion::~CheckVersion() {
}

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
QWidget *CheckVersion::optionsPage() {
	return new CheckVersionOptionsPage;
}

//------------------------------------------------------------------------------
// Name: menu(QWidget *parent)
// Desc: 
//------------------------------------------------------------------------------
QMenu *CheckVersion::menu(QWidget *parent) {

	if(m_Menu == 0) {
		m_Menu = new QMenu(tr("CheckVersion"), parent);
		m_Menu->addAction(tr("&Check For Latest Version"), this, SLOT(doMenu()), QKeySequence(tr("Ctrl+C")));
	}
	
	return m_Menu;
}

//------------------------------------------------------------------------------
// Name: doCheck()
// Desc: 
//------------------------------------------------------------------------------
void CheckVersion::doCheck() {

	if(m_Http == 0) {
		m_Http = new QHttp(this);
		connect(m_Http, SIGNAL(done(bool)), this, SLOT(requestDone(bool)));
	}
	
	m_Http->abort();
	
	const char *proxy = getenv("HTTP_PROXY");
	if(proxy == 0) {
		proxy = getenv("http_proxy");
	}
	
	if(proxy != 0) {
		const QUrl url(proxy);
		m_Http->setProxy(url.host(), url.port(80), url.userName(), url.password());
	}
	
    m_Http->setHost("www.codef00.com");
    m_Http->get("/projects/debugger-latest");
}

//------------------------------------------------------------------------------
// Name: doMenu()
// Desc: 
//------------------------------------------------------------------------------
void CheckVersion::doMenu() {
	m_InitialCheck = false;
	doCheck();
}

//------------------------------------------------------------------------------
// Name: requestDone()
// Desc: 
//------------------------------------------------------------------------------
void CheckVersion::requestDone(bool error) {
	if(error) {
		if(!m_InitialCheck) {
			QMessageBox::information(0, tr("EDB: An Error Occured"), m_Http->errorString());	
		}
	} else {
		const QByteArray result = m_Http->readAll();
		const QString s = result;

		if(edb::v1::intVersion(s) > edb::v1::edbVersion()) {
			QMessageBox::information(
				0, 
				tr("EDB: New Version Available"), 
				tr("There is a newer version of EDB available: <strong>%1</strong>").arg(s));
		} else {
			if(!m_InitialCheck) {
				QMessageBox::information(
					0,
					tr("EDB: You are up to date"),
					tr("You are running the latest version of EDB"));
			}
		}
	}
	m_InitialCheck = false;
}

Q_EXPORT_PLUGIN2(CheckVersion, CheckVersion)
