src: switch auth dialog to use a GTK UI template

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
Daniel P. Berrangé 2021-02-19 13:45:29 +00:00 committed by Daniel P. Berrangé
parent d59f550d21
commit 247d91d889
7 changed files with 108 additions and 52 deletions

View File

@ -312,7 +312,7 @@ parse_ovirt_uri(const gchar *uri_str, char **rest_uri, char **name, char **usern
}
static gboolean
authenticate_cb(RestProxy *proxy, G_GNUC_UNUSED RestProxyAuth *auth,
authenticate_cb(RestProxy *proxy, RestProxyAuth *rstauth,
G_GNUC_UNUSED gboolean retrying, gpointer user_data)
{
gchar *username = NULL;
@ -320,6 +320,7 @@ authenticate_cb(RestProxy *proxy, G_GNUC_UNUSED RestProxyAuth *auth,
VirtViewerWindow *window;
gboolean success = FALSE;
gboolean kiosk = FALSE;
VirtViewerAuth *auth;
g_object_get(proxy,
"username", &username,
@ -331,8 +332,10 @@ authenticate_cb(RestProxy *proxy, G_GNUC_UNUSED RestProxyAuth *auth,
username = g_strdup(g_get_user_name());
window = virt_viewer_app_get_main_window(VIRT_VIEWER_APP(user_data));
auth = virt_viewer_auth_new(virt_viewer_window_get_window(window));
do {
success = virt_viewer_auth_collect_credentials(virt_viewer_window_get_window(window),
success = virt_viewer_auth_collect_credentials(auth,
"oVirt",
NULL,
&username, &password);
@ -344,9 +347,10 @@ authenticate_cb(RestProxy *proxy, G_GNUC_UNUSED RestProxyAuth *auth,
"password", password,
NULL);
} else {
rest_proxy_auth_cancel(auth);
rest_proxy_auth_cancel(rstauth);
}
gtk_widget_destroy(GTK_WIDGET(auth));
g_free(username);
g_free(password);
return success;

View File

@ -2,7 +2,7 @@
<!-- Generated with glade 3.38.2 -->
<interface>
<requires lib="gtk+" version="3.18"/>
<object class="GtkDialog" id="auth">
<template class="VirtViewerAuth" parent="GtkDialog">
<property name="can-focus">False</property>
<property name="border-width">5</property>
<property name="title" translatable="yes">Authentication required</property>
@ -87,7 +87,7 @@
<property name="column-spacing">6</property>
<property name="row-spacing">6</property>
<child>
<object class="GtkLabel" id="prompt-password">
<object class="GtkLabel" id="promptPassword">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">Password:</property>
@ -99,7 +99,7 @@
</packing>
</child>
<child>
<object class="GtkLabel" id="prompt-username">
<object class="GtkLabel" id="promptUsername">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">Username:</property>
@ -107,7 +107,7 @@
</object>
</child>
<child>
<object class="GtkEntry" id="cred-username">
<object class="GtkEntry" id="credUsername">
<property name="visible">True</property>
<property name="can-focus">True</property>
</object>
@ -117,7 +117,7 @@
</packing>
</child>
<child>
<object class="GtkEntry" id="cred-password">
<object class="GtkEntry" id="credPassword">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="visibility">False</property>
@ -143,5 +143,5 @@
<action-widget response="-6">button-cancel</action-widget>
<action-widget response="-5">button-ok</action-widget>
</action-widgets>
</object>
</template>
</interface>

View File

@ -33,6 +33,59 @@
#include "virt-viewer-auth.h"
#include "virt-viewer-util.h"
struct _VirtViewerAuth
{
GtkDialog parent;
GtkWidget *credUsername;
GtkWidget *credPassword;
GtkWidget *promptUsername;
GtkWidget *promptPassword;
GtkWidget *message;
};
G_DEFINE_TYPE(VirtViewerAuth, virt_viewer_auth, GTK_TYPE_DIALOG)
static void
virt_viewer_auth_class_init(VirtViewerAuthClass *klass)
{
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
gtk_widget_class_set_template_from_resource(widget_class,
VIRT_VIEWER_RESOURCE_PREFIX "/ui/virt-viewer-auth.ui");
gtk_widget_class_bind_template_child(widget_class,
VirtViewerAuth,
message);
gtk_widget_class_bind_template_child(widget_class,
VirtViewerAuth,
credUsername);
gtk_widget_class_bind_template_child(widget_class,
VirtViewerAuth,
credPassword);
gtk_widget_class_bind_template_child(widget_class,
VirtViewerAuth,
promptUsername);
gtk_widget_class_bind_template_child(widget_class,
VirtViewerAuth,
promptPassword);
}
static void
virt_viewer_auth_init(VirtViewerAuth *self)
{
gtk_widget_init_template(GTK_WIDGET(self));
gtk_dialog_set_default_response(GTK_DIALOG(self), GTK_RESPONSE_OK);
}
VirtViewerAuth *virt_viewer_auth_new(GtkWindow *parent)
{
return g_object_new(VIRT_VIEWER_TYPE_AUTH,
"transient-for", parent,
NULL);
}
static void
show_password(GtkEntry *entry,
GtkEntryIconPosition pos G_GNUC_UNUSED,
@ -52,56 +105,40 @@ show_password(GtkEntry *entry,
* before setting the output parameter to the user-entered value.
*/
gboolean
virt_viewer_auth_collect_credentials(GtkWindow *window,
virt_viewer_auth_collect_credentials(VirtViewerAuth *self,
const char *type,
const char *address,
char **username,
char **password)
{
GtkWidget *dialog = NULL;
GtkBuilder *creds = virt_viewer_util_load_ui("virt-viewer-auth.ui");
GtkWidget *credUsername;
GtkWidget *credPassword;
GtkWidget *promptUsername;
GtkWidget *promptPassword;
GtkWidget *labelMessage;
int response;
char *message;
dialog = GTK_WIDGET(gtk_builder_get_object(creds, "auth"));
gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_OK);
gtk_window_set_transient_for(GTK_WINDOW(dialog), window);
labelMessage = GTK_WIDGET(gtk_builder_get_object(creds, "message"));
credUsername = GTK_WIDGET(gtk_builder_get_object(creds, "cred-username"));
promptUsername = GTK_WIDGET(gtk_builder_get_object(creds, "prompt-username"));
credPassword = GTK_WIDGET(gtk_builder_get_object(creds, "cred-password"));
promptPassword = GTK_WIDGET(gtk_builder_get_object(creds, "prompt-password"));
gtk_widget_set_sensitive(credUsername, username != NULL);
gtk_widget_set_sensitive(self->credUsername, username != NULL);
if (username && *username) {
gtk_entry_set_text(GTK_ENTRY(credUsername), *username);
gtk_entry_set_text(GTK_ENTRY(self->credUsername), *username);
/* if username is pre-filled, move focus to password field */
gtk_widget_grab_focus(credPassword);
gtk_widget_grab_focus(self->credPassword);
}
gtk_widget_set_sensitive(promptUsername, username != NULL);
gtk_widget_set_sensitive(credPassword, password != NULL);
gtk_widget_set_sensitive(promptPassword, password != NULL);
gtk_widget_set_sensitive(self->promptUsername, username != NULL);
gtk_widget_set_sensitive(self->credPassword, password != NULL);
gtk_widget_set_sensitive(self->promptPassword, password != NULL);
gtk_entry_set_icon_from_icon_name(GTK_ENTRY(credPassword),
gtk_entry_set_icon_from_icon_name(GTK_ENTRY(self->credPassword),
GTK_ENTRY_ICON_SECONDARY,
"eye-not-looking-symbolic");
gtk_entry_set_icon_sensitive(GTK_ENTRY(credPassword),
gtk_entry_set_icon_sensitive(GTK_ENTRY(self->credPassword),
GTK_ENTRY_ICON_SECONDARY,
TRUE);
gtk_entry_set_icon_activatable(GTK_ENTRY(credPassword),
gtk_entry_set_icon_activatable(GTK_ENTRY(self->credPassword),
GTK_ENTRY_ICON_SECONDARY,
TRUE);
gtk_entry_set_icon_tooltip_text(GTK_ENTRY(credPassword),
gtk_entry_set_icon_tooltip_text(GTK_ENTRY(self->credPassword),
GTK_ENTRY_ICON_SECONDARY,
_("Show / hide password text"));
g_signal_connect(credPassword, "icon-press", G_CALLBACK(show_password), credPassword);
g_signal_connect(self->credPassword, "icon-press",
G_CALLBACK(show_password), NULL);
if (address) {
message = g_strdup_printf(_("Authentication is required for the %s connection to:\n\n<b>%s</b>\n\n"),
@ -112,24 +149,21 @@ virt_viewer_auth_collect_credentials(GtkWindow *window,
type);
}
gtk_label_set_markup(GTK_LABEL(labelMessage), message);
gtk_label_set_markup(GTK_LABEL(self->message), message);
g_free(message);
gtk_widget_show_all(dialog);
response = gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_hide(dialog);
gtk_widget_show_all(GTK_WIDGET(self));
response = gtk_dialog_run(GTK_DIALOG(self));
gtk_widget_hide(GTK_WIDGET(self));
if (response == GTK_RESPONSE_OK) {
if (username) {
g_free(*username);
*username = g_strdup(gtk_entry_get_text(GTK_ENTRY(credUsername)));
*username = g_strdup(gtk_entry_get_text(GTK_ENTRY(self->credUsername)));
}
if (password)
*password = g_strdup(gtk_entry_get_text(GTK_ENTRY(credPassword)));
*password = g_strdup(gtk_entry_get_text(GTK_ENTRY(self->credPassword)));
}
gtk_widget_destroy(GTK_WIDGET(dialog));
g_object_unref(G_OBJECT(creds));
return response == GTK_RESPONSE_OK;
}

View File

@ -26,7 +26,18 @@
#include "virt-viewer-session.h"
gboolean virt_viewer_auth_collect_credentials(GtkWindow *window,
#define VIRT_VIEWER_TYPE_AUTH virt_viewer_auth_get_type()
G_DECLARE_FINAL_TYPE(VirtViewerAuth,
virt_viewer_auth,
VIRT_VIEWER,
AUTH,
GtkDialog)
GType virt_viewer_auth_get_type(void) G_GNUC_CONST;
VirtViewerAuth *virt_viewer_auth_new(GtkWindow *parent);
gboolean virt_viewer_auth_collect_credentials(VirtViewerAuth *auth,
const char *type,
const char *address,
char **username,

View File

@ -720,6 +720,7 @@ virt_viewer_session_spice_main_channel_event(SpiceChannel *channel,
{
const GError *error = NULL;
gchar *host = NULL;
VirtViewerAuth *auth = virt_viewer_auth_new(self->main_window);
g_debug("main channel: auth failure (wrong username/password?)");
error = spice_channel_get_error(channel);
@ -745,11 +746,12 @@ virt_viewer_session_spice_main_channel_event(SpiceChannel *channel,
}
g_object_get(self->session, "host", &host, NULL);
ret = virt_viewer_auth_collect_credentials(self->main_window,
ret = virt_viewer_auth_collect_credentials(auth,
"SPICE",
host,
username_required ? &user : NULL,
&password);
gtk_widget_destroy(GTK_WIDGET(auth));
g_free(host);
if (!ret) {
/* ret is false when dialog did not return GTK_RESPONSE_OK. We
@ -780,9 +782,10 @@ virt_viewer_session_spice_main_channel_event(SpiceChannel *channel,
if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_PROXY_NEED_AUTH) ||
g_error_matches(error, G_IO_ERROR, G_IO_ERROR_PROXY_AUTH_FAILED)) {
SpiceURI *proxy = spice_session_get_proxy_uri(self->session);
VirtViewerAuth *auth = virt_viewer_auth_new(self->main_window);
g_warn_if_fail(proxy != NULL);
ret = virt_viewer_auth_collect_credentials(self->main_window,
ret = virt_viewer_auth_collect_credentials(auth,
"proxy", spice_uri_get_hostname(proxy),
&user, &password);
if (!ret) {

View File

@ -324,10 +324,12 @@ virt_viewer_session_vnc_auth_credential(GtkWidget *src G_GNUC_UNUSED,
}
if (wantUsername || wantPassword) {
gboolean ret = virt_viewer_auth_collect_credentials(self->main_window,
VirtViewerAuth *auth = virt_viewer_auth_new(self->main_window);
gboolean ret = virt_viewer_auth_collect_credentials(auth,
"VNC", NULL,
wantUsername ? &username : NULL,
wantPassword ? &password : NULL);
gtk_widget_destroy(GTK_WIDGET(auth));
if (!ret) {
vnc_display_close(self->vnc);

View File

@ -984,14 +984,16 @@ virt_viewer_auth_libvirt_credentials(virConnectCredentialPtr cred,
if (username || password) {
VirtViewerWindow *vwin = virt_viewer_app_get_main_window(VIRT_VIEWER_APP(self));
GtkWindow *win = virt_viewer_window_get_window(vwin);
VirtViewerAuth *auth = virt_viewer_auth_new(win);
if (username && (*username == NULL || **username == '\0'))
*username = g_strdup(g_get_user_name());
self->auth_cancelled = !virt_viewer_auth_collect_credentials(win,
self->auth_cancelled = !virt_viewer_auth_collect_credentials(auth,
"libvirt",
self->uri,
username, password);
gtk_widget_destroy(GTK_WIDGET(auth));
if (self->auth_cancelled) {
ret = -1;
goto cleanup;