PostGIS  3.7.0dev-r@@SVN_REVISION@@

◆ pgui_action_export()

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

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

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