/***************************************************************************
 *                                                                         *
 *   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>
#include <QPixmap>
#include <QEvent>
#include <QStringList>
#include <QMdiSubWindow>
#include <QListView>
#include <QItemSelectionModel>

#include <dclib/dcos.h>
#include <dclib/core/cstring.h>

#include "DCDialogFileBrowser.h"
#include "dcfilebrowserhistory.h"
#include "dcfilebrowsersearchengine.h"

class CByteArray;
class CXml;
class DCFileItem;
class DCFilelistDecompressor;
class FileBrowserItem;
class FileBrowserModel;

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

public:
    typedef QHash<QString, QString> QStringHash;
    /** construtor */
    DCFileBrowser(QWidget * parent = 0, bool AllowOpen = true);
    /** destructor */
    virtual ~DCFileBrowser();

    /** */
    void InitTree(QString nick, QString hubname, QString hubhost, QString filename, QString jumpto = QString::null, QStringList dirs = QStringList());

    /** */
    void SetContainer(QMdiSubWindow *w) {
        m_pContainer = w;
    }

    /** */
    QMdiSubWindow *GetContainer() const {
        return m_pContainer;
    }

protected:
    /** to get events for folder opening */
    virtual void customEvent(QEvent * event);
    /** */
    virtual void closeEvent(QCloseEvent *e);
    /** */
    virtual bool eventFilter(QObject * object, QEvent * event);

private:
    /** */
    static QStringHash &GetDownloadDirs();
    /** */
    void ContinueInitTree(CXml * xml, CString * text);
    /** */
    void InitTXTTree(FileBrowserItem * item, CString * data);
    /** */
    void InitXMLTree(FileBrowserItem * item, CXml * xml);
    /** */
    void ParseXMLTree(CXml * xml, FileBrowserItem * item);
    /** */
    bool DownloadDirectory(QString dir);
    /** */
    void JumpTo(QString target);
    /** */
    void DownloadFile(QString rootPath, QString localPath, QString localName,
            QString remotePath, QString remoteName, ulonglong size,
            QString hash);
    /** */
    void DownloadPath(QString rootPath, QString localPath, QString localName, FileBrowserItem * item);

    /** */
    void createProxyTree(FileBrowserItem*, FileBrowserItem*);

    /** */
    void AddFile(FileBrowserItem * item, FileBrowserItem * fileitem);
    /** Recursively calculates the size of a folder's contents.
     *  This is only needed for non-xml filelists.
     */
    ulonglong CalcDirSize(FileBrowserItem * item);
    /** disable all the gui elements, while thread running */
    void DisableGuiElements();
    /** enable all the gui elements, after thread finished */
    void EnableGuiElements();

    /** */
    QString m_sNick;
    /** */
    QString m_sHubName;
    /** */
    QString m_sHubHost;
    /** */
    QString m_sFileName;
    /** */
    QString m_sJumpTo;
    /** */
    QStringList m_lDownloadDirs;
    /** */
    ulonglong m_nShareSize;
    /** */
    ulonglong m_nFileCount;
    /** */
    DCFilelistDecompressor * m_pDecomp;
    /** */
    CByteArray * m_pListData;

    /** To disable download for own filelist */
    bool m_bAllowDownload;
    /** Disable the open file button for own file list from options dialog */
    bool m_bAllowOpen;

    /** */
    QMdiSubWindow *m_pContainer;
    /** */
    static QStringHash download_dirs;

    /** */
    FileBrowserModel *model;
    /** */
    FileBrowserModel *treeModel;
    /** */
    FileBrowserItem *rootElem;
    /** */
    FileBrowserItem *proxyRoot;
    /** */
    DCFileBrowserHistory<FileBrowserItem*> history;
    /** */
    DCFileBrowserSearchEngine *search_engine;

private slots:
    /** */
    void slotCurrentChangedDirectory(const QModelIndex &);
    /** */
    void slotFileOpen();
    /** */
    void slotFileSave();
    /** */
    void CreateMenu(const QPoint &, bool);
    /** */
    void slotItemDoubleClicked(const QModelIndex &);
    /** */
    void slotSearchResultClicked(const QModelIndex &);
    /** */
    void slotThreadFinishedLoading();
    /** find the hub and jump to the user for this filelist */
    void slotGotoUser();
    /** */
    void slotRootChanged(FileBrowserItem*,FileBrowserItem*);
    /** */
    void slotHistoryForward();
    /** */
    void slotHistoryBack();
    /** */
    void slotDirUp();

    void selectionChanged(const QItemSelection&, const QItemSelection&);

    /** */
    void slotFILES_customContextMenuRequested(const QPoint &);
    /** */
    void slotDIRECTORY_customContextMenuRequested(const QPoint &);

    /** */
    void slotSearchEngineStateChanged(DCFileBrowserSearchEngine::State);
    /** */
    void slotDoSearch();
    /** */
    void slotSearchReset();
    /** */
    void slotNewModel(FileBrowserModel*);
    /** */
    void slotNewElement(FileBrowserItem*);
signals:
    void onDie(QWidget*);
};

inline DCFileBrowser::QStringHash &DCFileBrowser::GetDownloadDirs(){
    return download_dirs;
}


#endif
