def setContent(self): self.open(QtCore.QIODevice.ReadOnly | QtCore.QIODevice.Unbuffered) self.setHeader(QtNetwork.QNetworkRequest.ContentLengthHeader, QtCore.QVariant(len(self.content))) self.readyRead.emit() self.finished.emit() self.ftp.close()
def setListContent(self): u = self.url() if not u.path().endswith("/"): u.setPath(u.path() + "/") base_url = self.url().toString() base_path = u.path() self.open(QtCore.QIODevice.ReadOnly | QtCore.QIODevice.Unbuffered) content = (u'<html>\n' '<head>\n' ' <title>%s</title>\n' ' <style type="text/css">\n' ' th { background-color: #aaaaaa; color: black }\n' ' table { border: solid 1px #aaaaaa }\n' ' tr.odd { background-color: #dddddd; color: black\n }\n' ' tr.even { background-color: white; color: black\n }\n' ' </style>\n' '</head>\n\n' '<body>\n' '<h1>Listing for %s</h1>\n\n' '<table align="center" cellspacing="0" width="90%%">\n' '<tr><th>Name</th><th>Size</th></tr>\n' % (QtCore.Qt.escape(base_url), base_path)) parent = u.resolved(QtCore.QUrl("..")) if parent.isParentOf(u): content += (u'<tr><td><strong><a href="%s">' % parent.toString() + u'Parent directory</a></strong></td><td></td></tr>\n') i = 0 for item in self.items: child = u.resolved(QtCore.QUrl(item.name())) if i == 0: content += u'<tr class="odd">' else: content += u'<tr class="even">' content += u'<td><a href="%s">%s</a></td>' % ( child.toString(), QtCore.Qt.escape(item.name())) size = item.size() unit = 0 while size: new_size = size / 1024 if new_size and unit < len(self.units): size = new_size unit += 1 else: break if item.isFile(): try: bit = self.units[unit] except IndexError: bit = 'UNK' content += u'<td>%s %s</td></tr>\n' % (str(size), bit) else: content += u'<td></td></tr>\n' i = 1 - i content += u'</table>\n</body>\n</html>\n' self.content = content.encode('utf-8') self.setHeader(QtNetwork.QNetworkRequest.ContentTypeHeader, QtCore.QVariant("text/html; charset=UTF-8")) self.setHeader(QtNetwork.QNetworkRequest.ContentLengthHeader, QtCore.QVariant(len(self.content))) self.readyRead.emit() self.finished.emit() self.ftp.close()