
#include <gtk/gtk.h>
#include "gtklockbutton.h"
#include <gio/gio.h>
#include <polkit/polkit.h>

/* build with
 *
 * gcc -g -O0 -o testlockbutton gtklockbutton.c testlockbutton.c `pkg-config --cflags --libs gtk+-2.0 polkit-gobject-1` */

static GtkWidget *allowed_button;
static GtkWidget *can_acquire_button;
static GtkWidget *can_release_button;

static void
update_from_permission (GPermission *permission)
{
  gboolean allowed, can_acquire, can_release;

  g_object_get (permission,
                "allowed", &allowed,
                "can-acquire", &can_acquire,
                "can-release", &can_release,
                NULL);
  //g_print ("got allowed=%d can_acquire=%d can_release=%d\n", allowed, can_acquire, can_release);

  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (allowed_button), allowed);
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (can_acquire_button), can_acquire);
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (can_release_button), can_release);
}

static void
on_permission_changed (GPermission *permission,
                       GParamSpec  *pspec)
{
  update_from_permission (permission);
}

int
main (int argc, char *argv[])
{
  GtkWidget *window;
  GtkWidget *button;
  GtkWidget *box;
  GtkWidget *update;
  GtkWidget *label;
  GPermission *permission;
  const gchar *action_id;
  gchar *s;

  if (argc != 2)
    {
      g_printerr ("Usage: %s <action_id>\n", argv[0]);
      return 1;
    }

  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  box = gtk_vbox_new (FALSE, 5);
  gtk_container_add (GTK_CONTAINER (window), box);

  allowed_button = gtk_check_button_new_with_label ("Allowed");
  gtk_widget_set_sensitive (allowed_button, FALSE);
  gtk_container_add (GTK_CONTAINER (box), allowed_button);
  can_acquire_button = gtk_check_button_new_with_label ("Can acquire");
  gtk_widget_set_sensitive (can_acquire_button, FALSE);
  gtk_container_add (GTK_CONTAINER (box), can_acquire_button);
  can_release_button = gtk_check_button_new_with_label ("Can release");
  gtk_widget_set_sensitive (can_release_button, FALSE);
  gtk_container_add (GTK_CONTAINER (box), can_release_button);

  action_id = argv[1];
  s = g_strdup_printf ("Using PolkitPermission for action_id\n%s", action_id);
  label = gtk_label_new (s);
  g_free (s);
  gtk_container_add (GTK_CONTAINER (box), label);
  permission = polkit_permission_new_sync (action_id,
                                           NULL,  /* PolkitSubject* */
                                           NULL,  /* GCancellable* */
                                           NULL); /* GError* */

  g_signal_connect (permission, "notify", G_CALLBACK (on_permission_changed), NULL);
  update_from_permission (permission);

  button = gtk_lock_button_new (permission);
  gtk_container_add (GTK_CONTAINER (box), button);

  gtk_widget_show_all (window);

  gtk_main ();

  return 0;
}


