Logo Search packages:      
Sourcecode: lcrt version File versions  Download package

iqconnect.c

#undef __LCRT_DEBUG__
#include "debug.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <errno.h>
#include <libgen.h>

#include <gdk/gdkkeysyms.h>
#include <gtk/gtk.h>
#include "iwindow.h"
#include "iqconnect.h"
#include "debug.h"
#include "message.h"
#include "cqconnect.h"

int lcrt_qconnect_init_config(struct lcrt_qconnect *lqconnect);
int lcrt_qconnect_load_config(struct lcrt_qconnect *lqconnect);
int lcrt_qconnect_create_config(struct lcrt_qconnect *lqconnect);

int lcrt_create_qconnect(
    struct lcrt_window *parent, 
    GtkWindow *window, 
    lcrt_qconnect_flag_t flag, 
    const char *uname)
{
    struct lcrt_qconnect *lqconnect;

    debug_where();
    GtkWidget *quick_connect;
    GtkWidget *vbox;
    GtkWidget *hbox1;
    GtkWidget *label_protocol;
    GtkWidget *combobox_protocol;
    GtkWidget *hbox2;
    GtkWidget *label_hostname;
    GtkWidget *entry_hostname;
    GtkWidget *hbox4;
    GtkWidget *label_username;
    GtkWidget *entry_username;
    GtkWidget *hbox3;
    GtkWidget *label_firewall;
    GtkWidget *combobox_firewall;
    GtkWidget *label_port;
    GtkWidget *entry_port;
    GtkWidget *frame;
    GtkWidget *alignment;
    GtkWidget *hbox_frame;
    GtkWidget *vbox_frame;
    GtkWidget *vbox3;
    GtkWidget *checkbutton_passwd;
    GtkWidget *vscrollbar;
    GtkWidget *vbox2;
    GtkWidget *button_properties;
    GtkWidget *fixed3;
    GtkWidget *label_frame;
    GtkWidget *hbox7;
    GtkWidget *hbox8;
    GtkWidget *fixed1;
    GtkWidget *checkbutton_show_quick_connect_on_startup;
    GtkWidget *hbox6;
    GtkWidget *fixed2;
    GtkWidget *checkbutton_save_session;
    GtkWidget *checkbutton_open_in_a_tab;
    GtkWidget *dialog_action_area;
    GtkWidget *button_connect;
    GtkWidget *button_cancel;
    GtkAccelGroup *accel_group;
    int i;
    char *proto[LCRT_PROTOCOL_NUMBER] = {LCRT_PROTOCOL_NAME};
    char *firewall[LCRT_FIREWALL_NUMBER] = {LCRT_FIREWALL_NAME};
    debug_where();
    lqconnect = (struct lcrt_qconnect *)calloc(1, sizeof(struct lcrt_qconnect));
    
    if (lqconnect == NULL)
        goto err;

    lqconnect->parent = parent;
    lqconnect->flag = flag;
    lqconnect->uname = uname;
    debug_where();

    lcrt_qconnect_init_config(lqconnect);
    lcrt_qconnect_load_config(lqconnect);

    accel_group = gtk_accel_group_new ();

    quick_connect = gtk_dialog_new ();
    gtk_widget_set_size_request (quick_connect, 320, 360);
    gtk_window_set_transient_for(GTK_WINDOW(quick_connect), 
            window != NULL ? window : GTK_WINDOW(lqconnect->parent->window));
    gtk_widget_show(quick_connect);
    lqconnect->q_connect = quick_connect;
    if (flag == LCRT_QCONNECT_SESSION_OPTION)
        gtk_window_set_title (GTK_WINDOW (quick_connect), lqconnect->config.value[LCRT_Q_SESSION_OPTION]);
    else
        gtk_window_set_title (GTK_WINDOW (quick_connect), lqconnect->config.value[LCRT_Q_CONNECT]);
    gtk_window_set_modal(GTK_WINDOW(quick_connect), TRUE);
    gtk_window_set_position (GTK_WINDOW (quick_connect), GTK_WIN_POS_CENTER_ON_PARENT);
    gtk_window_set_resizable (GTK_WINDOW (quick_connect), FALSE);
    //gtk_window_set_type_hint (GTK_WINDOW (quick_connect), GDK_WINDOW_TYPE_HINT_DIALOG);
    gtk_dialog_set_has_separator (GTK_DIALOG (quick_connect), FALSE);

    g_signal_connect ((gpointer) quick_connect, "delete_event",
                    G_CALLBACK (lcrt_qconnect_on_window_delete),
                    lqconnect);
    vbox = GTK_DIALOG (quick_connect)->vbox;
      gtk_widget_show(vbox);

    hbox1 = gtk_hbox_new (FALSE, 0);
    gtk_widget_show (hbox1);
    gtk_box_pack_start (GTK_BOX (vbox), hbox1, TRUE, TRUE, 0);
    gtk_widget_set_size_request (hbox1, -1, 25);

    label_protocol = gtk_label_new (lqconnect->config.value[LCRT_Q_LB_PROTOCOL]);
    gtk_widget_show (label_protocol);
    gtk_box_pack_start (GTK_BOX (hbox1), label_protocol, FALSE, FALSE, 0);
    gtk_widget_set_size_request (label_protocol, 90, 25);
    gtk_misc_set_alignment (GTK_MISC (label_protocol), 0, 0.5);

    combobox_protocol = gtk_combo_box_entry_new_text ();
    lqconnect->q_cb_protocol = combobox_protocol;
    gtk_widget_show (combobox_protocol);
    gtk_box_pack_start (GTK_BOX (hbox1), combobox_protocol, FALSE, TRUE, 0);
    gtk_widget_set_size_request (combobox_protocol, 210, 25);
    g_signal_connect ((gpointer) combobox_protocol, "changed",
                    G_CALLBACK (lcrt_qconnect_on_protocol_changed),
                    lqconnect);
    
    for (i = 0; i < LCRT_PROTOCOL_NUMBER; i++) {
        gtk_combo_box_append_text (GTK_COMBO_BOX (combobox_protocol), proto[i]);
    }
    gtk_entry_set_editable(GTK_ENTRY(GTK_BIN(combobox_protocol)->child), FALSE);

    hbox2 = gtk_hbox_new (FALSE, 0);
    gtk_widget_show (hbox2);
    gtk_box_pack_start (GTK_BOX (vbox), hbox2, TRUE, TRUE, 0);
    gtk_widget_set_size_request (hbox2, -1, 25);

    label_hostname = gtk_label_new (lqconnect->config.value[LCRT_Q_LB_HOSTNAME]);
    gtk_widget_show (label_hostname);
    gtk_box_pack_start (GTK_BOX (hbox2), label_hostname, FALSE, FALSE, 0);
    gtk_widget_set_size_request (label_hostname, 90, 30);
    gtk_misc_set_alignment (GTK_MISC (label_hostname), 0, 0.5);

    entry_hostname = gtk_entry_new ();
    lqconnect->q_et_hostname = entry_hostname;
    gtk_widget_show (entry_hostname);
    gtk_box_pack_start (GTK_BOX (hbox2), entry_hostname, FALSE, TRUE, 0);
    gtk_widget_set_size_request (entry_hostname, 210, 30);
    gtk_entry_set_invisible_char (GTK_ENTRY (entry_hostname), 9679);
    g_signal_connect ((gpointer) entry_hostname, "changed",
                    G_CALLBACK (lcrt_qconnect_on_entry_hostname_changed),
                    lqconnect);

    hbox4 = gtk_hbox_new (FALSE, 0);
    gtk_widget_show (hbox4);
    gtk_box_pack_start (GTK_BOX (vbox), hbox4, TRUE, TRUE, 0);
    gtk_widget_set_size_request (hbox4, -1, 30);

    label_username = gtk_label_new (lqconnect->config.value[LCRT_Q_LB_USERNAME]);
    gtk_widget_show (label_username);
    gtk_box_pack_start (GTK_BOX (hbox4), label_username, FALSE, FALSE, 0);
    gtk_widget_set_size_request (label_username, 90, 30);
    gtk_misc_set_alignment (GTK_MISC (label_username), 0, 0.5);

    entry_username = gtk_entry_new ();
    lqconnect->q_et_username = entry_username;
    gtk_widget_show (entry_username);
    gtk_box_pack_start (GTK_BOX (hbox4), entry_username, FALSE, TRUE, 0);
    gtk_widget_set_size_request (entry_username, 210, 30);
    gtk_entry_set_invisible_char (GTK_ENTRY (entry_username), 9679);

    hbox3 = gtk_hbox_new (FALSE, 0);
    gtk_widget_show (hbox3);
    gtk_box_pack_start (GTK_BOX (vbox), hbox3, TRUE, TRUE, 0);
    gtk_widget_set_size_request (hbox3, -1, 25);

    label_firewall = gtk_label_new (lqconnect->config.value[LCRT_Q_LB_FIREWALL]);
    gtk_widget_show (label_firewall);
    gtk_box_pack_start (GTK_BOX (hbox3), label_firewall, FALSE, FALSE, 0);
    gtk_widget_set_size_request (label_firewall, 90, 25);
    gtk_misc_set_alignment (GTK_MISC (label_firewall), 0, 0.5);

    combobox_firewall = gtk_combo_box_entry_new_text ();
    lqconnect->q_cb_firewall = combobox_firewall;
    gtk_widget_show (combobox_firewall);
    gtk_box_pack_start (GTK_BOX (hbox3), combobox_firewall, FALSE, TRUE, 0);
    gtk_widget_set_size_request (combobox_firewall, 103, 25);
    debug_where();
    for (i = 0; i < LCRT_FIREWALL_NUMBER; i++) {
        gtk_combo_box_append_text (GTK_COMBO_BOX (combobox_firewall), firewall[i]);
    }
    gtk_entry_set_editable(GTK_ENTRY(GTK_BIN(combobox_firewall)->child), FALSE);
    debug_where();

    label_port = gtk_label_new (lqconnect->config.value[LCRT_Q_LB_PORT]);
    gtk_widget_show (label_port);
    gtk_box_pack_start (GTK_BOX (hbox3), label_port, FALSE, FALSE, 0);
    gtk_widget_set_size_request (label_port, 48, 30);
    gtk_misc_set_alignment (GTK_MISC (label_port), 0, 0.49);

    entry_port = gtk_entry_new ();
    lqconnect->q_et_port = entry_port;
    gtk_widget_show (entry_port);
    gtk_box_pack_start (GTK_BOX (hbox3), entry_port, FALSE, TRUE, 0);
    gtk_widget_set_size_request (entry_port, 60, 30);
    gtk_entry_set_invisible_char (GTK_ENTRY (entry_port), 9679);
    
    vbox3 = gtk_vbox_new (FALSE, 0); 
    gtk_widget_show (vbox3);
    gtk_box_pack_start (GTK_BOX (vbox), vbox3, TRUE, TRUE, 0); 
    gtk_widget_set_size_request (vbox3, 309, 117);

    frame = gtk_frame_new (NULL);
    lqconnect->q_ssh_frame = frame;
    gtk_widget_show (frame);
     gtk_widget_set_size_request (frame, 309, 117);
    gtk_box_pack_start (GTK_BOX (vbox3), frame, TRUE, TRUE, 0);
    gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_OUT);

    alignment = gtk_alignment_new (0.5, 0.5, 1, 1);
    gtk_widget_show (alignment);
    gtk_container_add (GTK_CONTAINER (frame), alignment);
    gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 0, 0, 12, 0);

    hbox_frame = gtk_hbox_new (FALSE, 0);
    gtk_widget_show (hbox_frame);
    gtk_container_add (GTK_CONTAINER (alignment), hbox_frame);

    vbox_frame = gtk_vbox_new (FALSE, 0);
    lqconnect->q_vbox_frame = vbox_frame;
    gtk_widget_show (vbox_frame);
    gtk_box_pack_start (GTK_BOX (hbox_frame), vbox_frame, FALSE, TRUE, 0);
    gtk_widget_set_size_request (vbox_frame, 170, -1);

    checkbutton_passwd = gtk_check_button_new_with_mnemonic (lqconnect->config.value[LCRT_Q_CB_PASSWD]);
    lqconnect->q_cb_passwd = checkbutton_passwd;
    gtk_widget_show (checkbutton_passwd);
    gtk_box_pack_start (GTK_BOX (vbox_frame), checkbutton_passwd, FALSE, TRUE, 0);
    gtk_widget_set_size_request (checkbutton_passwd, 79, -1);
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbutton_passwd), TRUE);


    vscrollbar = gtk_vscrollbar_new (GTK_ADJUSTMENT (gtk_adjustment_new (0, 0, 0, 0, 0, 0)));
    gtk_widget_show (vscrollbar);
    gtk_box_pack_start (GTK_BOX (hbox_frame), vscrollbar, FALSE, TRUE, 0);
    gtk_widget_set_size_request (vscrollbar, 20, -1);

    vbox2 = gtk_vbox_new (FALSE, 0);
    gtk_widget_show (vbox2);
    gtk_box_pack_start (GTK_BOX (hbox_frame), vbox2, TRUE, FALSE, 0);

    button_properties = gtk_button_new_with_mnemonic (lqconnect->config.value[LCRT_Q_BT_PROPERTIES]);
    lqconnect->q_bt_properties= button_properties;
    gtk_widget_show (button_properties);
    gtk_box_pack_start (GTK_BOX (vbox2), button_properties, FALSE, FALSE, 0);
    gtk_widget_set_size_request (button_properties, -1, 30);

    fixed3 = gtk_fixed_new ();
    gtk_widget_show (fixed3);
    gtk_box_pack_start (GTK_BOX (hbox_frame), fixed3, TRUE, TRUE, 0);
    gtk_widget_set_size_request (fixed3, 10, -1);

    label_frame = gtk_label_new (lqconnect->config.value[LCRT_Q_LB_FRAME]);
    gtk_widget_show (label_frame);
    gtk_frame_set_label_widget (GTK_FRAME (frame), label_frame);
    gtk_label_set_use_markup (GTK_LABEL (label_frame), TRUE);

    hbox7 = gtk_hbox_new (FALSE, 0);
    gtk_widget_show (hbox7);
    gtk_box_pack_start (GTK_BOX (vbox), hbox7, FALSE, FALSE, 0);

    hbox8 = gtk_hbox_new (FALSE, 0);
    gtk_widget_show (hbox8);
    gtk_box_pack_start (GTK_BOX (hbox7), hbox8, TRUE, TRUE, 0);

    fixed1 = gtk_fixed_new ();
    gtk_widget_show (fixed1);
    gtk_box_pack_start (GTK_BOX (hbox8), fixed1, FALSE, FALSE, 0);
    gtk_widget_set_size_request (fixed1, 15, -1);

    debug_where();
    checkbutton_show_quick_connect_on_startup = 
        gtk_check_button_new_with_mnemonic (lqconnect->config.value[LCRT_Q_CB_SHOW_QCONNECT]);
    lqconnect->q_cb_show_qconnect = checkbutton_show_quick_connect_on_startup;
    gtk_widget_show (checkbutton_show_quick_connect_on_startup);
    gtk_box_pack_start (GTK_BOX (hbox8), checkbutton_show_quick_connect_on_startup, FALSE, FALSE, 0);

    hbox6 = gtk_hbox_new (FALSE, 0);
    gtk_widget_show (hbox6);
    gtk_box_pack_start (GTK_BOX (vbox), hbox6, TRUE, TRUE, 0);

    fixed2 = gtk_fixed_new ();
    gtk_widget_show (fixed2);
    gtk_box_pack_start (GTK_BOX (hbox6), fixed2, FALSE, FALSE, 0);
    gtk_widget_set_size_request (fixed2, 15, -1);

    checkbutton_save_session = gtk_check_button_new_with_mnemonic (lqconnect->config.value[LCRT_Q_CB_SAVE_SESSION]);
    lqconnect->q_cb_save_session = checkbutton_save_session;
    gtk_widget_show (checkbutton_save_session);
    gtk_box_pack_start (GTK_BOX (hbox6), checkbutton_save_session, FALSE, FALSE, 0);
    gtk_widget_set_size_request (checkbutton_save_session, 150, -1);
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbutton_save_session), TRUE);

    checkbutton_open_in_a_tab = gtk_check_button_new_with_mnemonic (lqconnect->config.value[LCRT_Q_CB_OPEN_IN_A_TAB]);
    lqconnect->q_cb_open_in_a_tab = checkbutton_open_in_a_tab;
    gtk_widget_show (checkbutton_open_in_a_tab);
    gtk_box_pack_start (GTK_BOX (hbox6), checkbutton_open_in_a_tab, FALSE, FALSE, 0);

    dialog_action_area = GTK_DIALOG (quick_connect)->action_area;
    gtk_widget_show (dialog_action_area);
    gtk_button_box_set_layout (GTK_BUTTON_BOX (dialog_action_area), GTK_BUTTONBOX_END);


    button_cancel = gtk_button_new_with_mnemonic (lqconnect->config.value[LCRT_Q_BT_CANCEL]);
    lqconnect->q_bt_cancel = button_cancel;
    gtk_widget_show (button_cancel);
    gtk_dialog_add_action_widget (GTK_DIALOG (quick_connect), button_cancel, GTK_RESPONSE_CANCEL);
    GTK_WIDGET_SET_FLAGS (button_cancel, GTK_CAN_DEFAULT);
    g_signal_connect ((gpointer) button_cancel, "clicked",
                    G_CALLBACK (lcrt_qconnect_on_button_cancel_clicked),
                    lqconnect);
    if (flag == LCRT_QCONNECT_SESSION_OPTION)
        button_connect = gtk_button_new_with_mnemonic (lqconnect->config.value[LCRT_Q_BT_OK]);
    else
        button_connect = gtk_button_new_with_mnemonic (lqconnect->config.value[LCRT_Q_BT_CONNECT]);
    lqconnect->q_bt_connect = button_connect;
    gtk_widget_show (button_connect);
    gtk_dialog_add_action_widget (GTK_DIALOG (quick_connect), button_connect, GTK_RESPONSE_OK);
    GTK_WIDGET_SET_FLAGS (button_connect, GTK_CAN_DEFAULT);
    g_signal_connect ((gpointer) button_connect, "clicked",
                    G_CALLBACK (lcrt_qconnect_on_button_connect_clicked),
                    lqconnect);
    gtk_widget_set_sensitive(button_connect, FALSE);

    gtk_widget_add_accelerator(button_connect, "clicked", accel_group,
                               GDK_Return, (GdkModifierType) 0,
                                GTK_ACCEL_VISIBLE);

    gtk_combo_box_set_active(GTK_COMBO_BOX(combobox_protocol), LCRT_PROTOCOL_SSH2);
    gtk_combo_box_set_active(GTK_COMBO_BOX(combobox_firewall), 0);
    gtk_window_set_focus(GTK_WINDOW(quick_connect), entry_hostname);
    debug_where();
    gtk_window_add_accel_group (GTK_WINDOW (quick_connect), accel_group);
    if (flag == LCRT_QCONNECT_SESSION_OPTION) {
        struct lcrtc_user *user;
        char tmp[32];
        if ((user = lcrt_user_find_by_name(&parent->u_config, uname)) != NULL) {
            gtk_combo_box_set_active(GTK_COMBO_BOX(lqconnect->q_cb_protocol), user->protocol);
            gtk_entry_set_text(GTK_ENTRY(lqconnect->q_et_hostname), user->hostname);
            gtk_entry_set_text(GTK_ENTRY(lqconnect->q_et_username), user->username);
            sprintf(tmp, "%d", user->port);
            gtk_entry_set_text(GTK_ENTRY(lqconnect->q_et_port), tmp);
        }
    }
    return gtk_dialog_run(GTK_DIALOG(quick_connect));
