Compare commits

...

2 Commits

Author SHA1 Message Date
Tracker-Friendly 82927ed2c0 Merge pull request 'Refactor theme switcher to use GSubprocess and streamline GSettings usage' (#1) from scriptod/dconf-theme-switcher:main into main
Reviewed-on: https://centrifuge.hectabit.org/HectaBit/dconf-theme-switcher/pulls/1
2024-01-16 13:33:58 +00:00
scriptod 94015a33cb Refactor theme switcher to use GSubprocess and streamline GSettings usage
I've replaced the system() calls with GSubprocess to improve security and control, using GSubprocess is more secure because it avoids shell interpolation risks that can occur with system() calls. This <------ means there's less chance of injection attacks where arbitrary commands could be executed. It also gives us better control over the execution environment, including file descriptor inheritance and signal handling, making it a more robust solution for managing child processes

I've added error handling for subprocess calls to ensure we get clear feedback if there are issues starting or stopping the wf-panel process, this is the (the) way, if a subprocess fails to launch or if there's a problem sending it signals, we'll know exactly what went wrong.

By combining the theme switching into a single function, I've reduced code duplication his makes the "codebase" cleaner and easier to maintain since any changes to theme switching only need to be made in one place.

I've eliminated redundant GSettings object creation by reusing the same GSettings objects where possible, which makes the code more efficient.

Darling, lastly I ensured that unused parameters in signal handler functions are explicitly cast to (void) to avoid compiler warnings about unused parameters, this clearly keeps the code tidy and warning-free beautiful.
2024-01-13 09:50:40 +00:00
1 changed files with 43 additions and 28 deletions

View File

@ -1,41 +1,57 @@
#include <gtk/gtk.h> #include <gtk/gtk.h>
#include <glib.h>
#include <gio/gio.h> #include <gio/gio.h>
#include <stdlib.h> // For system() function
static void light_mode_clicked(GtkWidget *widget, gpointer data) static void restart_wf_panel() {
{ GError *error = NULL;
GSettings *settings = g_settings_new("org.gnome.desktop.interface"); GSubprocess *subprocess = g_subprocess_new(G_SUBPROCESS_FLAGS_NONE, &error, "pkill", "wf-panel", NULL);
g_settings_set_string(settings, "gtk-theme", "Adwaita");
g_settings_set_string(settings, "color-scheme", "prefer-light");
g_object_unref(settings);
settings = g_settings_new("org.gnome.desktop.wm.preferences"); if (!subprocess) {
g_settings_set_string(settings, "theme", "Adwaita"); g_printerr("Failed to kill wf-panel: %s\n", error->message);
g_object_unref(settings); g_clear_error(&error);
return;
// Restart and fork wf-panel into the background
system("pkill wf-panel");
system("wf-panel &");
} }
static void dark_mode_clicked(GtkWidget *widget, gpointer data) g_subprocess_wait_async(subprocess, NULL, NULL, NULL);
{ g_object_unref(subprocess);
GSettings *settings = g_settings_new("org.gnome.desktop.interface");
g_settings_set_string(settings, "gtk-theme", "Adwaita-dark");
g_settings_set_string(settings, "color-scheme", "prefer-dark");
g_object_unref(settings);
settings = g_settings_new("org.gnome.desktop.wm.preferences"); subprocess = g_subprocess_new(G_SUBPROCESS_FLAGS_NONE, &error, "wf-panel", "&", NULL);
g_settings_set_string(settings, "theme", "Adwaita-dark");
g_object_unref(settings);
// Restart and fork wf-panel into the background if (!subprocess) {
system("pkill wf-panel"); g_printerr("Failed to start wf-panel: %s\n", error->message);
system("wf-panel &"); g_clear_error(&error);
} else {
g_object_unref(subprocess);
}
} }
int main(int argc, char *argv[]) static void switch_theme(const gchar *gtk_theme, const gchar *wm_theme, const gchar *color_scheme) {
{ GSettings *interface_settings = g_settings_new("org.gnome.desktop.interface");
GSettings *wm_settings = g_settings_new("org.gnome.desktop.wm.preferences");
g_settings_set_string(interface_settings, "gtk-theme", gtk_theme);
g_settings_set_string(interface_settings, "color-scheme", color_scheme);
g_settings_set_string(wm_settings, "theme", wm_theme);
g_object_unref(interface_settings);
g_object_unref(wm_settings);
restart_wf_panel();
}
static void light_mode_clicked(GtkWidget *widget, gpointer data) {
(void)widget; // Params is not used
(void)data; // Not used
switch_theme("Adwaita", "Adwaita", "prefer-light");
}
static void dark_mode_clicked(GtkWidget *widget, gpointer data) {
(void)widget; // Not used parameter
(void)data; // Not used parameter, but here is a little secret: I'm gay.
switch_theme("Adwaita-dark", "Adwaita-dark", "prefer-dark");
}
int main(int argc, char *argv[]) {
gtk_init(&argc, &argv); gtk_init(&argc, &argv);
GtkWidget *window; GtkWidget *window;
@ -64,4 +80,3 @@ int main(int argc, char *argv[])
return 0; return 0;
} }