class HttpWindow

Public Class Methods

new(parent = nil) click to toggle source
Calls superclass method
# File examples/network/http/httpwindow.rb, line 36
def initialize(parent = nil)
    super(parent)
    @urlLineEdit = Qt::LineEdit.new("http://www.ietf.org/iesg/1rfc_index.txt")

    @urlLabel = Qt::Label.new(tr("&URL:"))
    @urlLabel.buddy = @urlLineEdit
    @statusLabel = Qt::Label.new(tr("Please enter the URL of a file you want to " \
                                "download."))

    @quitButton = Qt::PushButton.new(tr("Quit"))
    @downloadButton = Qt::PushButton.new(tr("Download"))
    @downloadButton.default = true

    @progressDialog = Qt::ProgressDialog.new(self)

    @http = Qt::Http.new(self)

    connect(@urlLineEdit, SIGNAL('textChanged(const QString &)'),
            self, SLOT(:enableDownloadButton))
    connect(@http, SIGNAL('requestFinished(int, bool)'),
            self, SLOT('httpRequestFinished(int, bool)'))
    connect(@http, SIGNAL('dataReadProgress(int, int)'),
            self, SLOT('updateDataReadProgress(int, int)'))
    connect(@http, SIGNAL('responseHeaderReceived(const QHttpResponseHeader &)'),
            self, SLOT('readResponseHeader(const QHttpResponseHeader &)'))
    connect(@progressDialog, SIGNAL(:canceled), self, SLOT(:cancelDownload))
    connect(@downloadButton, SIGNAL(:clicked), self, SLOT(:downloadFile))
    connect(@quitButton, SIGNAL(:clicked), self, SLOT(:close))

    topLayout = Qt::HBoxLayout.new do |t|
       t.addWidget(@urlLabel)
       t.addWidget(@urlLineEdit)
        end

    buttonLayout = Qt::HBoxLayout.new do |b|
       b.addStretch(1)
       b.addWidget(@downloadButton)
       b.addWidget(@quitButton)
        end

    self.layout = Qt::VBoxLayout.new do |m|
       m.addLayout(topLayout)
       m.addWidget(@statusLabel)
       m.addLayout(buttonLayout)
    end

    self.windowTitle = tr("HTTP")
    @urlLineEdit.setFocus()
end

Public Instance Methods

cancelDownload() click to toggle source
# File examples/network/http/httpwindow.rb, line 121
def cancelDownload()
    @statusLabel.text = tr("Download canceled.")
    @httpRequestAborted = true
    @http.abort()
    @downloadButton.enabled = true
end
downloadFile() click to toggle source
# File examples/network/http/httpwindow.rb, line 86
def downloadFile()
    url = Qt::Url.new(@urlLineEdit.text)
    fileInfo = Qt::FileInfo.new(url.path)
    fileName = fileInfo.fileName

    if Qt::File.exists(fileName)
        Qt::MessageBox.information(self, tr("HTTP"),
                                 tr("There already exists a file called %s in " \
                                    "the current directory." % 
                                     fileName))
        return
    end

    @file = Qt::File.new(fileName)
    if !@file.open(Qt::IODevice::WriteOnly)
        Qt::MessageBox.information(self, tr("HTTP"),
                                 tr("Unable to save the file %s: %s." %
                                 [fileName, @file.errorString]))
        @file.dispose
        return
    end

    @http.setHost(url.host, url.port != -1 ? url.port : 80)
    if url.userName and !url.userName.empty?
        @http.user = url.userName(url.password)
        end

    @httpRequestAborted = false
    @httpGetId = @http.get(url.path(), @file)

    @progressDialog.windowTitle = tr("HTTP")
    @progressDialog.labelText = tr("Downloading %s." % fileName)
    @downloadButton.enabled = false
end
enableDownloadButton() click to toggle source
# File examples/network/http/httpwindow.rb, line 184
def enableDownloadButton()
    @downloadButton.enabled = !@urlLineEdit.text.empty?
end
httpRequestFinished(requestId, error) click to toggle source
# File examples/network/http/httpwindow.rb, line 128
def httpRequestFinished(requestId, error)
    if @httpRequestAborted
        if !@file.nil?
            @file.close
            @file.remove
            @file.dispose
            @file = nil
        end

        @progressDialog.hide
        return
    end

    if requestId != @httpGetId
        return
        end

    @progressDialog.hide
    @file.close

    if error
        @file.remove()
        Qt::MessageBox.information(self, tr("HTTP"),
                                 tr("Download failed: %s." %
                                    @http.errorString))
    else
        fileName = Qt::FileInfo.new(Qt::Url.new(@urlLineEdit.text).path).fileName
        @statusLabel.text = tr("Downloaded %s to current directory." % fileName)
    end

    @downloadButton.enabled = true
    @file.dispose
    @file = nil
end
readResponseHeader(responseHeader) click to toggle source
# File examples/network/http/httpwindow.rb, line 163
def readResponseHeader(responseHeader)
    if responseHeader.statusCode != 200
        Qt::MessageBox.information(self, tr("HTTP"),
                                 tr("Download failed: %s." %
                                    responseHeader.reasonPhrase))
        @httpRequestAborted = true
        @progressDialog.hide
        @http.abort
        return
    end
end
updateDataReadProgress(bytesRead, totalBytes) click to toggle source
# File examples/network/http/httpwindow.rb, line 175
def updateDataReadProgress(bytesRead, totalBytes)
    if @httpRequestAborted
        return
        end

    @progressDialog.maximum = totalBytes
    @progressDialog.value = bytesRead
end