[gedit/dbus2: 1/9] Prepare and cleanup command line parsing for dbus



commit 307b6f3afc7c63cbd8e30ed7258d2cd5ba167f5b
Author: Jesse van den Kieboom <jessevdk gnome org>
Date:   Sun May 2 13:02:27 2010 +0200

    Prepare and cleanup command line parsing for dbus

 configure.ac                    |    1 +
 data/Makefile.am                |    9 +
 data/org.gnome.gedit.service.in |    3 +
 gedit/Makefile.am               |    9 +
 gedit/gedit-command-line.c      |  334 +++++++++++++++++
 gedit/gedit-command-line.h      |   70 ++++
 gedit/gedit-dbus.c              |   29 ++
 gedit/gedit-dbus.h              |   38 ++
 gedit/gedit-win32.c             |   62 ++++
 gedit/gedit-win32.h             |   48 +++
 gedit/gedit.c                   |  756 +++++----------------------------------
 gedit/osx/gedit-osx.c           |   33 ++-
 gedit/osx/gedit-osx.h           |   34 ++-
 13 files changed, 761 insertions(+), 665 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 4e95a33..30c0e29 100644
--- a/configure.ac
+++ b/configure.ac
@@ -275,6 +275,7 @@ PKG_CHECK_MODULES(GEDIT, [
 	gtk+-2.0 >= 2.16.0
 	gtksourceview-2.0 >= 2.9.7
 	gconf-2.0 >= 1.1.11
+	gdbus-standalone
 ])
 
 if test "$os_osx" = "no" &&
diff --git a/data/Makefile.am b/data/Makefile.am
index 669f1dd..8ff7646 100644
--- a/data/Makefile.am
+++ b/data/Makefile.am
@@ -8,6 +8,13 @@ schemas_in_files = gedit.schemas.in
 schemas_DATA 	 = $(schemas_in_files:.schemas.in=.schemas)
 @INTLTOOL_SCHEMAS_RULE@
 
+servicedir = $(datadir)/dbus-1/services
+service_in_files = org.gnome.gedit.service.in
+service_DATA = $(service_in_files:.service.in=.service)
+
+$(service_DATA): $(service_in_files) Makefile
+	@sed -e "s|\ bindir\@|$(bindir)|" $<> $@
+
 man_MANS = gedit.1
 
 pkgconfigdir = $(libdir)/pkgconfig
@@ -33,6 +40,7 @@ endif
 EXTRA_DIST = 				\
 	$(desktop_in_files)		\
 	$(schemas_in_files)		\
+	$(service_in_files)		\
 	gedit.schemas.in.in		\
 	$(man_MANS)			\
 	gedit.pc.in			\
@@ -41,6 +49,7 @@ EXTRA_DIST = 				\
 CLEANFILES =	 			\
 	$(desktop_DATA)			\
 	$(schemas_DATA)			\
+	$(service_DATA)			\
 	$(pkgconfig_DATA)
 
 
diff --git a/data/org.gnome.gedit.service.in b/data/org.gnome.gedit.service.in
new file mode 100644
index 0000000..9e6d0af
--- /dev/null
+++ b/data/org.gnome.gedit.service.in
@@ -0,0 +1,3 @@
+[D-BUS Service]
+Name=org.gnome.gedit
+Exec= bindir@/gedit
diff --git a/gedit/Makefile.am b/gedit/Makefile.am
index 5a2da38..5659268 100644
--- a/gedit/Makefile.am
+++ b/gedit/Makefile.am
@@ -64,6 +64,8 @@ endif
 
 NOINST_H_FILES =			\
 	gedit-close-button.h		\
+	gedit-command-line.h		\
+	gedit-dbus.h			\
 	gedit-dirs.h			\
 	gedit-document-input-stream.h	\
 	gedit-document-loader.h		\
@@ -89,6 +91,7 @@ NOINST_H_FILES =			\
 	gedittextregion.h		\
 	gedit-ui.h			\
 	gedit-window-private.h		\
+	gedit-win32.h			\
 	gseal-gtk-compat.h
 
 INST_H_FILES =				\
@@ -132,6 +135,7 @@ libgedit_la_SOURCES = 			\
 	$(POSIXIO_FILES)		\
 	gedit-app.c			\
 	gedit-close-button.c		\
+	gedit-command-line.c		\
 	gedit-commands-documents.c	\
 	gedit-commands-edit.c		\
 	gedit-commands-file.c		\
@@ -139,6 +143,7 @@ libgedit_la_SOURCES = 			\
 	gedit-commands-help.c		\
 	gedit-commands-search.c		\
 	gedit-commands-view.c		\
+	gedit-dbus.c			\
 	gedit-debug.c			\
 	gedit-dirs.c			\
 	gedit-document.c 		\
@@ -189,6 +194,10 @@ if !ENABLE_GVFS_METADATA
 libgedit_la_SOURCES += gedit-metadata-manager.c 
 endif
 
+if PLATFORM_OSX
+libgedit_la_SOURCES += gedit-win32.c
+endif
+
 gedit-enum-types.h: gedit-enum-types.h.template $(INST_H_FILES) $(GLIB_MKENUMS)
 	$(AM_V_GEN) (cd $(srcdir) && $(GLIB_MKENUMS) --template gedit-enum-types.h.template $(INST_H_FILES)) > $@
 
