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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
package HttpWindow;
use strict;
use warnings;
use QtCore4;
use QtGui4;
use QtNetwork4;
use QtCore4::isa qw( Qt::Dialog );
use QtCore4::slots
downloadFile => [],
cancelDownload => [],
httpRequestFinished => ['int', 'bool'],
readResponseHeader => ['const QHttpResponseHeader &'],
updateDataReadProgress => ['int', 'int',],
enableDownloadButton => [],
slotAuthenticationRequired => ['const QString &', 'quint16', 'QAuthenticator *'],
sslErrors => ['const QList<QSslError> &'];
use Ui_Dialog;
sub statusLabel() {
return this->{statusLabel};
}
sub urlLabel() {
return this->{urlLabel};
}
sub urlLineEdit() {
return this->{urlLineEdit};
}
sub progressDialog() {
return this->{progressDialog};
}
sub downloadButton() {
return this->{downloadButton};
}
sub quitButton() {
return this->{quitButton};
}
sub buttonBox() {
return this->{buttonBox};
}
sub http() {
return this->{http};
}
sub file() {
return this->{file};
}
sub httpGetId() {
return this->{httpGetId};
}
sub httpRequestAborted() {
return this->{httpRequestAborted};
}
sub NEW
{
my ($class, $parent) = @_;
$class->SUPER::NEW($parent);
this->{urlLineEdit} = Qt::LineEdit('https://');
this->{urlLabel} = Qt::Label(this->tr('&URL:'));
urlLabel->setBuddy(urlLineEdit);
this->{statusLabel} = Qt::Label(this->tr('Please enter the URL of a file you want to ' .
'download.'));
this->{downloadButton} = Qt::PushButton(this->tr('Download'));
downloadButton->setDefault(1);
this->{quitButton} = Qt::PushButton(this->tr('Quit'));
quitButton->setAutoDefault(0);
this->{buttonBox} = Qt::DialogButtonBox();
buttonBox->addButton(downloadButton, Qt::DialogButtonBox::ActionRole());
buttonBox->addButton(quitButton, Qt::DialogButtonBox::RejectRole());
this->{progressDialog} = Qt::ProgressDialog(this);
this->{http} = Qt::Http(this);
this->connect(urlLineEdit, SIGNAL 'textChanged(QString)',
this, SLOT 'enableDownloadButton()');
this->connect(http, SIGNAL 'requestFinished(int,bool)',
this, SLOT 'httpRequestFinished(int,bool)');
this->connect(http, SIGNAL 'dataReadProgress(int,int)',
this, SLOT 'updateDataReadProgress(int,int)');
this->connect(http, SIGNAL 'responseHeaderReceived(QHttpResponseHeader)',
this, SLOT 'readResponseHeader(QHttpResponseHeader)');
this->connect(http, SIGNAL 'authenticationRequired(QString,quint16,QAuthenticator*)',
this, SLOT 'slotAuthenticationRequired(QString,quint16,QAuthenticator*)');
this->connect(http, SIGNAL 'sslErrors(QList<QSslError>)',
this, SLOT 'sslErrors(QList<QSslError>)');
this->connect(progressDialog, SIGNAL 'canceled()', this, SLOT 'cancelDownload()');
this->connect(downloadButton, SIGNAL 'clicked()', this, SLOT 'downloadFile()');
this->connect(quitButton, SIGNAL 'clicked()', this, SLOT 'close()');
my $topLayout = Qt::HBoxLayout();
$topLayout->addWidget(urlLabel);
$topLayout->addWidget(urlLineEdit);
my $mainLayout = Qt::VBoxLayout();
$mainLayout->addLayout($topLayout);
$mainLayout->addWidget(statusLabel);
$mainLayout->addWidget(buttonBox);
this->setLayout($mainLayout);
setWindowTitle(this->tr('HTTP'));
urlLineEdit->setFocus();
}
sub downloadFile
{
my $url = Qt::Url(urlLineEdit->text());
my $fileInfo = Qt::FileInfo($url->path());
my $fileName = $fileInfo->fileName();
if (!defined $fileName) {
$fileName = 'index.html';
}
if (Qt::File::exists($fileName)) {
if (Qt::MessageBox::question(this, this->tr('HTTP'),
sprintf( this->tr('There already exists a file called %s in ' .
'the current directory. Overwrite?'), $fileName ),
Qt::MessageBox::Yes()|Qt::MessageBox::No(), Qt::MessageBox::No())
== Qt::MessageBox::No()) {
return;
}
Qt::File::remove($fileName);
}
this->{file} = Qt::File($fileName);
if (!file->open(Qt::IODevice::WriteOnly())) {
Qt::MessageBox::information(this, this->tr('HTTP'),
sprintf this->tr('Unable to save the file %s: %s.'),
$fileName, file->errorString());
this->{file} = undef;
return;
}
my $mode = lc($url->scheme()) eq 'https' ? Qt::Http::ConnectionModeHttps() : Qt::Http::ConnectionModeHttp();
http->setHost($url->host(), $mode, $url->port() == -1 ? 0 : $url->port());
if (!defined $url->userName()) {
http->setUser($url->userName(), $url->password());
}
this->{httpRequestAborted} = 0;
my $path = Qt::Url::toPercentEncoding($url->path(), Qt::ByteArray('!$&\'()*+,;=:@/'));
if ($path->isEmpty()) {
$path->append('/');
}
this->{httpGetId} = http->get($path->constData(), file);
progressDialog->setWindowTitle(this->tr('HTTP'));
progressDialog->setLabelText(sprintf this->tr('Downloading %s.'), $fileName);
downloadButton->setEnabled(0);
}
sub cancelDownload
{
statusLabel->setText(this->tr('Download canceled.'));
this->{httpRequestAborted} = 1;
http->abort();
downloadButton->setEnabled(1);
}
sub httpRequestFinished
{
my ($requestId, $error) = @_;
if ($requestId != httpGetId) {
return;
}
if (httpRequestAborted) {
if (file) {
file->close();
file->remove();
this->{file} = undef;
}
progressDialog->hide();
return;
}
if ($requestId != httpGetId) {
return;
}
progressDialog->hide();
file->close();
if ($error) {
file->remove();
Qt::MessageBox::information(this, this->tr('HTTP'),
sprintf this->tr('Download failed: %s.'),
http->errorString());
} else {
my $fileName = Qt::FileInfo(Qt::Url(urlLineEdit->text())->path())->fileName();
statusLabel->setText(sprintf this->tr('Downloaded %s to current directory.'), $fileName);
}
downloadButton->setEnabled(1);
this->{file} = undef;
}
sub readResponseHeader
{
my ($responseHeader) = @_;
my $code = $responseHeader->statusCode();
if (
$code == 200 || # Ok
$code == 301 || # Moved Permanently
$code == 302 || # Found
$code == 303 || # See Other
$code == 307 ) { # Temporary Redirect
# these are not error conditions
}
else {
Qt::MessageBox::information(this, this->tr('HTTP'),
sprintf this->tr('Download failed: %s.'),
$responseHeader->reasonPhrase());
this->{httpRequestAborted} = 1;
progressDialog->hide();
http->abort();
}
}
sub updateDataReadProgress
{
my ($bytesRead, $totalBytes) = @_;
if (httpRequestAborted) {
return;
}
progressDialog->setMaximum($totalBytes);
progressDialog->setValue($bytesRead);
}
sub enableDownloadButton
{
downloadButton->setEnabled(urlLineEdit->text());
}
sub slotAuthenticationRequired
{
my ($hostName, $foo, $authenticator) = @_;
my $dlg = Qt::Dialog();
my $ui = Ui_Dialog->setupUi($dlg);
$ui->setupUi($dlg);
$dlg->adjustSize();
$ui->siteDescription->setText(sprintf this->tr('%s at %s'), $authenticator->realm(), $hostName);
if ($dlg->exec() == Qt::Dialog::Accepted()) {
$authenticator->setUser($ui->userEdit->text());
$authenticator->setPassword($ui->passwordEdit->text());
}
}
sub sslErrors
{
my ($errors) = @_;
my $errorString;
foreach my $error ( @{$errors} ) {
if (defined $errorString) {
$errorString .= ', ';
}
$errorString .= $error->errorString();
}
if (Qt::MessageBox::warning(this, this->tr('HTTP Example'),
sprintf( this->tr('One or more SSL errors has occurred: %s'), $errorString),
Qt::MessageBox::Ignore() | Qt::MessageBox::Abort()) == Qt::MessageBox::Ignore()) {
http->ignoreSslErrors();
}
}
1;
|