toxygen_plugins/CopyableToxId/toxid.py

132 lines
5.1 KiB
Python
Raw Normal View History

2016-05-28 20:08:40 +00:00
import plugin_super_class
2017-06-20 17:55:11 +00:00
from PyQt5 import QtCore, QtWidgets
2016-05-28 20:08:40 +00:00
import json
class CopyableToxId(plugin_super_class.PluginSuperClass):
def __init__(self, *args):
super(CopyableToxId, self).__init__('CopyableToxId', 'toxid', *args)
self._data = json.loads(self.load_settings())
self._copy = False
2016-06-27 14:08:07 +00:00
self._curr = -1
2016-05-28 20:08:40 +00:00
self._timer = QtCore.QTimer()
self._timer.timeout.connect(lambda: self.timer())
self.load_translator()
def get_description(self):
2017-06-20 17:55:11 +00:00
return QtWidgets.QApplication.translate("TOXID", 'Plugin which allows you to copy TOX ID of your friend easily.')
2016-05-28 20:08:40 +00:00
def get_window(self):
inst = self
2017-06-20 17:55:11 +00:00
class Window(QtWidgets.QWidget):
2016-05-28 20:08:40 +00:00
def __init__(self):
super(Window, self).__init__()
self.setGeometry(QtCore.QRect(450, 300, 350, 100))
2017-06-20 17:55:11 +00:00
self.send = QtWidgets.QCheckBox(self)
2016-05-28 20:08:40 +00:00
self.send.setGeometry(QtCore.QRect(20, 10, 310, 25))
2017-06-20 17:55:11 +00:00
self.send.setText(QtWidgets.QApplication.translate("TOXID", "Send my TOX ID to contacts"))
self.setWindowTitle(QtWidgets.QApplication.translate("TOXID", "CopyableToxID"))
2016-05-28 20:08:40 +00:00
self.send.clicked.connect(self.update)
self.send.setChecked(inst._data['send_id'])
2017-06-20 17:55:11 +00:00
self.help = QtWidgets.QPushButton(self)
2016-05-28 20:08:40 +00:00
self.help.setGeometry(QtCore.QRect(20, 40, 200, 25))
2017-06-20 17:55:11 +00:00
self.help.setText(QtWidgets.QApplication.translate("TOXID", "List of commands"))
2016-05-28 20:08:40 +00:00
self.help.clicked.connect(lambda: inst.command('help'))
def update(self):
inst._data['send_id'] = self.send.isChecked()
inst.save_settings(json.dumps(inst._data))
return Window()
def lossless_packet(self, data, friend_number):
if len(data):
2016-06-27 14:08:07 +00:00
self._data['id'] = list(filter(lambda x: not x.startswith(data[:64]), self._data['id']))
2016-05-28 20:08:40 +00:00
self._data['id'].append(data)
if self._copy:
self._timer.stop()
self._copy = False
2017-06-20 17:55:11 +00:00
clipboard = QtWidgets.QApplication.clipboard()
2016-05-28 20:08:40 +00:00
clipboard.setText(data)
self.save_settings(json.dumps(self._data))
elif self._data['send_id']:
self.send_lossless(self._tox.self_get_address(), friend_number)
def error(self):
2017-06-20 17:55:11 +00:00
msgbox = QtWidgets.QMessageBox()
title = QtWidgets.QApplication.translate("TOXID", "Error")
2016-05-28 20:08:40 +00:00
msgbox.setWindowTitle(title.format(self._name))
2017-06-20 17:55:11 +00:00
text = QtWidgets.QApplication.translate("TOXID", "Tox ID cannot be copied")
2016-05-28 20:08:40 +00:00
msgbox.setText(text)
msgbox.exec_()
def timer(self):
self._copy = False
2016-06-27 14:08:07 +00:00
if self._curr + 1:
public_key = self._tox.friend_get_public_key(self._curr)
self._curr = -1
arr = list(filter(lambda x: x.startswith(public_key), self._data['id']))
if len(arr):
2017-06-20 17:55:11 +00:00
clipboard = QtWidgets.QApplication.clipboard()
2016-06-27 14:08:07 +00:00
clipboard.setText(arr[0])
else:
self.error()
else:
self.error()
2016-05-28 20:08:40 +00:00
self._timer.stop()
def friend_connected(self, friend_number):
self.send_lossless('', friend_number)
def command(self, text):
if text == 'copy':
num = self._profile.get_active_number()
if num == -1:
return
elif text.startswith('copy '):
num = int(text[5:])
if num < 0:
return
elif text == 'enable':
self._copy = True
return
elif text == 'disable':
self._copy = False
return
elif text == 'help':
2017-06-20 17:55:11 +00:00
msgbox = QtWidgets.QMessageBox()
title = QtWidgets.QApplication.translate("TOXID", "List of commands for plugin CopyableToxID")
2016-05-28 20:08:40 +00:00
msgbox.setWindowTitle(title)
2017-06-20 17:55:11 +00:00
text = QtWidgets.QApplication.translate("TOXID", """Commands:
2016-05-28 20:08:40 +00:00
copy: copy TOX ID of current friend
copy <friend_number>: copy TOX ID of friend with specified number
enable: allow send your TOX ID to friends
disable: disallow send your TOX ID to friends
2017-06-20 17:55:11 +00:00
help: show this help""")
2016-05-28 20:08:40 +00:00
msgbox.setText(text)
msgbox.exec_()
return
else:
return
public_key = self._tox.friend_get_public_key(num)
2016-06-27 14:08:07 +00:00
arr = list(filter(lambda x: x.startswith(public_key), self._data['id']))
if self._profile.get_friend_by_number(num).status is None and len(arr):
2017-06-20 17:55:11 +00:00
clipboard = QtWidgets.QApplication.clipboard()
2016-05-28 20:08:40 +00:00
clipboard.setText(arr[0])
elif self._profile.get_friend_by_number(num).status is not None:
self._copy = True
2016-06-27 14:08:07 +00:00
self._curr = num
2016-05-28 20:08:40 +00:00
self.send_lossless('', num)
self._timer.start(2000)
else:
self.error()
def get_menu(self, menu, num):
2017-06-20 17:55:11 +00:00
act = QtWidgets.QAction(QtWidgets.QApplication.translate("TOXID", "Copy TOX ID"), menu)
2016-05-28 20:08:40 +00:00
friend = self._profile.get_friend(num)
act.connect(act, QtCore.SIGNAL("triggered()"), lambda: self.command('copy ' + str(friend.number)))
return [act]