diff --git a/gedit/gedit-command-line.c b/gedit/gedit-command-line.c
new file mode 100644
index 0000000..d9d16f4
--- /dev/null
+++ b/gedit/gedit-command-line.c
@@ -0,0 +1,334 @@
+/*
+ * gedit-command-line.c
+ * This file is part of gedit
+ *
+ * Copyright (C) 2010 - Jesse van den Kieboom
+ *
+ * gedit 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.
+ *
+ * gedit 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 gedit; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA  02110-1301  USA
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <glib/gi18n.h>
+#include <gio/gio.h>
+#include <gtk/gtk.h>
+
+#include "gedit-command-line.h"
+#include "eggsmclient.h"
+
+#define GEDIT_COMMAND_LINE_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE((object), GEDIT_TYPE_COMMAND_LINE, GeditCommandLinePrivate))
+
+struct _GeditCommandLinePrivate
+{
+	/* These are directly set as options */
+	gchar *line_column_position;
+	gchar *encoding_charset;
+	gboolean new_window;
+	gboolean new_document;
+	gchar **remaining_args;
+
+	/* This result from post-processing command line arguments */
+	gint line_position;
+	gint column_position;
+	GSList *file_list;
+	const GeditEncoding *encoding;
+};
+
+G_DEFINE_TYPE (GeditCommandLine, gedit_command_line, G_TYPE_OBJECT)
+
+static void
+gedit_command_line_finalize (GObject *object)
+{
+	GeditCommandLine *command_line = GEDIT_COMMAND_LINE (object);
+
+	g_free (command_line->priv->encoding_charset);
+	g_free (command_line->priv->line_column_position);
+	g_strfreev (command_line->priv->remaining_args);
+
+	g_slist_foreach (command_line->priv->file_list, (GFunc)g_object_unref, NULL);
+	g_slist_free (command_line->priv->file_list);
+
+	G_OBJECT_CLASS (gedit_command_line_parent_class)->finalize (object);
+}
+
+static void
+gedit_command_line_class_init (GeditCommandLineClass *klass)
+{
+	GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+	object_class->finalize = gedit_command_line_finalize;
+
+	g_type_class_add_private (object_class, sizeof(GeditCommandLinePrivate));
+}
+
+static void
+gedit_command_line_init (GeditCommandLine *self)
+{
+	self->priv = GEDIT_COMMAND_LINE_GET_PRIVATE (self);
+}
+
+GeditCommandLine *
+gedit_command_line_new ()
+{
+	return g_object_new (GEDIT_TYPE_COMMAND_LINE, NULL);
+}
+
+static void
+show_version_and_quit (void)
+{
+	g_print ("%s - Version %s\n", g_get_application_name (), VERSION);
+
+	exit (0);
+}
+
+static void
+list_encodings_and_quit (void)
+{
+	gint i = 0;
+	const GeditEncoding *enc;
+
+	while ((enc = gedit_encoding_get_from_index (i)) != NULL)
+	{
+		g_print ("%s\n", gedit_encoding_get_charset (enc));
+
+		++i;
+	}
+
+	exit (0);
+}
+
+static void
+get_line_column_position (GeditCommandLine *command_line,
+                          const gchar      *arg)
+{
+	gchar **split;
+
+	split = g_strsplit (arg, ":", 2);
+
+	if (split != NULL)
+	{
+		if (split[0] != NULL)
+		{
+			command_line->priv->line_position = atoi (split[0]);
+		}
+
+		if (split[1] != NULL)
+		{
+			command_line->priv->column_position = atoi (split[1]);
+		}
+	}
+
+	g_strfreev (split);
+}
+
+static void
+process_remaining_arguments (GeditCommandLine *command_line)
+{
+	gint i;
+
+	if (!command_line->priv->remaining_args)
+	{
+		return;
+	}
+
+	for (i = 0; command_line->priv->remaining_args[i]; i++)
+	{
+		if (*command_line->priv->remaining_args[i] == '+')
+		{
+			if (*(command_line->priv->remaining_args[i] + 1) == '\0')
+			{
+				/* goto the last line of the document */
+				command_line->priv->line_position = G_MAXINT;
+				command_line->priv->column_position = 0;
+			}
+			else
+			{
+				get_line_column_position (command_line, command_line->priv->remaining_args[i] + 1);
+			}
+		}
+		else
+		{
+			GFile *file;
+
+			file = g_file_new_for_commandline_arg (command_line->priv->remaining_args[i]);
+			command_line->priv->file_list = g_slist_prepend (command_line->priv->file_list, file);
+		}
+	}
+
+	command_line->priv->file_list = g_slist_reverse (command_line->priv->file_list);
+}
+
+static void
+process_command_line (GeditCommandLine *command_line)
+{
+	/* Parse encoding */
+	if (command_line->priv->encoding_charset)
+	{
+		command_line->priv->encoding = gedit_encoding_get_from_charset (command_line->priv->encoding_charset);
+
+		if (command_line->priv->encoding == NULL)
+		{
+			g_print (_("%s: invalid encoding.\n"), command_line->priv->encoding_charset);
+		}
+
+		g_free (command_line->priv->encoding_charset);
+		command_line->priv->encoding_charset = NULL;
+	}
+
+	/* Parse remaining arguments */
+	process_remaining_arguments (command_line);
+}
+
+gboolean
+gedit_command_line_parse (GeditCommandLine   *command_line,
+                          int                *argc,
+                          char             ***argv)
+{
+	GOptionContext *context;
+	GError *error = NULL;
+
+	const GOptionEntry options[] =
+	{
+		/* Version */
+		{
+			"version", 'V', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
+			show_version_and_quit, N_("Show the application's version"), NULL
+		},
+
+		/* List available encodings */
+		{
+			"list-encodings", '\0', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
+			list_encodings_and_quit, N_("Display list of possible values for the encoding option"),
+			NULL
+		},
+
+		/* Encoding */
+		{
+			"encoding", '\0', 0, G_OPTION_ARG_STRING, &command_line->priv->encoding_charset,
+			N_("Set the character encoding to be used to open the files listed on the command line"),
+			N_("ENCODING")
+		},
+
+		/* Open a new window */
+		{
+			"new-window", '\0', 0, G_OPTION_ARG_NONE,
+			&command_line->priv->new_window,
+			N_("Create a new top-level window in an existing instance of gedit"),
+			NULL
+		},
+
+		/* Create a new empty document */
+		{
+			"new-document", '\0', 0, G_OPTION_ARG_NONE,
+			&command_line->priv->new_document,
+			N_("Create a new document in an existing instance of gedit"),
+			NULL
+		},
+
+		/* Goto line/column */
+		{
+			"+LINE[:COLUMN]", '\0', 0, G_OPTION_ARG_NONE,
+			&command_line->priv->line_column_position,
+			N_("Move cursor to LINE, COLUMN"),
+			NULL
+		},
+
+		/* collects file arguments */
+		{
+			G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_FILENAME_ARRAY,
+			&command_line->priv->remaining_args,
+			NULL,
+			N_("[FILE...]")
+		},
+
+		{NULL}
+	};
+
+	/* Setup command line options */
+	context = g_option_context_new (_("- Edit text files"));
+	g_option_context_add_main_entries (context, options, GETTEXT_PACKAGE);
+	g_option_context_add_group (context, gtk_get_option_group (FALSE));
+	g_option_context_add_group (context, egg_sm_client_get_option_group ());
+
+	gtk_init (argc, argv);
+
+	if (!g_option_context_parse (context, argc, argv, &error))
+	{
+		g_print(_("%s\nRun '%s --help' to see a full list of available command line options.\n"),
+			error->message, (*argv)[0]);
+
+		g_error_free (error);
+		return FALSE;
+	}
+
+	g_option_context_free (context);
+
+	/* Do some post-processing */
+	process_command_line (command_line);
+
+	return TRUE;
+}
+
+gboolean
+gedit_command_line_get_new_window (GeditCommandLine *command_line)
+{
+	g_return_val_if_fail (GEDIT_IS_COMMAND_LINE (command_line), FALSE);
+	return command_line->priv->new_window;
+}
+
+gboolean
+gedit_command_line_get_new_document (GeditCommandLine *command_line)
+{
+	g_return_val_if_fail (GEDIT_IS_COMMAND_LINE (command_line), FALSE);
+	return command_line->priv->new_document;
+}
+
+gint
+gedit_command_line_get_line_position (GeditCommandLine *command_line)
+{
+	g_return_val_if_fail (GEDIT_IS_COMMAND_LINE (command_line), 0);
+	return command_line->priv->line_position;
+}
+
+gint
+gedit_command_line_get_column_position (GeditCommandLine *command_line)
+{
+	g_return_val_if_fail (GEDIT_IS_COMMAND_LINE (command_line), 0);
+	return command_line->priv->column_position;
+}
+
+GSList *
+gedit_command_line_get_file_list (GeditCommandLine *command_line)
+{
+	g_return_val_if_fail (GEDIT_IS_COMMAND_LINE (command_line), NULL);
+	return command_line->priv->file_list;
+}
+
+const GeditEncoding *
+gedit_command_line_get_encoding (GeditCommandLine *command_line)
+{
+	g_return_val_if_fail (GEDIT_IS_COMMAND_LINE (command_line), NULL);
+	return command_line->priv->encoding;
+}
+
+
+/* ex:ts=8:noet: */
diff --git a/gedit/gedit-command-line.h b/gedit/gedit-command-line.h
new file mode 100644
index 0000000..cb277fa
--- /dev/null
+++ b/gedit/gedit-command-line.h
@@ -0,0 +1,70 @@
+/*
+ * gedit-command-line.h
+ * This file is part of gedit
+ *
+ * Copyright (C) 2010 - Jesse van den Kieboom
+ *
+ * gedit 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.
+ *
+ * gedit 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 gedit; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA  02110-1301  USA
+ */
+
+#ifndef __GEDIT_COMMAND_LINE_H__
+#define __GEDIT_COMMAND_LINE_H__
+
+#include <glib-object.h>
+#include "gedit-encodings.h"
+
+G_BEGIN_DECLS
+
+#define GEDIT_TYPE_COMMAND_LINE			(gedit_command_line_get_type ())
+#define GEDIT_COMMAND_LINE(obj)			(G_TYPE_CHECK_INSTANCE_CAST ((obj), GEDIT_TYPE_COMMAND_LINE, GeditCommandLine))
+#define GEDIT_COMMAND_LINE_CONST(obj)		(G_TYPE_CHECK_INSTANCE_CAST ((obj), GEDIT_TYPE_COMMAND_LINE, GeditCommandLine const))
+#define GEDIT_COMMAND_LINE_CLASS(klass)		(G_TYPE_CHECK_CLASS_CAST ((klass), GEDIT_TYPE_COMMAND_LINE, GeditCommandLineClass))
+#define GEDIT_IS_COMMAND_LINE(obj)		(G_TYPE_CHECK_INSTANCE_TYPE ((obj), GEDIT_TYPE_COMMAND_LINE))
+#define GEDIT_IS_COMMAND_LINE_CLASS(klass)	(G_TYPE_CHECK_CLASS_TYPE ((klass), GEDIT_TYPE_COMMAND_LINE))
+#define GEDIT_COMMAND_LINE_GET_CLASS(obj)	(G_TYPE_INSTANCE_GET_CLASS ((obj), GEDIT_TYPE_COMMAND_LINE, GeditCommandLineClass))
+
+typedef struct _GeditCommandLine		GeditCommandLine;
+typedef struct _GeditCommandLineClass	GeditCommandLineClass;
+typedef struct _GeditCommandLinePrivate	GeditCommandLinePrivate;
+
+struct _GeditCommandLine {
+	GObject parent;
+
+	GeditCommandLinePrivate *priv;
+};
+
+struct _GeditCommandLineClass {
+	GObjectClass parent_class;
+};
+
+GType gedit_command_line_get_type (void) G_GNUC_CONST;
+GeditCommandLine *gedit_command_line_new (void);
+
+gboolean gedit_command_line_parse (GeditCommandLine *command_line, int *argc, char ***argv);
+
+gboolean gedit_command_line_get_new_window (GeditCommandLine *command_line);
+gboolean gedit_command_line_get_new_document (GeditCommandLine *command_line);
+gint gedit_command_line_get_line_position (GeditCommandLine *command_line);
+gint gedit_command_line_get_column_position (GeditCommandLine *command_line);
+
+GSList *gedit_command_line_get_file_list (GeditCommandLine *command_line);
+const GeditEncoding *gedit_command_line_get_encoding (GeditCommandLine *command_line);
+
+G_END_DECLS
+
+#endif /* __GEDIT_COMMAND_LINE_H__ */
+
+/* ex:ts=8:noet: */
diff --git a/gedit/gedit-dbus.c b/gedit/gedit-dbus.c
new file mode 100644
index 0000000..47af4b5
--- /dev/null
+++ b/gedit/gedit-dbus.c
@@ -0,0 +1,29 @@
+/*
+ * gedit-dbus.c
+ * This file is part of gedit
+ *
+ * Copyright (C) 2010 - Jesse van den Kieboom
+ *
+ * gedit 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.
+ *
+ * gedit 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 gedit; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, 
+ * Boston, MA  02110-1301  USA
+ */
+
+#include "gedit-dbus.h"
+
+GeditDBusResult
+gedit_dbus_initialize (GeditCommandLine *command_line)
+{
+	return GEDIT_DBUS_RESULT_PROCEED;
+}
diff --git a/gedit/gedit-dbus.h b/gedit/gedit-dbus.h
new file mode 100644
index 0000000..fc38bcd
--- /dev/null
+++ b/gedit/gedit-dbus.h
@@ -0,0 +1,38 @@
+/*
+ * gedit-dbus.h
+ * This file is part of gedit
+ *
+ * Copyright (C) 2010 - Jesse van den Kieboom
+ *
+ * gedit 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.
+ *
+ * gedit 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 gedit; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA  02110-1301  USA
+ */
+
+#ifndef __GEDIT_DBUS_H__
+#define __GEDIT_DBUS_H__
+
+#include "gedit-command-line.h"
+
+typedef enum
+{
+	GEDIT_DBUS_RESULT_SUCCESS,
+	GEDIT_DBUS_RESULT_FAILED,
+	GEDIT_DBUS_RESULT_PROCEED
+} GeditDBusResult;
+
+GeditDBusResult gedit_dbus_initialize (GeditCommandLine *command_line);
+
+#endif /* __GEDIT_DBUS_H__ */
+
diff --git a/gedit/gedit-win32.c b/gedit/gedit-win32.c
new file mode 100644
index 0000000..1218b08
--- /dev/null
+++ b/gedit/gedit-win32.c
@@ -0,0 +1,62 @@
+#include <glib.h>
+#include "gedit-win32.h"
+
+static void
+setup_path (void)
+{
+	gchar *path;
+	gchar *installdir;
+	gchar *bin;
+
+	installdir = g_win32_get_package_installation_directory_of_module (NULL);
+
+	bin = g_build_filename (installdir, "bin", NULL);
+	g_free (installdir);
+
+	/* Set PATH to include the gedit executable's folder */
+	path = g_build_path (";", bin, g_getenv ("PATH"), NULL);
+	g_free (bin);
+
+	if (!g_setenv ("PATH", path, TRUE))
+	{
+		g_warning ("Could not set PATH for gedit");
+	}
+
+	g_free (path);
+}
+
+void
+gedit_win32_initialize (void)
+{
+	setup_path ();
+
+	/* If we open gedit from a console get the stdout printing */
+	if (fileno (stdout) != -1 &&
+		_get_osfhandle (fileno (stdout)) != -1)
+	{
+		/* stdout is fine, presumably redirected to a file or pipe */
+	}
+	else
+	{
+		typedef BOOL (* WINAPI AttachConsole_t) (DWORD);
+
+		AttachConsole_t p_AttachConsole =
+			(AttachConsole_t) GetProcAddress (GetModuleHandle ("kernel32.dll"),
+							  "AttachConsole");
+
+		if (p_AttachConsole != NULL && p_AttachConsole (ATTACH_PARENT_PROCESS))
+		{
+			freopen ("CONOUT$", "w", stdout);
+			dup2 (fileno (stdout), 1);
+			freopen ("CONOUT$", "w", stderr);
+			dup2 (fileno (stderr), 2);
+		}
+	}
+}
+
+void
+gedit_win32_prepare (GeditApp *app)
+{
+}
+
+/* ex:ts=8:noet: */
diff --git a/gedit/gedit-win32.h b/gedit/gedit-win32.h
new file mode 100644
index 0000000..e3f91e8
--- /dev/null
+++ b/gedit/gedit-win32.h
@@ -0,0 +1,48 @@
+/*
+ * gedit-win32.h
+ * This file is part of gedit
+ *
+ * Copyright (C) 2010 - Jesse van den Kieboom
+ *
+ * gedit 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.
+ *
+ * gedit 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 gedit; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA  02110-1301  USA
+ */
+
+#ifndef __GEDIT_WIN32_H__
+#define __GEDIT_WIN32_H__
+
+#include "gedit-app.h"
+
+#ifdef G_OS_WIN32
+void gedit_win32_initialize (void);
+void gedit_win32_prepare (GeditApp *app);
+
+#define SAVE_DATADIR DATADIR
+#undef DATADIR
+#include <io.h>
+#include <conio.h>
+#define _WIN32_WINNT 0x0500
+#include <windows.h>
+#define DATADIR SAVE_DATADIR
+#undef SAVE_DATADIR
+
+#else
+#define gedit_win32_initialize(...) ;
+#define gedit_win32_prepare(...) ;
+#endif
+
+#endif /* __GEDIT_WIN32_H__ */
+
+/* ex:ts=8:noet: */
diff --git a/gedit/gedit.c b/gedit/gedit.c
index 4956208..4f3ea9c 100644
--- a/gedit/gedit.c
+++ b/gedit/gedit.c
@@ -32,20 +32,24 @@
 #include <config.h>
 #endif
 
