1 /* MDB Tools - A library for reading MS Access database file
2  * Copyright (C) 2000 Brian Bruns
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #include "gmdb.h"
20 
21 extern GtkWidget *app;
22 extern MdbHandle *mdb;
23 
24 GtkBuilder *schemawin_xml;
25 static gchar backend[100];
26 static gchar tabname[MDB_MAX_OBJ_NAME+1];
27 static guint32 export_options;
28 
29 #define ALL_TABLES   "(All Tables)"
30 
31 static struct {
32 	const char *option_name;
33 	guint32     option_value;
34 } capabilities_xlt[] = {
35 	{ "drop_checkbox", MDB_SHEXP_DROPTABLE },
36 	{ "cstnotnull_checkbox", MDB_SHEXP_CST_NOTNULL },
37 	{ "cstnotempty_checkbox", MDB_SHEXP_CST_NOTEMPTY},
38 	{ "comments_checkbox", MDB_SHEXP_COMMENTS},
39 	{ "defaults_checkbox", MDB_SHEXP_DEFVALUES },
40 	{ "index_checkbox", MDB_SHEXP_INDEXES},
41 	{ "rel_checkbox", MDB_SHEXP_RELATIONS}
42 };
43 #define n_capabilities (sizeof(capabilities_xlt)/sizeof(capabilities_xlt[0]))
44 
45 static void
gmdb_schema_export(const gchar * file_path)46 gmdb_schema_export(const gchar *file_path)
47 {
48 FILE *outfile;
49 
50 	GtkWidget *dlg;
51 
52 	//printf("file path %s\n",file_path);
53 	if ((outfile=fopen(file_path, "w"))==NULL) {
54 		GtkWidget* dlg = gtk_message_dialog_new (NULL,
55 		    GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_WARNING, GTK_BUTTONS_CLOSE,
56 		    _("Unable to open file."));
57 		gtk_dialog_run (GTK_DIALOG (dlg));
58 		gtk_widget_destroy (dlg);
59 		return;
60 	}
61 	mdb_set_default_backend(mdb,backend);
62 
63 	mdb_print_schema(mdb, outfile, *tabname?tabname:NULL, NULL, export_options);
64 
65 	fclose(outfile);
66 	dlg = gtk_message_dialog_new (NULL,
67 	    GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_WARNING, GTK_BUTTONS_CLOSE,
68 	    _("Schema exported successfully."));
69 	gtk_dialog_run (GTK_DIALOG (dlg));
70 	gtk_widget_destroy (dlg);
71 }
72 void
gmdb_schema_export_cb(GtkWidget * w,gpointer data)73 gmdb_schema_export_cb(GtkWidget *w, gpointer data)
74 {
75 GtkWidget *schemawin, *checkbox, *chooser;
76 GtkComboBoxText *combobox;
77 const gchar *file_path;
78 gchar *tmp;
79 int i;
80 
81 	schemawin = GTK_WIDGET(gtk_builder_get_object(schemawin_xml, "schema_dialog"));
82 
83 	chooser = GTK_WIDGET(gtk_builder_get_object(schemawin_xml, "filename_entry"));
84 	file_path = gtk_entry_get_text(GTK_ENTRY(chooser));
85 
86 	combobox = GTK_COMBO_BOX_TEXT(gtk_builder_get_object(schemawin_xml, "table_combo"));
87 	tmp = gtk_combo_box_text_get_active_text(combobox);
88 	strncpy(tabname,tmp,MDB_MAX_OBJ_NAME);
89 	tabname[MDB_MAX_OBJ_NAME]=0;
90 	if (!strcmp(tabname,ALL_TABLES))
91 		tabname[0]='\0';
92 
93 	combobox = GTK_COMBO_BOX_TEXT(gtk_builder_get_object(schemawin_xml, "backend_combo"));
94 	tmp = gtk_combo_box_text_get_active_text(combobox);
95 	if (!strcmp(tmp, "Oracle"))
96 		strcpy(backend, "oracle");
97 	else if (!strcmp(tmp, "Sybase"))
98 		strcpy(backend,"sybase");
99 	else if (!strcmp(tmp,"MS SQL Server"))
100 		strcpy(backend,"sybase");
101 	else if (!strcmp(tmp, "PostgreSQL"))
102 		strcpy(backend,"postgres");
103 	else if (!strcmp(tmp,"MySQL"))
104 		strcpy(backend,"mysql");
105 	else if (!strcmp(tmp,"SQLite"))
106 		strcpy(backend,"sqlite");
107 	else
108 		strcpy(backend,"access");
109 
110 	/* make sure unknown default options are enabled */
111 	export_options = MDB_SHEXP_DEFAULT;
112 	for (i=0; i<n_capabilities; ++i)
113 		export_options &= ~capabilities_xlt[i].option_value;
114 
115 	/* fill the bit field from the checkboxes */
116 	for (i=0; i<n_capabilities; ++i) {
117 		checkbox = GTK_WIDGET(gtk_builder_get_object(schemawin_xml, capabilities_xlt[i].option_name));
118 		if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(checkbox)))
119 			export_options |= capabilities_xlt[i].option_value;
120 	}
121 	//printf("%s %s %02X\n",tabname,backend,export_options);
122 
123 	gmdb_schema_export(file_path);
124     gtk_widget_destroy(schemawin);
125 }
126 static void
check_default_options()127 check_default_options() {
128 	int i;
129 	GtkToggleButton *checkbox;
130 	for (i=0; i<n_capabilities; ++i) {
131 		checkbox = GTK_TOGGLE_BUTTON(gtk_builder_get_object(schemawin_xml, capabilities_xlt[i].option_name));
132 		gtk_toggle_button_set_active(checkbox, (MDB_SHEXP_DEFAULT & capabilities_xlt[i].option_value) != 0);
133 	}
134 }
135 static void
refresh_available_options()136 refresh_available_options() {
137 	GtkComboBoxText *combobox;
138 	GtkWidget *checkbox;
139 	guint32 capabilities;
140 	MdbBackend *backend_obj;
141 	int i;
142 
143 	combobox = GTK_COMBO_BOX_TEXT(gtk_builder_get_object(schemawin_xml, "backend_combo"));
144 	if (!combobox) return; /* window is beeing destroyed */
145 
146 	const gchar *backend_name = gtk_combo_box_text_get_active_text(combobox);
147 	if      (!strcmp(backend_name,"Oracle")) strcpy(backend,"oracle");
148 	else if (!strcmp(backend_name,"Sybase")) strcpy(backend,"sybase");
149 	else if (!strcmp(backend_name,"MS SQL Server")) strcpy(backend,"sybase");
150 	else if (!strcmp(backend_name,"PostgreSQL")) strcpy(backend,"postgres");
151 	else if (!strcmp(backend_name,"MySQL")) strcpy(backend,"mysql");
152 	else if (!strcmp(backend_name,"SQLite")) strcpy(backend,"sqlite");
153 	else strcpy(backend,"access");
154 
155 	backend_obj = (MdbBackend *) g_hash_table_lookup(mdb->backends, backend);
156 	//printf("backend_obj: %p\n", backend_obj);
157 
158 	capabilities = backend_obj->capabilities;
159 	//printf("backend capabilities: 0x%04x\n", capabilities);
160 	for (i=0; i<n_capabilities; ++i) {
161 		checkbox = GTK_WIDGET(gtk_builder_get_object(schemawin_xml, capabilities_xlt[i].option_name));
162 		gtk_widget_set_sensitive(checkbox, (capabilities & capabilities_xlt[i].option_value) != 0);
163 	}
164 }
165 
166 void
gmdb_schema_backend_cb(GtkComboBox * widget,gpointer data)167 gmdb_schema_backend_cb(GtkComboBox *widget, gpointer data)
168 {
169 	refresh_available_options();
170 }
171 
172 void
gmdb_schema_new_cb(GtkWidget * w,gpointer data)173 gmdb_schema_new_cb(GtkWidget *w, gpointer data)
174 {
175 	GtkComboBoxText *combobox;
176 	MdbCatalogEntry *entry;
177     GError *error = NULL;
178 	int i;
179 
180 	/* load the interface */
181 	schemawin_xml = gtk_builder_new();
182     if (!gtk_builder_add_from_file(schemawin_xml, GMDB_UIDIR "gmdb-schema.ui", NULL)) {
183         g_warning("Error adding " GMDB_UIDIR "gmdb-schema.ui: %s", error->message);
184         g_error_free(error);
185     }
186 	/* connect the signals in the interface */
187 	gtk_builder_connect_signals(schemawin_xml, NULL);
188 	/* set up capabilities call back. TODO: autoconnect should do that */
189 	combobox = GTK_COMBO_BOX_TEXT(gtk_builder_get_object(schemawin_xml, "backend_combo"));
190 	gtk_combo_box_set_active(GTK_COMBO_BOX(combobox), 0);
191 	g_signal_connect(combobox, "changed",
192 		G_CALLBACK(gmdb_schema_backend_cb), NULL);
193 
194 	/* set signals with user data, anyone know how to do this in glade? */
195 	combobox = GTK_COMBO_BOX_TEXT(gtk_builder_get_object(schemawin_xml, "table_combo"));
196 	gtk_combo_box_text_append_text(combobox, ALL_TABLES);
197 	/* add all user tables in catalog to list */
198 	for (i=0; i < mdb->num_catalog; i++) {
199 		entry = g_ptr_array_index (mdb->catalog, i);
200 		if (mdb_is_user_table(entry)) {
201 			gtk_combo_box_text_append_text(combobox, entry->object_name);
202 		}
203 	} /* for */
204 	gtk_combo_box_set_active(GTK_COMBO_BOX(combobox), 0);
205 
206 	check_default_options();
207 	refresh_available_options();
208 }
209