~ubuntu-branches/ubuntu/trusty/postgis/trusty-updates

« back to all changes in this revision

Viewing changes to loader/shp2pgsql-gui.c

  • Committer: Bazaar Package Importer
  • Author(s): Alan Boudreault
  • Date: 2010-09-29 09:16:10 UTC
  • mfrom: (1.1.12 upstream)
  • Revision ID: james.westby@ubuntu.com-20100929091610-vj4efw8woq34hdn7
Tags: 1.5.2-1
* New upstream release, with a few bug fixes.
* Added shp2pgsql-gui binary.
* Removed patches, applied upstream: getopt.    

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/**********************************************************************
2
 
 * $Id: shp2pgsql-gui.c 5181 2010-02-01 17:35:55Z pramsey $
 
2
 * $Id: shp2pgsql-gui.c 5983 2010-09-19 11:27:05Z mcayland $
3
3
 *
4
4
 * PostGIS - Spatial Types for PostgreSQL
5
5
 * http://postgis.refractions.net
17
17
#include <stdlib.h>
18
18
#include <string.h>
19
19
#include <gtk/gtk.h>
 
20
#ifdef MAC_INTEGRATION
 
21
#include <ige-mac-integration.h>
 
22
#endif
20
23
#include "libpq-fe.h"
21
24
#include "shp2pgsql-core.h"
22
25
 
23
 
#define GUI_RCSID "shp2pgsql-gui $Revision: 5181 $"
 
26
#define GUI_RCSID "shp2pgsql-gui $Revision: 5983 $"
24
27
 
25
28
/*
26
29
** Global variables for GUI only
71
74
static void
72
75
pgui_log_va(const char *fmt, va_list ap)
73
76
{
 
77
        GtkTextIter iter;
74
78
        char *msg;
75
79
 
76
80
        if (!lw_vasprintf (&msg, fmt, ap)) return;
77
81
 
78
 
        gtk_text_buffer_insert_at_cursor(textbuffer_log, msg, -1);
79
 
        gtk_text_buffer_insert_at_cursor(textbuffer_log, "\n", -1);
80
 
        gtk_text_view_scroll_mark_onscreen(GTK_TEXT_VIEW(textview_log), gtk_text_buffer_get_insert(textbuffer_log) );
 
82
        /* Append text to the end of the text area, scrolling if required to make it visible */
 
83
        gtk_text_buffer_get_end_iter(textbuffer_log, &iter);
 
84
        gtk_text_buffer_insert(textbuffer_log, &iter, msg, -1);
 
85
        gtk_text_buffer_insert(textbuffer_log, &iter, "\n", -1);
81
86
 
 
87
        gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(textview_log), &iter, 0.0, TRUE, 0.0, 1.0);
 
88
        
82
89
        /* Allow GTK to process events */
83
90
        while (gtk_events_pending())
84
91
                gtk_main_iteration();
121
128
 
122
129
        label = gtk_label_new(pgui_errmsg);
123
130
        dialog = gtk_dialog_new_with_buttons("Error", GTK_WINDOW(window_main),
124
 
                                             GTK_DIALOG_MODAL & GTK_DIALOG_NO_SEPARATOR & GTK_DIALOG_DESTROY_WITH_PARENT,
125
 
                                             GTK_STOCK_OK, GTK_RESPONSE_OK, NULL);
 
131
                                                                                 GTK_DIALOG_MODAL & GTK_DIALOG_NO_SEPARATOR & GTK_DIALOG_DESTROY_WITH_PARENT,
 
132
                                                                                 GTK_STOCK_OK, GTK_RESPONSE_OK, NULL);
126
133
        gtk_dialog_set_has_separator ( GTK_DIALOG(dialog), FALSE );
127
134
        gtk_container_set_border_width (GTK_CONTAINER(dialog), 5);
128
135
        gtk_container_set_border_width (GTK_CONTAINER (GTK_DIALOG(dialog)->vbox), 15);
