]> www.fi.muni.cz Git - evince.git/blob - previewer/ev-previewer.c
[shell] fix Issues #2 #3 #4
[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 #include <io.h>
32 #include <conio.h>
33 #if !(_WIN32_WINNT >= 0x0500)
34 #error "_WIN32_WINNT must be defined >= 0x0500"
35 #endif
36 #include <windows.h>
37 #endif
38
39 static gboolean      unlink_temp_file = FALSE;
40 static const gchar  *print_settings;
41 static const gchar **filenames;
42
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") },
47         { NULL }
48 };
49
50 static void
51 ev_previewer_unlink_tempfile (const gchar *filename)
52 {
53         GFile *file, *tempdir;
54
55         file = g_file_new_for_path (filename);
56         tempdir = g_file_new_for_path (g_get_tmp_dir ());
57
58         if (g_file_has_prefix (file, tempdir)) {
59                 g_file_delete (file, NULL, NULL);
60         }
61
62         g_object_unref (file);
63         g_object_unref (tempdir);
64 }
65
66 static void
67 ev_previewer_load_job_finished (EvJob           *job,
68                                 EvDocumentModel *model)
69 {
70         if (ev_job_is_failed (job)) {
71                 g_warning ("%s", job->error->message);
72                 g_object_unref (job);
73
74                 return;
75         }
76         ev_document_model_set_document (model, job->document);
77         g_object_unref (job);
78 }
79
80 static void
81 ev_previewer_load_document (const gchar     *filename,
82                             EvDocumentModel *model)
83 {
84         EvJob *job;
85         gchar *uri;
86         GFile  *file;
87
88         file = g_file_new_for_commandline_arg (filename);
89         uri = g_file_get_uri (file);
90         g_object_unref (file);
91
92         job = ev_job_load_new (uri);
93         g_signal_connect (job, "finished",
94                           G_CALLBACK (ev_previewer_load_job_finished),
95                           model);
96         ev_job_scheduler_push_job (job, EV_JOB_PRIORITY_NONE);
97         g_free (uri);
98 }
99
100 gint
101 main (gint argc, gchar **argv)
102 {
103         GtkWidget       *window;
104         GOptionContext  *context;
105         const gchar     *filename;
106         EvDocumentModel *model;
107         GError          *error = NULL;
108
109 #ifdef G_OS_WIN32
110     if (fileno (stdout) != -1 &&
111           _get_osfhandle (fileno (stdout)) != -1)
112         {
113           /* stdout is fine, presumably redirected to a file or pipe */
114         }
115     else
116     {
117           typedef BOOL (* WINAPI AttachConsole_t) (DWORD);
118
119           AttachConsole_t p_AttachConsole =
120             (AttachConsole_t) GetProcAddress (GetModuleHandle ("kernel32.dll"), "AttachConsole");
121
122           if (p_AttachConsole != NULL && p_AttachConsole (ATTACH_PARENT_PROCESS))
123       {
124               freopen ("CONOUT$", "w", stdout);
125               dup2 (fileno (stdout), 1);
126               freopen ("CONOUT$", "w", stderr);
127               dup2 (fileno (stderr), 2);
128
129       }
130         }
131 #endif
132
133         /* Init glib threads asap */
134         if (!g_thread_supported ())
135                 g_thread_init (NULL);
136
137 #ifdef ENABLE_NLS
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);
142 #endif
143         
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);
147
148         g_option_context_add_group (context, gtk_get_option_group (TRUE));
149         
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);
154
155                 return 1;
156         }
157         g_option_context_free (context);
158
159         if (!filenames) {
160                 g_warning ("File argument is required");
161                 
162                 return 1;
163         }
164
165         filename = filenames[0];
166         
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);
169
170                 return 1;
171         }
172
173         if (!ev_init ())
174                 return 1;
175
176         ev_stock_icons_init ();
177
178         g_set_application_name (_("GNOME Document Previewer"));
179         gtk_window_set_default_icon_name ("evince");
180
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);
190
191         ev_previewer_load_document (filename, model);
192         
193         gtk_main ();
194
195         if (unlink_temp_file)
196                 ev_previewer_unlink_tempfile (filename);
197         if (print_settings)
198                 ev_previewer_unlink_tempfile (print_settings);
199
200         ev_shutdown ();
201         ev_stock_icons_shutdown ();
202         g_object_unref (model);
203         
204         return 0;
205 }