err:
    lcrt_message_error(parent->window, lqconnect->config.value[LCRT_Q_ERR_MSG]);
    return GTK_RESPONSE_DELETE_EVENT;
}
int lcrt_qconnect_create_ssh2(struct lcrt_qconnect *lqconnect)
{
    GtkWidget *checkbutton_publickey;
    GtkWidget *checkbutton_keyboard_interactive;
    GtkWidget *checkbutton_gssapi;
    GtkWidget *vbox_frame = lqconnect->q_vbox_frame;

    checkbutton_publickey = gtk_check_button_new_with_mnemonic (lqconnect->config.value[LCRT_Q_CB_PUBLICKEY]);
    lqconnect->q_cb_publickey = checkbutton_publickey;
    gtk_widget_show (checkbutton_publickey);
    debug_where();
    gtk_box_pack_start (GTK_BOX (vbox_frame), checkbutton_publickey, FALSE, FALSE, 0);
    debug_where();
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbutton_publickey), TRUE);

    checkbutton_keyboard_interactive = gtk_check_button_new_with_mnemonic (lqconnect->config.value[LCRT_Q_CB_KEYBOARD]);
    lqconnect->q_cb_keyboard = checkbutton_keyboard_interactive;
    gtk_widget_show (checkbutton_keyboard_interactive);
    gtk_box_pack_start (GTK_BOX (vbox_frame), checkbutton_keyboard_interactive, FALSE, FALSE, 0);
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbutton_keyboard_interactive), TRUE);

    checkbutton_gssapi = gtk_check_button_new_with_mnemonic (lqconnect->config.value[LCRT_Q_CB_GSSAPI]);
    lqconnect->q_cb_gssapi = checkbutton_gssapi;
    gtk_widget_show (checkbutton_gssapi);
    gtk_box_pack_start (GTK_BOX (vbox_frame), checkbutton_gssapi, FALSE, FALSE, 0);
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbutton_gssapi), TRUE);
    return LCRTE_OK;
}
int lcrt_qconnect_destroy_ssh2(struct lcrt_qconnect *lqconnect)
{
    if (lqconnect->q_cb_publickey) {
        gtk_widget_destroy(lqconnect->q_cb_publickey);
        lqconnect->q_cb_publickey = NULL;
    }
    if (lqconnect->q_cb_keyboard) {
        gtk_widget_destroy(lqconnect->q_cb_keyboard);
        lqconnect->q_cb_keyboard = NULL;
    }
    if (lqconnect->q_cb_gssapi) {
        gtk_widget_destroy(lqconnect->q_cb_gssapi);
        lqconnect->q_cb_gssapi = NULL;
    }
    return LCRTE_OK;
}

