-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclipboard.py
More file actions
81 lines (70 loc) · 2.55 KB
/
clipboard.py
File metadata and controls
81 lines (70 loc) · 2.55 KB
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
# -*- coding: utf-8 -*-
# clipboard.py
# Clipboard monitoring module for tTalk
# Copyright (C) 2025 Chai Chaimee
# Licensed under GNU General Public License. See COPYING.txt for details.
import wx
import os
from time import sleep
from datetime import datetime
import addonHandler
addonHandler.initTranslation()
textContent = ""
tempContent = ""
class ClipboardMonitor(object):
def getClipboard(self):
clipboard = wx.Clipboard.Get()
max_retries = 10
retry_delay = 0.1
for i in range(max_retries):
try:
clipboard.Open()
break
except Exception:
sleep(retry_delay)
retry_delay *= 2
else:
return None
try:
if clipboard.IsSupported(wx.DataFormat(wx.DF_FILENAME)):
file_data = wx.FileDataObject()
clipboard.GetData(file_data)
return file_data.GetFilenames()
elif clipboard.IsSupported(wx.DataFormat(wx.DF_TEXT)):
text_data = wx.TextDataObject()
clipboard.GetData(text_data)
return text_data.GetText()
return None
finally:
if clipboard.IsOpened():
clipboard.Close()
def validClipboardData(self):
data = self.getClipboard()
if data is None:
return 0, None
if isinstance(data, list):
if len(data) == 1:
text = _("file/folder ") + os.path.basename(data[0])
elif len(data) <= 3:
names = "; ".join([os.path.basename(p) for p in data])
text = _("files/folders: ") + names
else:
text = str(len(data)) + _(" files/folders")
return 1, text
elif isinstance(data, str):
return 2, data[:1024] + "..." if len(data) > 1024 else data
return 0, None
def clipboardHasChanged(self):
global textContent, tempContent
current_data = self.getClipboard()
timestamp = datetime.now().strftime("%H:%M:%S.%f")
if isinstance(current_data, list):
tempContent = "_FILES_" + ";".join(sorted(current_data)) + timestamp
elif isinstance(current_data, str):
tempContent = "_TEXT_" + current_data + timestamp
else:
tempContent = "_UNKNOWN_" + timestamp
if tempContent != textContent:
textContent = tempContent
return True
return False