00001
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "../config.h"
00024
00025 #include <string.h>
00026
00027 #include "dialogs.h"
00028 #include "guiapi.h"
00029 #include "queries.h"
00030 #include "register.h"
00031 #include "tables.h"
00032
00033
00034 #define KEY_FIELDNAME "fieldname"
00036 extern void update_button_sensitivity(void);
00037
00039 static GtkWidget *dbConnectDialog = NULL;
00040
00041
00047 static void get_text_field(GtkWidget * w, gpointer data)
00048 {
00049 Form *frm = (Form *) data;
00050 gchar *fname;
00051 gchar *s;
00052
00053 if (!w) {
00054 return;
00055 }
00056
00057 fname = (gchar *) gtk_object_get_data(GTK_OBJECT(w), KEY_FIELDNAME);
00058 if (fname) {
00059 s = gtk_editable_get_chars(GTK_EDITABLE(w), 0, -1);
00060 if (s) {
00061 frm_set_value_by_name(frm, (char *) fname, s);
00062 g_free(s);
00063 s = NULL;
00064 }
00065 }
00066 }
00067
00072 static void load_form_from_pane(Form * frm, GtkWidget * pane)
00073 {
00074 gtk_container_foreach(GTK_CONTAINER(pane), get_text_field, frm);
00075 }
00076
00078 static void db_connect_dialog_hide(void)
00079 {
00080 if (dbConnectDialog != NULL) {
00081 gtk_grab_remove(dbConnectDialog);
00082 gtk_widget_hide(GTK_WIDGET(dbConnectDialog));
00083 }
00084 }
00085
00087 int db_connect_dialog_reconnect(GtkWidget * widget)
00088 {
00089 GtkWidget *notebook = GTK_WIDGET(connectorTable[0]);
00090 GtkWidget *pane = NULL;
00091 GList *children = NULL;
00092 ABuf *errmsg = NULL;
00093 ABuf *err = NULL;
00094
00095 int page;
00096 int i;
00097 DBConnection *conn = gui_get_connection();
00098 DBConnector *dbc = NULL;
00099
00100
00101 if (conn != NULL) {
00102 display_error("Internal error",
00103 "Internal error : connection should be closed");
00104 return CONNECT_ERROR;
00105 }
00106
00107
00108 if (!notebook)
00109 return CONNECT_ERROR;
00110
00111 page = gtk_notebook_current_page(GTK_NOTEBOOK(notebook));
00112
00113
00114 page++;
00115
00116 if (connectorTable[page] == NULL) {
00117 display_error("Internal error",
00118 "Internal error : open dialog badly created");
00119 return CONNECT_ERROR;
00120 }
00121
00122
00123 children = gtk_container_children(GTK_CONTAINER(notebook));
00124 if (!children)
00125 return CONNECT_ERROR;
00126
00127
00128 for (i = 0; i < (page - 1); i++)
00129 children = children->next;
00130
00131 pane = (GtkWidget *) (children->data);
00132 if (!pane)
00133 return CONNECT_ERROR;
00134
00135 dbc = connectorTable[page];
00136 load_form_from_pane(dbc->db_frm, pane);
00137 errmsg = frm_check_fields(dbc->db_frm);
00138 if (errmsg == NULL) {
00139 conn = db_connect(dbc);
00140 if (conn == NULL) {
00141 g_warning("Couldn't finish connecting.\n");
00142 return CONNECT_ERROR;
00143 }
00144 if (conn->connectionStatus == CONNECT_OK) {
00145 gui_set_connection(conn);
00146 }
00147 return conn->connectionStatus;
00148 }
00149 else {
00150 err = buf_new(1024);
00151 err = buf_strcpy(err, "You did not complete some "
00152 "required fields (");
00153 err = buf_strcat(err, errmsg->b_dat);
00154 err = buf_strcat(err, "). Please complete them and try " "again. \n");
00155 display_error("Connection error", err->b_dat);
00156 errmsg = buf_free(errmsg);
00157 err = buf_free(err);
00158 }
00159 return CONNECT_ERROR;
00160 }
00161
00167 static void set_text_field(GtkWidget * w, gpointer data)
00168 {
00169 Form *frm = (Form *) data;
00170 gpointer *fname;
00171 gchar *s;
00172 gint pos = 0;
00173
00174 if (!w) {
00175 return;
00176 }
00177
00178 fname = gtk_object_get_data(GTK_OBJECT(w), KEY_FIELDNAME);
00179 if (fname) {
00180 s = frm_get_value_by_name(frm, (char *) fname);
00181 gtk_editable_delete_text(GTK_EDITABLE(w), 0, -1);
00182 if (s)
00183 gtk_editable_insert_text(GTK_EDITABLE(w), s, strlen(s), &pos);
00184 }
00185 }
00186
00192 static void load_pane_from_form(GtkWidget * pane, Form * frm)
00193 {
00194 gtk_container_foreach(GTK_CONTAINER(pane), set_text_field, frm);
00195 }
00196
00200 static void do_browse_file_dialog(GtkWidget * widget, gpointer data)
00201 {
00202 file_open_dialog(data);
00203 }
00204
00207 static GtkWidget *make_form_pane(Form * frm)
00208 {
00209 GtkWidget *table;
00210 GtkWidget *lbl;
00211 GtkWidget *txt;
00212 GtkWidget *btnLbl;
00213 GtkWidget *openBtn;
00214 FormField *fld;
00215 char *tiptext;
00216 char *helptext;
00217 char *fieldname;
00218 int i;
00219 int fieldcount;
00220 int ftype;
00221 ABuf *buf = NULL;
00222 int somerequired = 0;
00223
00224
00225
00226 fieldcount = frm_get_field_count(frm);
00227 table = gtk_table_new(fieldcount + 1, 3, FALSE);
00228 gtk_container_border_width(GTK_CONTAINER(table), 6);
00229 for (i = 0; i < fieldcount; i++) {
00230 fld = frm_get_field(frm, i);
00231 fieldname = fld_get_name(fld);
00232
00233 buf = buf_strcpy(buf, fieldname);
00234 if (fld_get_relevance(fld) == FR_REQUIRED) {
00235 buf = buf_strcat(buf, "*");
00236 somerequired++;
00237 }
00238 lbl = gtk_label_new(buf->b_dat);
00239 gtk_misc_set_padding(GTK_MISC(lbl), 4, 0);
00240
00241
00242 gtk_label_set_justify(GTK_LABEL(lbl), GTK_JUSTIFY_RIGHT);
00243 gtk_widget_show(lbl);
00244
00245 ftype = fld_get_type(fld);
00246 tiptext = fld_get_tip(fld);
00247 helptext = fld_get_help(fld);
00248
00249 txt = gtk_entry_new();
00250 if (tiptext)
00251 gtk_tooltips_set_tip(gtk_tooltips_new(), txt, tiptext, helptext);
00252
00253 gtk_widget_show(txt);
00254 switch (ftype) {
00255 case FT_TEXT:
00256 break;
00257 case FT_PWD:
00258 gtk_entry_set_visibility(GTK_ENTRY(txt), FALSE);
00259 break;
00260 case FT_FILENAME:
00261 openBtn = gtk_button_new();
00262 btnLbl = gtk_label_new("Browse...");
00263 gtk_misc_set_padding(GTK_MISC(btnLbl), 4, 0);
00264 gtk_widget_show(btnLbl);
00265 gtk_widget_show(openBtn);
00266 gtk_container_add(GTK_CONTAINER(openBtn), btnLbl);
00267 gtk_signal_connect(GTK_OBJECT(openBtn), "clicked",
00268 GTK_SIGNAL_FUNC(do_browse_file_dialog), txt);
00269
00270 gtk_table_attach(GTK_TABLE(table), openBtn, 2, 3, i, i + 1, 0, 0,
00271 0, 0);
00272 break;
00273 }
00274 gtk_object_set_data(GTK_OBJECT(txt), KEY_FIELDNAME,
00275 strdup(frm_get_field_name(frm, i)));
00276 gtk_table_attach(GTK_TABLE(table), lbl, 0, 1, i, i + 1, GTK_SHRINK,
00277 GTK_FILL | GTK_EXPAND, 0, 0);
00278 gtk_table_attach_defaults(GTK_TABLE(table), txt, 1, 2, i, i + 1);
00279 }
00280 if (somerequired > 0) {
00281 lbl = gtk_label_new("* = required fields");
00282 gtk_label_set_justify(GTK_LABEL(lbl), GTK_JUSTIFY_LEFT);
00283 gtk_widget_show(lbl);
00284 gtk_table_attach_defaults(GTK_TABLE(table), lbl, 1, 2, fieldcount,
00285 fieldcount + 1);
00286 }
00287 buf_free(buf);
00288 load_pane_from_form(table, frm);
00289 return table;
00290 }
00291
00293 static void db_connect_dialog_cancel_callback(GtkWidget * widget,
00294 gpointer data)
00295 {
00296 GtkWidget *dialog = GTK_WIDGET(data);
00297
00298 gtk_grab_remove(dialog);
00299 gtk_widget_hide(dialog);
00300 }
00301
00305 static void db_connect_dialog_ok_callback(GtkWidget * widget, gpointer data)
00306 {
00307 if (db_connect_dialog_reconnect(widget) == CONNECT_OK)
00308 db_connect_dialog_hide();
00309 }
00310
00312 void db_connect_dialog_create(void)
00313 {
00314 int nb_db;
00315 if (dbConnectDialog != NULL)
00316 return;
00317
00318
00319 nb_db = 0;
00320 while (registerDB[nb_db] != NULL)
00321 nb_db++;
00322
00323 if (nb_db == 0) {
00324 display_error("Notice",
00325 "You do not have any database driver "
00326 "installed. When GtkSQL was built it could not find any supported database.");
00327 }
00328 else {
00329 GtkWidget *ok_button, *cancel_button, *notebook;
00330 GtkWidget *lbl;
00331 int i;
00332
00333
00334 connectorTable =
00335 (DBConnector **) malloc((nb_db + 2) * sizeof(DBConnector *));
00336
00337
00338 dbConnectDialog = gtk_dialog_new();
00339 gtk_window_set_title(GTK_WINDOW(dbConnectDialog),
00340 "Database connection");
00341
00342
00343 lbl = gtk_label_new("OK");
00344 gtk_misc_set_padding(GTK_MISC(lbl), 4, 0);
00345 gtk_widget_show(GTK_WIDGET(lbl));
00346 ok_button = gtk_button_new();
00347 gtk_container_add(GTK_CONTAINER(ok_button), lbl);
00348 gtk_signal_connect(GTK_OBJECT(ok_button), "clicked",
00349 GTK_SIGNAL_FUNC(db_connect_dialog_ok_callback),
00350 connectorTable);
00351 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dbConnectDialog)->action_area),
00352 ok_button, FALSE, FALSE, 0);
00353 GTK_WIDGET_SET_FLAGS(ok_button, GTK_CAN_DEFAULT);
00354 gtk_widget_grab_default(ok_button);
00355 gtk_widget_show(ok_button);
00356
00357 lbl = gtk_label_new("Cancel");
00358 gtk_misc_set_padding(GTK_MISC(lbl), 4, 0);
00359 gtk_widget_show(GTK_WIDGET(lbl));
00360 cancel_button = gtk_button_new();
00361 gtk_container_add(GTK_CONTAINER(cancel_button), lbl);
00362 gtk_signal_connect(GTK_OBJECT(cancel_button), "clicked",
00363 GTK_SIGNAL_FUNC(db_connect_dialog_cancel_callback),
00364 dbConnectDialog);
00365 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dbConnectDialog)->action_area),
00366 cancel_button, FALSE, FALSE, 0);
00367 gtk_widget_show(cancel_button);
00368
00369
00370 notebook = gtk_notebook_new();
00371 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(notebook), GTK_POS_TOP);
00372 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dbConnectDialog)->vbox),
00373 notebook, FALSE, FALSE, 0);
00374 gtk_widget_show(notebook);
00375
00376
00377 connectorTable[0] = (DBConnector *) notebook;
00378 for (i = 0; i < nb_db; i++) {
00379 GtkWidget *pane;
00380 Form *f;
00381
00382 connectorTable[i + 1] = registerDB[i] (display_error);
00383 f = connectorTable[i + 1]->db_frm;
00384 if (f) {
00385 pane = make_form_pane(f);
00386 gtk_widget_show(pane);
00387 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), pane,
00388 gtk_label_new(frm_get_title(f)));
00389 }
00390 }
00391 connectorTable[i + 1] = NULL;
00392 update_button_sensitivity();
00393 }
00394 }
00395
00397 void db_connect_dialog_show(void)
00398 {
00399 db_connect_dialog_create();
00400 gtk_window_position(GTK_WINDOW(dbConnectDialog), GTK_WIN_POS_MOUSE);
00401 gtk_window_set_policy(GTK_WINDOW(dbConnectDialog), FALSE, FALSE, FALSE);
00402 gtk_grab_add(dbConnectDialog);
00403 gtk_widget_show(dbConnectDialog);
00404 }