Class: FtpWindow

Inherits:
Qt::Dialog show all
Defined in:
ext/ruby/qtruby/examples/network/ftp/ftpwindow.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) ⇒ FtpWindow

Returns a new instance of FtpWindow.



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
# File 'ext/ruby/qtruby/examples/network/ftp/ftpwindow.rb', line 37

def initialize(parent = nil)
    super(parent)
    @ftp = nil
    @isDirectory = {}
    @ftpServerLabel = Qt::Label.new(tr("Ftp &server:"))
    @ftpServerLineEdit = Qt::LineEdit.new("ftp.trolltech.com")
    @ftpServerLabel.buddy = @ftpServerLineEdit

    @statusLabel = Qt::Label.new(tr("Please enter the name of an FTP server."))

    @fileList = Qt::ListWidget.new
    @fileList.enabled = false

    @connectButton = Qt::PushButton.new(tr("Connect"))
    @connectButton.default = true
    
    @downloadButton = Qt::PushButton.new(tr("Download"))
    @downloadButton.enabled = false

    @cdToParentButton = Qt::PushButton.new
    @cdToParentButton.icon = Qt::Icon.new("images/cdtoparent.png")
    @cdToParentButton.enabled = false

    @quitButton = Qt::PushButton.new(tr("Quit"))

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

    connect(@fileList, SIGNAL('itemDoubleClicked(QListWidgetItem *)'),
            self, SLOT('processItem(QListWidgetItem *)'))
    connect(@fileList, SIGNAL(:itemSelectionChanged),
            self, SLOT(:enableDownloadButton))
    connect(@progressDialog, SIGNAL(:canceled), self, SLOT(:cancelDownload))
    connect(@connectButton, SIGNAL(:clicked), self, SLOT(:connectOrDisconnect))
    connect(@cdToParentButton, SIGNAL(:clicked), self, SLOT(:cdToParent))
    connect(@downloadButton, SIGNAL(:clicked), self, SLOT(:downloadFile))
    connect(@quitButton, SIGNAL(:clicked), self, SLOT(:close))

    topLayout = Qt::HBoxLayout.new do |t|
        t.addWidget(@ftpServerLabel)
        t.addWidget(@ftpServerLineEdit)
        t.addWidget(@cdToParentButton)
        t.addWidget(@connectButton)
    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(@fileList)
        m.addWidget(@statusLabel)
        m.addLayout(buttonLayout)
    end

    setWindowTitle(tr("FTP"))
end

Instance Method Details

#addToList(urlInfo) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'ext/ruby/qtruby/examples/network/ftp/ftpwindow.rb', line 198

def addToList(urlInfo)
    item = Qt::ListWidgetItem.new
    item.text = urlInfo.name()
    pixmap = Qt::Pixmap.new(urlInfo.dir? ? "images/dir.png" : "images/file.png")
    item.icon = pixmap

    @isDirectory[urlInfo.name()] = urlInfo.isDir()
    @fileList.addItem(item)
    if @fileList.currentItem.nil?
        @fileList.currentItem = @fileList.item(0)
        @fileList.enabled = true
    end
end

#cancelDownloadObject



153
154
155
# File 'ext/ruby/qtruby/examples/network/ftp/ftpwindow.rb', line 153

def cancelDownload()
    @ftp.abort()
end

#cdToParentObject



226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'ext/ruby/qtruby/examples/network/ftp/ftpwindow.rb', line 226

def cdToParent()
    Qt::Application.overrideCursor = Qt::WaitCursor
    @fileList.clear()
    @isDirectory.clear()
    @currentPath = @currentPath.slice(@currentPath.rindex('/'), @currentPath.length)
    if @currentPath.empty?
        @cdToParentButton.enabled = false
        @ftp.cd("/")
    else
        @ftp.cd(@currentPath)
    end
    @ftp.list()
end

#connectOrDisconnectObject



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
# File 'ext/ruby/qtruby/examples/network/ftp/ftpwindow.rb', line 97

