source: ogClient-Git/src/linux/ogOperations.py @ 69d214f

Last change on this file since 69d214f was 69d214f, checked in by Jose M. Guisado <jguisado@…>, 3 years ago

#1065 linux: add systray icon

Adds a systray icon for linux mode. Uses pystray module. Expects a
favicon.ico stored in the same folder as the main ogclient python
script, but if not found a placeholder image is used.

  • Property mode set to 100644
File size: 2.4 KB
RevLine 
[2d3d31b]1#
2# Copyright (C) 2021 Soleta Networks <info@soleta.eu>
3#
4# This program is free software: you can redistribute it and/or modify it under
5# the terms of the GNU Affero General Public License as published by the
6# Free Software Foundation; either version 3 of the License, or
7# (at your option) any later version.
8
9import os
[69d214f]10from pystray import Icon, Menu, MenuItem
11import multiprocessing as mp
12from multiprocessing import Process
13from PIL import Image, ImageDraw
14
[2d3d31b]15from src.ogRest import ThreadState
16
17
[69d214f]18def _create_default_image():
19    """
20    Creates a default image for the tray icon. Use in case
21    no favicon.ico is found.
22    """
23    width = height = 250
24    color1 = (255, 255, 255)
25    color2 = (255, 0, 255)
26
27    image = Image.new('RGB', (width, height), color1)
28    dc = ImageDraw.Draw(image)
29    dc.rectangle(
30        (width // 2, 0, width, height // 2),
31        fill=color2)
32    dc.rectangle(
33        (0, height // 2, width // 2, height),
34        fill=color2)
35
36    return image
37
38
39def create_image():
40    try:
41        image = Image.open(r'./favicon.ico')
42        image = Image.composite(image, Image.new('RGB', image.size, 'white'), image)
43    except:
44        image = _create_default_image()
45    return image
46
47
48def create_systray():
49    menu = Menu(MenuItem('Powered by Soleta Networks!',
50                         lambda icon, item: 1))
51    icon = Icon('ogClient', create_image(), menu=menu)
52    assert icon.icon
53    icon.run()
54
55
56systray_p = Process(target=create_systray)
57
58
[2d3d31b]59class OgLinuxOperations:
60
[69d214f]61    def __init__(self):
62        mp.set_start_method('spawn')
63        systray_p.start()
64
[2d3d31b]65    def _restartBrowser(self, url):
66        raise NotImplementedError
67
68    def poweroff(self):
[69d214f]69        systray_p.terminate()
[2d3d31b]70        os.system('systemctl poweroff')
71
72    def reboot(self):
[69d214f]73        systray_p.terminate()
[2d3d31b]74        os.system('systemctl reboot')
75
76    def shellrun(self, request, ogRest):
77        raise NotImplementedError
78
79    def session(self, request, ogRest):
80        raise NotImplementedError
81
82    def hardware(self, path, ogRest):
83        raise NotImplementedError
84
85    def setup(self, request, ogRest):
86        raise NotImplementedError
87
88    def image_restore(self, request, ogRest):
89        raise NotImplementedError
90
91    def image_create(self, path, request, ogRest):
92        raise NotImplementedError
93
94    def refresh(self, ogRest):
95        return {"status": "LINUX"}
96
97    def probe(self, ogRest):
98        return {'status': 'LINUX' if ogRest.state != ThreadState.BUSY else 'BSY'}
Note: See TracBrowser for help on using the repository browser.