193
200
        else
194
201
                config->createindex = 0;
195
202
 
196
 
        /* Read the .shp file, don't ignore it */
 
203
        /* Don't read the .shp file, just the .dbf */
197
204
        if ( dbfonly )
 
205
        {
198
206
                config->readshape = 0;
 
207
                /* There will be no spatial column so don't create a spatial index */
 
208
                config->createindex = 0; 
 
209
        }
199
210
        else
200
211
                config->readshape = 1;
201
212
 
432
443
        const char *pg_pass = gtk_entry_get_text(GTK_ENTRY(entry_pg_pass));
433
444
        const char *pg_db = gtk_entry_get_text(GTK_ENTRY(entry_pg_db));
434
445
        char *connection_string = NULL;
435
 
 
436
 
        if ( ! pg_host || strlen(pg_host) == 0 )
437
 
        {
438
 
                pgui_seterr("Fill in the server host.");
439
 
                return NULL;
440
 
        }
441
 
        if ( ! pg_port || strlen(pg_port) == 0 )
442
 
        {
443
 
                pgui_seterr("Fill in the server port.");
444
 
                return NULL;
445
 
        }
446
 
        if ( ! pg_user || strlen(pg_user) == 0 )
447
 
        {
448
 
                pgui_seterr("Fill in the user name.");
449
 
                return NULL;
450
 
        }
451
 
        if ( ! pg_db || strlen(pg_db) == 0 )
452
 
        {
453
 
                pgui_seterr("Fill in the database name.");
454
 
                return NULL;
455
 
        }
456
 
        if ( ! atoi(pg_port) )
457
 
        {
458
 
                pgui_seterr("Server port must be a number.");
459
 
                return NULL;
460
 
        }
461
 
        if ( ! lw_asprintf(&connection_string, "user=%s password=%s port=%s host=%s dbname=%s", pg_user, pg_pass, pg_port, pg_host, pg_db) )
462
 
        {
463
 
                return NULL;
464
 
        }
465
 
        if ( connection_string )
466
 
        {
467
 
                return connection_string;
468
 
        }
469
 
        return NULL;
 
446
        char *escape_pg_pass = NULL;
 
447
 
 
448
        stringbuffer_t *sb = stringbuffer_create();
 
449
 
 
450
        /* Read the host */
 
451
        if ( pg_host && strlen(pg_host) > 0 )
 
452
        {
 
453
                vasbappend(sb, "host=%s ", pg_host);
 
454
        }
 
455
 
 
456
        /* Read the port */
 
457
        if ( pg_port && strlen(pg_port) > 0 )
 
458
        {       
 
459
                if ( ! atoi(pg_port) )
 
460
                {
 
461
                        pgui_seterr("Server port must be a number.");
 
462
                        stringbuffer_destroy(sb);
 
463
                        return NULL;
 
464
                }
 
465
                vasbappend(sb, "port=%s ", pg_port);
 
466
        }
 
467
 
 
468
        /* Read the user name */
 
469
        if ( pg_user && strlen(pg_user) > 0 )
 
470
        {
 
471
                vasbappend(sb, "user=%s ", pg_user);
 
472
        }
 
473
 
 
474
        /* Read the database name */
 
475
        if ( pg_db && strlen(pg_db) > 0 )
 
476
        {
 
477
                vasbappend(sb, "dbname=%s ", pg_db);
 
478
        }
 
479
 
 
480
        /* Read the password */
 
481
        if ( pg_pass && strlen(pg_pass) > 0 )
 
482
        {
 
483
                /* Escape the password in case it contains any special characters */
 
484
                escape_pg_pass = escape_connection_string((char *)pg_pass);
 
485
                vasbappend(sb, "password='%s' ", escape_pg_pass);
 
486
 
 
487
                /* Free the escaped version */
 
488
                if (escape_pg_pass != pg_pass)
 
489
                        free(escape_pg_pass);
 
490
        }
 
