Skip to content

Commit

Permalink
Smaller cleanups and fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lennolium committed Oct 11, 2023
1 parent 088c982 commit 97e6c42
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 144 deletions.
9 changes: 0 additions & 9 deletions ACKNOWLEDGMENTS
Original file line number Diff line number Diff line change
Expand Up @@ -217,15 +217,6 @@ Copyright (c) Python Software Foundation, Kenneth Reitz, Cory Benfield, Ian Cord
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.


darkdetect:
________________________________________________________________________
Copyright (c) 2019, Alberto Sottile All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of "darkdetect" nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL "Alberto Sottile" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


PySide6:
________________________________________________________________________
Qt is available under the GNU Lesser General Public License version 3.
Expand Down
2 changes: 1 addition & 1 deletion pyinstaller.spec
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,6 @@ app = BUNDLE(
'CFBundleVersion': build_version,
'NSHumanReadableCopyright': 'Copyright © 2023, Lennart Haack.',
'LSApplicationCategoryType': 'public.app-category.utilities',
'NSAppleEventsUsageDescription': 'swiftGuard is requesting access to System Events to provide the ability to shutdown and hibernate your Mac.\nThis permission will NEVER be used for any other purpose.',
'NSAppleEventsUsageDescription': 'swiftGuard is requesting access to System Events to provide the ability to shutdown and hibernate your Mac.\nThis permission will NEVER be used for any other purposes.',
},
)
115 changes: 57 additions & 58 deletions src/swiftguard/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,44 +370,47 @@ class TrayApp:
:ivar app: The main Qt application instance.
:type app: QApplication
:ivar theme: The current system theme (e.g., Light or Dark).
:type theme: str
:ivar resources: The resource paths for icons and images based on
the current theme.
:ivar style_hints: Style hints for real-time theme changes.
:type style_hints: QStyleHints
:ivar resources: Resource paths for icons and images based on the
current theme.
:type resources: dict | None
:ivar start_devices_count: A Counter object to keep track of
initially connected USB devices.
:type start_devices_count: collections.Counter
:ivar allowed_devices_count: A Counter object to keep track of
allowed USB devices.
:type allowed_devices_count: collections.Counter
:ivar current_devices_count: A Counter object to keep track of
currently connected USB devices.
:type current_devices_count: collections.Counter
:ivar config: Application configuration.
:type config: configparser.ConfigParser
:ivar app_icon: The system tray icon.
:type app_icon: QSystemTrayIcon
:ivar listen_usb: USB device listener.
:type listen_usb: listeners.ListenerUSB
:ivar listen_usb_thread: Thread for USB device listener.
:type listen_usb_thread: QThread
:methods:
- usb_worker_handle: Start or stop the USB worker thread for
monitoring connected devices.
- __init__: Initialize the TrayApp instance and set up the
system tray icon.
- menu_devices_update: Update the device menu based on connected
and allowed devices.
- whitelist_update: Add or remove devices from the whitelist.
- worker_handle: Start or stop the main worker thread for
manipulation detection.
- defuse: Defuse the manipulation detection and reset the alarm.
- manipulation: Handle manipulation detection and display
the alarm.
- defuse: Reset the alarm and return to normal monitoring mode.
- manipulation: Handle manipulation detection and display the
alarm.
- config_update: Update the application configuration based on
user settings.
- theme_worker_handle: Start or stop the theme listener thread.
- theme_listener: Listen for changes in the system theme and
update application resources accordingly.
user input.
- theme_update: Update the application theme based on system
settings.
- update_box: Display an update message if a new version is
available.
- create_tray_icon: Create and configure the system tray icon
and its menu.
- help: Display a help message with instructions for using
the application.
- about: Display an about message with information about the
application and its author.
- exit_handler: Handle application exit and cleanup.
- acknowledgments: Display acknowledgments for third-party
libraries used.
- help: Display instructions for using the program.
- about: Display information about the application and its
author.
- handle_exception: Custom exception handler for uncaught
exceptions.
- exit_handler: Handle application exit and cleanup tasks.
:param None:
:return: None
Expand Down Expand Up @@ -467,6 +470,7 @@ def __init__(self):
self.worker_thread = None
self.worker_handle("Guarding")

