]> www.fi.muni.cz Git - evince.git/blob - backend/djvu/djvu-document.c
Make sure load job doesn't finish successfully when the document is not
[evince.git] / backend / djvu / djvu-document.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
2 /*
3  * Copyright (C) 2005, Nickolay V. Shmyrev <nshmyrev@yandex.ru>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #include "config.h"
21
22 #include <config.h>
23 #include "djvu-document.h"
24 #include "djvu-text.h"
25 #include "djvu-links.h"
26 #include "djvu-document-private.h"
27 #include "ev-document-thumbnails.h"
28 #include "ev-file-exporter.h"
29 #include "ev-document-misc.h"
30 #include "ev-document-find.h"
31 #include "ev-document-links.h"
32 #include "ev-selection.h"
33 #include "ev-file-helpers.h"
34
35 #include <gdk-pixbuf/gdk-pixbuf-core.h>
36 #include <glib/gi18n.h>
37 #include <glib/gunicode.h>
38 #include <string.h>
39
40 #define SCALE_FACTOR 0.2
41
42 enum {
43         PROP_0,
44         PROP_TITLE
45 };
46
47 struct _DjvuDocumentClass
48 {
49         GObjectClass parent_class;
50 };
51
52 typedef struct _DjvuDocumentClass DjvuDocumentClass;
53
54 static void djvu_document_document_iface_init (EvDocumentIface *iface);
55 static void djvu_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
56 static void djvu_document_file_exporter_iface_init (EvFileExporterIface *iface);
57 static void djvu_document_find_iface_init (EvDocumentFindIface *iface);
58 static void djvu_document_document_links_iface_init  (EvDocumentLinksIface *iface);
59 static void djvu_selection_iface_init (EvSelectionIface *iface);
60
61 EV_BACKEND_REGISTER_WITH_CODE (DjvuDocument, djvu_document,
62     {
63       EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS, djvu_document_document_thumbnails_iface_init);
64       EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER, djvu_document_file_exporter_iface_init);
65       EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_FIND, djvu_document_find_iface_init);
66       EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LINKS, djvu_document_document_links_iface_init);
67       EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_SELECTION, djvu_selection_iface_init);
68      });
69
70
71 #define EV_DJVU_ERROR ev_djvu_error_quark ()
72
73 static GQuark
74 ev_djvu_error_quark (void)
75 {
76         static GQuark q = 0;
77         if (q == 0)
78                 q = g_quark_from_static_string ("ev-djvu-quark");
79         
80         return q;
81 }
82
83 static void
84 handle_message (const ddjvu_message_t *msg, GError **error)
85 {
86         switch (msg->m_any.tag) {
87                 case DDJVU_ERROR: {
88                         gchar *error_str;
89                         
90                         if (msg->m_error.filename) {
91                                 error_str = g_strdup_printf ("DjvuLibre error: %s:%d",
92                                                              msg->m_error.filename,
93                                                              msg->m_error.lineno);
94                         } else {
95                                 error_str = g_strdup_printf ("DjvuLibre error: %s",
96                                                              msg->m_error.message);
97                         }
98                         
99                         if (error) {
100                                 g_set_error (error, EV_DJVU_ERROR, 0, error_str);
101                         } else {
102                                 g_warning (error_str);
103                         }
104                                 
105                         g_free (error_str);
106                         return;
107                         }                                                    
108                         break;
109                 default:
110                         break;
111         }
112 }
113
114 void
115 djvu_handle_events (DjvuDocument *djvu_document, int wait, GError **error)
116 {
117         ddjvu_context_t *ctx = djvu_document->d_context;
118         const ddjvu_message_t *msg;
119         
120         if (!ctx)
121                 return;
122
123         if (wait)
124                 ddjvu_message_wait (ctx);
125
126         while ((msg = ddjvu_message_peek (ctx))) {
127                 handle_message (msg, error);
128                 ddjvu_message_pop (ctx);
129                 if (error && *error)
130                         return;
131         }
132 }
133
134 static void
135 djvu_wait_for_message (DjvuDocument *djvu_document, ddjvu_message_tag_t message, GError **error)
136 {
137         ddjvu_context_t *ctx = djvu_document->d_context;
138         const ddjvu_message_t *msg;
139
140         ddjvu_message_wait (ctx);
141         while ((msg = ddjvu_message_peek (ctx)) && (msg->m_any.tag != message)) {
142                 handle_message (msg, error);
143                 ddjvu_message_pop (ctx);
144                 if (error && *error)
145                         return;
146         }
147         if (msg && msg->m_any.tag == message)
148                 ddjvu_message_pop (ctx);
149 }
150
151 static gboolean
152 djvu_document_load (EvDocument  *document,
153                     const char  *uri,
154                     GError     **error)
155 {
156         DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
157         ddjvu_document_t *doc;
158         gchar *filename;
159         gboolean missing_files = FALSE;
160         GError *djvu_error = NULL;
161
162         /* FIXME: We could actually load uris  */
163         filename = g_filename_from_uri (uri, NULL, error);
164         if (!filename)
165                 return FALSE;
166         
167         doc = ddjvu_document_create_by_filename (djvu_document->d_context, filename, TRUE);
168
169         if (!doc) {
170                 g_free (filename);
171                 return FALSE;
172         }
173
174         if (djvu_document->d_document)
175             ddjvu_document_release (djvu_document->d_document);
176
177         djvu_document->d_document = doc;
178
179         djvu_wait_for_message (djvu_document, DDJVU_DOCINFO, &djvu_error);
180         if (djvu_error) {
181                 g_set_error (error,
182                              EV_DOCUMENT_ERROR,
183                              EV_DOCUMENT_ERROR_INVALID,
184                              djvu_error->message);
185                 g_error_free (djvu_error);
186                 g_free (filename);
187                 ddjvu_document_release (djvu_document->d_document);
188                 djvu_document->d_document = NULL;
189
190                 return FALSE;
191         }
192
193         if (ddjvu_document_decoding_error (djvu_document->d_document))
194                 djvu_handle_events (djvu_document, TRUE, &djvu_error);
195
196         if (djvu_error) {
197                 g_set_error (error,
198                              EV_DOCUMENT_ERROR,
199                              EV_DOCUMENT_ERROR_INVALID,
200                              djvu_error->message);
201                 g_error_free (djvu_error);
202                 g_free (filename);
203                 ddjvu_document_release (djvu_document->d_document);
204                 djvu_document->d_document = NULL;
205                 
206                 return FALSE;
207         }
208         
209         g_free (djvu_document->uri);
210         djvu_document->uri = g_strdup (uri);
211
212         if (ddjvu_document_get_type (djvu_document->d_document) == DDJVU_DOCTYPE_INDIRECT) {
213                 gint n_files;
214                 gint i;
215                 gchar *base;
216
217                 base = g_path_get_dirname (filename);
218
219                 n_files = ddjvu_document_get_filenum (djvu_document->d_document);
220                 for (i = 0; i < n_files; i++) {
221                         struct ddjvu_fileinfo_s fileinfo;
222                         gchar *file;
223                         
224                         ddjvu_document_get_fileinfo (djvu_document->d_document,
225                                                      i, &fileinfo);
226
227                         if (fileinfo.type != 'P')
228                                 continue;
229
230                         file = g_build_filename (base, fileinfo.id, NULL);
231                         if (!g_file_test (file, G_FILE_TEST_EXISTS)) {
232                                 missing_files = TRUE;
233                                 g_free (file);
234                                 
235                                 break;
236                         }
237                         g_free (file);
238                 }
239                 g_free (base);
240         }
241         g_free (filename);
242
243         if (missing_files) {
244                 g_set_error (error,
245                              G_FILE_ERROR,
246                              G_FILE_ERROR_EXIST,
247                              _("The document is composed by several files. "
248                                "One or more of such files cannot be accessed."));
249
250                 return FALSE;
251         }
252
253         return TRUE;
254 }
255
256
257 static gboolean
258 djvu_document_save (EvDocument  *document,
259                     const char  *uri,
260                     GError     **error)
261 {
262         DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
263
264         return ev_xfer_uri_simple (djvu_document->uri, uri, error);
265 }
266
267 int
268 djvu_document_get_n_pages (EvDocument  *document)
269 {
270         DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
271         
272         g_return_val_if_fail (djvu_document->d_document, 0);
273         
274         return ddjvu_document_get_pagenum (djvu_document->d_document);
275 }
276
277 static void
278 document_get_page_size (DjvuDocument *djvu_document,
279                         gint          page,
280                         double       *width,
281                         double       *height)
282 {
283         ddjvu_pageinfo_t info;
284         ddjvu_status_t r;
285         
286         while ((r = ddjvu_document_get_pageinfo(djvu_document->d_document, page, &info)) < DDJVU_JOB_OK)
287                 djvu_handle_events(djvu_document, TRUE, NULL);
288         
289         if (r >= DDJVU_JOB_FAILED)
290                 djvu_handle_events(djvu_document, TRUE, NULL);
291
292         *width = info.width * SCALE_FACTOR; 
293         *height = info.height * SCALE_FACTOR;
294 }
295
296 static void
297 djvu_document_get_page_size (EvDocument   *document,
298                              EvPage       *page,
299                              double       *width,
300                              double       *height)
301 {
302         DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
303
304         g_return_if_fail (djvu_document->d_document);
305
306         document_get_page_size (djvu_document, page->index,
307                                 width, height);
308 }
309
310 static cairo_surface_t *
311 djvu_document_render (EvDocument      *document, 
312                       EvRenderContext *rc)
313 {
314         DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
315         cairo_surface_t *surface;
316         gchar *pixels;
317         gint   rowstride;
318         ddjvu_rect_t rrect;
319         ddjvu_rect_t prect;
320         ddjvu_page_t *d_page;
321         ddjvu_page_rotation_t rotation;
322         double page_width, page_height, tmp;
323         static const cairo_user_data_key_t key;
324
325         d_page = ddjvu_page_create_by_pageno (djvu_document->d_document, rc->page->index);
326         
327         while (!ddjvu_page_decoding_done (d_page))
328                 djvu_handle_events(djvu_document, TRUE, NULL);
329
330         page_width = ddjvu_page_get_width (d_page) * rc->scale * SCALE_FACTOR + 0.5;
331         page_height = ddjvu_page_get_height (d_page) * rc->scale * SCALE_FACTOR + 0.5;
332         
333         switch (rc->rotation) {
334                 case 90:
335                         rotation = DDJVU_ROTATE_90;
336                         tmp = page_height;
337                         page_height = page_width;
338                         page_width = tmp;
339                         
340                         break;
341                 case 180:
342                         rotation = DDJVU_ROTATE_180;
343                         
344                         break;
345                 case 270:
346                         rotation = DDJVU_ROTATE_270;
347                         tmp = page_height;
348                         page_height = page_width;
349                         page_width = tmp;
350                         
351                         break;
352                 default:
353                         rotation = DDJVU_ROTATE_0;
354         }
355 #ifdef HAVE_CAIRO_FORMAT_STRIDE_FOR_WIDTH
356         rowstride = cairo_format_stride_for_width (CAIRO_FORMAT_RGB24, page_width);
357 #else
358         rowstride = page_width * 4;
359 #endif
360         pixels = (gchar *) g_malloc (page_height * rowstride);
361         surface = cairo_image_surface_create_for_data ((guchar *)pixels,
362                                                        CAIRO_FORMAT_RGB24,
363                                                        page_width,
364                                                        page_height,
365                                                        rowstride);
366         cairo_surface_set_user_data (surface, &key,
367                                      pixels, (cairo_destroy_func_t)g_free);
368         prect.x = 0;
369         prect.y = 0;
370         prect.w = page_width;
371         prect.h = page_height;
372         rrect = prect;
373
374         ddjvu_page_set_rotation (d_page, rotation);
375         
376         ddjvu_page_render (d_page, DDJVU_RENDER_COLOR,
377                            &prect,
378                            &rrect,
379                            djvu_document->d_format,
380                            rowstride,
381                            pixels);
382
383         return surface;
384 }
385
386 static void
387 djvu_document_finalize (GObject *object)
388 {
389         DjvuDocument *djvu_document = DJVU_DOCUMENT (object);
390
391         if (djvu_document->search)
392             djvu_text_free (djvu_document->search);
393
394         if (djvu_document->d_document)
395             ddjvu_document_release (djvu_document->d_document);
396             
397         if (djvu_document->opts)
398             g_string_free (djvu_document->opts, TRUE);
399
400         if (djvu_document->ps_filename)
401             g_free (djvu_document->ps_filename);
402             
403         ddjvu_context_release (djvu_document->d_context);
404         ddjvu_format_release (djvu_document->d_format);
405         ddjvu_format_release (djvu_document->thumbs_format);
406         g_free (djvu_document->uri);
407         
408         G_OBJECT_CLASS (djvu_document_parent_class)->finalize (object);
409 }
410
411 static void
412 djvu_document_class_init (DjvuDocumentClass *klass)
413 {
414         GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
415
416         gobject_class->finalize = djvu_document_finalize;
417 }
418
419 static EvDocumentInfo *
420 djvu_document_get_info (EvDocument *document)
421 {
422         EvDocumentInfo *info;
423
424         info = g_new0 (EvDocumentInfo, 1);
425
426         return info;
427 }
428
429 static void
430 djvu_document_document_iface_init (EvDocumentIface *iface)
431 {
432         iface->load = djvu_document_load;
433         iface->save = djvu_document_save;
434         iface->get_n_pages = djvu_document_get_n_pages;
435         iface->get_page_size = djvu_document_get_page_size;
436         iface->render = djvu_document_render;
437         iface->get_info = djvu_document_get_info;
438 }
439
440 static gchar *
441 djvu_selection_get_selected_text (EvSelection     *selection,
442                                   EvRenderContext *rc,
443                                   EvSelectionStyle style,
444                                   EvRectangle     *points)
445 {
446         DjvuDocument *djvu_document = DJVU_DOCUMENT (selection);
447         double width, height;
448         EvRectangle rectangle;
449         gchar *text;
450              
451         djvu_document_get_page_size (EV_DOCUMENT (djvu_document),
452                                      rc->page, &width, &height);                
453         rectangle.x1 = points->x1 / SCALE_FACTOR;
454         rectangle.y1 = (height - points->y2) / SCALE_FACTOR;
455         rectangle.x2 = points->x2 / SCALE_FACTOR;
456         rectangle.y2 = (height - points->y1) / SCALE_FACTOR;
457                 
458         text = djvu_text_copy (djvu_document, rc->page->index, &rectangle);
459       
460         if (text == NULL)
461                 text = g_strdup ("");
462                 
463         return text;
464 }
465
466 static void
467 djvu_selection_iface_init (EvSelectionIface *iface)
468 {
469         iface->get_selected_text = djvu_selection_get_selected_text;
470 }
471
472 static void
473 djvu_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
474                                          EvRenderContext      *rc, 
475                                          gint                 *width,
476                                          gint                 *height)
477 {
478         DjvuDocument *djvu_document = DJVU_DOCUMENT (document); 
479         gdouble page_width, page_height;
480         
481         djvu_document_get_page_size (EV_DOCUMENT(djvu_document), rc->page,
482                                      &page_width, &page_height);
483
484         if (rc->rotation == 90 || rc->rotation == 270) {
485                 *width = (gint) (page_height * rc->scale);
486                 *height = (gint) (page_width * rc->scale);
487         } else {
488                 *width = (gint) (page_width * rc->scale);
489                 *height = (gint) (page_height * rc->scale);
490         }
491 }
492
493 static GdkPixbuf *
494 djvu_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
495                                         EvRenderContext      *rc,
496                                         gboolean              border)
497 {
498         DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
499         GdkPixbuf *pixbuf, *rotated_pixbuf;
500         gdouble page_width, page_height;
501         gint thumb_width, thumb_height;
502         guchar *pixels;
503         
504         g_return_val_if_fail (djvu_document->d_document, NULL);
505
506         djvu_document_get_page_size (EV_DOCUMENT(djvu_document), rc->page,
507                                      &page_width, &page_height);
508         
509         thumb_width = (gint) (page_width * rc->scale);
510         thumb_height = (gint) (page_height * rc->scale);
511
512         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
513                                  thumb_width, thumb_height);
514         gdk_pixbuf_fill (pixbuf, 0xffffffff);
515         pixels = gdk_pixbuf_get_pixels (pixbuf);
516         
517         while (ddjvu_thumbnail_status (djvu_document->d_document, rc->page->index, 1) < DDJVU_JOB_OK)
518                 djvu_handle_events(djvu_document, TRUE, NULL);
519                     
520         ddjvu_thumbnail_render (djvu_document->d_document, rc->page->index, 
521                                 &thumb_width, &thumb_height,
522                                 djvu_document->thumbs_format,
523                                 gdk_pixbuf_get_rowstride (pixbuf), 
524                                 (gchar *)pixels);
525
526         rotated_pixbuf = gdk_pixbuf_rotate_simple (pixbuf, 360 - rc->rotation);
527         g_object_unref (pixbuf);
528
529         if (border) {
530               GdkPixbuf *tmp_pixbuf = rotated_pixbuf;
531               
532               rotated_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
533               g_object_unref (tmp_pixbuf);
534         }
535         
536         return rotated_pixbuf;
537 }
538
539 static void
540 djvu_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
541 {
542         iface->get_thumbnail = djvu_document_thumbnails_get_thumbnail;
543         iface->get_dimensions = djvu_document_thumbnails_get_dimensions;
544 }
545
546 /* EvFileExporterIface */
547 static void
548 djvu_document_file_exporter_begin (EvFileExporter        *exporter,
549                                    EvFileExporterContext *fc)
550 {
551         DjvuDocument *djvu_document = DJVU_DOCUMENT (exporter);
552         
553         if (djvu_document->ps_filename)
554                 g_free (djvu_document->ps_filename);    
555         djvu_document->ps_filename = g_strdup (fc->filename);
556
557         g_string_assign (djvu_document->opts, "-page=");
558 }
559
560 static void
561 djvu_document_file_exporter_do_page (EvFileExporter  *exporter,
562                                      EvRenderContext *rc)
563 {
564         DjvuDocument *djvu_document = DJVU_DOCUMENT (exporter);
565         
566         g_string_append_printf (djvu_document->opts, "%d,", (rc->page->index) + 1); 
567 }
568
569 static void
570 djvu_document_file_exporter_end (EvFileExporter *exporter)
571 {
572         int d_optc = 1; 
573         const char *d_optv[d_optc];
574
575         DjvuDocument *djvu_document = DJVU_DOCUMENT (exporter);
576
577         FILE *fn = fopen (djvu_document->ps_filename, "w");
578         if (fn == NULL) {
579                 g_warning ("Cannot open file ā€œ%sā€.", djvu_document->ps_filename);
580                 return;
581         }
582         
583         d_optv[0] = djvu_document->opts->str; 
584
585         ddjvu_job_t * job = ddjvu_document_print(djvu_document->d_document, fn, d_optc, d_optv);
586         while (!ddjvu_job_done(job)) {  
587                 djvu_handle_events (djvu_document, TRUE, NULL);
588         }
589
590         fclose(fn); 
591 }
592
593 static EvFileExporterCapabilities
594 djvu_document_file_exporter_get_capabilities (EvFileExporter *exporter)
595 {
596         return  EV_FILE_EXPORTER_CAN_PAGE_SET |
597                 EV_FILE_EXPORTER_CAN_COPIES |
598                 EV_FILE_EXPORTER_CAN_COLLATE |
599                 EV_FILE_EXPORTER_CAN_REVERSE |
600                 EV_FILE_EXPORTER_CAN_GENERATE_PS;
601 }
602
603 static void
604 djvu_document_file_exporter_iface_init (EvFileExporterIface *iface)
605 {
606         iface->begin = djvu_document_file_exporter_begin;
607         iface->do_page = djvu_document_file_exporter_do_page;
608         iface->end = djvu_document_file_exporter_end;
609         iface->get_capabilities = djvu_document_file_exporter_get_capabilities;
610 }
611
612 static void
613 djvu_document_init (DjvuDocument *djvu_document)
614 {
615         guint masks[4] = { 0xff0000, 0xff00, 0xff, 0xff000000 };
616         
617         djvu_document->d_context = ddjvu_context_create ("Evince");
618         djvu_document->d_format = ddjvu_format_create (DDJVU_FORMAT_RGBMASK32, 4, masks);
619         ddjvu_format_set_row_order (djvu_document->d_format, 1);
620
621         djvu_document->thumbs_format = ddjvu_format_create (DDJVU_FORMAT_RGB24, 0, 0);
622         ddjvu_format_set_row_order (djvu_document->thumbs_format, 1);
623
624         djvu_document->ps_filename = NULL;
625         djvu_document->opts = g_string_new ("");
626         
627         djvu_document->d_document = NULL;
628 }
629
630 static void
631 djvu_document_find_begin (EvDocumentFind   *document,
632                           int               page,
633                           const char       *search_string,
634                           gboolean          case_sensitive)
635 {
636         DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
637
638         if (djvu_document->search && 
639             strcmp (search_string, djvu_text_get_text (djvu_document->search)) == 0)
640                 return;
641
642         if (djvu_document->search)
643                 djvu_text_free (djvu_document->search);
644
645         djvu_document->search = djvu_text_new (djvu_document,
646                                                           page,
647                                                           case_sensitive,
648                                                           search_string);
649 }
650
651 static int
652 djvu_document_find_get_n_results (EvDocumentFind *document_find, int page)
653 {
654         DjvuText *search = DJVU_DOCUMENT (document_find)->search;
655
656         if (search) {
657                 return djvu_text_n_results (search, page);
658         } else {
659                 return 0;
660         }
661 }
662
663 static gboolean
664 djvu_document_find_get_result (EvDocumentFind *document_find,
665                                int             page,
666                                int             n_result,
667                                EvRectangle    *rectangle)
668 {
669         DjvuDocument *djvu_document = DJVU_DOCUMENT (document_find);
670         DjvuText *search = djvu_document->search;
671         EvRectangle *r;
672         double width, height;
673
674         if (search == NULL)
675                 return FALSE;
676
677         r = djvu_text_get_result (search, page, n_result);
678         if (r == NULL)
679                 return FALSE;
680
681         document_get_page_size (djvu_document, page, &width, &height);
682         rectangle->x1 = r->x1 * SCALE_FACTOR;
683         rectangle->y1 = height - r->y2 * SCALE_FACTOR;
684         rectangle->x2 = r->x2 * SCALE_FACTOR;
685         rectangle->y2 = height - r->y1 * SCALE_FACTOR;
686                 
687         return TRUE;
688 }
689
690 static int
691 djvu_document_find_page_has_results (EvDocumentFind *document_find,
692                                     int             page)
693 {
694         DjvuText *search = DJVU_DOCUMENT (document_find)->search;
695
696         return search && djvu_text_has_results (search, page);
697 }
698
699 static double
700 djvu_document_find_get_progress (EvDocumentFind *document_find)
701 {
702         DjvuText *search = DJVU_DOCUMENT (document_find)->search;
703         
704         if (search == NULL) {
705                 return 0;
706         }
707
708         return djvu_text_get_progress (search);
709 }
710
711 static void
712 djvu_document_find_cancel (EvDocumentFind *document)
713 {
714         DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
715
716         if (djvu_document->search) {
717                 djvu_text_free (djvu_document->search);
718                 djvu_document->search = NULL;
719         }
720 }
721
722 static void
723 djvu_document_find_iface_init (EvDocumentFindIface *iface)
724 {
725         iface->begin = djvu_document_find_begin;
726         iface->get_n_results = djvu_document_find_get_n_results;
727         iface->get_result = djvu_document_find_get_result;
728         iface->page_has_results = djvu_document_find_page_has_results;
729         iface->get_progress = djvu_document_find_get_progress;
730         iface->cancel = djvu_document_find_cancel;
731 }
732
733 static GList *
734 djvu_document_links_get_links (EvDocumentLinks *document_links,
735                                gint             page)
736 {
737         return djvu_links_get_links (document_links, page, SCALE_FACTOR);
738 }
739
740 static void
741 djvu_document_document_links_iface_init  (EvDocumentLinksIface *iface)
742 {
743         iface->has_document_links = djvu_links_has_document_links;
744         iface->get_links_model = djvu_links_get_links_model;
745         iface->get_links = djvu_document_links_get_links;
746         iface->find_link_dest = djvu_links_find_link_dest;
747 }