PostGIS  3.0.6dev-r@@SVN_REVISION@@

◆ pgui_action_export()

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

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

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