2 * this file is part of evince, a gnome document viewer
4 * Copyright (C) 2009 Carlos Garcia Campos <carlosgc@gnome.org>
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include <glib/gi18n.h>
25 #include <evince-document.h>
26 #include <evince-view.h>
28 #include "ev-previewer-window.h"
36 #if !(_WIN32_WINNT >= 0x0500)
37 #error "_WIN32_WINNT must be defined >= 0x0500"
42 static gboolean unlink_temp_file = FALSE;
43 static const gchar *print_settings;
44 static const gchar **filenames;
46 static const GOptionEntry goption_options[] = {
47 { "unlink-tempfile", 'u', 0, G_OPTION_ARG_NONE, &unlink_temp_file, N_("Delete the temporary file"), NULL },
48 { "print-settings", 'p', 0, G_OPTION_ARG_FILENAME, &print_settings, N_("Print settings file"), N_("FILE") },
49 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, NULL, N_("FILE") },
54 ev_previewer_unlink_tempfile (const gchar *filename)
56 GFile *file, *tempdir;
58 file = g_file_new_for_path (filename);
59 tempdir = g_file_new_for_path (g_get_tmp_dir ());
61 if (g_file_has_prefix (file, tempdir)) {
62 g_file_delete (file, NULL, NULL);
65 g_object_unref (file);
66 g_object_unref (tempdir);
70 ev_previewer_load_job_finished (EvJob *job,
71 EvDocumentModel *model)
73 if (ev_job_is_failed (job)) {
74 g_warning ("%s", job->error->message);
79 ev_document_model_set_document (model, job->document);
84 ev_previewer_load_document (const gchar *filename,
85 EvDocumentModel *model)
91 file = g_file_new_for_commandline_arg (filename);
92 uri = g_file_get_uri (file);
93 g_object_unref (file);
95 job = ev_job_load_new (uri);
96 g_signal_connect (job, "finished",
97 G_CALLBACK (ev_previewer_load_job_finished),
99 ev_job_scheduler_push_job (job, EV_JOB_PRIORITY_NONE);
104 main (gint argc, gchar **argv)
107 GOptionContext *context;
108 const gchar *filename;
109 EvDocumentModel *model;
110 GError *error = NULL;
113 if (fileno (stdout) != -1 &&
114 _get_osfhandle (fileno (stdout)) != -1)
116 /* stdout is fine, presumably redirected to a file or pipe */
120 typedef BOOL (* WINAPI AttachConsole_t) (DWORD);
122 AttachConsole_t p_AttachConsole =
123 (AttachConsole_t) GetProcAddress (GetModuleHandle ("kernel32.dll"), "AttachConsole");
125 if (p_AttachConsole != NULL && p_AttachConsole (ATTACH_PARENT_PROCESS))
127 freopen ("CONOUT$", "w", stdout);
128 dup2 (fileno (stdout), 1);
129 freopen ("CONOUT$", "w", stderr);
130 dup2 (fileno (stderr), 2);
136 /* Init glib threads asap */
137 if (!g_thread_supported ())
138 g_thread_init (NULL);
141 /* Initialize the i18n stuff */
142 bindtextdomain (GETTEXT_PACKAGE, ev_get_locale_dir());
143 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
144 textdomain (GETTEXT_PACKAGE);
147 context = g_option_context_new (_("GNOME Document Previewer"));
148 g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
149 g_option_context_add_main_entries (context, goption_options, GETTEXT_PACKAGE);
151 g_option_context_add_group (context, gtk_get_option_group (TRUE));
153 if (!g_option_context_parse (context, &argc, &argv, &error)) {
154 g_warning ("Error parsing command line arguments: %s", error->message);
155 g_error_free (error);
156 g_option_context_free (context);
160 g_option_context_free (context);
163 g_warning ("File argument is required");
168 filename = filenames[0];
170 if (!g_file_test (filename, G_FILE_TEST_IS_REGULAR)) {
171 g_warning ("Filename \"%s\" does not exist or is not a regular file", filename);
179 ev_stock_icons_init ();
181 g_set_application_name (_("GNOME Document Previewer"));
182 gtk_window_set_default_icon_name ("evince");
184 model = ev_document_model_new ();
185 window = ev_previewer_window_new (model);
186 ev_previewer_window_set_source_file (EV_PREVIEWER_WINDOW (window), filename);
187 ev_previewer_window_set_print_settings (EV_PREVIEWER_WINDOW (window), print_settings);
188 g_signal_connect (window, "delete-event",
189 G_CALLBACK (gtk_main_quit), NULL);
190 g_signal_connect (window, "destroy",
191 G_CALLBACK (gtk_main_quit), NULL);
192 gtk_widget_show (window);
194 ev_previewer_load_document (filename, model);
198 if (unlink_temp_file)
199 ev_previewer_unlink_tempfile (filename);
201 ev_previewer_unlink_tempfile (print_settings);
204 ev_stock_icons_shutdown ();
205 g_object_unref (model);