+ fix download dialog
git-svn-id: https://free-cad.svn.sourceforge.net/svnroot/free-cad/trunk@5200 e8eeb9e2-ec13-0410-a4a9-efa5cf37419d
This commit is contained in:
parent
d427cfa664
commit
11fd74ad2b
|
@ -30,139 +30,177 @@
|
|||
using namespace Gui::Dialog;
|
||||
|
||||
|
||||
|
||||
DownloadDialog::DownloadDialog( QUrl download_url, QString s, QString p )
|
||||
DownloadDialog::DownloadDialog(const QUrl& url, QWidget *parent)
|
||||
: QDialog(parent), url(url)
|
||||
{
|
||||
this->setAttribute(Qt::WA_DeleteOnClose);
|
||||
stopped = false;
|
||||
purpose = s;
|
||||
url = download_url;
|
||||
path = p;
|
||||
statusLabel = new QLabel( QLatin1String(""), this );
|
||||
progressDialog = new QProgressDialog(this);
|
||||
statusLabel = new QLabel(url.toString());
|
||||
progressBar = new QProgressBar(this);
|
||||
downloadButton = new QPushButton(tr("Download"));
|
||||
downloadButton->setDefault(true);
|
||||
cancelButton = new QPushButton(tr("Cancel"));
|
||||
closeButton = new QPushButton(tr("Close"));
|
||||
closeButton->setAutoDefault(false);
|
||||
|
||||
|
||||
buttonBox = new QDialogButtonBox;
|
||||
buttonBox->addButton(downloadButton, QDialogButtonBox::ActionRole);
|
||||
buttonBox->addButton(closeButton, QDialogButtonBox::RejectRole);
|
||||
buttonBox->addButton(cancelButton, QDialogButtonBox::RejectRole);
|
||||
cancelButton->hide();
|
||||
|
||||
http = new QHttp(this);
|
||||
buffer = new QBuffer(&ba, this);
|
||||
progressDialog->setLabel(statusLabel);
|
||||
QFileInfo fi( url.toString() );
|
||||
|
||||
if ( true != url.isValid() ||
|
||||
true == url.host().isEmpty() )
|
||||
{
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
statusLabel->setText( fi.fileName() );
|
||||
}
|
||||
connect(http, SIGNAL(requestFinished(int, bool)),
|
||||
this, SLOT(httpRequestFinished(int, bool)));
|
||||
connect(http, SIGNAL(dataReadProgress(int, int)),
|
||||
this, SLOT(updateDataReadProgress(int, int)));
|
||||
connect(http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)),
|
||||
this, SLOT(readResponseHeader(const QHttpResponseHeader &)));
|
||||
connect(http, SIGNAL(authenticationRequired(const QString &, quint16, QAuthenticator *)),
|
||||
this, SLOT(slotAuthenticationRequired(const QString &, quint16, QAuthenticator *)));
|
||||
connect(downloadButton, SIGNAL(clicked()), this, SLOT(downloadFile()));
|
||||
connect(cancelButton, SIGNAL(clicked()), this, SLOT(cancelDownload()));
|
||||
connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
|
||||
|
||||
buffer->open(QBuffer::ReadWrite);
|
||||
QHBoxLayout *topLayout = new QHBoxLayout;
|
||||
topLayout->addWidget(statusLabel);
|
||||
|
||||
if ( url.port() == -1 )
|
||||
{
|
||||
http->setHost( url.host(), 80 );
|
||||
}
|
||||
else
|
||||
{
|
||||
http->setHost( url.host(), url.port() );
|
||||
}
|
||||
|
||||
http_request_id = http->get(url.path(), buffer);
|
||||
|
||||
QObject::connect( http,
|
||||
SIGNAL( requestFinished(int, bool) ),
|
||||
this,
|
||||
SLOT( request_finished(int, bool) ) );
|
||||
QObject::connect( http,
|
||||
SIGNAL( dataReadProgress(int, int)),
|
||||
this,
|
||||
SLOT( update_progress(int, int) ) );
|
||||
QObject::connect( http,
|
||||
SIGNAL( responseHeaderReceived(const QHttpResponseHeader &) ),
|
||||
this,
|
||||
SLOT( read_response_header(const QHttpResponseHeader &) ) );
|
||||
QObject::connect( progressDialog,
|
||||
SIGNAL( canceled() ),
|
||||
this,
|
||||
SLOT( cancel_download() ) );
|
||||
QVBoxLayout *mainLayout = new QVBoxLayout;
|
||||
mainLayout->addLayout(topLayout);
|
||||
mainLayout->addWidget(progressBar);
|
||||
mainLayout->addWidget(buttonBox);
|
||||
setLayout(mainLayout);
|
||||
|
||||
setWindowTitle(tr("Download"));
|
||||
}
|
||||
|
||||
DownloadDialog::~DownloadDialog()
|
||||
{
|
||||
}
|
||||
|
||||
void DownloadDialog::request_finished( int request_id , bool request_error )
|
||||
void DownloadDialog::downloadFile()
|
||||
{
|
||||
if ( true == stopped ){
|
||||
buffer->close();
|
||||
progressDialog->hide();
|
||||
QFileInfo fileInfo(url.path());
|
||||
QString fileName = fileInfo.fileName();
|
||||
if (QFile::exists(fileName)) {
|
||||
if (QMessageBox::question(this, tr("Download"),
|
||||
tr("There already exists a file called %1 in "
|
||||
"the current directory. Overwrite?").arg(fileName),
|
||||
QMessageBox::Yes|QMessageBox::No, QMessageBox::No) == QMessageBox::No)
|
||||
return;
|
||||
QFile::remove(fileName);
|
||||
}
|
||||
|
||||
file = new QFile(fileName);
|
||||
if (!file->open(QIODevice::WriteOnly)) {
|
||||
QMessageBox::information(this, tr("Download"),
|
||||
tr("Unable to save the file %1: %2.")
|
||||
.arg(fileName).arg(file->errorString()));
|
||||
delete file;
|
||||
file = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
if ( http_request_id != request_id ){
|
||||
return;
|
||||
}
|
||||
QHttp::ConnectionMode mode = url.scheme().toLower() == QLatin1String("https")
|
||||
? QHttp::ConnectionModeHttps : QHttp::ConnectionModeHttp;
|
||||
http->setHost(url.host(), mode, url.port() == -1 ? 80 : url.port());
|
||||
|
||||
if (!url.userName().isEmpty())
|
||||
http->setUser(url.userName(), url.password());
|
||||
|
||||
if ( true == request_error ) {
|
||||
stopped = true;
|
||||
buffer->close();
|
||||
progressDialog->hide();
|
||||
download_finished( this, false, purpose, path, http->errorString() );
|
||||
}else{
|
||||
stopped = true;
|
||||
buffer->close();
|
||||
progressDialog->hide();
|
||||
download_finished( this, true, purpose, path, QLatin1String("") );
|
||||
}
|
||||
httpRequestAborted = false;
|
||||
QByteArray path = QUrl::toPercentEncoding(url.path(), "!$&'()*+,;=:@/");
|
||||
if (path.isEmpty())
|
||||
path = "/";
|
||||
httpGetId = http->get(QString::fromAscii(path), file);
|
||||
|
||||
statusLabel->setText(tr("Downloading %1.").arg(fileName));
|
||||
downloadButton->setEnabled(false);
|
||||
cancelButton->show();
|
||||
closeButton->hide();
|
||||
}
|
||||
|
||||
void DownloadDialog::read_response_header( const QHttpResponseHeader & response_header )
|
||||
void DownloadDialog::cancelDownload()
|
||||
{
|
||||
if ( true == stopped ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( response_header.statusCode() != 200 ) {
|
||||
stopped = true;
|
||||
progressDialog->hide();
|
||||
http->abort();
|
||||
download_finished( this, false, purpose, path, response_header.reasonPhrase() );
|
||||
}
|
||||
}
|
||||
|
||||
QByteArray DownloadDialog::return_data()
|
||||
{
|
||||
return ba;
|
||||
}
|
||||
|
||||
void DownloadDialog::update_progress( int read_bytes, int total_bytes )
|
||||
{
|
||||
if ( true == stopped ){
|
||||
return;
|
||||
}
|
||||
|
||||
progressDialog->setMaximum(total_bytes);
|
||||
progressDialog->setValue(read_bytes);
|
||||
}
|
||||
|
||||
void DownloadDialog::cancel_download()
|
||||
{
|
||||
statusLabel->setText(tr("Canceled."));
|
||||
stopped = true;
|
||||
statusLabel->setText(tr("Download canceled."));
|
||||
httpRequestAborted = true;
|
||||
http->abort();
|
||||
buffer->close();
|
||||
close();
|
||||
}
|
||||
|
||||
void DownloadDialog::closeEvent( QCloseEvent * e )
|
||||
void DownloadDialog::httpRequestFinished(int requestId, bool error)
|
||||
{
|
||||
if ( stopped == false ){
|
||||
stopped = true;
|
||||
http->abort();
|
||||
buffer->close();
|
||||
if (requestId != httpGetId)
|
||||
return;
|
||||
if (httpRequestAborted) {
|
||||
if (file) {
|
||||
file->close();
|
||||
file->remove();
|
||||
delete file;
|
||||
file = 0;
|
||||
}
|
||||
|
||||
progressBar->hide();
|
||||
return;
|
||||
}
|
||||
|
||||
e->accept();
|
||||
if (requestId != httpGetId)
|
||||
return;
|
||||
|
||||
progressBar->hide();
|
||||
file->close();
|
||||
|
||||
if (error) {
|
||||
file->remove();
|
||||
QMessageBox::information(this, tr("Download"),
|
||||
tr("Download failed: %1.")
|
||||
.arg(http->errorString()));
|
||||
}
|
||||
else {
|
||||
QString fileName = QFileInfo(url.path()).fileName();
|
||||
statusLabel->setText(tr("Downloaded %1 to current directory.").arg(fileName));
|
||||
}
|
||||
|
||||
downloadButton->setEnabled(true);
|
||||
cancelButton->hide();
|
||||
closeButton->show();
|
||||
delete file;
|
||||
file = 0;
|
||||
}
|
||||
|
||||
#include "moc_DownloadDialog.cpp"
|
||||
void DownloadDialog::readResponseHeader(const QHttpResponseHeader &responseHeader)
|
||||
{
|
||||
switch (responseHeader.statusCode()) {
|
||||
case 200: // Ok
|
||||
case 301: // Moved Permanently
|
||||
case 302: // Found
|
||||
case 303: // See Other
|
||||
case 307: // Temporary Redirect
|
||||
// these are not error conditions
|
||||
break;
|
||||
|
||||
default:
|
||||
QMessageBox::information(this, tr("Download"),
|
||||
tr("Download failed: %1.")
|
||||
.arg(responseHeader.reasonPhrase()));
|
||||
httpRequestAborted = true;
|
||||
progressBar->hide();
|
||||
http->abort();
|
||||
}
|
||||
}
|
||||
|
||||
void DownloadDialog::updateDataReadProgress(int bytesRead, int totalBytes)
|
||||
{
|
||||
if (httpRequestAborted)
|
||||
return;
|
||||
|
||||
progressBar->setMaximum(totalBytes);
|
||||
progressBar->setValue(bytesRead);
|
||||
}
|
||||
|
||||
void DownloadDialog::slotAuthenticationRequired(const QString &hostName, quint16, QAuthenticator *authenticator)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
#include "moc_DownloadDialog.cpp"
|
||||
|
|
|
@ -29,10 +29,12 @@
|
|||
#include <QMessageBox>
|
||||
#include <QBuffer>
|
||||
#include <QLabel>
|
||||
#include <QProgressBar>
|
||||
#include <QProgressDialog>
|
||||
#include <QHttp>
|
||||
#include <QFileInfo>
|
||||
#include <QCloseEvent>
|
||||
#include <QDialogButtonBox>
|
||||
|
||||
namespace Gui {
|
||||
namespace Dialog {
|
||||
|
@ -46,45 +48,30 @@ class GuiExport DownloadDialog : public QDialog
|
|||
Q_OBJECT
|
||||
|
||||
public:
|
||||
|
||||
DownloadDialog( QUrl url, QString s, QString p = QString() );
|
||||
DownloadDialog(const QUrl& url, QWidget *parent = 0);
|
||||
~DownloadDialog();
|
||||
QUrl url;
|
||||
QString purpose;
|
||||
QString path;
|
||||
QByteArray ba;
|
||||
bool stopped;
|
||||
QByteArray return_data();
|
||||
|
||||
public Q_SLOTS:
|
||||
|
||||
void request_finished( int, bool );
|
||||
void cancel_download();
|
||||
void update_progress( int read_bytes, int total_bytes );
|
||||
void read_response_header(const QHttpResponseHeader & response_header);
|
||||
|
||||
Q_SIGNALS:
|
||||
|
||||
void download_finished( DownloadDialog * d,
|
||||
bool ok,
|
||||
QString s,
|
||||
QString p,
|
||||
QString e );
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
QHttp * http;
|
||||
int http_request_id;
|
||||
QBuffer * buffer;
|
||||
void closeEvent( QCloseEvent * e );
|
||||
private Q_SLOTS:
|
||||
void downloadFile();
|
||||
void cancelDownload();
|
||||
void httpRequestFinished(int requestId, bool error);
|
||||
void readResponseHeader(const QHttpResponseHeader &responseHeader);
|
||||
void updateDataReadProgress(int bytesRead, int totalBytes);
|
||||
void slotAuthenticationRequired(const QString &, quint16, QAuthenticator *);
|
||||
|
||||
private:
|
||||
QLabel *statusLabel;
|
||||
QProgressBar *progressBar;
|
||||
QPushButton *downloadButton;
|
||||
QPushButton *closeButton;
|
||||
QPushButton *cancelButton;
|
||||
QDialogButtonBox *buttonBox;
|
||||
|
||||
bool url_is_valid;
|
||||
QProgressDialog * progressDialog;
|
||||
QLabel * statusLabel;
|
||||
|
||||
QUrl url;
|
||||
QHttp *http;
|
||||
QFile *file;
|
||||
int httpGetId;
|
||||
bool httpRequestAborted;
|
||||
};
|
||||
|
||||
} // namespace Dialog
|
||||
|
|
|
@ -30,6 +30,7 @@
|
|||
# include <QDateTime>
|
||||
# include <QHBoxLayout>
|
||||
# include <QMessageBox>
|
||||
# include <QNetworkRequest>
|
||||
# include <QPainter>
|
||||
# include <QPrinter>
|
||||
# include <QPrintDialog>
|
||||
|
@ -78,6 +79,7 @@ BrowserView::BrowserView(QWidget* parent)
|
|||
setCentralWidget(WebView);
|
||||
|
||||
WebView->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
|
||||
WebView->page()->setForwardUnsupportedContent(true);
|
||||
|
||||
connect(WebView, SIGNAL(loadStarted()),
|
||||
this, SLOT(onLoadStarted()));
|
||||
|
@ -87,6 +89,8 @@ BrowserView::BrowserView(QWidget* parent)
|
|||
this, SLOT(onLoadFinished()));
|
||||
connect(WebView, SIGNAL(linkClicked(const QUrl &)),
|
||||
this, SLOT(onLinkClicked(const QUrl &)));
|
||||
connect(WebView->page(), SIGNAL(downloadRequested(const QNetworkRequest &)),
|
||||
this, SLOT(onDownloadRequested(const QNetworkRequest &)));
|
||||
}
|
||||
|
||||
/** Destroys the object and frees any allocated resources */
|
||||
|
@ -107,19 +111,23 @@ void BrowserView::onLinkClicked (const QUrl & url)
|
|||
|
||||
//QString fragment = url. fragment();
|
||||
|
||||
if(scheme==QString::fromLatin1("http")){
|
||||
/* Dialog::DownloadDialog Dlg (url,QString::fromLatin1("c:/temp/test.fcstd"));
|
||||
int result = Dlg.exec();
|
||||
if(ext ==QString::fromLatin1("fcstd") )
|
||||
Gui::Command::doCommand(Gui::Command::Gui,"Gui.open('%s')",);
|
||||
|
||||
load(url);*/
|
||||
OpenURLInBrowser(url.toString().toLatin1());
|
||||
if (scheme==QString::fromLatin1("http")) {
|
||||
bool ok = false;
|
||||
if (ok) {
|
||||
//Dialog::DownloadDialog dlg (url,this/*QString::fromLatin1("c:/temp/test.fcstd")*/);
|
||||
//int result = dlg.exec();
|
||||
//if(ext ==QString::fromLatin1("fcstd") )
|
||||
// Gui::Command::doCommand(Gui::Command::Gui,"Gui.open('c:/temp/test.fcstd')");
|
||||
}
|
||||
else {
|
||||
load(url);
|
||||
}
|
||||
//OpenURLInBrowser(url.toString().toLatin1());
|
||||
}
|
||||
// run scripts if not from somewhere else!
|
||||
if((scheme.size() < 2 || scheme==QString::fromLatin1("file"))&& host.isEmpty()){
|
||||
if ((scheme.size() < 2 || scheme==QString::fromLatin1("file"))&& host.isEmpty()) {
|
||||
QFileInfo fi(path);
|
||||
if(fi.exists()){
|
||||
if (fi.exists()) {
|
||||
QString ext = fi.completeSuffix();
|
||||
if (ext == QString::fromLatin1("py")) {
|
||||
try {
|
||||
|
@ -143,6 +151,11 @@ bool BrowserView::chckHostAllowed(const QString& host)
|
|||
return host.isEmpty();
|
||||
}
|
||||
|
||||
void BrowserView::onDownloadRequested(const QNetworkRequest & request)
|
||||
{
|
||||
Dialog::DownloadDialog dlg (request.url(),this);
|
||||
int result = dlg.exec();
|
||||
}
|
||||
|
||||
void BrowserView::load(const char* URL)
|
||||
{
|
||||
|
@ -157,21 +170,23 @@ void BrowserView::load(const QUrl & url)
|
|||
|
||||
WebView->load(url);
|
||||
WebView->setUrl(url);
|
||||
if(url.scheme().size() < 2){
|
||||
if (url.scheme().size() < 2) {
|
||||
QString path = url.path();
|
||||
QFileInfo fi(path);
|
||||
QString name = fi.baseName();
|
||||
|
||||
setWindowTitle(name);
|
||||
}else
|
||||
}
|
||||
else {
|
||||
setWindowTitle(url.host());
|
||||
}
|
||||
|
||||
setWindowIcon(QWebSettings::iconForUrl(url));
|
||||
}
|
||||
|
||||
void BrowserView::setHtml(const QString& HtmlCode,const QUrl & BaseUrl,const QString& TabName)
|
||||
{
|
||||
if(isLoading)
|
||||
if (isLoading)
|
||||
stop();
|
||||
|
||||
WebView->setHtml(HtmlCode,BaseUrl);
|
||||
|
@ -261,7 +276,7 @@ bool BrowserView::onHasMsg(const char* pMsg) const
|
|||
/** Checking on close state. */
|
||||
bool BrowserView::canClose(void)
|
||||
{
|
||||
return true;
|
||||
return true;
|
||||
}
|
||||
|
||||
#include "moc_BrowserView.cpp"
|
||||
|
|
|
@ -21,8 +21,8 @@
|
|||
***************************************************************************/
|
||||
|
||||
|
||||
#ifndef GUI_BROWSERVIEW_H
|
||||
#define GUI_BROWSERVIEW_H
|
||||
#ifndef WEBGUI_BROWSERVIEW_H
|
||||
#define WEBGUI_BROWSERVIEW_H
|
||||
|
||||
|
||||
#include <Gui/MDIView.h>
|
||||
|
@ -30,6 +30,7 @@
|
|||
|
||||
class QWebView;
|
||||
class QUrl;
|
||||
class QNetworkRequest;
|
||||
|
||||
namespace WebGui {
|
||||
|
||||
|
@ -83,6 +84,7 @@ protected Q_SLOTS:
|
|||
void onLoadFinished();
|
||||
void onLinkClicked ( const QUrl & url ) ;
|
||||
bool chckHostAllowed(const QString& host);
|
||||
void onDownloadRequested(const QNetworkRequest & request);
|
||||
|
||||
private:
|
||||
QWebView* WebView;
|
||||
|
@ -92,4 +94,4 @@ private:
|
|||
|
||||
} // namespace WebGui
|
||||
|
||||
#endif // GUI_EDITORVIEW_H
|
||||
#endif // WEBGUI_BROWSERVIEW_H
|
||||
|
|
|
@ -157,7 +157,7 @@ CmdWebBrowserStop::CmdWebBrowserStop()
|
|||
sAppModule = "Web";
|
||||
sGroup = QT_TR_NOOP("Web");
|
||||
sMenuText = QT_TR_NOOP("Stop loading");
|
||||
sToolTipText = QT_TR_NOOP("Stop the actuall loading");
|
||||
sToolTipText = QT_TR_NOOP("Stop the current loading");
|
||||
sWhatsThis = sToolTipText;
|
||||
sStatusTip = sToolTipText;
|
||||
sPixmap = "actions/web-stop";
|
||||
|
|
Loading…
Reference in New Issue
Block a user