From 08e29887767114ada02f6c003cfcfdd22d25d021 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stig-=C3=98rjan=20Smelror?= Date: Fri, 9 Aug 2024 23:02:46 +0200 Subject: [PATCH] Update to version 1.5. Rename About button to Settings and create a Settings dialog where you can configure the commands for each button and moved the About section as well --- README.md | 1 + ssdd.c | 207 ++++++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 155 insertions(+), 53 deletions(-) diff --git a/README.md b/README.md index a5951b2..c089479 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,7 @@ Edit the `Makefile` or use the following commands: ```shell % make all # Compile % sudo make install # Install to /usr/local +% sudo make install PREFIX=/usr # Install to /usr ``` ### Manual compilation diff --git a/ssdd.c b/ssdd.c index 2098993..f5b385c 100644 --- a/ssdd.c +++ b/ssdd.c @@ -1,14 +1,22 @@ #include #include +#include #include "resources.h" // Function declarations (prototypes) static void execute_command(const gchar *command); -static void show_about_dialog(GtkWidget *widget); +static void show_settings_dialog(GtkWidget *widget); +static void show_about_tab(GtkWidget *box); +static void show_settings_tab(GtkWidget *box); static void button_clicked(GtkWidget *widget, gpointer data); static gboolean on_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data); static void show_confirmation_dialog(GtkWidget *widget, const gchar *label, const gchar *command); static void create_button(GtkWidget *grid, GtkApplication *app, const gchar *label_text, const gchar *icon_name, const gchar *command, int pos); +static void save_configuration(const gchar *commands[]); +static void load_configuration(gchar *commands[]); + +#define CONFIG_PATH g_build_filename(g_get_home_dir(), ".config/ssdd/config", NULL) +#define CONFIG_DIR g_build_filename(g_get_home_dir(), ".config/ssdd", NULL) static void execute_command(const gchar *command) { GError *error = NULL; @@ -28,31 +36,87 @@ static void execute_command(const gchar *command) { } } -static void show_about_dialog(GtkWidget *widget) { +static void show_settings_dialog(GtkWidget *widget) { GtkWidget *dialog; GtkWidget *content_area; + GtkWidget *notebook; + GtkWidget *settings_tab; + GtkWidget *about_tab; + GtkWidget *close_button; + + dialog = gtk_dialog_new_with_buttons("Settings", + NULL, + GTK_DIALOG_DESTROY_WITH_PARENT, + NULL); + content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog)); + + notebook = gtk_notebook_new(); + gtk_box_pack_start(GTK_BOX(content_area), notebook, TRUE, TRUE, 0); + + settings_tab = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10); + gtk_container_set_border_width(GTK_CONTAINER(settings_tab), 10); + gtk_notebook_append_page(GTK_NOTEBOOK(notebook), settings_tab, gtk_label_new("Settings")); + + about_tab = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10); + gtk_container_set_border_width(GTK_CONTAINER(about_tab), 10); + gtk_notebook_append_page(GTK_NOTEBOOK(notebook), about_tab, gtk_label_new("About")); + + show_settings_tab(settings_tab); + show_about_tab(about_tab); + + close_button = gtk_button_new_with_label("Close"); + g_signal_connect_swapped(close_button, "clicked", G_CALLBACK(gtk_widget_destroy), dialog); + gtk_box_pack_start(GTK_BOX(content_area), close_button, FALSE, FALSE, 10); + + gtk_widget_show_all(dialog); +} + +static void show_settings_tab(GtkWidget *box) { + const gchar *labels[] = { + "Logout Command", + "Reboot Command", + "Shutdown Command", + "Switch User Command", + "Suspend Command", + "Hibernate Command" + }; + + gchar *commands[6]; + load_configuration(commands); + + GtkWidget *grid = gtk_grid_new(); + gtk_grid_set_row_spacing(GTK_GRID(grid), 10); // Space between rows + gtk_grid_set_column_spacing(GTK_GRID(grid), 10); // Space between columns + gtk_box_pack_start(GTK_BOX(box), grid, TRUE, TRUE, 0); + + for (int i = 0; i < 6; i++) { + GtkWidget *label = gtk_label_new(labels[i]); + GtkWidget *entry = gtk_entry_new(); + gtk_entry_set_text(GTK_ENTRY(entry), commands[i]); + gtk_widget_set_hexpand(entry, TRUE); // Allow the entry to expand + gtk_widget_set_halign(label, GTK_ALIGN_END); // Align the label to the end + + gtk_grid_attach(GTK_GRID(grid), label, 0, i, 1, 1); + gtk_grid_attach(GTK_GRID(grid), entry, 1, i, 1, 1); + } + + for (int i = 0; i < 6; i++) { + g_free(commands[i]); + } +} + +static void show_about_tab(GtkWidget *box) { GtkWidget *label; GtkWidget *image; - GtkWidget *box; const gchar *about_text = "\nAbout Simple ShutDown Dialog\n\n" - "Version: 1.4\n" + "Version: 1.5\n" "Author: kekePower\n" - "URL: https://git.kekepower.com/kekePower/ssdd\n" - "Description: This is a simple Shutdown Dialog for Openbox.\n"; - - dialog = gtk_dialog_new_with_buttons("About Simple ShutDown Dialog", - NULL, - GTK_DIALOG_DESTROY_WITH_PARENT, - "_Close", - GTK_RESPONSE_CLOSE, - NULL); - content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog)); - box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); - gtk_container_add(GTK_CONTAINER(content_area), box); + "URL: https://git.kekepower.com/kekePower/ssdd\n" + "Description: This is a Simple ShutDown Dialog for Openbox.\n"; image = gtk_image_new_from_resource("/org/gtk/ssdd/ssdd-icon.png"); - gtk_image_set_pixel_size(GTK_IMAGE(image), 250); // Assuming original size is 500x500 + gtk_image_set_pixel_size(GTK_IMAGE(image), 250); gtk_box_pack_start(GTK_BOX(box), image, FALSE, FALSE, 0); label = gtk_label_new(NULL); @@ -61,11 +125,6 @@ static void show_about_dialog(GtkWidget *widget) { gtk_widget_set_halign(label, GTK_ALIGN_START); gtk_widget_set_valign(label, GTK_ALIGN_START); gtk_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0); - - gtk_widget_show_all(dialog); - - gtk_dialog_run(GTK_DIALOG(dialog)); - gtk_widget_destroy(dialog); } static void button_clicked(GtkWidget *widget, gpointer data) { @@ -77,8 +136,8 @@ static void button_clicked(GtkWidget *widget, gpointer data) { return; } - if (g_strcmp0(command, "about") == 0) { - show_about_dialog(widget); + if (g_strcmp0(command, "settings") == 0) { + show_settings_dialog(widget); } else { GtkWidget *window = gtk_widget_get_toplevel(widget); show_confirmation_dialog(window, label, command); @@ -102,7 +161,6 @@ static void show_confirmation_dialog(GtkWidget *parent_window, const gchar *labe dialog = gtk_dialog_new(); gtk_window_set_title(GTK_WINDOW(dialog), "Confirmation"); - // Set the transient parent correctly gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(parent_window)); gtk_window_set_modal(GTK_WINDOW(dialog), TRUE); @@ -121,13 +179,13 @@ static void show_confirmation_dialog(GtkWidget *parent_window, const gchar *labe gtk_dialog_add_buttons(GTK_DIALOG(dialog), "Yes", GTK_RESPONSE_YES, "No", GTK_RESPONSE_NO, - NULL); // Add NULL to terminate the list of buttons + NULL); - GtkWidget *button_box = gtk_dialog_get_content_area(GTK_DIALOG(dialog)); // Get content area instead of action area - gtk_box_set_spacing(GTK_BOX(button_box), 10); // Adjust spacing as needed + GtkWidget *button_box = gtk_dialog_get_content_area(GTK_DIALOG(dialog)); + gtk_box_set_spacing(GTK_BOX(button_box), 10); gtk_widget_set_margin_top(button_box, 10); - gtk_widget_show_all(dialog); // Show the dialog and its children + gtk_widget_show_all(dialog); response = gtk_dialog_run(GTK_DIALOG(dialog)); gtk_widget_destroy(dialog); @@ -145,7 +203,7 @@ static void create_button(GtkWidget *grid, GtkApplication *app, const gchar *lab GtkWidget *label; button = gtk_button_new(); - box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10); // Set internal box spacing to 10 + box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10); gtk_container_add(GTK_CONTAINER(button), box); image = gtk_image_new_from_icon_name(icon_name, GTK_ICON_SIZE_BUTTON); @@ -154,31 +212,73 @@ static void create_button(GtkWidget *grid, GtkApplication *app, const gchar *lab label = gtk_label_new(label_text); gtk_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0); - // Set margins for the button contents gtk_widget_set_margin_top(box, 5); gtk_widget_set_margin_bottom(box, 5); gtk_widget_set_margin_start(box, 5); gtk_widget_set_margin_end(box, 5); g_object_set_data(G_OBJECT(button), "app", app); - g_object_set_data(G_OBJECT(button), "label", (gpointer) label_text); - g_signal_connect(button, "clicked", G_CALLBACK(button_clicked), (gpointer) command); + g_object_set_data(G_OBJECT(button), "label", (gpointer)label_text); + g_signal_connect(button, "clicked", G_CALLBACK(button_clicked), (gpointer)command); gtk_grid_attach(GTK_GRID(grid), button, pos % 4, pos / 4, 1, 1); } +static void save_configuration(const gchar *commands[]) { + GError *error = NULL; + g_mkdir_with_parents(CONFIG_DIR, 0755); + + gchar *config_data = g_strjoinv("\n", (gchar **)commands); + g_file_set_contents(CONFIG_PATH, config_data, -1, &error); + + if (error) { + g_warning("Failed to save configuration: %s", error->message); + g_error_free(error); + } + + g_free(config_data); +} + +static void load_configuration(gchar *commands[]) { + GError *error = NULL; + gchar *config_data = NULL; + + g_mkdir_with_parents(CONFIG_DIR, 0755); + + if (!g_file_test(CONFIG_PATH, G_FILE_TEST_EXISTS)) { + const gchar *default_commands[] = { + "openbox --exit", + "systemctl reboot", + "systemctl poweroff", + "dm-tool switch-to-greeter", + "systemctl suspend", + "systemctl hibernate" + }; + save_configuration(default_commands); + } + + g_file_get_contents(CONFIG_PATH, &config_data, NULL, &error); + + if (error) { + g_warning("Failed to load configuration: %s", error->message); + g_error_free(error); + return; + } + + gchar **lines = g_strsplit(config_data, "\n", 6); + for (int i = 0; i < 6; i++) { + commands[i] = g_strdup(lines[i]); + } + + g_strfreev(lines); + g_free(config_data); +} + static void activate(GtkApplication *app, gpointer user_data) { GtkWidget *window; GtkWidget *grid; - const gchar *buttons[] = { - "openbox --exit", - "systemctl reboot", - "systemctl poweroff", - "dm-tool switch-to-greeter", - "systemctl suspend", - "systemctl hibernate", - "about", - "exit" - }; + gchar *commands[6]; + load_configuration(commands); + const gchar *icons[] = { "system-log-out", "view-refresh", @@ -186,17 +286,17 @@ static void activate(GtkApplication *app, gpointer user_data) { "system-users", "media-playback-pause", "media-playback-stop", - "help-about", + "preferences-system", "application-exit" }; - const gchar *labels[] = { // Define the labels array here + const gchar *labels[] = { "Logout", "Reboot", "Shutdown", "Switch User", "Suspend", "Hibernate", - "About", + "Settings", "Exit" }; @@ -204,12 +304,11 @@ static void activate(GtkApplication *app, gpointer user_data) { gtk_window_set_title(GTK_WINDOW(window), "Simple ShutDown Dialog"); gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER); - // Load the icon with error handling GError *error = NULL; GdkPixbuf *icon_pixbuf = gdk_pixbuf_new_from_resource("/org/gtk/ssdd/ssdd-icon.png", &error); if (icon_pixbuf) { gtk_window_set_icon(GTK_WINDOW(window), icon_pixbuf); - g_object_unref(icon_pixbuf); // Free the pixbuf + g_object_unref(icon_pixbuf); } else { g_warning("Failed to load icon: %s", error->message); g_error_free(error); @@ -218,13 +317,16 @@ static void activate(GtkApplication *app, gpointer user_data) { g_signal_connect(window, "key-press-event", G_CALLBACK(on_key_press), app); grid = gtk_grid_new(); - gtk_grid_set_row_spacing(GTK_GRID(grid), 0); // Set the row spacing to 0 - gtk_grid_set_column_spacing(GTK_GRID(grid), 0); // Set the column spacing to 0 + gtk_grid_set_row_spacing(GTK_GRID(grid), 0); + gtk_grid_set_column_spacing(GTK_GRID(grid), 0); gtk_container_add(GTK_CONTAINER(window), grid); - for (int i = 0; i < 8; i++) { - create_button(grid, app, labels[i], icons[i], buttons[i], i); + for (int i = 0; i < 6; i++) { + create_button(grid, app, labels[i], icons[i], commands[i], i); + g_free(commands[i]); // Free the memory allocated for commands } + create_button(grid, app, labels[6], icons[6], "settings", 6); + create_button(grid, app, labels[7], icons[7], "exit", 7); gtk_widget_show_all(window); } @@ -233,7 +335,6 @@ int main(int argc, char **argv) { GtkApplication *app; int status; - // Register the resource g_resources_register(resources_get_resource()); app = gtk_application_new("org.gtk.ssdd", G_APPLICATION_DEFAULT_FLAGS);