diff options
author | Douglas Soares de Andrade <douglas@archlinux.org> | 2009-08-19 15:10:28 -0300 |
---|---|---|
committer | Douglas Soares de Andrade <douglas@archlinux.org> | 2009-08-19 15:10:28 -0300 |
commit | d4e4c7fdf71ab52083e49ffdea1b7daeff6c8d8d (patch) | |
tree | 2285c0be0a319e7d463948fcf637ae0ed1e1bb15 /examples/network | |
parent | 704bf0e5e6ed9b2b8a3dcbf8c5ad2648d33f4d3f (diff) |
Adding the pyqt ported examples (replacing xmon examples because most of then did not work here)
Diffstat (limited to 'examples/network')
-rw-r--r-- | examples/network/README | 4 | ||||
-rwxr-xr-x | examples/network/blockingfortuneclient.py | 117 | ||||
-rwxr-xr-x | examples/network/broadcastreceiver.py | 40 | ||||
-rwxr-xr-x | examples/network/broadcastsender.py | 46 | ||||
-rwxr-xr-x | examples/network/fortuneclient.py | 142 | ||||
-rwxr-xr-x | examples/network/fortuneserver.py | 56 | ||||
-rwxr-xr-x | examples/network/ftp/ftp.py | 257 | ||||
-rw-r--r-- | examples/network/ftp/ftp_rc.py | 80 | ||||
-rwxr-xr-x | examples/network/http.py | 155 | ||||
-rwxr-xr-x | examples/network/loopback.py | 96 | ||||
-rwxr-xr-x | examples/network/threadedfortuneserver.py | 83 |
11 files changed, 641 insertions, 435 deletions
diff --git a/examples/network/README b/examples/network/README index 7cfd561..3618692 100644 --- a/examples/network/README +++ b/examples/network/README @@ -18,9 +18,9 @@ Finding the PyQt Examples and Demos launcher On Windows: The launcher can be accessed via the Windows Start menu. Select the menu -entry entitled "Examples and Demos" entry in the submenu containing PyQt4. +entry entitled "Examples and Demos" entry in the submenu containing PySide. On all platforms: -The source code for the launcher can be found in the examples/demos/qtdemo +The source code for the launcher can be found in the examples/tools/qtdemo directory in the PyQt package. diff --git a/examples/network/blockingfortuneclient.py b/examples/network/blockingfortuneclient.py index 068980d..cc402dc 100755 --- a/examples/network/blockingfortuneclient.py +++ b/examples/network/blockingfortuneclient.py @@ -23,16 +23,13 @@ # ############################################################################ -from PyQt4 import QtCore, QtGui, QtNetwork +import sys +from PySide import QtCore, QtGui, QtNetwork class FortuneThread(QtCore.QThread): - newFortune = QtCore.pyqtSignal(str) - - error = QtCore.pyqtSignal(int, str) - def __init__(self, parent=None): - super(FortuneThread, self).__init__(parent) + QtCore.QThread.__init__(self, parent) self.quit = False self.hostName = QtCore.QString() @@ -41,10 +38,8 @@ class FortuneThread(QtCore.QThread): self.port = 0 def __del__(self): - self.mutex.lock() self.quit = True self.cond.wakeOne() - self.mutex.unlock() self.wait() def requestNewFortune(self, hostname, port): @@ -69,12 +64,14 @@ class FortuneThread(QtCore.QThread): socket.connectToHost(serverName, serverPort) if not socket.waitForConnected(Timeout): - self.error.emit(socket.error(), socket.errorString()) + self.emit(QtCore.SIGNAL("error(int, const QString &)"), + socket.error(), socket.errorString()) return while socket.bytesAvailable() < 2: if not socket.waitForReadyRead(Timeout): - self.error.emit(socket.error(), socket.errorString()) + self.emit(QtCore.SIGNAL("error(int, const QString &)"), + socket.error(), socket.errorString()) return instr = QtCore.QDataStream(socket) @@ -83,36 +80,39 @@ class FortuneThread(QtCore.QThread): while socket.bytesAvailable() < blockSize: if not socket.waitForReadyRead(Timeout): - self.error.emit(socket.error(), socket.errorString()) + self.emit(QtCore.SIGNAL("error(int, const QString &)"), + socket.error(), socket.errorString()) return - self.mutex.lock() + locker = QtCore.QMutexLocker(self.mutex) + fortune = QtCore.QString() instr >> fortune - self.newFortune.emit(fortune) + self.emit(QtCore.SIGNAL("newFortune(const QString &)"), fortune) self.cond.wait(self.mutex) serverName = self.hostName serverPort = self.port - self.mutex.unlock() + del locker class BlockingClient(QtGui.QDialog): def __init__(self, parent=None): - super(BlockingClient, self).__init__(parent) + QtGui.QDialog.__init__(self, parent) + self._main = QtCore.QThread.currentThread() self.thread = FortuneThread() self.currentFortune = QtCore.QString() - hostLabel = QtGui.QLabel(self.tr("&Server name:")) - portLabel = QtGui.QLabel(self.tr("S&erver port:")) + self.hostLabel = QtGui.QLabel(self.tr("&Server name:")) + self.portLabel = QtGui.QLabel(self.tr("S&erver port:")) self.hostLineEdit = QtGui.QLineEdit("Localhost") self.portLineEdit = QtGui.QLineEdit() self.portLineEdit.setValidator(QtGui.QIntValidator(1, 65535, self)) - hostLabel.setBuddy(self.hostLineEdit) - portLabel.setBuddy(self.portLineEdit) + self.hostLabel.setBuddy(self.hostLineEdit) + self.portLabel.setBuddy(self.portLineEdit) self.statusLabel = QtGui.QLabel(self.tr("This example requires that " "you run the Fortune Server " @@ -122,27 +122,35 @@ class BlockingClient(QtGui.QDialog): self.getFortuneButton.setDefault(True) self.getFortuneButton.setEnabled(False) - quitButton = QtGui.QPushButton(self.tr("Quit")) - - buttonBox = QtGui.QDialogButtonBox() - buttonBox.addButton(self.getFortuneButton, - QtGui.QDialogButtonBox.ActionRole) - buttonBox.addButton(quitButton, QtGui.QDialogButtonBox.RejectRole) - - self.hostLineEdit.textChanged.connect(self.enableGetFortuneButton) - self.portLineEdit.textChanged.connect(self.enableGetFortuneButton) - self.getFortuneButton.clicked.connect(self.requestNewFortune) - quitButton.clicked.connect(self.close) - self.thread.newFortune.connect(self.showFortune) - self.thread.error.connect(self.displayError) + self.quitButton = QtGui.QPushButton(self.tr("Quit")) + + self.connect(self.hostLineEdit, + QtCore.SIGNAL("textChanged(const QString &)"), + self.enableGetFortuneButton) + self.connect(self.portLineEdit, + QtCore.SIGNAL("textChanged(const QString &)"), + self.enableGetFortuneButton) + self.connect(self.getFortuneButton, QtCore.SIGNAL("clicked()"), + self.requestNewFortune) + self.connect(self.quitButton, QtCore.SIGNAL("clicked()"), + self, QtCore.SLOT("close()")) + self.connect(self.thread, QtCore.SIGNAL("newFortune(const QString &)"), + self.showFortune) + self.connect(self.thread, QtCore.SIGNAL("error(int, const QString &)"), + self.displayError) + + buttonLayout = QtGui.QHBoxLayout() + buttonLayout.addStretch(1) + buttonLayout.addWidget(self.getFortuneButton) + buttonLayout.addWidget(self.quitButton) mainLayout = QtGui.QGridLayout() - mainLayout.addWidget(hostLabel, 0, 0) + mainLayout.addWidget(self.hostLabel, 0, 0) mainLayout.addWidget(self.hostLineEdit, 0, 1) - mainLayout.addWidget(portLabel, 1, 0) + mainLayout.addWidget(self.portLabel, 1, 0) mainLayout.addWidget(self.portLineEdit, 1, 1) mainLayout.addWidget(self.statusLabel, 2, 0, 1, 2) - mainLayout.addWidget(buttonBox, 3, 0, 1, 2) + mainLayout.addLayout(buttonLayout, 3, 0, 1, 2) self.setLayout(mainLayout) self.setWindowTitle(self.tr("Blocking Fortune Client")) @@ -151,7 +159,7 @@ class BlockingClient(QtGui.QDialog): def requestNewFortune(self): self.getFortuneButton.setEnabled(False) self.thread.requestNewFortune(self.hostLineEdit.text(), - self.portLineEdit.text().toInt()[0]) + self.portLineEdit.text().toInt()[0]) def showFortune(self, nextFortune): if nextFortune == self.currentFortune: @@ -164,34 +172,35 @@ class BlockingClient(QtGui.QDialog): def displayError(self, socketError, message): if socketError == QtNetwork.QAbstractSocket.HostNotFoundError: - QtGui.QMessageBox.information(self, - self.tr("Blocking Fortune Client"), - self.tr("The host was not found. Please check the host " - "and port settings.")) + QtGui.QMessageBox.information(self, + self.tr("Blocking Fortune Client"), + self.tr("The host was not found. " + "Please check the host and " + "port settings.")) elif socketError == QtNetwork.QAbstractSocket.ConnectionRefusedError: QtGui.QMessageBox.information(self, - self.tr("Blocking Fortune Client"), - self.tr("The connection was refused by the peer. Make " - "sure the fortune server is running, and check " - "that the host name and port settings are " - "correct.")) + self.tr("Blocking Fortune Client"), + self.tr("The connection was refused " + "by the peer. Make sure the " + "fortune server is running, " + "and check that the host " + "name and port settings are " + "correct.")) else: QtGui.QMessageBox.information(self, - self.tr("Blocking Fortune Client"), - self.tr("The following error occurred: %1.").arg(message)) + self.tr("Blocking Fortune Client"), + self.tr("The following error " + "occurred: %1.").arg(message)) self.getFortuneButton.setEnabled(True) def enableGetFortuneButton(self): - self.getFortuneButton.setEnabled( - not self.hostLineEdit.text().isEmpty() and - not self.portLineEdit.text().isEmpty()) - - -if __name__ == '__main__': + self.getFortuneButton.setEnabled( + not self.hostLineEdit.text().isEmpty() and + not self.portLineEdit.text().isEmpty()) - import sys +if __name__ == "__main__": app = QtGui.QApplication(sys.argv) client = BlockingClient() client.show() diff --git a/examples/network/broadcastreceiver.py b/examples/network/broadcastreceiver.py index 169ec6c..6d6f2a2 100755 --- a/examples/network/broadcastreceiver.py +++ b/examples/network/broadcastreceiver.py @@ -23,44 +23,44 @@ # ############################################################################ -from PyQt4 import QtGui, QtNetwork +import sys +from PySide import QtCore, QtGui, QtNetwork class Receiver(QtGui.QDialog): def __init__(self, parent=None): - super(Receiver, self).__init__(parent) - + QtGui.QDialog.__init__(self, parent) + self.statusLabel = QtGui.QLabel(self.tr("Listening for broadcasted messages")) - quitButton = QtGui.QPushButton(self.tr("&Quit")) - + self.quitButton = QtGui.QPushButton(self.tr("&Quit")) + self.udpSocket = QtNetwork.QUdpSocket(self) self.udpSocket.bind(45454) - - self.udpSocket.readyRead.connect(self.processPendingDatagrams) - quitButton.clicked.connect(self.close) - + + self.connect(self.udpSocket, QtCore.SIGNAL("readyRead()"), + self.processPendingDatagrams) + self.connect(self.quitButton, QtCore.SIGNAL("clicked()"), + self, QtCore.SLOT("close()")) + buttonLayout = QtGui.QHBoxLayout() buttonLayout.addStretch(1) - buttonLayout.addWidget(quitButton) - buttonLayout.addStretch(1) - + buttonLayout.addWidget(self.quitButton) + mainLayout = QtGui.QVBoxLayout() mainLayout.addWidget(self.statusLabel) mainLayout.addLayout(buttonLayout) self.setLayout(mainLayout) - + self.setWindowTitle(self.tr("Broadcast Receiver")) - + def processPendingDatagrams(self): while self.udpSocket.hasPendingDatagrams(): datagram, host, port = self.udpSocket.readDatagram(self.udpSocket.pendingDatagramSize()) - self.statusLabel.setText(self.tr("Received datagram: \"%1\"").arg(datagram)) - - -if __name__ == '__main__': - - import sys + self.statusLabel.setText(self.tr("Received datagram: \"%1\"") + .arg(datagram)) + +if __name__ == "__main__": app = QtGui.QApplication(sys.argv) receiver = Receiver() receiver.show() diff --git a/examples/network/broadcastsender.py b/examples/network/broadcastsender.py index 48fb5d3..623d04a 100755 --- a/examples/network/broadcastsender.py +++ b/examples/network/broadcastsender.py @@ -23,41 +23,44 @@ # ############################################################################ -from PyQt4 import QtCore, QtGui, QtNetwork +import sys +from PySide import QtCore, QtGui, QtNetwork class Sender(QtGui.QDialog): def __init__(self, parent=None): - super(Sender, self).__init__(parent) - - self.statusLabel = QtGui.QLabel(self.tr("Ready to broadcast datagrams on port 45454")) - + QtGui.QDialog.__init__(self, parent) + + self.statusLabel = QtGui.QLabel(self.tr("Ready to broadcast datagramms on port 45454")) self.startButton = QtGui.QPushButton(self.tr("&Start")) - quitButton = QtGui.QPushButton(self.tr("&Quit")) - - buttonBox = QtGui.QDialogButtonBox() - buttonBox.addButton(self.startButton, QtGui.QDialogButtonBox.ActionRole) - buttonBox.addButton(quitButton, QtGui.QDialogButtonBox.RejectRole) - + self.quitButton = QtGui.QPushButton(self.tr("&Quit")) self.timer = QtCore.QTimer(self) self.udpSocket = QtNetwork.QUdpSocket(self) self.messageNo = 1 - - self.startButton.clicked.connect(self.startBroadcasting) - quitButton.clicked.connect(self.close) - self.timer.timeout.connect(self.broadcastDatagramm) - + + self.connect(self.startButton, QtCore.SIGNAL("clicked()"), + self.startBroadcasting) + self.connect(self.quitButton, QtCore.SIGNAL("clicked()"), + self, QtCore.SLOT("close()")) + self.connect(self.timer, QtCore.SIGNAL("timeout()"), + self.broadcastDatagramm) + + buttonLayout = QtGui.QHBoxLayout() + buttonLayout.addStretch(1) + buttonLayout.addWidget(self.startButton) + buttonLayout.addWidget(self.quitButton) + mainLayout = QtGui.QVBoxLayout() mainLayout.addWidget(self.statusLabel) - mainLayout.addWidget(buttonBox) + mainLayout.addLayout(buttonLayout) self.setLayout(mainLayout) - + self.setWindowTitle(self.tr("Broadcast Sender")) def startBroadcasting(self): self.startButton.setEnabled(False) self.timer.start(1000) - + def broadcastDatagramm(self): self.statusLabel.setText(self.tr("Now broadcasting datagram %1").arg(self.messageNo)) datagram = "Broadcast message %d" % self.messageNo @@ -65,10 +68,7 @@ class Sender(QtGui.QDialog): self.messageNo += 1 -if __name__ == '__main__': - - import sys - +if __name__ == "__main__": app = QtGui.QApplication(sys.argv) sender = Sender() sender.show() diff --git a/examples/network/fortuneclient.py b/examples/network/fortuneclient.py index 8f293fa..9f7d3f6 100755 --- a/examples/network/fortuneclient.py +++ b/examples/network/fortuneclient.py @@ -1,124 +1,126 @@ #!/usr/bin/env python -"""PyQt4 port of the network/fortuneclient example from Qt v4.x""" +"""PySide port of the network/fortuneclient example from Qt v4.x""" -from PyQt4 import QtCore, QtGui, QtNetwork +import sys +from PySide import QtCore, QtGui, QtNetwork class Client(QtGui.QDialog): def __init__(self, parent=None): - super(Client, self).__init__(parent) - - self.blockSize = 0 - self.currentFortune = QtCore.QString() - - hostLabel = QtGui.QLabel(self.tr("&Server name:")) - portLabel = QtGui.QLabel(self.tr("S&erver port:")) + QtGui.QDialog.__init__(self, parent) + self.hostLabel = QtGui.QLabel(self.tr("&Server name:")) + self.portLabel = QtGui.QLabel(self.tr("S&erver port:")) + self.hostLineEdit = QtGui.QLineEdit("Localhost") self.portLineEdit = QtGui.QLineEdit() self.portLineEdit.setValidator(QtGui.QIntValidator(1, 65535, self)) - - hostLabel.setBuddy(self.hostLineEdit) - portLabel.setBuddy(self.portLineEdit) - - self.statusLabel = QtGui.QLabel(self.tr("This examples requires that " - "you run the Fortune Server " - "example as well.")) - + + self.hostLabel.setBuddy(self.hostLineEdit) + self.portLabel.setBuddy(self.portLineEdit) + + self.statusLabel = QtGui.QLabel(self.tr("This examples requires that you run " + "the Fortune Server example as well.")) + self.getFortuneButton = QtGui.QPushButton(self.tr("Get Fortune")) self.getFortuneButton.setDefault(True) self.getFortuneButton.setEnabled(False) - - quitButton = QtGui.QPushButton(self.tr("Quit")) - - buttonBox = QtGui.QDialogButtonBox() - buttonBox.addButton(self.getFortuneButton, - QtGui.QDialogButtonBox.ActionRole) - buttonBox.addButton(quitButton, QtGui.QDialogButtonBox.RejectRole) - + + self.quitButton = QtGui.QPushButton(self.tr("Quit")) + + self.timerId = -1 + self.blockSize = 0 + self.currentFortune = QtCore.QString() self.tcpSocket = QtNetwork.QTcpSocket(self) - - self.hostLineEdit.textChanged.connect(self.enableGetFortuneButton) - self.portLineEdit.textChanged.connect(self.enableGetFortuneButton) - self.getFortuneButton.clicked.connect(self.requestNewFortune) - quitButton.clicked.connect(self.close) - self.tcpSocket.readyRead.connect(self.readFortune) - self.tcpSocket.error.connect(self.displayError) - + + self.connect(self.hostLineEdit, QtCore.SIGNAL("textChanged(const QString &)"), self.enableGetFortuneButton) + self.connect(self.portLineEdit, QtCore.SIGNAL("textChanged(const QString &)"), self.enableGetFortuneButton) + self.connect(self.getFortuneButton, QtCore.SIGNAL("clicked()"), self.requestNewFortune) + self.connect(self.quitButton, QtCore.SIGNAL("clicked()"), self, QtCore.SLOT("close()")) + self.connect(self.tcpSocket, QtCore.SIGNAL("readyRead()"), self.readFortune) + self.connect(self.tcpSocket, QtCore.SIGNAL("error(QAbstractSocket::SocketError)"), self.displayError) + + buttonLayout = QtGui.QHBoxLayout() + buttonLayout.addStretch(1) + buttonLayout.addWidget(self.getFortuneButton) + buttonLayout.addWidget(self.quitButton) + mainLayout = QtGui.QGridLayout() - mainLayout.addWidget(hostLabel, 0, 0) + mainLayout.addWidget(self.hostLabel, 0, 0) mainLayout.addWidget(self.hostLineEdit, 0, 1) - mainLayout.addWidget(portLabel, 1, 0) + mainLayout.addWidget(self.portLabel, 1, 0) mainLayout.addWidget(self.portLineEdit, 1, 1) mainLayout.addWidget(self.statusLabel, 2, 0, 1, 2) - mainLayout.addWidget(buttonBox, 3, 0, 1, 2) + mainLayout.addLayout(buttonLayout, 3, 0, 1, 2) self.setLayout(mainLayout) - + self.setWindowTitle(self.tr("Fortune Client")) self.portLineEdit.setFocus() - + def requestNewFortune(self): self.getFortuneButton.setEnabled(False) self.blockSize = 0 self.tcpSocket.abort() - self.tcpSocket.connectToHost(self.hostLineEdit.text(), - self.portLineEdit.text().toInt()[0]) - + self.tcpSocket.connectToHost(self.hostLineEdit.text(), self.portLineEdit.text().toInt()[0]) + def readFortune(self): instr = QtCore.QDataStream(self.tcpSocket) instr.setVersion(QtCore.QDataStream.Qt_4_0) - + if self.blockSize == 0: if self.tcpSocket.bytesAvailable() < 2: return - + self.blockSize = instr.readUInt16() - + if self.tcpSocket.bytesAvailable() < self.blockSize: return - + nextFortune = QtCore.QString() instr >> nextFortune - + if nextFortune == self.currentFortune: - QtCore.QTimer.singleShot(0, self.requestNewFortune) + self.timerId = self.startTimer(10) return - + self.currentFortune = nextFortune self.statusLabel.setText(self.currentFortune) self.getFortuneButton.setEnabled(True) - + + def timerEvent(self, event): + if event.timerId() == self.timerId: + self.killTimer(self.timerId) + self.timerId = -1 + + self.requestNewFortune() + def displayError(self, socketError): - if socketError == QtNetwork.QAbstractSocket.RemoteHostClosedError: + if socketError == QtNetwork.QAbstractSocket.RemoteHostClosedError: pass - elif socketError == QtNetwork.QAbstractSocket.HostNotFoundError: - QtGui.QMessageBox.information(self, self.tr("Fortune Client"), - self.tr("The host was not found. Please check the host " - "name and port settings.")) - elif socketError == QtNetwork.QAbstractSocket.ConnectionRefusedError: - QtGui.QMessageBox.information(self, self.tr("Fortune Client"), - self.tr("The connection was refused by the peer. Make " - "sure the fortune server is running, and check " - "that the host name and port settings are " - "correct.")) + elif socketError == QtNetwork.QAbstractSocket.HostNotFoundError: + QtGui.QMessageBox.information(self, self.tr("Fortune Client"), self.tr( + "The host was not found. Please check the " + "host name and port settings.")) + elif socketError == QtNetwork.QAbstractSocket.ConnectionRefusedError: + QtGui.QMessageBox.information(self, self.tr("Fortune Client"), self.tr( + "The connection was refused by the peer. " + "Make sure the fortune server is running,\n" + "and check that the host name and port " + "settings are correct.")) else: QtGui.QMessageBox.information(self, self.tr("Fortune Client"), - self.tr("The following error occurred: %1.").arg(self.tcpSocket.errorString())) - + self.tr("The following error occurred: %1.") + .arg(self.tcpSocket.errorString())) + self.getFortuneButton.setEnabled(True) - + def enableGetFortuneButton(self): - self.getFortuneButton.setEnabled( - not self.hostLineEdit.text().isEmpty() and - not self.portLineEdit.text().isEmpty()) + self.getFortuneButton.setEnabled(not self.hostLineEdit.text().isEmpty() and + not self.portLineEdit.text().isEmpty()) if __name__ == '__main__': - - import sys - app = QtGui.QApplication(sys.argv) client = Client() - client.show() sys.exit(client.exec_()) diff --git a/examples/network/fortuneserver.py b/examples/network/fortuneserver.py index d663fa1..b0a7ee4 100755 --- a/examples/network/fortuneserver.py +++ b/examples/network/fortuneserver.py @@ -1,52 +1,50 @@ #!/usr/bin/env python -"""PyQt4 port of the network/fortuneserver example from Qt v4.x""" +"""PySide port of the network/fortuneserver example from Qt v4.x""" -import random import sys - -sys.path +=['/usr/local/lib/python2.6/site-packages'] - +import random from PySide import QtCore, QtGui, QtNetwork class Server(QtGui.QDialog): def __init__(self, parent=None): - super(Server, self).__init__(parent) + QtGui.QDialog.__init__(self, parent) - statusLabel = QtGui.QLabel() - quitButton = QtGui.QPushButton(self.tr("Quit")) - quitButton.setAutoDefault(False) + self.statusLabel = QtGui.QLabel() + self.quitButton = QtGui.QPushButton(self.tr("Quit")) + self.quitButton.setAutoDefault(False) self.tcpServer = QtNetwork.QTcpServer(self) if not self.tcpServer.listen(): QtGui.QMessageBox.critical(self, self.tr("Fortune Server"), - self.tr("Unable to start the server: %1.").arg(self.tcpServer.errorString())) + self.tr("Unable to start the server: %1.") + .arg(self.tcpServer.errorString())) self.close() return - statusLabel.setText(self.tr("The server is running on port %1.\n" - "Run the Fortune Client example now.").arg(self.tcpServer.serverPort())) + self.statusLabel.setText(self.tr("The server is running on port %1.\n" + "Run the Fortune Client example now.") + .arg(self.tcpServer.serverPort())) - self.fortunes = [ - self.tr("You've been leading a dog's life. Stay off the furniture."), - self.tr("You've got to think about tomorrow."), - self.tr("You will be surprised by a loud noise."), - self.tr("You will feel hungry again in another hour."), - self.tr("You might have mail."), - self.tr("You cannot kill time without injuring eternity."), - self.tr("Computers are not intelligent. They only think they are.")] + self.fortunes = QtCore.QStringList() + (self.fortunes << self.tr("You've been leading a dog's life. Stay off the furniture.") + << self.tr("You've got to think about tomorrow.") + << self.tr("You will be surprised by a loud noise.") + << self.tr("You will feel hungry again in another hour.") + << self.tr("You might have mail.") + << self.tr("You cannot kill time without injuring eternity.") + << self.tr("Computers are not intelligent. They only think they are.")) - quitButton.clicked.connect(self.close) - self.tcpServer.newConnection.connect(self.sendFortune) + self.connect(self.quitButton, QtCore.SIGNAL("clicked()"), self, QtCore.SLOT("close()")) + self.connect(self.tcpServer, QtCore.SIGNAL("newConnection()"), self.sendFortune) buttonLayout = QtGui.QHBoxLayout() buttonLayout.addStretch(1) - buttonLayout.addWidget(quitButton) - buttonLayout.addStretch(1) + buttonLayout.addWidget(self.quitButton) mainLayout = QtGui.QVBoxLayout() - mainLayout.addWidget(statusLabel) + mainLayout.addWidget(self.statusLabel) mainLayout.addLayout(buttonLayout) self.setLayout(mainLayout) @@ -57,21 +55,19 @@ class Server(QtGui.QDialog): out = QtCore.QDataStream(block, QtCore.QIODevice.WriteOnly) out.setVersion(QtCore.QDataStream.Qt_4_0) out.writeUInt16(0) - out << self.fortunes[random.randint(0, len(self.fortunes) - 1)] + out << self.fortunes[random.randint(0, self.fortunes.count() - 1)] out.device().seek(0) out.writeUInt16(block.size() - 2) clientConnection = self.tcpServer.nextPendingConnection() - clientConnection.disconnected.connect(clientConnection.deleteLater) + self.connect(clientConnection, QtCore.SIGNAL("disconnected()"), + clientConnection, QtCore.SLOT("deleteLater()")) clientConnection.write(block) clientConnection.disconnectFromHost() if __name__ == '__main__': - - import sys - app = QtGui.QApplication(sys.argv) server = Server() random.seed(None) diff --git a/examples/network/ftp/ftp.py b/examples/network/ftp/ftp.py index cf2d373..e4b8807 100755 --- a/examples/network/ftp/ftp.py +++ b/examples/network/ftp/ftp.py @@ -1,171 +1,135 @@ #!/usr/bin/env python -"""PyQt4 port of the network/ftp example from Qt v4.x""" +"""PySide port of the network/ftp example from Qt v4.x""" -from PyQt4 import QtCore, QtGui, QtNetwork +import sys +from PySide import QtCore, QtGui, QtNetwork import ftp_rc class FtpWindow(QtGui.QDialog): def __init__(self, parent=None): - super(FtpWindow, self).__init__(parent) - - self.isDirectory = {} - self.currentPath = QtCore.QString() - self.ftp = None - self.outFile = None - - ftpServerLabel = QtGui.QLabel(self.tr("Ftp &server:")) + QtGui.QDialog.__init__(self, parent) + + self.ftpServerLabel = QtGui.QLabel(self.tr("Ftp &server:")) self.ftpServerLineEdit = QtGui.QLineEdit("ftp.trolltech.com") - ftpServerLabel.setBuddy(self.ftpServerLineEdit) - + self.ftpServerLabel.setBuddy(self.ftpServerLineEdit) + self.statusLabel = QtGui.QLabel(self.tr("Please enter the name of an FTP server.")) - - self.fileList = QtGui.QTreeWidget() - self.fileList.setEnabled(False) - self.fileList.setRootIsDecorated(False) - self.fileList.setHeaderLabels(QtCore.QStringList() << self.tr("Name") << self.tr("Size") << self.tr("Owner") << self.tr("Group") << self.tr("Time")) - self.fileList.header().setStretchLastSection(False) - + + self.fileList = QtGui.QListWidget() + self.connectButton = QtGui.QPushButton(self.tr("Connect")) self.connectButton.setDefault(True) - + + self.downloadButton = QtGui.QPushButton(self.tr("Download")) + self.downloadButton.setEnabled(False) + self.cdToParentButton = QtGui.QPushButton() self.cdToParentButton.setIcon(QtGui.QIcon(":/images/cdtoparent.png")) self.cdToParentButton.setEnabled(False) - - self.downloadButton = QtGui.QPushButton(self.tr("Download")) - self.downloadButton.setEnabled(False) - + self.quitButton = QtGui.QPushButton(self.tr("Quit")) - - buttonBox = QtGui.QDialogButtonBox() - buttonBox.addButton(self.downloadButton, - QtGui.QDialogButtonBox.ActionRole) - buttonBox.addButton(self.quitButton, QtGui.QDialogButtonBox.RejectRole) - + + self.isDirectory = {} + self.currentPath = QtCore.QString() + self.ftp = QtNetwork.QFtp(self) + self.outFile = None + self.progressDialog = QtGui.QProgressDialog(self) - - self.fileList.itemActivated.connect(self.processItem) - self.fileList.currentItemChanged.connect(self.enableDownloadButton) - self.progressDialog.canceled.connect(self.cancelDownload) - self.connectButton.clicked.connect(self.connectOrDisconnect) - self.cdToParentButton.clicked.connect(self.cdToParent) - self.downloadButton.clicked.connect(self.downloadFile) - self.quitButton.clicked.connect(self.close) - + + self.connect(self.ftpServerLineEdit, QtCore.SIGNAL("textChanged(QString &)"), + self.enableConnectButton) + self.connect(self.fileList, QtCore.SIGNAL("itemDoubleClicked(QListWidgetItem *)"), + self.processItem) + self.connect(self.fileList, QtCore.SIGNAL("itemEntered(QListWidgetItem *)"), + self.processItem) + self.connect(self.fileList, QtCore.SIGNAL("itemSelectionChanged()"), self.enableDownloadButton) + self.connect(self.ftp, QtCore.SIGNAL("commandFinished(int, bool)"), self.ftpCommandFinished) + self.connect(self.ftp, QtCore.SIGNAL("listInfo(const QUrlInfo &)"), self.addToList) + self.connect(self.ftp, QtCore.SIGNAL("dataTransferProgress(qint64, qint64)"), + self.updateDataTransferProgress) + self.connect(self.progressDialog, QtCore.SIGNAL("canceled()"), self.cancelDownload) + self.connect(self.connectButton, QtCore.SIGNAL("clicked()"), self.connectToFtpServer) + self.connect(self.cdToParentButton, QtCore.SIGNAL("clicked()"), self.cdToParent) + self.connect(self.downloadButton, QtCore.SIGNAL("clicked()"), self.downloadFile) + self.connect(self.quitButton, QtCore.SIGNAL("clicked()"), self, QtCore.SLOT("close()")) + topLayout = QtGui.QHBoxLayout() - topLayout.addWidget(ftpServerLabel) + topLayout.addWidget(self.ftpServerLabel) topLayout.addWidget(self.ftpServerLineEdit) topLayout.addWidget(self.cdToParentButton) - topLayout.addWidget(self.connectButton) - + + buttonLayout = QtGui.QHBoxLayout() + buttonLayout.addStretch(1) + buttonLayout.addWidget(self.downloadButton) + buttonLayout.addWidget(self.connectButton) + buttonLayout.addWidget(self.quitButton) + mainLayout = QtGui.QVBoxLayout() mainLayout.addLayout(topLayout) mainLayout.addWidget(self.fileList) mainLayout.addWidget(self.statusLabel) - mainLayout.addWidget(buttonBox) + mainLayout.addLayout(buttonLayout) self.setLayout(mainLayout) - + self.setWindowTitle(self.tr("FTP")) - def sizeHint(self): - return QtCore.QSize(500, 300) - - def connectOrDisconnect(self): - if self.ftp: - self.ftp.abort() - self.ftp.deleteLater() - self.ftp = None - - self.fileList.setEnabled(False) - self.cdToParentButton.setEnabled(False) - self.downloadButton.setEnabled(False) - self.connectButton.setEnabled(True) - self.connectButton.setText(self.tr("Connect")) - self.setCursor(QtCore.Qt.ArrowCursor) - - return - - self.setCursor(QtCore.Qt.WaitCursor) - - self.ftp = QtNetwork.QFtp(self) - self.ftp.commandFinished.connect(self.ftpCommandFinished) - self.ftp.listInfo.connect(self.addToList) - self.ftp.dataTransferProgress.connect(self.updateDataTransferProgress) - - self.fileList.clear() - self.currentPath.clear() - self.isDirectory.clear() - - url = QtCore.QUrl(self.ftpServerLineEdit.text()) - if not url.isValid() or url.scheme().toLower() != 'ftp': - self.ftp.connectToHost(self.ftpServerLineEdit.text(), 21) - self.ftp.login() - else: - self.ftp.connectToHost(url.host(), url.port(21)) - - if not url.userName().isEmpty(): - self.ftp.login(QtCore.QUrl.fromPercentEncoding(url.userName().toLatin1()), url.password()) - else: - self.ftp.login() - - if not url.path().isEmpty(): - self.ftp.cd(url.path()) - - self.fileList.setEnabled(True) - self.connectButton.setEnabled(False) - self.connectButton.setText(self.tr("Disconnect")) + def connectToFtpServer(self): + QtGui.QApplication.setOverrideCursor(QtCore.Qt.WaitCursor) + self.ftp.connectToHost(self.ftpServerLineEdit.text()) + self.ftp.login() + self.ftp.list() self.statusLabel.setText(self.tr("Connecting to FTP server %1...") .arg(self.ftpServerLineEdit.text())) - + def downloadFile(self): - fileName = self.fileList.currentItem().text(0) - + fileName = QtCore.QString(self.fileList.currentItem().text()) + if QtCore.QFile.exists(fileName): - QtGui.QMessageBox.information(self, self.tr("FTP"), - self.tr("There already exists a file called %1 in the " - "current directory.").arg(fileName)) + QtGui.QMessageBox.information(self, self.tr("FTP"), self.tr( + "There already exists a file called %1 " + "in the current directory.").arg(fileName)) return - + self.outFile = QtCore.QFile(fileName) - if not self.outFile.open(QtCore.QIODevice.WriteOnly): + if not self.outFile.open(QtCore.QIODevice.WriteOnly): QtGui.QMessageBox.information(self, self.tr("FTP"), - self.tr("Unable to save the file %1: %2.").arg(fileName).arg(self.outFile.errorString())) + self.tr("Unable to save the file %1: %2.") + .arg(fileName).arg(self.outFile.errorString())) self.outFile = None return - - self.ftp.get(self.fileList.currentItem().text(0), self.outFile) - + + self.ftp.get(self.fileList.currentItem().text(), self.outFile) + self.progressDialog.setLabelText(self.tr("Downloading %1...").arg(fileName)) + self.progressDialog.show() self.downloadButton.setEnabled(False) - self.progressDialog.exec_() - + def cancelDownload(self): self.ftp.abort() - - def ftpCommandFinished(self, _, error): - self.setCursor(QtCore.Qt.ArrowCursor) - + + def ftpCommandFinished(self, int, error): if self.ftp.currentCommand() == QtNetwork.QFtp.ConnectToHost: if error: - QtGui.QMessageBox.information(self, self.tr("FTP"), - self.tr("Unable to connect to the FTP server at %1. " - "Please check that the host name is correct.").arg(self.ftpServerLineEdit.text())) - self.connectOrDisconnect() + QtGui.QApplication.restoreOverrideCursor() + QtGui.QMessageBox.information(self, self.tr("FTP"), self.tr( + "Unable to connect to the FTP server " + "at %1. Please check that the host " + "name is correct.") + .arg(self.ftpServerLineEdit.text())) return - - self.statusLabel.setText(self.tr("Logged onto %1.").arg(self.ftpServerLineEdit.text())) + + self.statusLabel.setText(self.tr("Logged onto %1.") + .arg(self.ftpServerLineEdit.text())) self.fileList.setFocus() + self.connectButton.setEnabled(False) self.downloadButton.setDefault(True) - self.connectButton.setEnabled(True) return - - if self.ftp.currentCommand() == QtNetwork.QFtp.Login: - self.ftp.list() - + if self.ftp.currentCommand() == QtNetwork.QFtp.Get: + QtGui.QApplication.restoreOverrideCursor() if error: self.statusLabel.setText(self.tr("Canceled download of %1.") .arg(self.outFile.fileName())) @@ -175,37 +139,32 @@ class FtpWindow(QtGui.QDialog): self.statusLabel.setText(self.tr("Downloaded %1 to current directory.") .arg(self.outFile.fileName())) self.outFile.close() - + self.outFile = None self.enableDownloadButton() - self.progressDialog.hide() elif self.ftp.currentCommand() == QtNetwork.QFtp.List: + QtGui.QApplication.restoreOverrideCursor() if not self.isDirectory: - self.fileList.addTopLevelItem(QtGui.QTreeWidgetItem([self.tr("<empty>")])) + self.fileList.addItem(self.tr("<empty>")) self.fileList.setEnabled(False) - + def addToList(self, urlInfo): - item = QtGui.QTreeWidgetItem() - item.setText(0, urlInfo.name()) - item.setText(1, QtCore.QString.number(urlInfo.size())) - item.setText(2, urlInfo.owner()) - item.setText(3, urlInfo.group()) - item.setText(4, urlInfo.lastModified().toString("MMM dd yyyy")) - + item = QtGui.QListWidgetItem() + item.setText(urlInfo.name()) if urlInfo.isDir(): icon = QtGui.QIcon(":/images/dir.png") else: icon = QtGui.QIcon(":/images/file.png") - item.setIcon(0, icon) - - self.isDirectory[urlInfo.name()] = urlInfo.isDir() - self.fileList.addTopLevelItem(item) + item.setIcon(icon) + + self.isDirectory[unicode(urlInfo.name())] = urlInfo.isDir() + self.fileList.addItem(item) if not self.fileList.currentItem(): - self.fileList.setCurrentItem(self.fileList.topLevelItem(0)) + self.fileList.setCurrentItem(self.fileList.item(0)) self.fileList.setEnabled(True) - + def processItem(self, item): - name = item.text(0) + name = unicode(item.text()) if self.isDirectory.get(name): self.fileList.clear() self.isDirectory.clear() @@ -213,10 +172,11 @@ class FtpWindow(QtGui.QDialog): self.ftp.cd(name) self.ftp.list() self.cdToParentButton.setEnabled(True) - self.setCursor(QtCore.Qt.WaitCursor) - + QtGui.QApplication.setOverrideCursor(QtCore.Qt.WaitCursor) + return + def cdToParent(self): - self.setCursor(QtCore.Qt.WaitCursor) + QtGui.QApplication.setOverrideCursor(QtCore.Qt.WaitCursor) self.fileList.clear() self.isDirectory.clear() self.currentPath = self.currentPath.left(self.currentPath.lastIndexOf('/')) @@ -225,27 +185,26 @@ class FtpWindow(QtGui.QDialog): self.ftp.cd("/") else: self.ftp.cd(self.currentPath) - + self.ftp.list() - + def updateDataTransferProgress(self, readBytes, totalBytes): self.progressDialog.setMaximum(totalBytes) self.progressDialog.setValue(readBytes) - + + def enableConnectButton(self): + self.connectButton.setEnabled(not self.ftpServerLineEdit.text().isEmpty()) + def enableDownloadButton(self): current = self.fileList.currentItem() if current: - currentFile = current.text(0) + currentFile = QtCore.QString(current.text()) self.downloadButton.setEnabled(not self.isDirectory.get(currentFile)) else: self.downloadButton.setEnabled(False) if __name__ == '__main__': - - import sys - app = QtGui.QApplication(sys.argv) ftpWin = FtpWindow() - ftpWin.show() sys.exit(ftpWin.exec_()) diff --git a/examples/network/ftp/ftp_rc.py b/examples/network/ftp/ftp_rc.py new file mode 100644 index 0000000..a47b8d2 --- /dev/null +++ b/examples/network/ftp/ftp_rc.py @@ -0,0 +1,80 @@ +# Resource object code +# +# Created: Wed Dec 28 19:56:25 2005 +# by: The Resource Compiler for PyQt (Qt v4.1.0) +# +# WARNING! All changes made in this file will be lost! + +from PySide import QtCore + +qt_resource_data = "\ +\x00\x00\x00\x9b\ +\x89\ +\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52\x00\ +\x00\x00\x10\x00\x00\x00\x10\x08\x06\x00\x00\x00\x1f\xf3\xff\x61\ +\x00\x00\x00\x09\x70\x48\x59\x73\x00\x00\x00\x48\x00\x00\x00\x48\ +\x00\x46\xc9\x6b\x3e\x00\x00\x00\x4d\x49\x44\x41\x54\x38\xcb\x63\ +\x60\x18\x34\x60\xe6\xcc\x99\xff\xd1\x31\x49\x9a\xcf\x9c\x39\xf3\ +\xff\xff\x7f\x06\x14\x9a\x28\x43\x70\x69\x46\x36\x04\xaf\xeb\x40\ +\x1c\x52\x01\xd4\x80\xff\x68\x06\x30\xe0\x75\x09\x32\x8d\xd5\x00\ +\x62\x35\x23\x85\xcd\xa8\x0b\xa8\xea\x02\x72\x30\xdc\x00\x28\xf8\ +\x4f\x26\xa6\x1c\x00\x00\x8c\xaf\xf9\x48\x94\xb3\xf7\xb4\x00\x00\ +\x00\x00\x49\x45\x4e\x44\xae\x42\x60\x82\ +\x00\x00\x00\x81\ +\x89\ +\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52\x00\ +\x00\x00\x10\x00\x00\x00\x10\x08\x04\x00\x00\x00\xb5\xfa\x37\xea\ +\x00\x00\x00\x09\x70\x48\x59\x73\x00\x00\x00\x48\x00\x00\x00\x48\ +\x00\x46\xc9\x6b\x3e\x00\x00\x00\x33\x49\x44\x41\x54\x28\xcf\x63\ +\x60\x20\x04\xea\xff\x63\x42\x34\x05\xe8\x60\x3f\xaa\x12\x6c\x0a\ +\xfe\x23\x2b\xc1\x54\xc0\x00\x83\xb8\x14\x40\x4c\x19\x55\x80\xa1\ +\x60\x3f\x56\x08\x57\xc0\xf0\x1f\x27\x04\x03\x00\x03\xba\x4b\xb1\ +\x6c\xf1\xe8\x3e\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60\x82\ +\ +\x00\x00\x00\x8b\ +\x89\ +\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52\x00\ +\x00\x00\x0f\x00\x00\x00\x0d\x08\x06\x00\x00\x00\x76\x1e\x34\x41\ +\x00\x00\x00\x09\x70\x48\x59\x73\x00\x00\x00\x48\x00\x00\x00\x48\ +\x00\x46\xc9\x6b\x3e\x00\x00\x00\x3d\x49\x44\x41\x54\x28\xcf\x63\ +\x60\x40\x80\xff\x58\x30\x51\xe0\xff\xff\xff\x33\x31\x30\x31\x06\ +\xfc\xa7\x00\x63\xb7\x95\x10\xc6\xa9\x99\x18\x03\xb1\x6a\x46\x76\ +\x16\x49\x9a\xb1\xf9\x8b\x7e\xce\xa6\x8a\x66\x42\x5e\xa0\x4d\x54\ +\x91\xa4\x99\x5c\x0c\x00\xd4\x48\xc0\x4e\x47\x46\xbd\x51\x00\x00\ +\x00\x00\x49\x45\x4e\x44\xae\x42\x60\x82\ +" + +qt_resource_name = "\ +\x00\x06\ +\x07\x03\x7d\xc3\ +\x00\x69\ +\x00\x6d\x00\x61\x00\x67\x00\x65\x00\x73\ +\x00\x07\ +\x0b\x05\x57\x87\ +\x00\x64\ +\x00\x69\x00\x72\x00\x2e\x00\x70\x00\x6e\x00\x67\ +\x00\x08\ +\x00\x28\x5a\xe7\ +\x00\x66\ +\x00\x69\x00\x6c\x00\x65\x00\x2e\x00\x70\x00\x6e\x00\x67\ +\x00\x0e\ +\x0f\x3b\x9c\x27\ +\x00\x63\ +\x00\x64\x00\x74\x00\x6f\x00\x70\x00\x61\x00\x72\x00\x65\x00\x6e\x00\x74\x00\x2e\x00\x70\x00\x6e\x00\x67\ +" + +qt_resource_struct = "\ +\x00\x00\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x01\ +\x00\x00\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00\x02\ +\x00\x00\x00\x26\x00\x00\x00\x00\x00\x01\x00\x00\x00\x9f\ +\x00\x00\x00\x12\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\ +\x00\x00\x00\x3c\x00\x00\x00\x00\x00\x01\x00\x00\x01\x24\ +" + +def qInitResources(): + QtCore.qRegisterResourceData(0x01, qt_resource_struct, qt_resource_name, qt_resource_data) + +def qCleanupResources(): + QtCore.qUnregisterResourceData(0x01, qt_resource_struct, qt_resource_name, qt_resource_data) + +qInitResources() diff --git a/examples/network/http.py b/examples/network/http.py new file mode 100755 index 0000000..873cdad --- /dev/null +++ b/examples/network/http.py @@ -0,0 +1,155 @@ +#!/usr/bin/env python + +"""PySide port of the network/http example from Qt v4.x""" + +import sys +from PySide import QtCore, QtGui, QtNetwork + + +class HttpWindow(QtGui.QDialog): + def __init__(self, parent=None): + QtGui.QDialog.__init__(self, parent) + + self.urlLineEdit = QtGui.QLineEdit("http://www.ietf.org/iesg/1rfc_index.txt") + + self.urlLabel = QtGui.QLabel(self.tr("&URL:")) + self.urlLabel.setBuddy(self.urlLineEdit) + self.statusLabel = QtGui.QLabel(self.tr("Please enter the URL of a file " + "you want to download.")) + + self.quitButton = QtGui.QPushButton(self.tr("Quit")) + self.downloadButton = QtGui.QPushButton(self.tr("Download")) + self.downloadButton.setDefault(True) + + self.progressDialog = QtGui.QProgressDialog(self) + + self.http = QtNetwork.QHttp(self) + self.outFile = None + self.httpGetId = 0 + self.httpRequestAborted = False + + self.connect(self.urlLineEdit, QtCore.SIGNAL("textChanged(QString &)"), + self.enableDownloadButton) + self.connect(self.http, QtCore.SIGNAL("requestFinished(int, bool)"), + self.httpRequestFinished) + self.connect(self.http, QtCore.SIGNAL("dataReadProgress(int, int)"), + self.updateDataReadProgress) + self.connect(self.http, QtCore.SIGNAL("responseHeaderReceived(QHttpResponseHeader &)"), + self.readResponseHeader) + self.connect(self.progressDialog, QtCore.SIGNAL("canceled()"), + self.cancelDownload) + self.connect(self.downloadButton, QtCore.SIGNAL("clicked()"), + self.downloadFile) + self.connect(self.quitButton, QtCore.SIGNAL("clicked()"), + self, QtCore.SLOT("close()")) + + topLayout = QtGui.QHBoxLayout() + topLayout.addWidget(self.urlLabel) + topLayout.addWidget(self.urlLineEdit) + + buttonLayout = QtGui.QHBoxLayout() + buttonLayout.addStretch(1) + buttonLayout.addWidget(self.downloadButton) + buttonLayout.addWidget(self.quitButton) + + mainLayout = QtGui.QVBoxLayout() + mainLayout.addLayout(topLayout) + mainLayout.addWidget(self.statusLabel) + mainLayout.addLayout(buttonLayout) + self.setLayout(mainLayout) + + self.setWindowTitle(self.tr("HTTP")) + self.urlLineEdit.setFocus() + + def downloadFile(self): + url = QtCore.QUrl(self.urlLineEdit.text()) + fileInfo = QtCore.QFileInfo(url.path()) + fileName = QtCore.QString(fileInfo.fileName()) + + if QtCore.QFile.exists(fileName): + QtGui.QMessageBox.information(self, self.tr("HTTP"), self.tr( + "There already exists a file called %1 " + "in the current directory.").arg(fileName)) + return + + self.outFile = QtCore.QFile(fileName) + if not self.outFile.open(QtCore.QIODevice.WriteOnly): + QtGui.QMessageBox.information(self, self.tr("HTTP"), + self.tr("Unable to save the file %1: %2.") + .arg(fileName).arg(self.outFile.errorString())) + self.outFile = None + return + + if url.port() != -1: + self.http.setHost(url.host(), url.port()) + else: + self.http.setHost(url.host(), 80) + if not url.userName().isEmpty(): + self.http.setUser(url.userName(), url.password()) + + self.httpRequestAborted = False + self.httpGetId = self.http.get(url.path(), self.outFile) + + self.progressDialog.setWindowTitle(self.tr("HTTP")) + self.progressDialog.setLabelText(self.tr("Downloading %1.").arg(fileName)) + self.downloadButton.setEnabled(False) + + def cancelDownload(self): + self.statusLabel.setText(self.tr("Download canceled.")) + self.httpRequestAborted = True + self.http.abort() + self.downloadButton.setEnabled(True) + + def httpRequestFinished(self, requestId, error): + if self.httpRequestAborted: + if self.outFile is not None: + self.outFile.close() + self.outFile.remove() + self.outFile = None + + self.progressDialog.hide() + return + + if requestId != self.httpGetId: + return + + self.progressDialog.hide() + self.outFile.close() + + if error: + self.outFile.remove() + QtGui.QMessageBox.information(self, self.tr("HTTP"), + self.tr("Download failed: %1.") + .arg(self.http.errorString())) + else: + fileName = QtCore.QFileInfo(QtCore.QUrl(self.urlLineEdit.text()).path()).fileName() + self.statusLabel.setText(self.tr("Downloaded %1 to current directory.").arg(fileName)) + + self.downloadButton.setEnabled(True) + self.outFile = None + + def readResponseHeader(self, responseHeader): + if responseHeader.statusCode() != 200: + QtGui.QMessageBox.information(self, self.tr("HTTP"), + self.tr("Download failed: %1.") + .arg(responseHeader.reasonPhrase())) + self.httpRequestAborted = True + self.progressDialog.hide() + self.http.abort() + return + + def updateDataReadProgress(self, bytesRead, totalBytes): + if self.httpRequestAborted: + return + + self.progressDialog.setMaximum(totalBytes) + self.progressDialog.setValue(bytesRead) + + def enableDownloadButton(self): + self.downloadButton.setEnabled(not self.urlLineEdit.text().isEmpty()) + + +if __name__ == '__main__': + app = QtGui.QApplication(sys.argv) + httpWin = HttpWindow() + sys.exit(httpWin.exec_()) diff --git a/examples/network/loopback.py b/examples/network/loopback.py index 8b6f8e1..8deab9f 100755 --- a/examples/network/loopback.py +++ b/examples/network/loopback.py @@ -23,7 +23,8 @@ # ############################################################################ -from PyQt4 import QtCore, QtGui, QtNetwork +import sys +from PySide import QtCore, QtGui, QtNetwork class Dialog(QtGui.QDialog): @@ -31,77 +32,87 @@ class Dialog(QtGui.QDialog): PayloadSize = 65536 def __init__(self, parent=None): - super(Dialog, self).__init__(parent) - + QtGui.QDialog.__init__(self, parent) + self.tcpServer = QtNetwork.QTcpServer() self.tcpClient = QtNetwork.QTcpSocket() self.bytesToWrite = 0 self.bytesWritten = 0 self.bytesReceived = 0 - + self.clientProgressBar = QtGui.QProgressBar() self.clientStatusLabel = QtGui.QLabel(self.tr("Client ready")) self.serverProgressBar = QtGui.QProgressBar() self.serverStatusLabel = QtGui.QLabel(self.tr("Server ready")) - + self.startButton = QtGui.QPushButton(self.tr("&Start")) self.quitButton = QtGui.QPushButton(self.tr("&Quit")) - - buttonBox = QtGui.QDialogButtonBox() - buttonBox.addButton(self.startButton, QtGui.QDialogButtonBox.ActionRole) - buttonBox.addButton(self.quitButton, QtGui.QDialogButtonBox.RejectRole) - - self.startButton.clicked.connect(self.start) - self.quitButton.clicked.connect(self.close) - self.tcpServer.newConnection.connect(self.acceptConnection) - self.tcpClient.connected.connect(self.startTransfer) - self.tcpClient.bytesWritten.connect(self.updateClientProgress) - self.tcpClient.error.connect(self.displayError) - + + self.connect(self.startButton, QtCore.SIGNAL("clicked()"), self.start) + self.connect(self.quitButton, QtCore.SIGNAL("clicked()"), + self, QtCore.SLOT("close()")) + self.connect(self.tcpServer, QtCore.SIGNAL("newConnection()"), + self.acceptConnection) + self.connect(self.tcpClient, QtCore.SIGNAL("connected()"), + self.startTransfer) + self.connect(self.tcpClient, QtCore.SIGNAL("bytesWritten(qint64)"), + self.updateClientProgress) + self.connect(self.tcpClient, + QtCore.SIGNAL("error(QAbstractSocket::SocketError)"), + self.displayError) + + buttonLayout = QtGui.QHBoxLayout() + buttonLayout.addStretch(1) + buttonLayout.addWidget(self.startButton) + buttonLayout.addWidget(self.quitButton) + mainLayout = QtGui.QVBoxLayout() mainLayout.addWidget(self.clientProgressBar) mainLayout.addWidget(self.clientStatusLabel) mainLayout.addWidget(self.serverProgressBar) mainLayout.addWidget(self.serverStatusLabel) - mainLayout.addStretch(1) - mainLayout.addSpacing(10) - mainLayout.addWidget(buttonBox) + mainLayout.addLayout(buttonLayout) self.setLayout(mainLayout) - + self.setWindowTitle(self.tr("Loopback")) - + def start(self): self.startButton.setEnabled(False) - + QtGui.QApplication.setOverrideCursor(QtCore.Qt.WaitCursor) - + self.bytesWritten = 0 self.bytesReceived = 0 - + while not self.tcpServer.isListening() and not self.tcpServer.listen(): ret = QtGui.QMessageBox.critical(self, self.tr("Loopback"), - self.tr("Unable to start the test: %1.").arg(self.tcpServer.errorString()), - QtGui.QMessageBox.Retry | QtGui.QMessageBox.Cancel) + self.tr("Unable to start the test: %1.").arg( + self.tcpServer.errorString()), + QtGui.QMessageBox.Retry, + QtGui.QMessageBox.Cancel) if ret == QtGui.QMessageBox.Cancel: return - + self.serverStatusLabel.setText(self.tr("Listening")) self.clientStatusLabel.setText(self.tr("Connecting")) - + self.tcpClient.connectToHost(QtNetwork.QHostAddress(QtNetwork.QHostAddress.LocalHost), self.tcpServer.serverPort()) def acceptConnection(self): self.tcpServerConnection = self.tcpServer.nextPendingConnection() - self.tcpServerConnection.readyRead.connect(self.updateServerProgress) - self.tcpServerConnection.error.connect(self.displayError) - + self.connect(self.tcpServerConnection, QtCore.SIGNAL("readyRead()"), + self.updateServerProgress) + self.connect(self.tcpServerConnection, + QtCore.SIGNAL("error(QAbstractSocket::SocketError)"), + self.displayError) + self.serverStatusLabel.setText(self.tr("Accepted connection")) self.tcpServer.close() - + def startTransfer(self): self.bytesToWrite = Dialog.TotalBytes - self.tcpClient.write(QtCore.QByteArray(Dialog.PayloadSize, '@')) self.clientStatusLabel.setText(self.tr("Connected")) - + def updateServerProgress(self): self.bytesReceived += self.tcpServerConnection.bytesAvailable() self.tcpServerConnection.readAll() @@ -110,12 +121,12 @@ class Dialog(QtGui.QDialog): self.serverProgressBar.setValue(self.bytesReceived) self.serverStatusLabel.setText(self.tr("Received %1MB") .arg(self.bytesReceived / (1024 * 1024))) - + if self.bytesReceived == Dialog.TotalBytes: self.tcpServerConnection.close() self.startButton.setEnabled(True) QtGui.QApplication.restoreOverrideCursor() - + def updateClientProgress(self, numBytes): self.bytesWritten += numBytes if self.bytesToWrite > 0: @@ -126,13 +137,15 @@ class Dialog(QtGui.QDialog): self.clientProgressBar.setValue(self.bytesWritten) self.clientStatusLabel.setText(self.tr("Sent %1MB") .arg(self.bytesWritten / (1024 * 1024))) - + def displayError(self, socketError): if socketError == QtNetwork.QTcpSocket.RemoteHostClosedError: return - + QtGui.QMessageBox.information(self, self.tr("Network error"), - self.tr("The following error occured: %1.").arg(self.tcpClient.errorString())) + self.tr("The following error occured: "\ + "%1.") + .arg(self.tcpClient.errorString())) self.tcpClient.close() self.tcpServer.close() @@ -144,10 +157,7 @@ class Dialog(QtGui.QDialog): QtGui.QApplication.restoreOverrideCursor() -if __name__ == '__main__': - - import sys - +if __name__ == "__main__": app = QtGui.QApplication(sys.argv) dialog = Dialog() dialog.show() diff --git a/examples/network/threadedfortuneserver.py b/examples/network/threadedfortuneserver.py index b7dfe8c..459a283 100755 --- a/examples/network/threadedfortuneserver.py +++ b/examples/network/threadedfortuneserver.py @@ -23,26 +23,24 @@ # ############################################################################ +import sys import random - -from PyQt4 import QtCore, QtGui, QtNetwork +from PySide import QtCore, QtGui, QtNetwork class FortuneThread(QtCore.QThread): - error = QtCore.pyqtSignal(QtNetwork.QTcpSocket.SocketError) - def __init__(self, socketDescriptor, fortune, parent): - super(FortuneThread, self).__init__(parent) + QtCore.QThread.__init__(self, parent) self.socketDescriptor = socketDescriptor self.text = fortune - + def run(self): tcpSocket = QtNetwork.QTcpSocket() if not tcpSocket.setSocketDescriptor(self.socketDescriptor): - self.error.emit(tcpSocket.error()) + self.emit(QtCore.SIGNAL("error(int)"), tcpSocket.error()) return - + block = QtCore.QByteArray() outstr = QtCore.QDataStream(block, QtCore.QIODevice.WriteOnly) outstr.setVersion(QtCore.QDataStream.Qt_4_0) @@ -50,71 +48,68 @@ class FortuneThread(QtCore.QThread): outstr << self.text outstr.device().seek(0) outstr.writeUInt16(block.count() - 2) - + tcpSocket.write(block) tcpSocket.disconnectFromHost() tcpSocket.waitForDisconnected() - + class FortuneServer(QtNetwork.QTcpServer): def __init__(self, parent=None): - super(FortuneServer, self).__init__(parent) + QtNetwork.QTcpServer.__init__(self, parent) - self.fortunes = [ - self.tr("You've been leading a dog's life. Stay off the furniture."), - self.tr("You've got to think about tomorrow."), - self.tr("You will be surprised by a loud noise."), - self.tr("You will feel hungry again in another hour."), - self.tr("You might have mail."), - self.tr("You cannot kill time without injuring eternity."), - self.tr("Computers are not intelligent. They only think they are.")] + self.fortunes = QtCore.QStringList() + (self.fortunes << self.tr("You've been leading a dog's life. Stay off the furniture.") + << self.tr("You've got to think about tomorrow.") + << self.tr("You will be surprised by a loud noise.") + << self.tr("You will feel hungry again in another hour.") + << self.tr("You might have mail.") + << self.tr("You cannot kill time without injuring eternity.") + << self.tr("Computers are not intelligent. They only think they are.")) def incomingConnection(self, socketDescriptor): - fortune = self.fortunes[random.randint(0, len(self.fortunes) - 1)] + fortune = self.fortunes[random.randint(0, self.fortunes.count()-1)] thread = FortuneThread(socketDescriptor, fortune, self) - thread.finished.connect(thread.deleteLater) + self.connect(thread, QtCore.SIGNAL("finished()"), thread, QtCore.SLOT("deleteLater()")) thread.start() class Dialog(QtGui.QDialog): def __init__(self, parent=None): - super(Dialog, self).__init__(parent) - + QtGui.QDialog.__init__(self, parent) + self.server = FortuneServer() - - statusLabel = QtGui.QLabel() - quitButton = QtGui.QPushButton(self.tr("Quit")) - quitButton.setAutoDefault(False) - + + self.statusLabel = QtGui.QLabel() + self.quitButton = QtGui.QPushButton(self.tr("Quit")) + self.quitButton.setAutoDefault(False) + if not self.server.listen(): - QtGui.QMessageBox.critical(self, - self.tr("Threaded Fortune Server"), - self.tr("Unable to start the server: %1.".arg(self.server.errorString()))) + QtGui.QMessageBox.critical(self, self.tr("Threaded Fortune Server"), + self.tr("Unable to start the server: %1." + .arg(self.server.errorString()))) self.close() return + + self.statusLabel.setText(self.tr("The server is running on port %1.\n"\ + "Run the Fortune Client example now.") + .arg(self.server.serverPort())) - statusLabel.setText(self.tr("The server is running on port %1.\n" - "Run the Fortune Client example now.").arg(self.server.serverPort())) - - quitButton.clicked.connect(self.close) - + self.connect(self.quitButton, QtCore.SIGNAL("clicked()"), self, QtCore.SLOT("close()")) + buttonLayout = QtGui.QHBoxLayout() buttonLayout.addStretch(1) - buttonLayout.addWidget(quitButton) - buttonLayout.addStretch(1) - + buttonLayout.addWidget(self.quitButton) + mainLayout = QtGui.QVBoxLayout() - mainLayout.addWidget(statusLabel) + mainLayout.addWidget(self.statusLabel) mainLayout.addLayout(buttonLayout) self.setLayout(mainLayout) self.setWindowTitle(self.tr("Threaded Fortune Server")) -if __name__ == '__main__': - - import sys - +if __name__ == "__main__": app = QtGui.QApplication(sys.argv) dialog = Dialog() dialog.show() |