]> www.fi.muni.cz Git - evince.git/blob - previewer/ev-previewer.c
[windows/build] Add -mwindows flag for true Windows apps
[evince.git] / previewer / ev-previewer.c
1 /* ev-previewer.c: 
2  *  this file is part of evince, a gnome document viewer
3  *
4  * Copyright (C) 2009 Carlos Garcia Campos <carlosgc@gnome.org>
5  *
6  * Evince is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Evince is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 #include <config.h>
22
23 #include <gtk/gtk.h>
24 #include <glib/gi18n.h>
25 #include <evince-document.h>
26 #include <evince-view.h>
27
28 #include "ev-previewer-window.h"
29
30 static gboolean      unlink_temp_file = FALSE;
31 static const gchar  *print_settings;
32 static const gchar **filenames;
33
34 static const GOptionEntry goption_options[] = {
35         { "unlink-tempfile", 'u', 0, G_OPTION_ARG_NONE, &unlink_temp_file, N_("Delete the temporary file"), NULL },
36         { "print-settings", 'p', 0, G_OPTION_ARG_FILENAME, &print_settings, N_("Print settings file"), N_("FILE") },
37         { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, NULL, N_("FILE") },
38         { NULL }
39 };
40
41 static void
42 ev_previewer_unlink_tempfile (const gchar *filename)
43 {
44         GFile *file, *tempdir;
45
46         file = g_file_new_for_path (filename);
47         tempdir = g_file_new_for_path (g_get_tmp_dir ());
48
49         if (g_file_has_prefix (file, tempdir)) {
50                 g_file_delete (file, NULL, NULL);
51         }
52
53         g_object_unref (file);
54         g_object_unref (tempdir);
55 }
56
57 static void
58 ev_previewer_load_job_finished (EvJob             *job,
59                                 EvPreviewerWindow *window)
60 {
61         if (ev_job_is_failed (job)) {
62                 g_warning ("%s", job->error->message);
63                 g_object_unref (job);
64
65                 return;
66         }
67
68         ev_previewer_window_set_document (window, job->document);
69         g_object_unref (job);
70 }
71
72 static void
73 ev_previewer_load_document (const gchar       *filename,
74                             EvPreviewerWindow *window)
75 {
76         EvJob *job;
77         gchar *uri;
78
79         uri = g_filename_to_uri (filename, NULL, NULL);
80         job = ev_job_load_new (uri);
81         g_signal_connect (job, "finished",
82                           G_CALLBACK (ev_previewer_load_job_finished),
83                           window);
84         ev_job_scheduler_push_job (job, EV_JOB_PRIORITY_NONE);
85         g_free (uri);
86 }
87
88 gint
89 main (gint argc, gchar **argv)
90 {
91         GtkWidget      *window;
92         GOptionContext *context;
93         const gchar    *filename;
94         GError         *error = NULL;
95
96         /* Init glib threads asap */
97         if (!g_thread_supported ())
98                 g_thread_init (NULL);
99
100 #ifdef ENABLE_NLS
101         /* Initialize the i18n stuff */
102         bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
103         bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
104         textdomain (GETTEXT_PACKAGE);
105 #endif
106         
107         context = g_option_context_new (_("GNOME Document Previewer"));
108         g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
109         g_option_context_add_main_entries (context, goption_options, GETTEXT_PACKAGE);
110
111         g_option_context_add_group (context, gtk_get_option_group (TRUE));
112         
113         if (!g_option_context_parse (context, &argc, &argv, &error)) {
114                 g_warning ("Error parsing command line arguments: %s", error->message);
115                 g_error_free (error);
116                 g_option_context_free (context);
117
118                 return 1;
119         }
120         g_option_context_free (context);
121
122         if (!filenames) {
123                 g_warning ("File argument is required");
124                 
125                 return 1;
126         }
127
128         filename = filenames[0];
129         
130         if (!g_file_test (filename, G_FILE_TEST_IS_REGULAR)) {
131                 g_warning ("Filename \"%s\" does not exist or is not a regular file", filename);
132
133                 return 1;
134         }
135
136         if (!ev_init ())
137                 return 1;
138
139         ev_stock_icons_init ();
140
141         g_set_application_name (_("GNOME Document Previewer"));
142         gtk_window_set_default_icon_name ("evince");
143
144         window = ev_previewer_window_new ();
145         ev_previewer_window_set_source_file (EV_PREVIEWER_WINDOW (window), filename);
146         ev_previewer_window_set_print_settings (EV_PREVIEWER_WINDOW (window), print_settings);
147         g_signal_connect (window, "delete-event",
148                           G_CALLBACK (gtk_main_quit), NULL);
149         g_signal_connect (window, "destroy",
150                           G_CALLBACK (gtk_main_quit), NULL);
151         gtk_widget_show (window);
152
153         ev_previewer_load_document (filename, EV_PREVIEWER_WINDOW (window));
154         
155         gtk_main ();
156
157         if (unlink_temp_file)
158                 ev_previewer_unlink_tempfile (filename);
159         if (print_settings)
160                 ev_previewer_unlink_tempfile (print_settings);
161
162         ev_shutdown ();
163         ev_stock_icons_shutdown ();
164         
165         return 0;
166 }