-#include <errno.h>
 #include <locale.h>
-#include <stdlib.h>
-#include <string.h>
 
 #include <glib.h>
 #include <glib/gi18n.h>
-#include <gtk/gtk.h>
 
-#ifdef GDK_WINDOWING_X11
-#include <gdk/gdkx.h>
-#endif
+#include "gedit-command-line.h"
+#include "gedit-dbus.h"
 
 #include "gedit-app.h"
+#include "gedit-encodings.h"
+
+#include "eggsmclient.h"
+#include "eggdesktopfile.h"
+
+#include "gedit-win32.h"
+#include "osx/gedit-osx.h"
+
+#include "gseal-gtk-compat.h"
 #include "gedit-commands.h"
 #include "gedit-debug.h"
 #include "gedit-dirs.h"
@@ -56,531 +60,105 @@
 #include "gedit-utils.h"
 #include "gedit-window.h"
 
-#include "eggsmclient.h"
-#include "eggdesktopfile.h"
-
-#include "gseal-gtk-compat.h"
-
-#ifdef G_OS_WIN32
-#define SAVE_DATADIR DATADIR
-#undef DATADIR
-#include <io.h>
-#include <conio.h>
-#define _WIN32_WINNT 0x0500
-#include <windows.h>
-#define DATADIR SAVE_DATADIR
-#undef SAVE_DATADIR
-#endif
-
-#ifdef OS_OSX
-#include <ige-mac-dock.h>
-#include <ige-mac-integration.h>
-#include "osx/gedit-osx.h"
-#endif
-
 #ifndef ENABLE_GVFS_METADATA
 #include "gedit-metadata-manager.h"
 #endif
 
