|
Packit Service |
f02b19 |
/*
|
|
Packit Service |
f02b19 |
* gnome-keyring
|
|
Packit Service |
f02b19 |
*
|
|
Packit Service |
f02b19 |
* Copyright (C) 2011 Collabora Ltd.
|
|
Packit Service |
f02b19 |
*
|
|
Packit Service |
f02b19 |
* This program is free software; you can redistribute it and/or modify
|
|
Packit Service |
f02b19 |
* it under the terms of the GNU Lesser General Public License as
|
|
Packit Service |
f02b19 |
* published by the Free Software Foundation; either version 2.1 of
|
|
Packit Service |
f02b19 |
* the License, or (at your option) any later version.
|
|
Packit Service |
f02b19 |
*
|
|
Packit Service |
f02b19 |
* This program is distributed in the hope that it will be useful, but
|
|
Packit Service |
f02b19 |
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
f02b19 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit Service |
f02b19 |
* Lesser General Public License for more details.
|
|
Packit Service |
f02b19 |
*
|
|
Packit Service |
f02b19 |
* You should have received a copy of the GNU Lesser General Public
|
|
Packit Service |
f02b19 |
* License along with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
Packit Service |
f02b19 |
*
|
|
Packit Service |
f02b19 |
* Author: Stef Walter <stefw@collabora.co.uk>
|
|
Packit Service |
f02b19 |
*/
|
|
Packit Service |
f02b19 |
|
|
Packit Service |
f02b19 |
#include "config.h"
|
|
Packit Service |
f02b19 |
|
|
Packit Service |
f02b19 |
#include "gcr/gcr.h"
|
|
Packit Service |
f02b19 |
#include "gcr/gcr-base.h"
|
|
Packit Service |
f02b19 |
|
|
Packit Service |
f02b19 |
#include <gtk/gtk.h>
|
|
Packit Service |
f02b19 |
#include <gdk/gdkx.h>
|
|
Packit Service |
f02b19 |
|
|
Packit Service |
f02b19 |
#include <err.h>
|
|
Packit Service |
f02b19 |
|
|
Packit Service |
f02b19 |
static const gchar *file_name = NULL;
|
|
Packit Service |
f02b19 |
static gchar *prompt_type = NULL;
|
|
Packit Service |
f02b19 |
static gint prompt_delay = 0;
|
|
Packit Service |
f02b19 |
static gboolean prompt_window = FALSE;
|
|
Packit Service |
f02b19 |
|
|
Packit Service |
f02b19 |
static gboolean
|
|
Packit Service |
f02b19 |
on_delay_timeout (gpointer data)
|
|
Packit Service |
f02b19 |
{
|
|
Packit Service |
f02b19 |
GMainLoop *loop = data;
|
|
Packit Service |
f02b19 |
g_main_loop_quit (loop);
|
|
Packit Service |
f02b19 |
return FALSE;
|
|
Packit Service |
f02b19 |
}
|
|
Packit Service |
f02b19 |
|
|
Packit Service |
f02b19 |
static void
|
|
Packit Service |
f02b19 |
prompt_perform (GtkWidget *parent)
|
|
Packit Service |
f02b19 |
{
|
|
Packit Service |
f02b19 |
GKeyFile *file;
|
|
Packit Service |
f02b19 |
GError *error = NULL;
|
|
Packit Service |
f02b19 |
gchar **groups, **keys;
|
|
Packit Service |
f02b19 |
GValue value = { 0, };
|
|
Packit Service |
f02b19 |
GParamSpec *spec;
|
|
Packit Service |
f02b19 |
GcrPrompt *prompt;
|
|
Packit Service |
f02b19 |
const gchar *key;
|
|
Packit Service |
f02b19 |
const gchar *password;
|
|
Packit Service |
f02b19 |
GcrPromptReply reply;
|
|
Packit Service |
f02b19 |
gchar *caller_id = NULL;
|
|
Packit Service |
f02b19 |
gboolean cont = TRUE;
|
|
Packit Service |
f02b19 |
GMainLoop *loop;
|
|
Packit Service |
f02b19 |
gchar *type;
|
|
Packit Service |
f02b19 |
gchar *choice;
|
|
Packit Service |
f02b19 |
guint i, j;
|
|
Packit Service |
f02b19 |
|
|
Packit Service |
f02b19 |
file = g_key_file_new ();
|
|
Packit Service |
f02b19 |
if (!g_key_file_load_from_file (file, file_name, G_KEY_FILE_NONE, &error))
|
|
Packit Service |
f02b19 |
errx (1, "couldn't load prompt info: %s", error->message);
|
|
Packit Service |
f02b19 |
|
|
Packit Service |
f02b19 |
if (!prompt_type || g_str_equal (prompt_type, "dialog"))
|
|
Packit Service |
f02b19 |
prompt = g_object_new (GCR_TYPE_PROMPT_DIALOG, NULL);
|
|
Packit Service |
f02b19 |
else if (g_str_equal (prompt_type, "system"))
|
|
Packit Service |
f02b19 |
prompt = gcr_system_prompt_open_for_prompter ("org.gnome.keyring.SystemPrompter", 5, NULL, &error);
|
|
Packit Service |
f02b19 |
else if (g_str_equal (prompt_type, "private"))
|
|
Packit Service |
f02b19 |
prompt = gcr_system_prompt_open_for_prompter ("org.gnome.keyring.PrivatePrompter", 5, NULL, &error);
|
|
Packit Service |
f02b19 |
else
|
|
Packit Service |
f02b19 |
errx (2, "invalid type: %s", prompt_type);
|
|
Packit Service |
f02b19 |
|
|
Packit Service |
f02b19 |
if (error != NULL)
|
|
Packit Service |
f02b19 |
errx (1, "couldn't create prompt: %s", error->message);
|
|
Packit Service |
f02b19 |
|
|
Packit Service |
f02b19 |
if (parent) {
|
|
Packit Service |
f02b19 |
caller_id = g_strdup_printf ("%lu", (gulong)GDK_WINDOW_XID (gtk_widget_get_window (parent)));
|
|
Packit Service |
f02b19 |
gcr_prompt_set_caller_window (GCR_PROMPT (prompt), caller_id);
|
|
Packit Service |
f02b19 |
g_free (caller_id);
|
|
Packit Service |
f02b19 |
}
|
|
Packit Service |
f02b19 |
|
|
Packit Service |
f02b19 |
loop = g_main_loop_new (NULL, FALSE);
|
|
Packit Service |
f02b19 |
groups = g_key_file_get_groups (file, NULL);
|
|
Packit Service |
f02b19 |
for (i = 0; cont && groups[i] != NULL; i++) {
|
|
Packit Service |
f02b19 |
|
|
Packit Service |
f02b19 |
if (i != 0) {
|
|
Packit Service |
f02b19 |
g_timeout_add_seconds (prompt_delay, on_delay_timeout, loop);
|
|
Packit Service |
f02b19 |
g_main_loop_run (loop);
|
|
Packit Service |
f02b19 |
}
|
|
Packit Service |
f02b19 |
|
|
Packit Service |
f02b19 |
keys = g_key_file_get_keys (file, groups[i], NULL, NULL);
|
|
Packit Service |
f02b19 |
for (j = 0; keys[j] != NULL; j++) {
|
|
Packit Service |
f02b19 |
key = keys[j];
|
|
Packit Service |
f02b19 |
if (g_str_equal (key, "type"))
|
|
Packit Service |
f02b19 |
continue;
|
|
Packit Service |
f02b19 |
spec = g_object_class_find_property (G_OBJECT_GET_CLASS (prompt), key);
|
|
Packit Service |
f02b19 |
if (spec == NULL)
|
|
Packit Service |
f02b19 |
errx (1, "couldn't find property %s on prompt %s",
|
|
Packit Service |
f02b19 |
key, G_OBJECT_TYPE_NAME (prompt));
|
|
Packit Service |
f02b19 |
g_value_init (&value, spec->value_type);
|
|
Packit Service |
f02b19 |
switch (spec->value_type) {
|
|
Packit Service |
f02b19 |
case G_TYPE_STRING:
|
|
Packit Service |
f02b19 |
g_value_take_string (&value, g_key_file_get_string (file, groups[i], key, NULL));
|
|
Packit Service |
f02b19 |
break;
|
|
Packit Service |
f02b19 |
case G_TYPE_INT:
|
|
Packit Service |
f02b19 |
g_value_set_int (&value, g_key_file_get_integer (file, groups[i], key, NULL));
|
|
Packit Service |
f02b19 |
break;
|
|
Packit Service |
f02b19 |
case G_TYPE_BOOLEAN:
|
|
Packit Service |
f02b19 |
g_value_set_boolean (&value, g_key_file_get_boolean (file, groups[i], key, NULL));
|
|
Packit Service |
f02b19 |
break;
|
|
Packit Service |
f02b19 |
default:
|
|
Packit Service |
f02b19 |
errx (1, "unsupported type %s for property %s",
|
|
Packit Service |
f02b19 |
g_type_name (spec->value_type), key);
|
|
Packit Service |
f02b19 |
break;
|
|
Packit Service |
f02b19 |
}
|
|
Packit Service |
f02b19 |
|
|
Packit Service |
f02b19 |
g_object_set_property (G_OBJECT (prompt), key, &value);
|
|
Packit Service |
f02b19 |
g_value_unset (&value);
|
|
Packit Service |
f02b19 |
}
|
|
Packit Service |
f02b19 |
|
|
Packit Service |
f02b19 |
g_strfreev (keys);
|
|
Packit Service |
f02b19 |
|
|
Packit Service |
f02b19 |
type = g_key_file_get_value (file, groups[i], "type", NULL);
|
|
Packit Service |
f02b19 |
if (g_strcmp0 (type, "password") == 0) {
|
|
Packit Service |
f02b19 |
password = gcr_prompt_password_run (prompt, NULL, &error);
|
|
Packit Service |
f02b19 |
if (error != NULL)
|
|
Packit Service |
f02b19 |
errx (1, "couldn't prompt for password: %s", error->message);
|
|
Packit Service |
f02b19 |
g_print ("prompt password: %s\n", password);
|
|
Packit Service |
f02b19 |
g_print ("password strength: %d\n", gcr_prompt_get_password_strength (prompt));
|
|
Packit Service |
f02b19 |
cont = (password != NULL);
|
|
Packit Service |
f02b19 |
} else if (g_strcmp0 (type, "confirm") == 0) {
|
|
Packit Service |
f02b19 |
reply = gcr_prompt_confirm_run (prompt, NULL, &error);
|
|
Packit Service |
f02b19 |
if (error != NULL)
|
|
Packit Service |
f02b19 |
errx (1, "couldn't prompt for confirm: %s", error->message);
|
|
Packit Service |
f02b19 |
g_print ("prompt confirm: %d\n", reply);
|
|
Packit Service |
f02b19 |
cont = (reply != GCR_PROMPT_REPLY_CANCEL);
|
|
Packit Service |
f02b19 |
} else {
|
|
Packit Service |
f02b19 |
errx (1, "unsupported prompt type: %s", type);
|
|
Packit Service |
f02b19 |
}
|
|
Packit Service |
f02b19 |
g_free (type);
|
|
Packit Service |
f02b19 |
|
|
Packit Service |
f02b19 |
choice = gcr_prompt_get_choice_label (prompt);
|
|
Packit Service |
f02b19 |
if (choice)
|
|
Packit Service |
f02b19 |
g_print ("choice chosen: %s", gcr_prompt_get_choice_chosen (prompt) ? "true" : "false");
|
|
Packit Service |
f02b19 |
g_free (choice);
|
|
Packit Service |
f02b19 |
g_print ("\n");
|
|
Packit Service |
f02b19 |
}
|
|
Packit Service |
f02b19 |
|
|
Packit Service |
f02b19 |
g_main_loop_unref (loop);
|
|
Packit Service |
f02b19 |
g_object_unref (prompt);
|
|
Packit Service |
f02b19 |
g_strfreev (groups);
|
|
Packit Service |
f02b19 |
g_key_file_free (file);
|
|
Packit Service |
f02b19 |
|
|
Packit Service |
f02b19 |
}
|
|
Packit Service |
f02b19 |
|
|
Packit Service |
f02b19 |
static void
|
|
Packit Service |
f02b19 |
on_prompt_clicked (GtkToolButton *button,
|
|
Packit Service |
f02b19 |
gpointer user_data)
|
|
Packit Service |
f02b19 |
{
|
|
Packit Service |
f02b19 |
prompt_perform (user_data);
|
|
Packit Service |
f02b19 |
}
|
|
Packit Service |
f02b19 |
|
|
Packit Service |
f02b19 |
static gboolean
|
|
Packit Service |
f02b19 |
on_window_delete (GtkWidget *widget,
|
|
Packit Service |
f02b19 |
GdkEvent *event,
|
|
Packit Service |
f02b19 |
gpointer user_data)
|
|
Packit Service |
f02b19 |
{
|
|
Packit Service |
f02b19 |
gtk_main_quit ();
|
|
Packit Service |
f02b19 |
return FALSE;
|
|
Packit Service |
f02b19 |
}
|
|
Packit Service |
f02b19 |
|
|
Packit Service |
f02b19 |
static GOptionEntry option_entries[] = {
|
|
Packit Service |
f02b19 |
{ "type", 'c', 0, G_OPTION_ARG_STRING, &prompt_type,
|
|
Packit Service |
f02b19 |
"'system', 'private' or 'dialog'", "type" },
|
|
Packit Service |
f02b19 |
{ "delay", 'd', 0, G_OPTION_ARG_INT, &prompt_delay,
|
|
Packit Service |
f02b19 |
"delay in seconds between prompts", "delay" },
|
|
Packit Service |
f02b19 |
{ "window", 'w', 0, G_OPTION_ARG_NONE, &prompt_window,
|
|
Packit Service |
f02b19 |
"prompt with a parent window", NULL },
|
|
Packit Service |
f02b19 |
{ NULL }
|
|
Packit Service |
f02b19 |
};
|
|
Packit Service |
f02b19 |
|
|
Packit Service |
f02b19 |
int
|
|
Packit Service |
f02b19 |
main (int argc, char *argv[])
|
|
Packit Service |
f02b19 |
{
|
|
Packit Service |
f02b19 |
GOptionContext *context;
|
|
Packit Service |
f02b19 |
GtkWidget *window;
|
|
Packit Service |
f02b19 |
GtkToolbar *toolbar;
|
|
Packit Service |
f02b19 |
GtkToolItem *item;
|
|
Packit Service |
f02b19 |
GError *error = NULL;
|
|
Packit Service |
f02b19 |
|
|
Packit Service |
f02b19 |
g_set_prgname ("frob-prompt");
|
|
Packit Service |
f02b19 |
|
|
Packit Service |
f02b19 |
context = g_option_context_new ("");
|
|
Packit Service |
f02b19 |
g_option_context_add_main_entries (context, option_entries, GETTEXT_PACKAGE);
|
|
Packit Service |
f02b19 |
g_option_context_add_group (context, gtk_get_option_group (TRUE));
|
|
Packit Service |
f02b19 |
|
|
Packit Service |
f02b19 |
if (!g_option_context_parse (context, &argc, &argv, &error))
|
|
Packit Service |
f02b19 |
errx (2, "%s", error->message);
|
|
Packit Service |
f02b19 |
|
|
Packit Service |
f02b19 |
g_option_context_free (context);
|
|
Packit Service |
f02b19 |
|
|
Packit Service |
f02b19 |
if (argc < 2)
|
|
Packit Service |
f02b19 |
errx (2, "specify file");
|
|
Packit Service |
f02b19 |
file_name = argv[1];
|
|
Packit Service |
f02b19 |
|
|
Packit Service |
f02b19 |
if (prompt_window) {
|
|
Packit Service |
f02b19 |
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
|
|
Packit Service |
f02b19 |
g_signal_connect (window, "delete-event", G_CALLBACK (on_window_delete), NULL);
|
|
Packit Service |
f02b19 |
|
|
Packit Service |
f02b19 |
toolbar = GTK_TOOLBAR (gtk_toolbar_new ());
|
|
Packit Service |
f02b19 |
gtk_toolbar_set_style (toolbar, GTK_TOOLBAR_TEXT);
|
|
Packit Service |
f02b19 |
item = gtk_tool_button_new (NULL, "Prompt");
|
|
Packit Service |
f02b19 |
g_signal_connect (item, "clicked", G_CALLBACK (on_prompt_clicked), window);
|
|
Packit Service |
f02b19 |
gtk_toolbar_insert (toolbar, item, 0);
|
|
Packit Service |
f02b19 |
gtk_container_add (GTK_CONTAINER (window), GTK_WIDGET (toolbar));
|
|
Packit Service |
f02b19 |
|
|
Packit Service |
f02b19 |
gtk_window_set_default_size (GTK_WINDOW (window), 400, 80);
|
|
Packit Service |
f02b19 |
gtk_widget_show_all (window);
|
|
Packit Service |
f02b19 |
|
|
Packit Service |
f02b19 |
gtk_main ();
|
|
Packit Service |
f02b19 |
|
|
Packit Service |
f02b19 |
} else {
|
|
Packit Service |
f02b19 |
prompt_perform (NULL);
|
|
Packit Service |
f02b19 |
}
|
|
Packit Service |
f02b19 |
|
|
Packit Service |
f02b19 |
g_free (prompt_type);
|
|
Packit Service |
f02b19 |
return 0;
|
|
Packit Service |
f02b19 |
}
|