File: main.c

package info (click to toggle)
libgda5 5.2.10-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 76,168 kB
  • sloc: ansic: 495,319; xml: 10,486; yacc: 5,165; sh: 4,451; makefile: 4,095; php: 1,416; java: 1,300; javascript: 1,298; python: 896; sql: 879; perl: 116
file content (266 lines) | stat: -rw-r--r-- 8,008 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
 * Copyright (C) 2009 - 2011 Vivien Malerba <malerba@gnome-db.org>
 * Copyright (C) 2010 David King <davidk@openismus.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include <unistd.h>
#include <glib/gi18n-lib.h>
#include <glib/gprintf.h>
#include <string.h>
#include <libgda-ui/libgda-ui.h>
#include "support.h"
#include "browser-core.h"
#include "browser-connection.h"
#include "browser-window.h"
#include "login-dialog.h"
#include "auth-dialog.h"
#include "browser-stock-icons.h"
#include "../config-info.h"

#ifdef HAVE_LOCALE_H
#include <locale.h>
#endif

/* Perspectives' factories */
#include "schema-browser/perspective-main.h"
#include "query-exec/perspective-main.h"
#include "data-manager/perspective-main.h"
#ifdef HAVE_LDAP
#include "ldap-browser/perspective-main.h"
#endif
/* #include "dummy-perspective/perspective-main.h" */

extern BrowserCoreInitFactories browser_core_init_factories;

GSList *
main_browser_core_init_factories (void)
{
	GSList *factories = NULL;
	factories = g_slist_append (factories, schema_browser_perspective_get_factory ());
	factories = g_slist_append (factories, query_exec_perspective_get_factory ());
	factories = g_slist_append (factories, data_manager_perspective_get_factory ());
#ifdef HAVE_LDAP
	factories = g_slist_append (factories, ldap_browser_perspective_get_factory ());
#endif
	/* factories = g_slist_append (factories, dummy_perspective_get_factory ()); */
	return factories;
}

static void
output_data_model (GdaDataModel *model)
{
	if (! getenv ("GDA_DATA_MODEL_DUMP_TITLE"))
		g_setenv ("GDA_DATA_MODEL_DUMP_TITLE", "Yes", TRUE);
	if (! getenv ("GDA_DATA_MODEL_NULL_AS_EMPTY"))
		g_setenv ("GDA_DATA_MODEL_NULL_AS_EMPTY", "Yes", TRUE);
	if (isatty (fileno (stdout))) {
		if (! getenv ("GDA_DATA_MODEL_DUMP_TRUNCATE"))
			g_setenv ("GDA_DATA_MODEL_DUMP_TRUNCATE", "-1", TRUE);
	}
	gda_data_model_dump (model, NULL);	
}


/* options */
gchar *perspective = NULL;
gboolean list_configs = FALSE;
gboolean list_providers = FALSE;
gboolean list_data_files = FALSE;
gchar *purge_data_files = NULL;

static GOptionEntry entries[] = {
        { "perspective", 'p', 0, G_OPTION_ARG_STRING, &perspective, "Perspective", "default perspective "
	  "to use when opening windows"},
        { "list-dsn", 'l', 0, G_OPTION_ARG_NONE, &list_configs, "List configured data sources and exit", NULL },
        { "list-providers", 'L', 0, G_OPTION_ARG_NONE, &list_providers, "List installed database providers and exit", NULL },
        { "data-files-list", 0, 0, G_OPTION_ARG_NONE, &list_data_files, "List files used to hold information related to each connection", NULL },
        { "data-files-purge", 0, 0, G_OPTION_ARG_STRING, &purge_data_files, "Remove some files used to hold information related to each connection", "criteria"},
	{ NULL, 0, 0, 0, NULL, NULL, NULL }
};