self.menu_tray = None
self.menu_settings = None
self.submenu = None
self.menu_enabled = None
Expand All @@ -478,8 +482,7 @@ def __init__(self):

# Create the listener for USB devices (for dynamic menu update).
listeners.Listeners.config = self.config
listeners.Listeners.intervall = 10
self.listen_usb = listeners.ListenerUSB()
self.listen_usb = listeners.ListenerUSB(intervall=1000)

# If a new device is connected, update the device menu.
self.listen_usb.triggered.connect(self.menu_devices_update)
Expand All @@ -504,19 +507,18 @@ def __init__(self):
if new_vers := check_updates():
self.update_box(new_vers)

# self.update_box("0.2.0")

def menu_devices_update(self):
"""
Update the device menu based on connected and allowed devices.
This function updates the device menu in the system tray based
on the currently connected USB devices and the devices allowed
in the whitelist. It can be called at startup and when devices
change.
:param startup: A boolean indicating whether the update is
occurring at application startup.
:type startup: bool
The menu_devices_update function updates the device menu in the
system tray based on the currently connected USB devices and
the devices allowed in the whitelist. It can be called at
startup and when devices change.
:param self: Refer to the instance of the class
:return: None
"""

Expand Down Expand Up @@ -847,8 +849,8 @@ def update_box(self, new_vers):

# Bold text.
msg_box.setText(
f"Update available!\n\n"
f"Installed version: {__version__}\nLatest version: "
f"Update Available!\n\n"
f"Currently Installed: {__version__}\nLatest Release: "
f"{new_vers}\n"
)

Expand Down Expand Up @@ -890,8 +892,6 @@ def update_box(self, new_vers):
self.config["Application"]["check_updates"] = "0"
conf.write(self.config)

# TODO: implement focus on dialog / move dialog to front.

