]> www.fi.muni.cz Git - evince.git/blob - thumbnailer/evince-thumbnailer.c
On Windows, determine the data directory on runtime.
[evince.git] / thumbnailer / evince-thumbnailer.c
1 /*
2    Copyright (C) 2005 Fernando Herrera <fherrera@onirica.com>
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 of the License, or
7    (at your option) 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 #include <config.h>
20
21 #include <evince-document.h>
22
23 #include <gio/gio.h>
24
25 #include <stdlib.h>
26 #include <string.h>
27
28 #define THUMBNAIL_SIZE 128
29
30 static gint size = THUMBNAIL_SIZE;
31 static const gchar **file_arguments;
32
33 static const GOptionEntry goption_options[] = {
34         { "size", 's', 0, G_OPTION_ARG_INT, &size, NULL, "SIZE" },
35         { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &file_arguments, NULL, "<input> <ouput>" },
36         { NULL }
37 };
38
39 struct AsyncData {
40         EvDocument  *document;
41         const gchar *output;
42         gint         size;
43         gboolean     success;
44 };
45
46 static EvDocument *
47 evince_thumbnailer_get_document (const gchar *uri)
48 {
49         EvDocument *document = NULL;
50         GError     *error = NULL;
51
52         document = ev_document_factory_get_document  (uri, &error);
53         if (error) {
54                 if (error->domain == EV_DOCUMENT_ERROR &&
55                     error->code == EV_DOCUMENT_ERROR_ENCRYPTED) {
56                         /* FIXME: Create a thumb for cryp docs */
57                         g_error_free (error);
58                         return NULL;
59                 }
60                 g_error_free (error);
61                 return NULL;
62         }
63         
64         return document;
65 }
66
67 static gboolean
68 evince_thumbnail_pngenc_get (EvDocument *document, const char *thumbnail, int size)
69 {
70         EvRenderContext *rc;
71         double width, height;
72         GdkPixbuf *pixbuf;
73         EvPage *page;
74
75         page = ev_document_get_page (document, 0);
76         
77         ev_document_get_page_size (document, page, &width, &height);
78
79         rc = ev_render_context_new (page, 0, size / width);
80         pixbuf = ev_document_thumbnails_get_thumbnail (EV_DOCUMENT_THUMBNAILS (document),
81                                                        rc, FALSE);
82         g_object_unref (rc);
83         g_object_unref (page);
84         
85         if (pixbuf != NULL) {
86                 const char *overlaid_icon_name = NULL;
87
88                 if (overlaid_icon_name) {
89                         GdkPixbuf *overlaid_pixbuf;
90
91 #ifdef G_OS_WIN32
92                         gchar *dir = g_win32_get_package_installation_directory_of_module (NULL);
93                         gchar *overlaid_icon_path = g_build_filename (dir, "share", "evince", overlaid_icon_name, NULL);
94                         g_free (dir);
95 #else
96                         gchar *overlaid_icon_path = g_strdup_printf ("%s/%s", DATADIR, overlaid_icon_name);
97 #endif
98                         overlaid_pixbuf = gdk_pixbuf_new_from_file (overlaid_icon_path, NULL);
99                         g_free (overlaid_icon_path);
100                         if (overlaid_pixbuf != NULL) {
101                                 int delta_height, delta_width;
102                                 
103                                 delta_width = gdk_pixbuf_get_width (pixbuf) -
104                                         gdk_pixbuf_get_width (overlaid_pixbuf);
105                                 delta_height = gdk_pixbuf_get_height (pixbuf) -
106                                         gdk_pixbuf_get_height (overlaid_pixbuf);
107                                 
108                                 gdk_pixbuf_composite (overlaid_pixbuf, pixbuf,
109                                                       delta_width, delta_height,
110                                                       gdk_pixbuf_get_width (overlaid_pixbuf),
111                                                       gdk_pixbuf_get_height (overlaid_pixbuf),
112                                                       delta_width, delta_height,
113                                                       1, 1,
114                                                       GDK_INTERP_NEAREST, 100);
115                                 
116                                 g_object_unref  (overlaid_pixbuf);
117                         }
118                 }
119                 
120                 if (gdk_pixbuf_save (pixbuf, thumbnail, "png", NULL, NULL)) {
121                         g_object_unref  (pixbuf);
122                         return TRUE;
123                 }
124
125                 g_object_unref  (pixbuf);
126         }
127         
128         return FALSE;
129 }
130
131 static gpointer
132 evince_thumbnail_pngenc_get_async (struct AsyncData *data)
133 {
134         ev_document_doc_mutex_lock ();
135         data->success = evince_thumbnail_pngenc_get (data->document,
136                                                      data->output,
137                                                      data->size);
138         ev_document_doc_mutex_unlock ();
139         
140         g_idle_add ((GSourceFunc)gtk_main_quit, NULL);
141         
142         return NULL;
143 }
144
145 static void
146 print_usage (GOptionContext *context)
147 {
148         gchar *help;
149
150         help = g_option_context_get_help (context, TRUE, NULL);
151         g_print ("%s", help);
152         g_free (help);
153 }
154
155 int
156 main (int argc, char *argv[])
157 {
158         EvDocument     *document;
159         GOptionContext *context;
160         const char     *input;
161         const char     *output;
162         char           *uri;
163         GFile          *file;
164         GError         *error = NULL;
165
166         context = g_option_context_new ("- GNOME Document Thumbnailer");
167         g_option_context_add_main_entries (context, goption_options, NULL);
168
169         if (!g_option_context_parse (context, &argc, &argv, &error)) {
170                 g_printerr ("%s\n", error->message);
171                 g_error_free (error);
172                 print_usage (context);
173                 g_option_context_free (context);
174
175                 return -1;
176         }
177
178         input = file_arguments ? file_arguments[0] : NULL;
179         output = input ? file_arguments[1] : NULL;
180         if (!input || !output) {
181                 print_usage (context);
182                 g_option_context_free (context);
183
184                 return -1;
185         }
186         
187         g_option_context_free (context);
188
189         if (size < 1) {
190                 g_print ("Size cannot be smaller than 1 pixel\n");
191                 return -1;
192         }
193
194         input = file_arguments[0];
195         output = file_arguments[1];
196
197         g_type_init ();
198
199         if (!g_thread_supported ())
200                 g_thread_init (NULL);
201
202         if (!ev_init ())
203                 return -1;
204
205         file = g_file_new_for_commandline_arg (input);
206         uri = g_file_get_uri (file);
207         document = evince_thumbnailer_get_document (uri);
208
209         g_object_unref (file);
210         g_free (uri);
211
212         if (!document) {
213                 ev_shutdown ();
214                 return -2;
215         }
216
217         if (!EV_IS_DOCUMENT_THUMBNAILS (document)) {
218                 g_object_unref (document);
219                 ev_shutdown ();
220                 return -2;
221         }
222
223         if (EV_IS_ASYNC_RENDERER (document)) {
224                 struct AsyncData data;
225
226                 gtk_init (&argc, &argv);
227                 
228                 data.document = document;
229                 data.output = output;
230                 data.size = size;
231
232                 g_thread_create ((GThreadFunc) evince_thumbnail_pngenc_get_async,
233                                  &data, FALSE, NULL);
234                 
235                 gtk_main ();
236
237                 g_object_unref (document);
238                 ev_shutdown ();
239
240                 return data.success ? 0 : -2;
241         }
242
243         if (!evince_thumbnail_pngenc_get (document, output, size)) {
244                 g_object_unref (document);
245                 ev_shutdown ();
246                 return -2;
247         }
248
249         g_object_unref (document);
250         ev_shutdown ();
251
252         return 0;
253 }