PostGIS  2.2.8dev-r@@SVN_REVISION@@

◆ pgui_action_export()

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

Definition at line 1763 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().

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