/*
Copyright (C) 2006 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 <QMenu>
#include <QMessageBox>
#include <QHttp> 
#include <QUrl>
#include <QSettings>
#include "Debugger.h"

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

	QSettings settings;
	settings.beginGroup("CheckVersion");
	m_CheckOnStart = settings.value("check_on_start.enabled", true).toBool();
	settings.endGroup();
	
	if(m_CheckOnStart) {
		doCheck();
	}
}

//------------------------------------------------------------------------------
// Name: ~CheckVersion()
// Desc: 
//------------------------------------------------------------------------------
CheckVersion::~CheckVersion() {
	delete m_Menu;
}

//------------------------------------------------------------------------------
// Name: setCheckOnStart(bool check)
// Desc: 
//------------------------------------------------------------------------------
void CheckVersion::setCheckOnStart(bool check) {
	QSettings settings;
	settings.beginGroup("CheckVersion");
	settings.setValue("check_on_start.enabled", check);
	settings.endGroup();
}

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

	if(m_Menu == 0) {
		m_Menu = new QMenu("CheckVersion", parent);
		m_Menu->addAction("&Check For Latest Version", this, SLOT(doMenu()), QKeySequence("Ctrl+C"));
		
		QAction *const action = new QAction("Check On Start", m_Menu);
	    action->setCheckable(true);
	    action->setChecked(m_CheckOnStart);
		m_Menu->addAction(action);
		connect(action, SIGNAL(toggled(bool)), this, SLOT(setCheckOnStart(bool)));
	}
	
	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(edb::v1::debuggerUI, "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::intVersion()) {
			QMessageBox::information(edb::v1::debuggerUI, "EDB: New Version Available", QString().sprintf("There is a newer version of EDB available: <strong>%s</strong>", qPrintable(s)));
		} else {
			if(!m_InitialCheck) {
				QMessageBox::information(edb::v1::debuggerUI, "EDB: You are up to date", "You are running the latest version of EDB");	
			}
		}
	}
	m_InitialCheck = false;
}

Q_EXPORT_PLUGIN2(CheckVersion, CheckVersion)
