]> www.fi.muni.cz Git - evince.git/blob - shell/ev-jobs.c
Massive changes. We now support text selection of pdfs, and not just
[evince.git] / shell / ev-jobs.c
1 #include "ev-jobs.h"
2 #include "ev-job-queue.h"
3 #include "ev-document-thumbnails.h"
4 #include "ev-document-links.h"
5 #include "ev-document-fonts.h"
6 #include "ev-selection.h"
7 #include "ev-async-renderer.h"
8
9 static void ev_job_init                 (EvJob               *job);
10 static void ev_job_class_init           (EvJobClass          *class);
11 static void ev_job_links_init           (EvJobLinks          *job);
12 static void ev_job_links_class_init     (EvJobLinksClass     *class);
13 static void ev_job_render_init          (EvJobRender         *job);
14 static void ev_job_render_class_init    (EvJobRenderClass    *class);
15 static void ev_job_thumbnail_init       (EvJobThumbnail      *job);
16 static void ev_job_thumbnail_class_init (EvJobThumbnailClass *class);
17 static void ev_job_load_init            (EvJobLoad           *job);
18 static void ev_job_load_class_init      (EvJobLoadClass      *class);
19
20 enum
21 {
22         FINISHED,
23         LAST_SIGNAL
24 };
25
26 static guint job_signals[LAST_SIGNAL] = { 0 };
27
28 G_DEFINE_TYPE (EvJob, ev_job, G_TYPE_OBJECT)
29 G_DEFINE_TYPE (EvJobLinks, ev_job_links, EV_TYPE_JOB)
30 G_DEFINE_TYPE (EvJobRender, ev_job_render, EV_TYPE_JOB)
31 G_DEFINE_TYPE (EvJobThumbnail, ev_job_thumbnail, EV_TYPE_JOB)
32 G_DEFINE_TYPE (EvJobLoad, ev_job_load, EV_TYPE_JOB)
33 G_DEFINE_TYPE (EvJobFonts, ev_job_fonts, EV_TYPE_JOB)
34
35 static void ev_job_init (EvJob *job) { /* Do Nothing */ }
36
37 static void
38 ev_job_dispose (GObject *object)
39 {
40         EvJob *job;
41
42         job = EV_JOB (object);
43
44         if (job->document) {
45                 g_object_unref (job->document);
46                 job->document = NULL;
47         }
48
49         (* G_OBJECT_CLASS (ev_job_parent_class)->dispose) (object);
50 }
51
52 static void
53 ev_job_class_init (EvJobClass *class)
54 {
55         GObjectClass *oclass;
56
57         oclass = G_OBJECT_CLASS (class);
58
59         oclass->dispose = ev_job_dispose;
60
61         job_signals [FINISHED] =
62                 g_signal_new ("finished",
63                               EV_TYPE_JOB,
64                               G_SIGNAL_RUN_LAST,
65                               G_STRUCT_OFFSET (EvJobClass, finished),
66                               NULL, NULL,
67                               g_cclosure_marshal_VOID__VOID,
68                               G_TYPE_NONE, 0);
69 }
70
71
72 static void ev_job_links_init (EvJobLinks *job) { /* Do Nothing */ }
73
74 static void
75 ev_job_links_dispose (GObject *object)
76 {
77         EvJobLinks *job;
78
79         job = EV_JOB_LINKS (object);
80
81         if (job->model) {
82                 g_object_unref (job->model);
83                 job->model = NULL;
84         }
85
86         (* G_OBJECT_CLASS (ev_job_links_parent_class)->dispose) (object);
87 }
88
89 static void
90 ev_job_links_class_init (EvJobLinksClass *class)
91 {
92         GObjectClass *oclass;
93
94         oclass = G_OBJECT_CLASS (class);
95
96         oclass->dispose = ev_job_links_dispose;
97 }
98
99
100 static void ev_job_render_init (EvJobRender *job) { /* Do Nothing */ }
101
102 static void
103 ev_job_render_dispose (GObject *object)
104 {
105         EvJobRender *job;
106
107         job = EV_JOB_RENDER (object);
108
109         if (job->pixbuf) {
110                 g_object_unref (job->pixbuf);
111                 job->pixbuf = NULL;
112         }
113
114         if (job->rc) {
115                 g_object_unref (job->rc);
116                 job->rc = NULL;
117         }
118
119         (* G_OBJECT_CLASS (ev_job_render_parent_class)->dispose) (object);
120 }
121
122 static void
123 ev_job_render_class_init (EvJobRenderClass *class)
124 {
125         GObjectClass *oclass;
126
127         oclass = G_OBJECT_CLASS (class);
128
129         oclass->dispose = ev_job_render_dispose;
130 }
131
132 static void ev_job_thumbnail_init (EvJobThumbnail *job) { /* Do Nothing */ }
133
134 static void
135 ev_job_thumbnail_dispose (GObject *object)
136 {
137         EvJobThumbnail *job;
138
139         job = EV_JOB_THUMBNAIL (object);
140
141         if (job->thumbnail) {
142                 g_object_unref (job->thumbnail);
143                 job->thumbnail = NULL;
144         }
145
146         (* G_OBJECT_CLASS (ev_job_thumbnail_parent_class)->dispose) (object);
147 }
148
149 static void
150 ev_job_thumbnail_class_init (EvJobThumbnailClass *class)
151 {
152         GObjectClass *oclass;
153
154         oclass = G_OBJECT_CLASS (class);
155
156         oclass->dispose = ev_job_thumbnail_dispose;
157 }
158
159 static void ev_job_load_init (EvJobLoad *job) { /* Do Nothing */ }
160
161 static void
162 ev_job_load_dispose (GObject *object)
163 {
164         EvJobLoad *job;
165
166         job = EV_JOB_LOAD (object);
167
168         if (job->uri) {
169                 g_free (job->uri);
170                 job->uri = NULL;
171         }
172
173         if (job->error) {
174                 g_error_free (job->error);
175                 job->error = NULL;
176         }
177
178         (* G_OBJECT_CLASS (ev_job_load_parent_class)->dispose) (object);
179 }
180
181 static void
182 ev_job_load_class_init (EvJobLoadClass *class)
183 {
184         GObjectClass *oclass;
185
186         oclass = G_OBJECT_CLASS (class);
187
188         oclass->dispose = ev_job_load_dispose;
189 }
190
191 /* Public functions */
192 void
193 ev_job_finished (EvJob *job)
194 {
195         g_return_if_fail (EV_IS_JOB (job));
196
197         g_signal_emit (job, job_signals[FINISHED], 0);
198 }
199
200 EvJob *
201 ev_job_links_new (EvDocument *document)
202 {
203         EvJob *job;
204
205         job = g_object_new (EV_TYPE_JOB_LINKS, NULL);
206         job->document = g_object_ref (document);
207
208         return job;
209 }
210
211 void
212 ev_job_links_run (EvJobLinks *job)
213 {
214         g_return_if_fail (EV_IS_JOB_LINKS (job));
215
216         ev_document_doc_mutex_lock ();
217         job->model = ev_document_links_get_links_model (EV_DOCUMENT_LINKS (EV_JOB (job)->document));
218         EV_JOB (job)->finished = TRUE;
219         ev_document_doc_mutex_unlock ();
220 }
221
222
223 EvJob *
224 ev_job_render_new (EvDocument      *document,
225                    EvRenderContext *rc,
226                    gint             width,
227                    gint             height,
228                    EvRectangle     *selection_points,
229                    gboolean         include_links,
230                    gboolean         include_selection)
231 {
232         EvJobRender *job;
233
234         g_return_val_if_fail (EV_IS_RENDER_CONTEXT (rc), NULL);
235         if (include_selection)
236                 g_return_val_if_fail (selection_points != NULL, NULL);
237
238         job = g_object_new (EV_TYPE_JOB_RENDER, NULL);
239
240         EV_JOB (job)->document = g_object_ref (document);
241         job->rc = g_object_ref (rc);
242         job->target_width = width;
243         job->target_height = height;
244         job->include_links = include_links;
245         job->include_selection = include_selection;
246
247         if (include_selection)
248                 job->selection_points = *selection_points;
249
250         if (EV_IS_ASYNC_RENDERER (document)) {  
251                 EV_JOB (job)->async = TRUE;
252         }
253
254         return EV_JOB (job);
255 }
256
257 static void
258 render_finished_cb (EvDocument *document, GdkPixbuf *pixbuf, EvJobRender *job)
259 {
260         g_signal_handlers_disconnect_by_func (EV_JOB (job)->document,
261                                               render_finished_cb, job);
262
263         EV_JOB (job)->finished = TRUE;
264         job->pixbuf = g_object_ref (pixbuf);
265         ev_job_finished (EV_JOB (job));
266 }
267
268 void
269 ev_job_render_run (EvJobRender *job)
270 {
271         g_return_if_fail (EV_IS_JOB_RENDER (job));
272
273         ev_document_doc_mutex_lock ();
274
275         if (EV_JOB (job)->async) {
276                 EvAsyncRenderer *renderer = EV_ASYNC_RENDERER (EV_JOB (job)->document);
277                 ev_async_renderer_render_pixbuf (renderer, job->rc->page, job->rc->scale);
278                 g_signal_connect (EV_JOB (job)->document, "render_finished",
279                                   G_CALLBACK (render_finished_cb), job);
280         } else {
281                 job->pixbuf = ev_document_render_pixbuf (EV_JOB (job)->document, job->rc);
282                 if (job->include_links)
283                         job->link_mapping = ev_document_get_links (EV_JOB (job)->document, job->rc->page);
284                 if (job->include_selection && EV_IS_SELECTION (EV_JOB (job)->document))
285                         ev_selection_render_selection (EV_SELECTION (EV_JOB (job)->document),
286                                                        job->rc,
287                                                        &(job->selection),
288                                                        &(job->selection_points),
289                                                        NULL);
290
291                 EV_JOB (job)->finished = TRUE;
292         }
293
294         ev_document_doc_mutex_unlock ();
295 }
296
297 EvJob *
298 ev_job_thumbnail_new (EvDocument *document,
299                       gint        page,
300                       gint        requested_width)
301 {
302         EvJobThumbnail *job;
303
304         job = g_object_new (EV_TYPE_JOB_THUMBNAIL, NULL);
305
306         EV_JOB (job)->document = g_object_ref (document);
307         job->page = page;
308         job->requested_width = requested_width;
309
310         return EV_JOB (job);
311 }
312
313 void
314 ev_job_thumbnail_run (EvJobThumbnail *job)
315 {
316         g_return_if_fail (EV_IS_JOB_THUMBNAIL (job));
317
318         ev_document_doc_mutex_lock ();
319
320         job->thumbnail =
321                 ev_document_thumbnails_get_thumbnail (EV_DOCUMENT_THUMBNAILS (EV_JOB (job)->document),
322                                                       job->page,
323                                                       job->requested_width,
324                                                       TRUE);
325         EV_JOB (job)->finished = TRUE;
326
327         ev_document_doc_mutex_unlock ();
328 }
329
330 EvJob *
331 ev_job_load_new (EvDocument *document,
332                  const gchar *uri)
333 {
334         EvJobLoad *job;
335
336         job = g_object_new (EV_TYPE_JOB_LOAD, NULL);
337
338         EV_JOB (job)->document = g_object_ref (document);
339         job->uri = g_strdup (uri);
340
341         return EV_JOB (job);
342 }
343
344 void
345 ev_job_load_run (EvJobLoad *job)
346 {
347         g_return_if_fail (EV_IS_JOB_LOAD (job));
348
349         ev_document_doc_mutex_lock ();
350         
351         if (job->error) {
352                 g_error_free (job->error);
353                 job->error = NULL;
354         }
355             
356         ev_document_load (EV_JOB(job)->document, job->uri, &job->error);
357         
358         EV_JOB (job)->finished = TRUE;
359
360         ev_document_doc_mutex_unlock ();
361 }
362
363 static void ev_job_fonts_init (EvJobFonts *job) { /* Do Nothing */ }
364
365 static void ev_job_fonts_class_init (EvJobFontsClass *class) { /* Do Nothing */ }
366
367 EvJob *
368 ev_job_fonts_new (EvDocument *document)
369 {
370         EvJobFonts *job;
371
372         job = g_object_new (EV_TYPE_JOB_FONTS, NULL);
373
374         EV_JOB (job)->document = g_object_ref (document);
375
376         return EV_JOB (job);
377 }
378
379 void
380 ev_job_fonts_run (EvJobFonts *job)
381 {
382         EvDocumentFonts *fonts;
383
384         g_return_if_fail (EV_IS_JOB_FONTS (job));
385
386         ev_document_doc_mutex_lock ();
387         
388         fonts = EV_DOCUMENT_FONTS (EV_JOB (job)->document);
389         job->scan_completed = !ev_document_fonts_scan (fonts, 20);
390         
391         EV_JOB (job)->finished = TRUE;
392
393         ev_document_doc_mutex_unlock ();
394 }