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"
33 #if !(_WIN32_WINNT >= 0x0500)
34 #error "_WIN32_WINNT must be defined >= 0x0500"
39 static gboolean unlink_temp_file = FALSE;
40 static const gchar *print_settings;
41 static const gchar **filenames;
43 static const GOptionEntry goption_options[] = {
44 { "unlink-tempfile", 'u', 0, G_OPTION_ARG_NONE, &unlink_temp_file, N_("Delete the temporary file"), NULL },
45 { "print-settings", 'p', 0, G_OPTION_ARG_FILENAME, &print_settings, N_("Print settings file"), N_("FILE") },
46 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, NULL, N_("FILE") },
51 ev_previewer_unlink_tempfile (const gchar *filename)
53 GFile *file, *tempdir;
55 file = g_file_new_for_path (filename);
56 tempdir = g_file_new_for_path (g_get_tmp_dir ());
58 if (g_file_has_prefix (file, tempdir)) {
59 g_file_delete (file, NULL, NULL);
62 g_object_unref (file);
63 g_object_unref (tempdir);
67 ev_previewer_load_job_finished (EvJob *job,
68 EvDocumentModel *model)
70 if (ev_job_is_failed (job)) {
71 g_warning ("%s", job->error->message);
76 ev_document_model_set_document (model, job->document);
81 ev_previewer_load_document (const gchar *filename,
82 EvDocumentModel *model)
88 file = g_file_new_for_commandline_arg (filename);
89 uri = g_file_get_uri (file);
90 g_object_unref (file);
92 job = ev_job_load_new (uri);
93 g_signal_connect (job, "finished",
94 G_CALLBACK (ev_previewer_load_job_finished),
96 ev_job_scheduler_push_job (job, EV_JOB_PRIORITY_NONE);
101 main (gint argc, gchar **argv)
104 GOptionContext *context;
105 const gchar *filename;
106 EvDocumentModel *model;
107 GError *error = NULL;
110 if (fileno (stdout) != -1 &&
111 _get_osfhandle (fileno (stdout)) != -1)
113 /* stdout is fine, presumably redirected to a file or pipe */
117 typedef BOOL (* WINAPI AttachConsole_t) (DWORD);
119 AttachConsole_t p_AttachConsole =
120 (AttachConsole_t) GetProcAddress (GetModuleHandle ("kernel32.dll"), "AttachConsole");
122 if (p_AttachConsole != NULL && p_AttachConsole (ATTACH_PARENT_PROCESS))
124 freopen ("CONOUT$", "w", stdout);
125 dup2 (fileno (stdout), 1);
126 freopen ("CONOUT$", "w", stderr);
127 dup2 (fileno (stderr), 2);
133 /* Init glib threads asap */
134 if (!g_thread_supported ())
135 g_thread_init (NULL);
138 /* Initialize the i18n stuff */
139 bindtextdomain (GETTEXT_PACKAGE, ev_get_locale_dir());
140 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
141 textdomain (GETTEXT_PACKAGE);
144 context = g_option_context_new (_("GNOME Document Previewer"));
145 g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
146 g_option_context_add_main_entries (context, goption_options, GETTEXT_PACKAGE);
148 g_option_context_add_group (context, gtk_get_option_group (TRUE));
150 if (!g_option_context_parse (context, &argc, &argv, &error)) {
151 g_warning ("Error parsing command line arguments: %s", error->message);
152 g_error_free (error);
153 g_option_context_free (context);
157 g_option_context_free (context);
160 g_warning ("File argument is required");
165 filename = filenames[0];
167 if (!g_file_test (filename, G_FILE_TEST_IS_REGULAR)) {
168 g_warning ("Filename \"%s\" does not exist or is not a regular file", filename);
176 ev_stock_icons_init ();
178 g_set_application_name (_("GNOME Document Previewer"));
179 gtk_window_set_default_icon_name ("evince");
181 model = ev_document_model_new ();
182 window = ev_previewer_window_new (model);
183 ev_previewer_window_set_source_file (EV_PREVIEWER_WINDOW (window), filename);
184 ev_previewer_window_set_print_settings (EV_PREVIEWER_WINDOW (window), print_settings);
185 g_signal_connect (window, "delete-event",
186 G_CALLBACK (gtk_main_quit), NULL);
187 g_signal_connect (window, "destroy",
188 G_CALLBACK (gtk_main_quit), NULL);
189 gtk_widget_show (window);
191 ev_previewer_load_document (filename, model);
195 if (unlink_temp_file)
196 ev_previewer_unlink_tempfile (filename);
198 ev_previewer_unlink_tempfile (print_settings);
201 ev_stock_icons_shutdown ();
202 g_object_unref (model);