Add new password dialog

This commit is contained in:
Arx Cruz 2010-07-10 16:13:40 -03:00
parent 728c840668
commit 10d0380220
6 changed files with 256 additions and 1 deletions

View File

@ -17,6 +17,7 @@ zenity_SOURCES = \
text.c \ text.c \
tree.c \ tree.c \
color.c \ color.c \
password.c \
util.c \ util.c \
util.h \ util.h \
zenity.h zenity.h

View File

@ -87,6 +87,9 @@ main (gint argc, gchar **argv) {
case MODE_COLOR: case MODE_COLOR:
zenity_colorselection (results->data, results->color_data); zenity_colorselection (results->data, results->color_data);
break; break;
case MODE_PASSWORD:
zenity_password_dialog (results->data, results->password_data);
break;
case MODE_ABOUT: case MODE_ABOUT:
zenity_about (results->data); zenity_about (results->data);
break; break;

View File

@ -113,6 +113,10 @@ static gboolean zenity_colorsel_active;
static gchar *zenity_colorsel_color; static gchar *zenity_colorsel_color;
static gboolean zenity_colorsel_show_palette; static gboolean zenity_colorsel_show_palette;
/* Password Dialog Options */
static gboolean zenity_password_active;
static gboolean zenity_password_show_username;
/* Miscelaneus Options */ /* Miscelaneus Options */
static gboolean zenity_misc_about; static gboolean zenity_misc_about;
static gboolean zenity_misc_version; static gboolean zenity_misc_version;
@ -823,6 +827,30 @@ static GOptionEntry scale_options[] = {
} }
}; };
static GOptionEntry password_dialog_options[] = {
{
"password",
'\0',
G_OPTION_FLAG_IN_MAIN,
G_OPTION_ARG_NONE,
&zenity_password_active,
N_("Display password dialog"),
NULL
},
{
"username",
'\0',
0,
G_OPTION_ARG_NONE,
&zenity_password_show_username,
N_("Display the username option"),
NULL
},
{
NULL
}
};
static GOptionEntry color_selection_options[] = { static GOptionEntry color_selection_options[] = {
{ {
"color-selection", "color-selection",
@ -901,6 +929,7 @@ zenity_option_init (void) {
results->tree_data = g_new0 (ZenityTreeData, 1); results->tree_data = g_new0 (ZenityTreeData, 1);
results->notification_data = g_new0 (ZenityNotificationData, 1); results->notification_data = g_new0 (ZenityNotificationData, 1);
results->color_data = g_new0 (ZenityColorData, 1); results->color_data = g_new0 (ZenityColorData, 1);
results->password_data = g_new0 (ZenityPasswordData, 1);
} }
void void
@ -938,7 +967,7 @@ zenity_option_free (void) {
if (zenity_colorsel_color) if (zenity_colorsel_color)
g_free (zenity_colorsel_color); g_free (zenity_colorsel_color);
g_option_context_free (ctx); g_option_context_free (ctx);
} }
@ -1170,6 +1199,18 @@ zenity_color_pre_callback (GOptionContext *context,
return TRUE; return TRUE;
} }
static gboolean
zenity_password_pre_callback (GOptionContext *context,
GOptionGroup *group,
gpointer data,
GError **error)
{
zenity_password_active = FALSE;
zenity_password_show_username = FALSE;
return TRUE;
}
static gboolean static gboolean
zenity_misc_pre_callback (GOptionContext *context, zenity_misc_pre_callback (GOptionContext *context,
GOptionGroup *group, GOptionGroup *group,
@ -1559,6 +1600,24 @@ zenity_color_post_callback (GOptionContext *context,
return TRUE; return TRUE;
} }
static gboolean
zenity_password_post_callback (GOptionContext *context,
GOptionGroup *group,
gpointer data,
GError **error)
{
zenity_option_set_dialog_mode (zenity_password_active, MODE_PASSWORD);
if (results->mode == MODE_PASSWORD) {
results->password_data->username = zenity_password_show_username;
} else {
if (zenity_password_show_username)
zenity_option_error (zenity_option_get_name(password_dialog_options, &zenity_password_show_username),
ERROR_SUPPORT);
}
return TRUE;
}
static gboolean static gboolean
zenity_misc_post_callback (GOptionContext *context, zenity_misc_post_callback (GOptionContext *context,
GOptionGroup *group, GOptionGroup *group,
@ -1733,6 +1792,17 @@ zenity_create_context (void)
g_option_group_set_translation_domain (a_group, GETTEXT_PACKAGE); g_option_group_set_translation_domain (a_group, GETTEXT_PACKAGE);
g_option_context_add_group(tmp_ctx, a_group); g_option_context_add_group(tmp_ctx, a_group);
/* Adds password dialog option entries */
a_group = g_option_group_new("password",
N_("Password dialog options"),
N_("Show password dialog options"), NULL, NULL);
g_option_group_add_entries (a_group, password_dialog_options);
g_option_group_set_parse_hooks (a_group,
zenity_password_pre_callback, zenity_password_post_callback);
g_option_group_set_error_hook (a_group, zenity_option_error_callback);
g_option_group_set_translation_domain (a_group, GETTEXT_PACKAGE);
g_option_context_add_group(tmp_ctx, a_group);
/* Adds misc option entries */ /* Adds misc option entries */
a_group = g_option_group_new("misc", a_group = g_option_group_new("misc",
N_("Miscellaneous options"), N_("Miscellaneous options"),

View File

@ -46,6 +46,7 @@ typedef enum {
MODE_INFO, MODE_INFO,
MODE_NOTIFICATION, MODE_NOTIFICATION,
MODE_COLOR, MODE_COLOR,
MODE_PASSWORD,
MODE_ABOUT, MODE_ABOUT,
MODE_VERSION, MODE_VERSION,
MODE_LAST MODE_LAST
@ -72,6 +73,7 @@ typedef struct {
ZenityTreeData *tree_data; ZenityTreeData *tree_data;
ZenityNotificationData *notification_data; ZenityNotificationData *notification_data;
ZenityColorData *color_data; ZenityColorData *color_data;
ZenityPasswordData *password_data;
} ZenityParsingOptions; } ZenityParsingOptions;
void zenity_option_error (gchar *string, void zenity_option_error (gchar *string,

169
src/password.c Normal file
View File

@ -0,0 +1,169 @@
/*
* password.c
*
* Copyright (C) 2010 Berislav Kovacki
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 121 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
* Authors: Arx Cruz <arxcruz@gmail.com>
*/
#include "config.h"
#include <string.h>
#include "zenity.h"
#include "util.h"
static ZenityData *zen_data;
static void zenity_password_dialog_response (GtkWidget *widget, int response, gpointer data);
void zenity_password_dialog (ZenityData *data, ZenityPasswordData *password_data)
{
GtkWidget *dialog;
GtkWidget *image;
GtkWidget *hbox;
GtkWidget *vbox_labels;
GtkWidget *vbox_entries;
GtkWidget *label;
GtkWidget *align;
zen_data = data;
dialog = gtk_dialog_new ();
gtk_dialog_add_button(GTK_DIALOG(dialog),
GTK_STOCK_CANCEL,
GTK_RESPONSE_CANCEL);
gtk_dialog_add_button(GTK_DIALOG(dialog),
GTK_STOCK_OK,
GTK_RESPONSE_OK);
image = gtk_image_new_from_stock(GTK_STOCK_DIALOG_AUTHENTICATION,
GTK_ICON_SIZE_DIALOG);
gtk_dialog_set_default_response(GTK_DIALOG(dialog),
GTK_RESPONSE_OK);
hbox = gtk_hbox_new(FALSE, 5);
gtk_box_pack_start(GTK_BOX(hbox),
image,
FALSE,
FALSE,
12);
label = gtk_label_new(_("Type your password"));
gtk_box_pack_start(GTK_BOX(hbox),
label,
FALSE,
FALSE,
12);
gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
hbox,
FALSE,
TRUE,
5);
vbox_labels = gtk_vbox_new(FALSE, 5);
vbox_entries = gtk_vbox_new(FALSE, 5);
hbox = gtk_hbox_new(FALSE, 5);
gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
hbox,
FALSE,
TRUE,
5);
gtk_box_pack_start(GTK_BOX(hbox),
vbox_labels,
FALSE,
TRUE,
12);
gtk_box_pack_start(GTK_BOX(hbox),
vbox_entries,
TRUE,
TRUE,
12);
if(password_data->username) {
align = gtk_alignment_new(0.0, 0.12, 0.0, 0.0);
label = gtk_label_new(_("Username:"));
gtk_container_add(GTK_CONTAINER(align), label);
gtk_box_pack_start(GTK_BOX(vbox_labels),
align,
TRUE,
FALSE,
12);
password_data->entry_username = gtk_entry_new();
gtk_box_pack_start(GTK_BOX(vbox_entries),
password_data->entry_username,
TRUE,
TRUE,
12);
}
align = gtk_alignment_new(0.0, 0.5, 0.0, 0.0);
label = gtk_label_new(_("Password:"));
gtk_container_add(GTK_CONTAINER(align), label);
gtk_box_pack_start(GTK_BOX(vbox_labels),
align,
TRUE,
FALSE,
12);
password_data->entry_password = gtk_entry_new();
gtk_entry_set_visibility(GTK_ENTRY(password_data->entry_password),
FALSE);
gtk_box_pack_start(GTK_BOX(vbox_entries),
password_data->entry_password,
TRUE,
TRUE,
12);
if (data->dialog_title)
gtk_window_set_title (GTK_WINDOW (dialog), data->dialog_title);
g_signal_connect (G_OBJECT (dialog), "response",
G_CALLBACK (zenity_password_dialog_response),
password_data);
gtk_widget_show_all(GTK_WIDGET(GTK_DIALOG(dialog)->vbox));
zenity_util_show_dialog (dialog);
if (data->timeout_delay > 0) {
g_timeout_add (data->timeout_delay * 1000,
(GSourceFunc) zenity_util_timeout_handle,
NULL);
}
gtk_main();
}
static void
zenity_password_dialog_response (GtkWidget *widget, int response, gpointer data)
{
ZenityPasswordData *password_data = (ZenityPasswordData *) data;
switch (response) {
case GTK_RESPONSE_OK:
zen_data->exit_code = zenity_util_return_exit_code (ZENITY_OK);
g_print ("%s\n", gtk_entry_get_text (GTK_ENTRY(password_data->entry_password)));
break;
case GTK_RESPONSE_CANCEL:
zen_data->exit_code = zenity_util_return_exit_code (ZENITY_CANCEL);
break;
default:
zen_data->exit_code = zenity_util_return_exit_code (ZENITY_ESC);
break;
}
gtk_main_quit ();
}

View File

@ -131,6 +131,13 @@ typedef struct {
gboolean show_palette; gboolean show_palette;
} ZenityColorData; } ZenityColorData;
typedef struct {
gboolean username;
gchar *password;
GtkWidget *entry_username;
GtkWidget *entry_password;
} ZenityPasswordData;
void zenity_calendar (ZenityData *data, void zenity_calendar (ZenityData *data,
ZenityCalendarData *calendar_data); ZenityCalendarData *calendar_data);
void zenity_msg (ZenityData *data, void zenity_msg (ZenityData *data,
@ -153,6 +160,9 @@ void zenity_scale (ZenityData *data,
ZenityScaleData *scale_data); ZenityScaleData *scale_data);
void zenity_about (ZenityData *data); void zenity_about (ZenityData *data);
void zenity_password_dialog (ZenityData *data,
ZenityPasswordData *password_data);
G_END_DECLS G_END_DECLS
#endif /* ZENITY_H */ #endif /* ZENITY_H */