def create_tray_icon(self):
"""
Create and configure the system tray icon and its menu.
Expand All @@ -910,11 +910,10 @@ def create_tray_icon(self):

tray_icon.setIcon(QIcon(self.resources["app-icon"]))

menu_tray = QMenu()
self.menu_tray = menu_tray
self.menu_tray = QMenu()

# Create a submenu for "Devices" initially disabled.
self.submenu = menu_tray.addMenu("Devices")
self.submenu = self.menu_tray.addMenu("Devices")
self.submenu.setIcon(QIcon(self.resources["usb-connection"]))
submenu_dummy = QAction("Searching ...", self.submenu)
submenu_dummy.setEnabled(False)
Expand All @@ -936,24 +935,24 @@ def create_tray_icon(self):
self.menu_enabled.entry.setVisible(False)
else:
self.menu_enabled.entry.setVisible(True)
menu_tray.addAction(self.menu_enabled.entry)
self.menu_tray.addAction(self.menu_enabled.entry)

# Create hidden "Manipulation" menu item.
self.menu_tamper = QAction("Manipulation", menu_tray)
self.menu_tamper = QAction("Manipulation", self.menu_tray)
self.menu_tamper.setIcon(QIcon(self.resources["shield-tamper"]))
if self.worker.tampered_var:
self.menu_tamper.setVisible(True)
else:
self.menu_tamper.setVisible(False)
self.menu_tamper.setShortcut(QKeySequence(Qt.CTRL | Qt.Key_E))
menu_tray.addAction(self.menu_tamper)
self.menu_tray.addAction(self.menu_tamper)
self.menu_tamper.triggered.connect(self.defuse)

menu_tray.addSeparator()
self.menu_tray.addSeparator()

# Create a "Settings" menu.
self.menu_settings = QMenu(" Settings", menu_tray)
menu_tray.addMenu(self.menu_settings)
self.menu_settings = QMenu(" Settings", self.menu_tray)
self.menu_tray.addMenu(self.menu_settings)

# Create an "Action" submenu within "Settings."
entry01 = ToggleEntry(
Expand Down Expand Up @@ -1028,25 +1027,25 @@ def create_tray_icon(self):
self.menu_settings.addAction(entry09.entry)

# Create "Help" menu item.
menu_help = QAction(" Help", menu_tray)
menu_tray.addAction(menu_help)
menu_help = QAction(" Help", self.menu_tray)
self.menu_tray.addAction(menu_help)
menu_help.triggered.connect(self.help)

# Create "About" menu item.
menu_about = QAction(" About", menu_tray)
menu_tray.addAction(menu_about)
menu_about = QAction(" About", self.menu_tray)
self.menu_tray.addAction(menu_about)
menu_about.triggered.connect(self.about)

menu_tray.addSeparator()
self.menu_tray.addSeparator()

# Create an "Exit" menu item and connect it to the exit handler.
menu_exit = QAction(" Exit", menu_tray)
menu_exit = QAction(" Exit", self.menu_tray)
menu_exit.setShortcut(QKeySequence(Qt.CTRL | Qt.Key_Q))
menu_exit.triggered.connect(self.exit_handler)
menu_tray.addAction(menu_exit)
self.menu_tray.addAction(menu_exit)

# Set the system tray menu.
tray_icon.setContextMenu(menu_tray)
tray_icon.setContextMenu(self.menu_tray)

return tray_icon

Expand Down
9 changes: 0 additions & 9 deletions src/swiftguard/resources/ACKNOWLEDGMENTS
Original file line number Diff line number Diff line change
Expand Up @@ -217,15 +217,6 @@ Copyright (c) Python Software Foundation, Kenneth Reitz, Cory Benfield, Ian Cord
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.


darkdetect:
________________________________________________________________________
Copyright (c) 2019, Alberto Sottile All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of "darkdetect" nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL "Alberto Sottile" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


PySide6:
________________________________________________________________________
Qt is available under the GNU Lesser General Public License version 3.
Expand Down
22 changes: 10 additions & 12 deletions src/swiftguard/utils/autostart.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import os
import shutil

from swiftguard.const import APP_PATH, CURRENT_PLATFORM, USER_HOME
from swiftguard import const

# Child logger.
LOGGER = logging.getLogger(__name__)
Expand All @@ -29,10 +29,8 @@
def add_autostart():
# TODO: docstring.
# macOS: Create launch agent.
if CURRENT_PLATFORM.startswith("DARWIN"):
launch_agent_dest = (
f"{USER_HOME}/Library/LaunchAgents/dev.lennolium.swiftguard.plist"
)
if const.CURRENT_PLATFORM.startswith("DARWIN"):
launch_agent_dest = f"{const.USER_HOME}/Library/LaunchAgents/dev.lennolium.swiftguard.plist"
try:
# Create LaunchAgents directory if it does not exist.
if not os.path.isdir(os.path.dirname(launch_agent_dest)):
Expand All @@ -47,7 +45,9 @@ def add_autostart():
if not os.path.isfile(launch_agent_dest):
shutil.copy(
os.path.join(
APP_PATH, "install", "dev.lennolium.swiftguard.plist"
const.APP_PATH,
"install",
"dev.lennolium.swiftguard.plist",
),
launch_agent_dest,
)
Expand All @@ -58,8 +58,8 @@ def add_autostart():
except Exception as e:
LOGGER.error(
f"Autostart could not be configured. Could not copy "
f"launch agent plist from {APP_PATH} to {launch_agent_dest}.\n"
f"Error: {str(e)}"
f"launch agent plist from {const.APP_PATH} to "
f"{launch_agent_dest}.\nError: {str(e)}"
)

return False
Expand Down Expand Up @@ -100,10 +100,8 @@ def add_autostart():

def del_autostart():
# macOS: Delete launch agent.
if CURRENT_PLATFORM.startswith("DARWIN"):
launch_agent_dest = (
f"{USER_HOME}/Library/LaunchAgents/dev.lennolium.swiftguard.plist"
)
if const.CURRENT_PLATFORM.startswith("DARWIN"):
launch_agent_dest = f"{const.USER_HOME}/Library/LaunchAgents/dev.lennolium.swiftguard.plist"

# If the launch agent exists, delete it.
if os.path.isfile(launch_agent_dest):
Expand Down
Loading

0 comments on commit 97e6c42

Please sign in to comment.