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
109
110
111
|
#!/usr/bin/env python3
import webview
import os
import sys
import threading
import http.server
import socketserver
import socket
import time
import json
def find_free_port():
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind(('', 0))
return s.getsockname()[1]
def start_server(path, port):
os.chdir(path)
handler = http.server.SimpleHTTPRequestHandler
with socketserver.TCPServer(("", port), handler) as httpd:
httpd.serve_forever()
class Api:
def __init__(self):
self.window = None
def set_window(self, window):
self.window = window
def save_file(self, content):
"""Opens a save file dialog and writes the content to the selected path."""
try:
# Generate default filename
filename = f"scholarflow_backup_{time.strftime('%Y%m%d')}.json"
# Open save file dialog
file_path = self.window.create_file_dialog(
webview.SAVE_DIALOG,
directory=os.path.expanduser("~"),
save_filename=filename,
file_types=('JSON files (*.json)', 'All files (*.*)')
)
if file_path:
# webview dialog returns a tuple on some platforms, a string on others
if isinstance(file_path, (list, tuple)):
file_path = file_path[0]
with open(file_path, 'w', encoding='utf-8') as f:
# Content is already a JSON string from JS
f.write(content)
return True
return False
except Exception as e:
print(f"[ScholarFlow] Save error: {e}")
return False
def on_closing(window):
title = "ScholarFlow - Çıkış"
message = "Uygulamadan çıkmadan önce verilerinizi yedeklemek (dışa aktarmak) ister misiniz?\n\n'Evet' derseniz bir konum seçip verilerinizi kaydedebilirsiniz.\n'Hayır' derseniz uygulama doğrudan kapanacaktır."
result = window.create_confirmation_dialog(title, message)
if result:
# User clicked "Yes" - Trigger the enhanced JS export which uses pywebview save dialog
window.evaluate_js("if(window.scholarflow) window.scholarflow.triggerExport()")
# Wait a bit longer for the dialog to be handled
time.sleep(0.5)
return True
if __name__ == "__main__":
script_dir = os.path.dirname(os.path.abspath(__file__))
possible_paths = [
os.path.join(script_dir, "dist"),
"/usr/share/webapps/scholarflow"
]
dist_path = None
for path in possible_paths:
if os.path.exists(path):
dist_path = path
break
if not dist_path:
sys.exit(1)
port = find_free_port()
t = threading.Thread(target=start_server, args=(dist_path, port))
t.daemon = True
t.start()
storage_dir = os.path.expanduser("~/.local/share/scholarflow")
os.makedirs(storage_dir, exist_ok=True)
api = Api()
window = webview.create_window(
'ScholarFlow',
f'http://localhost:{port}',
width=1200,
height=800,
js_api=api
)
api.set_window(window)
window.events.closing += on_closing
webview.start(storage_path=storage_dir)
os._exit(0)
|