]> www.fi.muni.cz Git - evince.git/blob - previewer/ev-previewer.c
14d4f2f332306543779310a2f53998b61013c0b5
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 #if !(_WIN32_WINNT >= 0x0500)
37 #error "_WIN32_WINNT must be defined >= 0x0500"
38 #endif
39 #include <windows.h>
40 #endif
41
42 static gboolean      unlink_temp_file = FALSE;
43 static const gchar  *print_settings;
44 static const gchar **filenames;
45
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") },
50         { NULL }
51 };
52
53 static void
54 ev_previewer_unlink_tempfile (const gchar *filename)
55 {
56         GFile *file, *tempdir;
57
58         file = g_file_new_for_path (filename);
59         tempdir = g_file_new_for_path (g_get_tmp_dir ());
60
61         if (g_file_has_prefix (file, tempdir)) {
62                 g_file_delete (file, NULL, NULL);
63         }
64
65         g_object_unref (file);
66         g_object_unref (tempdir);
67 }
68
69 static void
70 ev_previewer_load_job_finished (EvJob           *job,
71                                 EvDocumentModel *model)
72 {
73         if (ev_job_is_failed (job)) {
74                 g_warning ("%s", job->error->message);
75                 g_object_unref (job);
76
77                 return;
78         }
79         ev_document_model_set_document (model, job->document);
80         g_object_unref (job);
81 }
82
83 static void
84 ev_previewer_load_document (const gchar     *filename,
85                             EvDocumentModel *model)
86 {
87         EvJob *job;
88         gchar *uri;
89         GFile  *file;
90
91         file = g_file_new_for_commandline_arg (filename);
92         uri = g_file_get_uri (file);
93         g_object_unref (file);
94
95         job = ev_job_load_new (uri);
96         g_signal_connect (job, "finished",
97                           G_CALLBACK (ev_previewer_load_job_finished),
98                           model);
99         ev_job_scheduler_push_job (job, EV_JOB_PRIORITY_NONE);
100         g_free (uri);
101 }
102
103 static gchar*
104 ev_previewer_get_locale_dir ()
105 {
106 #ifdef G_OS_WIN32
107         gchar *install_dir = NULL, *locale_dir;
108         gchar *retval = NULL;
109
110         install_dir =
111             g_win32_get_package_installation_directory_of_module (NULL);
112
113         if (install_dir) {
114                 locale_dir = g_build_filename (install_dir,
115                                         "share", "locale", NULL);
116                 retval = g_win32_locale_filename_from_utf8 (locale_dir);
117                 g_free (install_dir);
118                 g_free (locale_dir);
119         }
120
121         if (retval)
122                 return retval;
123         else
124                 return g_strdup ("");
125 #else
126         return g_strdup (GNOMELOCALEDIR);
127 #endif
128 }
129
130 gint
131 main (gint argc, gchar **argv)
132 {
133         GtkWidget       *window;
134         GOptionContext  *context;
135         const gchar     *filename;
136         EvDocumentModel *model;
137         GError          *error = NULL;
138
139 #ifdef G_OS_WIN32
140     if (fileno (stdout) != -1 &&
141           _get_osfhandle (fileno (stdout)) != -1)
142         {
143           /* stdout is fine, presumably redirected to a file or pipe */
144         }
145     else
146     {
147           typedef BOOL (* WINAPI AttachConsole_t) (DWORD);
148
149           AttachConsole_t p_AttachConsole =
150             (AttachConsole_t) GetProcAddress (GetModuleHandle ("kernel32.dll"), "AttachConsole");
151
152           if (p_AttachConsole != NULL && p_AttachConsole (ATTACH_PARENT_PROCESS))
153       {
154               freopen ("CONOUT$", "w", stdout);
155               dup2 (fileno (stdout), 1);
156               freopen ("CONOUT$", "w", stderr);
157               dup2 (fileno (stderr), 2);
158
159       }
160         }
161 #endif
162
163         /* Init glib threads asap */
164         if (!g_thread_supported ())
165                 g_thread_init (NULL);
166
167 #ifdef ENABLE_NLS
168         /* Initialize the i18n stuff */
169         gchar *tmp = ev_previewer_get_locale_dir ();
170         bindtextdomain (GETTEXT_PACKAGE, tmp);
171         g_free (tmp);
172         bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
173         textdomain (GETTEXT_PACKAGE);
174 #endif
175         
176         context = g_option_context_new (_("GNOME Document Previewer"));
177         g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
178         g_option_context_add_main_entries (context, goption_options, GETTEXT_PACKAGE);
179
180         g_option_context_add_group (context, gtk_get_option_group (TRUE));
181         
182         if (!g_option_context_parse (context, &argc, &argv, &error)) {
183                 g_warning ("Error parsing command line arguments: %s", error->message);
184                 g_error_free (error);
185                 g_option_context_free (context);
186
187                 return 1;
188         }
189         g_option_context_free (context);
190
191         if (!filenames) {
192                 g_warning ("File argument is required");
193                 
194                 return 1;
195         }
196
197         filename = filenames[0];
198         
199         if (!g_file_test (filename, G_FILE_TEST_IS_REGULAR)) {
200                 g_warning ("Filename \"%s\" does not exist or is not a regular file", filename);
201
202                 return 1;
203         }
204
205         if (!ev_init ())
206                 return 1;
207
208         ev_stock_icons_init ();
209
210         g_set_application_name (_("GNOME Document Previewer"));
211         gtk_window_set_default_icon_name ("evince");
212
213         model = ev_document_model_new ();
214         window = ev_previewer_window_new (model);
215         ev_previewer_window_set_source_file (EV_PREVIEWER_WINDOW (window), filename);
216         ev_previewer_window_set_print_settings (EV_PREVIEWER_WINDOW (window), print_settings);
217         g_signal_connect (window, "delete-event",
218                           G_CALLBACK (gtk_main_quit), NULL);
219         g_signal_connect (window, "destroy",
220                           G_CALLBACK (gtk_main_quit), NULL);
221         gtk_widget_show (window);
222
223         ev_previewer_load_document (filename, model);
224         
225         gtk_main ();
226
227         if (unlink_temp_file)
228                 ev_previewer_unlink_tempfile (filename);
229         if (print_settings)
230                 ev_previewer_unlink_tempfile (print_settings);
231
232         ev_shutdown ();
233         ev_stock_icons_shutdown ();
234         g_object_unref (model);
235         
236         return 0;
237 }