Evince
Evince is a document viewer capable of displaying multiple and single page document formats like PDF and Postscript.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
ev-previewer.c
Go to the documentation of this file.
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  * Copyright © 2012 Christian Persch
6  *
7  * Evince is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * Evince is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  */
21 
22 #include "config.h"
23 
24 #include <gtk/gtk.h>
25 #include <glib/gi18n.h>
26 #include <evince-document.h>
27 #include <evince-view.h>
28 
29 #include "ev-previewer-window.h"
30 
31 #ifdef G_OS_WIN32
32 #include <io.h>
33 #include <conio.h>
34 #if !(_WIN32_WINNT >= 0x0500)
35 #error "_WIN32_WINNT must be defined >= 0x0500"
36 #endif
37 #include <windows.h>
38 #endif
39 
40 static gboolean unlink_temp_file = FALSE;
41 static gchar *print_settings = NULL;
42 static EvPreviewerWindow *window = NULL;
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  { 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
68  EvDocumentModel *model)
69 {
70  if (ev_job_is_failed (job)) {
71  g_object_unref (job);
72  return;
73  }
75  g_object_unref (job);
76 }
77 
78 static void
80  EvDocumentModel *model)
81 {
82  EvJob *job;
83  gchar *uri;
84 
85  uri = g_file_get_uri (file);
86 
87  job = ev_job_load_new (uri);
88  g_signal_connect (job, "finished",
89  G_CALLBACK (ev_previewer_load_job_finished),
90  model);
92  g_free (uri);
93 }
94 
95 static void
96 activate_cb (GApplication *application,
97  gpointer user_data)
98 {
99  if (window) {
100  gtk_window_present (GTK_WINDOW (window));
101  }
102 }
103 
104 static void
105 open_cb (GApplication *application,
106  GFile **files,
107  gint n_files,
108  const gchar *hint,
109  gpointer user_data)
110 {
111  EvDocumentModel *model;
112  GFile *file;
113  char *path;
114 
115  if (n_files != 1) {
116  g_application_quit (application);
117  return;
118  }
119 
120  file = files[0];
121 
122  model = ev_document_model_new ();
123  ev_previewer_load_document (file, model);
124 
125  window = ev_previewer_window_new (model);
126  g_object_unref (model);
127 
129  path = g_file_get_path (file);
131  g_free (path);
132 
133  gtk_window_present (GTK_WINDOW (window));
134 }
135 
136 gint
137 main (gint argc, gchar **argv)
138 {
139  GtkApplication *application;
140  GOptionContext *context;
141  GError *error = NULL;
142  gchar *path;
143  int status = 1;
144 
145 #ifdef G_OS_WIN32
146  if (fileno (stdout) != -1 &&
147  _get_osfhandle (fileno (stdout)) != -1)
148  {
149  /* stdout is fine, presumably redirected to a file or pipe */
150  }
151  else
152  {
153  typedef BOOL (* WINAPI AttachConsole_t) (DWORD);
154 
155  AttachConsole_t p_AttachConsole =
156  (AttachConsole_t) GetProcAddress (GetModuleHandle ("kernel32.dll"), "AttachConsole");
157 
158  if (p_AttachConsole != NULL && p_AttachConsole (ATTACH_PARENT_PROCESS))
159  {
160  freopen ("CONOUT$", "w", stdout);
161  dup2 (fileno (stdout), 1);
162  freopen ("CONOUT$", "w", stderr);
163  dup2 (fileno (stderr), 2);
164 
165  }
166  }
167 #endif
168 
169 #ifdef ENABLE_NLS
170  /* Initialize the i18n stuff */
171  bindtextdomain (GETTEXT_PACKAGE, ev_get_locale_dir());
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_printerr ("Error parsing command line arguments: %s\n", error->message);
184  g_error_free (error);
185  g_option_context_free (context);
186  return 1;
187  }
188  g_option_context_free (context);
189 
190  if (argc < 2) {
191  g_printerr ("File argument is required\n");
192  return 1;
193  } else if (argc > 2) {
194  g_printerr ("Too many files\n");
195  return 1;
196  }
197 
198  path = g_filename_from_uri (argv[1], NULL, NULL);
199  if (!g_file_test (argv[1], G_FILE_TEST_IS_REGULAR) && !g_file_test (path, G_FILE_TEST_IS_REGULAR)) {
200  g_printerr ("Filename \"%s\" does not exist or is not a regular file\n", argv[1]);
201  return 1;
202  }
203  g_free (path);
204 
205  if (!ev_init ())
206  return 1;
207 
209 
210  g_set_application_name (_("GNOME Document Previewer"));
211  gtk_window_set_default_icon_name ("evince");
212 
213  application = gtk_application_new (NULL,
214  G_APPLICATION_NON_UNIQUE |
215  G_APPLICATION_HANDLES_OPEN);
216  g_signal_connect (application, "activate", G_CALLBACK (activate_cb), NULL);
217  g_signal_connect (application, "open", G_CALLBACK (open_cb), NULL);
218 
219  status = g_application_run (G_APPLICATION (application), argc, argv);
220 
221  if (unlink_temp_file)
223  if (print_settings)
225 
226  ev_shutdown ();
228 
229  return status;
230 }