00001 00002 00003 00004
00005
00006 00007 00008 00009 00010 00011 00012 00013 00014 00015 00016 00017 00018 00019 00020 00021 00022 00023 00024
00025
00026 00027 00028 00029 00030 00031
00032
00033 #ifdef HAVE_CONFIG_H
00034 # include <config.h>
00035 #endif
00036
00037 #ifndef LIBPQ_H
00038 # include "libpq-fe.h"
00039 # define LIBPQ_H
00040 #endif
00041
00042 #include <gnome.h>
00043
00044 #include "interface.h"
00045 #include "support.h"
00046
00047 #include "fmaps.h"
00048
00049 void
00050 FM_Create_Map_Tables(const gchar *tablename)
00051 {
00052 gchar *buffer;
00053
00054 if (conn==NULL)
00055 {
00056 buffer = g_strdup_printf("No connection to database: %s",dbName);
00057 FM_MessageBox(buffer);
00058 g_free(buffer);
00059 return;
00060 }
00061
00062 FM_MessageBox("Function not yet implemented");
00063 }
00064
00065 int FM_Create_System_Table(PGconn *conn,gchar *tablename,gchar *tablestructure)
00066 {
00067 gchar *buffer;
00068 PGresult *qryResult;
00069 gint nbtuples;
00070 gint result;
00071
00072 g_return_val_if_fail (conn!=NULL,FALSE);
00073 g_return_val_if_fail (tablename!=NULL,FALSE);
00074 g_return_val_if_fail (tablestructure!=NULL,FALSE);
00075
00076 result = FALSE;
00077 buffer = g_strdup_printf("SELECT tablename FROM pg_tables WHERE tablename='%s'",tablename);
00078 qryResult = PQexec(conn,buffer);
00079 nbtuples = PQntuples(qryResult);
00080 PQclear(qryResult);
00081 g_free(buffer);
00082
00083 if (nbtuples==0)
00084 {
00085 buffer = g_strdup(tablestructure);
00086 qryResult = PQexec(conn,buffer);
00087
00088
00089 if (!(g_str_equal(PQresultErrorMessage(qryResult),"")))
00090 {
00091 g_warning("%s : %s",buffer, PQresultErrorMessage(qryResult));
00092 }
00093
00094 g_free(buffer);
00095 PQclear(qryResult);
00096 result = TRUE;
00097 }
00098 return (result);
00099 }
00100
00101
00102 00103 00104 00105 00106 00107 00108
00109
00110 void FM_CheckDB(PGconn *conn)
00111 {
00112 gint result;
00113
00114 g_return_if_fail (conn!=NULL);
00115
00116
00117
00118 result = FM_Create_System_Table(conn,"f_catalogue",F_CATALOGUE_TABLE);
00119
00120 result = FM_Create_System_Table(conn,"f_catalogue_initiative",F_CATALOGUE_INITITATIVE_TABLE);
00121 result = FM_Create_System_Table(conn,"f_catalogue_party",F_CATALOGUE_PARTY_TABLE);
00122 result = FM_Create_System_Table(conn,"f_catalogue_extents",F_CATALOGUE_EXTENTS_TABLE);
00123 result = FM_Create_System_Table(conn,"f_catalogue_resolution_code",F_CATALOGUE_RESOLUTION_CODE_TABLE);
00124 result = FM_Create_System_Table(conn,"f_catalogue_lang_data_code",F_CATALOGUE_LANG_DATA_CODE_TABLE);
00125 result = FM_Create_System_Table(conn,"f_catalogue_theme_code",F_CATALOGUE_THEME_CODE_TABLE);
00126 result = FM_Create_System_Table(conn,"f_catalogue_keyword_info",F_CATALOGUE_KEYWORD_INFO_TABLE);
00127 result = FM_Create_System_Table(conn,"f_catalogue_spatial_rep_type_code",F_CATALOGUE_SPATIAL_REP_TYPE_CODE_TABLE);
00128 result = FM_Create_System_Table(conn,"f_catalogue_distrib_id",F_CATALOGUE_DISTRIB_ID_TABLE);
00129 result = FM_Create_System_Table(conn,"f_catalogue_format_code",F_CATALOGUE_FORMAT_CODE_TABLE);
00130 result = FM_Create_System_Table(conn,"f_catalogue_media",F_CATALOGUE_MEDIA_TABLE);
00131 result = FM_Create_System_Table(conn,"f_catalogue_resource_url",F_CATALOGUE_RESOURCE_URL_TABLE);
00132
00133
00134
00135 result = FM_Create_System_Table(conn,"f_projection",F_PROJ_TABLE);
00136
00137 result = FM_Create_System_Table(conn,"f_datum",F_DATUM_TABLE);
00138
00139 result = FM_Create_System_Table(conn,"f_ellipsoid",F_ELLIPSOID_TABLE);
00140
00141 result = FM_Create_System_Table(conn,"f_projdatum",F_PROJDATUM_TABLE);
00142
00143 }
00144
00145 void
00146 FM_ConnectDB(void)
00147 {
00148 GtkWidget *Message;
00149 GtkWidget *Entry;
00150 gchar *buffer;
00151
00152 Entry=gtk_object_get_data(GTK_OBJECT(Fmaps),"appbarfmaps");
00153
00154 if (conn==NULL)
00155 {
00156
00157 conn = PQsetdbLogin(pghost, pgport, pgoptions, pgtty, dbName, username, password);
00158 }
00159 00160 00161
00162 if (PQstatus(conn) == CONNECTION_BAD)
00163 {
00164 buffer = g_strdup_printf("Connection to database '%s' failed\n %s",dbName,PQerrorMessage(conn));
00165 Message = gnome_error_dialog(buffer);
00166 PQfinish(conn);
00167 gnome_appbar_set_default(GNOME_APPBAR(Entry),"Connection Failed");
00168 return;
00169 }
00170 gnome_appbar_set_default(GNOME_APPBAR(Entry),"Connected");
00171
00172
00173
00174 FM_CheckDB(conn);
00175
00176
00177
00178 FM_Rebuild_Directory();
00179
00180 }
00181
00182 void
00183 fmaps_quit(void)
00184 {
00185 if(conn!=NULL)
00186 {
00187 PQfinish(conn);
00188 }
00189 gtk_main_quit();
00190 }
00191
00192 void
00193 fmaps_init(void)
00194 {
00195
00196 00197 00198 00199 00200 00201
00202 pghost = NULL;
00203 pgport = NULL;
00204 pgoptions = NULL;
00205 pgtty = NULL;
00206 dbName = NULL;
00207 username = NULL;
00208 password = NULL;
00209
00210 conn=NULL;
00211
00212
00213
00214 gnome_config_push_prefix("FMaps/connection/");
00215
00216 pghost = gnome_config_get_string("pghost");
00217 pgport = gnome_config_get_string("pgport");
00218 pgoptions = gnome_config_get_string("pgoptions");
00219 pgtty = gnome_config_get_string("pgtty");
00220 dbName = gnome_config_get_string("dbName");
00221 username = gnome_config_get_string("username");
00222 password = gnome_config_get_string("password");
00223
00224 gnome_config_pop_prefix();
00225
00226
00227 if (dbName==NULL || strcmp(dbName,"")==0)
00228 {
00229 dbName=g_strdup("gisdata");
00230 }
00231 if (username==NULL || strcmp(username,"")==0)
00232 {
00233 username=NULL;
00234 }
00235
00236 }
00237
00238
00239 int
00240 main (int argc, char *argv[])
00241 {
00242
00243 bindtextdomain (PACKAGE, PACKAGE_LOCALE_DIR);
00244 textdomain (PACKAGE);
00245
00246 gnome_init ("fmaps", VERSION, argc, argv);
00247 fmaps_init();
00248
00249
00250 Fmaps = create_Fmaps ();
00251
00252 gtk_widget_show (Fmaps);
00253 gtk_main ();
00254 return 0;
00255 }
00256
00257 void FM_MessageBox(const char *message)
00258 {
00259 GtkWidget *messagebox;
00260 gint key;
00261
00262 messagebox=gnome_message_box_new(message,GNOME_MESSAGE_BOX_WARNING,GNOME_STOCK_BUTTON_OK,NULL);
00263 gtk_window_set_position(GTK_WINDOW(messagebox),GTK_WIN_POS_CENTER);
00264 key=gnome_dialog_run(GNOME_DIALOG(messagebox));
00265 return;
00266 }
00267
00268 00269 00270
00271
00272 void FM_Rebuild_Directory(void)
00273 {
00274 GtkCTree *ctree;
00275 gchar *buffer;
00276 PGresult *qryResult;
00277 PGresult *qryResult2;
00278 gint i;
00279 gint j;
00280 gint nbtuples;
00281 gint nbtuples2;
00282 gchar *col[3];
00283 GtkCTreeNode *gtktreenode=NULL;
00284 GtkCTreeNode *node;
00285 FM_RowStatus *newrowstatus;
00286 FM_RowStatus *rowstatus;
00287 FM_RowStatus *subrowstatus;
00288 gint result;
00289 gchar *tablename;
00290 gchar *itemname;
00291
00292 ctree=GTK_CTREE(gtk_object_get_data(GTK_OBJECT(Fmaps),"ctree_table_list"));
00293
00294 buffer=g_strdup("SELECT DISTINCT series from f_catalogue ORDER BY series");
00295 qryResult = PQexec(conn,buffer);
00296 g_free(buffer);
00297
00298 nbtuples = PQntuples(qryResult);
00299
00300 node = gtk_ctree_node_nth(GTK_CTREE(ctree),0);
00301
00302
00303 if (node==NULL)
00304 {
00305
00306 gtktreenode = NULL;
00307
00308 for (i=0;i<nbtuples;i++)
00309 {
00310 buffer = PQgetvalue(qryResult,i,0);
00311 col[0] = g_strdup(buffer);
00312 col[1] = g_strdup("two");
00313 col[2] = g_strdup("three");
00314 gtktreenode = gtk_ctree_insert_node(GTK_CTREE(ctree),NULL,gtktreenode,col,8,NULL,NULL,NULL,NULL,FALSE,FALSE);
00315 newrowstatus = g_malloc(sizeof(FM_RowStatus));
00316 newrowstatus->expandedbefore=FALSE;
00317 newrowstatus->selectable=FALSE;
00318 newrowstatus->selected=FALSE;
00319 gtk_ctree_node_set_row_data_full(GTK_CTREE(ctree),gtktreenode,newrowstatus,g_free);
00320 }
00321
00322 } else
00323 {
00324
00325
00326 i=0;
00327 while (i<nbtuples || node!=NULL)
00328 {
00329 rowstatus = gtk_ctree_node_get_row_data(GTK_CTREE(ctree),node);
00330
00331 if (!rowstatus->selectable)
00332 {
00333 result = gtk_ctree_node_get_pixtext(GTK_CTREE(ctree),node,0,col,NULL,NULL,NULL);
00334 tablename = g_strchomp(*col);
00335 itemname = PQgetvalue(qryResult,i,0);
00336 g_warning("%s %s",tablename,itemname);
00337
00338
00339 if (strcmp(tablename,itemname)>0)
00340 {
00341 g_warning("insert: %s",itemname);
00342 i++;
00343 }
00344
00345 if (strcmp(tablename,itemname)==0)
00346 {
00347 g_warning("matching");
00348
00349 if (rowstatus->expandedbefore)
00350 {
00351 g_warning("refreshing table list of item: %s",itemname);
00352
00353
00354 gtk_ctree_remove_node(ctree,node);
00355
00356
00357 col[0] = g_strdup(itemname);
00358 col[1] = g_strdup("two");
00359 col[2] = g_strdup("three");
00360 gtktreenode = gtk_ctree_insert_node(GTK_CTREE(ctree),NULL,gtktreenode,col,8,NULL,NULL,NULL,NULL,FALSE,FALSE);
00361 newrowstatus = g_malloc(sizeof(FM_RowStatus));
00362 newrowstatus->expandedbefore=FALSE;
00363 newrowstatus->selectable=FALSE;
00364 newrowstatus->selected=FALSE;
00365 gtk_ctree_node_set_row_data_full(GTK_CTREE(ctree),gtktreenode,newrowstatus,g_free);
00366
00367
00368
00369 buffer=g_strdup_printf("SELECT DISTINCT meta_file_id,series FROM f_catalogue WHERE series='%s' ORDER BY meta_file_id",itemname);
00370 qryResult2 = PQexec(conn,buffer);
00371 g_free(buffer);
00372
00373 nbtuples2 = PQntuples(qryResult2);
00374 gtktreenode = NULL;
00375
00376 for (j=0;j<nbtuples2;j++)
00377 {
00378 buffer = PQgetvalue(qryResult2,j,0);
00379 col[0] = g_strdup(buffer);
00380 col[1] = NULL;
00381 col[2] = NULL;
00382 gtktreenode = gtk_ctree_insert_node(ctree,GTK_CTREE_NODE(node),gtktreenode,col,8,NULL,NULL,NULL,NULL,TRUE,FALSE);
00383
00384 subrowstatus=g_malloc(sizeof(FM_RowStatus));
00385 subrowstatus->expandedbefore=TRUE;
00386 subrowstatus->selectable=TRUE;
00387 subrowstatus->selected=FALSE;
00388 gtk_ctree_node_set_row_data_full(ctree,GTK_CTREE_NODE(gtktreenode),subrowstatus,g_free);
00389 }
00390 newrowstatus=g_malloc(sizeof(FM_RowStatus));
00391 newrowstatus->expandedbefore=TRUE;
00392 newrowstatus->selectable=FALSE;
00393 newrowstatus->selected=FALSE;
00394 gtk_ctree_node_set_row_data_full(ctree,GTK_CTREE_NODE(node),newrowstatus,g_free);
00395 gtk_ctree_expand(ctree,GTK_CTREE_NODE(node));
00396
00397 PQclear(qryResult2);
00398
00399 }
00400 node=GTK_CTREE_NODE_NEXT(node);
00401 i++;
00402 }
00403
00404 if (strcmp(tablename,itemname)<0)
00405 {
00406 g_warning("delete: %s",tablename);
00407 node=GTK_CTREE_NODE_NEXT(node);
00408 }
00409 } else
00410 {
00411 node=GTK_CTREE_NODE_NEXT(node);
00412 }
00413 }
00414 }
00415 PQclear(qryResult);
00416 }
00417
00418 void FM_Rebuild_Clist(GtkCList *clist,gint type)
00419 {
00420 gchar *buffer;
00421 gchar *buffer2;
00422 PGresult *qryResult;
00423 PGresult *qryResult2;
00424 gchar *col[13];
00425 gint nbtuples;
00426 gint nbtuples2;
00427 gint i;
00428 gint j;
00429 GtkFMapsDatumType dattype;
00430
00431 g_return_if_fail(clist!=NULL);
00432 g_return_if_fail(conn!=NULL);
00433
00434 gtk_clist_clear(clist);
00435
00436 switch (type)
00437 {
00438 case 0:
00439 buffer=g_strdup("SELECT DISTINCT * FROM f_datum");
00440 qryResult = PQexec(conn,buffer);
00441 g_free(buffer);
00442
00443 nbtuples = PQntuples(qryResult);
00444 buffer2=NULL;
00445 i=0;
00446 while(i<nbtuples)
00447 {
00448 for(j=0;j<12;j++)
00449 {
00450 buffer = PQgetvalue(qryResult,i,j);
00451 if (j==2)
00452 {
00453 dattype=atoi(buffer);
00454 switch(dattype)
00455 {
00456 case DAT_NONE:
00457 col[j]=g_strdup("NONE");
00458 break;
00459 case DAT_MOLENDENSKY:
00460 col[j]=g_strdup("Molendensky");
00461 break;
00462 case DAT_BURSA_WOLFE:
00463 col[j]=g_strdup("Bursa-Wolfe");
00464 break;
00465 }
00466 }else if (j==3)
00467 {
00468
00469 buffer2=g_strdup_printf("SELECT DISTINCT name FROM f_ellipsoid WHERE ellipsoidid=%s",buffer);
00470 qryResult2 = PQexec(conn,buffer2);
00471 g_free(buffer2);
00472 nbtuples2 = PQntuples(qryResult2);
00473 if (nbtuples2!=0)
00474 {
00475 col[j]=g_strdup(PQgetvalue(qryResult2,0,0));
00476 } else
00477 {
00478 col[j]=g_strdup("NONE");
00479 }
00480 PQclear(qryResult2);
00481 } else
00482 {
00483 col[j]=g_strdup(buffer);
00484 }
00485 }
00486 gtk_clist_append(clist,col);
00487 for(j=0;j<12;j++)
00488 {
00489 g_free(col[j]);;
00490 }
00491 i++;
00492 }
00493 PQclear(qryResult);
00494 break;
00495
00496 case 1:
00497 buffer=g_strdup("SELECT DISTINCT * FROM f_ellipsoid");
00498 qryResult = PQexec(conn,buffer);
00499 g_free(buffer);
00500
00501 nbtuples = PQntuples(qryResult);
00502 i=0;
00503 while(i<nbtuples)
00504 {
00505 buffer = PQgetvalue(qryResult,i,0);
00506 col[0]=g_strdup(buffer);
00507 buffer = PQgetvalue(qryResult,i,1);
00508 col[1]=g_strdup(buffer);
00509 buffer = PQgetvalue(qryResult,i,2);
00510 col[2]=g_strdup(buffer);
00511 buffer = PQgetvalue(qryResult,i,3);
00512 col[3]=g_strdup(buffer);
00513 gtk_clist_append(GTK_CLIST(clist),col);
00514 g_free(col[0]);
00515 g_free(col[1]);
00516 g_free(col[2]);
00517 g_free(col[3]);
00518 i++;
00519 }
00520
00521 PQclear(qryResult);
00522 break;
00523 case 2:
00524 buffer=g_strdup("SELECT DISTINCT * FROM f_projection");
00525 qryResult = PQexec(conn,buffer);
00526 g_free(buffer);
00527
00528 nbtuples = PQntuples(qryResult);
00529 i=0;
00530 while(i<nbtuples)
00531 {
00532 for(j=0;j<=12;j++)
00533 {
00534 buffer = PQgetvalue(qryResult,i,j);
00535 if(j==2)
00536 {
00537 switch(atoi(buffer))
00538 {
00539 case 0:
00540 col[j]=g_strdup("NONE");
00541 break;
00542 case 1:
00543 col[j]=g_strdup("Longitude/Latitude");
00544 break;
00545 case 2:
00546 col[j]=g_strdup("Mercator");
00547 break;
00548 }
00549 } else
00550 {
00551 col[j]=g_strdup(buffer);
00552 }
00553 }
00554 gtk_clist_append(GTK_CLIST(clist),col);
00555 for(j=0;j<=12;j++)
00556 {
00557 g_free(col[j]);
00558 }
00559 i++;
00560 }
00561
00562 PQclear(qryResult);
00563 break;
00564 }
00565 }