-static guint32 startup_timestamp = 0;
-
-#ifndef G_OS_WIN32
-#include "bacon-message-connection.h"
-
-static BaconMessageConnection *connection;
-#endif
-
-/* command line */
-static gint line_position = 0;
-static gint column_position = 0;
-static gchar *encoding_charset = NULL;
-static gboolean new_window_option = FALSE;
-static gboolean new_document_option = FALSE;
-static gchar **remaining_args = NULL;
-static GSList *file_list = NULL;
-
-static void
-show_version_and_quit (void)
-{
-	g_print ("%s - Version %s\n", g_get_application_name (), VERSION);
-
-	exit (0);
-}
-
-static void
-list_encodings_and_quit (void)
-{
-	gint i = 0;
-	const GeditEncoding *enc;
-
-	while ((enc = gedit_encoding_get_from_index (i)) != NULL) 
-	{
-		g_print ("%s\n", gedit_encoding_get_charset (enc));
-
-		++i;
-	}
-
-	exit (0);
-}
-
-static const GOptionEntry options [] =
-{
-	{ "version", 'V', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
-	  show_version_and_quit, N_("Show the application's version"), NULL },
-
-	{ "encoding", '\0', 0, G_OPTION_ARG_STRING, &encoding_charset,
-	  N_("Set the character encoding to be used to open the files listed on the command line"), N_("ENCODING")},
-
-	{ "list-encodings", '\0', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
-	  list_encodings_and_quit, N_("Display list of possible values for the encoding option"), NULL},
-
-	{ "new-window", '\0', 0, G_OPTION_ARG_NONE, &new_window_option,
-	  N_("Create a new top-level window in an existing instance of gedit"), NULL },
-
-	{ "new-document", '\0', 0, G_OPTION_ARG_NONE, &new_document_option,
-	  N_("Create a new document in an existing instance of gedit"), NULL },
-
-	{ G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_FILENAME_ARRAY, &remaining_args,
-	  NULL, N_("[FILE...] [+LINE[:COLUMN]]") }, /* collects file arguments */
-
-	{NULL}
-};
-
 static void
