| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 2013-2018 CSSlayer <[email protected]> |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "browserdialog.h" |
| 9 |
#include "guicommon.h" |
| 10 |
#include <QDebug> |
| 11 |
#include <QDialog> |
| 12 |
#include <QIcon> |
| 13 |
#include <QMessageBox> |
| 14 |
#include <QProgressBar> |
| 15 |
#include <QTemporaryFile> |
| 16 |
#include <QUrl> |
| 17 |
#include <QUrlQuery> |
| 18 |
#include <QWidget> |
| 19 |
#include <fcitx-utils/i18n.h> |
| 20 |
|
| 21 |
namespace fcitx { |
| 22 |
|
| 23 |
using WebPageBase = QWebEnginePage; |
| 24 |
using WebViewType = QWebEngineView; |
| 25 |
|
| 26 |
/* |
| 27 |
* a typical link looks like this. |
| 28 |
* http://download.pinyin.sogou.com/dict/download_cell.php?id=15207&name=%D6%B2%CE%EF%B4%CA%BB%E3%B4%F3%C8%AB%A1%BE%B9%D9%B7%BD%CD%C6%BC%F6%A1%BF |
| 29 |
*/ |
| 30 |
|
| 31 |
class WebPage : public WebPageBase { |
| 32 |
public: |
| 33 |
WebPage(BrowserDialog *dialog) : WebPageBase(dialog), dialog_(dialog) {} |
| 34 |
|
| 35 |
protected: |
| 36 |
bool acceptNavigationRequest(const QUrl &url, NavigationType /*type*/, |
| 37 |
bool /*isMainFrame*/) override { |
| 38 |
return dialog_->linkClicked(url); |
| 39 |
} |
| 40 |
|
| 41 |
WebPageBase *createWindow(WebPageBase::WebWindowType /*type*/) override { |
| 42 |
return this; |
| 43 |
} |
| 44 |
|
| 45 |
private: |
| 46 |
BrowserDialog *dialog_; |
| 47 |
}; |
| 48 |
|
| 49 |
BrowserDialog::BrowserDialog(QWidget *parent) |
| 50 |
: QDialog(parent), page_(new WebPage(this)) { |
| 51 |
setupUi(this); |
| 52 |
webView_->setPage(page_); |
| 53 |
setWindowIcon(QIcon::fromTheme("internet-web-browser")); |
| 54 |
setWindowTitle(_("Browse Sogou Cell Dict repository")); |
| 55 |
|
| 56 |
connect(webView_, &WebViewType::loadProgress, progressBar_, |
| 57 |
&QProgressBar::setValue); |
| 58 |
connect(webView_, &WebViewType::loadStarted, progressBar_, |
| 59 |
&QProgressBar::show); |
| 60 |
connect(webView_, &WebViewType::loadFinished, progressBar_, |
| 61 |
&QProgressBar::hide); |
| 62 |
webView_->load(QUrl(URL_BASE)); |
| 63 |
} |
| 64 |
|
| 65 |
BrowserDialog::~BrowserDialog() {} |
| 66 |
|
| 67 |
QString BrowserDialog::decodeName(const QByteArray &in) { |
| 68 |
QByteArray out = QByteArray::fromPercentEncoding(in); |
| 69 |
return QString::fromUtf8(out); |
| 70 |
} |
| 71 |
|
| 72 |
bool BrowserDialog::linkClicked(const QUrl &url) { |
| 73 |
do { |
| 74 |
if (url.host() != DOWNLOAD_HOST_BASE && url.host() != HOST_BASE) { |
| 75 |
break; |
| 76 |
} |
| 77 |
|
| 78 |
// Now the site seems to have "d/dict/download_cell.php", just to make |
| 79 |
// it works with both. |
| 80 |
if (!url.path().endsWith("/dict/download_cell.php")) { |
| 81 |
break; |
| 82 |
} |
| 83 |
QUrlQuery query(url); |
| 84 |
QString id = query.queryItemValue("id"); |
| 85 |
QByteArray name = |
| 86 |
query.queryItemValue("name", QUrl::FullyEncoded).toLatin1(); |
| 87 |
QString sname = decodeName(name); |
| 88 |
|
| 89 |
name_ = sname; |
| 90 |
url_ = url; |
| 91 |
|
| 92 |
if (!id.isEmpty() && !sname.isEmpty()) { |
| 93 |
accept(); |
| 94 |
return false; |
| 95 |
} |
| 96 |
} while (0); |
| 97 |
|
| 98 |
if (url.host() != HOST_BASE) { |
| 99 |
QMessageBox::information(this, _("Wrong Link"), |
| 100 |
_("No browsing outside pinyin.sogou.com, now " |
| 101 |
"redirect to home page.")); |
| 102 |
webView_->load(QUrl(URL_BASE)); |
| 103 |
return false; |
| 104 |
} |
| 105 |
return true; |
| 106 |
} |
| 107 |
|
| 108 |
} // namespace fcitx |