| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "gui/messagebrowser.h" |
| 4 | |
| 5 | #include "gui/messagebox.h" |
| 6 | #include "gui/messagetextbrowser.h" |
| 7 | #include "gui/reusable/searchtextwidget.h" |
| 8 | #include "miscellaneous/application.h" |
| 9 | #include "miscellaneous/settings.h" |
| 10 | #include "network-web/webfactory.h" |
| 11 | #include "services/abstract/serviceroot.h" |
| 12 | |
| 13 | #include <QKeyEvent> |
| 14 | #include <QRegularExpression> |
| 15 | #include <QScrollBar> |
| 16 | #include <QToolTip> |
| 17 | #include <QVBoxLayout> |
| 18 | |
| 19 | MessageBrowser::MessageBrowser(bool should_resize_to_fit, QWidget* parent) |
| 20 | : QWidget(parent), m_txtBrowser(new MessageTextBrowser(this)), m_searchWidget(new SearchTextWidget(this)), |
| 21 | m_layout(new QVBoxLayout(this)) { |
| 22 | m_layout->setContentsMargins(3, 3, 3, 3); |
| 23 | m_layout->addWidget(m_txtBrowser, 1); |
| 24 | m_layout->addWidget(m_searchWidget); |
| 25 | |
| 26 | if (should_resize_to_fit) { |
| 27 | m_txtBrowser->setSizePolicy(QSizePolicy::Policy::Preferred, QSizePolicy::Policy::MinimumExpanding); |
| 28 | } |
| 29 | |
| 30 | connect(m_searchWidget, &SearchTextWidget::searchCancelled, this, [this]() { |
| 31 | m_txtBrowser->textCursor().clearSelection(); |
| 32 | m_txtBrowser->moveCursor(QTextCursor::MoveOperation::Left); |
| 33 | }); |
| 34 | connect(m_searchWidget, &SearchTextWidget::searchForText, this, [this](const QString& text, bool backwards) { |
| 35 | if (backwards) { |
| 36 | m_txtBrowser->find(text, QTextDocument::FindFlag::FindBackward); |
| 37 | } |
| 38 | else { |
| 39 | m_txtBrowser->find(text); |
| 40 | } |
| 41 | }); |
| 42 | |
| 43 | connect(m_txtBrowser, &QTextBrowser::anchorClicked, this, &MessageBrowser::onAnchorClicked); |
| 44 | connect(m_txtBrowser, QOverload<const QUrl&>::of(&QTextBrowser::highlighted), [=](const QUrl& url) { |
| 45 | Q_UNUSED(url) |
| 46 | QToolTip::showText(QCursor::pos(), tr("Click this link to download it or open it with external browser."), this); |
| 47 | }); |
| 48 | |
| 49 | m_searchWidget->hide(); |
| 50 | installEventFilter(this); |
| 51 | |
| 52 | reloadFontSettings(); |
| 53 | } |
| 54 | |
| 55 | void MessageBrowser::clear(bool also_hide) { |
| 56 | m_txtBrowser->clear(); |
| 57 | m_pictures.clear(); |
| 58 | m_searchWidget->hide(); |
| 59 | |
| 60 | if (also_hide) { |
| 61 | hide(); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | QString MessageBrowser::prepareHtmlForMessage(const Message& message) { |
| 66 | QString html = QString("<h2 align=\"center\">%1</h2>").arg(message.m_title); |
| 67 | |
| 68 | if (!message.m_url.isEmpty()) { |
| 69 | html += QString("[url] <a href=\"%1\">%1</a><br/>").arg(message.m_url); |
| 70 | } |
| 71 | |
| 72 | for (const Enclosure& enc : message.m_enclosures) { |
| 73 | QString enc_url; |
| 74 | |
| 75 | if (!enc.m_url.contains(QRegularExpression(QSL("^(http|ftp|\\/)")))) { |
| 76 | enc_url = QString(INTERNAL_URL_PASSATTACHMENT) + QL1S("/?") + enc.m_url; |
| 77 | } |
| 78 | else { |
| 79 | enc_url = enc.m_url; |
| 80 | } |
| 81 | |
| 82 | html += QString("[%2] <a href=\"%1\">%1</a><br/>").arg(enc_url, enc.m_mimeType); |
| 83 | } |
| 84 | |
| 85 | QRegularExpression imgTagRegex("\\<img[^\\>]*src\\s*=\\s*[\"\']([^\"\']*)[\"\'][^\\>]*\\>", |
| 86 | QRegularExpression::PatternOption::CaseInsensitiveOption | |
| 87 | QRegularExpression::PatternOption::InvertedGreedinessOption); |
| 88 | QRegularExpressionMatchIterator i = imgTagRegex.globalMatch(message.m_contents); |
| 89 | QString pictures_html; |
| 90 | |
| 91 | while (i.hasNext()) { |
| 92 | QRegularExpressionMatch match = i.next(); |
| 93 | |
| 94 | m_pictures.append(match.captured(1)); |
| 95 | pictures_html += QString("<br/>[%1] <a href=\"%2\">%2</a>").arg(tr("image"), match.captured(1)); |
| 96 | } |
| 97 | |
| 98 | if (qApp->settings()->value(GROUP(Messages), SETTING(Messages::DisplayImagePlaceholders)).toBool()) { |
| 99 | html += message.m_contents; |
| 100 | } |
| 101 | else { |
| 102 | QString cnts = message.m_contents; |
| 103 | |
| 104 | html += cnts.replace(imgTagRegex, QString()); |
| 105 | } |
| 106 | |
| 107 | html += pictures_html; |
| 108 | |
| 109 | /*html = html |
| 110 | .replace(QSL("\r\n"), QSL("\n")) |
| 111 | .replace(QL1C('\r'), QL1C('\n')) |
| 112 | .remove(QL1C('\n'));*/ |
| 113 | |
| 114 | // TODO: If FgInteresting not defined by the skin |
| 115 | // use current pallette/Highlight color perhaps.E |
| 116 | return QSL("<html>" |
| 117 | "<head><style>" |
| 118 | "a { color: %2; }" |
| 119 | "</style></head>" |
| 120 | "<body>%1</body>" |
| 121 | "</html>") |
| 122 | .arg(html, |
| 123 | qApp->skins()->currentSkin().colorForModel(SkinEnums::PaletteColors::FgInteresting).value<QColor>().name()); |
| 124 | } |
| 125 | |
| 126 | bool MessageBrowser::eventFilter(QObject* watched, QEvent* event) { |
| 127 | Q_UNUSED(watched) |
| 128 | |
| 129 | if (event->type() == QEvent::Type::KeyPress) { |
| 130 | auto* key_event = static_cast<QKeyEvent*>(event); |
| 131 | |
| 132 | if (key_event->matches(QKeySequence::StandardKey::Find)) { |
| 133 | |
| 134 | m_searchWidget->clear(); |
| 135 | m_searchWidget->show(); |
| 136 | m_searchWidget->setFocus(); |
| 137 | return true; |
| 138 | } |
| 139 | else if (key_event->key() == Qt::Key::Key_Escape) { |
| 140 | m_searchWidget->cancelSearch(); |
| 141 | return true; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | void MessageBrowser::onAnchorClicked(const QUrl& url) { |
| 149 | if (url.toString().startsWith(INTERNAL_URL_PASSATTACHMENT) && m_root != nullptr && |
| 150 | m_root->getParentServiceRoot()->downloadAttachmentOnMyOwn(url)) { |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | if (!url.isEmpty()) { |
| 155 | bool open_externally_now = |
| 156 | qApp->settings()->value(GROUP(Browser), SETTING(Browser::OpenLinksInExternalBrowserRightAway)).toBool(); |
| 157 | |
| 158 | if (open_externally_now) { |
| 159 | qApp->web()->openUrlInExternalBrowser(url.toString()); |
| 160 | } |
| 161 | else { |
| 162 | // User clicked some URL. Open it in external browser or download? |
| 163 | MsgBox box(qApp->mainFormWidget()); |
| 164 | |
| 165 | box.setText(tr("You clicked some link. You can download the link contents or open it in external web browser.")); |
| 166 | box.setInformativeText(tr("What action do you want to take?")); |
| 167 | box.setDetailedText(url.toString()); |
| 168 | |
| 169 | QAbstractButton* btn_open = box.addButton(tr("Open in external browser"), QMessageBox::ButtonRole::ActionRole); |
| 170 | QAbstractButton* btn_download = box.addButton(tr("Download"), QMessageBox::ButtonRole::ActionRole); |
| 171 | QAbstractButton* btn_cancel = box.addButton(QMessageBox::StandardButton::Cancel); |
| 172 | bool always; |
| 173 | |
| 174 | MsgBox::setCheckBox(&box, tr("Always open links in external browser."), &always); |
| 175 | |
| 176 | box.setDefaultButton(QMessageBox::StandardButton::Cancel); |
| 177 | box.exec(); |
| 178 | |
| 179 | if (box.clickedButton() != box.button(QMessageBox::StandardButton::Cancel)) { |
| 180 | // Store selected checkbox value. |
| 181 | qApp->settings()->setValue(GROUP(Browser), Browser::OpenLinksInExternalBrowserRightAway, always); |
| 182 | } |
| 183 | |
| 184 | if (box.clickedButton() == btn_open) { |
| 185 | qApp->web()->openUrlInExternalBrowser(url.toString()); |
| 186 | } |
| 187 | else if (box.clickedButton() == btn_download) { |
| 188 | qApp->downloadManager()->download(url); |
| 189 | } |
| 190 | |
| 191 | btn_download->deleteLater(); |
| 192 | btn_open->deleteLater(); |
| 193 | btn_cancel->deleteLater(); |
| 194 | } |
| 195 | } |
| 196 | else { |
| 197 | MsgBox::show(qApp->mainFormWidget(), |
| 198 | QMessageBox::Warning, |
| 199 | tr("Incorrect link"), |
| 200 | tr("Selected hyperlink is invalid.")); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | void MessageBrowser::reloadFontSettings() { |
| 205 | const Settings* settings = qApp->settings(); |
| 206 | QFont fon; |
| 207 | |
| 208 | fon.fromString(settings->value(GROUP(Messages), SETTING(Messages::PreviewerFontStandard)).toString()); |
| 209 | m_txtBrowser->setFont(fon); |
| 210 | } |
| 211 | |
| 212 | void MessageBrowser::loadUrl(const QString& url) { |
| 213 | onAnchorClicked(url); |
| 214 | } |
| 215 | |
| 216 | void MessageBrowser::loadMessage(const Message& message, RootItem* root) { |
| 217 | Q_UNUSED(root) |
| 218 | |
| 219 | auto html = prepareHtmlForMessage(message); |
| 220 | |
| 221 | m_txtBrowser->setHtml(html); |
| 222 | m_txtBrowser->verticalScrollBar()->triggerAction(QScrollBar::SliderAction::SliderToMinimum); |
| 223 | m_searchWidget->hide(); |
| 224 | } |
| 225 | |
| 226 | double MessageBrowser::verticalScrollBarPosition() const { |
| 227 | return m_txtBrowser->verticalScrollBar()->value(); |
| 228 | } |
| 229 | |
| 230 | void MessageBrowser::setVerticalScrollBarPosition(double pos) { |
| 231 | m_txtBrowser->verticalScrollBar()->setValue(pos); |
| 232 | } |