2018-10-10 12:49:48 +02:00
|
|
|
"""
|
|
|
|
src/bitmessageqt/messageview.py
|
|
|
|
===============================
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2015-12-14 19:43:39 +01:00
|
|
|
from PyQt4 import QtCore, QtGui
|
|
|
|
|
2018-10-10 12:49:48 +02:00
|
|
|
from safehtmlparser import SafeHTMLParser
|
|
|
|
|
2015-12-14 19:43:39 +01:00
|
|
|
|
2015-12-15 01:24:10 +01:00
|
|
|
class MessageView(QtGui.QTextBrowser):
|
2018-10-10 12:49:48 +02:00
|
|
|
"""Message content viewer class, can switch between plaintext and HTML"""
|
2015-12-14 19:43:39 +01:00
|
|
|
MODE_PLAIN = 0
|
|
|
|
MODE_HTML = 1
|
2018-10-10 12:49:48 +02:00
|
|
|
|
|
|
|
def __init__(self, parent=0):
|
2015-12-14 19:43:39 +01:00
|
|
|
super(MessageView, self).__init__(parent)
|
2018-10-10 12:49:48 +02:00
|
|
|
self.mode = MessageView.MODE_PLAIN
|
2015-12-14 19:43:39 +01:00
|
|
|
self.html = None
|
2015-12-15 01:24:10 +01:00
|
|
|
self.setOpenExternalLinks(False)
|
|
|
|
self.setOpenLinks(False)
|
|
|
|
self.anchorClicked.connect(self.confirmURL)
|
2015-12-15 03:51:06 +01:00
|
|
|
self.out = ""
|
|
|
|
self.outpos = 0
|
|
|
|
self.document().setUndoRedoEnabled(False)
|
|
|
|
self.rendering = False
|
2016-01-04 11:13:31 +01:00
|
|
|
self.defaultFontPointSize = self.currentFont().pointSize()
|
2015-12-15 03:51:06 +01:00
|
|
|
self.verticalScrollBar().valueChanged.connect(self.lazyRender)
|
2016-10-20 20:26:53 +02:00
|
|
|
self.setWrappingWidth()
|
|
|
|
|
|
|
|
def resizeEvent(self, event):
|
2018-10-10 12:49:48 +02:00
|
|
|
"""View resize event handler"""
|
2016-10-20 20:26:53 +02:00
|
|
|
super(MessageView, self).resizeEvent(event)
|
|
|
|
self.setWrappingWidth(event.size().width())
|
2018-10-10 12:49:48 +02:00
|
|
|
|
2015-12-14 19:43:39 +01:00
|
|
|
def mousePressEvent(self, event):
|
2018-10-10 12:49:48 +02:00
|
|
|
"""Mouse press button event handler"""
|
|
|
|
if event.button() == QtCore.Qt.LeftButton and self.html and self.html.has_html and self.cursorForPosition(
|
|
|
|
event.pos()).block().blockNumber() == 0:
|
2015-12-14 19:43:39 +01:00
|
|
|
if self.mode == MessageView.MODE_PLAIN:
|
|
|
|
self.showHTML()
|
|
|
|
else:
|
|
|
|
self.showPlain()
|
|
|
|
else:
|
|
|
|
super(MessageView, self).mousePressEvent(event)
|
2016-01-04 11:13:31 +01:00
|
|
|
|
|
|
|
def wheelEvent(self, event):
|
2018-10-10 12:49:48 +02:00
|
|
|
"""Mouse wheel scroll event handler"""
|
2016-01-04 11:13:31 +01:00
|
|
|
# super will actually automatically take care of zooming
|
|
|
|
super(MessageView, self).wheelEvent(event)
|
2018-10-10 12:49:48 +02:00
|
|
|
if (QtGui.QApplication.queryKeyboardModifiers() &
|
|
|
|
QtCore.Qt.ControlModifier) == QtCore.Qt.ControlModifier and event.orientation() == QtCore.Qt.Vertical:
|
2016-01-23 09:55:12 +01:00
|
|
|
zoom = self.currentFont().pointSize() * 100 / self.defaultFontPointSize
|
2018-10-10 12:49:48 +02:00
|
|
|
QtGui.QApplication.activeWindow().statusBar().showMessage(
|
|
|
|
QtGui.QApplication.translate("MainWindow", "Zoom level %1%").arg(str(zoom)))
|
2016-01-04 11:13:31 +01:00
|
|
|
|
2016-10-20 20:26:53 +02:00
|
|
|
def setWrappingWidth(self, width=None):
|
2018-10-10 12:49:48 +02:00
|
|
|
"""Set word-wrapping width"""
|
2016-10-20 20:26:53 +02:00
|
|
|
self.setLineWrapMode(QtGui.QTextEdit.FixedPixelWidth)
|
|
|
|
if width is None:
|
|
|
|
width = self.width()
|
|
|
|
self.setLineWrapColumnOrWidth(width)
|
|
|
|
|
2015-12-15 01:24:10 +01:00
|
|
|
def confirmURL(self, link):
|
2018-10-10 12:49:48 +02:00
|
|
|
"""Show a dialog requesting URL opening confirmation"""
|
2016-02-29 00:47:07 +01:00
|
|
|
if link.scheme() == "mailto":
|
2018-01-18 15:14:29 +01:00
|
|
|
window = QtGui.QApplication.activeWindow()
|
|
|
|
window.ui.lineEditTo.setText(link.path())
|
2016-02-29 00:47:07 +01:00
|
|
|
if link.hasQueryItem("subject"):
|
2018-01-18 15:14:29 +01:00
|
|
|
window.ui.lineEditSubject.setText(
|
|
|
|
link.queryItemValue("subject"))
|
2016-02-29 00:47:07 +01:00
|
|
|
if link.hasQueryItem("body"):
|
2018-01-18 15:14:29 +01:00
|
|
|
window.ui.textEditMessage.setText(
|
|
|
|
link.queryItemValue("body"))
|
|
|
|
window.setSendFromComboBox()
|
|
|
|
window.ui.tabWidgetSend.setCurrentIndex(0)
|
|
|
|
window.ui.tabWidget.setCurrentIndex(
|
|
|
|
window.ui.tabWidget.indexOf(window.ui.send)
|
|
|
|
)
|
|
|
|
window.ui.textEditMessage.setFocus()
|
2016-02-29 00:47:07 +01:00
|
|
|
return
|
2018-10-10 12:49:48 +02:00
|
|
|
reply = QtGui.QMessageBox.warning(
|
|
|
|
self,
|
|
|
|
QtGui.QApplication.translate(
|
|
|
|
"MessageView",
|
|
|
|
"Follow external link"),
|
|
|
|
QtGui.QApplication.translate(
|
|
|
|
"MessageView",
|
|
|
|
"The link \"%1\" will open in a browser. It may be a security risk, it could de-anonymise you"
|
|
|
|
" or download malicious data. Are you sure?").arg(unicode(link.toString())),
|
|
|
|
QtGui.QMessageBox.Yes,
|
|
|
|
QtGui.QMessageBox.No)
|
2015-12-15 01:24:10 +01:00
|
|
|
if reply == QtGui.QMessageBox.Yes:
|
|
|
|
QtGui.QDesktopServices.openUrl(link)
|
2015-12-14 19:43:39 +01:00
|
|
|
|
2018-10-10 12:49:48 +02:00
|
|
|
def loadResource(self, restype, name):
|
|
|
|
"""
|
|
|
|
Callback for loading referenced objects, such as an image. For security reasons at the moment doesn't do
|
|
|
|
anything)
|
|
|
|
"""
|
|
|
|
pass
|
2016-03-01 02:21:10 +01:00
|
|
|
|
2015-12-15 03:51:06 +01:00
|
|
|
def lazyRender(self):
|
2018-10-10 12:49:48 +02:00
|
|
|
"""
|
|
|
|
Partially render a message. This is to avoid UI freezing when loading huge messages. It continues loading as
|
|
|
|
you scroll down.
|
|
|
|
"""
|
2015-12-15 03:51:06 +01:00
|
|
|
if self.rendering:
|
|
|
|
return
|
|
|
|
self.rendering = True
|
|
|
|
position = self.verticalScrollBar().value()
|
|
|
|
cursor = QtGui.QTextCursor(self.document())
|
2018-10-10 12:49:48 +02:00
|
|
|
while self.outpos < len(self.out) and self.verticalScrollBar().value(
|
|
|
|
) >= self.document().size().height() - 2 * self.size().height():
|
2015-12-15 03:51:06 +01:00
|
|
|
startpos = self.outpos
|
|
|
|
self.outpos += 10240
|
|
|
|
# find next end of tag
|
|
|
|
if self.mode == MessageView.MODE_HTML:
|
|
|
|
pos = self.out.find(">", self.outpos)
|
|
|
|
if pos > self.outpos:
|
2018-02-08 11:58:38 +01:00
|
|
|
self.outpos = pos + 1
|
2015-12-15 03:51:06 +01:00
|
|
|
cursor.movePosition(QtGui.QTextCursor.End, QtGui.QTextCursor.MoveAnchor)
|
|
|
|
cursor.insertHtml(QtCore.QString(self.out[startpos:self.outpos]))
|
|
|
|
self.verticalScrollBar().setValue(position)
|
|
|
|
self.rendering = False
|
2018-10-10 12:49:48 +02:00
|
|
|
|
2015-12-14 19:43:39 +01:00
|
|
|
def showPlain(self):
|
2018-10-10 12:49:48 +02:00
|
|
|
"""Render message as plain text."""
|
2015-12-14 19:43:39 +01:00
|
|
|
self.mode = MessageView.MODE_PLAIN
|
|
|
|
out = self.html.raw
|
|
|
|
if self.html.has_html:
|
2018-10-10 12:49:48 +02:00
|
|
|
out = "<div align=\"center\" style=\"text-decoration: underline;\"><b>" + unicode(
|
|
|
|
QtGui.QApplication.translate(
|
|
|
|
"MessageView", "HTML detected, click here to display")) + "</b></div><br/>" + out
|
2015-12-15 03:51:06 +01:00
|
|
|
self.out = out
|
|
|
|
self.outpos = 0
|
|
|
|
self.setHtml("")
|
|
|
|
self.lazyRender()
|
2015-12-14 19:43:39 +01:00
|
|
|
|
|
|
|
def showHTML(self):
|
2018-10-10 12:49:48 +02:00
|
|
|
"""Render message as HTML"""
|
2015-12-14 19:43:39 +01:00
|
|
|
self.mode = MessageView.MODE_HTML
|
|
|
|
out = self.html.sanitised
|
2018-10-10 12:49:48 +02:00
|
|
|
out = "<div align=\"center\" style=\"text-decoration: underline;\"><b>" + unicode(
|
|
|
|
QtGui.QApplication.translate("MessageView", "Click here to disable HTML")) + "</b></div><br/>" + out
|
2015-12-15 03:51:06 +01:00
|
|
|
self.out = out
|
|
|
|
self.outpos = 0
|
|
|
|
self.setHtml("")
|
|
|
|
self.lazyRender()
|
2015-12-14 19:43:39 +01:00
|
|
|
|
|
|
|
def setContent(self, data):
|
2018-10-10 12:49:48 +02:00
|
|
|
"""Set message content from argument"""
|
2015-12-14 19:43:39 +01:00
|
|
|
self.html = SafeHTMLParser()
|
2015-12-15 03:51:06 +01:00
|
|
|
self.html.reset()
|
|
|
|
self.html.reset_safe()
|
2016-02-25 10:13:39 +01:00
|
|
|
self.html.allow_picture = True
|
2017-05-15 12:25:30 +02:00
|
|
|
self.html.feed(data)
|
2015-12-14 19:43:39 +01:00
|
|
|
self.html.close()
|
2016-02-25 10:13:39 +01:00
|
|
|
self.showPlain()
|