clients attempt reconnection after socket close
[ppt-control.git] / ppt_control / ppt_control.py
index f942b328f2c2a637ef94015b184a54fc1eaa9941..664866677a3749a5aa48ca271f75a7789c50622f 100755 (executable)
@@ -37,10 +37,10 @@ global refresh_daemon
 global status_label\r
 global http_label\r
 global ws_label\r
+global reset_ppt_button\r
 global http_server\r
 scheduler = None\r
 current_slideshow = None\r
-CACHEDIR = r'''C:\Windows\Temp\ppt-cache'''\r
 interface_root = None\r
 CONFIG_FILE = r'''..\ppt-control.ini'''\r
 LOGFILE = r'''..\ppt-control.log'''\r
@@ -51,12 +51,17 @@ http_label = None
 ws_label = None\r
 ws_daemon = None\r
 http_server = None\r
+reset_ppt_button = None\r
 \r
 \r
 class Handler(server.SimpleHTTPRequestHandler):\r
     def __init__(self, *args, **kwargs):\r
         super().__init__(*args, directory=os.path.dirname(os.path.realpath(__file__)) + r'''\static''')\r
         \r
+    def log_request(self, code='-', size='-'):\r
+        return\r
+\r
+        \r
     def translate_path(self, path):\r
         """Translate a /-separated PATH to the local filename syntax.\r
 \r
@@ -81,12 +86,12 @@ class Handler(server.SimpleHTTPRequestHandler):
             black = 0\r
             if current_slideshow:\r
                 try:\r
-                    path = CACHEDIR + "\\" + current_slideshow.name()\r
+                    path = config.prefs["Main"]["cache"] + "\\" + current_slideshow.name()\r
                 except Exception as e:\r
-                    path = os.path.join(os.path.dirname(os.path.realpath(__file__)), r'''\static\black.jpg''') + '/'\r
+                    path = "black.jpg"\r
                     logger.warning("Failed to get current slideshow name: ", e)\r
             else:\r
-                path = os.path.join(os.path.dirname(os.path.realpath(__file__)), r'''\static\black.jpg''') + '/'\r
+                path = "black.jpg"\r
                 return path\r
             words.pop(0)\r
         else:\r
@@ -103,7 +108,7 @@ class Handler(server.SimpleHTTPRequestHandler):
 \r
 def run_http():\r
     global http_server\r
-    http_server = server.HTTPServer(("", 80), Handler)\r
+    http_server = server.HTTPServer((config.prefs["HTTP"]["interface"], config.prefs.getint("HTTP", "port")), Handler)\r
     http_server.serve_forever()\r
 \r
 STATE_DEFAULT = {"connected": 0, "current": 0, "total": 0, "visible": 0, "name": ""}\r
@@ -190,7 +195,6 @@ async def ws_receive(websocket, path):
                 notify_state()\r
             else:\r
                 logger.error("Received unnsupported event: {}", data)\r
-            message = ""\r
     finally:\r
         USERS.remove(websocket)\r
 \r
@@ -209,7 +213,7 @@ def run_ws():
     ws_queue = asyncio.Queue()\r
     global loop\r
     loop = asyncio.get_event_loop()\r
-    start_server = websockets.serve(ws_handler, "0.0.0.0", 5678, ping_interval=None)\r
+    start_server = websockets.serve(ws_handler, config.prefs["WebSocket"]["interface"], config.prefs.getint("WebSocket", "port"), ping_interval=None)\r
     asyncio.get_event_loop().run_until_complete(start_server)\r
     asyncio.get_event_loop().run_forever()\r
 \r
@@ -226,6 +230,7 @@ def restart_http():
         http_server = None\r
         refresh_status()\r
     start_http()\r
+    time.sleep(0.5)\r
     refresh_status()\r
 \r
 def start_ws():\r
@@ -236,7 +241,7 @@ def start_ws():
     logger.info("Started websocket server")\r
 \r
 class Slideshow:\r
-    def __init__(self, instance):\r
+    def __init__(self, instance, blackwhite):\r
         self.instance = instance\r
         if self.instance is None:\r
             raise ValueError("PPT instance cannot be None")\r
@@ -249,10 +254,13 @@ class Slideshow:
             raise ValueError("PPT instance has no  active presentation")\r
         self.presentation = self.instance.ActivePresentation\r
 \r
+        self.blackwhite = blackwhite\r
+\r
         self.export_current_next()\r
 \r
     def unload(self):\r
         connect_ppt()\r
+        reset_ppt_button.config(state = tk.DISABLED)\r
 \r
     def refresh(self):\r
         try:\r
@@ -336,7 +344,10 @@ class Slideshow:
     def black(self):\r
         try:\r
             self.refresh()\r
-            self.view.State = 3\r
+            if self.blackwhite == "both" and self.view.State == 4:\r
+                self.view.state = 1\r
+            else:\r
+                self.view.State = 3\r
             self.export_current_next()\r
         except (ValueError, pywintypes.com_error):\r
             self.unload()\r
@@ -344,7 +355,10 @@ class Slideshow:
     def white(self):\r
         try:\r
             self.refresh()\r
-            self.view.State = 4\r
+            if self.blackwhite == "both" and self.view.State == 3:\r
+                self.view.state = 1\r
+            else:\r
+                self.view.State = 4\r
             self.export_current_next()\r
         except (ValueError, pywintypes.com_error):\r
             self.unload()\r
@@ -371,21 +385,21 @@ class Slideshow:
         self.export(self.current_slide() + 2)\r
 \r
     def export(self, slide):\r
-        destination = CACHEDIR + "\\" + self.name() + "\\" + str(slide) + ".jpg"\r
+        destination = config.prefs["Main"]["cache"] + "\\" + self.name() + "\\" + str(slide) + ".jpg"\r
         os.makedirs(os.path.dirname(destination), exist_ok=True)\r
         if not os.path.exists(destination) or time.time() - os.path.getmtime(destination) > config.prefs.getint("Main", "cache_timeout"):\r
             if slide <= self.total_slides():\r
                 attempts = 0\r
                 while attempts < 3:\r
                     try:\r
-                        self.presentation.Slides(slide).Export(destination, "JPG")\r
+                        self.presentation.Slides(slide).Export(destination, config.prefs["Main"]["cache_format"])\r
                         break\r
                     except:\r
                         pass\r
                     attempts += 1\r
             elif slide == self.total_slides() + 1:\r
                 try:\r
-                    shutil.copyfileobj(open(os.path.dirname(os.path.realpath(__file__)), r'''\static\black.jpg''', 'rb'), open(destination, 'wb'))\r
+                    shutil.copyfileobj(open(os.path.dirname(os.path.realpath(__file__)) + r'''\static\black.jpg''', 'rb'), open(destination, 'wb'))\r
                 except Exception as e:\r
                     logger.warning("Failed to copy black slide: " + str(e))\r
             else:\r
@@ -416,6 +430,8 @@ def refresh_status():
         status_label.config(text="PowerPoint status: " + ("not " if not STATE["connected"] else "") +  "connected")\r
         http_label.config(text="HTTP server: " + ("not " if http_server is None else "") +  "running")\r
         #ws_label.config(text="WebSocket server: " + ("not " if ws_daemon is not None or not ws_daemon.is_alive() else "") +  "running")\r
+    if reset_ppt_button is not None:\r
+        reset_ppt_button.config(state = tk.DISABLED if not STATE["connected"] else tk.NORMAL)\r
 \r
 def connect_ppt():\r
     global STATE\r
@@ -430,7 +446,7 @@ def connect_ppt():
         try:\r
             instance = get_ppt_instance()\r
             global current_slideshow\r
-            current_slideshow = Slideshow(instance)\r
+            current_slideshow = Slideshow(instance, config.prefs["Main"]["blackwhite"])\r
             STATE["connected"] = 1\r
             STATE["current"] = current_slideshow.current_slide()\r
             STATE["total"] = current_slideshow.total_slides()\r
@@ -463,7 +479,7 @@ def open_settings(_=None):
     global interface_root\r
     interface_root = tk.Tk()\r
     interface_root.protocol("WM_DELETE_WINDOW", on_closing)\r
-    interface_root.iconphoto(False, tk.PhotoImage(file="static/icons/ppt.png"))\r
+    interface_root.iconphoto(False, tk.PhotoImage(file=os.path.dirname(os.path.realpath(__file__)) + r'''\static\icons\ppt.png'''))\r
     interface_root.geometry("600x300+300+300")\r
     app = Interface(interface_root)\r
     interface_thread = threading.Thread(target=interface_root.mainloop())\r
@@ -489,6 +505,7 @@ class Interface(ttk.Frame):
         global status_label\r
         global http_label\r
         global ws_label\r
+        global reset_ppt_button\r
         self.parent.title("ppt-control")\r
         self.style = ttk.Style()\r
         #self.style.theme_use("default")\r
@@ -503,6 +520,7 @@ class Interface(ttk.Frame):
         save_button.place(x=400, y=280)\r
 \r
         reset_ppt_button = ttk.Button(self, text="Reconnect", command=connect_ppt)\r
+        reset_ppt_button.config(state = tk.DISABLED)\r
         reset_ppt_button.place(x=300, y=10)\r
 \r
         reset_http_button = ttk.Button(self, text="Restart", command=restart_http)\r
@@ -523,13 +541,21 @@ class Interface(ttk.Frame):
         refresh_status()\r
         \r
         \r
+def exit_action(icon):\r
+    logger.debug("User requested shutdown")\r
+    icon.visible = False\r
+    icon.stop()\r
 \r
 def show_icon():\r
     logger.debug("Starting system tray icon")\r
-    menu = (pystray.MenuItem("Status", lambda: null_action(), enabled=False),\r
+    icon = pystray.Icon("ppt-control")\r
+    icon.menu = (pystray.MenuItem("Status", lambda: null_action(), enabled=False),\r
             pystray.MenuItem("Restart", lambda: start()),\r
-            pystray.MenuItem("Settings", lambda: open_settings()))\r
-    icon = pystray.Icon("ppt-control", Image.open(os.path.dirname(os.path.realpath(__file__)) + r'''\static\icons\ppt.ico'''), "ppt-control", menu)\r
+            pystray.MenuItem("Stop", lambda: exit_action(icon)),\r
+            pystray.MenuItem("Settings", lambda: open_settings())\r
+            )\r
+    icon.icon = Image.open(os.path.dirname(os.path.realpath(__file__)) + r'''\static\icons\ppt.ico''')\r
+    icon.title = "ppt-control"\r
     icon.visible = True\r
     icon.run(setup=start)\r
 \r
@@ -565,7 +591,7 @@ def start_interface():
 \r
     #logging.getLogger("asyncio").setLevel(logging.ERROR)\r
     #logging.getLogger("asyncio.coroutines").setLevel(logging.ERROR)\r
-    #logging.getLogger("websockets.server").setLevel(logging.ERROR)\r
+    logging.getLogger("websockets.server").setLevel(logging.ERROR)\r
     #logging.getLogger("websockets.protocol").setLevel(logging.ERROR)\r
 \r
 \r
@@ -573,6 +599,7 @@ def start_interface():
 \r
     # Start systray icon and server\r
     show_icon()\r
+    sys.exit(0)\r
 \r
 if __name__ == "__main__":\r
     start_interface()\r