Onionr/onionr/static-data/default-plugins/gui/main.py

136 lines
3.9 KiB
Python
Raw Normal View History

2018-04-22 00:09:48 +00:00
#!/usr/bin/python
'''
Onionr - P2P Microblogging Platform & Social network
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
'''
# Imports some useful libraries
2018-05-16 03:08:42 +00:00
import logger, config, core
import os, sqlite3, threading
from onionrblockapi import Block
2018-04-22 00:09:48 +00:00
2018-05-14 04:11:31 +00:00
plugin_name = 'gui'
2018-05-16 03:08:42 +00:00
def send():
global message
block = Block()
block.setType('txt')
block.setContent(message)
logger.debug('Sent message in block %s.' % block.save(sign = True))
2018-04-22 00:09:48 +00:00
def sendMessage():
global sendEntry
2018-05-16 03:08:42 +00:00
global message
message = sendEntry.get()
t = threading.Thread(target = send)
t.start()
sendEntry.delete(0, len(message))
2018-04-22 00:09:48 +00:00
def update():
global listedBlocks, listbox, runningCheckDelayCount, runningCheckDelay, root, daemonStatus
2018-05-16 03:08:42 +00:00
for i in Block.getBlocks(type = 'txt'):
if i.getContent().strip() == '' or i.getHash() in listedBlocks:
2018-04-22 00:09:48 +00:00
continue
2018-05-16 03:08:42 +00:00
listbox.insert(99999, str(i.getContent()))
listedBlocks.append(i.getHash())
listbox.see(99999)
2018-04-22 00:09:48 +00:00
runningCheckDelayCount += 1
if runningCheckDelayCount == runningCheckDelay:
2018-05-16 03:08:42 +00:00
resp = pluginapi.daemon.local_command('ping')
2018-04-22 00:09:48 +00:00
if resp == 'pong':
2018-05-16 03:08:42 +00:00
daemonStatus.config(text = "Onionr Daemon Status: Running")
2018-04-22 00:09:48 +00:00
else:
2018-05-16 03:08:42 +00:00
daemonStatus.config(text = "Onionr Daemon Status: Not Running")
2018-04-22 00:09:48 +00:00
runningCheckDelayCount = 0
root.after(10000, update)
2018-05-16 03:08:42 +00:00
def reallyOpenGUI():
2018-04-23 04:54:51 +00:00
import tkinter
2018-04-22 00:09:48 +00:00
global root, runningCheckDelay, runningCheckDelayCount, scrollbar, listedBlocks, nodeInfo, keyInfo, idText, idEntry, pubKeyEntry, listbox, daemonStatus, sendEntry
2018-04-23 04:54:51 +00:00
root = tkinter.Tk()
2018-04-22 00:09:48 +00:00
root.title("Onionr GUI")
runningCheckDelay = 5
runningCheckDelayCount = 4
2018-04-23 04:54:51 +00:00
scrollbar = tkinter.Scrollbar(root)
scrollbar.pack(side=tkinter.RIGHT, fill=tkinter.Y)
2018-04-22 00:09:48 +00:00
listedBlocks = []
2018-04-23 04:54:51 +00:00
nodeInfo = tkinter.Frame(root)
keyInfo = tkinter.Frame(root)
2018-05-14 04:11:31 +00:00
2018-05-06 23:00:28 +00:00
hostname = pluginapi.get_onionr().get_hostname()
2018-05-16 03:08:42 +00:00
logger.debug('Onionr Hostname: %s' % hostname)
2018-05-06 23:00:28 +00:00
idText = hostname
2018-04-22 00:09:48 +00:00
2018-04-23 04:54:51 +00:00
idEntry = tkinter.Entry(nodeInfo)
2018-05-16 03:08:42 +00:00
tkinter.Label(nodeInfo, text = "Node Address: ").pack(side=tkinter.LEFT)
2018-04-22 00:09:48 +00:00
idEntry.pack()
idEntry.insert(0, idText.strip())
idEntry.configure(state="readonly")
nodeInfo.pack()
2018-04-23 04:54:51 +00:00
pubKeyEntry = tkinter.Entry(keyInfo)
2018-04-22 00:09:48 +00:00
2018-04-23 04:54:51 +00:00
tkinter.Label(keyInfo, text="Public key: ").pack(side=tkinter.LEFT)
2018-04-22 00:09:48 +00:00
pubKeyEntry.pack()
pubKeyEntry.insert(0, pluginapi.get_core()._crypto.pubKey)
pubKeyEntry.configure(state="readonly")
keyInfo.pack()
2018-04-23 04:54:51 +00:00
sendEntry = tkinter.Entry(root)
sendBtn = tkinter.Button(root, text='Send Message', command=sendMessage)
sendEntry.pack(side=tkinter.TOP, pady=5)
sendBtn.pack(side=tkinter.TOP)
2018-04-22 00:09:48 +00:00
2018-05-06 23:00:28 +00:00
listbox = tkinter.Listbox(root, yscrollcommand=tkinter.Scrollbar.set, height=15)
2018-04-22 00:09:48 +00:00
2018-04-23 04:54:51 +00:00
listbox.pack(fill=tkinter.BOTH, pady=25)
2018-04-22 00:09:48 +00:00
2018-04-23 04:54:51 +00:00
daemonStatus = tkinter.Label(root, text="Onionr Daemon Status: unknown")
2018-04-22 00:09:48 +00:00
daemonStatus.pack()
2018-05-06 23:00:28 +00:00
scrollbar.config(command=tkinter.Listbox.yview)
2018-04-22 00:09:48 +00:00
root.after(2000, update)
root.mainloop()
2018-05-16 03:08:42 +00:00
def openGUI():
t = threading.Thread(target = reallyOpenGUI)
t.daemon = False
t.start()
2018-04-22 00:09:48 +00:00
def on_init(api, data = None):
global pluginapi
pluginapi = api
api.commands.register(['gui', 'launch-gui', 'open-gui'], openGUI)
api.commands.register_help('gui', 'Opens a graphical interface for Onionr')
return