]> www.fi.muni.cz Git - evince.git/blob - previewer/ev-previewer.c
7a83eee07be11e347938b7ed6cc63aa11496dc71
[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 #ifdef G_OS_WIN32
31 #ifdef DATADIR
32 #undef DATADIR
33 #endif
34 #include <io.h>
35 #include <conio.h>
36 #define _WIN32_WINNT 0x0500
37 #include <windows.h>
38 #endif
39
40 static gboolean      unlink_temp_file = FALSE;
41 static const gchar  *print_settings;
42 static const gchar **filenames;
43
44 static const GOptionEntry goption_options[] = {
45         { "unlink-tempfile", 'u', 0, G_OPTION_ARG_NONE, &unlink_temp_file, N_("Delete the temporary file"), NULL },
46         { "print-settings", 'p', 0, G_OPTION_ARG_FILENAME, &print_settings, N_("Print settings file"), N_("FILE") },
47         { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, NULL, N_("FILE") },
48         { NULL }
49 };
50
51 static void
52 ev_previewer_unlink_tempfile (const gchar *filename)
53 {
54         GFile *file, *tempdir;
55
56         file = g_file_new_for_path (filename);
57         tempdir = g_file_new_for_path (g_get_tmp_dir ());
58
59         if (g_file_has_prefix (file, tempdir)) {
60                 g_file_delete (file, NULL, NULL);
61         }
62
63         g_object_unref (file);
64         g_object_unref (tempdir);
65 }
66
67 static void
68 ev_previewer_load_job_finished (EvJob           *job,
69                                 EvDocumentModel *model)
70 {
71         if (ev_job_is_failed (job)) {
72                 g_warning ("%s", job->error->message);
73                 g_object_unref (job);
74
75                 return;
76         }
77         ev_document_model_set_document (model, job->document);
78         g_object_unref (job);
79 }
80
81 static void
82 ev_previewer_load_document (const gchar     *filename,
83                             EvDocumentModel *model)
84 {
85         EvJob *job;
86         gchar *uri;
87
88         uri = g_filename_to_uri (filename, NULL, NULL);
89         job = ev_job_load_new (uri);
90         g_signal_connect (job, "finished",
91                           G_CALLBACK (ev_previewer_load_job_finished),
92                           model);
93         ev_job_scheduler_push_job (job, EV_JOB_PRIORITY_NONE);
94         g_free (uri);
95 }
96
97 gint
98 main (gint argc, gchar **argv)
99 {
100         GtkWidget       *window;
101         GOptionContext  *context;
102         const gchar     *filename;
103         EvDocumentModel *model;
104         GError          *error = NULL;
105
106 #ifdef G_OS_WIN32
107     if (fileno (stdout) != -1 &&
108           _get_osfhandle (fileno (stdout)) != -1)
109         {
110           /* stdout is fine, presumably redirected to a file or pipe */
111         }
112     else
113     {
114           typedef BOOL (* WINAPI AttachConsole_t) (DWORD);
115
116           AttachConsole_t p_AttachConsole =
117             (AttachConsole_t) GetProcAddress (GetModuleHandle ("kernel32.dll"), "AttachConsole");
118
119           if (p_AttachConsole != NULL && p_AttachConsole (ATTACH_PARENT_PROCESS))
120       {
121               freopen ("CONOUT$", "w", stdout);
122               dup2 (fileno (stdout), 1);
123               freopen ("CONOUT$", "w", stderr);
124               dup2 (fileno (stderr), 2);
125
126       }
127         }
128 #endif
129
130         /* Init glib threads asap */
131         if (!g_thread_supported ())
132                 g_thread_init (NULL);
133
134 #ifdef ENABLE_NLS
135         /* Initialize the i18n stuff */
136         bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
137         bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
138         textdomain (GETTEXT_PACKAGE);
139 #endif
140         
141         context = g_option_context_new (_("GNOME Document Previewer"));
142         g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
143         g_option_context_add_main_entries (context, goption_options, GETTEXT_PACKAGE);
144
145         g_option_context_add_group (context, gtk_get_option_group (TRUE));
146         
147         if (!g_option_context_parse (context, &argc, &argv, &error)) {
148                 g_warning ("Error parsing command line arguments: %s", error->message);
149                 g_error_free (error);
150                 g_option_context_free (context);
151
152                 return 1;
153         }
154         g_option_context_free (context);
155
156         if (!filenames) {
157                 g_warning ("File argument is required");
158                 
159                 return 1;
160         }
161
162         filename = filenames[0];
163         
164         if (!g_file_test (filename, G_FILE_TEST_IS_REGULAR)) {
165                 g_warning ("Filename \"%s\" does not exist or is not a regular file", filename);
166
167                 return 1;
168         }
169
170         if (!ev_init ())
171                 return 1;
172
173         ev_stock_icons_init ();
174
175         g_set_application_name (_("GNOME Document Previewer"));
176         gtk_window_set_default_icon_name ("evince");
177
178         model = ev_document_model_new ();
179         window = ev_previewer_window_new (model);
180         ev_previewer_window_set_source_file (EV_PREVIEWER_WINDOW (window), filename);
181         ev_previewer_window_set_print_settings (EV_PREVIEWER_WINDOW (window), print_settings);
182         g_signal_connect (window, "delete-event",
183                           G_CALLBACK (gtk_main_quit), NULL);
184         g_signal_connect (window, "destroy",
185                           G_CALLBACK (gtk_main_quit), NULL);
186         gtk_widget_show (window);
187
188         ev_previewer_load_document (filename, model);
189         
190         gtk_main ();
191
192         if (unlink_temp_file)
193                 ev_previewer_unlink_tempfile (filename);
194         if (print_settings)
195                 ev_previewer_unlink_tempfile (print_settings);
196
197         ev_shutdown ();
198         ev_stock_icons_shutdown ();
199         g_object_unref (model);
200         
201         return 0;
202 }