summaryrefslogtreecommitdiff
path: root/src/password.c
blob: fc270216d64e7b416a1192440c01ff693d3d006a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* vim: colorcolumn=80 ts=4 sw=4
 */
/*
 * password.c
 *
 * Copyright © 2010 Arx Cruz
 * Copyright © 2021-2023 Logan Rathbone
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser 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 "util.h"
#include "zenity.h"

#include <string.h>

#include <config.h>

static ZenityData *zen_data;

static void zenity_password_dialog_response (GtkWidget *widget, char *rstr, gpointer data);

void
zenity_password_dialog (ZenityData *data, ZenityPasswordData *password_data)
{
	GtkBuilder *builder;
	GtkWidget *dialog;
	GtkWidget *grid;
	GtkWidget *label;
	int pass_row = 0;

	/* Set global */
	zen_data = data;

	builder = zenity_util_load_ui_file ("zenity_password_dialog", "zenity_password_box", NULL);

	if (builder == NULL)
	{
		data->exit_code = zenity_util_return_exit_code (ZENITY_ERROR);
		return;
	}

	dialog = GTK_WIDGET(gtk_builder_get_object (builder,
				"zenity_password_dialog"));

	if (data->extra_label)
	{
		ZENITY_UTIL_ADD_EXTRA_LABELS (dialog)
	}

	if (data->ok_label) {
		ZENITY_UTIL_SETUP_OK_BUTTON_LABEL (dialog);
	}

	if (data->cancel_label)
	{
		ZENITY_UTIL_SETUP_CANCEL_BUTTON_LABEL (dialog);
	}

	grid = GTK_WIDGET(gtk_builder_get_object (builder,
				"zenity_password_grid"));

	/* Checks if username has been passed as a parameter */
	if (password_data->username)
	{
		/* Change the password label to ask for both username and password */
		label = GTK_WIDGET(gtk_builder_get_object (builder,
					"zenity_password_title"));
		gtk_label_set_text (GTK_LABEL(label),
				_("Type your username and password"));

		/* Add the username label and entry and increment the row for the
		 * password entry so it will be added below the username.
		 */
		label = gtk_label_new (_("Username:"));
		gtk_grid_attach (GTK_GRID(grid), label,
				0,		/* col */
				0,		/* row */
				1, 1);	/* width/height by cell. */	

		password_data->entry_username = gtk_entry_new ();
		gtk_grid_attach (GTK_GRID(grid), password_data->entry_username,
				1,
				0,
				1, 1);

		++pass_row;
	}

	label = gtk_label_new (_("Password:"));
	gtk_grid_attach (GTK_GRID(grid), label,
			0,		/* col */
			pass_row,		/* row */
			1, 1);	/* width/height by cell. */	

	password_data->entry_password = gtk_entry_new ();
	gtk_entry_set_visibility (GTK_ENTRY(password_data->entry_password), FALSE);
	gtk_entry_set_input_purpose (GTK_ENTRY(password_data->entry_password),
			GTK_INPUT_PURPOSE_PASSWORD);
	gtk_entry_set_activates_default (GTK_ENTRY(password_data->entry_password),
			TRUE);
	gtk_grid_attach (GTK_GRID(grid), password_data->entry_password,
			1,
			pass_row,
			1, 1);

	if (data->dialog_title)
		adw_message_dialog_set_heading (ADW_MESSAGE_DIALOG(dialog), data->dialog_title);;

	if (data->modal)
		gtk_window_set_modal (GTK_WINDOW(dialog), TRUE);

	g_signal_connect (dialog, "response", G_CALLBACK(zenity_password_dialog_response), password_data);

	zenity_util_show_dialog (dialog);

	if (data->timeout_delay > 0)
	{
		g_timeout_add_seconds (data->timeout_delay,
			(GSourceFunc) zenity_util_timeout_handle,
			dialog);
	}
	zenity_util_gapp_main (GTK_WINDOW(dialog));
}

static void
zenity_password_dialog_response (GtkWidget *widget, char *rstr, gpointer data)
{
	ZenityPasswordData *password_data = data;
	GtkEntryBuffer *user_buff, *pass_buff;
	ZenityExitCode response = zenity_util_parse_dialog_response (rstr);

	user_buff = gtk_entry_get_buffer (GTK_ENTRY(password_data->entry_username));
	pass_buff = gtk_entry_get_buffer (GTK_ENTRY(password_data->entry_password));

	switch (response)
	{
		case ZENITY_OK:
			zenity_util_exit_code_with_data (ZENITY_OK, zen_data);
			if (password_data->username) {
				g_print ("%s|%s\n",
					gtk_entry_buffer_get_text (user_buff),
					gtk_entry_buffer_get_text (pass_buff));
			}
			else {
				g_print ("%s\n",
					gtk_entry_buffer_get_text (pass_buff));
			}
			break;

		case ZENITY_CANCEL:
			zen_data->exit_code = zenity_util_return_exit_code (ZENITY_CANCEL);
			break;

		default:
			if (zen_data->extra_label &&
				response < (int)g_strv_length (zen_data->extra_label))
				printf ("%s\n", zen_data->extra_label[response]);
			zen_data->exit_code = zenity_util_return_exit_code (ZENITY_ESC);
			break;
	}
	zenity_util_gapp_quit (GTK_WINDOW(widget), zen_data);
}