-free_command_line_data (void)
-{
-	g_slist_foreach (file_list, (GFunc) g_object_unref, NULL);
-	g_slist_free (file_list);
-	file_list = NULL;
-
-	g_strfreev (remaining_args);
-	remaining_args = NULL;
-
-	g_free (encoding_charset);
-	encoding_charset = NULL;
-	
-	new_window_option = FALSE;
-	new_document_option = FALSE;
-	line_position = 0;
-	column_position = 0;
-}
-
-static void
-get_line_column_position (const gchar *arg)
-{
-	gchar **split;
-
-	split = g_strsplit (arg, ":", 2);
-
-	if (split != NULL)
-	{
-		if (split[0] != NULL)
-			line_position = atoi (split[0]);
-
-		if (split[1] != NULL)
-			column_position = atoi (split[1]);
-	}
-
-	g_strfreev (split);
-}
-
-static void
-gedit_get_command_line_data (void)
-{
-	if (remaining_args)
-	{
-		gint i;
-
-		for (i = 0; remaining_args[i]; i++) 
-		{
-			if (*remaining_args[i] == '+')
-			{
-				if (*(remaining_args[i] + 1) == '\0')
-				{
-					/* goto the last line of the document */
-					line_position = G_MAXINT;
-					column_position = 0;
-				}
-				else
-					get_line_column_position (remaining_args[i] + 1);
-			}
-			else
-			{
-				GFile *file;
-
-				file = g_file_new_for_commandline_arg (remaining_args[i]);
-				file_list = g_slist_prepend (file_list, file);
-			} 
-		}
-
-		file_list = g_slist_reverse (file_list);
-	}
-	
-	if (encoding_charset &&
-	    (gedit_encoding_get_from_charset (encoding_charset) == NULL))
-	{
-		g_print (_("%s: invalid encoding.\n"),
-			 encoding_charset);
-	}
-}
-
-static guint32
-get_startup_timestamp (void)
+gedit_main (GeditCommandLine *command_line)
 {
-	const gchar *startup_id_env;
-	gchar *startup_id = NULL;
-	gchar *time_str;
-	gchar *end;
-	gulong retval = 0;
-
-	/* we don't unset the env, since startup-notification
-	 * may still need it */
-	startup_id_env = g_getenv ("DESKTOP_STARTUP_ID");
-	if (startup_id_env == NULL)
-		goto out;
+	GeditPluginsEngine *engine;
+	GeditWindow *window;
+	GeditApp *app;
+	gboolean restored = FALSE;
+	gchar *dir;
+	gchar *icon_dir;
 
-	startup_id = g_strdup (startup_id_env);
+	gedit_debug_message (DEBUG_APP, "Set icon");
 
-	time_str = g_strrstr (startup_id, "_TIME");
-	if (time_str == NULL)
-		goto out;
+	dir = gedit_dirs_get_gedit_data_dir ();
+	icon_dir = g_build_filename (dir, "icons", NULL);
+	g_free (dir);
 
-	errno = 0;
+	gtk_icon_theme_append_search_path (gtk_icon_theme_get_default (), icon_dir);
+	g_free (icon_dir);
 
-	/* Skip past the "_TIME" part */
-	time_str += 5;
+#ifdef GDK_WINDOWING_X11
+	/* Set the associated .desktop file */
+	egg_set_desktop_file (DATADIR "/applications/gedit.desktop");
+#else
+	/* manually set name and icon */
+	g_set_application_name("gedit");
+	gtk_window_set_default_icon_name ("accessories-text-editor");
+#endif
 
-	retval = strtoul (time_str, &end, 0);
-	if (end == time_str || errno != 0)
-		retval = 0;
+	/* Init plugins engine */
+	gedit_debug_message (DEBUG_APP, "Init plugins");
+	engine = gedit_plugins_engine_get_default ();
 
- out:
-	g_free (startup_id);
+	gtk_about_dialog_set_url_hook (gedit_utils_activate_url, NULL, NULL);
 
-	return (retval > 0) ? retval : 0;
-}
+	/* Initialize session management */
+	gedit_debug_message (DEBUG_APP, "Init session manager");
+	gedit_session_init ();
 
-#ifndef G_OS_WIN32
-static GdkDisplay *
-display_open_if_needed (const gchar *name)
-{
-	GSList *displays;
-	GSList *l;
-	GdkDisplay *display = NULL;
+	app = gedit_app_get_default ();
 
-	displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
+	gedit_win32_prepare (app);
+	gedit_osx_prepare (app);
 
-	for (l = displays; l != NULL; l = l->next)
+	if (gedit_session_is_restored ())
 	{
-		if (strcmp (gdk_display_get_name ((GdkDisplay *) l->data), name) == 0)
-		{
-			display = l->data;
-			break;
-		}
+		restored = gedit_session_load ();
 	}
 
-	g_slist_free (displays);
-
-	return display != NULL ? display : gdk_display_open (name);
-}
-
-/* serverside */
-static void
-on_message_received (const char *message,
-		     gpointer    data)
-{
-	const GeditEncoding *encoding = NULL;
-	gchar **commands;
-	gchar **params;
-	gint workspace;
-	gint viewport_x;
-	gint viewport_y;
-	gchar *display_name;
-	gint screen_number;
-	gint i;
-	GeditApp *app;
-	GeditWindow *window;
-	GdkDisplay *display;
-	GdkScreen *screen;
-
-	g_return_if_fail (message != NULL);
-
-	gedit_debug_message (DEBUG_APP, "Received message:\n%s\n", message);
-
-	commands = g_strsplit (message, "\v", -1);
-
-	/* header */
-	params = g_strsplit (commands[0], "\t", 6);
-	startup_timestamp = atoi (params[0]);
-	display_name = params[1];
-	screen_number = atoi (params[2]);
-	workspace = atoi (params[3]);
-	viewport_x = atoi (params[4]);
-	viewport_y = atoi (params[5]);
-
-	display = display_open_if_needed (display_name);
-	if (display == NULL)
+	if (!restored)
 	{
-		g_warning ("Could not open display %s\n", display_name);
-		g_strfreev (params);
-		goto out;
-	}
-
-	screen = gdk_display_get_screen (display, screen_number);
+		GSList *file_list;
 
-	g_strfreev (params);
+		gedit_debug_message (DEBUG_APP, "Create main window");
+		window = gedit_app_create_window (app, NULL);
 
-	/* body */
-	for (i = 1; commands[i] != NULL; i++)
-	{
-		params = g_strsplit (commands[i], "\t", -1);
+		file_list = gedit_command_line_get_file_list (command_line);
 
-		if (strcmp (params[0], "NEW-WINDOW") == 0)
-		{
-			new_window_option = TRUE;
-		}
-		else if (strcmp (params[0], "NEW-DOCUMENT") == 0)
-		{
-			new_document_option = TRUE;
-		}
-		else if (strcmp (params[0], "OPEN-URIS") == 0)
+		if (file_list != NULL)
 		{
-			gint n_uris, j;
-			gchar **uris;
-
-			get_line_column_position (params[1]);
-
-			if (params[2] != '\0')
-				encoding = gedit_encoding_get_from_charset (params[2]);
-
-			n_uris = atoi (params[3]);
-			uris = g_strsplit (params[4], " ", n_uris);
-
-			for (j = 0; j < n_uris; j++)
-			{
-				GFile *file;
-
-				file = g_file_new_for_uri (uris[j]);
-				file_list = g_slist_prepend (file_list, file);
-			}
-
-			file_list = g_slist_reverse (file_list);
-
-			/* the list takes ownerhip of the strings,
-			 * only free the array */
-			g_free (uris);
+			gedit_debug_message (DEBUG_APP, "Load files");
+			_gedit_cmd_load_files_from_prompt (window,
+			                                   file_list,
+			                                   gedit_command_line_get_encoding (command_line),
+			                                   gedit_command_line_get_line_position (command_line),
+			                                   gedit_command_line_get_column_position (command_line));
 		}
 		else
 		{
-			g_warning ("Unexpected bacon command");
-		}
-
-		g_strfreev (params);
-	}
-
-	/* execute the commands */
-
-	app = gedit_app_get_default ();
-
-	if (new_window_option)
-	{
-		window = gedit_app_create_window (app, screen);
-	}
-	else
-	{
-		/* get a window in the current workspace (if exists) and raise it */
-		window = _gedit_app_get_window_in_viewport (app,
-							    screen,
-							    workspace,
-							    viewport_x,
-							    viewport_y);
-	}
-
-	if (file_list != NULL)
-	{
-		_gedit_cmd_load_files_from_prompt (window,
-						   file_list,
-						   encoding,
-						   line_position,
-						   column_position);
-
-		if (new_document_option)
+			gedit_debug_message (DEBUG_APP, "Create tab");
 			gedit_window_create_tab (window, TRUE);
-	}
-	else
-	{
-		GeditDocument *doc;
-		doc = gedit_window_get_active_document (window);
+		}
 
-		if (doc == NULL ||
-		    !gedit_document_is_untouched (doc) ||
-		    new_document_option)
-			gedit_window_create_tab (window, TRUE);
+		gedit_debug_message (DEBUG_APP, "Show window");
+		gtk_widget_show (GTK_WIDGET (window));
 	}
 
-	/* set the proper interaction time on the window.
-	 * Fall back to roundtripping to the X server when we
-	 * don't have the timestamp, e.g. when launched from
-	 * terminal. We also need to make sure that the window
-	 * has been realized otherwise it will not work. lame.
-	 */
-	if (!gtk_widget_get_realized (GTK_WIDGET (window)))
-		gtk_widget_realize (GTK_WIDGET (window));
+	gedit_debug_message (DEBUG_APP, "Start gtk-main");
+	gtk_main ();
 
-#ifdef GDK_WINDOWING_X11
-	if (startup_timestamp <= 0)
-		startup_timestamp = gdk_x11_get_server_time (gtk_widget_get_window (GTK_WIDGET (window)));
+	/* Cleanup */
+	g_object_unref (engine);
 
-	gdk_x11_window_set_user_time (gtk_widget_get_window (GTK_WIDGET (window)),
-				      startup_timestamp);
+#ifndef ENABLE_GVFS_METADATA
+	gedit_metadata_manager_shutdown ();
 #endif
-
-	gtk_window_present (GTK_WINDOW (window));
-
- out:
-	g_strfreev (commands);
-	
-	free_command_line_data ();
-}
-
-/* clientside */
-static void
-send_bacon_message (void)
-{
-	GdkScreen *screen;
-	GdkDisplay *display;
-	const gchar *display_name;
-	gint screen_number;
-	gint ws;
-	gint viewport_x;
-	gint viewport_y;
-	GString *command;
-
-	/* the messages have the following format:
-	 * <---                                  header                                     ---> <----            body             ----->
-	 * timestamp \t display_name \t screen_number \t workspace \t viewport_x \t viewport_y \v OP1 \t arg \t arg \v OP2 \t arg \t arg|...
-	 *
-	 * when the arg is a list of uri, they are separated by a space.
-	 * So the delimiters are \v for the commands, \t for the tokens in
-	 * a command and ' ' for the uris: note that such delimiters cannot
-	 * be part of an uri, this way parsing is easier.
-	 */
-
-	gedit_debug (DEBUG_APP);
-
-	screen = gdk_screen_get_default ();
-	display = gdk_screen_get_display (screen);
-
-	display_name = gdk_display_get_name (display);
-	screen_number = gdk_screen_get_number (screen);
-
-	gedit_debug_message (DEBUG_APP, "Display: %s", display_name);
-	gedit_debug_message (DEBUG_APP, "Screen: %d", screen_number);	
-
-	ws = gedit_utils_get_current_workspace (screen);
-	gedit_utils_get_current_viewport (screen, &viewport_x, &viewport_y);
-
-	command = g_string_new (NULL);
-
-	/* header */
-	g_string_append_printf (command,
-				"%" G_GUINT32_FORMAT "\t%s\t%d\t%d\t%d\t%d",
-				startup_timestamp,
-				display_name,
-				screen_number,
-				ws,
-				viewport_x,
-				viewport_y);
-
-	/* NEW-WINDOW command */
-	if (new_window_option)
-	{
-		command = g_string_append_c (command, '\v');
-		command = g_string_append (command, "NEW-WINDOW");
-	}
-
-	/* NEW-DOCUMENT command */
-	if (new_document_option)
-	{
-		command = g_string_append_c (command, '\v');
-		command = g_string_append (command, "NEW-DOCUMENT");
-	}
-
-	/* OPEN_URIS command, optionally specify line_num and encoding */
-	if (file_list)
-	{
-		GSList *l;
-
-		command = g_string_append_c (command, '\v');
-		command = g_string_append (command, "OPEN-URIS");
-
-		g_string_append_printf (command,
-					"\t%d:%d\t%s\t%u\t",
-					line_position, column_position,
-					encoding_charset ? encoding_charset : "",
-					g_slist_length (file_list));
-
-		for (l = file_list; l != NULL; l = l->next)
-		{
-			gchar *uri;
-
-			uri = g_file_get_uri (G_FILE (l->data));
-			command = g_string_append (command, uri);
-			if (l->next != NULL)
-				command = g_string_append_c (command, ' ');
-
-			g_free (uri);
-		}
-	}
-
-	gedit_debug_message (DEBUG_APP, "Bacon Message: %s", command->str);
-	
-	bacon_message_connection_send (connection,
-				       command->str);
-
-	g_string_free (command, TRUE);
 }
-#endif /* G_OS_WIN32 */
 
-#ifdef G_OS_WIN32
-static void
-setup_path (void)
-{
-	gchar *path;
-	gchar *installdir;
-	gchar *bin;
-
-	installdir = g_win32_get_package_installation_directory_of_module (NULL);
-
-	bin = g_build_filename (installdir,
-				"bin", NULL);
-	g_free (installdir);
-
-	/* Set PATH to include the gedit executable's folder */
-	path = g_build_path (";",
-			     bin,
-			     g_getenv ("PATH"),
-			     NULL);
-	g_free (bin);
-
-	if (!g_setenv ("PATH", path, TRUE))
-		g_warning ("Could not set PATH for gedit");
-
-	g_free (path);
-}
-#endif
 
 int
 main (int argc, char *argv[])
 {
-	GOptionContext *context;
-	GeditPluginsEngine *engine;
-	GeditWindow *window;
-	GeditApp *app;
-	gboolean restored = FALSE;
-	GError *error = NULL;
 	gchar *dir;
-	gchar *icon_dir;
+	GeditCommandLine *command_line;
+	gboolean ret;
+	GeditDBusResult dbusret;
 
 	/* Init type system as soon as possible */
 	g_type_init ();
@@ -591,202 +169,56 @@ main (int argc, char *argv[])
 	/* Setup debugging */
 	gedit_debug_init ();
 	gedit_debug_message (DEBUG_APP, "Startup");
-	
+
+	/* Setup locale/gettext */
 	setlocale (LC_ALL, "");
 
 	dir = gedit_dirs_get_gedit_locale_dir ();
 	bindtextdomain (GETTEXT_PACKAGE, dir);
 	g_free (dir);
+
 	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
 	textdomain (GETTEXT_PACKAGE);
 
-	startup_timestamp = get_startup_timestamp();
-
-	/* Setup command line options */
-	context = g_option_context_new (_("- Edit text files"));
-	g_option_context_add_main_entries (context, options, GETTEXT_PACKAGE);
-	g_option_context_add_group (context, gtk_get_option_group (FALSE));
-	g_option_context_add_group (context, egg_sm_client_get_option_group ());
-
-#ifdef G_OS_WIN32
-	setup_path ();
+	/* Initialize platforms */
+	gedit_win32_initialize ();
+	gedit_osx_initialize ();
 
-	/* If we open gedit from a console get the stdout printing */
-	if (fileno (stdout) != -1 &&
-		_get_osfhandle (fileno (stdout)) != -1)
-	{
-		/* stdout is fine, presumably redirected to a file or pipe */
-	}
-	else
-	{
-		typedef BOOL (* WINAPI AttachConsole_t) (DWORD);
-
-		AttachConsole_t p_AttachConsole =
-			(AttachConsole_t) GetProcAddress (GetModuleHandle ("kernel32.dll"),
-							  "AttachConsole");
-
-		if (p_AttachConsole != NULL && p_AttachConsole (ATTACH_PARENT_PROCESS))
-		{
-			freopen ("CONOUT$", "w", stdout);
-			dup2 (fileno (stdout), 1);
-			freopen ("CONOUT$", "w", stderr);
-			dup2 (fileno (stderr), 2);
-		}
-	}
-#endif
+	/* Parse command line arguments */
+	command_line = gedit_command_line_new ();
 
-	gtk_init (&argc, &argv);
+	ret = gedit_command_line_parse (command_line, &argc, &argv);
 
-	if (!g_option_context_parse (context, &argc, &argv, &error))
+	if (!ret)
 	{
-	        g_print(_("%s\nRun '%s --help' to see a full list of available command line options.\n"),
-			error->message, argv[0]);
-		g_error_free (error);
+		g_object_unref (command_line);
 		return 1;
 	}
 
-	g_option_context_free (context);
-
-#ifndef G_OS_WIN32
-	gedit_debug_message (DEBUG_APP, "Create bacon connection");
-
-	connection = bacon_message_connection_new ("gedit");
-
-	if (connection != NULL)
-	{
-		if (!bacon_message_connection_get_is_server (connection)) 
-		{
-			gedit_debug_message (DEBUG_APP, "I'm a client");
-
-			gedit_get_command_line_data ();
-
-			send_bacon_message ();
-			
-			free_command_line_data ();
-			
-			/* we never popup a window... tell startup-notification
-			 * that we are done.
-			 */
-			gdk_notify_startup_complete ();
-
-			bacon_message_connection_free (connection);
-
-			exit (0);
-		}
-		else 
-		{
-		  	gedit_debug_message (DEBUG_APP, "I'm a server");
-
-			bacon_message_connection_set_callback (connection,
-							       on_message_received,
-							       NULL);
-		}
-	}
-	else
-	{
-		g_warning ("Cannot create the 'gedit' connection.");
-	}
-#endif
-
-	gedit_debug_message (DEBUG_APP, "Set icon");
-	
-	dir = gedit_dirs_get_gedit_data_dir ();
-	icon_dir = g_build_filename (dir,
-				     "icons",
-				     NULL);
-	g_free (dir);
-	
-	gtk_icon_theme_append_search_path (gtk_icon_theme_get_default (),
-					   icon_dir);
-	g_free (icon_dir);
-
-#ifdef GDK_WINDOWING_X11
-	/* Set the associated .desktop file */
-	egg_set_desktop_file (DATADIR "/applications/gedit.desktop");
-#else
-	/* manually set name and icon */
-	g_set_application_name("gedit");
-	gtk_window_set_default_icon_name ("accessories-text-editor");
-#endif
-
-	/* Load user preferences */
+	/* Initialize preferences before launching dbus */
 	gedit_debug_message (DEBUG_APP, "Init prefs manager");
 	gedit_prefs_manager_app_init ();
 
-	/* Init plugins engine */
-	gedit_debug_message (DEBUG_APP, "Init plugins");
-	engine = gedit_plugins_engine_get_default ();
-
-	gtk_about_dialog_set_url_hook (gedit_utils_activate_url, NULL, NULL);
-	
-	/* Initialize session management */
-	gedit_debug_message (DEBUG_APP, "Init session manager");		
-	gedit_session_init ();
-
-#ifdef OS_OSX
-	ige_mac_menu_set_global_key_handler_enabled (FALSE);
-#endif
-
-	if (gedit_session_is_restored ())
-		restored = gedit_session_load ();
+	/* Run over dbus */
+	dbusret = gedit_dbus_initialize (command_line);
 
-	if (!restored)
+	switch (dbusret)
 	{
-		gedit_debug_message (DEBUG_APP, "Analyze command line data");
-		gedit_get_command_line_data ();
-		
-		gedit_debug_message (DEBUG_APP, "Get default app");
-		app = gedit_app_get_default ();
-
-		gedit_debug_message (DEBUG_APP, "Create main window");
-		window = gedit_app_create_window (app, NULL);
+		case GEDIT_DBUS_RESULT_SUCCESS:
+		case GEDIT_DBUS_RESULT_FAILED: /* fallthrough */
+			g_object_unref (command_line);
+			gedit_prefs_manager_app_shutdown ();
 
-		if (file_list != NULL)
-		{
-			const GeditEncoding *encoding = NULL;
-		
-			if (encoding_charset)
-				encoding = gedit_encoding_get_from_charset (encoding_charset);
-		
-			gedit_debug_message (DEBUG_APP, "Load files");
-			_gedit_cmd_load_files_from_prompt (window, 
-							   file_list, 
-							   encoding, 
-							   line_position,
-							   column_position);
-		}
-		else
-		{
-			gedit_debug_message (DEBUG_APP, "Create tab");
-			gedit_window_create_tab (window, TRUE);
-		}
-		
-		gedit_debug_message (DEBUG_APP, "Show window");
-		gtk_widget_show (GTK_WIDGET (window));
-
-		free_command_line_data ();
+			return dbusret == GEDIT_DBUS_RESULT_SUCCESS ? 0 : 1;
+		break;
+		case GEDIT_DBUS_RESULT_PROCEED:
+		break;
 	}
 
-	gedit_debug_message (DEBUG_APP, "Start gtk-main");
-	
-#ifdef OS_OSX
-	gedit_osx_init(gedit_app_get_default ());
-#endif
-	gtk_main();
-
-#ifndef G_OS_WIN32
-	bacon_message_connection_free (connection);
-#endif
+	gedit_main (command_line);
 
-	/* We kept the original engine reference here. So let's unref it to
-	 * finalize it properly. 
-	 */
-	g_object_unref (engine);
 	gedit_prefs_manager_app_shutdown ();
-
-#ifndef ENABLE_GVFS_METADATA
-	gedit_metadata_manager_shutdown ();
-#endif
+	g_object_unref (command_line);
 
 	return 0;
 }
