/*
For general Scribus (>=1.3.2) copyright and licensing information please refer
to the COPYING file provided with the program. Following this notice may exist
a copyright and/or license notice that predates the release of Scribus 1.3.2
for which a new license (GPL+exception) is in place.
*/
/***************************************************************************
*   Copyright (C) 2004 by Craig Bradney                                   *
*   cbradney@zip.com.au                                                   *
*   Copyright (C) 2005 by Petr Vanek                                      *
*   petr@yarpen.cz                                                        *
*                                                                         *
*   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.,                                       *
*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/

#ifndef HELPBROWSER_H
#define HELPBROWSER_H

#include <QAction>
#include <QItemSelection>
#include <QList>
#include <QMainWindow>
#include <QMenu>
#include <QMenuBar>
#include <QPair>
#include <QString>
#include <QToolBar>
#include <QUrl>
#include <QVariant>
#include <QWidget>
#include <QXmlInputSource>
#include <QEvent>
#include <QTreeWidgetItem>

class ScHelpTreeModel;

#include "HelpSideBar.h"

//! \brief A structure holding title/file url reference.
struct histd2 {
	QString url;
	QString title;
};

namespace Ui {
    class HelpBrowser;
}

class HelpBrowser : public QMainWindow
{
    Q_OBJECT

public:
	HelpBrowser(QWidget* parent);
	HelpBrowser(QWidget* parent, const QString& caption, const QString& guiLangage="en", const QString& jumpToSection="", const QString& jumpToFile="");
	~HelpBrowser();
	
	
	/*! \brief History menu. It's public because of history reader - separate class */
	QMenu* histMenu;
	/*! \brief Mapping the documents for history. */
	QMap<QAction*, histd2> mHistory;
	/*! \brief Set text to the browser
	\param str a QString with text (html) */
	void setText(const QString& str);
	
	static bool firstRun;

protected:
	virtual void changeEvent(QEvent* e);
	void closeEvent(QCloseEvent * event);

	void setupLocalUI();
	/*! \brief Reads saved bookmarks from external file */
	void readBookmarks();
	/*! \brief Search in doc files in spec. dir.
	It uses directory-recursion. I hope that the documentation will have
	only 2-3 level dir structure so it doesn't matter.
	\author Petr Vanek <petr@yarpen.cz> */
	void searchingInDirectory(const QString&);

	/*! \brief Reads saved history of browsing. */
	void readHistory();

	/*! \brief Tell the user there is no help available */
	void displayNoHelp();
	
	QMenu* fileMenu;
	QMenu* editMenu;
	QMenu* bookMenu;
	QMenu* viewMenu;
	QAction* filePrint;
	QAction* fileExit;
	QAction* editFind;
	QAction* editFindNext;
	QAction* editFindPrev;
	QAction* viewContents;
	QAction* viewSearch;
	QAction* viewBookmarks;
	QAction* bookAdd;
	QAction* bookDel;
	QAction* bookDelAll;
	QAction* goHome;
	QAction* goBack;
	QAction* goFwd;
	QAction* zoomIn;
	QAction* zoomOut;
	QAction* zoomOriginal;
	HelpSideBar *helpSideBar;
	qreal zoomFactor;
	//! \brief Selected language is here. If there is no docs for this language, "en" is used.
	QString language;
	//! \brief QString holding location of menu.xml we are using, we load the help files from here
	QString finalBaseDir;
	/*! \brief Text to be found in document */
	QString findText;
	
	ScHelpTreeModel* menuModel;
	QMap<QString, QString> quickHelpIndex;
	QMap<QString, QPair<QString, QString> > bookmarkIndex;
	
protected slots:
	virtual void languageChange();
	void histChosen(QAction* i);
	void jumpToHelpSection(const QString& jumpToSection, const QString& jumpToFile="");
	void loadHelp(const QString& filename);
	void loadMenu();
	void showLinkContents(const QString &link);
	QString bookmarkFile();
	QString historyFile();

	/*! \brief Load doc file when user select filename in content view. */
	void itemSelected(const QItemSelection & selected, const QItemSelection & deselected);

	/*! \brief Load doc file when user select filename in search view. */
	void itemSearchSelected(QTreeWidgetItem *, int);

	/*! \brief Load doc file when user select filename in bookmark view. */
	void itemBookmarkSelected(QTreeWidgetItem *, int);

	/*! \brief Performs searching in documentation.
	It walks through installed documentation and searching in all text files
	\author Petr Vanek <petr@yarpen.cz> */
	void searchingButton_clicked();

	/*! \brief Find text in one document.
	Classical ctrl+f searching.
	\author Petr Vanek <petr@yarpen.cz> */
	void find();

	/*! \brief Find next occurences of the text in one document.
	\author Petr Vanek <petr@yarpen.cz> */
	void findNext();

	/*! \brief Find previous occurences of the text in one document.
	\author Petr Vanek <petr@yarpen.cz> */
	void findPrevious();

	/*! \brief Print the documentation.
	Based on the Qt example.
	*/
	void print();

	/*! \brief Add document into bookmarks. */
	void bookmarkButton_clicked();

	/*! \brief Delete selected document from bookmarks. */
	void deleteBookmarkButton_clicked();

	/*! \brief Delete all bookmarks */
	void deleteAllBookmarkButton_clicked();

	/*! \brief Show helpSideBar Contents Tab */
	void viewContents_clicked();

	/*! \brief Show helpSideBar Search Tab */
	void viewSearch_clicked();

	/*! \brief Show helpSideBar Bookmarks */
	void viewBookmarks_clicked();

	/*! \brief zoom In */
	void zoomIn_clicked();
	
	/*! \brief zoom Original Size */
	void zoomOriginal_clicked();

	/*! \brief zoom Out*/
	void zoomOut_clicked();

	/*! \brief User clicked on an external link */
	void handleExternalLink(const QUrl &);

	/*! \brief Show Wait Cursor while loading a page*/
	void loadStarted();
	
	/*! \brief Restore Default Cursor */
	void loadFinished(bool);
	void linkHovered (const QString &, const QString &, const QString & );
signals:
	void closed();

protected:
    QScopedPointer<Ui::HelpBrowser> m_Ui;

};

#endif // HELPBROWSER_H

