Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Enable/disable autostart in tray #950

Closed
munix9 opened this issue May 25, 2023 · 12 comments
Closed

[Feature Request] Enable/disable autostart in tray #950

munix9 opened this issue May 25, 2023 · 12 comments
Labels
feature a whole new feature

Comments

@munix9
Copy link
Contributor

munix9 commented May 25, 2023

Enable/disable autostart in tray.

Summary:

I have created a patch to be able to enable/disable autostart in the tray.
It is a proof of concept (hack!) and some things can certainly be implemented better.

The following paths and files are defined as prerequisites for the checks and activation:
/etc/xdg/autostart/opensnitch_ui.desktop (optional)
~/.config/autostart/opensnitch_ui.desktop
/usr/share/applications/opensnitch_ui.desktop

This should all be standard, it works fine on openSUSE Tumbleweed (KDE Plasma, X11).

Maybe it can be used as a template to implement the functionality.

--- a/ui/opensnitch/service.py
+++ b/ui/opensnitch/service.py
@@ -181,11 +181,57 @@
         self._menu_enable_fw = self._menu.addAction(self.MENU_ENTRY_FW_DISABLE)
         self._menu_enable_fw.setEnabled(False)
         self._menu_enable_fw.triggered.connect(self._on_enable_interception_clicked)
+
+        self._menu.addSeparator()
+        self._menu_autostart = self._menu.addAction("Autostart")
+        self._menu_autostart.setCheckable(True)
+        self._menu_autostart.setChecked(self._is_autostart_enabled())
+        self._menu_autostart.triggered.connect(self._on_switch_autostart)
+        self._menu.addSeparator()
+
         self._menu.addAction(self.MENU_ENTRY_HELP).triggered.connect(
                 lambda: QtGui.QDesktopServices.openUrl(QtCore.QUrl(Config.HELP_CONFIG_URL))
                 )
         self._menu.addAction(self.MENU_ENTRY_CLOSE).triggered.connect(self._on_close)
 
