/****************************************************************************
**
** Copyright (C) 1992-2004 Trolltech AS. All rights reserved.
**
** This file is part of the assistant application of the Qt Toolkit.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
**   information about Qt Commercial License Agreements.
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/

#ifndef HELPDIALOG_H
#define HELPDIALOG_H

#include "ui_helpdialog.h"
#include "index.h"
#include "helpwindow.h"
#include "docuparser.h"

#include <q3listbox.h>
#include <q3listview.h>
#include <qmap.h>
#include <qstringlist.h>
#include <qvalidator.h>
#include <qmenu.h>
#include <qhash.h>

class QProgressBar;
class MainWindow;
class QTextBrowser;

class HelpNavigationListItem : public Q3ListBoxText
{
public:
    HelpNavigationListItem(Q3ListBox *ls, const QString &txt);

    void addLink(const QString &link);
    QStringList links() const { return linkList; }
private:
    QStringList linkList;

};

class SearchValidator : public QValidator
{
    Q_OBJECT
public:
    SearchValidator(QObject *parent)
        : QValidator(parent) {}
    ~SearchValidator() {}
    QValidator::State validate(QString &str, int &) const;
};

class HelpNavigationContentsItem : public Q3ListViewItem
{
public:
    HelpNavigationContentsItem(Q3ListView *v, Q3ListViewItem *after);
    HelpNavigationContentsItem(Q3ListViewItem *v, Q3ListViewItem *after);

    void setLink(const QString &lnk);
    QString link() const;

private:
    QString theLink;
};

class HelpDialog : public QWidget
{
    Q_OBJECT
public:
    HelpDialog(QWidget *parent, MainWindow *h);

    inline QTabWidget *tabWidget() const
    { return ui.tabWidget; }

    QString titleOfLink(const QString &link);
    bool eventFilter(QObject *, QEvent *);
    bool lastWindowClosed() { return lwClosed; }

    void timerEvent(QTimerEvent *e);

signals:
    void showLink(const QString &s);
    void showSearchLink(const QString &s, const QStringList &terms);

public slots:
    void initialize();
    void startSearch();
    void addBookmark();

private slots:
    void on_buttonAdd_clicked();
    void on_buttonRemove_clicked();
    void on_termsEdit_returnPressed();
    void on_helpButton_clicked();
    void on_searchButton_clicked();
    void on_resultBox_returnPressed(Q3ListBoxItem*);
    void on_resultBox_mouseButtonClicked(int, Q3ListBoxItem*, const QPoint &);

    void showResultPage(Q3ListBoxItem *);

    void showTopic(int, Q3ListBoxItem *, const QPoint &);
    void showTopic(int, Q3ListViewItem *, const QPoint &);
    void showTopic(Q3ListViewItem *);
    void loadIndexFile();
    void insertContents();
    void setupFullTextIndex();
    void currentTabChanged(int index);
    void currentIndexChanged(Q3ListBoxItem *i);
    void showTopic();
    void searchInIndex(const QString &s);
    void currentBookmarkChanged(Q3ListViewItem *i);
    void currentContentsChanged(Q3ListViewItem *i);
    void toggleContents();
    void toggleIndex();
    void toggleBookmarks();
    void toggleSearch();
    void lastWinClosed();
    void setIndexingProgress(int prog);
    void showItemMenu(Q3ListBoxItem *item, const QPoint &pos);
    void showItemMenu(Q3ListViewItem *item, const QPoint &pos);
    void insertBookmarks();
    void processEvents();

private:
    typedef QList<ContentItem> ContentList;
    void removeOldCacheFiles();
    void buildKeywordDB();
    Q_UINT32 getFileAges();
    void showIndexTopic();
    void showBookmarkTopic();
    void setupTitleMap();
    void saveBookmarks();
    void showContentsTopic();
    void showInitDoneMessage();
    void buildContentDict();

private:
    Ui::HelpDialog ui;

    QMap<QString, QString> titleMap;
    bool indexDone, bookmarksInserted, titleMapDone, contentsInserted;
    bool lwClosed;
    MainWindow *help;
    QString documentationPath;
    Index *fullTextIndex;
    QStringList terms, foundDocs;
    bool initDoneMsgShown;
    void getAllContents();
    QHash<QString, ContentList> contentList;
    QMenu *itemPopup;
    QString cacheFilesPath;

    QAction *actionOpenCurrentTab;
    QAction *actionOpenLinkInNewWindow;
    QAction *actionOpenLinkInNewTab;
};

#endif
