-
Notifications
You must be signed in to change notification settings - Fork 1
/
chat.py
108 lines (80 loc) · 2.23 KB
/
chat.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
97
98
99
100
101
102
103
104
105
106
107
108
# GUI
#!/usr/bin/env python
from PyQt5 import QtWidgets
import logging
import threading
from twisted.internet import reactor
import webbrowser
import os
import sys
import serve
from __version__ import __version__
def get_open_port(desired=0):
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind(("",desired))
except socket.error:
return get_open_port(0)
s.listen(1)
port = s.getsockname()[1]
s.close()
return port
PORT = get_open_port(8001)
DBDIR = sys.argv[1] if len(sys.argv) > 1 else None
webthread = None
def start_thread():
global webthread
# Start a thread for the web server.
if not DBDIR:
return
webthread = threading.Thread(target=serve.serve, args=(DBDIR, PORT))
webthread.start()
def open_browser():
webbrowser.open("http://localhost:%d/" % (PORT))
def open_about():
webbrowser.open("https://lowerquality.com/chat")
app = QtWidgets.QApplication(sys.argv)
w = QtWidgets.QWidget()
w.resize(250, 150)
w.setWindowTitle('Chat')
def run_chat():
global DBDIR
# Select a database directory
if DBDIR is None:
DBDIR = QtWidgets.QFileDialog.getExistingDirectory(
None,
"Select a database folder",
os.path.join(os.path.expanduser("~"), "Desktop"),
QtWidgets.QFileDialog.ShowDirsOnly)
start_thread()
txt.setText("Chat (%s) is running" % (__version__))
btn.setText("Open in browser")
btn.clicked.disconnect()
btn.clicked.connect(open_browser)
def quit_server():
app.exit()
layout = QtWidgets.QVBoxLayout()
w.setLayout(layout)
txt = QtWidgets.QLabel('Chat (v.%s) is not running' % (__version__))
layout.addWidget(txt)
btn = QtWidgets.QPushButton('Run chat')
btn.setStyleSheet("font-weight: bold;")
layout.addWidget(btn)
btn.clicked.connect(run_chat)
abt = QtWidgets.QPushButton('About Chat')
layout.addWidget(abt)
abt.clicked.connect(open_about)
quitb = QtWidgets.QPushButton('Quit')
layout.addWidget(quitb)
quitb.clicked.connect(quit_server)
w.show()
if DBDIR is not None:
run_chat()
w.raise_()
w.activateWindow()
app.exec_()
logging.info("Waiting for server to quit.")
reactor.callFromThread(reactor.stop)
if webthread:
webthread.join()