/*
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 "Debugger.h"

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

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

//------------------------------------------------------------------------------
// 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"));
	}
	
	return m_Menu;
}

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

	if(m_Http == 0) {
		m_Http = new QHttp(this);
		connect(m_Http, SIGNAL(done(bool)), this, SLOT(requestDone()));
	}
	
    m_Http->setHost("www.codef00.com");	// id == 1
    m_Http->get("/projects/debugger-latest");		// id == 2
	
}

//------------------------------------------------------------------------------
// Name: requestDone()
// Desc: 
//------------------------------------------------------------------------------
void CheckVersion::requestDone() {
	const QByteArray result = m_Http->readAll();
	const QString s(result);	
	
	if(Debugger::intVersion(s) > Debugger::intVersion()) {
		QMessageBox::information(Debugger::debuggerGUI, "EDB: New Version Available", QString().sprintf("There is a newer version of EDB available: %s", qPrintable(s)));
	} else {
		QMessageBox::information(Debugger::debuggerGUI, "EDB: You are up to date", "You are running the latest version of EDB");	
	}
}

Q_EXPORT_PLUGIN2(CheckVersion, CheckVersion)
