If there is an existing onionshare-gui process, open a new tab and quit

This commit is contained in:
Micah Lee 2019-11-28 12:35:57 -08:00
parent e7c683528d
commit 58bc258507
No known key found for this signature in database
GPG Key ID: 403C2657CD994F73
2 changed files with 35 additions and 28 deletions

View File

@ -22,3 +22,4 @@ stem==1.7.1
urllib3==1.25.3
Werkzeug==0.15.6
watchdog==0.9.0
psutil==5.6.7

View File

@ -23,11 +23,10 @@ import sys
import platform
import argparse
import signal
import json
import psutil
from PyQt5 import QtCore, QtWidgets
if platform.system() == "Linux":
import psutil
from onionshare.common import Common
from .gui_common import GuiCommon
@ -110,6 +109,9 @@ def main():
# Verbose mode?
common.verbose = verbose
# Attach the GUI common parts to the common object
common.gui = GuiCommon(common, qtapp, local_only)
# Validation
if filenames:
valid = True
@ -124,7 +126,6 @@ def main():
sys.exit()
# Is there another onionshare-gui running?
if common.platform == "Linux":
existing_pid = None
for proc in psutil.process_iter(attrs=["pid", "name", "cmdline"]):
if proc.info["pid"] == os.getpid():
@ -138,19 +139,24 @@ def main():
if proc.info["cmdline"] and len(proc.info["cmdline"]) >= 2:
if (
os.path.basename(proc.info["cmdline"][0]).lower() == "python"
and os.path.basename(proc.info["cmdline"][1])
== "onionshare-gui"
and os.path.basename(proc.info["cmdline"][1]) == "onionshare-gui"
):
existing_pid = proc.info["pid"]
break
if existing_pid:
print(f"Opening tab in existing OnionShare window (pid {proc.info['pid']})")
# TODO: open tab
return
# Attach the GUI common parts to the common object
common.gui = GuiCommon(common, qtapp, local_only)
# Make an event for the existing OnionShare window
if filenames:
obj = {"type": "new_share_tab", "filenames": filenames}
else:
obj = {"type": "new_tab"}
# Write that event to disk
with open(common.gui.events_filename, "a") as f:
f.write(json.dumps(obj) + "\n")
return
# Launch the gui
main_window = MainWindow(common, filenames)