Clickable email and http links in plain text

Email addresses and URIs are now clickable when viewing a message in
plain text mode. Clicking an email address moves to the Send tab, while
clicking an URI has the same result as clicking an URI in html mode, it
will ask for confirmation before opening it in external handler.
This commit is contained in:
Peter Šurda 2016-02-29 07:47:07 +08:00
parent f27ca0d3d6
commit 96a1726426
2 changed files with 21 additions and 1 deletions

View File

@ -1,5 +1,6 @@
from PyQt4 import QtCore, QtGui from PyQt4 import QtCore, QtGui
from urlparse import urlparse
from safehtmlparser import * from safehtmlparser import *
class MessageView(QtGui.QTextBrowser): class MessageView(QtGui.QTextBrowser):
@ -42,6 +43,16 @@ class MessageView(QtGui.QTextBrowser):
QtGui.QApplication.activeWindow().statusBar().showMessage(QtGui.QApplication.translate("MainWindow", "Zoom level %1%").arg(str(zoom))) QtGui.QApplication.activeWindow().statusBar().showMessage(QtGui.QApplication.translate("MainWindow", "Zoom level %1%").arg(str(zoom)))
def confirmURL(self, link): def confirmURL(self, link):
if link.scheme() == "mailto":
QtGui.QApplication.activeWindow().ui.lineEditTo.setText(link.path())
if link.hasQueryItem("subject"):
QtGui.QApplication.activeWindow().ui.lineEditSubject.setText(link.queryItemValue("subject"))
if link.hasQueryItem("body"):
QtGui.QApplication.activeWindow().ui.textEditMessage.setText(link.queryItemValue("body"))
QtGui.QApplication.activeWindow().ui.tabWidgetSend.setCurrentIndex(0)
QtGui.QApplication.activeWindow().ui.tabWidget.setCurrentIndex(1)
QtGui.QApplication.activeWindow().ui.textEditMessage.setFocus()
return
reply = QtGui.QMessageBox.warning(self, reply = QtGui.QMessageBox.warning(self,
QtGui.QApplication.translate(type(self).__name__, MessageView.CONFIRM_TITLE), QtGui.QApplication.translate(type(self).__name__, MessageView.CONFIRM_TITLE),
QtGui.QApplication.translate(type(self).__name__, MessageView.CONFIRM_TEXT).arg(str(link.toString())), QtGui.QApplication.translate(type(self).__name__, MessageView.CONFIRM_TEXT).arg(str(link.toString())),

View File

@ -1,5 +1,6 @@
from HTMLParser import HTMLParser from HTMLParser import HTMLParser
import inspect import inspect
import re
from urllib import quote, quote_plus from urllib import quote, quote_plus
from urlparse import urlparse from urlparse import urlparse
@ -20,6 +21,9 @@ class SafeHTMLParser(HTMLParser):
'th', 'thead', 'tr', 'tt', 'u', 'ul', 'var', 'video'] 'th', 'thead', 'tr', 'tt', 'u', 'ul', 'var', 'video']
replaces = [["&", "&amp;"], ["\"", "&quot;"], ["<", "&lt;"], [">", "&gt;"], ["\n", "<br/>"], ["\t", "&nbsp;&nbsp;&nbsp;&nbsp;"], [" ", "&nbsp; "], [" ", "&nbsp; "], ["<br/> ", "<br/>&nbsp;"]] replaces = [["&", "&amp;"], ["\"", "&quot;"], ["<", "&lt;"], [">", "&gt;"], ["\n", "<br/>"], ["\t", "&nbsp;&nbsp;&nbsp;&nbsp;"], [" ", "&nbsp; "], [" ", "&nbsp; "], ["<br/> ", "<br/>&nbsp;"]]
src_schemes = [ "data" ] src_schemes = [ "data" ]
uriregex1 = re.compile(r'(\b(?:https?|telnet|gopher|file|wais|ftp):[\w/#~:.?+=&%@!\-.:;?\\-]+?(?=[.:?\-]*(?:[^\w/#~:;.?+=&%@!\-.:?\-]|$)))')
uriregex2 = re.compile(r'<a href="([^"]+)&amp;')
emailregex = re.compile(r'\b([A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,})\b')
@staticmethod @staticmethod
def multi_replace(text): def multi_replace(text):
@ -88,7 +92,12 @@ class SafeHTMLParser(HTMLParser):
def feed(self, data): def feed(self, data):
HTMLParser.feed(self, data) HTMLParser.feed(self, data)
tmp = SafeHTMLParser.multi_replace(data) tmp = SafeHTMLParser.multi_replace(data)
self.raw += unicode(tmp, 'utf-8', 'replace') tmp = SafeHTMLParser.uriregex1.sub(
r'<a href="\1">\1</a>',
unicode(tmp, 'utf-8', 'replace'))
tmp = SafeHTMLParser.uriregex2.sub(r'<a href="\1&', tmp)
tmp = SafeHTMLParser.emailregex.sub(r'<a href="mailto:\1">\1</a>', tmp)
self.raw += tmp
def is_html(self, text = None, allow_picture = False): def is_html(self, text = None, allow_picture = False):
if text: if text: