
//  Copyright (C) 2009 by Arnaud Dupuis
//  a.dupuis@infinityperl.org
//  http://www.infinityperl.org
// 
//  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 3 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.,
//  59 Temple Place - Suite 330, Boston, MA  02111-1307, USA

#ifdef ASSUMA_WITH_UPDATER

#ifndef ASSUMA_UPDATEDIALOG_H
#define ASSUMA_UPDATEDIALOG_H

#include <QDialog>
#include <QProcess>
#include "ui_assuma_updatedialog.h"
#include "assuma_mainwindow.h"

class QString;
class QNetworkAccessManager;
class QIODevice;
class QFile;
class AssumaUpdateDelegate;
class QNetworkReply;
class QListWidgetItem;
/*! \class AssumaUpdateDialog
* \brief An update dialog.
*
* This class is the default upgrade dialog of Association Subscribers Manager.
* The most important features are available through the public API.
* This API will evolve in a near futur to make it more "provider independant".
* This will allow, for example, anybody to embded this dialog in its own plugin to provide custom upgrades.
*/
class AssumaUpdateDialog : public QDialog, public Ui::AssumaUpdateDialog
{
	Q_OBJECT
	
	public:
		enum Filters {
			AssumaUpdates,
			AddonsUpdates,
			AddonsCatalog,
		};
		/*!
		Construct a new update dialog.
		At the moment, the parent argument is mandatory, this constructor use it to get the configuration widget.
		Proxy and users settings are taken from the configuration widget. This is bad and we know it, it will change soon.
		\attention this constructor is likely to change soon.
		\attention IMPORTANT: authentication mechanism have been suppressed since it is no longer available on server side !
		*/
		AssumaUpdateDialog(AssumaMainWindow *parent);
		~AssumaUpdateDialog();
	public slots:
		/*!
		Set the proxy to use.
		\param url proxy URL
		\param port proxy port (default is 8080)
		\param login proxy login
		\param password proxy password
		*/
		void setProxy( const QString&, const int port = 8080, const QString & login = "" , const QString & password = "" );
		/*!
		Start the update checking process (connect to the update server, download the update list and present it in the main list).
		*/
		void checkForUpdates();
		/*!
		Add item item to the list of updates.
		\param item item to add.
		*/
		void addItem( QListWidgetItem * );
		/*!
		Load a theme for the UI.
		\param themeName the name of the theme to load. This is the name of a directory in the theme/ directory.
		*/
		void loadTheme( const QString & themeName = "default" );
	signals:
		/*!
		This signal is emitted once an update finished. 
		The boolean parameters is true is upgrade was successfull and false otherwise.
		*/
		void updateFinished(bool);
	private slots:
		void currentRowChanged(int);
		void on_closeButton_clicked();
		void on_checkButton_clicked();
                void on_installButton_clicked();
		void networkManagerRequestFinished( QNetworkReply * );
		void downloadProgressSlot(qint64, qint64);
	private:
		// Methods
		QString itemHtml(QListWidgetItem *);
		QListWidgetItem * itemFromOperationId( int );
		void checkMd5Sum( QListWidgetItem * , QFile* );
		void installUpdate( QListWidgetItem * , QFile* );
		int countVisibleItems( QListWidget * );
		// Variables
		bool haveBeenUpdated;
		QString m_proxyUrl;
		int m_proxyPort;
		QString m_proxyLogin;
		QString m_proxyPassword;
		QMap<QString,int> httpOpsMap;
		QMap<QNetworkReply *, QListWidgetItem *> m_networkOpsMap;
		QMap<QString,QIODevice *> httpBuffersMap;
		QNetworkAccessManager *m_networkManager;
		QTime m_time;
		int m_lastTick;
	
};

#endif //ASSUMA_UPDATEDIALOG_H

#endif //ASSUMA_WITH_UPDATER