491
 
 
492
        /* Return the connection string */
 
493
        connection_string = strdup(stringbuffer_getstring(sb));
 
494
        stringbuffer_destroy(sb);       
 
495
        return connection_string;
470
496
}
471
497
 
472
498
static void
484
510
        char *ptr = strstr(connection_string, "password");
485
511
        if ( ptr )
486
512
        {
487
 
                ptr += 9;
488
 
                while ( *ptr != ' ' && *ptr != '\0' )
 
513
                ptr += 10;
 
514
                while ( *ptr != '\'' && *ptr != '\0' )
489
515
                {
490
 
                        *ptr = '*';
491
 
                        ptr++;
 
516
                        /* If we find a \, hide both it and the next character */
 
517
                        if ( *ptr == '\\' )
 
518
                                *ptr++ = '*';
 
519
                
 
520
                        *ptr++ = '*';
492
521
                }
493
522
        }
494
523
        return;
603
632
        char *connection_string = NULL;
604
633
        char *connection_sanitized = NULL;
605
634
        char *dest_string = NULL;
606
 
        int ret, i = 0;
 
635
        int ret, record_count = 0, i = 0, progress_bar_update_frequency = 1;
607
636
        char *header, *footer, *record;
608
637
        PGresult *result;
609
638
 
722
751
        /* Main loop: iterate through all of the records and send them to stdout */
723
752
        pgui_logf("Importing shapefile (%d records)...", ShpLoaderGetRecordCount(state));
724
753
 
 
754
    /* Number of records in this file */
 
755
    record_count = ShpLoaderGetRecordCount(state);
 
756
    /* Assume a progress bar 1000 pixels across, how we only want to update it every N records */
 
757
    progress_bar_update_frequency = 1 + record_count / 1000;
 
758
    
725
759
        import_running = TRUE;
726
 
        for (i = 0; i < ShpLoaderGetRecordCount(state) && import_running; i++)
 
760
        for (i = 0; (i < record_count) && import_running; i++)
727
761
        {
728
762
                ret = ShpLoaderGenerateSQLRowStatement(state, i, &record);
729
763
 
775
809
                }
776
810
 
777
811
                /* Update the progress bar */
778
 
                gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress), (float)i / ShpLoaderGetRecordCount(state));
 
812
        if( i % progress_bar_update_frequency == 0 )
 
813
        {
 
814
                    gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress), (float)i / record_count);
 
815
            }
779
816
 
780
817
                /* Allow GTK events to get a look in */
781
818
                while (gtk_events_pending())
985
1022
        GtkWidget *hbox_buttons, *button_options, *button_import, *button_cancel, *button_about;
986
1023
        /* Log section */
987
1024
        GtkWidget *scrolledwindow_log;
 
1025
#ifdef MAC_INTEGRATION
 
1026
        /* MacOSX Command-Quit menu item */
 
1027
        GtkWidget *menu_item_quit;
 
1028
#endif
988
1029
 
989
1030
        /* create the main, top level, window */
990
1031
        window_main = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1012
1053
        file_chooser_dialog_shape = gtk_file_chooser_dialog_new( "Select a Shape File", GTK_WINDOW (window_main), GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL);
1013
1054
        file_chooser_button_shape = gtk_file_chooser_button_new_with_dialog( file_chooser_dialog_shape );
1014
1055
        gtk_container_set_border_width (GTK_CONTAINER (file_chooser_button_shape), 8);
 
1056
        /* Filter for .shp files */
1015
1057
        file_filter_shape = gtk_file_filter_new();
1016
1058
        gtk_file_filter_add_pattern(GTK_FILE_FILTER(file_filter_shape), "*.shp");
1017
 
        gtk_file_filter_set_name(GTK_FILE_FILTER(file_filter_shape), "Shapefiles (*.shp)");
1018
 
        gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(file_chooser_button_shape), file_filter_shape);
 
