From 0f6b17e8cdd00acce0ac5c148fc9daa9c676d8da Mon Sep 17 00:00:00 2001 From: j Date: Mon, 1 Feb 2016 17:27:30 +0530 Subject: [PATCH] close tray icon if oml is gone --- trayicon/Open Media Library.py | 34 ++++++++++++++++++++++++++++++---- trayicon/install.py | 6 ++++++ trayicon/setup.py | 2 +- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/trayicon/Open Media Library.py b/trayicon/Open Media Library.py index 61d626e..afa1cc7 100644 --- a/trayicon/Open Media Library.py +++ b/trayicon/Open Media Library.py @@ -4,6 +4,8 @@ import signal import subprocess import sys import webbrowser +import time +from threading import Thread import win32api import win32con @@ -23,7 +25,8 @@ class OMLTrayIcon(object): FIRST_ID = 1023 def __init__(self): - launch() + self.thread_id = win32api.GetCurrentThreadId() + launch(self) name = "Open Media Library" self.icon = "ico/oml.ico" self.hover_text = name @@ -197,9 +200,30 @@ class OMLTrayIcon(object): else: menu_action(self) -def check_pid(pid): + def quit(self): + nid = (self.hwnd, 0) + win32gui.Shell_NotifyIcon(win32gui.NIM_DELETE, nid) + win32api.PostThreadMessage(self.thread_id, win32con.WM_QUIT, 0, 0) + +class Check(Thread): + def __init__(self, parent, pid): + self.parent = parent + self.pid = pid + Thread.__init__(self) + self.daemon = True + self.start() + + def run(self): + time.sleep(60) + while check_pid(self.pid): + time.sleep(60) + self.parent.quit() + +def check_pid(pidfile): + if not os.path.exists(pidfile): + return False try: - with open(pid) as fd: + with open(pidfile) as fd: pid = int(fd.read()) except: return False @@ -213,13 +237,15 @@ def check_pid(pid): def launch(sysTrayIcon=None): base = os.path.join(os.getenv('LOCALAPPDATA'), 'Open Media Library') pid = os.path.join(base, 'data', 'openmedialibrary.pid') - if os.path.exists(pid) and check_pid(pid): + if check_pid(pid): webbrowser.open_new_tab(os.path.join(base, 'openmedialibrary', 'static', 'html', 'load.html')) + sysTrayIcon._check = Check(sysTrayIcon, pid) elif os.path.exists(base): python = os.path.join(base, 'platform_win32', 'pythonw.exe') oml = os.path.join(base, 'openmedialibrary') subprocess.Popen([python, 'oml', 'server', pid], cwd=oml, start_new_session=True) webbrowser.open_new_tab(os.path.join(base, 'openmedialibrary', 'static', 'html', 'load.html')) + sysTrayIcon._check = Check(sysTrayIcon, pid) else: install.run(base) diff --git a/trayicon/install.py b/trayicon/install.py index bb3e567..528a3dc 100644 --- a/trayicon/install.py +++ b/trayicon/install.py @@ -100,6 +100,12 @@ class Install(Thread): self.status["release"] = release self.status["progress"] = 0 platform = get_platform() + if not 'platform_win32' in release: + release['platform_win32'] = { + 'name': 'platform_win32-20160201-3-3d473b8.tar.bz2', + 'version': '20160201-3-3d473b8.tar.bz2', + 'platform': 'win32', + } for module in sorted(release['modules']): if release['modules'][module].get('platform', platform) == platform: package_tar = release['modules'][module]['name'] diff --git a/trayicon/setup.py b/trayicon/setup.py index d7eb45e..3389b05 100644 --- a/trayicon/setup.py +++ b/trayicon/setup.py @@ -38,7 +38,7 @@ oml = Executable( setup( name = "Open Media Library", version = "0.1", - description = "share media collectoins", + description = "Open Media Library", options = { "build_exe": build_exe_options, "bdist_msi": bdist_msi_options