1 /*
2 * Virt Viewer: A virtual machine console viewer
3 *
4 * Copyright (C) 2007-2012 Red Hat, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * Author: Daniel P. Berrange <berrange@redhat.com>
21 */
22
23 #include <config.h>
24
25 #include <gtk/gtk.h>
26 #include <glib/gi18n.h>
27 #include <string.h>
28
29 #ifdef HAVE_GTK_VNC
30 #include <vncdisplay.h>
31 #endif
32
33 #include "virt-viewer-auth.h"
34 #include "virt-viewer-util.h"
35
36
37 struct _VirtViewerAuth
38 {
39 GtkDialog parent;
40 GtkWidget *credUsername;
41 GtkWidget *credPassword;
42 GtkWidget *promptUsername;
43 GtkWidget *promptPassword;
44 GtkWidget *message;
45 };
46
G_DEFINE_TYPE(VirtViewerAuth,virt_viewer_auth,GTK_TYPE_DIALOG)47 G_DEFINE_TYPE(VirtViewerAuth, virt_viewer_auth, GTK_TYPE_DIALOG)
48
49 static void
50 virt_viewer_auth_class_init(VirtViewerAuthClass *klass)
51 {
52 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
53
54 gtk_widget_class_set_template_from_resource(widget_class,
55 VIRT_VIEWER_RESOURCE_PREFIX "/ui/virt-viewer-auth.ui");
56
57 gtk_widget_class_bind_template_child(widget_class,
58 VirtViewerAuth,
59 message);
60 gtk_widget_class_bind_template_child(widget_class,
61 VirtViewerAuth,
62 credUsername);
63 gtk_widget_class_bind_template_child(widget_class,
64 VirtViewerAuth,
65 credPassword);
66 gtk_widget_class_bind_template_child(widget_class,
67 VirtViewerAuth,
68 promptUsername);
69 gtk_widget_class_bind_template_child(widget_class,
70 VirtViewerAuth,
71 promptPassword);
72 }
73
74 static void
virt_viewer_auth_init(VirtViewerAuth * self)75 virt_viewer_auth_init(VirtViewerAuth *self)
76 {
77 gtk_widget_init_template(GTK_WIDGET(self));
78
79 gtk_dialog_set_default_response(GTK_DIALOG(self), GTK_RESPONSE_OK);
80 }
81
virt_viewer_auth_new(GtkWindow * parent)82 VirtViewerAuth *virt_viewer_auth_new(GtkWindow *parent)
83 {
84 return g_object_new(VIRT_VIEWER_TYPE_AUTH,
85 "transient-for", parent,
86 NULL);
87 }
88
89 static void
show_password(GtkEntry * entry,GtkEntryIconPosition pos G_GNUC_UNUSED,GdkEvent event G_GNUC_UNUSED)90 show_password(GtkEntry *entry,
91 GtkEntryIconPosition pos G_GNUC_UNUSED,
92 GdkEvent event G_GNUC_UNUSED)
93 {
94 gboolean visible = gtk_entry_get_visibility(entry);
95 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(entry),
96 GTK_ENTRY_ICON_SECONDARY,
97 visible ?
98 "eye-not-looking-symbolic" :
99 "eye-open-negative-filled-symbolic");
100 gtk_entry_set_visibility(entry, !visible);
101 }
102
103 /* NOTE: if username is provided, and *username is non-NULL, the user input
104 * field will be pre-filled with this value. The existing string will be freed
105 * before setting the output parameter to the user-entered value.
106 */
107 gboolean
virt_viewer_auth_collect_credentials(VirtViewerAuth * self,const char * type,const char * address,char ** username,char ** password)108 virt_viewer_auth_collect_credentials(VirtViewerAuth *self,
109 const char *type,
110 const char *address,
111 char **username,
112 char **password)
113 {
114 int response;
115 char *message;
116
117 gtk_entry_set_text(GTK_ENTRY(self->credUsername), "");
118 gtk_entry_set_text(GTK_ENTRY(self->credPassword), "");
119 if (username) {
120 gtk_widget_show(self->credUsername);
121 gtk_widget_show(self->promptUsername);
122 if (*username) {
123 gtk_entry_set_text(GTK_ENTRY(self->credUsername), *username);
124 /* if username is pre-filled, move focus to password field */
125 if (password) {
126 gtk_widget_grab_focus(self->credPassword);
127 }
128 }
129 } else {
130 gtk_widget_hide(self->credUsername);
131 gtk_widget_hide(self->promptUsername);
132 }
133 if (password) {
134 gtk_widget_show(self->credPassword);
135 gtk_widget_show(self->promptPassword);
136 } else {
137 gtk_widget_hide(self->credPassword);
138 gtk_widget_hide(self->promptPassword);
139 }
140
141 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(self->credPassword),
142 GTK_ENTRY_ICON_SECONDARY,
143 "eye-not-looking-symbolic");
144 gtk_entry_set_icon_sensitive(GTK_ENTRY(self->credPassword),
145 GTK_ENTRY_ICON_SECONDARY,
146 TRUE);
147 gtk_entry_set_icon_activatable(GTK_ENTRY(self->credPassword),
148 GTK_ENTRY_ICON_SECONDARY,
149 TRUE);
150 gtk_entry_set_icon_tooltip_text(GTK_ENTRY(self->credPassword),
151 GTK_ENTRY_ICON_SECONDARY,
152 _("Show / hide password text"));
153
154 g_signal_connect(self->credPassword, "icon-press",
155 G_CALLBACK(show_password), NULL);
156
157 if (address) {
158 message = g_strdup_printf(_("Authentication is required for the %s connection to:\n\n<b>%s</b>\n\n"),
159 type,
160 address);
161 } else {
162 message = g_strdup_printf(_("Authentication is required for the %s connection:\n"),
163 type);
164 }
165
166 gtk_label_set_markup(GTK_LABEL(self->message), message);
167 g_free(message);
168
169 gtk_widget_show(GTK_WIDGET(self));
170 response = gtk_dialog_run(GTK_DIALOG(self));
171 gtk_widget_hide(GTK_WIDGET(self));
172
173 if (response == GTK_RESPONSE_OK) {
174 if (username) {
175 g_free(*username);
176 *username = g_strdup(gtk_entry_get_text(GTK_ENTRY(self->credUsername)));
177 }
178 if (password)
179 *password = g_strdup(gtk_entry_get_text(GTK_ENTRY(self->credPassword)));
180 }
181
182 return response == GTK_RESPONSE_OK;
183 }
184