1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
/***************************************************************************
* Copyright (C) 2013~2013 by CSSlayer *
* wengxt@gmail.com *
* *
* 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., *
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#include <QMessageBox>
#include <QIcon>
#include <QTextCodec>
#include <QTemporaryFile>
#include <QUrl>
#include <QUrlQuery>
#include <QWebEngineView>
#include <QDebug>
#include "guicommon.h"
#include "filedownloader.h"
#include "scelconverter.h"
#include "browserdialog.h"
#include "ui_browserdialog.h"
/*
* a typical link looks like this.
* 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
*/
class WebPage : public QWebEnginePage {
public:
WebPage(BrowserDialog *dialog) : QWebEnginePage(dialog), m_dialog(dialog) {
}
protected:
bool acceptNavigationRequest(const QUrl &url, NavigationType type, bool isMainFrame) override {
return m_dialog->linkClicked(url);
}
private:
BrowserDialog *m_dialog;
};
BrowserDialog::BrowserDialog(QWidget* parent): QDialog(parent)
,m_ui(new Ui::BrowserDialog), m_page(new WebPage(this))
{
m_ui->setupUi(this);
m_ui->webView->setPage(m_page);
m_ui->listWidget->hide();
setWindowIcon(QIcon::fromTheme("internet-web-browser"));
setWindowTitle(_("Browse Sogou Cell Dict repository"));
connect(m_ui->webView, &QWebEngineView::loadProgress, m_ui->progressBar, &QProgressBar::setValue);
connect(m_ui->webView, &QWebEngineView::loadStarted, m_ui->progressBar, &QProgressBar::show);
connect(m_ui->webView, &QWebEngineView::loadFinished, m_ui->progressBar, &QProgressBar::hide);
m_ui->webView->load(QUrl(URL_BASE));
}
BrowserDialog::~BrowserDialog()
{
delete m_ui;
}
QString BrowserDialog::decodeName(const QByteArray& in)
{
QTextCodec* codec = QTextCodec::codecForName("UTF-8");
if (!codec) {
return QString();
}
QByteArray out = QByteArray::fromPercentEncoding(in);
return codec->toUnicode(out);
}
bool BrowserDialog::linkClicked(const QUrl& url)
{
do {
if (url.host() != DOWNLOAD_HOST_BASE) {
break;
}
if (url.path() != "/dict/download_cell.php") {
break;
}
QUrlQuery query(url);
QString id = query.queryItemValue("id");
QByteArray name = query.queryItemValue("name", QUrl::FullyEncoded).toLatin1();
QString sname = decodeName(name);
m_name = sname;
if (!id.isEmpty() && !sname.isEmpty()) {
download(url);
return false;
}
} while(0);
if (url.host() != HOST_BASE) {
QMessageBox::information(this, _("Wrong Link"),
_("No browsing outside pinyin.sogou.com, now redirect to home page."));
m_ui->webView->load(QUrl(URL_BASE));
return false;
} else {
return true;
}
}
void BrowserDialog::download(const QUrl& url)
{
m_ui->webView->stop();
m_ui->webView->hide();
m_ui->progressBar->hide();
m_ui->listWidget->show();
FileDownloader* downloader = new FileDownloader(this);
connect(downloader, SIGNAL(message(QMessageBox::Icon,QString)), SLOT(showMessage(QMessageBox::Icon,QString)));
connect(downloader, SIGNAL(finished(bool)), SLOT(downloadFinished(bool)));
connect(downloader, SIGNAL(finished(bool)), downloader, SLOT(deleteLater()));
downloader->download(url);
}
void BrowserDialog::showMessage(QMessageBox::Icon msgLevel, const QString& message)
{
QString iconName;
switch(msgLevel) {
case QMessageBox::Warning:
iconName = "dialog-warning";
break;
case QMessageBox::Critical:
iconName = "dialog-error";
break;
case QMessageBox::Information:
iconName = "dialog-information";
break;
default:
break;
}
QListWidgetItem* item = new QListWidgetItem(QIcon::fromTheme(iconName), message, m_ui->listWidget);
m_ui->listWidget->addItem(item);
}
void BrowserDialog::downloadFinished(bool succ)
{
FileDownloader* downloader = qobject_cast< FileDownloader* >(sender());
if (!succ) {
return;
}
QString fileName = downloader->fileName();
ScelConverter* converter = new ScelConverter(this);
connect(converter, SIGNAL(message(QMessageBox::Icon,QString)), SLOT(showMessage(QMessageBox::Icon,QString)));
connect(converter, SIGNAL(finished(bool)), SLOT(convertFinished(bool)));
connect(converter, SIGNAL(finished(bool)), converter, SLOT(deleteLater()));
converter->convert(fileName, m_name.append(".txt"));
}
void BrowserDialog::convertFinished(bool succ)
{
if (succ) {
accept();
}
}
|