PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ pgui_action_export()

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

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

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().

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