1059
        gtk_file_filter_set_name(GTK_FILE_FILTER(file_filter_shape), "Shape Files (*.shp)");
 
1060
        gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(file_chooser_button_shape), file_filter_shape);
 
1061
        /* Filter for .dbf files */
 
1062
        file_filter_shape = gtk_file_filter_new();
 
1063
        gtk_file_filter_add_pattern(GTK_FILE_FILTER(file_filter_shape), "*.dbf");
 
1064
        gtk_file_filter_set_name(GTK_FILE_FILTER(file_filter_shape), "DBF Files (*.dbf)");
 
1065
        gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(file_chooser_button_shape), file_filter_shape);
 
1066
        
1019
1067
        gtk_container_add (GTK_CONTAINER (frame_shape), file_chooser_button_shape);
1020
1068
        g_signal_connect (G_OBJECT (file_chooser_button_shape), "file-set", G_CALLBACK (pgui_action_shape_file_set), NULL);
1021
1069
 
1158
1206
        gtk_container_add (GTK_CONTAINER (scrolledwindow_log), textview_log);
1159
1207
        gtk_container_add (GTK_CONTAINER (frame_log), scrolledwindow_log);
1160
1208
 
 
1209
#ifdef MAC_INTEGRATION
 
1210
        /*
 
1211
        ** OS/X menu integration for command-Q
 
1212
        */
 
1213
        menu_item_quit = gtk_menu_item_new();
 
1214
        g_signal_connect (G_OBJECT (menu_item_quit), "activate", G_CALLBACK (pgui_action_cancel), NULL);
 
1215
        ige_mac_menu_set_quit_menu_item (GTK_MENU_ITEM (menu_item_quit));
 
1216
#endif 
 
1217
 
1161
1218
        /*
1162
1219
        ** Main window
1163
1220
        */
1197
1254
{
1198
1255
        char c;
1199
1256
 
1200
 
        /* Parse command line options and set configuration */
 
1257
        /* Set default configuration */
1201
1258
        config = malloc(sizeof(SHPLOADERCONFIG));
1202
1259
        set_config_defaults(config);
1203
1260
 
1204
1261
        /* Here we override any defaults for the GUI */
1205
1262
        config->createindex = 1;
1206
1263
 
 
1264
        /* Prepare our shape connection */
1207
1265
        conn = malloc(sizeof(SHPCONNECTIONCONFIG));
1208
1266
        memset(conn, 0, sizeof(SHPCONNECTIONCONFIG));
1209
1267
 
 
1268
    /* Read any environment values */
 
1269
    conn->port = getenv("PGPORT");
 
1270
    conn->username = getenv("PGUSER");
 
1271
    conn->database = getenv("PGDATABASE");
 
1272
    conn->host = getenv("PGHOST");
 
1273
 
 
1274
        /* Over-ride the environment with any command line options */
1210
1275
        while ((c = pgis_getopt(argc, argv, "U:p:W:d:h:")) != -1)
1211
1276
        {
1212
1277
                switch (c)
1213
1278
                {
1214
1279
                case 'U':
1215
 
                        conn->username = optarg;
 
1280
                        conn->username = pgis_optarg;
1216
1281
                        break;
1217
1282
                case 'p':
1218
 
                        conn->port = optarg;
 
1283
                        conn->port = pgis_optarg;
1219
1284
                        break;
1220
1285
                case 'W':
1221
 
                        conn->password = optarg;
 
1286
                        conn->password = pgis_optarg;
1222
1287
                        break;
1223
1288
                case 'd':
1224
 
                        conn->database = optarg;
 
1289
                        conn->database = pgis_optarg;
1225
1290
                        break;
1226
1291
                case 'h':
1227
 
                        conn->host = optarg;
 
1292
                        conn->host = pgis_optarg;
1228
1293
                        break;
1229
1294
                default:
1230
1295
                        usage();