Main Page   Alphabetical List   Compound List   File List   Compound Members   File Members  

main.c

Go to the documentation of this file.
00001  /*
00002  * Initial main.c file generated by Glade. Edit as required.
00003  * Glade will not overwrite this file.
00004  */
00005 
00006 /* main.c - where everything start...
00007  *
00008  * Copyright (C) 2000  Franck Martin <franck@sopac.org>
00009  *
00010  * This Program is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2 of the License, or (at your option) any later version.
00014  *
00015  * This Program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Library General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public
00021  * License along with this Program; if not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00023  * Boston, MA 02111-1307, USA.
00024  */
00025 
00026 /*
00027  * Modified by the FMaps Team and others 2000.  See the AUTHORS
00028  * file for a list of people on the FMaps Team.  See the ChangeLog
00029  * files for a list of changes.  These files are distributed with
00030  * FMaps at http://FMaps.sourceforge.net/.
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);  /* make the database fmaps compatible */
00086     qryResult = PQexec(conn,buffer);
00087 
00088     /* debug */
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 /* FM_CheckDB
00103  * Check that the database is compatible with FMaps
00104  *  For the moment check that the system tables exists, if not create them
00105  *  The Procedure should check for the structure of the tables also
00106  *  And Perform upgrades if needed
00107  *
00108  */
00109  
00110 void FM_CheckDB(PGconn *conn)
00111 {
00112   gint result;
00113   
00114   g_return_if_fail (conn!=NULL);
00115   
00116   /* Create the Metadata tables ISO 15046-15 Conformance Level 1 */
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   /* Create the Projection/Datum reference tables */
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     /* make a connection to the database */
00157     conn = PQsetdbLogin(pghost, pgport, pgoptions, pgtty, dbName, username, password);
00158   }
00159     /*
00160     * check to see that the backend connection was successfully made
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   /* Check if f_tables exists, if not creates it...*/
00173 
00174   FM_CheckDB(conn);
00175 
00176   /* retreive the list of tables and store it in ctree_table_list*/
00177 
00178   FM_Rebuild_Directory();
00179   
00180 }
00181 
00182 void
00183 fmaps_quit(void)
00184 {
00185   if(conn!=NULL)
00186   {
00187         PQfinish(conn); /* terminate the connection to database */
00188   }
00189   gtk_main_quit();
00190 }
00191 
00192 void
00193 fmaps_init(void)
00194 {
00195 
00196   /*
00197   * begin, by setting the parameters for a backend connection if the
00198   * parameters are null, then the system will try to use reasonable
00199   * defaults by looking up environment variables or, failing that,
00200   * using hardwired constants
00201   */
00202   pghost = NULL;                        /* host name of the backend server */
00203   pgport = NULL;                        /* port of the backend server */
00204   pgoptions = NULL;                     /* special options to start up the backend server */
00205   pgtty = NULL;                         /* debugging tty for the backend server */
00206   dbName = NULL;                        /* name of your database */
00207   username = NULL;                      /* automatic authentication */
00208   password = NULL;                      /* automatic authentication */
00209 
00210   conn=NULL;
00211 
00212   /* Let's use gnome configuration functions */
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   /* load some defaults */
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   /* display the main window */
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 /* This procedure create or rebuild the directory in the main application window
00269  * It should be called each time an import or deletion is called
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   /* There are no nodes in the tree so we create the ctree */
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  /* there are nodes so we refresh the ctree */
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       /* this an item and not a table so we check what to do*/
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         /* there is a new item it needs to be inserted */
00339         if (strcmp(tablename,itemname)>0)
00340         {
00341 g_warning("insert: %s",itemname);      
00342           i++; 
00343         }
00344         /* item and table match so we refresh the list */
00345         if (strcmp(tablename,itemname)==0)
00346         {
00347 g_warning("matching");
00348           /* it has been expanded before so we refresh */
00349           if (rowstatus->expandedbefore)
00350           {
00351 g_warning("refreshing table list of item: %s",itemname);
00352 
00353             /*remove the node (prune the tree*/
00354             gtk_ctree_remove_node(ctree,node);
00355             
00356             /*re insert the node */
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             /* and expand */
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               /*subrowstatus = gtk_ctree_node_get_row_data(ctree,GTK_CTREE_NODE(gtktreenode));*/
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         /* this item does not exist anymore so delete*/
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:       /* datum */
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)     /* type */
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)       /* ellipsoidid */
00467           {
00468             /* retreive ellipsoid name*/
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:       /* ellipsoid */
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:       /* projection */
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 }

Generated at Sat Jan 6 20:55:34 2001 for FMaps by doxygen1.2.1 written by Dimitri van Heesch, © 1997-2000