]> www.fi.muni.cz Git - evince.git/blob - previewer/ev-previewer.c
a425037cad3dd612d9a13f365447bd2e4ea6f2f0
[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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, 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 #include "ev-stock-icons.h"
28
29 #include "ev-previewer-window.h"
30
31 static gboolean      unlink_temp_file = FALSE;
32 static const gchar  *print_settings;
33 static const gchar **filenames;
34
35 static const GOptionEntry goption_options[] = {
36         { "unlink-tempfile", 'u', 0, G_OPTION_ARG_NONE, &unlink_temp_file, N_("Delete the temporary file"), NULL },
37         { "print-settings", 'p', 0, G_OPTION_ARG_FILENAME, &print_settings, N_("Print settings file"), N_("FILE") },
38         { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, NULL, N_("FILE") },
39         { NULL }
40 };
41
42 static void
43 ev_previewer_unlink_tempfile (const gchar *filename)
44 {
45         GFile *file, *tempdir;
46
47         file = g_file_new_for_path (filename);
48         tempdir = g_file_new_for_path (g_get_tmp_dir ());
49
50         if (g_file_has_prefix (file, tempdir)) {
51                 g_file_delete (file, NULL, NULL);
52         }
53
54         g_object_unref (file);
55         g_object_unref (tempdir);
56 }
57
58 static void
59 ev_previewer_load_job_finished (EvJob             *job,
60                                 EvPreviewerWindow *window)
61 {
62         if (ev_job_is_failed (job)) {
63                 g_warning ("%s", job->error->message);
64                 g_object_unref (job);
65
66                 return;
67         }
68
69         ev_previewer_window_set_document (window, job->document);
70         g_object_unref (job);
71 }
72
73 static void
74 ev_previewer_load_document (const gchar       *filename,
75                             EvPreviewerWindow *window)
76 {
77         EvJob *job;
78         gchar *uri;
79
80         uri = g_filename_to_uri (filename, NULL, NULL);
81         job = ev_job_load_new (uri);
82         g_signal_connect (job, "finished",
83                           G_CALLBACK (ev_previewer_load_job_finished),
84                           window);
85         ev_job_scheduler_push_job (job, EV_JOB_PRIORITY_NONE);
86         g_free (uri);
87 }
88
89 gint
90 main (gint argc, gchar **argv)
91 {
92         GtkWidget      *window;
93         GOptionContext *context;
94         const gchar    *filename;
95         GError         *error = NULL;
96
97         /* Init glib threads asap */
98         if (!g_thread_supported ())
99                 g_thread_init (NULL);
100
101 #ifdef ENABLE_NLS
102         /* Initialize the i18n stuff */
103         bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
104         bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
105         textdomain (GETTEXT_PACKAGE);
106 #endif
107         
108         context = g_option_context_new (_("GNOME Document Previewer"));
109         g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
110         g_option_context_add_main_entries (context, goption_options, GETTEXT_PACKAGE);
111
112         g_option_context_add_group (context, gtk_get_option_group (TRUE));
113         
114         if (!g_option_context_parse (context, &argc, &argv, &error)) {
115                 g_warning ("Error parsing command line arguments: %s", error->message);
116                 g_error_free (error);
117                 g_option_context_free (context);
118
119                 return 1;
120         }
121         g_option_context_free (context);
122
123         if (!filenames) {
124                 g_warning ("File argument is required");
125                 
126                 return 1;
127         }
128
129         filename = filenames[0];
130         
131         if (!g_file_test (filename, G_FILE_TEST_IS_REGULAR)) {
132                 g_warning ("Filename \"%s\" does not exist or is not a regular file", filename);
133
134                 return 1;
135         }
136
137         if (!ev_init ())
138                 return 1;
139
140         ev_stock_icons_init ();
141
142         g_set_application_name (_("GNOME Document Previewer"));
143         gtk_window_set_default_icon_name ("evince");
144
145         window = ev_previewer_window_new ();
146         ev_previewer_window_set_source_file (EV_PREVIEWER_WINDOW (window), filename);
147         ev_previewer_window_set_print_settings (EV_PREVIEWER_WINDOW (window), print_settings);
148         g_signal_connect (window, "delete-event",
149                           G_CALLBACK (gtk_main_quit), NULL);
150         g_signal_connect (window, "destroy",
151                           G_CALLBACK (gtk_main_quit), NULL);
152         gtk_widget_show (window);
153
154         ev_previewer_load_document (filename, EV_PREVIEWER_WINDOW (window));
155         
156         gtk_main ();
157
158         if (unlink_temp_file)
159                 ev_previewer_unlink_tempfile (filename);
160         if (print_settings)
161                 ev_previewer_unlink_tempfile (print_settings);
162
163         ev_shutdown ();
164         ev_stock_icons_shutdown ();
165         
166         return 0;
167 }