int lcrt_qconnect_create_ssh1(struct lcrt_qconnect *lqconnect)
{
    GtkWidget *checkbutton_rsa;
    GtkWidget *checkbutton_tis;
    GtkWidget *vbox_frame = lqconnect->q_vbox_frame;

    checkbutton_rsa = gtk_check_button_new_with_mnemonic (lqconnect->config.value[LCRT_Q_CB_RSA]);
    lqconnect->q_cb_rsa = checkbutton_rsa;
    gtk_widget_show (checkbutton_rsa);
    gtk_box_pack_start (GTK_BOX (vbox_frame), checkbutton_rsa, FALSE, FALSE, 0);
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbutton_rsa), TRUE);

    checkbutton_tis = gtk_check_button_new_with_mnemonic (lqconnect->config.value[LCRT_Q_CB_TIS]);
    lqconnect->q_cb_tis = checkbutton_tis;
    gtk_widget_show (checkbutton_tis);
    gtk_box_pack_start (GTK_BOX (vbox_frame), checkbutton_tis, FALSE, FALSE, 0);
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbutton_tis), TRUE);
    return LCRTE_OK;
}
int lcrt_qconnect_destroy_ssh1(struct lcrt_qconnect *lqconnect)
{
    if (lqconnect->q_cb_rsa) {
        gtk_widget_destroy(lqconnect->q_cb_rsa);
        lqconnect->q_cb_rsa = NULL;
    }
    if (lqconnect->q_cb_tis) {
        gtk_widget_destroy(lqconnect->q_cb_tis);
        lqconnect->q_cb_tis = NULL;
    }
    return LCRTE_OK;
}
void lcrt_destroy_qconnect(struct lcrt_qconnect *lqconnect)
{
    if (lqconnect)
        free(lqconnect);
}
static const char *lcrt_qconnect_get_db_name(struct lcrt_qconnect *lqconnect)
{
    return lqconnect->config.db.db_name;
}
static const char *lcrt_qconnect_get_tb_name(struct lcrt_qconnect *lqconnect)
{
    return lqconnect->config.db.db_table;
}

