Updated the dialogue
This commit is contained in:
		@@ -1,41 +1,153 @@
 | 
			
		||||
#include "gio/gio.h"
 | 
			
		||||
#include "glib-object.h"
 | 
			
		||||
#include "glib.h"
 | 
			
		||||
#include <adwaita.h>
 | 
			
		||||
#include <gtk/gtk.h>
 | 
			
		||||
#include <stdbool.h>
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
 | 
			
		||||
#define YES 0
 | 
			
		||||
#define NO 1
 | 
			
		||||
#define PERM 2
 | 
			
		||||
 | 
			
		||||
int exit_code = 0;
 | 
			
		||||
gboolean is_permanent = false;
 | 
			
		||||
GtkEntryBuffer *entry_buffer = NULL;
 | 
			
		||||
GtkWidget *checkbox = NULL;
 | 
			
		||||
 | 
			
		||||
static void negative_response(GtkWindow *window) {
 | 
			
		||||
  fprintf(stdout, "%s", gtk_entry_buffer_get_text(entry_buffer));
 | 
			
		||||
  exit_code = (gtk_check_button_get_active(GTK_CHECK_BUTTON(checkbox)))
 | 
			
		||||
                  ? YES | PERM
 | 
			
		||||
                  : YES;
 | 
			
		||||
  gtk_window_close(window);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void positive_response(GtkWindow *window) {
 | 
			
		||||
  fprintf(stdout, "%s", gtk_entry_buffer_get_text(entry_buffer));
 | 
			
		||||
  exit_code = (gtk_check_button_get_active(GTK_CHECK_BUTTON(checkbox)))
 | 
			
		||||
                  ? NO | PERM
 | 
			
		||||
                  : NO;
 | 
			
		||||
  gtk_window_close(window);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void on_check_button_toggled(GtkToggleButton *button,
 | 
			
		||||
                                    gpointer user_data) {
 | 
			
		||||
  gboolean active = gtk_toggle_button_get_active(button);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void on_activate(GtkApplication *app, gpointer user_data) {
 | 
			
		||||
  // Create the main window
 | 
			
		||||
  AdwMessageDialog *dialog = ADW_MESSAGE_DIALOG(
 | 
			
		||||
      adw_message_dialog_new(NULL, "Allow access?", "allow_access?"));
 | 
			
		||||
  AdwWindow *window = ADW_WINDOW(adw_window_new());
 | 
			
		||||
  gtk_window_set_application(GTK_WINDOW(window), app);
 | 
			
		||||
  gtk_window_set_title(GTK_WINDOW(window), "icfs");
 | 
			
		||||
  // gtk_window_set_default_size(GTK_WINDOW(window), 300, 150);
 | 
			
		||||
 | 
			
		||||
  // Set the dialog as transient for the (non-existent) parent window
 | 
			
		||||
  gtk_window_set_application(GTK_WINDOW(dialog), app);
 | 
			
		||||
  AdwStatusPage *content = ADW_STATUS_PAGE(adw_status_page_new());
 | 
			
		||||
  adw_status_page_set_title(content, "Allow access?");
 | 
			
		||||
 | 
			
		||||
  // Add response buttons
 | 
			
		||||
  adw_message_dialog_add_response(dialog, "cancel", "Cancel");
 | 
			
		||||
  adw_message_dialog_add_response(dialog, "ok", "OK");
 | 
			
		||||
  char *description = NULL;
 | 
			
		||||
  asprintf(
 | 
			
		||||
      &description,
 | 
			
		||||
      "Allow process <tt>%s</tt> with PID <tt>%s</tt> to access <tt>%s</tt>",
 | 
			
		||||
      g_object_get_data(G_OBJECT(app), "accessing_name"),
 | 
			
		||||
      g_object_get_data(G_OBJECT(app), "accessing_pid"),
 | 
			
		||||
      g_object_get_data(G_OBJECT(app), "access_dir"));
 | 
			
		||||
  adw_status_page_set_description(content, description);
 | 
			
		||||
  free(description);
 | 
			
		||||
 | 
			
		||||
  // Set default response
 | 
			
		||||
  adw_message_dialog_set_default_response(dialog, "ok");
 | 
			
		||||
  entry_buffer = gtk_entry_buffer_new(
 | 
			
		||||
      g_object_get_data(G_OBJECT(app), "access_dir"),
 | 
			
		||||
      strlen(g_object_get_data(G_OBJECT(app), "access_dir")));
 | 
			
		||||
 | 
			
		||||
  // Set close response (when clicking X)
 | 
			
		||||
  adw_message_dialog_set_close_response(dialog, "cancel");
 | 
			
		||||
  GtkWidget *entry = gtk_entry_new();
 | 
			
		||||
  gtk_entry_set_buffer(GTK_ENTRY(entry), entry_buffer);
 | 
			
		||||
  gtk_entry_set_placeholder_text(GTK_ENTRY(entry), "Enter filename");
 | 
			
		||||
  gtk_widget_set_hexpand(entry, TRUE);
 | 
			
		||||
 | 
			
		||||
  // Connect response handler
 | 
			
		||||
  // g_signal_connect(dialog, "response", G_CALLBACK(gtk_window_close), dialog);
 | 
			
		||||
  // Create a prefix label and box
 | 
			
		||||
  GtkWidget *entry_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
 | 
			
		||||
  GtkWidget *prefix_label =
 | 
			
		||||
      gtk_label_new(g_object_get_data(G_OBJECT(app), "root_folder"));
 | 
			
		||||
  gtk_box_append(GTK_BOX(entry_box), prefix_label);
 | 
			
		||||
  gtk_box_append(GTK_BOX(entry_box), entry);
 | 
			
		||||
 | 
			
		||||
  checkbox = gtk_check_button_new_with_label("Permanent");
 | 
			
		||||
  gtk_check_button_set_active(GTK_CHECK_BUTTON(checkbox), false);
 | 
			
		||||
  // gtk_widget_set_halign(checkbox, GTK_ALIGN_CENTER);
 | 
			
		||||
 | 
			
		||||
  GtkWidget *yes_button = gtk_button_new_with_label("Yes");
 | 
			
		||||
  gtk_widget_set_hexpand(yes_button, TRUE);
 | 
			
		||||
  g_signal_connect_swapped(yes_button, "clicked", G_CALLBACK(positive_response),
 | 
			
		||||
                           window);
 | 
			
		||||
 | 
			
		||||
  GtkWidget *no_button = gtk_button_new_with_label("No");
 | 
			
		||||
  gtk_widget_set_hexpand(no_button, TRUE);
 | 
			
		||||
  g_signal_connect_swapped(no_button, "clicked", G_CALLBACK(negative_response),
 | 
			
		||||
                           window);
 | 
			
		||||
 | 
			
		||||
  GtkWidget *button_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 6);
 | 
			
		||||
  gtk_box_append(GTK_BOX(button_box), yes_button);
 | 
			
		||||
  gtk_box_append(GTK_BOX(button_box), no_button);
 | 
			
		||||
  gtk_widget_set_halign(button_box, GTK_ALIGN_FILL);
 | 
			
		||||
 | 
			
		||||
  // Combine everything in a box
 | 
			
		||||
  GtkWidget *box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 12);
 | 
			
		||||
  gtk_box_append(GTK_BOX(box), GTK_WIDGET(content));
 | 
			
		||||
  gtk_box_append(GTK_BOX(box), entry_box);
 | 
			
		||||
  gtk_box_append(GTK_BOX(box), checkbox);
 | 
			
		||||
  gtk_box_append(GTK_BOX(box), button_box);
 | 
			
		||||
  gtk_widget_set_margin_top(GTK_WIDGET(box), 12);
 | 
			
		||||
  gtk_widget_set_margin_bottom(GTK_WIDGET(box), 12);
 | 
			
		||||
  gtk_widget_set_margin_start(GTK_WIDGET(box), 12);
 | 
			
		||||
  gtk_widget_set_margin_end(GTK_WIDGET(box), 12);
 | 
			
		||||
 | 
			
		||||
  // g_signal_connect(window, "response", G_CALLBACK(gtk_window_close), window);
 | 
			
		||||
 | 
			
		||||
  // Show the dialog
 | 
			
		||||
  gtk_window_present(GTK_WINDOW(dialog));
 | 
			
		||||
  adw_window_set_content(window, box);
 | 
			
		||||
  gtk_window_present(GTK_WINDOW(window));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int on_command_line(GApplication *app, GApplicationCommandLine *cmdline,
 | 
			
		||||
                           gpointer user_data) {
 | 
			
		||||
  gchar **argv;
 | 
			
		||||
  gint argc;
 | 
			
		||||
 | 
			
		||||
  argv = g_application_command_line_get_arguments(cmdline, &argc);
 | 
			
		||||
 | 
			
		||||
  // Handle your arguments here
 | 
			
		||||
  if (argc >= 4) {
 | 
			
		||||
    fprintf(stderr, "%s\n", argv[1]);
 | 
			
		||||
    g_object_set_data_full(G_OBJECT(app), "accessing_pid", g_strdup(argv[1]),
 | 
			
		||||
                           g_free);
 | 
			
		||||
    g_object_set_data_full(G_OBJECT(app), "accessing_name", g_strdup(argv[2]),
 | 
			
		||||
                           g_free);
 | 
			
		||||
    g_object_set_data_full(G_OBJECT(app), "root_folder", g_strdup(argv[3]),
 | 
			
		||||
                           g_free);
 | 
			
		||||
    g_object_set_data_full(G_OBJECT(app), "access_dir", g_strdup(argv[4]),
 | 
			
		||||
                           g_free);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  g_strfreev(argv);
 | 
			
		||||
 | 
			
		||||
  // Activate the application
 | 
			
		||||
  g_application_activate(app);
 | 
			
		||||
  return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main(int argc, char **argv) {
 | 
			
		||||
  // Create a new application
 | 
			
		||||
  AdwApplication *app = adw_application_new("com.example.zenityclone",
 | 
			
		||||
                                            G_APPLICATION_DEFAULT_FLAGS);
 | 
			
		||||
                                            G_APPLICATION_HANDLES_COMMAND_LINE);
 | 
			
		||||
 | 
			
		||||
  g_signal_connect(app, "command-line", G_CALLBACK(on_command_line), NULL);
 | 
			
		||||
  g_signal_connect(app, "activate", G_CALLBACK(on_activate), NULL);
 | 
			
		||||
 | 
			
		||||
  // Run the application
 | 
			
		||||
  int status = g_application_run(G_APPLICATION(app), argc, argv);
 | 
			
		||||
  g_object_unref(app);
 | 
			
		||||
 | 
			
		||||
  return status;
 | 
			
		||||
  return (status == 0) ? exit_code : status;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user