1 /* this file is part of evince, a gnome document viewer
3 * Copyright (C) 2009 Carlos Garcia Campos
5 * Evince is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * Evince is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #include "ev-job-scheduler.h"
24 #include "ev-mapping.h"
25 #include "ev-selection.h"
26 #include "ev-document-links.h"
27 #include "ev-document-forms.h"
28 #include "ev-document-images.h"
29 #include "ev-document-annotations.h"
30 #include "ev-page-cache.h"
32 typedef struct _EvPageCacheData {
38 GList *form_field_mapping;
40 GdkRegion *text_mapping;
47 EvPageCacheData *page_list;
49 EvJobPageDataFlags flags;
52 struct _EvPageCacheClass {
53 GObjectClass parent_class;
56 static void job_page_data_finished_cb (EvJob *job,
59 G_DEFINE_TYPE (EvPageCache, ev_page_cache, G_TYPE_OBJECT)
62 ev_page_cache_data_free (EvPageCacheData *data)
65 g_object_unref (data->job);
69 if (data->link_mapping) {
70 ev_mapping_list_free (data->link_mapping, g_object_unref);
71 data->link_mapping = NULL;
74 if (data->image_mapping) {
75 ev_mapping_list_free (data->image_mapping, g_object_unref);
76 data->image_mapping = NULL;
79 if (data->form_field_mapping) {
80 ev_mapping_list_free (data->form_field_mapping, g_object_unref);
81 data->form_field_mapping = NULL;
84 if (data->annot_mapping) {
85 ev_mapping_list_free (data->annot_mapping, g_object_unref);
86 data->annot_mapping = NULL;
89 if (data->text_mapping) {
90 gdk_region_destroy (data->text_mapping);
91 data->text_mapping = NULL;
96 ev_page_cache_finalize (GObject *object)
98 EvPageCache *cache = EV_PAGE_CACHE (object);
101 if (cache->page_list) {
102 for (i = 0; i < cache->n_pages; i++) {
103 EvPageCacheData *data;
105 data = &cache->page_list[i];
108 g_signal_handlers_disconnect_by_func (data->job,
109 G_CALLBACK (job_page_data_finished_cb),
111 ev_page_cache_data_free (data);
114 g_free (cache->page_list);
115 cache->page_list = NULL;
119 if (cache->document) {
120 g_object_unref (cache->document);
121 cache->document = NULL;
124 G_OBJECT_CLASS (ev_page_cache_parent_class)->finalize (object);
128 ev_page_cache_init (EvPageCache *cache)
133 ev_page_cache_class_init (EvPageCacheClass *klass)
135 GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
137 g_object_class->finalize = ev_page_cache_finalize;
140 static EvJobPageDataFlags
141 get_flags_for_document (EvDocument *document)
143 EvJobPageDataFlags flags = EV_PAGE_DATA_INCLUDE_NONE;
145 if (EV_IS_DOCUMENT_LINKS (document))
146 flags |= EV_PAGE_DATA_INCLUDE_LINKS;
147 if (EV_IS_DOCUMENT_IMAGES (document))
148 flags |= EV_PAGE_DATA_INCLUDE_IMAGES;
149 if (EV_IS_DOCUMENT_FORMS (document))
150 flags |= EV_PAGE_DATA_INCLUDE_FORMS;
151 if (EV_IS_DOCUMENT_ANNOTATIONS (document))
152 flags |= EV_PAGE_DATA_INCLUDE_ANNOTS;
153 if (EV_IS_SELECTION (document))
154 flags |= EV_PAGE_DATA_INCLUDE_TEXT;
160 ev_page_cache_new (EvDocument *document)
164 g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
166 cache = EV_PAGE_CACHE (g_object_new (EV_TYPE_PAGE_CACHE, NULL));
167 cache->document = g_object_ref (document);
168 cache->n_pages = ev_document_get_n_pages (document);
169 cache->flags = get_flags_for_document (document);
171 if (cache->flags != EV_PAGE_DATA_INCLUDE_NONE) {
172 cache->page_list = g_new0 (EvPageCacheData, cache->n_pages);
179 job_page_data_finished_cb (EvJob *job,
182 EvJobPageData *job_data = EV_JOB_PAGE_DATA (job);
183 EvPageCacheData *data;
185 data = &cache->page_list[job_data->page];
186 data->link_mapping = job_data->link_mapping;
187 data->image_mapping = job_data->image_mapping;
188 data->form_field_mapping = job_data->form_field_mapping;
189 data->annot_mapping = job_data->annot_mapping;
190 data->text_mapping = job_data->text_mapping;
193 g_object_unref (data->job);
198 ev_page_cache_set_page_range (EvPageCache *cache,
204 if (cache->flags == EV_PAGE_DATA_INCLUDE_NONE)
207 for (i = start; i <= end; i++) {
208 EvPageCacheData *data = &cache->page_list[i];
210 if (data->done || data->job)
213 data->job = ev_job_page_data_new (cache->document, i, cache->flags);
214 g_signal_connect (data->job, "finished",
215 G_CALLBACK (job_page_data_finished_cb),
217 ev_job_scheduler_push_job (data->job, EV_JOB_PRIORITY_NONE);
222 ev_page_cache_get_flags (EvPageCache *cache)
228 ev_page_cache_set_flags (EvPageCache *cache,
229 EvJobPageDataFlags flags)
231 cache->flags = flags;
235 ev_page_cache_get_link_mapping (EvPageCache *cache,
238 EvPageCacheData *data;
240 g_return_val_if_fail (EV_IS_PAGE_CACHE (cache), NULL);
241 g_return_val_if_fail (page >= 0 && page < cache->n_pages, NULL);
243 if (!(cache->flags & EV_PAGE_DATA_INCLUDE_LINKS))
246 data = &cache->page_list[page];
248 return data->link_mapping;
251 return EV_JOB_PAGE_DATA (data->job)->link_mapping;
253 return data->link_mapping;
257 ev_page_cache_get_image_mapping (EvPageCache *cache,
260 EvPageCacheData *data;
262 g_return_val_if_fail (EV_IS_PAGE_CACHE (cache), NULL);
263 g_return_val_if_fail (page >= 0 && page < cache->n_pages, NULL);
265 if (!(cache->flags & EV_PAGE_DATA_INCLUDE_IMAGES))
268 data = &cache->page_list[page];
270 return data->image_mapping;
273 return EV_JOB_PAGE_DATA (data->job)->image_mapping;
275 return data->image_mapping;
279 ev_page_cache_get_form_field_mapping (EvPageCache *cache,
282 EvPageCacheData *data;
284 g_return_val_if_fail (EV_IS_PAGE_CACHE (cache), NULL);
285 g_return_val_if_fail (page >= 0 && page < cache->n_pages, NULL);
287 if (!(cache->flags & EV_PAGE_DATA_INCLUDE_FORMS))
290 data = &cache->page_list[page];
292 return data->form_field_mapping;
295 return EV_JOB_PAGE_DATA (data->job)->form_field_mapping;
297 return data->form_field_mapping;
301 ev_page_cache_get_annot_mapping (EvPageCache *cache,
304 EvPageCacheData *data;
306 g_return_val_if_fail (EV_IS_PAGE_CACHE (cache), NULL);
307 g_return_val_if_fail (page >= 0 && page < cache->n_pages, NULL);
309 if (!(cache->flags & EV_PAGE_DATA_INCLUDE_ANNOTS))
312 data = &cache->page_list[page];
314 return data->annot_mapping;
317 return EV_JOB_PAGE_DATA (data->job)->annot_mapping;
319 return data->annot_mapping;
323 ev_page_cache_get_text_mapping (EvPageCache *cache,
326 EvPageCacheData *data;
328 g_return_val_if_fail (EV_IS_PAGE_CACHE (cache), NULL);
329 g_return_val_if_fail (page >= 0 && page < cache->n_pages, NULL);
331 if (!(cache->flags & EV_PAGE_DATA_INCLUDE_TEXT))
334 data = &cache->page_list[page];
336 return data->text_mapping;
339 return EV_JOB_PAGE_DATA (data->job)->text_mapping;
341 return data->text_mapping;