[evince/wip/app: 7/19] previewer: Port to GtkApplication



commit 49607ef64943f12be18a6c59863a29be6b16858a
Author: Christian Persch <chpe gnome org>
Date:   Tue Jun 12 17:12:34 2012 +0200

    previewer: Port to GtkApplication

 configure.ac                    |    2 +-
 previewer/ev-previewer-window.c |   43 ++++++++++--
 previewer/ev-previewer-window.h |    6 +-
 previewer/ev-previewer.c        |  148 +++++++++++++++++++++------------------
 4 files changed, 122 insertions(+), 77 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 49555c3..da84d1a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -117,7 +117,7 @@ dnl Specify required versions of dependencies
 CAIRO_REQUIRED=1.10.0
 GLIB_REQUIRED=2.33.2
 KEYRING_REQUIRED=2.22.0
-GTK_REQUIRED=3.0.2
+GTK_REQUIRED=3.4.4
 NAUTILUS_REQUIRED=2.91.4
 
 AC_SUBST([GLIB_REQUIRED])
diff --git a/previewer/ev-previewer-window.c b/previewer/ev-previewer-window.c
index 83cbe64..615cf32 100644
--- a/previewer/ev-previewer-window.c
+++ b/previewer/ev-previewer-window.c
@@ -30,7 +30,7 @@
 #include "ev-previewer-window.h"
 
 struct _EvPreviewerWindow {
-	GtkWindow         base_instance;
+	GtkApplicationWindow base_instance;
 
 	EvDocumentModel  *model;
 	EvDocument       *document;
@@ -51,10 +51,12 @@ struct _EvPreviewerWindow {
 #endif
 	gchar            *print_job_title;
 	gchar            *source_file;
+
+        gboolean          unlink_temp_file;
 };
 
 struct _EvPreviewerWindowClass {
-	GtkWindowClass base_class;
+	GtkApplicationWindowClass base_class;
 };
 
 enum {
@@ -65,7 +67,23 @@ enum {
 #define MIN_SCALE 0.05409
 #define MAX_SCALE 4.0
 
-G_DEFINE_TYPE (EvPreviewerWindow, ev_previewer_window, GTK_TYPE_WINDOW)
+G_DEFINE_TYPE (EvPreviewerWindow, ev_previewer_window, GTK_TYPE_APPLICATION_WINDOW)
+
+static void
+unlink_tempfile (const gchar *filename)
+{
+        GFile *file, *tempdir;
+
+        file = g_file_new_for_path (filename);
+        tempdir = g_file_new_for_path (g_get_tmp_dir ());
+
+        if (g_file_has_prefix (file, tempdir)) {
+                g_file_delete (file, NULL, NULL);
+        }
+
+        g_object_unref (file);
+        g_object_unref (tempdir);
+}
 
 static gdouble
 get_screen_dpi (EvPreviewerWindow *window)
@@ -469,6 +487,8 @@ ev_previewer_window_dispose (GObject *object)
 	}
 
 	if (window->source_file) {
+                if (window->unlink_temp_file)
+                      unlink_tempfile (window->source_file);
 		g_free (window->source_file);
 		window->source_file = NULL;
 	}
@@ -480,6 +500,8 @@ static void
 ev_previewer_window_init (EvPreviewerWindow *window)
 {
 	gtk_window_set_default_size (GTK_WINDOW (window), 600, 600);
+
+        window->unlink_temp_file = FALSE;
 }
 
 static void
@@ -627,10 +649,13 @@ ev_previewer_window_class_init (EvPreviewerWindowClass *klass)
 }
 
 /* Public methods */
-GtkWidget *
+EvPreviewerWindow *
 ev_previewer_window_new (EvDocumentModel *model)
 {
-	return GTK_WIDGET (g_object_new (EV_TYPE_PREVIEWER_WINDOW, "model", model, NULL));
+	return g_object_new (EV_TYPE_PREVIEWER_WINDOW, 
+                             "application", g_application_get_default (),
+                             "model", model,
+                             NULL);
 }
 
 void
@@ -683,6 +708,7 @@ ev_previewer_window_set_print_settings (EvPreviewerWindow *window,
 		}
 
 		g_key_file_free (key_file);
+                unlink_tempfile (print_settings);
 	} else {
 		window->print_settings = gtk_print_settings_new ();
 		window->print_page_setup = gtk_page_setup_new ();
@@ -697,3 +723,10 @@ ev_previewer_window_set_source_file (EvPreviewerWindow *window,
 		g_free (window->source_file);
 	window->source_file = g_strdup (source_file);
 }
+
+void
+ev_previewer_window_set_unlink_source (EvPreviewerWindow *window,
+                                       gboolean           unlink_temp_file)
+{
+        window->unlink_temp_file = unlink_temp_file != FALSE;
+}
diff --git a/previewer/ev-previewer-window.h b/previewer/ev-previewer-window.h
index 698b13f..4d73984 100644
--- a/previewer/ev-previewer-window.h
+++ b/previewer/ev-previewer-window.h
@@ -38,13 +38,15 @@ G_BEGIN_DECLS
 typedef struct _EvPreviewerWindow      EvPreviewerWindow;
 typedef struct _EvPreviewerWindowClass EvPreviewerWindowClass;
 
-GType      ev_previewer_window_get_type           (void) G_GNUC_CONST;
-GtkWidget *ev_previewer_window_new                (EvDocumentModel   *model);
+GType              ev_previewer_window_get_type   (void) G_GNUC_CONST;
+EvPreviewerWindow *ev_previewer_window_new        (EvDocumentModel   *model);
 
 void       ev_previewer_window_set_print_settings (EvPreviewerWindow *window,
 						   const gchar       *print_settings);
 void       ev_previewer_window_set_source_file    (EvPreviewerWindow *window,
 						   const gchar       *source_file);
+void       ev_previewer_window_set_unlink_source  (EvPreviewerWindow *window,
+                                                   gboolean           unlink_temp_file);
 
 G_END_DECLS
 
diff --git a/previewer/ev-previewer.c b/previewer/ev-previewer.c
index 1b1b787..7749775 100644
--- a/previewer/ev-previewer.c
+++ b/previewer/ev-previewer.c
@@ -2,6 +2,7 @@
  *  this file is part of evince, a gnome document viewer
  *
  * Copyright (C) 2009 Carlos Garcia Campos <carlosgc gnome org>
+ * Copyright  2012 Christian Persch
  *
  * Evince is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by
@@ -18,10 +19,11 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
-#include <config.h>
+#include "config.h"
 
 #include <gtk/gtk.h>
 #include <glib/gi18n.h>
+#include <locale.h>
 #include <evince-document.h>
 #include <evince-view.h>
 
@@ -36,41 +38,22 @@
 #include <windows.h>
 #endif
 
-static gboolean      unlink_temp_file = FALSE;
-static const gchar  *print_settings;
-static const gchar **filenames;
+static gboolean unlink_temp_file = FALSE;
+static gchar *print_settings = NULL;
+static EvPreviewerWindow *window = NULL;
 
 static const GOptionEntry goption_options[] = {
 	{ "unlink-tempfile", 'u', 0, G_OPTION_ARG_NONE, &unlink_temp_file, N_("Delete the temporary file"), NULL },
 	{ "print-settings", 'p', 0, G_OPTION_ARG_FILENAME, &print_settings, N_("Print settings file"), N_("FILE") },
-	{ G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, NULL, N_("FILE") },
 	{ NULL }
 };
 
 static void
-ev_previewer_unlink_tempfile (const gchar *filename)
-{
-	GFile *file, *tempdir;
-
-	file = g_file_new_for_path (filename);
-	tempdir = g_file_new_for_path (g_get_tmp_dir ());
-
-	if (g_file_has_prefix (file, tempdir)) {
-		g_file_delete (file, NULL, NULL);
-	}
-
-	g_object_unref (file);
-	g_object_unref (tempdir);
-}
-
-static void
 ev_previewer_load_job_finished (EvJob           *job,
 				EvDocumentModel *model)
 {
 	if (ev_job_is_failed (job)) {
-		g_warning ("%s", job->error->message);
 		g_object_unref (job);
-
 		return;
 	}
 	ev_document_model_set_document (model, job->document);
@@ -78,16 +61,13 @@ ev_previewer_load_job_finished (EvJob           *job,
 }
 
 static void
-ev_previewer_load_document (const gchar     *filename,
+ev_previewer_load_document (GFile           *file,
 			    EvDocumentModel *model)
 {
 	EvJob *job;
 	gchar *uri;
-	GFile  *file;
 
-	file = g_file_new_for_commandline_arg (filename);
 	uri = g_file_get_uri (file);
-	g_object_unref (file);
 
 	job = ev_job_load_new (uri);
 	g_signal_connect (job, "finished",
@@ -97,14 +77,55 @@ ev_previewer_load_document (const gchar     *filename,
 	g_free (uri);
 }
 
+static void
+activate_cb (GApplication *application,
+             gpointer user_data)
+{
+        if (window) {
+                gtk_window_present (GTK_WINDOW (window));
+        }
+}
+
+static void
+open_cb (GApplication *application,
+         GFile **files,
+         gint n_files,
+         const gchar *hint,
+         gpointer user_data)
+{
+        EvDocumentModel *model;
+        GFile           *file;
+        char            *path;
+
+        if (n_files != 1) {
+                g_application_quit (application);
+                return;
+        }
+
+        file = files[0];
+
+        model = ev_document_model_new ();
+        ev_previewer_load_document (file, model);
+
+        window = ev_previewer_window_new (model);
+        g_object_unref (model);
+
+        ev_previewer_window_set_unlink_source (EV_PREVIEWER_WINDOW (window), unlink_temp_file);
+        ev_previewer_window_set_print_settings (EV_PREVIEWER_WINDOW (window), print_settings);
+        path = g_file_get_path (file);
+        ev_previewer_window_set_source_file (EV_PREVIEWER_WINDOW (window), path);
+        g_free (path);
+
+        gtk_window_present (GTK_WINDOW (window));
+}
+
 gint
 main (gint argc, gchar **argv)
 {
-	GtkWidget       *window;
+        GtkApplication  *application;
 	GOptionContext  *context;
-	const gchar     *filename;
-	EvDocumentModel *model;
 	GError          *error = NULL;
+        int              status = 1;
 
 #ifdef G_OS_WIN32
     if (fileno (stdout) != -1 &&
@@ -136,66 +157,55 @@ main (gint argc, gchar **argv)
 	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
 	textdomain (GETTEXT_PACKAGE);
 #endif
-	
+
+        setlocale (LC_ALL, "");
+
 	context = g_option_context_new (_("GNOME Document Previewer"));
 	g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
 	g_option_context_add_main_entries (context, goption_options, GETTEXT_PACKAGE);
 
 	g_option_context_add_group (context, gtk_get_option_group (TRUE));
-	
+
 	if (!g_option_context_parse (context, &argc, &argv, &error)) {
 		g_warning ("Error parsing command line arguments: %s", error->message);
 		g_error_free (error);
 		g_option_context_free (context);
-
-		return 1;
+                return 1;
 	}
 	g_option_context_free (context);
 
-	if (!filenames) {
-		g_warning ("File argument is required");
-		
-		return 1;
-	}
-
-	filename = filenames[0];
-	
-	if (!g_file_test (filename, G_FILE_TEST_IS_REGULAR)) {
-		g_warning ("Filename \"%s\" does not exist or is not a regular file", filename);
-
-		return 1;
+	if (argc < 2) {
+		g_printerr ("File argument is required\n");
+                return 1;
+	} else if (argc > 2) {
+                g_printerr ("Too many files\n");
+                return 1;
+        }
+
+	if (!g_file_test (argv[1], G_FILE_TEST_IS_REGULAR)) {
+		g_warning ("Filename \"%s\" does not exist or is not a regular file", argv[1]);
+                return 1;
 	}
 
-	if (!ev_init ())
-		return 1;
+	if (!ev_init ()) {
+                return 1;
+        }
 
 	ev_stock_icons_init ();
 
 	g_set_application_name (_("GNOME Document Previewer"));
 	gtk_window_set_default_icon_name ("evince");
 
-	model = ev_document_model_new ();
-	window = ev_previewer_window_new (model);
-	ev_previewer_window_set_source_file (EV_PREVIEWER_WINDOW (window), filename);
-	ev_previewer_window_set_print_settings (EV_PREVIEWER_WINDOW (window), print_settings);
-	g_signal_connect (window, "delete-event",
-			  G_CALLBACK (gtk_main_quit), NULL);
-	g_signal_connect (window, "destroy",
-			  G_CALLBACK (gtk_main_quit), NULL);
-	gtk_widget_show (window);
-
-	ev_previewer_load_document (filename, model);
-	
-	gtk_main ();
-
-	if (unlink_temp_file)
-		ev_previewer_unlink_tempfile (filename);
-	if (print_settings)
-		ev_previewer_unlink_tempfile (print_settings);
+        application = gtk_application_new (NULL,
+                                           G_APPLICATION_NON_UNIQUE |
+                                           G_APPLICATION_HANDLES_OPEN);
+        g_signal_connect (application, "activate", G_CALLBACK (activate_cb), NULL);
+        g_signal_connect (application, "open", G_CALLBACK (open_cb), NULL);
+
+        status = g_application_run (G_APPLICATION (application), argc, argv);
 
 	ev_shutdown ();
 	ev_stock_icons_shutdown ();
-	g_object_unref (model);
-	
-	return 0;
+
+	return status;
 }



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