24
#include "PreCompiled.h"
27
# include <QApplication>
30
# include <QMessageBox>
34
#include <App/Application.h>
35
#include <Base/Console.h>
37
#include "NetworkRetriever.h"
39
#include "BitmapFactory.h"
40
#include "FileDialog.h"
41
#include "MainWindow.h"
42
#include "ui_DlgAuthorization.h"
46
using namespace Gui::Dialog;
50
struct NetworkRetrieverP
77
NetworkRetriever::NetworkRetriever( QObject * parent )
80
d = new NetworkRetrieverP;
91
wget = new QProcess(this);
94
connect(wget, qOverload<int, QProcess::ExitStatus>(&QProcess::finished),
95
this, &NetworkRetriever::wgetFinished);
98
connect(qApp, &QApplication::lastWindowClosed, wget, &QProcess::kill);
101
NetworkRetriever::~NetworkRetriever()
116
void NetworkRetriever::testFailure()
118
if ( wget->state() == QProcess::Running )
121
QString msg = tr("Download started...");
122
Base::Console().Message("%s\n", msg.toUtf8().constData());
130
void NetworkRetriever::setNumberOfTries( int tries )
138
void NetworkRetriever::setOutputFile( const QString& out )
148
void NetworkRetriever::setEnableTimestamp(bool ts)
158
void NetworkRetriever::setProxy( const QString& proxy, const QString& user, const QString& passwd )
172
void NetworkRetriever::setEnableRecursive( bool recursive, int level )
174
d->recurse = recursive;
182
void NetworkRetriever::setFollowRelative( bool folRel )
191
void NetworkRetriever::setEnableConvert( bool convert )
193
d->convert = convert;
200
void NetworkRetriever::setFetchImages( bool img )
209
void NetworkRetriever::setEnableHTMLExtension( bool html )
217
void NetworkRetriever::setNoParent( bool nop )
225
void NetworkRetriever::setOutputDirectory( const QString& dir )
233
bool NetworkRetriever::startDownload( const QString& startUrl )
238
d->startUrl = startUrl;
241
if ( !d->proxy.isEmpty() )
243
QStringList env = wget->environment();
244
env << QString::fromLatin1("http_proxy=%1").arg(d->proxy);
245
env << QString::fromLatin1("ftp_proxy=%1").arg(d->proxy);
246
wget->setEnvironment(env);
250
QStringList env = wget->environment();
251
env.removeAll(QString::fromLatin1("http_proxy=%1").arg(d->proxy));
252
env.removeAll(QString::fromLatin1("ftp_proxy=%1").arg(d->proxy));
253
wget->setEnvironment(env);
256
QStringList wgetArguments;
261
if ( !d->dir.isEmpty() )
264
if (!dir.exists(d->dir))
266
if (!dir.mkdir(d->dir))
268
Base::Console().Error("Directory '%s' could not be created.", (const char*)d->dir.toLatin1());
273
wget->setWorkingDirectory( dir.path() );
277
if ( !d->proxy.isEmpty() )
279
if ( !d->user.isEmpty() )
281
wgetArguments << QString::fromLatin1("--proxy-user=%1").arg( d->user );
282
if ( !d->passwd.isEmpty() )
284
wgetArguments << QString::fromLatin1("--proxy-passwd=%1").arg( d->passwd );
290
if ( !d->outputFile.isEmpty() )
291
wgetArguments << QString::fromLatin1("--output-document=%1").arg( d->outputFile );
294
wgetArguments << QString::fromLatin1("-N");
297
wgetArguments << QString::fromLatin1("-p");
300
wgetArguments<< QString::fromLatin1("-L");
303
wgetArguments << QString::fromLatin1("-r");
304
wgetArguments << QString::fromLatin1("--level=%1").arg( d->level );
308
wgetArguments << QString::fromLatin1("-np");
312
wgetArguments << QString::fromLatin1("-k");
314
wgetArguments << QString::fromLatin1("--tries=%1").arg( d->tries );
317
wgetArguments << QString::fromLatin1("-E");
320
wgetArguments << startUrl;
324
QString cwd = QDir::currentPath ();
325
if ( !d->dir.isEmpty() )
327
QDir::setCurrent(d->dir);
330
wget->start(QString::fromLatin1("wget"), wgetArguments);
331
QDir::setCurrent( cwd );
333
wget->start(QString::fromLatin1("wget"), wgetArguments);
336
return wget->state() == QProcess::Running;
342
bool NetworkRetriever::isDownloading() const
344
return wget->state() == QProcess::Running;
350
void NetworkRetriever::abort()
352
if ( wget->state() == QProcess::Running)
354
QTimer::singleShot( 2000, wget, &QProcess::kill);
358
void NetworkRetriever::wgetFinished(int exitCode, QProcess::ExitStatus status)
362
wget->setReadChannel(QProcess::StandardError);
363
if (wget->canReadLine()) {
364
QByteArray data = wget->readAll();
365
Base::Console().Warning(data);
374
bool NetworkRetriever::testWget()
377
proc.setProgram(QString::fromLatin1("wget"));
379
bool ok = proc.state() == QProcess::Running;
381
proc.waitForFinished();
387
StdCmdDownloadOnlineHelp::StdCmdDownloadOnlineHelp( QObject * parent)
388
: QObject(parent), Command("Std_DownloadOnlineHelp")
391
sMenuText = QT_TR_NOOP("Download online help");
392
sToolTipText = QT_TR_NOOP("Download %1's online help");
393
sWhatsThis = "Std_DownloadOnlineHelp";
394
sStatusTip = QT_TR_NOOP("Download %1's online help");
397
wget = new NetworkRetriever( this );
399
wget->setEnableRecursive( true, 5 );
400
wget->setNumberOfTries( 3 );
401
wget->setEnableHTMLExtension( true );
402
wget->setEnableConvert( true );
404
wget->setEnableTimestamp( true );
405
wget->setFetchImages( true );
406
wget->setFollowRelative( false );
407
wget->setNoParent( true );
409
connect(wget, &NetworkRetriever::wgetExited, this, &StdCmdDownloadOnlineHelp::wgetFinished);
412
StdCmdDownloadOnlineHelp::~StdCmdDownloadOnlineHelp()
417
Action * StdCmdDownloadOnlineHelp::createAction()
421
QString exe = QString::fromStdString(App::Application::getExecutableName());
422
pcAction = new Action(this,getMainWindow());
423
pcAction->setText(QCoreApplication::translate(
424
this->className(), getMenuText()));
425
pcAction->setToolTip(QCoreApplication::translate(
426
this->className(), getToolTipText()).arg(exe));
427
pcAction->setStatusTip(QCoreApplication::translate(
428
this->className(), getStatusTip()).arg(exe));
429
pcAction->setWhatsThis(QCoreApplication::translate(
430
this->className(), getWhatsThis()).arg(exe));
431
pcAction->setIcon(Gui::BitmapFactory().pixmap(getPixmap()));
432
pcAction->setShortcut(QString::fromLatin1(getAccel()));
437
void StdCmdDownloadOnlineHelp::languageChange()
440
QString exe = QString::fromStdString(App::Application::getExecutableName());
441
_pcAction->setText(QCoreApplication::translate(
442
this->className(), getMenuText()));
443
_pcAction->setToolTip(QCoreApplication::translate(
444
this->className(), getToolTipText()).arg(exe));
445
_pcAction->setStatusTip(QCoreApplication::translate(
446
this->className(), getStatusTip()).arg(exe));
447
_pcAction->setWhatsThis(QCoreApplication::translate(
448
this->className(), getWhatsThis()).arg(exe));
452
void StdCmdDownloadOnlineHelp::activated(int iMsg)
455
if (!wget->isDownloading()) {
456
ParameterGrp::handle hGrp = App::GetApplication().GetUserParameter().GetGroup("BaseApp");
457
hGrp = hGrp->GetGroup("Preferences")->GetGroup("OnlineHelp");
458
std::string url = hGrp->GetASCII("DownloadURL", "www.freecad.org/wiki/");
459
std::string prx = hGrp->GetASCII("ProxyText", "");
460
bool bUseProxy = hGrp->GetBool ("UseProxy", false);
461
bool bAuthor = hGrp->GetBool ("Authorize", false);
468
QDialog dlg(getMainWindow());
470
Ui_DlgAuthorization ui;
473
if (dlg.exec() == QDialog::Accepted) {
474
username = ui.username->text();
475
password = ui.password->text();
479
wget->setProxy(QString::fromLatin1(prx.c_str()), username, password);
483
bool canStart = false;
486
QString path = QString::fromStdString(App::Application::getHomePath());
487
path += QString::fromLatin1("/doc/");
488
ParameterGrp::handle hURLGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/OnlineHelp");
489
path = QString::fromUtf8(hURLGrp->GetASCII( "DownloadLocation", path.toLatin1() ).c_str());
495
if (QMessageBox::critical(getMainWindow(), tr("Non-existing directory"),
496
tr("The directory '%1' does not exist.\n\n"
497
"Do you want to specify an existing directory?").arg(fi.filePath()),
498
QMessageBox::Yes | QMessageBox::No) !=
506
path = FileDialog::getExistingDirectory();
507
if ( path.isEmpty() )
512
if (!fi.permission( QFile::WriteUser)) {
513
if (QMessageBox::critical(getMainWindow(), tr("Missing permission"),
514
tr("You don't have write permission to '%1'\n\n"
515
"Do you want to specify another directory?").arg(fi.filePath()),
516
QMessageBox::Yes | QMessageBox::No) !=
523
path = FileDialog::getExistingDirectory();
524
if ( path.isEmpty() )
529
wget->setOutputDirectory( path );
536
bool ok = wget->startDownload(QString::fromLatin1(url.c_str()));
538
Base::Console().Error("The tool 'wget' couldn't be found. Please check your installation.");
539
else if ( wget->isDownloading() && _pcAction )
540
_pcAction->setText(tr("Stop downloading"));
549
void StdCmdDownloadOnlineHelp::wgetFinished()
552
_pcAction->setText(QCoreApplication::translate(
553
this->className(), getMenuText()));
556
#include "moc_NetworkRetriever.cpp"