PostGIS  3.7.0dev-r@@SVN_REVISION@@

◆ pgui_action_export()

static void pgui_action_export ( GtkWidget *  widget,
gpointer  data 
)
static

Definition at line 1789 of file shp2pgsql-gui.c.

1790 {
1791  SHPDUMPERCONFIG *dumper_table_config;
1792  SHPDUMPERSTATE *state;
1793  gint is_valid;
1794  gpointer gptr;
1795  GtkTreeIter iter;
1796  char *output_shapefile, *orig_shapefile;
1797  char progress_text[GUIMSG_LINE_MAXLEN+1];
1798  gchar *folder_path;
1799 
1800  int ret, success = FALSE, i = 0;
1801 
1802  /* Get the first row of the import list */
1803  is_valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(export_table_list_store), &iter);
1804  if (!is_valid)
1805  {
1806  pgui_seterr(_("ERROR: You haven't specified any tables to export"));
1808 
1809  return;
1810  }
1811 
1812  /* Firstly make sure that we can connect to the database - if we can't then there isn't much
1813  point doing anything else... */
1814  if (!connection_test())
1815  {
1816  pgui_seterr(_("Unable to connect to the database - please check your connection settings"));
1818 
1819  /* Open the connections UI for the user */
1821 
1822  gtk_widget_show_all(GTK_WIDGET(window_conn));
1823  return;
1824  }
1825 
1826  /* Now open the file selector dialog so the user can specify where they would like the output
1827  files to reside */
1828  if (gtk_dialog_run(GTK_DIALOG(dialog_folderchooser)) != GTK_RESPONSE_ACCEPT)
1829  {
1830  gtk_widget_hide(dialog_folderchooser);
1831 
1832  return;
1833  }
1834 
1835  gtk_widget_hide(dialog_folderchooser);
1836  folder_path = gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER(dialog_folderchooser));
1837 
1838  /* Now everything is set up, let's extract the tables */
1839  is_valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(export_table_list_store), &iter);
1840  while (is_valid)
1841  {
1842  /* Grab the SHPDUMPERCONFIG for this row */
1843  gtk_tree_model_get(GTK_TREE_MODEL(export_table_list_store), &iter, EXPORT_POINTER_COLUMN, &gptr, -1);
1844  dumper_table_config = (SHPDUMPERCONFIG *)gptr;
1845 
1846  pgui_logf("\n==============================");
1847  pgui_logf("Exporting with configuration: %s, %s, %s", dumper_table_config->table, dumper_table_config->schema, dumper_table_config->shp_file);
1848 
1849  /* Export is running */
1850  is_running = TRUE;
1851  success = FALSE;
1852 
1853  /* Disable the button to prevent multiple imports running at the same time */
1854  gtk_widget_set_sensitive(widget, FALSE);
1855 
1856  /* Allow GTK events to get a look in */
1857  while (gtk_events_pending())
1858  gtk_main_iteration();
1859 
1860  /* Create the state for each configuration */
1861  state = ShpDumperCreate(dumper_table_config);
1862  state->config->conn = conn;
1863 
1864  /* Save the original shapefile name, then create a temporary version containing the full path */
1865  orig_shapefile = dumper_table_config->shp_file;
1866  output_shapefile = malloc(strlen(folder_path) + strlen(dumper_table_config->shp_file) + 2);
1867  strcpy(output_shapefile, folder_path);
1868  strcat(output_shapefile, G_DIR_SEPARATOR_S);
1869  strcat(output_shapefile, dumper_table_config->shp_file);
1870 
1871  dumper_table_config->shp_file = output_shapefile;
1872 
1873  /* Connect to the database */
1874  ret = ShpDumperConnectDatabase(state);
1875  if (ret != SHPDUMPEROK)
1876  {
1877  pgui_seterr("%s", state->message);
1879 
1880  goto export_cleanup;
1881  }
1882 
1883  /* Display the progress dialog */
1884  gtk_label_set_text(GTK_LABEL(label_progress), _("Initialising..."));
1885  gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress), 0.0);
1886  gtk_widget_show_all(dialog_progress);
1887 
1888  ret = ShpDumperOpenTable(state);
1889  if (ret != SHPDUMPEROK)
1890  {
1891  pgui_logf("%s", state->message);
1892 
1893  if (ret == SHPDUMPERERR)
1894  {
1895  gtk_widget_hide(dialog_progress);
1896 
1897  pgui_seterr("%s", state->message);
1899 
1900  goto export_cleanup;
1901  }
1902  }
1903 
1904  /* Update the text */
1905  snprintf(progress_text, GUIMSG_LINE_MAXLEN, _("Exporting table %s (%d records)..."), dumper_table_config->table, ShpDumperGetRecordCount(state));
1906  progress_text[GUIMSG_LINE_MAXLEN] = '\0';
1907  gtk_label_set_text(GTK_LABEL(label_progress), progress_text);
1908 
1909  /* Allow GTK events to get a look in */
1910  while (gtk_events_pending())
1911  gtk_main_iteration();
1912 
1913  pgui_logf(_("Done (postgis major version: %d)"), state->pgis_major_version);
1914  pgui_logf(_("Output shape: %s"), shapetypename(state->outshptype));
1915 
1916  for (i = 0; i < ShpDumperGetRecordCount(state) && is_running == TRUE; i++)
1917  {
1918  ret = ShpLoaderGenerateShapeRow(state);
1919  if (ret != SHPDUMPEROK)
1920  {
1921  pgui_logf("%s", state->message);
1922 
1923  if (ret == SHPDUMPERERR)
1924  {
1925  gtk_widget_hide(dialog_progress);
1926 
1927  pgui_seterr("%s", state->message);
1929 
1930  goto export_cleanup;
1931  }
1932  }
1933 
1934  /* Update the progress bar */
1935  gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress), (float)i / ShpDumperGetRecordCount(state));
1936 
1937  /* Allow GTK events to get a look in */
1938  while (gtk_events_pending())
1939  gtk_main_iteration();
1940  }
1941 
1942  /* Finish the dump */
1943  ret = ShpDumperCloseTable(state);
1944  if (ret != SHPDUMPEROK)
1945  {
1946  pgui_logf("%s", state->message);
1947 
1948  if (ret == SHPDUMPERERR)
1949  {
1950  gtk_widget_hide(dialog_progress);
1951 
1952  pgui_seterr("%s", state->message);
1954  }
1955  }
1956 
1957  /* Indicate success */
1958  if (is_running)
1959  success = TRUE;
1960 
1961 export_cleanup:
1962 
1963  /* Tidy up everything */
1964  ShpDumperDestroy(state);
1965 
1966  /* Reset shapefile back to original form (without full path) */
1967  dumper_table_config->shp_file = orig_shapefile;
1968 
1969  /* Get next entry */
1970  is_valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(export_table_list_store), &iter);
1971  }
1972 
1973  /* Export has definitely finished */
1974  is_running = FALSE;
1975  if (!success)
1976  pgui_logf(_("Table export failed."));
1977  else
1978  pgui_logf(_("Table export completed."));
1979 
1980  /* Enable the button once again */
1981  gtk_widget_set_sensitive(widget, TRUE);
1982 
1983  /* Silly GTK bug means we have to hide and show the button for it to work again! */
1984  gtk_widget_hide(widget);
1985  gtk_widget_show(widget);
1986 
1987  /* Hide the progress dialog */
1988  gtk_widget_hide(dialog_progress);
1989 
1990  /* Allow GTK events to get a look in */
1991  while (gtk_events_pending())
1992  gtk_main_iteration();
1993 
1994  return;
1995 }
#define TRUE
Definition: dbfopen.c:73
#define FALSE
Definition: dbfopen.c:72
void * malloc(YYSIZE_T)
int ShpDumperGetRecordCount(SHPDUMPERSTATE *state)
void ShpDumperDestroy(SHPDUMPERSTATE *state)
char * shapetypename(int num)
SHPDUMPERSTATE * ShpDumperCreate(SHPDUMPERCONFIG *config)
int ShpDumperConnectDatabase(SHPDUMPERSTATE *state)
int ShpLoaderGenerateShapeRow(SHPDUMPERSTATE *state)
int ShpDumperCloseTable(SHPDUMPERSTATE *state)
int ShpDumperOpenTable(SHPDUMPERSTATE *state)
#define SHPDUMPEROK
#define SHPDUMPERERR
static GtkWidget * label_progress
static GtkWidget * progress
static void static void static void static void pgui_seterr(const char *fmt,...) __attribute__((format(printf
static volatile int is_running
static void static void static void pgui_logf(const char *fmt,...) __attribute__((format(printf
static void pgui_raise_error_dialogue(void)
static void static void static void static void static void update_conn_ui_from_conn_config(void)
static GtkWidget * dialog_progress
static int connection_test(void)
GtkListStore * export_table_list_store
Definition: shp2pgsql-gui.c:82
@ EXPORT_POINTER_COLUMN
#define GUIMSG_LINE_MAXLEN
static GtkWidget * dialog_folderchooser
static SHPCONNECTIONCONFIG * conn
static GtkWidget * window_conn
#define _(String)
Definition: shpcommon.h:24
SHPCONNECTIONCONFIG * conn
SHPDUMPERCONFIG * config
char message[SHPDUMPERMSGLEN]

References _, shp_dumper_state::config, shp_dumper_config::conn, conn, connection_test(), dialog_folderchooser, dialog_progress, EXPORT_POINTER_COLUMN, export_table_list_store, FALSE, GUIMSG_LINE_MAXLEN, is_running, label_progress, malloc(), shp_dumper_state::message, shp_dumper_state::outshptype, shp_dumper_state::pgis_major_version, pgui_logf(), pgui_raise_error_dialogue(), pgui_seterr(), progress, shp_dumper_config::schema, shapetypename(), shp_dumper_config::shp_file, ShpDumperCloseTable(), ShpDumperConnectDatabase(), ShpDumperCreate(), ShpDumperDestroy(), SHPDUMPERERR, ShpDumperGetRecordCount(), SHPDUMPEROK, ShpDumperOpenTable(), ShpLoaderGenerateShapeRow(), shp_dumper_config::table, TRUE, update_conn_ui_from_conn_config(), and window_conn.

Referenced by pgui_create_main_window().

Here is the call graph for this function:
Here is the caller graph for this function: