[gedit/wip/gtkapp: 12/38] Handle the launch of the window from the application



commit e2c574ae62fb5740b4cd0d71e33da8ee896dfcc9
Author: Ignacio Casal Quinteiro <icq gnome org>
Date:   Sun May 6 12:34:57 2012 +0200

    Handle the launch of the window from the application

 gedit/gedit-app.c |   35 ++++++++++-
 gedit/gedit.c     |  178 ++++++++++++-----------------------------------------
 2 files changed, 72 insertions(+), 141 deletions(-)
---
diff --git a/gedit/gedit-app.c b/gedit/gedit-app.c
index 46953c3..f539109 100644
--- a/gedit/gedit-app.c
+++ b/gedit/gedit-app.c
@@ -242,6 +242,36 @@ gedit_app_set_window_title_impl (GeditApp    *app,
 	gtk_window_set_title (GTK_WINDOW (window), title);
 }
 
+static void
+gedit_app_startup (GApplication *application)
+{
+	const gchar *dir;
+	gchar *icon_dir;
+
+	G_APPLICATION_CLASS (gedit_app_parent_class)->startup (application);
+
+	gedit_debug_message (DEBUG_APP, "Set icon");
+
+	dir = gedit_dirs_get_gedit_data_dir ();
+	icon_dir = g_build_filename (dir, "icons", NULL);
+
+	gtk_icon_theme_append_search_path (gtk_icon_theme_get_default (), icon_dir);
+	g_free (icon_dir);
+}
+
+static void
+gedit_app_activate (GApplication *application)
+{
+	GeditWindow *window;
+
+	gedit_debug_message (DEBUG_APP, "Create main window");
+	window = gedit_app_create_window (GEDIT_APP (application), NULL);
+	gtk_application_add_window (GTK_APPLICATION (application), GTK_WINDOW (window));
+
+	gedit_debug_message (DEBUG_APP, "Show window");
+	gtk_widget_show (GTK_WIDGET (window));
+}
+
 static gboolean
 ensure_user_config_dir (void)
 {
@@ -540,6 +570,8 @@ gedit_app_class_init (GeditAppClass *klass)
 	object_class->constructor = gedit_app_constructor;
 	object_class->constructed = gedit_app_constructed;
 
+	app_class->startup = gedit_app_startup;
+	app_class->activate = gedit_app_activate;
 	app_class->shutdown = gedit_app_shutdown;
 
 	klass->last_window_destroyed = gedit_app_last_window_destroyed_impl;
@@ -715,9 +747,8 @@ gedit_app_get_default (void)
 #endif
 
 	return GEDIT_APP (g_object_new (type,
-	                                "application-id", "org.gnome.gedit",
+	                                "application-id", "org.gnome.Gedit",
 	                                // FIXME: should be HANDLES_COMMAND_LINE
-	                                "flags", G_APPLICATION_FLAGS_NONE,
 	                                NULL));
 }
 
diff --git a/gedit/gedit.c b/gedit/gedit.c
index b3a652d..18127e9 100644
--- a/gedit/gedit.c
+++ b/gedit/gedit.c
@@ -37,154 +37,26 @@
 #include <glib.h>
 #include <glib/gi18n.h>
 
-#include "gedit-command-line.h"
-
 #include "gedit-app.h"
-#include "gedit-encodings.h"
 
-#include "gedit-commands.h"
 #include "gedit-debug.h"
 #include "gedit-dirs.h"
-#include "gedit-encodings.h"
 #include "gedit-plugins-engine.h"
 #include "gedit-session.h"
-#include "gedit-utils.h"
-#include "gedit-window.h"
 
 #ifndef ENABLE_GVFS_METADATA
 #include "gedit-metadata-manager.h"
 #define METADATA_FILE "gedit-metadata.xml"
 #endif
 
-static void
-gedit_main_window (void)
+int
+main (int argc, char *argv[])
 {
-	GSList *file_list;
-	GeditWindow *window;
-	GeditCommandLine *command_line;
 	GeditApp *app;
-	gboolean doc_created = FALSE;
-	const gchar *geometry;
-
-	app = gedit_app_get_default ();
-
-	gedit_debug_message (DEBUG_APP, "Create main window");
-	window = gedit_app_create_window (app, NULL);
-
-	command_line = gedit_command_line_get_default ();
-	file_list = gedit_command_line_get_file_list (command_line);
-
-	if (file_list != NULL)
-	{
-		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);
-	}
-
-	if (!doc_created || gedit_command_line_get_new_document (command_line))
-	{
-		gedit_debug_message (DEBUG_APP, "Create tab");
-		gedit_window_create_tab (window, TRUE);
-	}
-
-	geometry = gedit_command_line_get_geometry (command_line);
-
-	gedit_debug_message (DEBUG_APP, "Show window");
-	gtk_widget_show (GTK_WIDGET (window));
-
-	if (geometry)
-	{
-		gtk_window_parse_geometry (GTK_WINDOW (window),
-		                           geometry);
-	}
-}
-
-static void
-gedit_main (gboolean service)
-{
 	GeditPluginsEngine *engine;
-	GeditApp *app;
 	const gchar *dir;
-	gchar *icon_dir;
 	gint status;
 
-	gedit_debug_message (DEBUG_APP, "Set icon");
-
-	dir = gedit_dirs_get_gedit_data_dir ();
-	icon_dir = g_build_filename (dir, "icons", NULL);
-
-	gtk_icon_theme_append_search_path (gtk_icon_theme_get_default (), icon_dir);
-	g_free (icon_dir);
-
-	/* Init plugins engine */
-	gedit_debug_message (DEBUG_APP, "Init plugins");
-	engine = gedit_plugins_engine_get_default ();
-
-	app = gedit_app_get_default ();
-
-	/* Initialize session management */
-	gedit_debug_message (DEBUG_APP, "Init session manager");
-	gedit_session_init ();
-
-	if (!service)
-	{
-		gboolean restored = FALSE;
-
-		if (gedit_session_is_restored ())
-		{
-			restored = gedit_session_load ();
-		}
-
-		if (!restored)
-		{
-			gedit_main_window ();
-		}
-	}
-
-	_gedit_app_ready (app);
-
-	gedit_debug_message (DEBUG_APP, "Start gtk-main");
-	// FIXME: the status has to be returned and we must parse the
-	// commandline stuff with g application itself
-	status = g_application_run (G_APPLICATION (app), 0, NULL);
-
-	/* Make sure settings are saved */
-	g_settings_sync ();
-
-	/* Cleanup */
-	g_object_unref (engine);
-	g_object_unref (app);
-
-	gedit_dirs_shutdown ();
-
-#ifndef ENABLE_GVFS_METADATA
-	gedit_metadata_manager_shutdown ();
-#endif
-}
-
-int
-main (int argc, char *argv[])
-{
-	const gchar *dir;
-	GeditCommandLine *command_line;
-	gboolean ret;
-	gboolean service = FALSE;
-
 #ifndef ENABLE_GVFS_METADATA
 	const gchar *cache_dir;
 	gchar *metadata_filename;
@@ -216,22 +88,50 @@ main (int argc, char *argv[])
 	g_free (metadata_filename);
 #endif
 
-	/* Parse command line arguments */
-	command_line = gedit_command_line_get_default ();
-
-	ret = gedit_command_line_parse (command_line, &argc, &argv);
+	/* FIXME: This is needed if not we get a crash building with introspection */
+#ifdef ENABLE_INTROSPECTION
+	GOptionContext *context;
+	GError *error = NULL;
 
-	if (!ret)
+	context = g_option_context_new (_("- Edit text files"));
+	g_option_context_add_group (context, g_irepository_get_option_group ());
+	if (!g_option_context_parse (context, &argc, &argv, &error))
 	{
-		g_object_unref (command_line);
+		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 1;
 	}
+	g_option_context_free (context);
+#endif
 
-	gedit_main (service);
+	/* Init plugins en thegine */
+	gedit_debug_message (DEBUG_APP, "Init plugins");
+	engine = gedit_plugins_engine_get_default ();
+
+	/* Initialize session management */
+	gedit_debug_message (DEBUG_APP, "Init session manager");
+	gedit_session_init ();
+
+	gedit_debug_message (DEBUG_APP, "Run application");
+	app = gedit_app_get_default ();
+	status = g_application_run (G_APPLICATION (app), argc, argv);
+
+	/* Make sure settings are saved */
+	g_settings_sync ();
+
+	/* Cleanup */
+	g_object_unref (app);
+	g_object_unref (engine);
 
-	g_object_unref (command_line);
+	gedit_dirs_shutdown ();
+
+#ifndef ENABLE_GVFS_METADATA
+	gedit_metadata_manager_shutdown ();
+#endif
 
-	return 0;
+	return status;
 }
 
 /* ex:set ts=8 noet: */



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