gedit.c 7.03 KB
Newer Older
Paolo Borelli's avatar
Paolo Borelli committed
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
/*
 * gedit.c
 * This file is part of gedit
 *
 * Copyright (C) 2005 - Paolo Maggi 
 *
 * 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., 59 Temple Place, Suite 330, 
 * Boston, MA 02111-1307, USA.
 */

/*
 * Modified by the gedit Team, 2005. See the AUTHORS file for a 
 * list of people on the gedit Team.  
 * See the ChangeLog files for a list of changes. 
 *
 * $Id$
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <locale.h>

37
#include <glib.h>
Paolo Borelli's avatar
Paolo Borelli committed
38
#include <glib/gi18n.h>
39

40 41
#include "gedit-command-line.h"
#include "gedit-dbus.h"
Paolo Borelli's avatar
Paolo Borelli committed
42 43

#include "gedit-app.h"
44 45
#include "gedit-encodings.h"

Paolo Borelli's avatar
Paolo Borelli committed
46 47
#include "gedit-commands.h"
#include "gedit-debug.h"
48
#include "gedit-dirs.h"
Paolo Borelli's avatar
Paolo Borelli committed
49 50 51 52 53 54
#include "gedit-encodings.h"
#include "gedit-plugins-engine.h"
#include "gedit-session.h"
#include "gedit-utils.h"
#include "gedit-window.h"

55 56
#ifndef ENABLE_GVFS_METADATA
#include "gedit-metadata-manager.h"
57
#define METADATA_FILE "gedit-metadata.xml"
58 59
#endif

60 61 62
#ifdef G_OS_UNIX
#include <gio/gunixinputstream.h>
#include <unistd.h>
63
#endif
Paolo Borelli's avatar
Paolo Borelli committed
64

65
static gboolean
66
gedit_main_load_from_stdin (GeditWindow *window,
Garrett Regier's avatar
Garrett Regier committed
67
                            gboolean     jump_to)
68
{
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
#ifdef G_OS_UNIX
	GInputStream *stream;
	const GeditEncoding *encoding;
	gint line_position;
	gint column_position;
	GeditCommandLine *command_line;

	command_line = gedit_command_line_get_default ();

	encoding = gedit_command_line_get_encoding (command_line);
	line_position = gedit_command_line_get_line_position (command_line);
	column_position = gedit_command_line_get_column_position (command_line);

	/* Construct a stream for stdin */
	stream = g_unix_input_stream_new (STDIN_FILENO, TRUE);

	gedit_window_create_tab_from_stream (window,
	                                     stream,
	                                     encoding,
	                                     line_position,
	                                     column_position,
	                                     jump_to);
	g_object_unref (stream);
92 93 94
	return TRUE;
#else
	return FALSE;
95
#endif
96 97 98
}

static void
99
gedit_main_window (void)
Paolo Borelli's avatar
Paolo Borelli committed
100
{
101 102 103 104 105 106
	GSList *file_list;
	GeditWindow *window;
	GeditCommandLine *command_line;
	GeditApp *app;
	gboolean doc_created = FALSE;
	const gchar *geometry;
Paolo Borelli's avatar
Paolo Borelli committed
107

108
	app = gedit_app_get_default ();
109

110 111
	gedit_debug_message (DEBUG_APP, "Create main window");
	window = gedit_app_create_window (app, NULL);
112

113 114
	command_line = gedit_command_line_get_default ();
	file_list = gedit_command_line_get_file_list (command_line);
115

116
	if (file_list != NULL)
117
	{
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
		GSList *loaded;
		const GeditEncoding *encoding;
		gint line_position;
		gint column_position;

		encoding = gedit_command_line_get_encoding (command_line);
		line_position = gedit_command_line_get_line_position (command_line);
		column_position = gedit_command_line_get_column_position (command_line);

		gedit_debug_message (DEBUG_APP, "Load files");
		loaded = _gedit_cmd_load_files_from_prompt (window,
		                                            file_list,
		                                            encoding,
		                                            line_position,
		                                            column_position);

		doc_created = loaded != NULL;
		g_slist_free (loaded);
136 137
	}

138
	if (gedit_utils_can_read_from_stdin ())
Paolo Borelli's avatar
Paolo Borelli committed
139
	{
140
		doc_created = gedit_main_load_from_stdin (window, !doc_created);
Paolo Borelli's avatar
Paolo Borelli committed
141
	}
142 143

	if (!doc_created || gedit_command_line_get_new_document (command_line))
144
	{
145 146
		gedit_debug_message (DEBUG_APP, "Create tab");
		gedit_window_create_tab (window, TRUE);
147
	}
Paolo Borelli's avatar
Paolo Borelli committed
148

149
	geometry = gedit_command_line_get_geometry (command_line);
150

151 152
	gedit_debug_message (DEBUG_APP, "Show window");
	gtk_widget_show (GTK_WIDGET (window));
153

154
	if (geometry)
155
	{
156 157
		gtk_window_parse_geometry (GTK_WINDOW (window),
		                           geometry);
158 159 160
	}
}

