-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
96 lines (76 loc) · 3.6 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from ts3plugin import ts3plugin
import ts3defines
import ts3lib as ts3
from PythonQt.QtCore import Qt, QTimer
from PythonQt.QtGui import QLabel, QPixmap
import urllib.request
from pytson import getCurrentApiVersion
class ImageInChat(ts3plugin):
name = "Image Preview"
requestAutoload = False
version = "1.0"
try: apiVersion = getCurrentApiVersion()
except: apiVersion = 21
author = "Luemmel"
description = "Creates previews for image urls."
offersConfigure = False
commandKeyword = ""
infoTitle = None
hotkeys = []
menuItems = [(ts3defines.PluginMenuType.PLUGIN_MENU_TYPE_GLOBAL, 0, "Contact Manager Settings", "")]
status = True
showTime = 5000
imageMaxHeight = 200
contenttypes = ["image/jpeg", "image/png", "image/gif"]
def getWidgetByObjectName(self, name):
QApp = QApplication.instance()
widgets = QApp.topLevelWidgets()
widgets = widgets + QApp.allWidgets()
for x in widgets:
if str(x.objectName) == name: return x
def onTextMessageEvent(self, schid, targetMode, toID, fromID, fromName, fromUniqueIdentifier, message, ffIgnored):
if self.status:
(error, myid) = ts3.getClientID(schid)
# get plain url from bbcode url
# https://github.com/DerLuemmel/pyTSon_ts3_linkinfo/blob/master/__init__.py
message = message.lower()
if not myid == fromID and ("[url]" in message or "[url=" in message):
start = message.find("[url]")
if not start == -1:
end = message.find("[/url]")
message = message[start + 5:end]
else:
start = message.find("[url=")
end = message.find("]")
message = message[start + 5:end]
# Open stream and read content type
stream = urllib.request.urlopen(message)
contenttype = stream.info().get_content_type()
# if contenttype is png, gif, jpeg
if contenttype in self.contenttypes:
# get image data
data = stream.read()
# label and pixmap
image = QLabel("image")
pixmap = QPixmap()
# load url data to pixmap
pixmap.loadFromData(data)
# set max width to self.imageMaxHeight
pixmap = pixmap.scaledToHeight(self.imageMaxHeight, Qt.FastTransformation)
# set image to label
image.setPixmap(pixmap)
# set margins for better looking
image.setStyleSheet("QLabel{margin: 10px;}")
# Get chatlayout
chatlayout = self.getWidgetByObjectName("MainWindowChatWidget").layout()
# Add image to chatlayout
chatlayout.addWidget(image)
# Deletes image label after self.showTime
QTimer.singleShot(self.showTime, lambda : self.removeImage(chatlayout, image))
def removeImage(self, chatlayout, image):
chatlayout.removeWidget(image)
image.deleteLater()
def __init__(self):
pass
def stop(self):
pass