+    def _is_autostart_enabled(self):
+        desktopFile = "opensnitch_ui.desktop"
+        systemAutostart = "/etc/xdg/autostart/" + desktopFile
+        if not os.path.isfile(systemAutostart) and os.path.isfile("/usr" + systemAutostart):
+            systemAutostart = "/usr" + systemAutostart
+        userAutostart = os.environ['HOME'] + "/.config/autostart/" + desktopFile
+        if os.path.isfile(userAutostart):
+            retVal = os.system("grep -qsi \"^Hidden=true\" " + userAutostart)
+            if retVal != 0:
+                return True
+        elif os.path.isfile(systemAutostart):
+            return True
+
+        return False
+
+    def _on_switch_autostart(self):
+        desktopFile = "opensnitch_ui.desktop"
+        systemDesktop = "/usr/share/applications/" + desktopFile
+        systemAutostart = "/etc/xdg/autostart/" + desktopFile
+        if not os.path.isfile(systemAutostart) and os.path.isfile("/usr" + systemAutostart):
+            systemAutostart = "/usr" + systemAutostart
+        userAutostart = os.environ['HOME'] + "/.config/autostart/" + desktopFile
+
+        if not os.path.exists(os.path.dirname(userAutostart)):
+            os.system("mkdir -p " + os.path.dirname(userAutostart))
+
+        if self._menu_autostart.isChecked():
+            if os.path.isfile(systemAutostart) and os.path.isfile(userAutostart):
+                os.remove(userAutostart)
+            else:
+                os.system("cp " + systemDesktop + " " + userAutostart)
+        else:
+            if os.path.isfile(systemAutostart):
+                os.system("cp " + systemDesktop + " " + userAutostart)
+                os.system("echo 'Hidden=true' >> " + userAutostart)
+            elif os.path.isfile(userAutostart):
+                os.remove(userAutostart)
+
     def _show_gui_if_tray_not_available(self):
         """If the system tray is not available or ready, show the GUI after
         10s. This delay helps to skip showing up the GUI when DEs' autologin is on.
@munix9 munix9 added the feature a whole new feature label May 25, 2023
@swx-1
Copy link

swx-1 commented May 25, 2023

I am using Arch KDE and by default it autostarts in tray?

.config/autostart/opensnitch_ui.desktop:

[Desktop Entry]
Categories=System;Security;Monitor;Network;
Comment=Interactive application firewall
Exec=/bin/sh -c "pkill -15 opensnitch-ui; opensnitch-ui"
GenericName=OpenSnitch Firewall
Icon=opensnitch-ui
Keywords=system;firewall;policies;security;polkit;policykit;
Name=OpenSnitch
NoDisplay=false
Terminal=false
Type=Application
X-GNOME-Autostart-Delay=3
X-GNOME-Autostart-enabled=true

@munix9
Copy link
Contributor Author

munix9 commented May 25, 2023

I am using Arch KDE and by default it autostarts in tray?

.config/autostart/opensnitch_ui.desktop:

[Desktop Entry]
Categories=System;Security;Monitor;Network;
Comment=Interactive application firewall
Exec=/bin/sh -c "pkill -15 opensnitch-ui; opensnitch-ui"
GenericName=OpenSnitch Firewall
Icon=opensnitch-ui
Keywords=system;firewall;policies;security;polkit;policykit;
Name=OpenSnitch
NoDisplay=false
Terminal=false
Type=Application
X-GNOME-Autostart-Delay=3
X-GNOME-Autostart-enabled=true

Yes, autostart already works.
But the point here is to be able to enable/disable autostart as a user via the GUI.

@gustavo-iniguez-goya
Copy link
Collaborator

hi @munix9 !

The only problem I see is the path /etc/xdg/autostart/opensnitch_ui.desktop which requires root privileges.

Anyway, this looks more to me like a Settings option, wouldn't it make more sense to add it to the Preferences->UI dialog?

@munix9
Copy link
Contributor Author

munix9 commented May 25, 2023

hi @munix9 !

The only problem I see is the path /etc/xdg/autostart/opensnitch_ui.desktop which requires root privileges.

Nope, that's not a problem:

/etc/xdg/autostart/opensnitch_ui.desktop should be provided by the package creator (and by make install?) and may belong to root without problems.
/etc/xdg/autostart/opensnitch_ui.desktop is the central autostart file which says that every user gets opensnitch-ui started after login.

The only place that is write-accessed is ~/.config/autostart/opensnitch_ui.desktop and that belongs to the logged in user, so no problem there either.

The following applies:
If there is a corresponding desktop file under /etc/xdg/autostart/, then the corresponding program is started after login.
If there is also the file in the directory ~/.config/autostart/, but with the additional line Hidden=True, then the program is not started - the autostart is therefore disabled.

In the patch, the presence of the file /etc/xdg/autostart/opensnitch_ui.desktop is not strictly necessary, but it is still taken into account.

Anyway, this looks more to me like a Settings option, wouldn't it make more sense to add it to the Preferences->UI dialog?

Sure, or in both places? In the tray it is faster to find, but is certainly a matter of taste.

Background: It's been my experience that users don't necessarily like it when an option can't be disabled - at least simply via the GUI.

Via console it is feasible, sure, but not every user feels comfortable acting in a console.

@munix9
Copy link
Contributor Author

munix9 commented May 25, 2023

In debian the file /etc/xdg/autostart/opensnitch_ui.desktop is a link to /usr/share/applications/opensnitch_ui.desktop - this is also possible.

https://salsa.debian.org/go-team/packages/opensnitch/-/blob/debian/sid/debian/python3-opensnitch-ui.postinst#L8

@gustavo-iniguez-goya
Copy link
Collaborator

ok @munix9 , understood! I'll add it in a few days.

@munix9
Copy link
Contributor Author

munix9 commented May 28, 2023

ok @munix9 , understood! I'll add it in a few days.

Thank you, there is no hurry, it is a desirable feature.

I should mention that due to the "/etc and /usr/etc" topic there might be an additional system directory, namely /usr/etc/xdg/autostart/.

See also #671

@lainedfles
Copy link
Contributor

This will be a nice feature, thanks! Perhaps we can resolve some of these path discrepancies using a module like xdg-base-dirs.

@munix9
Copy link
Contributor Author

munix9 commented Jun 8, 2023

This will be a nice feature, thanks! Perhaps we can resolve some of these path discrepancies using a module like xdg-base-dirs.

Or pyxdg - that would certainly make sense to use one of them.

@gustavo-iniguez-goya
Copy link
Collaborator

added in 5962101

@jreus
Copy link

jreus commented Jul 3, 2023

+1 this would be a great feature. I only use opensnitch in certain working situations and would like it to easy to toggle the autostart feature. In most applications I see this offered as a setting or even an option in the tray itself.

@lainedfles
Copy link
Contributor

It works great. Thanks @munix9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a whole new feature
Projects
None yet
Development

No branches or pull requests

5 participants