File: httpwindow.cpp

package info (click to toggle)
gpsshogi 0.7.0-3.3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 111,280 kB
  • sloc: cpp: 80,962; perl: 12,610; ruby: 3,929; javascript: 1,631; makefile: 1,202; sh: 473; tcl: 166; ansic: 67
file content (149 lines) | stat: -rw-r--r-- 4,304 bytes parent folder | download | duplicates (3)
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
#include "httpwindow.h"
#if QT_VERSION >= 0x050000
#  include <QtWidgets>
#else
#  include <QtGui>
#endif
#include <QtNetwork>
#include <QNetworkAccessManager>

QString HttpWindow::lastUrl;

HttpWindow::HttpWindow(QWidget *parent, bool reload)
    : QDialog(parent)
{
    setModal(true);
    progressDialog = new QProgressDialog(this);
    progressDialog->setMinimumDuration(1000); // msec

    urlLineEdit = new QLineEdit(lastUrl);

    urlLabel = new QLabel(tr("&URL:"));
    urlLabel->setBuddy(urlLineEdit);
    statusLabel = new QLabel(tr("Please enter the URL of a csa file"));

    quitButton = new QPushButton(tr("Cancel"));
    downloadButton = new QPushButton(tr("Open"));
    downloadButton->setDefault(true);

    http = new QNetworkAccessManager(this);
    connect(http, SIGNAL(finished(QNetworkReply*)),
                  this, SLOT(httpRequestFinished(QNetworkReply*)));
    connect(urlLineEdit, SIGNAL(textChanged(const QString &)),
            this, SLOT(enableDownloadButton()));
    connect(downloadButton, SIGNAL(clicked()), this, SLOT(downloadFile()));
    connect(quitButton, SIGNAL(clicked()), this, SLOT(reject()));

    QHBoxLayout *topLayout = new QHBoxLayout;
    topLayout->addWidget(urlLabel);
    topLayout->addWidget(urlLineEdit);

    QHBoxLayout *buttonLayout = new QHBoxLayout;
    buttonLayout->addStretch(1);
    buttonLayout->addWidget(downloadButton);
    buttonLayout->addWidget(quitButton);

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addLayout(topLayout);
    mainLayout->addWidget(statusLabel);
    mainLayout->addLayout(buttonLayout);
    setLayout(mainLayout);

    setWindowTitle(tr("HTTP"));
    const QClipboard *clipboard = QApplication::clipboard();
    const QString text = clipboard->text();
    if (!text.isEmpty())
    {
      urlLineEdit->setText(text);
    }
    urlLineEdit->setFocus();
    if (reload)
      downloadFile();
}

void HttpWindow::downloadFile()
{
    httpRequestAborted = false;
    const QUrl url(urlLineEdit->text());
    lastUrl = url.toString();
    QFileInfo fileInfo(url.path());
    fileName = fileInfo.fileName();

    file = new QFile(fileName);
    if (!file->open(QIODevice::WriteOnly)) {
        QMessageBox::information(this, tr("HTTP"),
                                 tr("Unable to save the file %1: %2.")
                                 .arg(fileName).arg(file->errorString()));
        delete file;
        file = 0;
        return;
    }

    progressDialog->setWindowTitle(tr("HTTP"));
    progressDialog->setLabelText(tr("Downloading %1.").arg(fileName));
    progressDialog->setWindowModality(Qt::WindowModal);
    downloadButton->setEnabled(false);

    QNetworkRequest request(url);
    reply = http->get(request);

    connect(reply, SIGNAL(downloadProgress (qint64, qint64)),
            this, SLOT(updateDataReadProgress(qint64, qint64)));
    connect(progressDialog, SIGNAL(canceled()), this, SLOT(cancelDownload()));
}

void HttpWindow::cancelDownload()
{
    statusLabel->setText(tr("Download canceled."));
    httpRequestAborted = true;
    reply->abort();
    downloadButton->setEnabled(true);
    reject();
}

void HttpWindow::httpRequestFinished(QNetworkReply *reply)
{
    if (httpRequestAborted || 
        reply->error() != QNetworkReply::NoError) {
        if (file) {
            file->close();
            file->remove();
            delete file;
            file = 0;
        }

        QMessageBox::information(this, tr("HTTP"),
                                 tr("Download failed: %1.")
                                 .arg(reply->error()));
        reject();
    } else {
      QTextStream out(file);
      out << reply->readAll();

      file->close();
      delete file;
      file = 0;

      QString fileName = QFileInfo(QUrl(urlLineEdit->text()).path()).fileName();
      statusLabel->setText(tr("Downloaded %1 to current directory.").arg(fileName));
      downloadButton->setEnabled(true);

      accept();
    }

    reply->deleteLater();
}

void HttpWindow::updateDataReadProgress(qint64 bytesRead, qint64 totalBytes)
{
    if (httpRequestAborted)
      return;

    progressDialog->setMaximum(totalBytes);
    progressDialog->setValue(bytesRead);
}

void HttpWindow::enableDownloadButton()
{
    downloadButton->setEnabled(!urlLineEdit->text().isEmpty());
}