Paolo Borelli's avatar
Paolo Borelli committed
161
static void
162
gedit_main (gboolean service)
Paolo Borelli's avatar
Paolo Borelli committed
163
{
164
	GeditPluginsEngine *engine;
Paolo Borelli's avatar
Paolo Borelli committed
165
	GeditApp *app;
166
	const gchar *dir;
167
	gchar *icon_dir;
168

169
	gedit_debug_message (DEBUG_APP, "Set icon");
170

171 172
	dir = gedit_dirs_get_gedit_data_dir ();
	icon_dir = g_build_filename (dir, "icons", NULL);
173

174 175
	gtk_icon_theme_append_search_path (gtk_icon_theme_get_default (), icon_dir);
	g_free (icon_dir);
Paolo Borelli's avatar
Paolo Borelli committed
176

177 178 179
	/* Init plugins engine */
	gedit_debug_message (DEBUG_APP, "Init plugins");
	engine = gedit_plugins_engine_get_default ();
Paolo Borelli's avatar
Paolo Borelli committed
180 181 182

	app = gedit_app_get_default ();

183 184 185
	/* Initialize session management */
	gedit_debug_message (DEBUG_APP, "Init session manager");
	gedit_session_init ();
Paolo Borelli's avatar
Paolo Borelli committed
186

Paolo Borelli's avatar
Paolo Borelli committed
187
	if (!service)
Paolo Borelli's avatar
Paolo Borelli committed
188
	{
Paolo Borelli's avatar
Paolo Borelli committed
189
		gboolean restored = FALSE;
Paolo Borelli's avatar
Paolo Borelli committed
190

Paolo Borelli's avatar
Paolo Borelli committed
191 192 193 194 195 196 197 198 199
		if (gedit_session_is_restored ())
		{
			restored = gedit_session_load ();
		}

		if (!restored)
		{
			gedit_main_window ();
		}
Paolo Borelli's avatar
Paolo Borelli committed
200 201
	}

202 203
	gedit_debug_message (DEBUG_APP, "Start gtk-main");
	gtk_main ();
204

Paolo Borelli's avatar
Paolo Borelli committed
205 206 207
	/* Make sure settings are saved */
	g_settings_sync ();

208 209 210
	/* Cleanup */
	g_object_unref (engine);
	g_object_unref (app);
211

212 213
	gedit_dirs_shutdown ();

214 215
#ifndef ENABLE_GVFS_METADATA
	gedit_metadata_manager_shutdown ();
216
#endif
217
}
Paolo Borelli's avatar
Paolo Borelli committed
218 219 220 221

int
main (int argc, char *argv[])
{
222
	const gchar *dir;
223 224 225 226 227
	GeditCommandLine *command_line;
	gboolean ret;
	GeditDBus *dbus;
	GeditDBusResult dbusret;
	gboolean service = FALSE;
Paolo Borelli's avatar
Paolo Borelli committed
228

229 230 231 232 233
#ifndef ENABLE_GVFS_METADATA
	const gchar *cache_dir;
	gchar *metadata_filename;
#endif

234 235 236
	/* Init type system as soon as possible */
	g_type_init ();

237 238 239
	/* Init glib threads asap */
	g_thread_init (NULL);

Paolo Borelli's avatar
Paolo Borelli committed
240 241 242
	/* Setup debugging */
	gedit_debug_init ();
	gedit_debug_message (DEBUG_APP, "Startup");
243

244
	/* Setup locale/gettext */
Paolo Borelli's avatar
Paolo Borelli committed
245 246
	setlocale (LC_ALL, "");

247 248
	gedit_dirs_init ();

249 250
	dir = gedit_dirs_get_gedit_locale_dir ();
	bindtextdomain (GETTEXT_PACKAGE, dir);
251

252
	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
Paolo Borelli's avatar
Paolo Borelli committed
253 254
	textdomain (GETTEXT_PACKAGE);

255 256 257 258 259 260 261 262 263 264 265
#ifndef ENABLE_GVFS_METADATA
	/* Setup metadata-manager */
	cache_dir = gedit_dirs_get_user_cache_dir ();

	metadata_filename = g_build_filename (cache_dir, METADATA_FILE, NULL);

	gedit_metadata_manager_init (metadata_filename);

	g_free (metadata_filename);
#endif

266 267
	/* Parse command line arguments */
	command_line = gedit_command_line_get_default ();
268

269
	ret = gedit_command_line_parse (command_line, &argc, &argv);
270

271
	if (!ret)
272
	{
273
		g_object_unref (command_line);
274 275
		return 1;
	}
Paolo Borelli's avatar
Paolo Borelli committed
276

277 278 279
	/* Run over dbus */
	dbus = gedit_dbus_new ();
	dbusret = gedit_dbus_run (dbus);
Paolo Borelli's avatar
Paolo Borelli committed
280

281
	switch (dbusret)
282
	{
283 284 285 286 287 288 289 290 291 292 293 294
		case GEDIT_DBUS_RESULT_SUCCESS:
		case GEDIT_DBUS_RESULT_FAILED: /* fallthrough */
			g_object_unref (command_line);
			g_object_unref (dbus);

			return dbusret == GEDIT_DBUS_RESULT_SUCCESS ? 0 : 1;
		break;
		case GEDIT_DBUS_RESULT_PROCEED_SERVICE:
			service = TRUE;
		break;
		case GEDIT_DBUS_RESULT_PROCEED:
		break;
295
	}
296

297
	gedit_main (service);
Paolo Borelli's avatar
Paolo Borelli committed
298

299 300
	g_object_unref (dbus);
	g_object_unref (command_line);
Paolo Borelli's avatar
Paolo Borelli committed
301 302 303

	return 0;
}
304

305
/* ex:set ts=8 noet: */