int
main (int argc, char *argv[])
{
	GOptionContext *context;
        GError *error = NULL;
	gboolean have_loop = FALSE;

	/* set factories function */
	browser_core_init_factories = main_browser_core_init_factories;

	context = g_option_context_new (_("[DSN|connection string]..."));
        g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
	g_option_context_add_group (context, gtk_get_option_group (TRUE));
        g_option_context_set_ignore_unknown_options (context, TRUE);
        if (!g_option_context_parse (context, &argc, &argv, &error)) {
                g_fprintf  (stderr, "Can't parse arguments: %s\n", error->message);
                return EXIT_FAILURE;
        }
        g_option_context_free (context);

	/* treat here lists of providers and defined DSN */
	if (list_providers) {
		gda_init ();
		setlocale (LC_ALL, "");
		GdaDataModel *model;
		if (argc == 2)
			model = config_info_detail_provider (argv[1], &error);
		else
			model = config_info_list_all_providers ();

		if (model) {
			output_data_model (model);
			g_object_unref (model);
		}
		else {
			g_print (_("Error: %s\n"), 
				 error && error->message ? error->message : _("No detail"));
			g_clear_error (&error);
		}
		return 0;
	}
	if (list_configs) {
		gda_init ();
		setlocale (LC_ALL, "");
		GdaDataModel *model;
		if (argc == 2)
			model = config_info_detail_dsn (argv[1], &error);
		else
			model = config_info_list_all_dsn ();
		if (model) {
			output_data_model (model);
			g_object_unref (model);
		}
		else {
			g_print (_("Error: %s\n"),
				 error && error->message ? error->message : _("No detail"));
			g_clear_error (&error);
		}
		return 0;
	}
	if (list_data_files) {
		gchar *confdir;
		GdaDataModel *model;

		gda_init ();
		setlocale (LC_ALL, "");
		confdir = config_info_compute_dict_directory ();
		g_print (_("All files are in the directory: %s\n"), confdir);
		g_free (confdir);
		model = config_info_list_data_files (&error);
		if (model) {
			output_data_model (model);
			g_object_unref (model);
		}
		else
			g_print (_("Can't get the list of files used to store information about each connection: %s\n"),
				 error->message);	
		return 0;
	}
	if (purge_data_files) {
		gchar *tmp;

		gda_init ();
		setlocale (LC_ALL, "");
		tmp = config_info_purge_data_files (purge_data_files, &error);
		if (tmp) {
			g_print ("%s\n", tmp);
			g_free (tmp);
		}
		if (error)
			g_print (_("Error while purging files used to store information about each connection: %s\n"),
				 error->message);	
		return 0;
	}

	gtk_init (&argc, &argv);
	gdaui_init ();

#ifdef HAVE_MAC_INTEGRATION
	theApp = g_object_new (GTK_TYPE_OSX_APPLICATION, NULL);
#endif
	browser_stock_icons_init ();

	browser_core_set_default_factory (perspective);

	if (argc == 1) {
		GError *error = NULL;
		if (browser_connection_open (&error))
			have_loop = TRUE;
		else
			g_print ("Connection NOT opened: %s\n",
				 error && error->message ? error->message : _("No detail"));
	}
	else {
		AuthDialog *dialog;
		GError *error = NULL;
		gint i;

		dialog = auth_dialog_new (NULL);
		
		for (i = 1; i < argc; i++) {
			if (! auth_dialog_add_cnc_string (dialog, argv[i], &error)) {
				gtk_widget_destroy (GTK_WIDGET (dialog));
				dialog = NULL;
				g_print ("Connection NOT opened: %s\n",
					 error && error->message ? error->message : _("No detail"));
				break;
			}
			config_info_modify_argv (argv[i]);
		}
		
		if (dialog) {
			have_loop = auth_dialog_run (dialog);
			if (have_loop) {
				/* create a window for each opened connection */
				const GSList *cnclist, *list;

				cnclist = auth_dialog_get_connections (dialog);
				for (list = cnclist; list; list = list->next) {
					AuthDialogConnection *ad = (AuthDialogConnection*) list->data;
					if (ad->cnc) {
						BrowserConnection *bcnc;
						BrowserWindow *bwin;
						bcnc = browser_connection_new (ad->cnc);
						bwin = browser_window_new (bcnc, NULL);
						
						browser_core_take_window (bwin);
						browser_core_take_connection (bcnc);
					}
					else {
						g_print ("Connection NOT opened: %s\n",
							 ad->cnc_open_error && ad->cnc_open_error->message ? 
							 ad->cnc_open_error->message : _("No detail"));
						have_loop = FALSE;
						break;
					}
				}
			}
			gtk_widget_destroy ((GtkWidget*) dialog);
		}
	}
	
	/*g_print ("Main (GTK+) THREAD is %p\n", g_thread_self ());*/
	if (have_loop)
		/* application loop */
		gtk_main ();
	else if (browser_core_exists ())
		g_object_unref (browser_core_get ());

	return 0;
}