int lcrt_qconnect_init_config(struct lcrt_qconnect *lqconnect)
{
    char db_name[256];
    int i;
    static char *name[LCRT_Q_NUMBER] = {LCRT_Q_NAME};
    static char *value[LCRT_Q_NUMBER] = {LCRT_Q_VALUE};
    static int shortcut[LCRT_Q_NUMBER][2] = {LCRT_Q_SHORTCUT};

    assert(lqconnect);

    memset(&lqconnect->config, 0, sizeof(struct lcrtc_qconnect));
    snprintf(db_name, sizeof(db_name), "%s", lcrt_config_get_language());
    lcrt_config_init(&lqconnect->config.db, db_name, LCRT_IQCONNECT_TABLE);

    for (i = 0; i < LCRT_Q_NUMBER; i++) {
        lqconnect->config.name[i] = name[i];
        strncpy(lqconnect->config.value[i], value[i], sizeof(lqconnect->config.value[i]));
        lqconnect->config.shortcut[i][0] = shortcut[i][0];
        lqconnect->config.shortcut[i][1] = shortcut[i][1];
    }
    lqconnect->get_db = lcrt_qconnect_get_db_name;
    lqconnect->get_tb = lcrt_qconnect_get_tb_name;

    return 0;
}
int lcrt_qconnect_load_config(struct lcrt_qconnect *lqconnect)
{
    int rv, i;

    assert(lqconnect);

    rv = lqconnect->config.db.select(&lqconnect->config.db, "SELECT * FROM %s", lqconnect->get_tb(lqconnect));
    debug_where();
    if (rv == LCRTE_NO_TABLE) {
        //lcrt_qconnect_create_config(lqconnect);
        return LCRTE_NO_CONFIG;
    }
    debug_where();
    for (i = 0; i < LCRT_Q_NUMBER && rv == LCRTE_OK; i++) {
        strncpy(lqconnect->config.value[i], lqconnect->config.db.get_text_col(&lqconnect->config.db, 1), 
                sizeof(lqconnect->config.value[i]));
        rv = lqconnect->config.db.get_row(&lqconnect->config.db);
        debug_print("[%-10s|%-10s]: name = [%-20s] value = [%-20s] shortcut = {%-5d, %d}\n",
                basename((char *)lqconnect->get_db(lqconnect)),
                lqconnect->get_tb(lqconnect),
                lqconnect->config.name[i],
                lqconnect->config.value[i],
                lqconnect->config.shortcut[i][0],
                lqconnect->config.shortcut[i][1]);
    }

    return rv;
}
int lcrt_qconnect_create_config(struct lcrt_qconnect *lqconnect)
{
    int i;

    assert(lqconnect);
    lqconnect->config.db.exec(&lqconnect->config.db, 
                         "CREATE TABLE %s( \
                          name VARCHAR(64) PRIMARY KEY, \
                          value VARCHAR(255),  \
                          shortcut INTEGER, \
                          shortcut_mask INTEGER)",
                         lqconnect->get_tb(lqconnect));
    for (i = 0; i < LCRT_Q_NUMBER; i++) {
        lqconnect->config.db.exec(&lqconnect->config.db,
                             "INSERT INTO %s VALUES('%s', '%s', %d, %d)",
                             lqconnect->get_tb(lqconnect),
                             lqconnect->config.name[i], 
                             lqconnect->config.value[i],
                             lqconnect->config.shortcut[i][0],
                             lqconnect->config.shortcut[i][1]);
    }
    lqconnect->config.db.close(&lqconnect->config.db);
    return 0;
}

Generated by  Doxygen 1.6.0   Back to index