2020-05-29 17:13:23 +00:00
|
|
|
#!/usr/bin/env python3
|
2020-05-15 15:58:27 +00:00
|
|
|
import webview
|
|
|
|
|
|
|
|
from threading import Thread, Lock
|
|
|
|
import sys
|
2020-05-17 17:00:04 +00:00
|
|
|
import os.path as path
|
2020-08-12 14:39:47 +00:00
|
|
|
from os import makedirs
|
2020-05-15 15:58:27 +00:00
|
|
|
from time import sleep
|
|
|
|
from server import run_server
|
|
|
|
from http.client import HTTPConnection
|
|
|
|
from deemix.utils.localpaths import getConfigFolder
|
|
|
|
|
|
|
|
server_lock = Lock()
|
|
|
|
|
|
|
|
def url_ok(url, port):
|
|
|
|
try:
|
|
|
|
conn = HTTPConnection(url, port)
|
|
|
|
conn.request('GET', '/')
|
|
|
|
r = conn.getresponse()
|
|
|
|
return r.status == 200
|
|
|
|
except:
|
|
|
|
print("Server not started")
|
|
|
|
return False
|
|
|
|
|
2020-05-17 17:00:04 +00:00
|
|
|
def save_position():
|
|
|
|
window = webview.windows[0]
|
2020-07-02 14:56:19 +00:00
|
|
|
x = int(window.x)
|
|
|
|
y = int(window.y)
|
2020-06-12 10:52:18 +00:00
|
|
|
w = int(window.width)
|
|
|
|
h = int(window.height)
|
2020-06-30 17:03:51 +00:00
|
|
|
if x < 0: x = 0
|
|
|
|
if y < 0: y = 0
|
2020-05-17 17:00:04 +00:00
|
|
|
with open(path.join(configFolder, '.UIposition'), 'w') as f:
|
|
|
|
f.write("|".join([str(x),str(y),str(w),str(h)]))
|
|
|
|
|
2020-05-15 15:58:27 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
url = "127.0.0.1"
|
2020-06-11 12:44:30 +00:00
|
|
|
port = 6595
|
2020-05-15 15:58:27 +00:00
|
|
|
if len(sys.argv) >= 2:
|
2020-05-26 10:22:32 +00:00
|
|
|
try:
|
|
|
|
port = int(sys.argv[1])
|
|
|
|
except ValueError:
|
|
|
|
pass
|
2020-08-05 14:14:39 +00:00
|
|
|
portable = None
|
|
|
|
server = False
|
2020-05-26 10:22:32 +00:00
|
|
|
if '--portable' in sys.argv:
|
|
|
|
portable = path.join(path.dirname(path.realpath(__file__)), 'config')
|
2020-08-05 14:14:39 +00:00
|
|
|
if '--server' in sys.argv or '-s' in sys.argv:
|
|
|
|
server = True
|
|
|
|
|
2020-06-05 07:58:59 +00:00
|
|
|
t = Thread(target=run_server, args=(port, url, portable))
|
2020-05-15 15:58:27 +00:00
|
|
|
t.daemon = True
|
|
|
|
t.start()
|
|
|
|
|
2020-08-05 14:14:39 +00:00
|
|
|
if not server:
|
|
|
|
while not url_ok(url, port):
|
|
|
|
sleep(1)
|
|
|
|
if portable:
|
|
|
|
configFolder = portable
|
|
|
|
else:
|
|
|
|
configFolder = getConfigFolder()
|
2020-05-15 15:58:27 +00:00
|
|
|
|
2020-08-05 14:14:39 +00:00
|
|
|
if path.isfile(path.join(configFolder, '.UIposition')):
|
|
|
|
try:
|
|
|
|
with open(path.join(configFolder, '.UIposition'), 'r') as f:
|
|
|
|
(x,y,w,h) = f.read().strip().split("|")
|
|
|
|
x = int(x)
|
|
|
|
y = int(y)
|
|
|
|
w = int(w)
|
|
|
|
h = int(h)
|
|
|
|
if x < 0: x = 0
|
|
|
|
if y < 0: y = 0
|
|
|
|
except:
|
|
|
|
x = None
|
|
|
|
y = None
|
|
|
|
w = 800
|
|
|
|
h = 600
|
|
|
|
else:
|
2020-05-17 17:00:04 +00:00
|
|
|
x = None
|
|
|
|
y = None
|
|
|
|
w = 800
|
|
|
|
h = 600
|
2020-08-05 14:14:39 +00:00
|
|
|
window = webview.create_window('deemix', 'http://'+url+':'+str(port),
|
2020-08-08 16:05:58 +00:00
|
|
|
x=x, y=y, width=w, height=h, text_select=True)
|
2020-08-05 14:14:39 +00:00
|
|
|
window.closing += save_position
|
|
|
|
if sys.platform == "win32":
|
|
|
|
from webview.platforms.cef import settings
|
2020-08-12 14:39:47 +00:00
|
|
|
cacheFolder = path.join(configFolder, 'cefCache')
|
|
|
|
makedirs(cacheFolder, exist_ok=True)
|
2020-08-05 14:14:39 +00:00
|
|
|
settings.update({
|
|
|
|
'persist_session_cookies': True,
|
2020-08-12 14:39:47 +00:00
|
|
|
'cache_path': cacheFolder
|
2020-08-05 14:14:39 +00:00
|
|
|
})
|
|
|
|
webview.start(gui='cef')
|
|
|
|
elif sys.platform == "linux":
|
|
|
|
try:
|
|
|
|
from gi import require_version as rv
|
|
|
|
rv('WebKit2', '4.0')
|
|
|
|
print("Starting with GTK")
|
|
|
|
webview.start()
|
|
|
|
except (ValueError, ImportError):
|
|
|
|
print("Starting with QT")
|
|
|
|
webview.start(gui='qt')
|
|
|
|
else:
|
2020-07-07 12:27:39 +00:00
|
|
|
webview.start()
|
2020-08-05 14:14:39 +00:00
|
|
|
conn = HTTPConnection(url, port)
|
|
|
|
conn.request('GET', '/shutdown')
|
2020-05-15 15:58:27 +00:00
|
|
|
t.join()
|