/***************************************************************************
                          dcfilebrowser.h  -  description
                             -------------------
    begin                : Fre Nov 29 2002
    copyright            : (C) 2002 by Mathias Küster
    email                : mathen@users.berlios.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef DCFILEBROWSER_H
#define DCFILEBROWSER_H

#include <qwidget.h>
#include <q3listview.h>
#include <qpixmap.h>
//Added by qt3to4:
#include <QList>
#include <QThread>
#include <QEvent>

#include "dcfiletool.h"

#include <dclib/dcos.h>
#include <dclib/core/cxml.h>
#include <dclib/core/cbytearray.h>

#include "DCDialogFileBrowser.h"

#include "dcwidget.h"

/**
  *@author Mathias Küster
  */

class DCFilelistDecompressor : public QThread {
Q_OBJECT

public:
	/** constructor for decompressing a list from disk */
	DCFilelistDecompressor( QWidget * widget, CString filename );
	/** constructor for decompressing our own list from memory */
	DCFilelistDecompressor( QWidget * widget, CByteArray * memory );
	/** destructor */
	virtual ~DCFilelistDecompressor();
	
	/** the method that runs in the thread */
	virtual void run();
	
	/** returns the pointer to the decompressed data */
	CString * getData();
	/** */
	CByteArray * recompressed;

signals:
	/** */
	virtual void finished( bool ok );

private:
	/** */
	CString * LoadOldValknutList( );
	/** file name */
	CString filename;
	/** decompress from memory */
	CByteArray * compressed;
	/** pointer to the decompressed data */
	CString * data;
	/** the gui to send the finished event to */
	QWidget * gui;
};

class DCFileItem {
public:
	/** construtor */
	DCFileItem() {};
	/** destructor */
	virtual ~DCFileItem() {};

	/** */
	QString m_sName;
	/** */
	QString m_sSize;
	/** */
	QString m_sHash;
	/** */
	bool m_bIsDir;
};

class DCFileBrowserListItem : public Q3ListViewItem {
public:
	/** construtor */
	DCFileBrowserListItem( Q3ListView * parent ) : Q3ListViewItem(parent) {
		m_pFileItem = 0;
	};
	/** construtor */
	DCFileBrowserListItem( Q3ListViewItem * parent ) : Q3ListViewItem(parent) {
		m_pFileItem = 0;
	};

	/** destructor */
	virtual ~DCFileBrowserListItem() {};
	
	virtual QString key( int column, bool ascending ) const {
		if ( column == 1 )
		{
			return QString().setNum(m_nBytes).rightJustify( 20, '0' );
		}
		else
		{
			return Q3ListViewItem::key( column, ascending );
		}
	};

	/** */
	QList<DCFileItem*> m_pFileList;
	/** */
	QString m_sName;
	/** Size in bytes of directory contents */
	ulonglong m_nBytes;
	/** Pointer to the DCFileItem that represents this folder */
	DCFileItem * m_pFileItem;
};

/* This is necessary because ListView_DIRECTORY->currentItem() is not reliable. */
class DCFBListViewItemWithPointer : public DC_FBListViewItem {
public:
	/** Constructor */
	DCFBListViewItemWithPointer( Q3ListView * parent ) : DC_FBListViewItem( parent ) { pDirItem = 0; };
	/** Constructor */
	DCFBListViewItemWithPointer( Q3ListViewItem * parent ) : DC_FBListViewItem ( parent ) { pDirItem = 0; };
	
	/** Destructor */
	virtual ~DCFBListViewItemWithPointer() {};
	
	/** The left pane item for this right pane item */
	DCFileBrowserListItem * pDirItem;
};

class DCFileBrowser : public QWidget, private Ui::DCDialogFileBrowser  {
Q_OBJECT

public: 
	/** construtor */
	DCFileBrowser( QWidget* parent=0, const char *name=0, Qt::WindowFlags wflags=0, bool viewonly=FALSE );
	/** destructor */
	virtual ~DCFileBrowser();

	/** */
	void InitDocument();
	/** */
	void InitTree( QString nick, QString hubname, QString hubhost, QString filename );
	/** */
	void ContinueInitTree( CString * data );
	/** */
	void InitTXTTree( Q3ListViewItem * item, CString * data );
	/** */
	void InitXMLTree( Q3ListViewItem * item, CString * data );
	/** */
	void ParseXMLTree( CXml * xml, xmlNodePtr node, Q3ListViewItem * item );
	/** to get events for folder opening */
	virtual bool event( QEvent * event );

private:
	/** */
	void CreateMenu( Q3ListViewItem * item, bool direntry, int column );
	/** */
	QString CreateRemotePath( Q3ListViewItem * item );
	/** */
	int selectedItems( Q3ListView * list, QList<Q3ListViewItem*> & lst );

	/** */
	void DownloadFile( QString rootPath, QString localPath, QString localName,
			   QString remotePath, QString remoteName, ulonglong size,
			   QString hash );
	/** */
	void DownloadPath( QString rootPath, QString localPath, QString localName, Q3ListViewItem * item );

	/** */
	void AddFile( Q3ListViewItem * item, DCFileItem * fileitem );
	/** */
	void AddDirectory( Q3ListViewItem * item, QString name );
	/** */
	void ClearView();
	/** Recursively calculates the size of a folder's contents.
	 *  This is only needed for non-xml filelists.
	 */
	ulonglong CalcDirSize( Q3ListViewItem * item );
	/** Gets the left pane item for the right pane folder item */
	Q3ListViewItem * GetDirItem( Q3ListViewItem * item );
	/** disable all the gui elements, while thread running */
	void DisableGuiElements();
	/** enable all the gui elements, after thread finished */
	void EnableGuiElements();
	
	/** */
	void GetOwnList();
	
	/** */
	QString m_sNick;
	/** */
	QString m_sHubName;
	/** */
	QString m_sHubHost;
	/** */
	CByteArray m_ListData;
	/** */
	DCFileTool m_FileTool;
	/** */
	QString m_sFileName;
	/** */
	ulonglong m_nShareSize;
	/** */
	ulonglong m_nFileCount;
	/** */
	QPixmap m_FolderPixmap;
	/** */
	DCFilelistDecompressor * m_pDecomp;

private slots:
	/** */
	void slotRightButtonClickedDirectory( Q3ListViewItem *, const QPoint &, int );
	/** */
	void slotRightButtonClickedFiles( Q3ListViewItem *, const QPoint &, int );
	/** */
	void slotCurrentChangedDirectory();
	/** */
	void slotExpandedDirectory( Q3ListViewItem * );
	/** */
	void slotCollapsedDirectory( Q3ListViewItem * );
	/** */
	void slotFileOpen();
	/** */
	void slotFileSave();
	/** */
	void slotItemDoubleClicked( Q3ListViewItem *, const QPoint &, int );
	/** */
	void slotThreadFinishedLoading( bool ok );
};

#endif