diff --git a/gedit/osx/gedit-osx.c b/gedit/osx/gedit-osx.c
index 4a5a17b..cf24f78 100644
--- a/gedit/osx/gedit-osx.c
+++ b/gedit/osx/gedit-osx.c
@@ -1,3 +1,25 @@
+/*
+ * gedit-osx.c
+ * This file is part of gedit
+ *
+ * Copyright (C) 2010 - Jesse van den Kieboom
+ *
+ * gedit 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.
+ *
+ * gedit 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 gedit; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, 
+ * Boston, MA  02110-1301  USA
+ */
+
 #include "gedit-osx.h"
 #include <gdk/gdkquartz.h>
 #include <Carbon/Carbon.h>
@@ -83,14 +105,21 @@ destroy_delegate (GeditOSXDelegate *delegate)
 }
 
 void
-gedit_osx_init(GeditApp *app)
+gedit_osx_inititialize (void)
+{
+}
+
+void
+gedit_osx_prepare (GeditApp *app)
 {
 	GeditOSXDelegate *delegate = [[GeditOSXDelegate alloc] init];
-	
+
 	g_object_set_data_full (G_OBJECT (app),
 	                        "GeditOSXDelegate",
 	                        delegate,
 	                        (GDestroyNotify)destroy_delegate);
+
+	ige_mac_menu_set_global_key_handler_enabled (FALSE);
 }
 
 /* ex:ts=8:noet: */
diff --git a/gedit/osx/gedit-osx.h b/gedit/osx/gedit-osx.h
index a5e4546..a4b522d 100644
--- a/gedit/osx/gedit-osx.h
+++ b/gedit/osx/gedit-osx.h
@@ -1,3 +1,25 @@
+/*
+ * gedit-osx.h
+ * This file is part of gedit
+ *
+ * Copyright (C) 2010 - Jesse van den Kieboom
+ *
+ * gedit 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.
+ *
+ * gedit 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 gedit; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA  02110-1301  USA
+ */
+
 #ifndef __GEDIT_OSX_H__
 #define __GEDIT_OSX_H__
 
@@ -5,7 +27,12 @@
 #include <gedit/gedit-window.h>
 #include <gedit/gedit-app.h>
 
-void	gedit_osx_init (GeditApp *app);
+#ifdef OS_OSX
+#include <ige-mac-dock.h>
+#include <ige-mac-integration.h>
+
+void	gedit_osx_inititialize (void);
+void	gedit_osx_prepare (GeditApp *app);
 
 void 	gedit_osx_set_window_title 	(GeditWindow   *window, 
 					 gchar const   *title,
@@ -14,6 +41,11 @@ void 	gedit_osx_set_window_title 	(GeditWindow   *window,
 gboolean gedit_osx_show_url 		(const gchar *url);
 gboolean gedit_osx_show_help		(const gchar *link_id);
 
+#else
+#define gedit_osx_initialize(...) ;
+#define gedit_osx_prepare(...) ;
+#endif
+
 #endif /* __GEDIT_OSX_H__ */
 
 /* ex:ts=8:noet: */



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]