MainMenusDialogs.py 6.77 KB
Newer Older
Hakan Dündar's avatar
Hakan Dündar committed
1 2 3 4 5
#!/usr/bin/env python3

# ----------------------------------- MainMenusGUI - Main Menus GUI Import Function (contains import code of this module in order to avoid running them during module import) -----------------------------------
def main_menus_gui_import_func():

6
    global Gtk, os
Hakan Dündar's avatar
Hakan Dündar committed
7 8 9 10 11 12 13

    import gi
    gi.require_version('Gtk', '3.0')
    from gi.repository import Gtk
    import os


14 15
    global Config
    import Config
Hakan Dündar's avatar
Hakan Dündar committed
16 17 18 19 20


# ----------------------------------- MainMenusGUI - Main Menus GUI Function (the code of this module in order to avoid running them during module import and defines GUI functions/signals) -----------------------------------
def main_menus_gui_func():

21 22 23
    # Define builder and get all objects (Main Menu GUI and About Dialog) from GUI file.
    builder = Gtk.Builder()
    builder.add_from_file(os.path.dirname(os.path.realpath(__file__)) + "/../ui/MainMenusDialogs.ui")
Hakan Dündar's avatar
Hakan Dündar committed
24 25


26 27 28
    # ********************** Define object names for Main Menu GUI and About Dialog **********************
    global menu1001m
    global menuitem1002m, menuitem1003m, menuitem1004m, menuitem1005m, menuitem1006m, checkmenuitem1001m
Hakan Dündar's avatar
Hakan Dündar committed
29

30
    global aboutdialog1001d
Hakan Dündar's avatar
Hakan Dündar committed
31

32
    # ********************** Get objects for Main Menu GUI and About Dialog **********************
33 34 35 36 37 38 39
    menu1001m = builder.get_object('menu1001m')
    menuitem1002m = builder.get_object('menuitem1002m')
    menuitem1003m = builder.get_object('menuitem1003m')
    menuitem1004m = builder.get_object('menuitem1004m')
    menuitem1005m = builder.get_object('menuitem1005m')
    menuitem1006m = builder.get_object('menuitem1006m')
    checkmenuitem1001m = builder.get_object('checkmenuitem1001m')
Hakan Dündar's avatar
Hakan Dündar committed
40

41
    aboutdialog1001d = builder.get_object('aboutdialog1001d')
Hakan Dündar's avatar
Hakan Dündar committed
42 43


44
    # ********************** Define object functions for Main Menu GUI **********************
Hakan Dündar's avatar
Hakan Dündar committed
45 46 47 48 49 50 51 52 53
    def on_menu1001m_show(widget):
        checkmenuitem1001m.disconnect_by_func(on_checkmenuitem1001m_toggled)                  # Disconnect "on_checkmenuitem1001m_toggled" function in order to prevent it from sending event signals when toggling is performed by the code for reflecting the user preference about "Floating Window".
        if Config.show_floating_summary == 0:
            checkmenuitem1001m.set_active(False)
        if Config.show_floating_summary == 1:
            checkmenuitem1001m.set_active(True)
        checkmenuitem1001m.connect("toggled", on_checkmenuitem1001m_toggled)

    def on_menuitem1002m_activate(widget):                                                    # "Open Terminal" menu item
54 55 56
        if 'subprocess' not in globals():
            global subprocess
            import subprocess
57 58 59
        if 'Thread' not in globals():
            global Thread
            from threading import Thread
60 61
        def main_menus_gui_open_terminal_func():
            (subprocess.check_output(["x-terminal-emulator", "-e", "/bin/bash"], shell=False)).decode()
Hakan Dündar's avatar
Hakan Dündar committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
        open_terminal_thread = Thread(target=main_menus_gui_open_terminal_func, daemon=True).start()    # Terminal is run in another thread in order not to wait end of the run which occurs in single threaded code execution.

    def on_checkmenuitem1001m_toggled(widget):                                                # "Floating Summary" menu item
        if "FloatingSummary" not in globals():                                                # Floating Summary window might have been opened on the application start and user may want to hide it from the Main Menu of the application. Existance check of the "FloatingSummary" variable is performed before the "if checkmenuitem1001m.get_active() == False:" statement in order to avoid errors of FloatingSummary not defined.
            global FloatingSummary
            import FloatingSummary
        if checkmenuitem1001m.get_active() == True:
            FloatingSummary.floating_summary_import_func()
            FloatingSummary.floating_summary_initial_func()
            FloatingSummary.floating_summary_thread_run_func()
            FloatingSummary.floating_summary_window.show()
            Config.show_floating_summary = 1
        if checkmenuitem1001m.get_active() == False:
            FloatingSummary.floating_summary_window.hide()
            Config.show_floating_summary = 0
        Config.config_save_func()

    def on_menuitem1003m_activate(widget):                                                    # "Restart as Root" menu item
80 81 82 83 84 85
        if 'signal' not in globals():
            global signal
            import signal
        if 'Thread' not in globals():
            global Thread
            from threading import Thread
Hakan Dündar's avatar
Hakan Dündar committed
86
        def restart_as_root():                                                                # Running action is performed in a separate thread for letting rest of the function code to be run without waiting closing the new opened application.
87
           os.system("pkexec system-monitoring-center")                                       # For running application as root by using polkit authentication window
Hakan Dündar's avatar
Hakan Dündar committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
        restart_as_root_thread = Thread(target=restart_as_root, daemon=True).start()          # Define a thread and run it
        os.kill(os.getpid(), signal.SIGTERM)                                                  # Get PID of the current application and end it

    def on_menuitem1004m_activate(widget):                                                    # "Settings" menu item
        if "SettingsGUI" not in globals():                                                    # Settings module is imported and the following functions are run only one time during application run. This statement is used in order to avoid them running on every window opening.
            global SettingsGUI
            import SettingsGUI
            SettingsGUI.settings_gui_import_func()
            SettingsGUI.settings_gui_func()
        SettingsGUI.window2001.show()

    def on_menuitem1005m_activate(widget):                                                    # "About" menu item
        try:
            software_version = open(os.path.dirname(os.path.abspath(__file__)) + "/__version__").readline()
        except:
            pass
        aboutdialog1001d.set_version(software_version)
        aboutdialog1001d.run()
        aboutdialog1001d.hide()

    def on_menuitem1006m_activate(widget):                                                    # "Quit" menu item
        Gtk.main_quit()


112
    # ********************** Connect signals to GUI objects for Main Menu GUI **********************
Hakan Dündar's avatar
Hakan Dündar committed
113 114 115 116 117 118 119
    menu1001m.connect("show", on_menu1001m_show)
    menuitem1002m.connect("activate", on_menuitem1002m_activate)
    checkmenuitem1001m.connect("toggled", on_checkmenuitem1001m_toggled)
    menuitem1003m.connect("activate", on_menuitem1003m_activate)
    menuitem1004m.connect("activate", on_menuitem1004m_activate)
    menuitem1005m.connect("activate", on_menuitem1005m_activate)
    menuitem1006m.connect("activate", on_menuitem1006m_activate)