| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #ifndef TEXTBROWSERVIEWER_H |
| 4 | #define TEXTBROWSERVIEWER_H |
| 5 | |
| 6 | #include "gui/webviewers/webviewer.h" |
| 7 | #include "network-web/adblock/adblockmanager.h" |
| 8 | |
| 9 | #include <QNetworkReply> |
| 10 | #include <QPixmap> |
| 11 | #include <QPointer> |
| 12 | #include <QTextBrowser> |
| 13 | #include <QTimer> |
| 14 | |
| 15 | class QContextMenuEvent; |
| 16 | class QResizeEvent; |
| 17 | class WebBrowser; |
| 18 | class Downloader; |
| 19 | |
| 20 | class TextBrowserViewer; |
| 21 | |
| 22 | class RSSGUARD_DLLSPEC TextBrowserDocument : public QTextDocument { |
| 23 | Q_OBJECT |
| 24 | |
| 25 | public: |
| 26 | explicit TextBrowserDocument(TextBrowserViewer* parent = nullptr); |
| 27 | |
| 28 | protected: |
| 29 | virtual QVariant loadResource(int type, const QUrl& name); |
| 30 | |
| 31 | private: |
| 32 | QPointer<TextBrowserViewer> m_viewer; |
| 33 | }; |
| 34 | |
| 35 | class RSSGUARD_DLLSPEC TextBrowserViewer : public QTextBrowser, public WebViewer { |
| 36 | Q_OBJECT |
| 37 | Q_INTERFACES(WebViewer) |
| 38 | |
| 39 | public: |
| 40 | explicit TextBrowserViewer(QWidget* parent = nullptr); |
| 41 | virtual ~TextBrowserViewer(); |
| 42 | |
| 43 | QVariant loadOneResource(int type, const QUrl& name); |
| 44 | |
| 45 | virtual QSize sizeHint() const; |
| 46 | virtual void bindToBrowser(WebBrowser* browser); |
| 47 | virtual void findText(const QString& text, bool backwards); |
| 48 | virtual void setUrl(const QUrl& url); |
| 49 | virtual void setHtml(const QString& html, const QUrl& base_url = {}); |
| 50 | virtual void setReadabledHtml(const QString& html, const QUrl& base_url = {}); |
| 51 | virtual QString html() const; |
| 52 | virtual QUrl url() const; |
| 53 | virtual void clear(); |
| 54 | virtual void loadMessages(const QList<Message>& messages, RootItem* root); |
| 55 | virtual PreparedHtml htmlForMessages(const QList<Message>& messages, RootItem* root) const; |
| 56 | virtual double verticalScrollBarPosition() const; |
| 57 | virtual void setVerticalScrollBarPosition(double pos); |
| 58 | virtual void applyFont(const QFont& fon); |
| 59 | virtual qreal zoomFactor() const; |
| 60 | virtual void setZoomFactor(qreal zoom_factor); |
| 61 | |
| 62 | bool resourcesEnabled() const; |
| 63 | void setResourcesEnabled(bool enabled); |
| 64 | |
| 65 | protected: |
| 66 | virtual ContextMenuData provideContextMenuData(QContextMenuEvent* event) const; |
| 67 | |
| 68 | virtual void contextMenuEvent(QContextMenuEvent* event); |
| 69 | virtual void resizeEvent(QResizeEvent* event); |
| 70 | virtual void wheelEvent(QWheelEvent* event); |
| 71 | |
| 72 | private slots: |
| 73 | void enableResources(bool enable); |
| 74 | void downloadLink(); |
| 75 | void onAnchorClicked(const QUrl& url); |
| 76 | void reloadHtmlDelayed(); |
| 77 | void downloadNextNeededResource(); |
| 78 | void resourceDownloaded(const QUrl& url, |
| 79 | QNetworkReply::NetworkError status, |
| 80 | int http_code, |
| 81 | const QByteArray& contents = QByteArray()); |
| 82 | |
| 83 | signals: |
| 84 | void reloadDocument(); |
| 85 | void pageTitleChanged(const QString& new_title); |
| 86 | void pageUrlChanged(const QUrl& url); |
| 87 | void pageIconChanged(const QIcon&); |
| 88 | void linkMouseHighlighted(const QUrl& url); |
| 89 | void loadingStarted(); |
| 90 | void loadingProgress(int progress); |
| 91 | void loadingFinished(bool success); |
| 92 | void newWindowRequested(WebViewer* viewer); |
| 93 | void closeWindowRequested(); |
| 94 | |
| 95 | private: |
| 96 | PreparedHtml prepareLegacyHtmlForMessage(const QList<Message>& messages, RootItem* selected_item) const; |
| 97 | |
| 98 | void setHtmlPrivate(const QString& html, const QUrl& base_url); |
| 99 | BlockingResult blockedWithAdblock(const QUrl& url); |
| 100 | |
| 101 | QString decodeHtmlData(const QByteArray& data, const QString& content_type) const; |
| 102 | |
| 103 | private: |
| 104 | QScopedPointer<Downloader> m_downloader; |
| 105 | bool m_resourcesEnabled; |
| 106 | QList<QUrl> m_neededResources; // All URLs here must be resolved. |
| 107 | Downloader* m_resourceDownloader; |
| 108 | QThread* m_resourceDownloaderThread; |
| 109 | |
| 110 | // Contains list of precisely sized images per each url. |
| 111 | QMap<QUrl, QMap<int, QByteArray>> m_loadedResources; // All URLs here must be resolved. |
| 112 | QPixmap m_placeholderImage; |
| 113 | QPixmap m_placeholderImageError; |
| 114 | QUrl m_currentUrl; |
| 115 | QString m_currentHtml; |
| 116 | |
| 117 | QPointer<RootItem> m_root; |
| 118 | QFont m_baseFont; |
| 119 | qreal m_zoomFactor = 1.0; |
| 120 | QScopedPointer<QAction> m_actionEnableResources; |
| 121 | QScopedPointer<QAction> m_actionDownloadLink; |
| 122 | QScopedPointer<TextBrowserDocument> m_document; |
| 123 | QPoint m_lastContextMenuPos; |
| 124 | }; |
| 125 | |
| 126 | #endif // TEXTBROWSERVIEWER_H |