Class: HttpWindow

Inherits:
Qt::Dialog show all
Defined in:
ext/ruby/qtruby/examples/network/http/httpwindow.rb

Instance Method Summary collapse

Methods inherited from Qt::Dialog

#exec

Methods inherited from Qt::Base

#%, #&, #*, #**, #+, #-, #-@, #/, #<, #<<, #<=, #==, #>, #>=, #>>, #QCOMPARE, #QEXPECT_FAIL, #QFAIL, #QSKIP, #QTEST, #QVERIFY, #QVERIFY2, #QWARN, #^, ancestors, #is_a?, #methods, private_slots, #protected_methods, #public_methods, q_classinfo, q_signal, q_slot, signals, #singleton_methods, slots, #|, #~

Constructor Details

#initialize(parent = nil) ⇒ HttpWindow

Returns a new instance of HttpWindow.



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
# File 'ext/ruby/qtruby/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

Instance Method Details

#cancelDownloadObject



121
122
123
124
125
126
# File 'ext/ruby/qtruby/examples/network/http/httpwindow.rb', line 121

def cancelDownload()
    @statusLabel.text = tr("Download canceled.")
    @httpRequestAborted = true
    @http.abort()
    @downloadButton.enabled = true
end

#downloadFileObject



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
# File 'ext/ruby/qtruby/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.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

#enableDownloadButtonObject



184
185
186
# File 'ext/ruby/qtruby/examples/network/http/httpwindow.rb', line 184

def enableDownloadButton()
    @downloadButton.enabled = !@urlLineEdit.text.empty?
end

#httpRequestFinished(requestId, error) ⇒ Object



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
# File 'ext/ruby/qtruby/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) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
# File 'ext/ruby/qtruby/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) ⇒ Object



175
176
177
178
179
180
181
182
# File 'ext/ruby/qtruby/examples/network/http/httpwindow.rb', line 175

def updateDataReadProgress(bytesRead, totalBytes)
    if @httpRequestAborted
        return
	end

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