http share
This commit is contained in:
@@ -4,6 +4,7 @@ files = [
|
|||||||
"core/dukto.py",
|
"core/dukto.py",
|
||||||
"core/file_search.py",
|
"core/file_search.py",
|
||||||
"core/headers.py",
|
"core/headers.py",
|
||||||
|
"core/http_share.py",
|
||||||
"core/updater.py",
|
"core/updater.py",
|
||||||
"core/web_search.py",
|
"core/web_search.py",
|
||||||
"strings/en.json",
|
"strings/en.json",
|
||||||
|
|||||||
307
core/http_share.py
Normal file
307
core/http_share.py
Normal file
@@ -0,0 +1,307 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import socket
|
||||||
|
import threading
|
||||||
|
import mimetypes
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import List, Optional, Callable
|
||||||
|
from http.server import HTTPServer, BaseHTTPRequestHandler
|
||||||
|
import html
|
||||||
|
|
||||||
|
def format_size(bytes_val: int) -> str:
|
||||||
|
if bytes_val is None: return ""
|
||||||
|
if bytes_val < 1024:
|
||||||
|
return f"{bytes_val} Bytes"
|
||||||
|
elif bytes_val < 1024**2:
|
||||||
|
return f"{bytes_val/1024:.2f} KB"
|
||||||
|
elif bytes_val < 1024**3:
|
||||||
|
return f"{bytes_val/1024**2:.2f} MB"
|
||||||
|
else:
|
||||||
|
return f"{bytes_val/1024**3:.2f} GB"
|
||||||
|
|
||||||
|
|
||||||
|
class FileShareHandler(BaseHTTPRequestHandler):
|
||||||
|
shared_files: List[str] = []
|
||||||
|
shared_text: Optional[str] = None
|
||||||
|
on_download: Optional[Callable[[str, str], None]] = None
|
||||||
|
|
||||||
|
def log_message(self, format, *args):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def _get_base_html(self, title: str, body_content: str) -> str:
|
||||||
|
return f"""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||||
|
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||||
|
<head>
|
||||||
|
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
||||||
|
<title>{html.escape(title)}</title>
|
||||||
|
<style type="text/css">
|
||||||
|
body {{
|
||||||
|
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
|
||||||
|
background-color: #f4f4f9;
|
||||||
|
color: #333;
|
||||||
|
margin: 0;
|
||||||
|
padding: 20px;
|
||||||
|
}}
|
||||||
|
.container {{
|
||||||
|
max-width: 800px;
|
||||||
|
margin: 0 auto;
|
||||||
|
background-color: #ffffff;
|
||||||
|
border: 1px solid #dcdcdc;
|
||||||
|
border-radius: 8px;
|
||||||
|
padding: 20px 30px;
|
||||||
|
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
|
||||||
|
}}
|
||||||
|
h1, h2 {{
|
||||||
|
color: #4a4a4a;
|
||||||
|
border-bottom: 2px solid #eaeaea;
|
||||||
|
padding-bottom: 10px;
|
||||||
|
}}
|
||||||
|
p {{
|
||||||
|
line-height: 1.6;
|
||||||
|
color: #555;
|
||||||
|
}}
|
||||||
|
table {{
|
||||||
|
width: 100%;
|
||||||
|
border-collapse: collapse;
|
||||||
|
margin-top: 20px;
|
||||||
|
}}
|
||||||
|
th, td {{
|
||||||
|
text-align: left;
|
||||||
|
padding: 12px;
|
||||||
|
border-bottom: 1px solid #ddd;
|
||||||
|
}}
|
||||||
|
th {{
|
||||||
|
background-color: #f8f8f8;
|
||||||
|
}}
|
||||||
|
tr:hover {{
|
||||||
|
background-color: #f1f1f1;
|
||||||
|
}}
|
||||||
|
a {{
|
||||||
|
color: #007bff;
|
||||||
|
text-decoration: none;
|
||||||
|
}}
|
||||||
|
a:hover {{
|
||||||
|
text-decoration: underline;
|
||||||
|
}}
|
||||||
|
textarea {{
|
||||||
|
width: 98%;
|
||||||
|
padding: 10px;
|
||||||
|
font-family: "Courier New", Courier, monospace;
|
||||||
|
border: 1px solid #ccc;
|
||||||
|
border-radius: 4px;
|
||||||
|
background-color: #fafafa;
|
||||||
|
}}
|
||||||
|
.section {{
|
||||||
|
margin-bottom: 30px;
|
||||||
|
}}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="container">
|
||||||
|
{body_content}
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>"""
|
||||||
|
|
||||||
|
def do_GET(self):
|
||||||
|
if self.path == '/':
|
||||||
|
self.send_combined_index_page()
|
||||||
|
elif self.path.startswith('/download/'):
|
||||||
|
self.handle_download()
|
||||||
|
else:
|
||||||
|
self.send_error(404, "Not Found")
|
||||||
|
|
||||||
|
def send_combined_index_page(self):
|
||||||
|
body_parts = ["<h1>CLARA Share</h1>"]
|
||||||
|
has_content = False
|
||||||
|
|
||||||
|
# Text section
|
||||||
|
if self.shared_text:
|
||||||
|
has_content = True
|
||||||
|
escaped_text = html.escape(self.shared_text)
|
||||||
|
body_parts.append(f"""<div class="section">
|
||||||
|
<h2>Shared Text</h2>
|
||||||
|
<p>Select the text below and copy it.</p>
|
||||||
|
<textarea readonly="readonly" rows="15">{escaped_text}</textarea>
|
||||||
|
</div>""")
|
||||||
|
|
||||||
|
# Files section
|
||||||
|
if self.shared_files:
|
||||||
|
has_content = True
|
||||||
|
file_rows = []
|
||||||
|
for i, filepath in enumerate(self.shared_files):
|
||||||
|
try:
|
||||||
|
path = Path(filepath)
|
||||||
|
if path.exists() and path.is_file():
|
||||||
|
file_rows.append(
|
||||||
|
f'<tr>'
|
||||||
|
f'<td>{html.escape(path.name)}</td>'
|
||||||
|
f'<td>{format_size(path.stat().st_size)}</td>'
|
||||||
|
f'<td><a href="/download/{i}">Download</a></td>'
|
||||||
|
f'</tr>'
|
||||||
|
)
|
||||||
|
except Exception:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if not file_rows:
|
||||||
|
file_table = '<p>No valid files are currently being shared.</p>'
|
||||||
|
else:
|
||||||
|
file_table = f"""<table>
|
||||||
|
<tr><th>Filename</th><th>Size</th><th>Link</th></tr>
|
||||||
|
{''.join(file_rows)}
|
||||||
|
</table>"""
|
||||||
|
|
||||||
|
body_parts.append(f"""<div class="section">
|
||||||
|
<h2>Shared Files</h2>
|
||||||
|
{file_table}
|
||||||
|
</div>""")
|
||||||
|
|
||||||
|
if not has_content:
|
||||||
|
body_parts.append("<p>No content is currently being shared.</p>")
|
||||||
|
|
||||||
|
html_content = self._get_base_html("CLARA Share", "".join(body_parts)).encode('utf-8')
|
||||||
|
|
||||||
|
self.send_response(200)
|
||||||
|
self.send_header('Content-Type', 'text/html; charset=utf-8')
|
||||||
|
self.send_header('Content-Length', str(len(html_content)))
|
||||||
|
self.end_headers()
|
||||||
|
self.wfile.write(html_content)
|
||||||
|
|
||||||
|
def handle_download(self):
|
||||||
|
try:
|
||||||
|
index = int(self.path.split('/')[-1])
|
||||||
|
|
||||||
|
if 0 <= index < len(self.shared_files):
|
||||||
|
filepath = self.shared_files[index]
|
||||||
|
path = Path(filepath)
|
||||||
|
|
||||||
|
if not path.exists() or not path.is_file():
|
||||||
|
self.send_error(404, "File not found")
|
||||||
|
return
|
||||||
|
|
||||||
|
client_ip = self.client_address[0]
|
||||||
|
|
||||||
|
if FileShareHandler.on_download:
|
||||||
|
FileShareHandler.on_download(path.name, client_ip)
|
||||||
|
|
||||||
|
mime_type, _ = mimetypes.guess_type(str(path))
|
||||||
|
if mime_type is None:
|
||||||
|
mime_type = 'application/octet-stream'
|
||||||
|
|
||||||
|
file_size = path.stat().st_size
|
||||||
|
|
||||||
|
self.send_response(200)
|
||||||
|
self.send_header('Content-Type', mime_type)
|
||||||
|
self.send_header('Content-Length', str(file_size))
|
||||||
|
self.send_header('Content-Disposition', f'attachment; filename="{path.name}"')
|
||||||
|
self.end_headers()
|
||||||
|
|
||||||
|
with open(path, 'rb') as f:
|
||||||
|
self.wfile.write(f.read())
|
||||||
|
|
||||||
|
else:
|
||||||
|
self.send_error(404, "File not found")
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error handling download: {e}")
|
||||||
|
if not self.wfile.closed:
|
||||||
|
self.send_error(500, "Internal Server Error")
|
||||||
|
|
||||||
|
|
||||||
|
class FileShareServer:
|
||||||
|
def __init__(self, port: int = 8080):
|
||||||
|
self.port = port
|
||||||
|
self.server: Optional[HTTPServer] = None
|
||||||
|
self.thread: Optional[threading.Thread] = None
|
||||||
|
self.running = False
|
||||||
|
self.shared_files: List[str] = []
|
||||||
|
self.shared_text: Optional[str] = None
|
||||||
|
|
||||||
|
self.on_download: Optional[Callable[[str, str], None]] = None
|
||||||
|
|
||||||
|
def get_local_ip(self) -> str:
|
||||||
|
try:
|
||||||
|
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
|
||||||
|
s.connect(("8.8.8.8", 80))
|
||||||
|
return s.getsockname()[0]
|
||||||
|
except Exception:
|
||||||
|
return "127.0.0.1"
|
||||||
|
|
||||||
|
def _start_server_if_needed(self) -> str:
|
||||||
|
if self.running and self.server:
|
||||||
|
local_ip = self.get_local_ip()
|
||||||
|
return f"http://{local_ip}:{self.port}"
|
||||||
|
|
||||||
|
FileShareHandler.on_download = self.on_download
|
||||||
|
|
||||||
|
port = self.port
|
||||||
|
max_attempts = 10
|
||||||
|
for attempt in range(max_attempts):
|
||||||
|
try:
|
||||||
|
self.server = HTTPServer(('0.0.0.0', port), FileShareHandler)
|
||||||
|
break
|
||||||
|
except OSError:
|
||||||
|
port += 1
|
||||||
|
if attempt == max_attempts - 1:
|
||||||
|
raise RuntimeError(f"Could not find available port after {max_attempts} attempts")
|
||||||
|
|
||||||
|
self.port = port
|
||||||
|
self.running = True
|
||||||
|
|
||||||
|
self.thread = threading.Thread(target=self._run_server, daemon=True)
|
||||||
|
self.thread.start()
|
||||||
|
|
||||||
|
local_ip = self.get_local_ip()
|
||||||
|
return f"http://{local_ip}:{self.port}"
|
||||||
|
|
||||||
|
def share_files(self, files: List[str]) -> str:
|
||||||
|
self.shared_files = files
|
||||||
|
FileShareHandler.shared_files = self.shared_files
|
||||||
|
return self._start_server_if_needed()
|
||||||
|
|
||||||
|
def add_files(self, files: List[str]):
|
||||||
|
if not self.running:
|
||||||
|
return
|
||||||
|
|
||||||
|
current_files = set(self.shared_files)
|
||||||
|
for f in files:
|
||||||
|
if f not in current_files:
|
||||||
|
self.shared_files.append(f)
|
||||||
|
|
||||||
|
def share_text(self, text: str) -> str:
|
||||||
|
self.shared_text = text
|
||||||
|
FileShareHandler.shared_text = self.shared_text
|
||||||
|
return self._start_server_if_needed()
|
||||||
|
|
||||||
|
def _run_server(self):
|
||||||
|
if self.server:
|
||||||
|
try:
|
||||||
|
self.server.serve_forever()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
self.running = False
|
||||||
|
|
||||||
|
def stop(self):
|
||||||
|
if self.server:
|
||||||
|
self.running = False
|
||||||
|
self.server.shutdown()
|
||||||
|
self.server.server_close()
|
||||||
|
self.server = None
|
||||||
|
|
||||||
|
if self.thread and self.thread.is_alive():
|
||||||
|
self.thread.join(timeout=2)
|
||||||
|
self.thread = None
|
||||||
|
|
||||||
|
self.shared_files = []
|
||||||
|
self.shared_text = None
|
||||||
|
FileShareHandler.shared_files = []
|
||||||
|
FileShareHandler.shared_text = None
|
||||||
|
|
||||||
|
def is_running(self) -> bool:
|
||||||
|
return self.running
|
||||||
|
|
||||||
|
def get_url(self) -> Optional[str]:
|
||||||
|
if self.running:
|
||||||
|
local_ip = self.get_local_ip()
|
||||||
|
return f"http://{local_ip}:{self.port}"
|
||||||
|
return None
|
||||||
@@ -47,9 +47,10 @@
|
|||||||
"search_files": "Search Files",
|
"search_files": "Search Files",
|
||||||
"search_web": "Search Web",
|
"search_web": "Search Web",
|
||||||
"calculator": "Calculator",
|
"calculator": "Calculator",
|
||||||
"send_menu": "Send",
|
"share_menu": "Share",
|
||||||
"send_files_submenu": "Send File(s)",
|
"share_files_submenu": "File(s)",
|
||||||
"send_text_submenu": "Send Text",
|
"share_text_submenu": "Text",
|
||||||
|
"via_browser": "Via Browser...",
|
||||||
"check_updates": "Check for updates",
|
"check_updates": "Check for updates",
|
||||||
"restart": "Restart",
|
"restart": "Restart",
|
||||||
"toggle_visibility": "Hide/Show",
|
"toggle_visibility": "Hide/Show",
|
||||||
@@ -59,6 +60,26 @@
|
|||||||
"send_files_dialog_title": "Select files to send to {peer_signature}",
|
"send_files_dialog_title": "Select files to send to {peer_signature}",
|
||||||
"send_text_dialog_title": "Send Text to {peer_signature}",
|
"send_text_dialog_title": "Send Text to {peer_signature}",
|
||||||
"send_text_dialog_label": "Enter text to send:",
|
"send_text_dialog_label": "Enter text to send:",
|
||||||
|
"share_browser_dialog_title": "Select files to share via browser",
|
||||||
|
"share_text_browser_dialog_title": "Share Text via Browser",
|
||||||
|
"share_text_browser_dialog_label": "Enter text to share:",
|
||||||
|
"share_browser_title": "Sharing Content",
|
||||||
|
"share_browser_text_files": "Files are now being shared!",
|
||||||
|
"share_browser_text_text": "Text is now being shared!",
|
||||||
|
"share_browser_url": "Share this URL",
|
||||||
|
"share_browser_files_info": "Sharing {count} file(s)",
|
||||||
|
"share_browser_text_info": "Sharing a block of text.",
|
||||||
|
"copy_url": "Copy URL",
|
||||||
|
"open_browser": "Open in Browser",
|
||||||
|
"stop_sharing": "Stop Sharing",
|
||||||
|
"url_copied_title": "URL Copied",
|
||||||
|
"url_copied_text": "The sharing URL has been copied to your clipboard.",
|
||||||
|
"sharing_stopped_title": "Sharing Stopped",
|
||||||
|
"sharing_stopped_text": "Content sharing has been stopped.",
|
||||||
|
"share_error_title": "Sharing Error",
|
||||||
|
"share_error_text": "Failed to start sharing: {error}",
|
||||||
|
"download_notification_title": "File Downloaded",
|
||||||
|
"download_notification_text": "{filename} was downloaded by {ip}",
|
||||||
"receive_confirm_title": "Incoming Transfer",
|
"receive_confirm_title": "Incoming Transfer",
|
||||||
"receive_confirm_text": "You have an incoming transfer from {sender_ip}.\nDo you want to accept it?",
|
"receive_confirm_text": "You have an incoming transfer from {sender_ip}.\nDo you want to accept it?",
|
||||||
"progress_dialog": {
|
"progress_dialog": {
|
||||||
|
|||||||
@@ -47,9 +47,10 @@
|
|||||||
"search_files": "Find Files",
|
"search_files": "Find Files",
|
||||||
"search_web": "Search Web",
|
"search_web": "Search Web",
|
||||||
"calculator": "Calculator",
|
"calculator": "Calculator",
|
||||||
"send_menu": "Send",
|
"share_menu": "Share",
|
||||||
"send_files_submenu": "Send Files",
|
"share_files_submenu": "File(s)",
|
||||||
"send_text_submenu": "Send Message",
|
"share_text_submenu": "Message",
|
||||||
|
"via_browser": "Via Browser...",
|
||||||
"check_updates": "Check for Updates",
|
"check_updates": "Check for Updates",
|
||||||
"restart": "Restart",
|
"restart": "Restart",
|
||||||
"toggle_visibility": "Hide/Show",
|
"toggle_visibility": "Hide/Show",
|
||||||
@@ -59,6 +60,26 @@
|
|||||||
"send_files_dialog_title": "Choose files for {peer_signature}",
|
"send_files_dialog_title": "Choose files for {peer_signature}",
|
||||||
"send_text_dialog_title": "Message to {peer_signature}",
|
"send_text_dialog_title": "Message to {peer_signature}",
|
||||||
"send_text_dialog_label": "What would you like to say?",
|
"send_text_dialog_label": "What would you like to say?",
|
||||||
|
"share_browser_dialog_title": "Pick files to share in your browser",
|
||||||
|
"share_text_browser_dialog_title": "Broadcast a Message",
|
||||||
|
"share_text_browser_dialog_label": "What's the message?",
|
||||||
|
"share_browser_title": "Ready to Share!",
|
||||||
|
"share_browser_text_files": "Your files are ready for anyone with the link!",
|
||||||
|
"share_browser_text_text": "Your message is live for anyone with the link!",
|
||||||
|
"share_browser_url": "Share this link",
|
||||||
|
"share_browser_files_info": "Sharing {count} file(s)",
|
||||||
|
"share_browser_text_info": "Sharing a message.",
|
||||||
|
"copy_url": "Copy Link",
|
||||||
|
"open_browser": "Open in Browser",
|
||||||
|
"stop_sharing": "Stop Sharing",
|
||||||
|
"url_copied_title": "Link Copied!",
|
||||||
|
"url_copied_text": "The sharing link is now on your clipboard. Send it to anyone!",
|
||||||
|
"sharing_stopped_title": "Sharing Stopped",
|
||||||
|
"sharing_stopped_text": "Sharing has been stopped.",
|
||||||
|
"share_error_title": "Oops!",
|
||||||
|
"share_error_text": "Couldn't start sharing: {error}",
|
||||||
|
"download_notification_title": "Someone downloaded a file!",
|
||||||
|
"download_notification_text": "{filename} was downloaded by {ip}",
|
||||||
"receive_confirm_title": "Incoming!",
|
"receive_confirm_title": "Incoming!",
|
||||||
"receive_confirm_text": "{sender_ip} wants to send you something.\nAccept it?",
|
"receive_confirm_text": "{sender_ip} wants to send you something.\nAccept it?",
|
||||||
"progress_dialog": {
|
"progress_dialog": {
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ from core.discord_presence import presence
|
|||||||
from core.dukto import Peer
|
from core.dukto import Peer
|
||||||
from core.file_search import find
|
from core.file_search import find
|
||||||
from core.web_search import MullvadLetaWrapper
|
from core.web_search import MullvadLetaWrapper
|
||||||
|
from core.http_share import FileShareServer
|
||||||
|
|
||||||
from windows.app_launcher import AppLauncherDialog
|
from windows.app_launcher import AppLauncherDialog
|
||||||
from windows.file_search import FileSearchResults
|
from windows.file_search import FileSearchResults
|
||||||
@@ -32,6 +33,9 @@ class MainWindow(QtWidgets.QMainWindow):
|
|||||||
send_start_signal = QtCore.Signal(str)
|
send_start_signal = QtCore.Signal(str)
|
||||||
send_complete_signal = QtCore.Signal(list)
|
send_complete_signal = QtCore.Signal(list)
|
||||||
dukto_error_signal = QtCore.Signal(str)
|
dukto_error_signal = QtCore.Signal(str)
|
||||||
|
|
||||||
|
# HTTP share signals
|
||||||
|
http_download_signal = QtCore.Signal(str, str)
|
||||||
|
|
||||||
|
|
||||||
def __init__(self, dukto_handler, strings, restart=False, no_quit=False, super_menu=True):
|
def __init__(self, dukto_handler, strings, restart=False, no_quit=False, super_menu=True):
|
||||||
@@ -63,6 +67,10 @@ class MainWindow(QtWidgets.QMainWindow):
|
|||||||
self.super_menu = super_menu
|
self.super_menu = super_menu
|
||||||
self.dukto_handler = dukto_handler
|
self.dukto_handler = dukto_handler
|
||||||
self.progress_dialog = None
|
self.progress_dialog = None
|
||||||
|
|
||||||
|
# HTTP file sharing
|
||||||
|
self.http_share = FileShareServer(port=8080)
|
||||||
|
self.http_share.on_download = lambda filename, ip: self.http_download_signal.emit(filename, ip)
|
||||||
|
|
||||||
# Connect Dukto callbacks to emit signals
|
# Connect Dukto callbacks to emit signals
|
||||||
self.dukto_handler.on_peer_added = lambda peer: self.peer_added_signal.emit(peer)
|
self.dukto_handler.on_peer_added = lambda peer: self.peer_added_signal.emit(peer)
|
||||||
@@ -87,47 +95,13 @@ class MainWindow(QtWidgets.QMainWindow):
|
|||||||
self.send_start_signal.connect(self.handle_send_start)
|
self.send_start_signal.connect(self.handle_send_start)
|
||||||
self.send_complete_signal.connect(self.handle_send_complete)
|
self.send_complete_signal.connect(self.handle_send_complete)
|
||||||
self.dukto_error_signal.connect(self.handle_dukto_error)
|
self.dukto_error_signal.connect(self.handle_dukto_error)
|
||||||
|
self.http_download_signal.connect(self.handle_http_download)
|
||||||
|
|
||||||
self.tray = QtWidgets.QSystemTrayIcon(self)
|
self.tray = QtWidgets.QSystemTrayIcon(self)
|
||||||
self.tray.setIcon(QtGui.QIcon(str(ASSET)))
|
self.tray.setIcon(QtGui.QIcon(str(ASSET)))
|
||||||
|
|
||||||
s = self.strings["main_window"]["right_menu"]
|
self.build_menus()
|
||||||
|
|
||||||
# RIGHT MENU
|
|
||||||
right_menu = QtWidgets.QMenu()
|
|
||||||
right_menu.addAction(s["launch_app"], self.start_app_launcher)
|
|
||||||
right_menu.addAction(s["search_files"], self.start_file_search)
|
|
||||||
right_menu.addAction(s["search_web"], self.start_web_search)
|
|
||||||
right_menu.addAction(s.get("calculator", "Calculator"), self.start_calculator)
|
|
||||||
right_menu.addSeparator()
|
|
||||||
send_menu_right = right_menu.addMenu(s["send_menu"])
|
|
||||||
self.send_files_submenu_right = send_menu_right.addMenu(s["send_files_submenu"])
|
|
||||||
self.send_text_submenu_right = send_menu_right.addMenu(s["send_text_submenu"])
|
|
||||||
right_menu.addSeparator()
|
|
||||||
right_menu.addAction(s["check_updates"], self.update_git)
|
|
||||||
if restart:
|
|
||||||
right_menu.addAction(s["restart"], self.restart_application)
|
|
||||||
right_menu.addAction(s["toggle_visibility"], self.toggle_visible)
|
|
||||||
right_menu.addSeparator()
|
|
||||||
if not no_quit:
|
|
||||||
right_menu.addAction(s["quit"], QtWidgets.QApplication.quit)
|
|
||||||
self.tray.setContextMenu(right_menu)
|
|
||||||
self.tray.activated.connect(self.handle_tray_activated)
|
|
||||||
self.tray.show()
|
|
||||||
|
|
||||||
# LEFT MENU
|
|
||||||
self.left_menu = QtWidgets.QMenu()
|
|
||||||
self.left_menu.addAction(s["launch_app"], self.start_app_launcher)
|
|
||||||
self.left_menu.addAction(s["search_files"], self.start_file_search)
|
|
||||||
self.left_menu.addAction(s["search_web"], self.start_web_search)
|
|
||||||
self.left_menu.addAction(s.get("calculator", "Calculator"), self.start_calculator)
|
|
||||||
self.left_menu.addSeparator()
|
|
||||||
send_menu_left = self.left_menu.addMenu(s["send_menu"])
|
|
||||||
self.send_files_submenu_left = send_menu_left.addMenu(s["send_files_submenu"])
|
|
||||||
self.send_text_submenu_left = send_menu_left.addMenu(s["send_text_submenu"])
|
|
||||||
|
|
||||||
self.update_peer_menus()
|
|
||||||
|
|
||||||
# always on top timer
|
# always on top timer
|
||||||
self.stay_on_top_timer = QtCore.QTimer(self)
|
self.stay_on_top_timer = QtCore.QTimer(self)
|
||||||
self.stay_on_top_timer.timeout.connect(self.ensure_on_top)
|
self.stay_on_top_timer.timeout.connect(self.ensure_on_top)
|
||||||
@@ -137,6 +111,92 @@ class MainWindow(QtWidgets.QMainWindow):
|
|||||||
self.show_menu_signal.connect(self.show_menu)
|
self.show_menu_signal.connect(self.show_menu)
|
||||||
self.start_hotkey_listener()
|
self.start_hotkey_listener()
|
||||||
|
|
||||||
|
def build_menus(self):
|
||||||
|
s = self.strings["main_window"]["right_menu"]
|
||||||
|
|
||||||
|
# LEFT MENU (Main widget)
|
||||||
|
self.left_menu = QtWidgets.QMenu()
|
||||||
|
self.left_menu.addAction(s["launch_app"], self.start_app_launcher)
|
||||||
|
self.left_menu.addAction(s["search_files"], self.start_file_search)
|
||||||
|
self.left_menu.addAction(s["search_web"], self.start_web_search)
|
||||||
|
self.left_menu.addAction(s.get("calculator", "Calculator"), self.start_calculator)
|
||||||
|
self.left_menu.addSeparator()
|
||||||
|
share_menu_left = self.left_menu.addMenu(s["share_menu"])
|
||||||
|
self.share_files_submenu_left = share_menu_left.addMenu(s["share_files_submenu"])
|
||||||
|
self.share_text_submenu_left = share_menu_left.addMenu(s["share_text_submenu"])
|
||||||
|
self.stop_share_action_left = share_menu_left.addAction("Stop Browser Share", self.stop_browser_share)
|
||||||
|
self.left_menu.addSeparator()
|
||||||
|
|
||||||
|
# RIGHT MENU (Tray icon)
|
||||||
|
right_menu = QtWidgets.QMenu()
|
||||||
|
right_menu.addAction(s["launch_app"], self.start_app_launcher)
|
||||||
|
right_menu.addAction(s["search_files"], self.start_file_search)
|
||||||
|
right_menu.addAction(s["search_web"], self.start_web_search)
|
||||||
|
right_menu.addAction(s.get("calculator", "Calculator"), self.start_calculator)
|
||||||
|
right_menu.addSeparator()
|
||||||
|
share_menu_right = right_menu.addMenu(s["share_menu"])
|
||||||
|
self.share_files_submenu_right = share_menu_right.addMenu(s["share_files_submenu"])
|
||||||
|
self.share_text_submenu_right = share_menu_right.addMenu(s["share_text_submenu"])
|
||||||
|
self.stop_share_action_right = share_menu_right.addAction("Stop Browser Share", self.stop_browser_share)
|
||||||
|
right_menu.addSeparator()
|
||||||
|
right_menu.addAction(s["check_updates"], self.update_git)
|
||||||
|
|
||||||
|
if "--restart" in sys.argv:
|
||||||
|
right_menu.addAction(s["restart"], self.restart_application)
|
||||||
|
right_menu.addAction(s["toggle_visibility"], self.toggle_visible)
|
||||||
|
right_menu.addSeparator()
|
||||||
|
if "--no-quit" not in sys.argv:
|
||||||
|
right_menu.addAction(s["quit"], QtWidgets.QApplication.quit)
|
||||||
|
|
||||||
|
self.tray.setContextMenu(right_menu)
|
||||||
|
self.tray.activated.connect(self.handle_tray_activated)
|
||||||
|
self.tray.show()
|
||||||
|
|
||||||
|
self.update_peer_menus()
|
||||||
|
self.update_share_menu_state()
|
||||||
|
|
||||||
|
def update_share_menu_state(self):
|
||||||
|
s_menu = self.strings["main_window"]["right_menu"]
|
||||||
|
|
||||||
|
is_sharing = self.http_share.is_running()
|
||||||
|
has_shared_files = bool(self.http_share.shared_files)
|
||||||
|
has_shared_text = bool(self.http_share.shared_text)
|
||||||
|
|
||||||
|
# Set visibility of stop action
|
||||||
|
self.stop_share_action_left.setVisible(is_sharing)
|
||||||
|
self.stop_share_action_right.setVisible(is_sharing)
|
||||||
|
|
||||||
|
# Configure file share menus
|
||||||
|
for menu in [self.share_files_submenu_left, self.share_files_submenu_right]:
|
||||||
|
for action in menu.actions():
|
||||||
|
if hasattr(action, 'is_browser_action'):
|
||||||
|
menu.removeAction(action)
|
||||||
|
|
||||||
|
action_text = "Add File(s)..." if has_shared_files else s_menu["via_browser"]
|
||||||
|
browser_action = menu.addAction(action_text)
|
||||||
|
browser_action.is_browser_action = True
|
||||||
|
browser_action.triggered.connect(self.start_file_share_browser)
|
||||||
|
|
||||||
|
if any(not a.isSeparator() and not hasattr(a, 'is_browser_action') for a in menu.actions()):
|
||||||
|
if not any(a.isSeparator() for a in menu.actions()):
|
||||||
|
menu.addSeparator()
|
||||||
|
|
||||||
|
# Configure text share menus
|
||||||
|
for menu in [self.share_text_submenu_left, self.share_text_submenu_right]:
|
||||||
|
for action in menu.actions():
|
||||||
|
if hasattr(action, 'is_browser_action'):
|
||||||
|
menu.removeAction(action)
|
||||||
|
|
||||||
|
action_text = "Change Shared Text..." if has_shared_text else s_menu["via_browser"]
|
||||||
|
browser_action = menu.addAction(action_text)
|
||||||
|
browser_action.is_browser_action = True
|
||||||
|
browser_action.triggered.connect(self.start_text_share_browser)
|
||||||
|
|
||||||
|
if any(not a.isSeparator() and not hasattr(a, 'is_browser_action') for a in menu.actions()):
|
||||||
|
if not any(a.isSeparator() for a in menu.actions()):
|
||||||
|
menu.addSeparator()
|
||||||
|
|
||||||
|
|
||||||
def show_menu(self):
|
def show_menu(self):
|
||||||
self.left_menu.popup(QtGui.QCursor.pos())
|
self.left_menu.popup(QtGui.QCursor.pos())
|
||||||
|
|
||||||
@@ -151,6 +211,8 @@ class MainWindow(QtWidgets.QMainWindow):
|
|||||||
|
|
||||||
def closeEvent(self, event):
|
def closeEvent(self, event):
|
||||||
self.listener.stop()
|
self.listener.stop()
|
||||||
|
if self.http_share.is_running():
|
||||||
|
self.http_share.stop()
|
||||||
super().closeEvent(event)
|
super().closeEvent(event)
|
||||||
|
|
||||||
def ensure_on_top(self):
|
def ensure_on_top(self):
|
||||||
@@ -164,72 +226,144 @@ class MainWindow(QtWidgets.QMainWindow):
|
|||||||
def mousePressEvent(self, event: QtGui.QMouseEvent):
|
def mousePressEvent(self, event: QtGui.QMouseEvent):
|
||||||
if event.button() == QtCore.Qt.LeftButton: #type: ignore
|
if event.button() == QtCore.Qt.LeftButton: #type: ignore
|
||||||
self.left_menu.popup(event.globalPosition().toPoint())
|
self.left_menu.popup(event.globalPosition().toPoint())
|
||||||
|
|
||||||
elif event.button() == QtCore.Qt.RightButton: #type: ignore
|
elif event.button() == QtCore.Qt.RightButton: #type: ignore
|
||||||
self.tray.contextMenu().popup(event.globalPosition().toPoint())
|
self.tray.contextMenu().popup(event.globalPosition().toPoint())
|
||||||
|
|
||||||
def handle_tray_activated(self, reason):
|
def handle_tray_activated(self, reason):
|
||||||
if reason == QtWidgets.QSystemTrayIcon.ActivationReason.Trigger:
|
if reason == QtWidgets.QSystemTrayIcon.ActivationReason.Trigger:
|
||||||
self.left_menu.popup(QtGui.QCursor.pos())
|
self.tray.contextMenu().popup(QtGui.QCursor.pos())
|
||||||
|
|
||||||
def toggle_visible(self):
|
def toggle_visible(self):
|
||||||
self.setVisible(not self.isVisible())
|
self.setVisible(not self.isVisible())
|
||||||
|
|
||||||
def update_peer_menus(self):
|
def update_peer_menus(self):
|
||||||
self.send_files_submenu_left.clear()
|
s_main = self.strings["main_window"]
|
||||||
self.send_text_submenu_left.clear()
|
no_peers_str = s_main["no_peers"]
|
||||||
self.send_files_submenu_right.clear()
|
|
||||||
self.send_text_submenu_right.clear()
|
|
||||||
|
|
||||||
no_peers_str = self.strings["main_window"]["no_peers"]
|
|
||||||
|
|
||||||
peers = list(self.dukto_handler.peers.values())
|
peers = list(self.dukto_handler.peers.values())
|
||||||
|
|
||||||
|
for menu in [self.share_files_submenu_left, self.share_files_submenu_right, self.share_text_submenu_left, self.share_text_submenu_right]:
|
||||||
|
actions_to_remove = [a for a in menu.actions() if not a.isSeparator() and not hasattr(a, 'is_browser_action')]
|
||||||
|
for action in actions_to_remove:
|
||||||
|
menu.removeAction(action)
|
||||||
|
|
||||||
if not peers:
|
if not peers:
|
||||||
no_peers_action_left_files = self.send_files_submenu_left.addAction(no_peers_str)
|
for menu in [self.share_files_submenu_left, self.share_files_submenu_right, self.share_text_submenu_left, self.share_text_submenu_right]:
|
||||||
no_peers_action_left_files.setEnabled(False)
|
action = menu.addAction(no_peers_str)
|
||||||
no_peers_action_left_text = self.send_text_submenu_left.addAction(no_peers_str)
|
action.setEnabled(False)
|
||||||
no_peers_action_left_text.setEnabled(False)
|
else:
|
||||||
|
for peer in sorted(peers, key=lambda p: p.signature):
|
||||||
no_peers_action_right_files = self.send_files_submenu_right.addAction(no_peers_str)
|
for files_menu, text_menu in [(self.share_files_submenu_left, self.share_text_submenu_left), (self.share_files_submenu_right, self.share_text_submenu_right)]:
|
||||||
no_peers_action_right_files.setEnabled(False)
|
file_action = files_menu.addAction(peer.signature)
|
||||||
no_peers_action_right_text = self.send_text_submenu_right.addAction(no_peers_str)
|
text_action = text_menu.addAction(peer.signature)
|
||||||
no_peers_action_right_text.setEnabled(False)
|
file_action.triggered.connect(lambda checked=False, p=peer: self.start_file_send(p))
|
||||||
return
|
text_action.triggered.connect(lambda checked=False, p=peer: self.start_text_send(p))
|
||||||
|
|
||||||
for peer in sorted(peers, key=lambda p: p.signature):
|
self.update_share_menu_state()
|
||||||
file_action_left = self.send_files_submenu_left.addAction(peer.signature)
|
|
||||||
file_action_right = self.send_files_submenu_right.addAction(peer.signature)
|
|
||||||
|
|
||||||
text_action_left = self.send_text_submenu_left.addAction(peer.signature)
|
|
||||||
text_action_right = self.send_text_submenu_right.addAction(peer.signature)
|
|
||||||
|
|
||||||
file_action_left.triggered.connect(lambda checked=False, p=peer: self.start_file_send(p))
|
|
||||||
file_action_right.triggered.connect(lambda checked=False, p=peer: self.start_file_send(p))
|
|
||||||
|
|
||||||
text_action_left.triggered.connect(lambda checked=False, p=peer: self.start_text_send(p))
|
|
||||||
text_action_right.triggered.connect(lambda checked=False, p=peer: self.start_text_send(p))
|
|
||||||
|
|
||||||
def start_file_send(self, peer: Peer):
|
def start_file_send(self, peer: Peer):
|
||||||
dialog_title = self.strings["main_window"]["send_files_dialog_title"].format(peer_signature=peer.signature)
|
dialog_title = self.strings["main_window"]["send_files_dialog_title"].format(peer_signature=peer.signature)
|
||||||
file_paths, _ = QtWidgets.QFileDialog.getOpenFileNames(
|
file_paths, _ = QtWidgets.QFileDialog.getOpenFileNames(self, dialog_title, str(Path.home()))
|
||||||
self,
|
|
||||||
dialog_title,
|
|
||||||
str(Path.home()),
|
|
||||||
)
|
|
||||||
if file_paths:
|
if file_paths:
|
||||||
self.dukto_handler.send_file(peer.address, file_paths, peer.port)
|
self.dukto_handler.send_file(peer.address, file_paths, peer.port)
|
||||||
|
|
||||||
def start_text_send(self, peer: Peer):
|
def start_text_send(self, peer: Peer):
|
||||||
dialog_title = self.strings["main_window"]["send_text_dialog_title"].format(peer_signature=peer.signature)
|
dialog_title = self.strings["main_window"]["send_text_dialog_title"].format(peer_signature=peer.signature)
|
||||||
dialog_label = self.strings["main_window"]["send_text_dialog_label"]
|
dialog_label = self.strings["main_window"]["send_text_dialog_label"]
|
||||||
text, ok = QtWidgets.QInputDialog.getMultiLineText(
|
text, ok = QtWidgets.QInputDialog.getMultiLineText(self, dialog_title, dialog_label)
|
||||||
self,
|
|
||||||
dialog_title,
|
|
||||||
dialog_label
|
|
||||||
)
|
|
||||||
if ok and text:
|
if ok and text:
|
||||||
self.dukto_handler.send_text(peer.address, text, peer.port)
|
self.dukto_handler.send_text(peer.address, text, peer.port)
|
||||||
|
|
||||||
|
def start_file_share_browser(self):
|
||||||
|
s = self.strings["main_window"]
|
||||||
|
is_adding = bool(self.http_share.shared_files)
|
||||||
|
|
||||||
|
dialog_title = "Select files to add" if is_adding else s["share_browser_dialog_title"]
|
||||||
|
file_paths, _ = QtWidgets.QFileDialog.getOpenFileNames(self, dialog_title, str(Path.home()))
|
||||||
|
|
||||||
|
if not file_paths:
|
||||||
|
return
|
||||||
|
|
||||||
|
try:
|
||||||
|
if is_adding:
|
||||||
|
self.http_share.add_files(file_paths)
|
||||||
|
self.tray.showMessage("Files Added", f"{len(file_paths)} file(s) added to the share.", QtWidgets.QSystemTrayIcon.Information, 2000) #type: ignore
|
||||||
|
else:
|
||||||
|
url = self.http_share.share_files(file_paths)
|
||||||
|
main_text = s["share_browser_text_files"]
|
||||||
|
info_text = s["share_browser_files_info"].format(count=len(file_paths))
|
||||||
|
if not self.http_share.shared_text:
|
||||||
|
self._show_sharing_dialog(url, main_text, info_text)
|
||||||
|
|
||||||
|
self.update_share_menu_state()
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
QtWidgets.QMessageBox.critical(self, s["share_error_title"], s["share_error_text"].format(error=str(e)))
|
||||||
|
|
||||||
|
def start_text_share_browser(self):
|
||||||
|
s = self.strings["main_window"]
|
||||||
|
is_changing = bool(self.http_share.shared_text)
|
||||||
|
|
||||||
|
text, ok = QtWidgets.QInputDialog.getMultiLineText(
|
||||||
|
self,
|
||||||
|
s["share_text_browser_dialog_title"],
|
||||||
|
s["share_text_browser_dialog_label"],
|
||||||
|
self.http_share.shared_text or ""
|
||||||
|
)
|
||||||
|
|
||||||
|
if not (ok and text):
|
||||||
|
return
|
||||||
|
|
||||||
|
try:
|
||||||
|
url = self.http_share.share_text(text)
|
||||||
|
if not is_changing:
|
||||||
|
main_text = s["share_browser_text_text"]
|
||||||
|
info_text = s["share_browser_text_info"]
|
||||||
|
if not self.http_share.shared_files:
|
||||||
|
self._show_sharing_dialog(url, main_text, info_text)
|
||||||
|
|
||||||
|
self.update_share_menu_state()
|
||||||
|
except Exception as e:
|
||||||
|
QtWidgets.QMessageBox.critical(self, s["share_error_title"], s["share_error_text"].format(error=str(e)))
|
||||||
|
|
||||||
|
def stop_browser_share(self):
|
||||||
|
s = self.strings["main_window"]
|
||||||
|
if self.http_share.is_running():
|
||||||
|
self.http_share.stop()
|
||||||
|
self.tray.showMessage(s["sharing_stopped_title"], s["sharing_stopped_text"], QtWidgets.QSystemTrayIcon.Information, 2000) #type: ignore
|
||||||
|
self.update_share_menu_state()
|
||||||
|
|
||||||
|
def _show_sharing_dialog(self, url: str, main_text: str, info_text: str):
|
||||||
|
s = self.strings["main_window"]
|
||||||
|
msg = QtWidgets.QMessageBox(self)
|
||||||
|
msg.setIcon(QtWidgets.QMessageBox.Information) #type: ignore
|
||||||
|
msg.setWindowTitle(s["share_browser_title"])
|
||||||
|
msg.setText(main_text)
|
||||||
|
msg.setInformativeText(f"{s['share_browser_url']}:\n\n{url}\n\n{info_text}")
|
||||||
|
|
||||||
|
copy_btn = msg.addButton(s["copy_url"], QtWidgets.QMessageBox.ActionRole) #type: ignore
|
||||||
|
open_btn = msg.addButton(s["open_browser"], QtWidgets.QMessageBox.ActionRole) #type: ignore
|
||||||
|
msg.addButton(QtWidgets.QMessageBox.Ok) #type: ignore
|
||||||
|
|
||||||
|
msg.exec()
|
||||||
|
|
||||||
|
clicked = msg.clickedButton()
|
||||||
|
if clicked == copy_btn:
|
||||||
|
clipboard = QtWidgets.QApplication.clipboard()
|
||||||
|
clipboard.setText(url)
|
||||||
|
self.tray.showMessage(s["url_copied_title"], s["url_copied_text"], QtWidgets.QSystemTrayIcon.Information, 2000) #type: ignore
|
||||||
|
elif clicked == open_btn:
|
||||||
|
QtGui.QDesktopServices.openUrl(QtCore.QUrl(url))
|
||||||
|
|
||||||
|
@QtCore.Slot(str, str)
|
||||||
|
def handle_http_download(self, filename: str, client_ip: str):
|
||||||
|
s = self.strings["main_window"]
|
||||||
|
self.tray.showMessage(
|
||||||
|
s["download_notification_title"],
|
||||||
|
s["download_notification_text"].format(filename=filename, ip=client_ip),
|
||||||
|
QtWidgets.QSystemTrayIcon.Information, #type: ignore
|
||||||
|
3000
|
||||||
|
)
|
||||||
|
|
||||||
def show_receive_confirmation(self, sender_ip: str):
|
def show_receive_confirmation(self, sender_ip: str):
|
||||||
reply = QtWidgets.QMessageBox.question(
|
reply = QtWidgets.QMessageBox.question(
|
||||||
@@ -434,6 +568,8 @@ class MainWindow(QtWidgets.QMainWindow):
|
|||||||
def restart_application(self):
|
def restart_application(self):
|
||||||
presence.end()
|
presence.end()
|
||||||
self.dukto_handler.shutdown()
|
self.dukto_handler.shutdown()
|
||||||
|
if self.http_share.is_running():
|
||||||
|
self.http_share.stop()
|
||||||
|
|
||||||
args = [sys.executable] + sys.argv
|
args = [sys.executable] + sys.argv
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user