/****************************************************************************
** Dooble - The Secure Internet Web Browser
**
** Copyright (c) 2008, 2009, 2010, 2011, 2012 Alexis Megas,
** Gunther van Dooble, and the Dooble Team.
** All rights reserved.
**
** License: GPL2 only:
** 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; version 2 of the License only.
**
** 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
** or see here: http://www.gnu.org/licenses/gpl.html
**
** For the WebKit library, please see: http://webkit.org.
**
** THE CODE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY
** EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
** ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
** GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
** IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
** ARISING IN ANY WAY OUT OF THE USE OF THIS APPLICATION, EVEN IF ADVISED
** OF THE POSSIBILITY OF SUCH DAMAGE.
**
** Please report all praise, requests, bugs, and problems to the project
** team and administrators: http://sf.net/projects/dooble.
**
** You can find us listed at our project page. New team members are welcome.
** The name of the authors should not be used to endorse or promote products
** derived from Dooble without specific prior written permission.
** If you use this code for other projects, please let us know.
**
** Web sites:
**   http://sf.net/projects/dooble
**   http://dooble.sf.net
****************************************************************************/

#ifndef _dview_h_
#define _dview_h_

#include <QUrl>
#include <QIcon>
#include <QPointer>
#include <QHostInfo>
#include <QByteArray>
#include <QStackedWidget>

class dwebpage;
class dwebview;
class dftpbrowser;
class dfilemanager;
class dwebviewsimple;
class dnetworkdirreply;
class dnetworkftpreply;
class dnetworkerrorreply;

class QWebView;
class QSslError;
class QWebFrame;
class QNetworkReply;
#ifdef DOOBLE_USE_QGRAPHICSWEBVIEW
class QGraphicsView;
class QGraphicsScene;
#endif
class QWebHistoryItem;

class dview: public QStackedWidget
{
  Q_OBJECT

 public:
  dview(QWidget *parent, const QByteArray &history);
  ~dview();
  int progress(void) const;
  bool isDir(void) const;
  bool isFtp(void) const;
  bool isLoaded(void) const;
  bool canGoBack(void) const;
  bool isModified(void) const;
  bool canGoForward(void) const;
  bool isBookmarked(void) const;
  bool isBookmarkWorthy(void) const;
  bool hasSecureConnection(void) const;
  QUrl url(void) const;
  QUrl webviewUrl(void) const;
  void back(void);
  void load(const QUrl &url);
  void post(const QUrl &url, const QString &text);
  void stop(void);
  void print(QPrinter *printer);
  void reload(void);
  void update(void);
  void forward(void);
  void goToItem(const QWebHistoryItem &item);
  void setFocus(void);
  void setTabAction(QAction *action);
  void zoomTextOnly(const bool state);
  void findIpAddress(const QUrl &url);
  void setZoomFactor(const qreal factor);
  void initializeFtpBrowser(void);
  void initializeFileManager(void);
  void removeRestorationFiles(void);
  void recordRestorationHistory(void);
  qreal zoomFactor(void) const;
  QIcon icon(void) const;
  QIcon webviewIcon(void) const;
  QAction *tabAction(void) const;
  QString html(void);
  QString title(void) const;
  QString ipAddress(void) const;
  QString description(void) const;
  QString statusMessage(void) const;
  dwebpage *page(void) const;
  QWebFrame *currentFrame(void);
  QByteArray history(void);
  QList<QWebHistoryItem> backItems(const int n) const;
  QList<QWebHistoryItem> forwardItems(const int n) const;

 private:
  int m_percentLoaded;
  int m_lastInfoLookupId;
  bool m_pageLoaded;
  QUrl m_url;
  QUrl selectedUrl;
  QUrl selectedImageUrl;
  QString m_title;
  QString m_ipAddress;
  QString m_historyRestorationFileName;
  dwebview *webView;
  QByteArray m_history;
  dftpbrowser *ftpBrowser;
  dfilemanager *fileManager;
#ifdef DOOBLE_USE_QGRAPHICSWEBVIEW
  QGraphicsView *m_view;
  QGraphicsScene *m_scene;
#endif
  QPointer<QAction> m_action;
  int tabIndex(void);
  int downloadPrompt(const QString &fileName);
  void enterEvent(QEvent *event);
#ifdef DOOBLE_USE_QGRAPHICSWEBVIEW
  void resizeEvent(QResizeEvent *event);
#endif
  void prepareRestorationFileNames(void);
  qint64 parentId(void);

 private slots:
  void slotStop(void);
  void slotPaste(void);
  void slotFinished(dnetworkdirreply *reply);
  void slotFinished(dnetworkftpreply *reply);
  void slotFinished(dnetworkerrorreply *reply);
  void slotLoadPage(const QUrl &url);
  void slotSaveLink(void);
  void slotSaveLink(const QUrl &url);
  void slotSaveImage(void);
  void slotSslErrors(QNetworkReply *reply, const QList<QSslError> &errors);
  void slotViewImage(void);
  void slotUrlChanged(const QUrl &url);
  void slotIconChanged(void);
  void slotLinkClicked(const QUrl &url);
  void slotLoadStarted(void);
  void slotClearHistory(void);
  void slotHostLookedUp(const QHostInfo &hostInfo);
  void slotLoadFinished(bool ok);
  void slotLoadProgress(int progress);
  void slotTitleChanged(const QString &title);
  void slotLoadErrorPage(const QUrl &url);
  void slotViewPageSource(void);
  void slotCopyLinkLocation(void);
  void slotCopyLinkLocation(const QUrl &url);
  void slotCopySelectedText(void);
  void slotOpenLinkInNewTab(void);
  void slotOpenLinkInNewTab(const QUrl &url);
  void slotSelectionChanged(void);
  void slotCopyImageLocation(void);
  void slotOpenImageInNewTab(void);
  void slotPrintCurrentFrame(void);
  void slotOpenLinkInNewWindow(void);
  void slotOpenLinkInNewWindow(const QUrl &url);
  void slotOpenImageInNewWindow(void);
  void slotSetTextSizeMultiplier(const qreal multiplier);
  void slotInitialLayoutCompleted(void);
  void slotReencodeRestorationFile(void);
  void slotHandleUnsupportedContent(const QUrl &url);
  void slotHandleUnsupportedContent(QNetworkReply *reply);
  void slotCustomContextMenuRequested(const QPoint &pos);

 signals:
  void goBack(void);
  void saveUrl(const QUrl &url, const int choice);
  void copyLink(const QUrl &url);
  void goReload(void);
  void saveFile(const QString &fileName, const QUrl &url, const int choice);
  void goForward(void);
  void viewImage(const QUrl &url);
  void urlChanged(const QUrl &url);
  void iconChanged(void);
  void loadStarted(void);
  void viewEntered(void);
  void loadFinished(bool);
  void loadProgress(const int);
  void titleChanged(const QString &title);
  void printRequested(QWebFrame *frame);
  void viewPageSource(void);
  void ipAddressChanged(const QString &ipAddress);
  void openLinkInNewTab(const QUrl &url);
  void selectionChanged(const QString &text);
  void openLinkInNewWindow(const QUrl &url);
};

#endif
