PageRenderTime 35ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/evince-3.5.4/previewer/ev-previewer.c

#
C | 201 lines | 141 code | 39 blank | 21 comment | 14 complexity | bbad213319daae3d4290fcb551eff63a MD5 | raw file
Possible License(s): GPL-2.0
  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. #include <config.h>
  21. #include <gtk/gtk.h>
  22. #include <glib/gi18n.h>
  23. #include <evince-document.h>
  24. #include <evince-view.h>
  25. #include "ev-previewer-window.h"
  26. #ifdef G_OS_WIN32
  27. #include <io.h>
  28. #include <conio.h>
  29. #if !(_WIN32_WINNT >= 0x0500)
  30. #error "_WIN32_WINNT must be defined >= 0x0500"
  31. #endif
  32. #include <windows.h>
  33. #endif
  34. static gboolean unlink_temp_file = FALSE;
  35. static const gchar *print_settings;
  36. static const gchar **filenames;
  37. static const GOptionEntry goption_options[] = {
  38. { "unlink-tempfile", 'u', 0, G_OPTION_ARG_NONE, &unlink_temp_file, N_("Delete the temporary file"), NULL },
  39. { "print-settings", 'p', 0, G_OPTION_ARG_FILENAME, &print_settings, N_("Print settings file"), N_("FILE") },
  40. { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, NULL, N_("FILE") },
  41. { NULL }
  42. };
  43. static void
  44. ev_previewer_unlink_tempfile (const gchar *filename)
  45. {
  46. GFile *file, *tempdir;
  47. file = g_file_new_for_path (filename);
  48. tempdir = g_file_new_for_path (g_get_tmp_dir ());
  49. if (g_file_has_prefix (file, tempdir)) {
  50. g_file_delete (file, NULL, NULL);
  51. }
  52. g_object_unref (file);
  53. g_object_unref (tempdir);
  54. }
  55. static void
  56. ev_previewer_load_job_finished (EvJob *job,
  57. EvDocumentModel *model)
  58. {
  59. if (ev_job_is_failed (job)) {
  60. g_warning ("%s", job->error->message);
  61. g_object_unref (job);
  62. return;
  63. }
  64. ev_document_model_set_document (model, job->document);
  65. g_object_unref (job);
  66. }
  67. static void
  68. ev_previewer_load_document (const gchar *filename,
  69. EvDocumentModel *model)
  70. {
  71. EvJob *job;
  72. gchar *uri;
  73. GFile *file;
  74. file = g_file_new_for_commandline_arg (filename);
  75. uri = g_file_get_uri (file);
  76. g_object_unref (file);
  77. job = ev_job_load_new (uri);
  78. g_signal_connect (job, "finished",
  79. G_CALLBACK (ev_previewer_load_job_finished),
  80. model);
  81. ev_job_scheduler_push_job (job, EV_JOB_PRIORITY_NONE);
  82. g_free (uri);
  83. }
  84. gint
  85. main (gint argc, gchar **argv)
  86. {
  87. GtkWidget *window;
  88. GOptionContext *context;
  89. const gchar *filename;
  90. EvDocumentModel *model;
  91. GError *error = NULL;
  92. #ifdef G_OS_WIN32
  93. if (fileno (stdout) != -1 &&
  94. _get_osfhandle (fileno (stdout)) != -1)
  95. {
  96. /* stdout is fine, presumably redirected to a file or pipe */
  97. }
  98. else
  99. {
  100. typedef BOOL (* WINAPI AttachConsole_t) (DWORD);
  101. AttachConsole_t p_AttachConsole =
  102. (AttachConsole_t) GetProcAddress (GetModuleHandle ("kernel32.dll"), "AttachConsole");
  103. if (p_AttachConsole != NULL && p_AttachConsole (ATTACH_PARENT_PROCESS))
  104. {
  105. freopen ("CONOUT$", "w", stdout);
  106. dup2 (fileno (stdout), 1);
  107. freopen ("CONOUT$", "w", stderr);
  108. dup2 (fileno (stderr), 2);
  109. }
  110. }
  111. #endif
  112. #ifdef ENABLE_NLS
  113. /* Initialize the i18n stuff */
  114. bindtextdomain (GETTEXT_PACKAGE, ev_get_locale_dir());
  115. bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
  116. textdomain (GETTEXT_PACKAGE);
  117. #endif
  118. context = g_option_context_new (_("GNOME Document Previewer"));
  119. g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
  120. g_option_context_add_main_entries (context, goption_options, GETTEXT_PACKAGE);
  121. g_option_context_add_group (context, gtk_get_option_group (TRUE));
  122. if (!g_option_context_parse (context, &argc, &argv, &error)) {
  123. g_warning ("Error parsing command line arguments: %s", error->message);
  124. g_error_free (error);
  125. g_option_context_free (context);
  126. return 1;
  127. }
  128. g_option_context_free (context);
  129. if (!filenames) {
  130. g_warning ("File argument is required");
  131. return 1;
  132. }
  133. filename = filenames[0];
  134. if (!g_file_test (filename, G_FILE_TEST_IS_REGULAR)) {
  135. g_warning ("Filename \"%s\" does not exist or is not a regular file", filename);
  136. return 1;
  137. }
  138. if (!ev_init ())
  139. return 1;
  140. ev_stock_icons_init ();
  141. g_set_application_name (_("GNOME Document Previewer"));
  142. gtk_window_set_default_icon_name ("evince");
  143. model = ev_document_model_new ();
  144. window = ev_previewer_window_new (model);
  145. ev_previewer_window_set_source_file (EV_PREVIEWER_WINDOW (window), filename);
  146. ev_previewer_window_set_print_settings (EV_PREVIEWER_WINDOW (window), print_settings);
  147. g_signal_connect (window, "delete-event",
  148. G_CALLBACK (gtk_main_quit), NULL);
  149. g_signal_connect (window, "destroy",
  150. G_CALLBACK (gtk_main_quit), NULL);
  151. gtk_widget_show (window);
  152. ev_previewer_load_document (filename, model);
  153. gtk_main ();
  154. if (unlink_temp_file)
  155. ev_previewer_unlink_tempfile (filename);
  156. if (print_settings)
  157. ev_previewer_unlink_tempfile (print_settings);
  158. ev_shutdown ();
  159. ev_stock_icons_shutdown ();
  160. g_object_unref (model);
  161. return 0;
  162. }