clickable links on chatlogs

This commit is contained in:
cidoku 2025-02-23 17:32:16 -03:00
parent 9eeb7897f1
commit 92085020b8

View File

@ -1,4 +1,4 @@
import thread, time, os, buttons, urllib, charselect, ini, random import thread, time, os, buttons, urllib, charselect, ini, random, re
from os.path import exists, basename from os.path import exists, basename
from ConfigParserEdit import ConfigParser from ConfigParserEdit import ConfigParser
from constants import * from constants import *
@ -68,6 +68,8 @@ SCALING_SMOOTH = 2
DOWNLOAD_BLACKLIST = [] DOWNLOAD_BLACKLIST = []
URL_REGEX = r"https?://(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b(?:[-a-zA-Z0-9()@:%_+.~#?&/=]*)"
VIEWPORT_W = 256*2 VIEWPORT_W = 256*2
VIEWPORT_H = 192*2 VIEWPORT_H = 192*2
@ -173,6 +175,9 @@ class ChatLogs(QtGui.QTextEdit):
self.type = logtype self.type = logtype
self.savelog = ini.read_ini_bool("AO2XP.ini", "General", "save logs") self.savelog = ini.read_ini_bool("AO2XP.ini", "General", "save logs")
self.combinelog = ini.read_ini_bool("AO2XP.ini", "General", "combined logs") self.combinelog = ini.read_ini_bool("AO2XP.ini", "General", "combined logs")
self.setMouseTracking(True)
self.anchor = None
if not exists("chatlogs"): if not exists("chatlogs"):
os.mkdir("chatlogs") os.mkdir("chatlogs")
@ -189,14 +194,27 @@ class ChatLogs(QtGui.QTextEdit):
else: else:
self.logfile = open("chatlogs/OOC_%d%.2d%.2d_on_%.2d.%.2d.%.2d.txt" % (currtime[0], currtime[1], currtime[2], currtime[3], currtime[4], currtime[5]), "w") self.logfile = open("chatlogs/OOC_%d%.2d%.2d_on_%.2d.%.2d.%.2d.txt" % (currtime[0], currtime[1], currtime[2], currtime[3], currtime[4], currtime[5]), "w")
else: else:
self.logfile = None self.logfile = None
def mouseMoveEvent(self, e):
super(ChatLogs, self).mouseMoveEvent(e)
self.anchor = self.anchorAt(e.pos())
if self.anchor:
QtGui.QApplication.setOverrideCursor(QtCore.Qt.PointingHandCursor)
else:
QtGui.QApplication.setOverrideCursor(QtCore.Qt.ArrowCursor)
def mouseReleaseEvent(self, e):
if self.anchor:
QtGui.QDesktopServices.openUrl(QtCore.QUrl(self.anchor))
QtGui.QApplication.setOverrideCursor(QtCore.Qt.ArrowCursor)
self.anchor = None
def __del__(self): def __del__(self):
if self.savelog: if self.savelog:
self.logfile.close() self.logfile.close()
def append(self, text): def append(self, text):
super(ChatLogs, self).append(text)
if self.savelog: if self.savelog:
if isinstance(text, str) or isinstance(text, unicode): if isinstance(text, str) or isinstance(text, unicode):
if self.combinelog and not "Log started" in text: if self.combinelog and not "Log started" in text:
@ -215,6 +233,12 @@ class ChatLogs(QtGui.QTextEdit):
else: else:
self.logfile.write(text.toUtf8()+"\n") self.logfile.write(text.toUtf8()+"\n")
if "http" in text:
text = unicode(text) # Get rid of QStrings
text = re.sub(URL_REGEX, r'<a href="\g<0>">\g<0></a>', text)
super(ChatLogs, self).append(text)
class AOCharMovie(QtGui.QLabel): class AOCharMovie(QtGui.QLabel):
done = QtCore.pyqtSignal() done = QtCore.pyqtSignal()
use_pillow = 0 use_pillow = 0
@ -2174,6 +2198,7 @@ class gui(QtGui.QWidget):
if f_char.lower() != self.charlist[f_char_id][0].lower(): if f_char.lower() != self.charlist[f_char_id][0].lower():
logcharname = self.charlist[f_char_id][0] + ' (' + f_char.decode("utf-8") + ')' logcharname = self.charlist[f_char_id][0] + ' (' + f_char.decode("utf-8") + ')'
chatmsg = self.m_chatmessage[CHATMSG]
if self.m_chatmessage[SHOWNAME] and self.m_chatmessage[SHOWNAME].lower() != f_char.lower(): if self.m_chatmessage[SHOWNAME] and self.m_chatmessage[SHOWNAME].lower() != f_char.lower():
try: try:
@ -2182,7 +2207,7 @@ class gui(QtGui.QWidget):
logcharname += " (???)" logcharname += " (???)"
if evidence == -1: if evidence == -1:
self.icLog.append('[%d:%.2d] %s: %s' % (t[3], t[4], logcharname, self.m_chatmessage[CHATMSG])) self.icLog.append('[%d:%.2d] %s: %s' % (t[3], t[4], logcharname, chatmsg))
else: else:
eviname = '(NULL) %d' % evidence eviname = '(NULL) %d' % evidence
try: try:
@ -2190,7 +2215,7 @@ class gui(QtGui.QWidget):
except: except:
pass pass
self.icLog.append('[%d:%.2d] %s: %s\n%s presented an evidence: %s' % (t[3], t[4], logcharname, self.m_chatmessage[CHATMSG], f_char, eviname)) self.icLog.append('[%d:%.2d] %s: %s\n%s presented an evidence: %s' % (t[3], t[4], logcharname, chatmsg, f_char, eviname))
self.is_additive = (self.m_chatmessage[ADDITIVE] == "1") self.is_additive = (self.m_chatmessage[ADDITIVE] == "1")
@ -3489,7 +3514,6 @@ class TCP_Thread(QtCore.QThread):
elif header == 'CT': elif header == 'CT':
name = decode_ao_str(network[1].decode('utf-8')) name = decode_ao_str(network[1].decode('utf-8'))
chatmsg = decode_ao_str(network[2].decode('utf-8').replace("\n", "<br />")) chatmsg = decode_ao_str(network[2].decode('utf-8').replace("\n", "<br />"))
#self.parent.ooclog.append('<b>%s:</b> %s' % (name, chatmsg))
self.OOC_Log.emit("<b>%s:</b> %s" % (name, chatmsg)) self.OOC_Log.emit("<b>%s:</b> %s" % (name, chatmsg))
elif header == 'PV': elif header == 'PV':