00001
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "../config.h"
00024 #include <stdlib.h>
00025 #include <string.h>
00026
00027 #include "buf.h"
00028 #include "db_connect_dlg.h"
00029 #include "dbapi.h"
00030 #include "dialogs.h"
00031 #include "gui.h"
00032 #include "guiapi.h"
00033 #include "queries.h"
00034 #include "status.h"
00035 #include "tables.h"
00036 #include "utils.h"
00037 #include "workspace.h"
00038
00040 void gui_add_query(void) {
00041 query_add();
00042 }
00043
00046 int gui_connection_ok(void)
00047 {
00048 if (gui_get_connection() != NULL)
00049 return 1;
00050 else
00051 return 0;
00052 }
00053
00055 void gui_delete_all_queries(void)
00056 {
00057 int i, nb = nbQueries;
00058
00059
00060 gtk_signal_disconnect(GTK_OBJECT(queryNotebook), queryNotebookPageChangeHandler);
00061 queryNotebookPageChangeHandler = 0;
00062
00063 for (i = 0; i < nb; i++)
00064 query_delete_at_position(0);
00065
00066 lastQuery = 0;
00067 nbQueries = 0;
00068 }
00069
00071 void gui_disconnect(void) {
00072 DBConnection *conn = gui_get_connection();
00073 if (conn == NULL) {
00074 g_warning("Can't disconnect. Already disconnected.");
00075 return;
00076 }
00077 tables_connection_closed();
00078 gui_delete_all_queries();
00079
00080
00081 queries_init();
00082 db_disconnect(conn, NULL);
00083 guiConnection = NULL;
00084 }
00085
00088 DBConnection *gui_get_connection(void)
00089 {
00090 return guiConnection;
00091 }
00092
00095 char *gui_get_connection_url(void)
00096 {
00097 DBConnector *connector;
00098 DBConnection *conn = gui_get_connection();
00099 if (conn == NULL)
00100 return NULL;
00101
00102 connector = conn->connector;
00103 if (connector == NULL)
00104 return NULL;
00105
00106 return connector->form_to_url(connector->db_frm);
00107 }
00108
00111 int gui_get_query_count(void) {
00112 return query_count();
00113 }
00114
00117 int gui_get_result_columns()
00118 {
00119 void *result;
00120 DBConnection *conn;
00121
00122
00123 conn = gui_get_connection();
00124 if (conn == NULL)
00125 return 0;
00126
00127 result = query_get_result(query_get_current());
00128 if (result == NULL)
00129 return 0;
00130
00131 if (conn->DBget_query_status(conn, result) != DB_QUERY_RECORDS_OK)
00132 return 0;
00133
00134 return conn->DBget_field_number(conn, result);
00135 }
00136
00141 char *gui_get_result_field(int row, int column)
00142 {
00143 DBConnection *conn = gui_get_connection();
00144 void *result = query_get_current_result();
00145 int nb_tuples, nb_cols;
00146 int stat;
00147
00148 stat = conn->DBget_query_status(conn, result);
00149 if (stat == DB_QUERY_RECORDS_OK) {
00150 nb_tuples = conn->DBget_record_number(conn, result);
00151 nb_cols = conn->DBget_field_number(conn, result);
00152
00153 if ((row >= nb_tuples) || (column >= nb_cols))
00154 return "";
00155
00156 return conn->DBget_field_value(conn, result, row, column);
00157 }
00158 else
00159 return "";
00160 }
00161
00164 int gui_get_result_rows(void)
00165 {
00166 void *result;
00167 DBConnection *conn;
00168
00169
00170 conn = gui_get_connection();
00171 if (conn == NULL)
00172 return 0;
00173
00174 result = query_get_result(query_get_current());
00175 if (result == NULL)
00176 return 0;
00177
00178 if (conn->DBget_query_status(conn, result) != DB_QUERY_RECORDS_OK)
00179 return 0;
00180
00181 return conn->DBget_record_number(conn, result);
00182 }
00183
00187 void gui_init(int argc, char **argv) {
00188 add_pixmap_directory(PACKAGE_PIXMAPS_DIR);
00189
00190
00191 gtk_init(&argc, &argv);
00192 gtk_rc_parse(get_rcname());
00193
00194 mainWindow = create_mainWindow();
00195
00196 gtk_widget_show(mainWindow);
00197 populate_mainWindow();
00198 setup_widget_vars();
00199
00200 db_connect_dialog_create();
00201
00202
00203 update_button_sensitivity();
00204 }
00205
00208 void gui_load_query_from_file(char *filename)
00209 {
00210 char *name;
00211 ABuf *buf = buf_new(256);
00212 FILE *fp;
00213 GtkWidget *cq = query_get_current();
00214 QueryData *qd = gtk_object_get_data(GTK_OBJECT(cq), QDATA_KEY);
00215
00216 query_clear();
00217
00218 gtk_text_freeze(GTK_TEXT(cq));
00219 fp = fopen(filename, "r");
00220 if (fp != NULL) {
00221 while (fgets(buf->b_dat, buf->b_len, fp) != NULL)
00222 gtk_text_insert(GTK_TEXT(cq), NULL, NULL, NULL, buf->b_dat, -1);
00223
00224 fclose(fp);
00225 querydata_set_filename(qd, filename);
00226 querydata_set_unchanged(qd);
00227 name = get_short_filename(filename);
00228 if (name == NULL)
00229 name = "Untitled";
00230
00231 gui_set_query_name(name);
00232 }
00233 gtk_text_thaw(GTK_TEXT(cq));
00234 buf_free(buf);
00235 }
00236
00239 int gui_run(void) {
00240
00241 gui_get_workspace();
00242
00243 running++;
00244 gtk_main();
00245 running--;
00246 return 0;
00247 }
00248
00250 void gui_send_query() {
00251 #define ERROREXEC_S " Error executing query \n %s "
00252 GtkWidget *current_query = query_get_current();
00253 char *cquery = gtk_editable_get_chars(GTK_EDITABLE(current_query), 0, -1);
00254 void *result =
00255 (void *) gtk_object_get_data(GTK_OBJECT(current_query), RESULT_KEY);
00256 char *status =
00257 (char *) gtk_object_get_data(GTK_OBJECT(current_query), STATUS_KEY);
00258 DBConnection *conn = gui_get_connection();
00259
00260 if (result != NULL)
00261 conn->DBclear_result(conn, result);
00262
00263
00264 results_display(NULL, QUERY_FAKE,
00265 gtk_notebook_current_page(GTK_NOTEBOOK(queryNotebook)));
00266
00267 status_push("Query in progress...");
00268
00269 result = conn->DBexecute_query(conn, cquery);
00270 gtk_object_set_data(GTK_OBJECT(current_query), RESULT_KEY, result);
00271 if ((conn->DBget_query_status(conn, result) != DB_QUERY_RECORDS_OK) &&
00272 (conn->DBget_query_status(conn, result) != DB_QUERY_COMMAND_OK) &&
00273 (conn->DBget_query_status(conn, result) != DB_QUERY_EMPTY)) {
00274 GtkWidget *dialog;
00275 char *msg = conn->DBget_error_message(conn);
00276 ABuf *buf = buf_new(strlen(ERROREXEC_S) + strlen(msg));
00277 if (!buf) {
00278 g_error("SendQuery() could not allocate memory for a buffer\n");
00279 return;
00280 }
00281 snprintf(buf->b_dat, buf->b_len, ERROREXEC_S, msg);
00282 dialog =
00283 create_standard_dialog("Error executing query", buf->b_dat, 1,
00284 NULL, "Ok", NULL, NULL, NULL, NULL);
00285 gtk_widget_show(dialog);
00286 results_display((void *) buf->b_dat, QUERY_ERROR,
00287 gtk_notebook_current_page(GTK_NOTEBOOK
00288 (queryNotebook)));
00289
00290 sprintf(status, "Error in query");
00291 buf_free(buf);
00292 }
00293 else {
00294 int nbf = conn->DBget_field_number(conn, result);
00295 int nbr = conn->DBget_record_number(conn, result);
00296
00297 results_display(result, QUERY_NEW,
00298 gtk_notebook_current_page(GTK_NOTEBOOK
00299 (queryNotebook)));
00300 sprintf(status, "Query results : %d field%sand %d record%s", nbf,
00301 (nbf != 1 ? "s " : " "), nbr, (nbr != 1 ? "s" : ""));
00302 }
00303
00304 g_free(cquery);
00305
00306 status_pop();
00307
00308 if (status != NULL) {
00309 if (queryStatus == 1)
00310 status_pop();
00311 status_push(status);
00312 queryStatus = 1;
00313 }
00314 else
00315 queryStatus = 0;
00316 }
00317
00320 void gui_set_connection(DBConnection * con)
00321 {
00322 char buf[500];
00323 char *s = NULL;
00324
00325 if (guiConnection != NULL)
00326 gui_disconnect();
00327
00328 guiConnection = con;
00329
00330 tables_new_connection();
00331 gui_add_query();
00332 update_button_sensitivity();
00333 if (con != NULL)
00334 s = con->DBget_db_name(con);
00335
00336 if (!s)
00337 s = "";
00338
00339 snprintf(buf, sizeof(buf)-1, "GtkSQL: %s", s);
00340 buf[sizeof(buf)-1] = '\0';
00341 gtk_window_set_title(GTK_WINDOW(mainWindow), buf);
00342 }
00343
00347 void gui_set_query_name(gchar *name) {
00348 int position = gtk_notebook_current_page(GTK_NOTEBOOK(queryNotebook));
00349 query_set_name(position, name);
00350 }
00351
00354 void gui_set_query_text(char *txt)
00355 {
00356 GtkWidget *cq = query_get_current();
00357 QueryData *qd = gtk_object_get_data(GTK_OBJECT(cq), QDATA_KEY);
00358
00359 query_clear();
00360
00361
00362 gtk_text_freeze(GTK_TEXT(cq));
00363 gtk_text_insert(GTK_TEXT(cq), NULL, NULL, NULL, txt, -1);
00364 querydata_set_unchanged(qd);
00365 gtk_text_thaw(GTK_TEXT(cq));
00366 }