All pastes #779185 Raw Edit

Mine

public text v1 · immutable
·published 2007-11-18 08:14 UTC
rendered paste body
#include <gtk/gtktable.h>
#include <gtk/gtkentry.h>
#include <gtk/gtklabel.h>
#include <gtk/gtkbutton.h>
#include <gtk/gtkstock.h>
#include "magicssh.h"

GtkWidget* magicssh_tunnel_widget_new(void) {
    return GTK_WIDGET(g_object_new(MAGICSSH_TUNNEL_WIDGET_TYPE, NULL));
}

gboolean magicssh_tunnel_widget_validate(MagicsshTunnelWidget *tw) {
    if (tw->remote_host) {
        //g_print("validating: %d, %s, %d\n", tw->local_port, tw->remote_host, tw->remote_port);
    } else {
        g_print("validating: %d, (null), %d\n", tw->local_port, tw->remote_port);
    }
    if (tw->local_port != 0 && tw->remote_host != NULL && tw->remote_port != 0) {
        tw->tunnel = magicssh_tunnel_new();
        tw->tunnel->local_port = tw->local_port;
        tw->tunnel->remote_host = tw->remote_host;
        tw->tunnel->remote_port = tw->remote_port;
        g_print("Made a tunnel!\n");
        return TRUE;
    }
    tw->tunnel = NULL;
    return FALSE;
}

void remove_button_callback(GtkWidget* widget, gpointer void_tw) {
    MagicsshTunnelWidget* tw = (MagicsshTunnelWidget*)void_tw;
    gtk_widget_destroy(tw);
    widget = widget;
    tw = tw;
}

gboolean local_port_text_focus_out(GtkWidget *widget, gpointer void_tw) {
    MagicsshTunnelWidget* tw = (MagicsshTunnelWidget*) void_tw;
    const gchar* text = gtk_entry_get_text(GTK_ENTRY(widget));
    g_print("local_port_text_focus_out\n");
    tw->local_port = 0;
    if (text[0] == '\0') {
        magicssh_tunnel_widget_validate(tw);
        return FALSE;
    }
    guint64 local_port = g_ascii_strtoull(text, NULL, 0);
    if (local_port == 0 || local_port > 65536) {
        gtk_widget_grab_focus(widget);
        return TRUE;
    }
    tw->local_port = (guint16)local_port;
    magicssh_tunnel_widget_validate(tw);
    return FALSE;
}

gboolean remote_host_text_focus_out(GtkWidget *widget, gpointer void_tw) {
    MagicsshTunnelWidget* tw = (MagicsshTunnelWidget*) void_tw;
    const gchar* text = gtk_entry_get_text(GTK_ENTRY(widget));
    g_print("remote_host_text_focus_out\n");
    tw->remote_host = NULL;
    if (text[0] == '\0') {
        magicssh_tunnel_widget_validate(tw);
        return FALSE;
    }
    /* XXX Add validation for hostnames */
    tw->remote_host = g_strdup(text);
    magicssh_tunnel_widget_validate(tw);
    return FALSE;
}

gboolean remote_port_text_focus_out(GtkWidget *widget, gpointer void_tw) {
    MagicsshTunnelWidget* tw = (MagicsshTunnelWidget*) void_tw;
    const gchar* text = gtk_entry_get_text(GTK_ENTRY(widget));
    g_print("remote_port_text_focus_out\n");
    tw->remote_port = 0;
    if (text[0] == '\0') {
        magicssh_tunnel_widget_validate(tw);
        return FALSE;
    }
    guint64 remote_port = g_ascii_strtoull(text, NULL, 0);
    if (remote_port == 0 || remote_port > 65536) {
        gtk_widget_grab_focus(widget);
        return TRUE;
    }
    tw->remote_port = (guint16)remote_port;
    magicssh_tunnel_widget_validate(tw);
    return FALSE;
}

static void magicssh_tunnel_widget_init(GTypeInstance *instance, gpointer g_class) {
    MagicsshTunnelWidget *self = (MagicsshTunnelWidget*) instance;
    MagicsshTunnelWidgetClass *klass = (MagicsshTunnelWidgetClass*)g_class;

    g_print("magicssh_tunnel_widget_init\n");
    self->local_port = 0;
    self->remote_host = NULL;
    self->remote_port = 0;
    self->tunnel = NULL;

    GtkWidget* label;
    
    klass = klass;
    
    gtk_table_resize(GTK_TABLE(self), 5, 2);
    gtk_table_set_homogeneous(GTK_TABLE(self), FALSE);

    gtk_table_set_col_spacings(GTK_TABLE(self), 10);

    label = gtk_label_new("Local Port");
    gtk_table_attach_defaults(GTK_TABLE(self), label, 0, 1, 0, 1);
    gtk_widget_show(label);

    self->local_port_text = gtk_entry_new_with_max_length(5);
    gtk_table_attach_defaults(GTK_TABLE(self), self->local_port_text, 0, 1, 1, 2);
    g_signal_connect(self->local_port_text, "focus-out-event", G_CALLBACK(local_port_text_focus_out), (gpointer)self);
    gtk_widget_show(self->local_port_text);

    label = gtk_label_new("Remote Host");
    gtk_table_attach_defaults(GTK_TABLE(self), label, 1, 2, 0, 1);
    gtk_widget_show(label);
    
    self->remote_host_text = gtk_entry_new();
    gtk_table_attach_defaults(GTK_TABLE(self), self->remote_host_text, 1, 2, 1, 2);
    g_signal_connect(self->remote_host_text, "focus-out-event", G_CALLBACK(remote_host_text_focus_out), (gpointer)self);
    gtk_widget_show(self->remote_host_text);

    label = gtk_label_new("Remote Port");
    gtk_table_attach_defaults(GTK_TABLE(self), label, 2, 3, 0, 1);
    gtk_widget_show(label);

    self->remote_port_text = gtk_entry_new_with_max_length(5);
    gtk_table_attach_defaults(GTK_TABLE(self), self->remote_port_text, 2, 3, 1, 2);
    g_signal_connect(self->remote_port_text, "focus-out-event", G_CALLBACK(remote_port_text_focus_out), (gpointer)self);
    gtk_widget_show(self->remote_port_text);

    self->remove_button = gtk_button_new();
    gtk_button_set_image(GTK_BUTTON(self->remove_button), gtk_image_new_from_stock(GTK_STOCK_REMOVE, GTK_ICON_SIZE_BUTTON));
    gtk_table_attach_defaults(GTK_TABLE(self), self->remove_button, 3, 5, 0, 2);
    gtk_widget_set_sensitive(self->remove_button, FALSE);
    g_signal_connect(self->remove_button, "clicked", G_CALLBACK(remove_button_callback), (gpointer)self);
    gtk_widget_show(self->remove_button);
}

GType magicssh_tunnel_widget_get_type (void) {
    static GType type = 0;
    if (type == 0) {
        static const GTypeInfo info = {
            sizeof (MagicsshTunnelWidgetClass),
            NULL,   /* base_init */
            NULL,   /* base_finalize */
            NULL,   /* class_init */
            NULL,   /* class_finalize */
            NULL,   /* class_data */
            sizeof (MagicsshTunnelWidget),
            0,      /* n_preallocs */
            magicssh_tunnel_widget_init, /* instance_init */
        };
        type = g_type_register_static (GTK_TYPE_TABLE,
                "MagicsshTunnelWidget",
                &info, 0);
    }
    return type;
}