def connectOrDisconnect()
    if !@ftp.nil?
        @ftp.abort
        @ftp.deleteLater
        @fileList.enabled = false
        @downloadButton.enabled = false
        @connectButton.text = tr("Connect")
        return
    end
    
    Qt::Application.overrideCursor = Qt::Cursor.new(Qt::WaitCursor)
    
    @ftp = Qt::Ftp.new(self)
    connect(@ftp, SIGNAL('commandFinished(int, bool)'),
            self, SLOT('ftpCommandFinished(int, bool)'))
    connect(@ftp, SIGNAL('listInfo(const QUrlInfo &)'),
            self, SLOT('addToList(const QUrlInfo &)'))
    connect(@ftp, SIGNAL('dataTransferProgress(qint64, qint64)'),
            self, SLOT('updateDataTransferProgress(qint64, qint64)'))

    @ftp.connectToHost(@ftpServerLineEdit.text())
    @ftp.()
    @ftp.list()

    @fileList.enabled = true
    @connectButton.text = tr("Disconnect")
    @statusLabel.text = tr("Connecting to FTP server %s..." % @ftpServerLineEdit.text)
end

#downloadFileObject



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
# File 'ext/ruby/qtruby/examples/network/ftp/ftpwindow.rb', line 126

def downloadFile()
    @fileName = @fileList.currentItem().text()

    if Qt::File.exists(@fileName)
        Qt::MessageBox.information(self, tr("FTP"),
                                 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("FTP"),
                                 tr("Unable to save the @file %s: %s." %
                                 [@fileName, file.errorString] ) )
        @file.dispose
        return
    end

    @ftp.get(@fileList.currentItem.text, file)

    @progressDialog.labelText = tr("Downloading %s..." % @fileName)
    @progressDialog.show()
    @downloadButton.enabled = false
end

#enableDownloadButtonObject



245
246
247
248
249
250
251
252
253
# File 'ext/ruby/qtruby/examples/network/ftp/ftpwindow.rb', line 245

def enableDownloadButton()
    current = @fileList.currentItem()
    if !current.nil?
        currentFile = current.text()
        @downloadButton.enabled = !@isDirectory[currentFile]
    else
        @downloadButton.enabled = false
    end
end

#ftpCommandFinished(count, error) ⇒ Object



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
# File 'ext/ruby/qtruby/examples/network/ftp/ftpwindow.rb', line 157

def ftpCommandFinished(count, error)
    if @ftp.currentCommand == Qt::Ftp::ConnectToHost
        if error
            Qt::Application.restoreOverrideCursor
            Qt::MessageBox.information(self, tr("FTP"),
                                     tr("Unable to connect to the FTP server " \
                                        "at %s. Please check that the host " \
                                        "name is correct." %
                                     @ftpServerLineEdit.text) )
            return
        end

        @statusLabel.text = tr("Logged onto %s." % @ftpServerLineEdit.text)
        @fileList.setFocus()
        @downloadButton.default = true
        return
    end

    if @ftp.currentCommand == Qt::Ftp::Get
        Qt::Application.restoreOverrideCursor
        if error
            @statusLabel.text = tr("Canceled download of %s." %
                                 @file.fileName)
            @file.close
            @file.remove
        else
            @statusLabel.text = tr("Downloaded %s to current directory." %
                                 @file.fileName)
            @file.close
        end
        @file.dispose
        enableDownloadButton()
    elsif @ftp.currentCommand == Qt::Ftp::List
        Qt::Application.restoreOverrideCursor
        if @isDirectory.empty?
            @fileList.addItem(tr("<empty>"))
            @fileList.enabled = false
        end
    end
end

#processItem(item) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'ext/ruby/qtruby/examples/network/ftp/ftpwindow.rb', line 212

def processItem(item)
    name = item.text
    if @isDirectory[name]
        @fileList.clear()
        @isDirectory.clear()
        @currentPath += "/" + name
        @ftp.cd(name)
        @ftp.list
        @cdToParentButton.enabled = true
        Qt::Application.overrideCursor = Qt::WaitCursor
        return
    end
end

#updateDataTransferProgress(readBytes, totalBytes) ⇒ Object



240
241
242
243
# File 'ext/ruby/qtruby/examples/network/ftp/ftpwindow.rb', line 240

def updateDataTransferProgress(readBytes, totalBytes)
    @progressDialog.maximum = totalBytes
    @progressDialog.value = readBytes
end