]> www.fi.muni.cz Git - evince.git/blob - shell/main.c
[windows] Properly define the _WIN32_WINNT version
[evince.git] / shell / main.c
1 /*
2  *  Copyright (C) 2004 Marco Pesenti Gritti
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2, or (at your option)
7  *  any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  */
19
20 #include "config.h"
21
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include <glib/gstdio.h>
26 #include <glib/gi18n.h>
27 #include <gtk/gtk.h>
28
29 #include "ev-application.h"
30 #include "ev-debug.h"
31 #include "ev-init.h"
32 #include "ev-file-helpers.h"
33 #include "ev-stock-icons.h"
34 #include "ev-metadata.h"
35
36 #ifdef WITH_SMCLIENT
37 #include "eggsmclient.h"
38 #ifdef GDK_WINDOWING_X11
39 #include "eggdesktopfile.h"
40 #endif
41 #endif /* WITH_SMCLIENT */
42
43 #ifdef G_OS_WIN32
44 #ifdef DATADIR
45 #undef DATADIR
46 #endif
47 #include <io.h>
48 #include <conio.h>
49 #if !(_WIN32_WINNT >= 0x0500)
50 #error "_WIN32_WINNT must be defined >= 0x0500"
51 #endif
52 #include <windows.h>
53 #endif
54
55 static gchar   *ev_page_label;
56 static gchar   *ev_find_string;
57 static gboolean preview_mode = FALSE;
58 static gboolean fullscreen_mode = FALSE;
59 static gboolean presentation_mode = FALSE;
60 static gboolean unlink_temp_file = FALSE;
61 static gchar   *print_settings;
62 static const char **file_arguments = NULL;
63
64 static gboolean
65 option_version_cb (const gchar *option_name,
66                    const gchar *value,
67                    gpointer     data,
68                    GError     **error)
69 {
70   g_print ("%s %s\n", _("GNOME Document Viewer"), VERSION);
71
72   exit (0);
73   return FALSE;
74 }
75
76 static const GOptionEntry goption_options[] =
77 {
78         { "page-label", 'p', 0, G_OPTION_ARG_STRING, &ev_page_label, N_("The page of the document to display."), N_("PAGE")},
79         { "fullscreen", 'f', 0, G_OPTION_ARG_NONE, &fullscreen_mode, N_("Run evince in fullscreen mode"), NULL },
80         { "presentation", 's', 0, G_OPTION_ARG_NONE, &presentation_mode, N_("Run evince in presentation mode"), NULL },
81         { "preview", 'w', 0, G_OPTION_ARG_NONE, &preview_mode, N_("Run evince as a previewer"), NULL },
82         { "find", 'l', 0, G_OPTION_ARG_STRING, &ev_find_string, N_("The word or phrase to find in the document"), N_("STRING")},
83         { "unlink-tempfile", 'u', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &unlink_temp_file, NULL, NULL },
84         { "print-settings", 't', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_FILENAME, &print_settings, NULL, NULL },
85         { "version", 0, G_OPTION_FLAG_NO_ARG | G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_CALLBACK, option_version_cb, NULL, NULL },
86         { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &file_arguments, NULL, N_("[FILE...]") },
87         { NULL }
88 };
89
90 static gboolean
91 launch_previewer (void)
92 {
93         GString *cmd_str;
94         gchar   *cmd;
95         gint     argc;
96         gchar  **argv;
97         gboolean retval = FALSE;
98         GError  *error = NULL;
99
100         /* Rebuild the command line, ignoring options
101          * not supported by the previewer and taking only
102          * the first path given
103          */
104         cmd_str = g_string_new ("evince-previewer");
105                 
106         if (print_settings) {
107                 gchar *quoted;
108
109                 quoted = g_shell_quote (print_settings);
110                 g_string_append_printf (cmd_str, " --print-settings %s", quoted);
111                 g_free (quoted);
112         }
113
114         if (unlink_temp_file)
115                 g_string_append (cmd_str, " --unlink-tempfile");
116
117         if (file_arguments) {
118                 gchar *quoted;
119                 
120                 quoted = g_shell_quote (file_arguments[0]);
121                 g_string_append_printf (cmd_str, " %s", quoted);
122                 g_free (quoted);
123         }
124
125         cmd = g_string_free (cmd_str, FALSE);
126         g_shell_parse_argv (cmd, &argc, &argv, &error);
127         g_free (cmd);
128         
129         if (!error) {
130                 retval = gdk_spawn_on_screen (gdk_screen_get_default (),
131                                               NULL, argv, NULL,
132                                               G_SPAWN_SEARCH_PATH,
133                                               NULL, NULL, NULL,
134                                               &error);
135                 g_strfreev (argv);
136         }
137
138         if (error) {
139                 g_warning ("Error launching previewer: %s\n", error->message);
140                 g_error_free (error);
141         }
142
143         return retval;
144 }
145
146 static void
147 load_files (const char **files)
148 {
149         GdkScreen       *screen = gdk_screen_get_default ();
150         EvWindowRunMode  mode = EV_WINDOW_MODE_NORMAL;
151         gint             i;
152         EvLinkDest      *global_dest = NULL;
153
154         if (!files) {
155                 if (!ev_application_has_window (EV_APP))
156                         ev_application_open_window (EV_APP, screen, GDK_CURRENT_TIME);
157                 return;
158         }
159
160         if (ev_page_label)
161                 global_dest = ev_link_dest_new_page_label (ev_page_label);
162
163         if (fullscreen_mode)
164                 mode = EV_WINDOW_MODE_FULLSCREEN;
165         else if (presentation_mode)
166                 mode = EV_WINDOW_MODE_PRESENTATION;
167
168         for (i = 0; files[i]; i++) {
169                 gchar       *uri;
170                 gchar       *label;
171                 GFile       *file;
172                 EvLinkDest  *dest = NULL;
173                 const gchar *app_uri;
174
175                 file = g_file_new_for_commandline_arg (files[i]);
176                 uri = g_file_get_uri (file);
177                 g_object_unref (file);
178
179                 app_uri = ev_application_get_uri (EV_APP);
180                 if (app_uri && strcmp (app_uri, uri) == 0) {
181                         g_free (uri);
182                         continue;
183                 }
184
185                 label = strchr (uri, '#');
186                 if (label) {
187                         *label = 0;
188                         label++;
189                         dest = ev_link_dest_new_page_label (label);
190                 } else if (global_dest) {
191                         dest = g_object_ref (global_dest);
192                 }
193
194                 ev_application_open_uri_at_dest (EV_APP, uri, screen, dest,
195                                                  mode, ev_find_string,
196                                                  GDK_CURRENT_TIME);
197
198                 if (dest)
199                         g_object_unref (dest);
200                 g_free (uri);
201         }
202 }
203
204 int
205 main (int argc, char *argv[])
206 {
207         GOptionContext *context;
208         GError         *error = NULL;
209
210 #ifdef G_OS_WIN32
211
212     if (fileno (stdout) != -1 &&
213           _get_osfhandle (fileno (stdout)) != -1)
214         {
215           /* stdout is fine, presumably redirected to a file or pipe */
216         }
217     else
218     {
219           typedef BOOL (* WINAPI AttachConsole_t) (DWORD);
220
221           AttachConsole_t p_AttachConsole =
222             (AttachConsole_t) GetProcAddress (GetModuleHandle ("kernel32.dll"), "AttachConsole");
223
224           if (p_AttachConsole != NULL && p_AttachConsole (ATTACH_PARENT_PROCESS))
225       {
226               freopen ("CONOUT$", "w", stdout);
227               dup2 (fileno (stdout), 1);
228               freopen ("CONOUT$", "w", stderr);
229               dup2 (fileno (stderr), 2);
230
231       }
232         }
233 #endif
234
235         /* Init glib threads asap */
236         if (!g_thread_supported ())
237                 g_thread_init (NULL);
238
239 #ifdef ENABLE_NLS
240         /* Initialize the i18n stuff */
241         bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
242         bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
243         textdomain (GETTEXT_PACKAGE);
244 #endif
245
246         context = g_option_context_new (N_("GNOME Document Viewer"));
247         g_option_context_set_translation_domain(context, GETTEXT_PACKAGE);
248         g_option_context_add_main_entries (context, goption_options, GETTEXT_PACKAGE);
249
250 #ifdef WITH_SMCLIENT
251         g_option_context_add_group (context, egg_sm_client_get_option_group ());
252 #endif
253
254         g_option_context_add_group (context, gtk_get_option_group (TRUE));
255
256         if (!g_option_context_parse (context, &argc, &argv, &error)) {
257                 g_printerr ("Cannot parse arguments: %s\n", error->message);
258                 g_error_free (error);
259                 g_option_context_free (context);
260
261                 return 1;
262         }
263         g_option_context_free (context);
264
265         if (preview_mode) {
266                 gboolean retval;
267                 
268                 retval = launch_previewer ();
269                 
270                 return retval ? 0 : 1;
271         }
272
273         if (!ev_init ())
274                 return 1;
275
276         ev_stock_icons_init ();
277
278 #if defined(WITH_SMCLIENT) && defined(GDK_WINDOWING_X11)
279         egg_set_desktop_file (GNOMEDATADIR "/applications/evince.desktop");
280 #else
281         /* Manually set name and icon */
282         g_set_application_name (_("Document Viewer"));
283         gtk_window_set_default_icon_name ("evince");
284 #endif /* WITH_SMCLIENT && GDK_WINDOWING_X11 */
285
286         ev_application_load_session (EV_APP);
287         load_files (file_arguments);
288         if (ev_application_has_window (EV_APP)) {
289                 /* Change directory so we don't prevent unmounting in case the initial cwd
290                  * is on an external device (see bug #575436)
291                  */
292                 g_chdir (g_get_home_dir ());
293
294                 gtk_main ();
295         }
296
297         ev_shutdown ();
298         ev_stock_icons_shutdown ();
299
300         return 0;
301 }