60f8909256ac86ccc75f44523d7dda6f3344612e
[claws.git] / src / plugins / pdf_viewer / poppler_viewer.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright(C) 1999-2007 the Claws Mail Team
4  * This file Copyright (C) 2007 Salvatore De Paolis 
5  * <iwkse@claws-mail.org> 
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  *(at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #  include "config.h"
24 #include "claws-features.h"
25 #endif
26
27 #include <glib.h>
28 #include <glib/gi18n.h>
29
30 #include "poppler_viewer.h"
31 #include "printing.h"
32 #include "prefs_common.h"
33 #include "gtk/gtkutils.h"
34 #include "mimeview.h"
35 #ifndef POPPLER_WITH_GDK
36 #include "stdbool.h"
37 #endif
38
39 static FileType pdf_viewer_mimepart_get_type(MimeInfo *partinfo);
40 static MimeViewerFactory pdf_viewer_factory;
41
42 static void pdf_viewer_show_mimepart(MimeViewer *_viewer, const gchar *infile,
43                                 MimeInfo *partinfo);
44
45 static MimeViewer *pdf_viewer_create(void);
46 static void pdf_viewer_clear(MimeViewer *_viewer);
47 static void pdf_viewer_destroy(MimeViewer *_viewer);
48 static void pdf_viewer_update(MimeViewer *_viewer, gboolean reload_file, int page_num);
49
50 static GtkWidget *pdf_viewer_get_widget(MimeViewer *_viewer);
51
52 static void pdf_viewer_hide_index_pane(PdfViewer *viewer);
53 static void pdf_viewer_set_index_button_sensitive(PdfViewer *viewer);
54 static void pdf_viewer_scroll_to(PdfViewer *viewer, gfloat x, gfloat y);
55
56 static void search_matches_free(PdfViewer *viewer);
57 static gboolean pdf_viewer_text_search(MimeViewer *_viewer, gboolean backward,
58                                      const gchar *str, gboolean case_sens);
59 static void pdf_viewer_render_selection(PdfViewer *viewer, PopplerRectangle *rect, PageResult *page_results);
60 static void pdf_viewer_render_page(PopplerPage *page, GtkWidget *view, double width, double height, double zoom, gint rotate);
61
62 static char * pdf_viewer_get_document_format_data(GTime utime);
63 static void pdf_viewer_get_document_index(PdfViewer *viewer, PopplerIndexIter *index_iter, GtkTreeIter *parentiter);
64 static void pdf_viewer_index_row_activated(GtkTreeView          *tree_view,
65                                         GtkTreePath             *path,
66                                         GtkTreeViewColumn       *column,
67                                         gpointer                 data);
68
69 static GtkTable * pdf_viewer_fill_info_table(PdfViewer *viewer);
70
71 /* Callbacks */
72 static void pdf_viewer_move_events_cb(GtkWidget *widget, GdkEventMotion *event, PdfViewer *viewer); 
73 static void pdf_viewer_button_press_events_cb(GtkWidget *widget, GdkEventButton *event, PdfViewer *viewer); 
74 static void pdf_viewer_mouse_scroll_destroy_cb(GtkWidget *widget, GdkEventButton *event, PdfViewer *viewer); 
75 static void pdf_viewer_button_first_page_cb(GtkButton *button, PdfViewer *viewer);
76 static void pdf_viewer_button_last_page_cb(GtkButton *button, PdfViewer *viewer);
77 static void pdf_viewer_button_zoom_in_cb(GtkButton *button, PdfViewer *viewer);
78 static void pdf_viewer_button_zoom_out_cb(GtkButton *button, PdfViewer *viewer);
79 static void pdf_viewer_button_zoom_fit_cb(GtkButton *button, PdfViewer *viewer);
80 static void pdf_viewer_button_zoom_width_cb(GtkButton *button, PdfViewer *viewer);
81 static void pdf_viewer_button_rotate_right_cb(GtkButton *button, PdfViewer *viewer);
82 static void pdf_viewer_button_rotate_left_cb(GtkButton *button, PdfViewer *viewer);
83 static void pdf_viewer_spin_change_page_cb(GtkSpinButton *button, PdfViewer *viewer);
84 static void pdf_viewer_spin_zoom_scroll_cb(GtkSpinButton *button, PdfViewer *viewer);
85 /* Show/Hide the index pane */
86 static void pdf_viewer_show_document_index_cb(GtkButton *button, PdfViewer *viewer);
87 static void pdf_viewer_button_document_info_cb(GtkButton *button, PdfViewer *viewer);
88
89 static void pdf_viewer_show_controls(PdfViewer *viewer, gboolean show);
90 static gboolean pdf_viewer_scroll_page(MimeViewer *_viewer, gboolean up);
91 static void pdf_viewer_scroll_one_line(MimeViewer *_viewer, gboolean up);
92
93 /** Claws-Mail Plugin functions*/
94 gint plugin_init(gchar **error);
95 const gchar *plugin_name(void);
96 const gchar *plugin_desc(void);
97 const gchar *plugin_type(void);
98 const gchar *plugin_licence(void);
99 const gchar *plugin_version(void);
100 struct PluginFeature *plugin_provides(void);
101
102 #ifndef POPPLER_WITH_GDK
103 static void
104 copy_cairo_surface_to_pixbuf (cairo_surface_t *surface,
105                               GdkPixbuf       *pixbuf)
106 {
107         int cairo_width, cairo_height, cairo_rowstride;
108         unsigned char *pixbuf_data, *dst, *cairo_data;
109         int pixbuf_rowstride, pixbuf_n_channels;
110         unsigned int *src;
111         int x, y;
112
113         cairo_width = cairo_image_surface_get_width (surface);
114         cairo_height = cairo_image_surface_get_height (surface);
115         cairo_rowstride = cairo_image_surface_get_stride (surface);
116         cairo_data = cairo_image_surface_get_data (surface);
117
118         pixbuf_data = gdk_pixbuf_get_pixels (pixbuf);
119         pixbuf_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
120         pixbuf_n_channels = gdk_pixbuf_get_n_channels (pixbuf);
121
122         if (cairo_width > gdk_pixbuf_get_width (pixbuf))
123                 cairo_width = gdk_pixbuf_get_width (pixbuf);
124         if (cairo_height > gdk_pixbuf_get_height (pixbuf))
125                 cairo_height = gdk_pixbuf_get_height (pixbuf);
126         for (y = 0; y < cairo_height; y++) {
127                 src = (unsigned int *) (cairo_data + y * cairo_rowstride);
128                 dst = pixbuf_data + y * pixbuf_rowstride;
129                 for (x = 0; x < cairo_width; x++) {
130                         dst[0] = (*src >> 16) & 0xff;
131                         dst[1] = (*src >> 8) & 0xff; 
132                         dst[2] = (*src >> 0) & 0xff;
133                         if (pixbuf_n_channels == 4)
134                                 dst[3] = (*src >> 24) & 0xff;
135                         dst += pixbuf_n_channels;
136                         src++;
137                 }
138         }
139 }
140 static void
141 _poppler_page_render_to_pixbuf (PopplerPage *page,
142                                 int src_x, int src_y,
143                                 int src_width, int src_height,
144                                 double scale,
145                                 int rotation,
146                                 gboolean printing,
147                                 GdkPixbuf *pixbuf)
148 {
149         cairo_t *cr;
150         cairo_surface_t *surface;
151
152         surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
153                                         src_width, src_height);
154         cr = cairo_create (surface);
155         cairo_save (cr);
156         switch (rotation) {
157         case 90:
158                 cairo_translate (cr, src_x + src_width, -src_y);
159                 break;
160         case 180:
161                 cairo_translate (cr, src_x + src_width, src_y + src_height);
162                 break;
163         case 270:
164                 cairo_translate (cr, -src_x, src_y + src_height);
165                 break;
166         default:
167                 cairo_translate (cr, -src_x, -src_y);
168         }
169
170         if (scale != 1.0)
171                 cairo_scale (cr, scale, scale);
172
173         if (rotation != 0)
174                 cairo_rotate (cr, rotation * G_PI / 180.0);
175
176         if (printing)
177                 poppler_page_render_for_printing (page, cr);
178         else
179                 poppler_page_render (page, cr);
180         cairo_restore (cr);
181
182         cairo_set_operator (cr, CAIRO_OPERATOR_DEST_OVER);
183         cairo_set_source_rgb (cr, 1., 1., 1.);
184         cairo_paint (cr);
185
186         cairo_destroy (cr);
187
188         copy_cairo_surface_to_pixbuf (surface, pixbuf);
189         cairo_surface_destroy (surface);
190 }
191
192 /**
193  * poppler_page_render_to_pixbuf:
194  * @page: the page to render from
195  * @src_x: x coordinate of upper left corner  
196  * @src_y: y coordinate of upper left corner  
197  * @src_width: width of rectangle to render  
198  * @src_height: height of rectangle to render
199  * @scale: scale specified as pixels per point
200  * @rotation: rotate the document by the specified degree
201  * @pixbuf: pixbuf to render into
202  *
203  * First scale the document to match the specified pixels per point,
204  * then render the rectangle given by the upper left corner at
205  * (src_x, src_y) and src_width and src_height.
206  * This function is for rendering a page that will be displayed.
207  * If you want to render a page that will be printed use
208  * poppler_page_render_to_pixbuf_for_printing() instead
209  *
210  * Deprecated: 0.16
211  **/
212 static void
213 poppler_page_render_to_pixbuf (PopplerPage *page,
214                                int src_x, int src_y,
215                                int src_width, int src_height,
216                                double scale,
217                                int rotation,
218                                GdkPixbuf *pixbuf)
219 {
220         g_return_if_fail (POPPLER_IS_PAGE (page));
221         g_return_if_fail (scale > 0.0);
222         g_return_if_fail (pixbuf != NULL);
223
224         _poppler_page_render_to_pixbuf (page, src_x, src_y,
225                                   src_width, src_height,
226                                   scale, rotation,
227                                   FALSE,
228                                   pixbuf);
229 }
230 #endif
231 static GtkWidget *pdf_viewer_get_widget(MimeViewer *_viewer)
232 {
233         PdfViewer *viewer = (PdfViewer *) _viewer;
234         debug_print("pdf_viewer_get_widget: %p\n", viewer->vbox);
235
236         return GTK_WIDGET(viewer->vbox);
237 }
238 /** Hide the index panel */
239 static void pdf_viewer_hide_index_pane(PdfViewer *viewer)
240 {
241         if (viewer->pdf_index) {   
242                 poppler_index_iter_free(viewer->pdf_index);
243                 viewer->pdf_index = NULL;
244                 gtk_widget_hide(GTK_WIDGET(viewer->frame_index));
245         }
246 }
247
248 static void search_matches_free(PdfViewer *viewer)
249 {
250         GList *cur; 
251         for(cur = viewer->text_found; cur; cur = cur->next) {
252                 PageResult *res = (PageResult *)cur->data;
253                 g_list_free(res->results);
254                 g_free(res);
255         }
256         g_list_free(viewer->text_found);
257         viewer->text_found = NULL;
258         g_free(viewer->last_search);
259         viewer->last_search = NULL;
260         if (viewer->last_rect && viewer->last_page_result) {
261                 viewer->last_rect = NULL;
262                 viewer->last_page_result = NULL;
263                 pdf_viewer_update((MimeViewer *)viewer, 
264                         FALSE, 
265                         gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(viewer->cur_page)));
266         }
267 }
268
269 static void pdf_viewer_scroll_to(PdfViewer *viewer, gfloat x, gfloat y)
270 {
271         GtkAdjustment *vadj;
272         GtkAdjustment *hadj;
273         vadj = gtk_scrolled_window_get_vadjustment(
274                 GTK_SCROLLED_WINDOW(viewer->scrollwin));
275         
276         if (y < vadj->value) {
277                 vadj->value = y;
278         }
279         else {
280                 while(y > vadj->value + vadj->page_size) {
281                         vadj->value += vadj->page_size;
282                 }
283         }
284         
285         hadj = gtk_scrolled_window_get_hadjustment(
286                 GTK_SCROLLED_WINDOW(viewer->scrollwin));
287         
288         if (x < hadj->value) {
289                 hadj->value = x;
290         }
291         else {
292                 while(x > hadj->value + hadj->page_size) {
293                         hadj->value += hadj->page_size;
294                 }
295         }
296
297         g_signal_emit_by_name(G_OBJECT(hadj), "value-changed", 0);      
298         g_signal_emit_by_name(G_OBJECT(vadj), "value-changed", 0);      
299 }
300 static void pdf_viewer_render_page(PopplerPage *page, GtkWidget *view, double width, 
301                                                                                                 double height, double zoom, gint rotate)
302 {
303         GdkPixbuf *pb;
304         
305         debug_print("width: %f\n", width);
306         pb = gdk_pixbuf_new(GDK_COLORSPACE_RGB, 
307                                 FALSE, 8, 
308                                 (int)(width * zoom), 
309                                 (int)(height * zoom));  
310         
311                         poppler_page_render_to_pixbuf(page, 0, 0, 
312                                 (int)(width * zoom), 
313                                 (int)(height * zoom), 
314                                 zoom, rotate, pb);
315                 
316                         gtk_image_set_from_pixbuf(GTK_IMAGE(view), pb);
317                         g_object_unref(G_OBJECT(pb));
318 }
319 static void pdf_viewer_render_selection(PdfViewer *viewer, PopplerRectangle *rect, PageResult *page_results)
320 {
321         gint selw, selh;
322         double width_points, height_points;
323         gint width, height;
324         GdkPixbuf *sel_pb, *page_pb;
325         gfloat x1, x2, y1, y2;  
326         
327
328         gint cur_page_num = 
329                 gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(viewer->cur_page));
330         
331         viewer->last_match = viewer->res_cnt;
332         
333         viewer->last_rect = NULL;
334         viewer->last_page_result = NULL;
335         if (cur_page_num != page_results->page_num) {
336                 /* we changed page. update the view */
337                 gtk_spin_button_set_value(GTK_SPIN_BUTTON(viewer->cur_page), 
338         (gdouble) page_results->page_num);
339         }
340                         
341         viewer->last_rect = rect;
342         viewer->last_page_result = page_results;
343
344         GTK_EVENTS_FLUSH();
345                                 
346         poppler_page_get_size(POPPLER_PAGE(viewer->pdf_page), &width_points, &height_points);
347         width = (int)((width_points * viewer->zoom) + 0.5);
348         height = (int)((height_points * viewer->zoom) + 0.5);
349
350         if (viewer->rotate == 90) {
351                 x1 = MIN(rect->y1,rect->y2) * viewer->zoom;
352                 x2 = MAX(rect->y1,rect->y2) * viewer->zoom;
353                 y1 = MAX(rect->x1,rect->x2) * viewer->zoom;
354                 y2 = MIN(rect->x1,rect->x2) * viewer->zoom;
355                 selw = (x2 - x1);
356                 selh = (y1 - y2);
357
358         } else if (viewer->rotate == 180) {
359                 x1 = width - rect->x2 * viewer->zoom;
360                 x2 = width - rect->x1 * viewer->zoom;
361                 y1 = height - rect->y2 * viewer->zoom;
362                 y2 = height - rect->y1 * viewer->zoom;
363                 selw = (x2 - x1);
364                 selh = (y2 - y1);
365                 y1 = height - y1;
366                 y2 = height - y2;
367
368         } else if (viewer->rotate == 270) {
369                 x1 = height - MAX(rect->y1,rect->y2) * viewer->zoom;
370                 x2 = height - MIN(rect->y1,rect->y2) * viewer->zoom;
371                 y1 = width - MIN(rect->x1,rect->x2) * viewer->zoom;
372                 y2 = width - MAX(rect->x1,rect->x2) * viewer->zoom;
373                 selw = (x2 - x1);
374                 selh = (y1 - y2);
375         } else {
376                 x1 = rect->x1 * viewer->zoom;
377                 x2 = rect->x2 * viewer->zoom;
378                 y1 = rect->y1 * viewer->zoom;
379                 y2 = rect->y2 * viewer->zoom;
380                 selw = (x2 - x1);
381                 selh = (y2 - y1);
382                 y1 = height - y1;
383                 y2 = height - y2;
384         }
385                                 
386         sel_pb = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, 
387                                                         selw, selh);
388
389         gdk_pixbuf_fill(sel_pb, SELECTION_COLOR);
390                                 
391         page_pb = gtk_image_get_pixbuf(GTK_IMAGE(viewer->pdf_view));
392                                 
393         page_pb = gdk_pixbuf_new(GDK_COLORSPACE_RGB, 
394                                         FALSE, 8, 
395                                 (int)(viewer->width * viewer->zoom), 
396                                 (int)(viewer->height * viewer->zoom));  
397         
398         poppler_page_render_to_pixbuf(viewer->pdf_page, 
399                                         0, 
400                                         0, 
401                                 (int)(viewer->width * viewer->zoom), 
402                                 (int)(viewer->height * viewer->zoom), 
403                                         viewer->zoom, 
404                                         viewer->rotate, 
405                                         page_pb);
406                                 
407         gdk_pixbuf_composite(sel_pb, page_pb, 
408                                         x1, y2, selw, selh, 0, 0, 
409                                         viewer->zoom, viewer->zoom, 
410                                         GDK_INTERP_BILINEAR, ALPHA_CHANNEL);
411                                 
412         gtk_image_set_from_pixbuf(GTK_IMAGE(viewer->pdf_view), page_pb);
413         
414         pdf_viewer_scroll_to(viewer, MIN(x1,x2), MIN(y1,y2));
415
416         g_object_unref(G_OBJECT(sel_pb));
417         g_object_unref(G_OBJECT(page_pb));
418
419 }
420
421 static gboolean pdf_viewer_text_search(MimeViewer *_viewer, gboolean backward,
422                                      const gchar *str, gboolean case_sens)
423 {
424         PdfViewer *viewer = (PdfViewer *)_viewer;
425         GList *all_pages_results, *cur_page_results;
426         viewer->res_cnt = 0;
427
428         debug_print("pdf_viewer_text_search: %s\n", str);
429         main_window_cursor_wait(mainwindow_get_mainwindow());
430         
431         if (viewer->last_search && strcmp(str, viewer->last_search)) {
432                 search_matches_free(viewer);
433                 viewer->last_match = -1;
434                 viewer->num_matches = 0;
435         } else if (!viewer->last_search) {
436                 viewer->last_match = -1;
437                 viewer->num_matches = 0;
438         }
439         /* It's a new search, build list of matches 
440          * across all pages */
441         if (viewer->last_match == -1) {
442                 gint i; 
443                 
444                 for(i = 1; i <= viewer->num_pages; i++) {
445                         
446                         PopplerPage *pdf_page = poppler_document_get_page(viewer->pdf_doc, i - 1);
447                         viewer->page_results = poppler_page_find_text(pdf_page, str);
448                         
449                         if (viewer->page_results != NULL) {
450                                 debug_print("page_results %p\n", viewer->page_results);
451                                 /* store results for this page */
452                                 gint num_res = 0;
453                                 PageResult *res = g_new0(PageResult, 1);
454                                 res->results = viewer->page_results;
455                                 res->page_num = i;
456                                 /* found text, prepend this page(faster than append) */
457                                 viewer->text_found = g_list_prepend(viewer->text_found, res);
458                                 num_res = g_list_length(viewer->page_results);
459                                 debug_print("%d results on page %d\n", num_res, i);
460                                 viewer->num_matches += num_res;
461                         }
462                         g_object_unref(G_OBJECT(pdf_page));
463                 }
464
465                 if (viewer->text_found == NULL) {
466                         main_window_cursor_normal(mainwindow_get_mainwindow());
467                         return FALSE;
468                 }
469                 /* put back the list in the correct order */
470                 viewer->text_found = g_list_reverse(viewer->text_found);
471         } 
472         
473         if (!viewer->text_found) {
474                 main_window_cursor_normal(mainwindow_get_mainwindow());
475                 return FALSE;
476         } else {
477                 viewer->last_search = g_strdup(str);
478         }
479         
480         if (backward) {
481                 /* if backward, we have to initialize stuff to search 
482                  * from the end */
483                 viewer->res_cnt = viewer->num_matches-1;
484                 if (viewer->last_match == -1) {
485                         viewer->last_match = viewer->num_matches+1;
486                 }
487                 all_pages_results = g_list_last(viewer->text_found);
488         } 
489         else {
490                 all_pages_results = viewer->text_found;
491         }
492         
493         for(; all_pages_results; all_pages_results = (backward?all_pages_results->prev:all_pages_results->next)) {
494                 
495                 PageResult * page_results = (PageResult *)all_pages_results->data;
496
497                 if (backward) {
498                         cur_page_results = g_list_last(page_results->results);
499                 }
500                 else {
501                         cur_page_results = page_results->results;
502                 }
503
504                 for(; cur_page_results; cur_page_results = (backward?cur_page_results->prev:cur_page_results->next)) {
505
506                         gboolean valid = FALSE;
507                         /* first valid result is the last+1 if searching
508                          * forward, last-1 if searching backward */
509                         if (backward) {
510                                 valid = (viewer->res_cnt < viewer->last_match);
511                         }
512                         else {
513                                 valid = (viewer->res_cnt > viewer->last_match);
514                         }
515                         if (valid) {
516                                 pdf_viewer_render_selection(viewer, 
517                                         (PopplerRectangle *)cur_page_results->data,
518                                                 page_results);
519                                 main_window_cursor_normal(mainwindow_get_mainwindow());
520                                 return TRUE;
521                         }
522                         
523                         if (backward) {
524                                 viewer->res_cnt--;
525                         }
526                         else {
527                                 viewer->res_cnt++;
528                         }
529                 }
530         }
531         main_window_cursor_normal(mainwindow_get_mainwindow());
532         search_matches_free(viewer);
533         return FALSE;
534 }
535
536 static void pdf_viewer_get_document_index(PdfViewer *viewer, PopplerIndexIter *index_iter, GtkTreeIter *parentiter)
537 {
538         PopplerAction *action;
539         PopplerIndexIter *child;
540         GtkTreeIter childiter;
541
542         debug_print("get document index\n");
543         do      {
544                 gint page_num = 0;
545                 
546                 action = poppler_index_iter_get_action(index_iter);
547
548                 if (action->type != POPPLER_ACTION_GOTO_DEST) {
549                         poppler_action_free(action);
550                         continue;
551                 }
552
553                 if (action->goto_dest.dest->type == POPPLER_DEST_XYZ || action->goto_dest.dest->type == POPPLER_DEST_FITH) {
554                         page_num = action->goto_dest.dest->page_num;
555                 }
556 #ifdef HAVE_POPPLER_DEST_NAMED
557                 else if (action->goto_dest.dest->type == POPPLER_DEST_NAMED) {
558                         PopplerDest *dest = poppler_document_find_dest(
559                                         viewer->pdf_doc, action->goto_dest.dest->named_dest);
560                         if (dest->type != POPPLER_DEST_XYZ) {
561                                 g_warning("couldn't figure out link\n");
562                                 poppler_dest_free(dest);
563                                 continue;
564                         }
565                         page_num = dest->page_num;
566                         poppler_dest_free(dest);
567                 } 
568 #endif
569                 else {
570 #ifdef HAVE_POPPLER_DEST_NAMED
571                         g_warning("unhandled link type %d\nplease contact developers\n", action->goto_dest.dest->type);
572 #else
573                         g_warning("unhandled link type %d\nplease upgrade libpoppler-glib to 0.5.4\n", action->goto_dest.dest->type);
574 #endif
575                         continue;
576                 }
577                 gtk_tree_store_append(GTK_TREE_STORE(viewer->index_model), &childiter, parentiter);
578                 gtk_tree_store_set(GTK_TREE_STORE(viewer->index_model), &childiter,
579                                                 INDEX_NAME, action->named.title,
580                                                 INDEX_PAGE, page_num,
581                                                 INDEX_TOP, action->goto_dest.dest->top,
582                                                 -1);
583                 poppler_action_free(action);
584                 child = poppler_index_iter_get_child(index_iter);
585                 if (child) {
586                         pdf_viewer_get_document_index(viewer, child, &childiter);
587                         poppler_index_iter_free(child);
588                 }
589         }
590         while(poppler_index_iter_next(index_iter));
591 }
592
593 static void pdf_viewer_index_row_activated(GtkTreeView          *tree_view,
594                                         GtkTreePath             *path,
595                                         GtkTreeViewColumn       *column,
596                                         gpointer                 data)
597 {
598         GtkTreeIter iter;
599         GtkTreeModel *model = gtk_tree_view_get_model(tree_view);
600         PdfViewer *viewer = (PdfViewer *)data;
601         gint page_num = 0;
602         
603         debug_print("index_row_activated\n");
604         if (!gtk_tree_model_get_iter(model, &iter, path)) return;
605
606         gtk_tree_model_get(model, &iter, 
607                            INDEX_PAGE, &page_num,
608                            -1);
609
610         if (page_num > 0) {
611                 gtk_spin_button_set_value(GTK_SPIN_BUTTON(viewer->cur_page),(gdouble)page_num);
612                 debug_print("Page num: %d\n", page_num);
613         }
614         GTK_EVENTS_FLUSH();
615 }
616
617 /** Disable the index button if the document doesn't have an index */
618 static void pdf_viewer_set_index_button_sensitive(PdfViewer *viewer)
619 {
620         viewer->pdf_index  = poppler_index_iter_new(viewer->pdf_doc);
621         if (viewer->pdf_index) {        
622                 if (!gtk_widget_is_sensitive(viewer->doc_index)) {
623                         gtk_widget_set_sensitive(viewer->doc_index, TRUE);
624                 }
625         }
626         else {
627                 gtk_widget_set_sensitive(viewer->doc_index, FALSE);
628         }
629
630     poppler_index_iter_free(viewer->pdf_index);
631     viewer->pdf_index = NULL;
632 }
633
634 static char * pdf_viewer_get_document_format_data(GTime utime) 
635 {
636         time_t time = (time_t) utime;
637         struct tm t;
638         char s[256];
639         const char *fmt_hack = "%c";
640         size_t len;
641
642         if (time == 0 || !localtime_r(&time, &t)) return NULL;
643
644         len = strftime(s, sizeof(s), fmt_hack, &t);
645         
646         if (len == 0 || s[0] == '\0') return NULL;
647
648         return g_locale_to_utf8(s, -1, NULL, NULL, NULL);
649 }
650
651 #define ADD_TO_TABLE(LABEL, VALUE) \
652         label = gtk_label_new(LABEL); \
653         gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5); \
654         gtk_misc_set_padding(GTK_MISC(label), 4, 0); \
655         gtk_table_attach(viewer->table_doc_info, label, 0, 1, row, row+1, GTK_EXPAND | GTK_FILL, 0, 0, 0); \
656         \
657         label = gtk_label_new(VALUE); \
658         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5); \
659         gtk_misc_set_padding(GTK_MISC(label), 4, 0); \
660         gtk_table_attach(viewer->table_doc_info, label, 1, 2, row, row+1, GTK_EXPAND | GTK_FILL, 0, 0, 0); \
661         row++;
662
663         
664 static GtkTable * pdf_viewer_fill_info_table(PdfViewer *viewer)
665 {
666         GtkWidget *label;
667         const gchar *title, *format, *author, *subject, *keywords, *creator, *producer;
668         gboolean linearized;
669         gchar *tmp;
670         gint row = 0;
671
672         GTime creation_date, mod_date;
673
674         PopplerPageLayout layout;
675         PopplerPageMode mode;
676         PopplerPermissions permissions;
677         PopplerViewerPreferences view_prefs;
678
679         title = format = author = subject = keywords = creator = producer = tmp = 0;
680
681         g_object_get(viewer->pdf_doc,
682                                 "title", &title,
683                                 "format", &format,
684                                 "author", &author,
685                                 "subject", &subject,
686                                 "keywords", &keywords,
687                                 "creation-date", &creation_date,
688                                 "permissions", &permissions,
689                                 "mod-date", &mod_date,
690                                 "creator", &creator,
691                                 "producer", &producer,  
692                                 "linearized", &linearized,
693                                 "page-mode", &mode,
694                                 "page-layout", &layout,
695                                 "viewer-preferences", &view_prefs,
696                                 NULL);
697
698         viewer->table_doc_info = GTK_TABLE(gtk_table_new(13, 2, FALSE));
699
700         ADD_TO_TABLE(_("Filename:"), viewer->target_filename)
701         ADD_TO_TABLE(_("Size:"), to_human_readable(viewer->to_load->length))
702         ADD_TO_TABLE(NULL, NULL)
703         ADD_TO_TABLE(_("Title:"), title)
704         ADD_TO_TABLE(_("Subject:"), subject)
705         ADD_TO_TABLE(_("Author:"), author)
706         ADD_TO_TABLE(_("Keywords:"), keywords)
707         ADD_TO_TABLE(_("Creator:"), creator)
708         ADD_TO_TABLE(_("Producer:"), producer)
709
710         tmp = pdf_viewer_get_document_format_data(creation_date);
711         ADD_TO_TABLE(_("Created:"), tmp)
712         g_free(tmp);
713
714         tmp = pdf_viewer_get_document_format_data(mod_date);
715         ADD_TO_TABLE(_("Modified:"), tmp)
716         g_free(tmp);
717         
718         ADD_TO_TABLE(_("Format:"), format)
719         if (linearized) {
720                 ADD_TO_TABLE(_("Optimized:"), _("Yes"))
721         }
722         else {
723                 ADD_TO_TABLE(_("Optimized:"), _("No"))
724         }
725         //ADD_TO_TABLE(_("Page Mode:"), pdf_viewer_get_document_info_mode(mode)) 
726         //ADD_TO_TABLE(_("Page Layout:"), pdf_viewer_get_document_info_layout(layout))
727
728         return(GtkTable *) viewer->table_doc_info;
729 }
730 #undef ADD_TO_TABLE
731
732 static FileType pdf_viewer_mimepart_get_type(MimeInfo *partinfo)
733 {
734         gchar *content_type = NULL;
735         FileType type = TYPE_UNKNOWN;
736         debug_print("mimepart_get_type\n");
737         if ((partinfo->type == MIMETYPE_APPLICATION) &&
738         (!g_ascii_strcasecmp(partinfo->subtype, "octet-stream"))) {
739                 
740                 const gchar *filename;
741
742                 filename = procmime_mimeinfo_get_parameter(partinfo, "filename");
743                 
744                         if (filename == NULL)
745                                 filename = procmime_mimeinfo_get_parameter(partinfo, "name");
746                         if (filename != NULL)
747                                 content_type = procmime_get_mime_type(filename);
748         } 
749         else {
750                 content_type = procmime_get_content_type_str(partinfo->type, partinfo->subtype);
751         }
752
753         if (content_type == NULL) type = TYPE_UNKNOWN;
754         else if (!strcmp(content_type, "application/pdf")) type = TYPE_PDF;
755         else if (!strcmp(content_type, "application/postscript")) type = TYPE_PS;
756         else type = TYPE_UNKNOWN;
757         
758         g_free(content_type);
759         return type;
760 }
761
762 /* Callbacks */
763 static void pdf_viewer_button_first_page_cb(GtkButton *button, PdfViewer *viewer) 
764 {
765         
766         gtk_spin_button_spin(GTK_SPIN_BUTTON(viewer->cur_page), GTK_SPIN_HOME, 1);
767 }
768
769 static void pdf_viewer_button_prev_page_cb(GtkButton *button, PdfViewer *viewer) 
770 {
771         
772         gtk_spin_button_spin(GTK_SPIN_BUTTON(viewer->cur_page), GTK_SPIN_STEP_BACKWARD, 1);
773 }
774
775 static void pdf_viewer_button_next_page_cb(GtkButton *button, PdfViewer *viewer) 
776 {
777         
778         gtk_spin_button_spin(GTK_SPIN_BUTTON(viewer->cur_page), GTK_SPIN_STEP_FORWARD, 1);
779 }
780
781 static void pdf_viewer_button_last_page_cb(GtkButton *button, PdfViewer *viewer) 
782 {
783         
784         gtk_spin_button_spin(GTK_SPIN_BUTTON(viewer->cur_page), GTK_SPIN_END, 1);
785 }
786
787 static void pdf_viewer_spin_change_page_cb(GtkSpinButton *button, PdfViewer *viewer)
788 {
789         pdf_viewer_update((MimeViewer *)viewer, 
790                         FALSE, 
791                         gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(viewer->cur_page)));
792 }
793
794 static void pdf_viewer_button_zoom_in_cb(GtkButton *button, PdfViewer *viewer) 
795 {
796
797         gtk_spin_button_spin(GTK_SPIN_BUTTON(viewer->zoom_scroll), GTK_SPIN_STEP_FORWARD, ZOOM_FACTOR);
798 }
799
800 static void pdf_viewer_spin_zoom_scroll_cb(GtkSpinButton *button, PdfViewer *viewer)
801 {
802         viewer->zoom = gtk_spin_button_get_value(GTK_SPIN_BUTTON(viewer->zoom_scroll));
803         pdf_viewer_update((MimeViewer *)viewer,
804                         FALSE,
805                         gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(viewer->cur_page)));
806 }
807
808 static void pdf_viewer_button_zoom_out_cb(GtkButton *button, PdfViewer *viewer) 
809 {
810
811         gtk_spin_button_spin(GTK_SPIN_BUTTON(viewer->zoom_scroll), GTK_SPIN_STEP_BACKWARD, ZOOM_FACTOR);
812         
813 }
814
815 static void pdf_viewer_button_press_events_cb(GtkWidget *widget, GdkEventButton *event, PdfViewer *viewer) 
816 {
817         gchar *uri;
818         #ifdef HAVE_POPPLER_DEST_NAMED
819         PopplerDest *dest;
820         #endif
821         static GdkCursor *hand_cur = NULL;
822         
823         if (!hand_cur) hand_cur = gdk_cursor_new(GDK_FLEUR);
824
825         /* Execute Poppler Links */
826         if (event->button == 1 && viewer->in_link) {
827                 switch (viewer->link_action->type) {
828                 case POPPLER_ACTION_UNKNOWN:
829                         debug_print("action unknown\n");
830                         break;
831                 case POPPLER_ACTION_GOTO_DEST:
832                         if (viewer->link_action->goto_dest.dest->type == POPPLER_DEST_XYZ || 
833                                         viewer->link_action->goto_dest.dest->type == POPPLER_DEST_FITH) {
834                                 gtk_spin_button_set_value(GTK_SPIN_BUTTON(viewer->cur_page), 
835                                                                           (gdouble)viewer->link_action->goto_dest.dest->page_num);
836                         }
837                 #ifdef HAVE_POPPLER_DEST_NAMED
838                         else if (viewer->link_action->goto_dest.dest->type == POPPLER_DEST_NAMED) {
839                                 dest = poppler_document_find_dest(
840                                         viewer->pdf_doc, viewer->link_action->goto_dest.dest->named_dest);
841                         if (dest->type != POPPLER_DEST_XYZ) {
842                                 g_warning("couldn't figure out link\n");
843                                 poppler_dest_free(dest);
844                                 break;
845                         }
846                         gtk_spin_button_set_value(GTK_SPIN_BUTTON(viewer->cur_page), 
847                                                                           (gdouble)dest->page_num);
848                         poppler_dest_free(dest);
849                 } 
850                 #endif
851                         break;
852                 case POPPLER_ACTION_GOTO_REMOTE:
853                         #ifdef HAVE_POPPLER_DEST_NAMED
854                         dest = poppler_document_find_dest(
855                                         viewer->pdf_doc, viewer->link_action->goto_remote.dest->named_dest);
856                         if (dest->type != POPPLER_DEST_XYZ) {
857                                 g_warning ("couldn't figure out link\n");
858                                 poppler_dest_free(dest);
859                                 break;
860                         }
861                         gtk_spin_button_set_value(GTK_SPIN_BUTTON(viewer->cur_page),
862                                                                           (gdouble)dest->page_num);
863                         poppler_dest_free(dest);
864                         #endif
865                         break;
866                 case POPPLER_ACTION_LAUNCH:
867                         debug_print("action launch not yet implemented\n");
868                         break;
869                 case POPPLER_ACTION_URI:
870                         uri = g_strdup(viewer->link_action->uri.uri);
871                         if (!g_ascii_strncasecmp(uri, "mailto:", 7)) 
872                                 compose_new(NULL, uri + 7, NULL);
873                         else 
874                                 open_uri(uri, prefs_common_get_uri_cmd());
875                         g_free(uri);
876                         break;
877                 case POPPLER_ACTION_NAMED:
878                         debug_print("action named not yet implemented\n");
879                         break;
880                 case POPPLER_ACTION_MOVIE:
881                         debug_print("yoyoyo ;-) a movie?\n");
882                         break;
883                 }
884                 if (((MimeViewer *)viewer)->mimeview && 
885                         ((MimeViewer *)viewer)->mimeview->messageview && 
886                         ((MimeViewer *)viewer)->mimeview->messageview->window && 
887                         ((MimeViewer *)viewer)->mimeview->messageview->window->window) 
888                         gdk_window_set_cursor (((MimeViewer *)viewer)->mimeview->messageview->window->window, NULL);
889                 else
890                         gdk_window_set_cursor (mainwindow_get_mainwindow()->window->window, NULL);
891         }
892
893         /* Init document to be scrolled with left mouse click */
894         if (event->button == 1 && !viewer->in_link) {
895                 viewer->pdf_view_scroll = TRUE;
896                 if (((MimeViewer *)viewer)->mimeview && 
897                         ((MimeViewer *)viewer)->mimeview->messageview && 
898                         ((MimeViewer *)viewer)->mimeview->messageview->window && 
899                         ((MimeViewer *)viewer)->mimeview->messageview->window->window) 
900                         gdk_window_set_cursor (((MimeViewer *)viewer)->mimeview->messageview->window->window, hand_cur);
901                 else
902                         gdk_window_set_cursor (mainwindow_get_mainwindow()->window->window, hand_cur);
903
904                 viewer->last_x = event->x;
905                 viewer->last_y = event->y;
906                 viewer->last_dir_x = 0;
907                 viewer->last_dir_y = 0;
908         }
909 }
910 /* Set the normal cursor*/
911 static void pdf_viewer_mouse_scroll_destroy_cb(GtkWidget *widget, GdkEventButton *event, PdfViewer *viewer) 
912 {
913         
914         if (event->button == 1) {
915                 viewer->pdf_view_scroll = FALSE;
916                 if (((MimeViewer *)viewer)->mimeview && 
917                         ((MimeViewer *)viewer)->mimeview->messageview && 
918                         ((MimeViewer *)viewer)->mimeview->messageview->window && 
919                         ((MimeViewer *)viewer)->mimeview->messageview->window->window) 
920                         gdk_window_set_cursor (((MimeViewer *)viewer)->mimeview->messageview->window->window, NULL);
921                 else
922                         gdk_window_set_cursor (mainwindow_get_mainwindow()->window->window, NULL);
923         }
924 }
925
926 static void pdf_viewer_move_events_cb(GtkWidget *widget, GdkEventMotion *event, PdfViewer *viewer) 
927 {
928         /* Grab the document and scroll it with mouse */ 
929         if (viewer->pdf_view_scroll) {
930
931                 viewer->pdf_view_vadj = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(viewer->scrollwin));
932                 viewer->pdf_view_hadj = gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(viewer->scrollwin));
933                         
934                         if (event->x < viewer->last_x
935                                         && viewer->pdf_view_hadj->value < (viewer->pdf_view_hadj->upper - viewer->pdf_view_hadj->page_size)) {
936                                 if (viewer->last_dir_x == -1) {
937                                         viewer->pdf_view_hadj->value += viewer->last_x - event->x; 
938                                         g_signal_emit_by_name(G_OBJECT(viewer->pdf_view_hadj),
939                                                                 "value_changed", 0);
940                                 }
941                                 viewer->last_dir_x = -1;
942                         }
943                         else if (event->x > viewer->last_x
944                                         && viewer->pdf_view_hadj->value > 0.0)  {
945                                 if (viewer->last_dir_x == +1) {
946                                         viewer->pdf_view_hadj->value += viewer->last_x - event->x; 
947                                         g_signal_emit_by_name(G_OBJECT(viewer->pdf_view_hadj),
948                                                                 "value_changed", 0);
949                                 }
950                                 viewer->last_dir_x = +1;
951                         }
952
953                         if (event->y < viewer->last_y
954                                         && viewer->pdf_view_vadj->value < (viewer->pdf_view_vadj->upper - viewer->pdf_view_vadj->page_size)) {
955                                 if (viewer->last_dir_y == -1) {
956                                         viewer->pdf_view_vadj->value += viewer->last_y - event->y; 
957                                         g_signal_emit_by_name(G_OBJECT(viewer->pdf_view_vadj),
958                                                                 "value_changed", 0);
959                                 }
960                                 viewer->last_dir_y = -1;
961                         }
962                         else if (event->y > viewer->last_y
963                                         && viewer->pdf_view_vadj->value > 0.0)  {
964                                 if (viewer->last_dir_y == +1) {
965                                         viewer->pdf_view_vadj->value += viewer->last_y - event->y; 
966                                         g_signal_emit_by_name(G_OBJECT(viewer->pdf_view_vadj),
967                                                                 "value_changed", 0);
968                                 }
969                                 viewer->last_dir_y = +1;
970                         }
971                         viewer->last_x = event->x;
972                         viewer->last_y = event->y;
973                         GTK_EVENTS_FLUSH();
974                 } 
975         else {  
976         /* Link Mapping */
977         static GList *l;
978         static GdkCursor *link_cur = NULL;
979         static GtkRequisition size;
980         static gdouble x,y, x1, y1, x2, y2;
981         gboolean ccur;
982         
983         viewer->pdf_view_vadj = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(viewer->scrollwin));
984         viewer->pdf_view_hadj = gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(viewer->scrollwin));
985         
986         if (!link_cur) link_cur = gdk_cursor_new(GDK_HAND2);
987
988         ccur = FALSE;
989         viewer->in_link = FALSE;        
990         for (l = viewer->link_map; l; l = g_list_next (l)) {
991                 PopplerLinkMapping *lmapping;
992                 lmapping = (PopplerLinkMapping *)l->data;
993
994                 x1 = lmapping->area.x1;
995                 y1 = lmapping->area.y1;
996                 x2 = lmapping->area.x2;
997                 y2 = lmapping->area.y2;
998                 gtk_widget_size_request(viewer->pdf_view, &size);
999                 
1000                 switch (viewer->rotate) {
1001                 case 0:
1002                 case 360:
1003                                 if (size.width != viewer->pdf_view_hadj->upper)
1004                                         x = (event->x - (viewer->pdf_view_hadj->upper - size.width) / 2) / viewer->zoom;
1005                                 else
1006                                         x = event->x / viewer->zoom;
1007
1008                                 y = (viewer->pdf_view_vadj->upper - event->y) / viewer->zoom;
1009                         break;
1010                 case 90:
1011                                 if (size.width != viewer->pdf_view_hadj->upper)
1012                                         y = (event->x - (viewer->pdf_view_hadj->upper - size.width) / 2) / viewer->zoom;
1013                                 else
1014                                         y = event->x / viewer->zoom;
1015
1016                                 x = (event->y) / viewer->zoom;
1017                         break;
1018                 case 180:
1019                                 if (size.width != viewer->pdf_view_hadj->upper)
1020                                         x = ((viewer->pdf_view_hadj->upper -  event->x) - ((viewer->pdf_view_hadj->upper - size.width) / 2)) / viewer->zoom;
1021                                 else
1022                                         x =  ((viewer->pdf_view_hadj->upper -  event->x) - (viewer->pdf_view_hadj->upper - size.width)) / viewer->zoom;
1023
1024                                 y = (event->y) / viewer->zoom;
1025                         break;
1026                 case 270:
1027                                 if (size.width != viewer->pdf_view_hadj->upper)
1028                                         y = ((viewer->pdf_view_hadj->upper -  event->x) - ((viewer->pdf_view_hadj->upper - size.width) / 2)) / viewer->zoom;
1029                                 else
1030                                         y =  ((viewer->pdf_view_hadj->upper -  event->x) - (viewer->pdf_view_hadj->upper - size.width)) / viewer->zoom;
1031
1032                                 x = (viewer->pdf_view_vadj->upper - event->y) / viewer->zoom;
1033                         break;
1034                 }
1035
1036                 if ( (x > x1 && x < x2) && (y > y1 && y < y2) ) {
1037                                 viewer->in_link = TRUE;
1038                         if (((MimeViewer *)viewer)->mimeview && 
1039                                 ((MimeViewer *)viewer)->mimeview->messageview && 
1040                                 ((MimeViewer *)viewer)->mimeview->messageview->window && 
1041                                 ((MimeViewer *)viewer)->mimeview->messageview->window->window) 
1042                                         gdk_window_set_cursor (((MimeViewer *)viewer)->mimeview->messageview->window->window, link_cur);
1043                                 else
1044                                         gdk_window_set_cursor (mainwindow_get_mainwindow()->window->window, link_cur);
1045                                 
1046                                 viewer->link_action = lmapping->action; 
1047                                 ccur = TRUE;
1048                 }
1049                 if (!ccur) {
1050                         if (((MimeViewer *)viewer)->mimeview && 
1051                                 ((MimeViewer *)viewer)->mimeview->messageview && 
1052                                 ((MimeViewer *)viewer)->mimeview->messageview->window && 
1053                                 ((MimeViewer *)viewer)->mimeview->messageview->window->window) 
1054                                 gdk_window_set_cursor (((MimeViewer *)viewer)->mimeview->messageview->window->window, NULL);
1055                         else
1056                                 gdk_window_set_cursor (mainwindow_get_mainwindow()->window->window, NULL);
1057                 }
1058         }
1059         g_free(l);
1060         }
1061 }
1062 static gboolean pdf_viewer_scroll_cb(GtkWidget *widget, GdkEventScroll *event,
1063                                     PdfViewer *viewer)
1064 {
1065         GtkAdjustment *adj = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(viewer->scrollwin));
1066         static gboolean in_scroll_cb = FALSE;
1067         gboolean handled = FALSE;
1068         gint cur_p = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(viewer->cur_page));               
1069
1070         if (in_scroll_cb)
1071                 return FALSE;
1072
1073         in_scroll_cb = TRUE;
1074
1075         if (event->direction == GDK_SCROLL_UP &&
1076             adj->value == adj->lower && 
1077             cur_p > 1) {
1078                 gtk_spin_button_spin(GTK_SPIN_BUTTON(viewer->cur_page), GTK_SPIN_STEP_BACKWARD, 1);
1079                 adj->value = adj->upper - adj->page_size;
1080                 handled = TRUE;
1081         } else if (event->direction == GDK_SCROLL_DOWN &&
1082             adj->value + adj->page_size == adj->upper &&
1083             cur_p < viewer->num_pages) {
1084                 gtk_spin_button_spin(GTK_SPIN_BUTTON(viewer->cur_page), GTK_SPIN_STEP_FORWARD, 1);
1085                 adj->value = 0.0;
1086                 handled = TRUE;
1087         }
1088         in_scroll_cb = FALSE;
1089         return handled;
1090 }
1091
1092 static void pdf_viewer_button_zoom_fit_cb(GtkButton *button, PdfViewer *viewer)
1093 {
1094         GtkAllocation *allocation;
1095         double xratio, yratio;
1096         allocation = &(viewer->scrollwin->allocation);
1097         debug_print("width: %d\n", allocation->width);
1098         debug_print("height: %d\n", allocation->height);
1099         xratio = allocation->width / viewer->width;
1100         yratio = allocation->height / viewer->height;
1101
1102         if (xratio >= yratio) {
1103                 viewer->zoom = yratio;
1104                 gtk_spin_button_set_value(GTK_SPIN_BUTTON(viewer->zoom_scroll),viewer->zoom);
1105         }
1106         else {
1107                 viewer->zoom = xratio;
1108                 gtk_spin_button_set_value(GTK_SPIN_BUTTON(viewer->zoom_scroll),viewer->zoom);
1109         }
1110 }
1111
1112 static void pdf_viewer_button_zoom_width_cb(GtkButton *button, PdfViewer *viewer)
1113 {
1114         GtkAllocation *allocation;
1115         double xratio;
1116         allocation = &(viewer->scrollwin->allocation);
1117         debug_print("width: %d\n", allocation->width);
1118         xratio = allocation->width / viewer->width;
1119         gtk_spin_button_set_value(GTK_SPIN_BUTTON(viewer->zoom_scroll), xratio);
1120 }
1121
1122 static void pdf_viewer_button_rotate_right_cb(GtkButton *button, PdfViewer *viewer)
1123 {
1124         if (viewer->rotate == 360) {
1125                 viewer->rotate = 0;
1126         }
1127         
1128         viewer->rotate += (gint) ROTATION;
1129         pdf_viewer_update((MimeViewer *)viewer, FALSE,
1130                 gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(viewer->cur_page)));
1131 }
1132
1133 static void pdf_viewer_button_rotate_left_cb(GtkButton *button, PdfViewer *viewer)
1134 {
1135         if (viewer->rotate == 0) {
1136                 viewer->rotate = 360;
1137         }
1138         
1139         viewer->rotate = abs(viewer->rotate -(gint) ROTATION);
1140         pdf_viewer_update((MimeViewer *)viewer, FALSE,
1141                 gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(viewer->cur_page)));
1142 }
1143
1144 /* Show/Hide the index pane */
1145 static void pdf_viewer_show_document_index_cb(GtkButton *button, PdfViewer *viewer)
1146 {
1147         if (!viewer->pdf_index) {
1148                 viewer->pdf_index = poppler_index_iter_new(viewer->pdf_doc);
1149         }
1150         
1151         gtk_tree_store_clear(GTK_TREE_STORE(viewer->index_model));
1152         
1153         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(viewer->doc_index))) {
1154                 pdf_viewer_get_document_index(viewer,(PopplerIndexIter *) viewer->pdf_index, NULL);
1155                 gtk_widget_show(GTK_WIDGET(viewer->frame_index));
1156         }
1157         else {
1158                 pdf_viewer_hide_index_pane(viewer);
1159         }
1160                 
1161 }
1162
1163 static void pdf_viewer_button_document_info_cb(GtkButton *button, PdfViewer *viewer)
1164 {
1165         gchar *buf;
1166         buf = g_strdup_printf(_("PDF Viewer Plugin"));
1167
1168         alertpanel_full(buf, NULL, GTK_STOCK_CLOSE, NULL, NULL,
1169                                         FALSE,(GtkWidget *) pdf_viewer_fill_info_table(viewer), 
1170                                         ALERT_NOTICE, 
1171                                         G_ALERTDEFAULT);
1172         g_free(buf);
1173 }
1174
1175 /*
1176 static const char * poppler_get_document_info_mode(PopplerPageMode mode)
1177 {
1178         GEnumValue *enum_value;
1179
1180         enum_value = g_enum_get_value((GEnumClass *) g_type_class_peek(POPPLER_TYPE_PAGE_MODE), mode);
1181         return(gchar *) enum_value->value_name;
1182 }
1183 static const char * poppler_get_document_info_layout(PopplerPageLayout layout)
1184 {
1185
1186         GEnumValue *enum_value;
1187
1188         enum_value = g_enum_get_value((GEnumClass *) g_type_class_peek(POPPLER_TYPE_PAGE_LAYOUT), layout);
1189         return(gchar *) enum_value->value_name;
1190 }
1191 */
1192 static void pdf_viewer_show_controls(PdfViewer *viewer, gboolean show)
1193 {
1194         if (show) {
1195                 gtk_widget_show(viewer->first_page);
1196                 gtk_widget_show(viewer->cur_page);
1197                 gtk_widget_show(viewer->prev_page);
1198                 gtk_widget_show(viewer->next_page);
1199                 gtk_widget_show(viewer->last_page);
1200                 gtk_widget_show(viewer->zoom_in);
1201                 gtk_widget_show(viewer->zoom_out);
1202                 gtk_widget_show(viewer->zoom_fit);
1203                 gtk_widget_show(viewer->zoom_width);
1204                 gtk_widget_show(viewer->zoom_scroll);
1205                 gtk_widget_show(viewer->widgets_table);
1206                 gtk_widget_show(viewer->rotate_right);
1207                 gtk_widget_show(viewer->rotate_left);
1208                 gtk_widget_show(viewer->doc_info);
1209                 gtk_widget_show(viewer->doc_index);
1210         } else {
1211                 gtk_widget_hide(viewer->first_page);
1212                 gtk_widget_hide(viewer->cur_page);
1213                 gtk_widget_hide(viewer->prev_page);
1214                 gtk_widget_hide(viewer->next_page);
1215                 gtk_widget_hide(viewer->last_page);
1216                 gtk_widget_hide(viewer->zoom_in);
1217                 gtk_widget_hide(viewer->zoom_out);
1218                 gtk_widget_hide(viewer->zoom_fit);
1219                 gtk_widget_hide(viewer->zoom_width);
1220                 gtk_widget_hide(viewer->widgets_table);
1221                 gtk_widget_hide(viewer->rotate_right);
1222                 gtk_widget_hide(viewer->rotate_left);
1223                 gtk_widget_hide(viewer->doc_info);
1224                 gtk_widget_hide(viewer->doc_index);
1225                 gtk_widget_hide(viewer->zoom_scroll);
1226         }
1227 }
1228 /** Render the current page, page_num on the viewer */
1229 static void pdf_viewer_update(MimeViewer *_viewer, gboolean reload_file, int page_num) 
1230 {
1231
1232         PdfViewer *viewer = (PdfViewer *) _viewer;
1233         GError *error;
1234         gchar *tmpfile = NULL;
1235         gchar *tmp;
1236
1237         debug_print("pdf_viewer_update\n");
1238
1239         error = NULL;
1240         if (reload_file) {
1241                 if (viewer->pdf_doc) {
1242                         g_object_unref(G_OBJECT(viewer->pdf_doc));
1243                         viewer->pdf_doc = NULL;
1244                 }
1245                 
1246                 if (pdf_viewer_mimepart_get_type(viewer->to_load) == TYPE_PS) {
1247                         stock_pixbuf_gdk(viewer->hbox, 
1248                                         STOCK_PIXMAP_MIME_PS, 
1249                                         &viewer->icon_pixbuf);
1250                         gtk_image_set_from_pixbuf(GTK_IMAGE(viewer->icon_type),
1251                                                                         viewer->icon_pixbuf);
1252                 } 
1253                 else if (pdf_viewer_mimepart_get_type(viewer->to_load) == TYPE_PDF) {
1254                         stock_pixbuf_gdk(viewer->hbox, 
1255                         STOCK_PIXMAP_MIME_PDF, 
1256                         &viewer->icon_pixbuf);
1257                         gtk_image_set_from_pixbuf(GTK_IMAGE(viewer->icon_type), 
1258                                                                         viewer->icon_pixbuf);
1259                 } 
1260                 else {
1261                         stock_pixbuf_gdk(viewer->hbox, 
1262                         STOCK_PIXMAP_MIME_APPLICATION, 
1263                         &viewer->icon_pixbuf);
1264                         gtk_image_set_from_pixbuf(GTK_IMAGE(viewer->icon_type), 
1265                                                                         viewer->icon_pixbuf);
1266                 }
1267
1268                 gtk_label_set_text(GTK_LABEL(viewer->doc_label), _("Loading..."));      
1269                 pdf_viewer_show_controls(viewer, FALSE);
1270                 main_window_cursor_wait(mainwindow_get_mainwindow());
1271
1272                 GTK_EVENTS_FLUSH();
1273
1274                 if (pdf_viewer_mimepart_get_type(viewer->to_load) == TYPE_PS) {
1275                         gchar *cmdline = NULL, *tmp = NULL, *gspath = NULL;
1276                         gint result = 0;
1277
1278                         if ((gspath = g_find_program_in_path("gs")) != NULL) {
1279                                 g_free(gspath);
1280                                 /* convert postscript to pdf */
1281                                 tmpfile = get_tmp_file();
1282                                 cmdline = g_strdup_printf(
1283                                         "gs -dSAFER -dCompatibilityLevel=1.2 -q -dNOPAUSE -dBATCH "
1284                                           "-sDEVICE=pdfwrite -sOutputFile=%s -c .setpdfwrite -f \"%s\"",
1285                                         tmpfile, viewer->filename);
1286                                 result = execute_command_line(cmdline, FALSE);
1287                                 if (result == 0) {
1288                                         tmp = g_filename_to_uri(tmpfile, NULL, NULL);
1289                                         viewer->pdf_doc = poppler_document_new_from_file( tmp, NULL, &error);
1290                                         g_free(tmp);
1291                                 } 
1292                                 else {
1293                                         g_warning("gs conversion failed: %s returned %d\n", cmdline, result);
1294                                         tmp = g_strdup_printf("gs: err %d", result);
1295                                         alertpanel_warning("%s", tmp);
1296                                         g_free(tmp);
1297                                 }
1298         
1299                                 g_free(cmdline);
1300                                 claws_unlink(tmpfile);
1301                                 g_free(tmpfile);
1302                         }
1303                         else {
1304                                 g_warning("gs conversion disabled: gs binary was not found\n");
1305                                 alertpanel_warning("PostScript view disabled: required gs program not found");
1306                                 result = 1;
1307                                         
1308                         }
1309                         if (result != 0) {
1310                                 main_window_cursor_normal(mainwindow_get_mainwindow());
1311                                 return;
1312                         }
1313                 }   
1314                 else {
1315                         viewer->pdf_doc = poppler_document_new_from_file( viewer->fsname, NULL, &error);
1316                 }
1317                 
1318                 viewer->num_pages = poppler_document_get_n_pages(viewer->pdf_doc);
1319
1320                 g_signal_handlers_block_by_func(G_OBJECT(viewer->cur_page), pdf_viewer_spin_change_page_cb,(gpointer *)viewer);
1321                 gtk_spin_button_set_range(GTK_SPIN_BUTTON(viewer->cur_page), 
1322                                                                         1, 
1323                                                                 (gdouble)viewer->num_pages );
1324
1325                 g_signal_handlers_unblock_by_func(G_OBJECT(viewer->cur_page), pdf_viewer_spin_change_page_cb,(gpointer *)viewer);
1326                 gtk_spin_button_spin(GTK_SPIN_BUTTON(viewer->cur_page), GTK_SPIN_HOME, 1);
1327                 tmp = g_strdup_printf(_("%s Document"),pdf_viewer_mimepart_get_type(viewer->to_load) == TYPE_PDF ? "PDF":"Postscript");
1328                 CLAWS_SET_TIP(
1329                                 GTK_WIDGET(viewer->icon_type_ebox),
1330                                 tmp);
1331                 g_free(tmp);
1332
1333                 tmp = g_strdup_printf(_("of %d"), viewer->num_pages);
1334                 gtk_label_set_text(GTK_LABEL(viewer->doc_label), tmp);
1335                 g_free(tmp);
1336
1337                 pdf_viewer_show_controls(viewer, TRUE);
1338                 main_window_cursor_normal(mainwindow_get_mainwindow());
1339         } 
1340         if (viewer->pdf_doc == NULL) {
1341                 strretchomp(error->message);
1342                 stock_pixbuf_gdk(viewer->hbox, 
1343                                 STOCK_PIXMAP_MIME_APPLICATION, 
1344                                 &viewer->icon_pixbuf);
1345
1346                 gtk_image_set_from_pixbuf(GTK_IMAGE(viewer->icon_type), viewer->icon_pixbuf);
1347                 alertpanel_error("%s", error->message);
1348         
1349                 pdf_viewer_show_controls(viewer, FALSE);
1350                 g_error_free(error);
1351                 return;
1352         }
1353
1354         if (page_num == 1) { 
1355                 gtk_widget_set_sensitive(viewer->first_page, FALSE);
1356                 gtk_widget_set_sensitive(viewer->prev_page, FALSE);
1357         }
1358         else {
1359                 gtk_widget_set_sensitive(viewer->first_page, TRUE);
1360                 gtk_widget_set_sensitive(viewer->prev_page, TRUE);
1361         }
1362
1363         if (page_num == viewer->num_pages) { 
1364                 gtk_widget_set_sensitive(viewer->last_page, FALSE);
1365                 gtk_widget_set_sensitive(viewer->next_page, FALSE);
1366         }
1367         else {
1368                 gtk_widget_set_sensitive(viewer->last_page, TRUE);
1369                 gtk_widget_set_sensitive(viewer->next_page, TRUE);
1370         }
1371
1372         /* check for the index if exists */
1373         pdf_viewer_set_index_button_sensitive((PdfViewer *) viewer);
1374         
1375         if (page_num > 0 && page_num <= viewer->num_pages) {
1376
1377                 GTK_EVENTS_FLUSH();
1378                 
1379                 if (viewer->pdf_page) {
1380                         g_object_unref(G_OBJECT(viewer->pdf_page));
1381                 }
1382                         
1383                 viewer->pdf_page = poppler_document_get_page(viewer->pdf_doc, page_num - 1);
1384
1385                 if (viewer->pdf_page == NULL) {
1386                         g_warning("Page not found\n");
1387                         return;
1388                 }   
1389         
1390                 if (viewer->rotate == 90 || viewer->rotate == 270) {
1391                         poppler_page_get_size(viewer->pdf_page, &viewer->height, &viewer->width);
1392                 } 
1393                 else {
1394                         poppler_page_get_size(viewer->pdf_page, &viewer->width, &viewer->height);
1395                 }
1396
1397                 if (viewer->last_rect && viewer->last_page_result &&
1398                     viewer->last_page_result->page_num == page_num) {
1399                         pdf_viewer_render_selection(viewer, viewer->last_rect, viewer->last_page_result);
1400                 }
1401                 else {
1402                         pdf_viewer_render_page(viewer->pdf_page, viewer->pdf_view, viewer->width, 
1403                                                                         viewer->height, viewer->zoom, viewer->rotate);
1404
1405                 }
1406         
1407         /* Get Links Mapping */
1408         if (viewer->link_map) {
1409                 poppler_page_free_link_mapping(viewer->link_map);
1410         }
1411         viewer->link_map = poppler_page_get_link_mapping(viewer->pdf_page);
1412         
1413         }
1414 }
1415
1416
1417 static void pdf_viewer_show_mimepart(MimeViewer *_viewer, const gchar *infile,
1418                                 MimeInfo *partinfo)
1419 {
1420         PdfViewer *viewer = (PdfViewer *) _viewer;
1421         gchar buf[4096];
1422         const gchar *charset = NULL;
1423         MessageView *messageview = ((MimeViewer *)viewer)->mimeview 
1424                                         ?((MimeViewer *)viewer)->mimeview->messageview 
1425                                         : NULL;
1426
1427         viewer->rotate = 0;
1428         viewer->to_load = partinfo;
1429
1430         memset(buf, 0, sizeof(buf));
1431         messageview->updating = TRUE;
1432         debug_print("pdf_viewer_show_mimepart\n");
1433
1434         if (viewer->filename != NULL) {
1435                 claws_unlink(viewer->filename);
1436                 g_free(viewer->filename);
1437                 viewer->filename = NULL;
1438         }
1439         
1440         viewer->mimeinfo = NULL;
1441         
1442         if (partinfo) {
1443                 viewer->target_filename = procmime_get_part_file_name(partinfo);
1444                 viewer->filename = procmime_get_tmp_file_name(partinfo);
1445                 viewer->fsname = g_filename_to_uri(viewer->filename, NULL, NULL);
1446         }
1447         
1448         if (partinfo && !(procmime_get_part(viewer->filename, partinfo) < 0)) {
1449
1450                 if (_viewer && _viewer->mimeview && 
1451                                 _viewer->mimeview->messageview->forced_charset) {
1452                         charset = _viewer->mimeview->messageview->forced_charset;
1453                 }
1454                 else {
1455                         charset = procmime_mimeinfo_get_parameter(partinfo, "charset");
1456                 }
1457                 if (charset == NULL) {
1458                         charset = conv_get_locale_charset_str();
1459                 }
1460
1461                 debug_print("using charset %s\n", charset);
1462                 
1463                 viewer->mimeinfo = partinfo;
1464         }
1465         
1466         pdf_viewer_update((MimeViewer *)viewer, TRUE, 1);
1467         
1468         messageview->updating = FALSE;
1469 }
1470
1471 static void pdf_viewer_clear(MimeViewer *_viewer)
1472 {
1473         PdfViewer *viewer = (PdfViewer *) _viewer;
1474         GtkAdjustment *vadj;
1475                 
1476         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(viewer->doc_index), FALSE);
1477         gtk_widget_hide(viewer->frame_index);
1478
1479         debug_print("pdf_viewer_clear\n");
1480         viewer->to_load = NULL;
1481         
1482         if (viewer->pdf_doc) {
1483                 g_object_unref(G_OBJECT(viewer->pdf_doc));
1484                 viewer->pdf_doc = NULL;
1485         }
1486         
1487         vadj = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(viewer->scrollwin));
1488         vadj->value = 0.0;
1489         g_signal_emit_by_name(G_OBJECT(vadj), "value-changed", 0);
1490         vadj = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(viewer->scrollwin_index));
1491         vadj->value = 0.0;
1492         g_signal_emit_by_name(G_OBJECT(vadj), "value-changed", 0);
1493         gtk_tree_store_clear(GTK_TREE_STORE(viewer->index_model));
1494         gtk_image_set_from_pixbuf(GTK_IMAGE(viewer->pdf_view), NULL);
1495 }
1496
1497 static void pdf_viewer_destroy(MimeViewer *_viewer)
1498 {
1499         PdfViewer *viewer = (PdfViewer *) _viewer;
1500
1501         debug_print("pdf_viewer_destroy\n");
1502         
1503         if (viewer->pdf_index) poppler_index_iter_free(viewer->pdf_index);
1504
1505         poppler_page_free_link_mapping (viewer->link_map);
1506         g_object_unref(GTK_WIDGET(viewer->vbox));
1507         g_object_unref(GTK_WIDGET(viewer->pdf_view));
1508         g_object_unref(GTK_WIDGET(viewer->doc_index_pane));
1509         g_object_unref(GTK_WIDGET(viewer->scrollwin));
1510         g_object_unref(GTK_WIDGET(viewer->scrollwin_index));
1511         claws_unlink(viewer->filename);
1512         g_free(viewer->filename);
1513         g_free(viewer);
1514 }
1515
1516 static gboolean pdf_viewer_scroll_page(MimeViewer *_viewer, gboolean up)
1517 {
1518         PdfViewer *viewer = (PdfViewer *)_viewer;
1519         GtkAdjustment *vadj = gtk_scrolled_window_get_vadjustment(
1520                                 GTK_SCROLLED_WINDOW(viewer->scrollwin));
1521         
1522         gint cur_p = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(viewer->cur_page));
1523         
1524         if (viewer->pdf_view == NULL) return FALSE;
1525
1526         if (!gtkutils_scroll_page(GTK_WIDGET(viewer->pdf_view), vadj, up)) {
1527                 if (!up && cur_p != viewer->num_pages) {
1528                         gtk_spin_button_spin(GTK_SPIN_BUTTON(viewer->cur_page), GTK_SPIN_STEP_FORWARD, 1);
1529                         vadj = gtk_scrolled_window_get_vadjustment(
1530                                         GTK_SCROLLED_WINDOW(viewer->scrollwin));
1531                         vadj->value = 0.0;
1532                         g_signal_emit_by_name(G_OBJECT(vadj), "value-changed", 0);
1533                         return TRUE;
1534                 } 
1535                 else if (up && cur_p != 1) {
1536                         gtk_spin_button_spin(GTK_SPIN_BUTTON(viewer->cur_page), GTK_SPIN_STEP_BACKWARD, 1);
1537                         vadj = gtk_scrolled_window_get_vadjustment(
1538                                         GTK_SCROLLED_WINDOW(viewer->scrollwin));
1539                         vadj->value = vadj->upper - vadj->page_size;
1540                         g_signal_emit_by_name(G_OBJECT(vadj), "value-changed", 0);
1541                         return TRUE;
1542                 } 
1543                 return FALSE;
1544         } 
1545         else return TRUE;
1546 }
1547
1548 static void pdf_viewer_scroll_one_line(MimeViewer *_viewer, gboolean up)
1549 {
1550         PdfViewer *viewer = (PdfViewer *)_viewer;
1551         GtkAdjustment *vadj = gtk_scrolled_window_get_vadjustment(
1552                                 GTK_SCROLLED_WINDOW(viewer->scrollwin));
1553         gint cur_p;
1554         cur_p = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(viewer->cur_page));
1555
1556         if (viewer->pdf_view == NULL) return; 
1557                 debug_print("up: %d\n", up);    
1558                 if (vadj->value <(vadj->upper - vadj->page_size))  {
1559                         gtkutils_scroll_one_line(GTK_WIDGET(viewer->pdf_view), vadj, up);
1560                 }
1561                 else {
1562                         if (cur_p != viewer->num_pages) {
1563                                 pdf_viewer_scroll_page((MimeViewer *)viewer, up);
1564                         }
1565                 }
1566
1567 }
1568
1569 #define BUTTON_H_PADDING 3
1570 #define ADD_BUTTON_TO_TABLE(widget, stock_image) \
1571         widget = gtk_button_new(); \
1572         img = gtk_image_new_from_stock(stock_image, GTK_ICON_SIZE_MENU); \
1573         gtk_button_set_image(GTK_BUTTON(widget), img); \
1574         gtk_table_attach(GTK_TABLE(viewer->widgets_table), GTK_WIDGET(widget), \
1575                                 col, col+1, 0, 1, 0, 0, BUTTON_H_PADDING, 0); \
1576         col++;
1577
1578 #define ADD_SEP_TO_TABLE \
1579         sep = gtk_label_new(""); \
1580         gtk_table_attach(GTK_TABLE(viewer->widgets_table), GTK_WIDGET(sep), \
1581                                         col, col+1, 0, 1, 0, 0, 0, 0); \
1582         gtk_table_set_col_spacing(GTK_TABLE(viewer->widgets_table), col, 3*BUTTON_H_PADDING); \
1583         col++;
1584
1585 #if GTK_CHECK_VERSION(2,10,0) && POPPLER_HAS_CAIRO && !USE_LIBGNOMEPRINT
1586 static PangoContext *pdf_viewer_get_pango_context(gpointer data)
1587 {
1588         return NULL;
1589 }
1590
1591 static gpointer pdf_viewer_get_data_to_print(gpointer data, gint sel_start, gint sel_end)
1592 {
1593         return NULL; /* we don't need it */
1594 }
1595
1596 static void pdf_viewer_cb_begin_print(GtkPrintOperation *op, GtkPrintContext *context,
1597                            gpointer user_data)
1598 {
1599   PrintData *print_data;
1600   PopplerDocument *pdf_doc;
1601   gint n_pages = 0;
1602   print_data = (PrintData*) user_data;
1603   pdf_doc = (PopplerDocument *)printing_get_renderer_data(print_data);
1604
1605   debug_print("Preparing print job...\n");
1606
1607   n_pages = poppler_document_get_n_pages(pdf_doc);
1608   printing_set_n_pages(print_data, n_pages);
1609   gtk_print_operation_set_n_pages(op, n_pages);
1610
1611   debug_print("Starting print job...\n");
1612 }
1613
1614 static void pdf_viewer_cb_draw_page(GtkPrintOperation *op, GtkPrintContext *context,
1615                          int page_nr, gpointer user_data)
1616 {
1617   cairo_t *cr;
1618   PrintData *print_data;
1619   PopplerDocument *pdf_doc;
1620   PopplerPage *pdf_page;
1621   
1622   print_data = (PrintData*) user_data;
1623   pdf_doc = (PopplerDocument *)printing_get_renderer_data(print_data);
1624   pdf_page = poppler_document_get_page(pdf_doc, page_nr);
1625
1626   cr = gtk_print_context_get_cairo_context(context);
1627   cairo_scale(cr, printing_get_zoom(print_data), printing_get_zoom(print_data));
1628   cairo_set_source_rgb(cr, 0., 0., 0.);
1629
1630   poppler_page_render(pdf_page, cr);
1631
1632   g_object_unref(G_OBJECT(pdf_page));
1633
1634   debug_print("Sent page %d to printer\n", page_nr+1);
1635 }
1636
1637 static void pdf_viewer_print(MimeViewer *mviewer)
1638 {
1639         PdfViewer *viewer = (PdfViewer *)mviewer;
1640         PrintRenderer *pdf_renderer = g_new0(PrintRenderer, 1);
1641         MainWindow *mainwin = mainwindow_get_mainwindow();
1642         
1643         pdf_renderer->get_pango_context = pdf_viewer_get_pango_context;
1644         pdf_renderer->get_data_to_print = pdf_viewer_get_data_to_print;
1645         pdf_renderer->cb_begin_print    = pdf_viewer_cb_begin_print;
1646         pdf_renderer->cb_draw_page      = pdf_viewer_cb_draw_page;
1647
1648         printing_print_full(mainwin ? GTK_WINDOW(mainwin->window):NULL, 
1649                         pdf_renderer, viewer->pdf_doc, -1, -1);
1650         
1651         g_free(pdf_renderer);
1652 }
1653 #endif
1654
1655 static MimeViewer *pdf_viewer_create(void)
1656 {
1657         PdfViewer *viewer;
1658         GtkTreeViewColumn *column;
1659         GtkCellRenderer *renderer;
1660         GtkTreeStore *tree_store;
1661         GtkWidget *sep;
1662         GtkWidget *img;
1663         gint col = 0;
1664
1665         viewer = g_new0(PdfViewer, 1);
1666         debug_print("pdf_viewer_create\n");
1667     
1668         viewer->last_x = 0;
1669         viewer->last_y = 0;
1670         viewer->mimeviewer.factory = &pdf_viewer_factory;
1671         viewer->mimeviewer.get_widget = pdf_viewer_get_widget;
1672         viewer->mimeviewer.show_mimepart = pdf_viewer_show_mimepart;
1673         viewer->mimeviewer.clear_viewer = pdf_viewer_clear;
1674         viewer->mimeviewer.destroy_viewer = pdf_viewer_destroy;
1675         viewer->mimeviewer.text_search = pdf_viewer_text_search;
1676         viewer->mimeviewer.scroll_page = pdf_viewer_scroll_page;
1677         viewer->mimeviewer.scroll_one_line = pdf_viewer_scroll_one_line;
1678 #if GTK_CHECK_VERSION(2,10,0) && POPPLER_HAS_CAIRO && !USE_LIBGNOMEPRINT
1679         viewer->mimeviewer.print = pdf_viewer_print;
1680 #endif
1681         viewer->scrollwin = gtk_scrolled_window_new(NULL, NULL);
1682         viewer->scrollwin_index = gtk_scrolled_window_new(NULL, NULL);
1683         viewer->pdf_view_ebox = gtk_event_box_new();
1684         gtk_event_box_set_visible_window(GTK_EVENT_BOX(viewer->pdf_view_ebox), FALSE);
1685                                                         
1686         viewer->mimeinfo  = NULL;
1687
1688         viewer->pdf_view = gtk_image_new();
1689         gtk_widget_set_events(viewer->pdf_view_ebox,
1690                                                 GDK_BUTTON_RELEASE_MASK
1691                                                 | GDK_POINTER_MOTION_MASK
1692                                                 | GDK_BUTTON_PRESS_MASK
1693                                                 | GDK_BUTTON_MOTION_MASK
1694                                             );
1695         gtk_container_add (GTK_CONTAINER(viewer->pdf_view_ebox), viewer->pdf_view);
1696         viewer->icon_type = gtk_image_new();
1697         viewer->icon_type_ebox = gtk_event_box_new();
1698
1699         gtk_container_add(GTK_CONTAINER(viewer->icon_type_ebox), viewer->icon_type);
1700
1701         viewer->doc_label = gtk_label_new("");
1702
1703         viewer->widgets_table = gtk_table_new(1, 1, FALSE);
1704
1705         viewer->doc_index_pane = gtk_hpaned_new();
1706
1707         viewer->frame_index = gtk_frame_new(NULL);
1708         gtk_frame_set_shadow_type(GTK_FRAME(viewer->frame_index), GTK_SHADOW_IN);
1709         gtk_widget_set_size_request(viewer->frame_index, 18, -1);
1710         gtk_frame_set_label(GTK_FRAME(viewer->frame_index), _("Document Index"));
1711
1712 #if !(GTK_CHECK_VERSION(2,12,0))
1713         viewer->button_bar_tips = tips;
1714 #endif
1715
1716         ADD_SEP_TO_TABLE
1717         ADD_BUTTON_TO_TABLE(viewer->first_page, GTK_STOCK_GOTO_FIRST)
1718         ADD_BUTTON_TO_TABLE(viewer->prev_page, GTK_STOCK_GO_BACK)
1719         viewer->cur_page = gtk_spin_button_new_with_range(0.0, 0.0, 1.0);
1720         viewer->zoom_scroll = gtk_spin_button_new_with_range(0.20, 8.0, 0.20);
1721         gtk_spin_button_set_value(GTK_SPIN_BUTTON(viewer->zoom_scroll), 1.0);
1722         viewer->zoom = 1.0;
1723         gtk_spin_button_set_numeric(GTK_SPIN_BUTTON(viewer->cur_page), TRUE);
1724         gtk_spin_button_set_numeric(GTK_SPIN_BUTTON(viewer->zoom_scroll), TRUE);
1725         gtk_table_attach(GTK_TABLE(viewer->widgets_table), GTK_WIDGET(viewer->cur_page),
1726                                         col, col+1, 
1727                                         0, 1, 0, 0, 
1728                                         BUTTON_H_PADDING, 
1729                                         0);
1730         col++;
1731         gtk_table_attach(GTK_TABLE(viewer->widgets_table), GTK_WIDGET(viewer->doc_label),
1732                                         col, col+1, 
1733                                         0, 1, 0, 0, 
1734                                         BUTTON_H_PADDING, 
1735                                         0);
1736         col++;
1737
1738         ADD_BUTTON_TO_TABLE(viewer->next_page, GTK_STOCK_GO_FORWARD)
1739         ADD_BUTTON_TO_TABLE(viewer->last_page, GTK_STOCK_GOTO_LAST)
1740         ADD_SEP_TO_TABLE
1741         ADD_BUTTON_TO_TABLE(viewer->zoom_fit, GTK_STOCK_ZOOM_FIT)
1742         ADD_BUTTON_TO_TABLE(viewer->zoom_in, GTK_STOCK_ZOOM_IN)
1743         gtk_table_attach(GTK_TABLE(viewer->widgets_table), GTK_WIDGET(viewer->zoom_scroll),
1744                                         col, col+1, 
1745                                         0, 1, 0, 0, 
1746                                         BUTTON_H_PADDING, 
1747                                         0);
1748         col++;
1749         ADD_BUTTON_TO_TABLE(viewer->zoom_out, GTK_STOCK_ZOOM_OUT)
1750         ADD_BUTTON_TO_TABLE(viewer->zoom_width, GTK_STOCK_FULLSCREEN)
1751         ADD_SEP_TO_TABLE
1752         ADD_BUTTON_TO_TABLE(viewer->rotate_left, GTK_STOCK_UNDO)
1753         ADD_BUTTON_TO_TABLE(viewer->rotate_right, GTK_STOCK_REDO)
1754         ADD_SEP_TO_TABLE
1755         ADD_BUTTON_TO_TABLE(viewer->doc_info, GTK_STOCK_INFO)
1756
1757         viewer->doc_index = GTK_WIDGET(gtk_toggle_tool_button_new_from_stock(GTK_STOCK_INDEX));
1758         gtk_widget_set_size_request(GTK_WIDGET(viewer->doc_index), 26, 26);
1759         gtk_table_attach(GTK_TABLE(viewer->widgets_table), GTK_WIDGET(viewer->doc_index),
1760                                         col, col+1, 
1761                                         0, 1, 0, 0, 
1762                                         BUTTON_H_PADDING, 
1763                                         0);
1764         col++;
1765         
1766         gtk_scrolled_window_set_policy(
1767                         GTK_SCROLLED_WINDOW(viewer->scrollwin), 
1768                         GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1769
1770         gtk_scrolled_window_set_shadow_type(
1771                         GTK_SCROLLED_WINDOW(viewer->scrollwin),
1772                         GTK_SHADOW_IN);
1773
1774         gtk_scrolled_window_add_with_viewport(
1775                         GTK_SCROLLED_WINDOW(viewer->scrollwin),
1776                         viewer->pdf_view_ebox);
1777
1778         viewer->vbox = gtk_vbox_new(FALSE, 4);
1779         viewer->hbox = gtk_hbox_new(FALSE, 4);
1780
1781     /* treeview */
1782         tree_store = gtk_tree_store_new(N_INDEX_COLUMNS,
1783                                         G_TYPE_STRING,
1784                                         G_TYPE_INT,
1785                                         G_TYPE_DOUBLE);
1786
1787         viewer->index_list = gtk_tree_view_new_with_model(GTK_TREE_MODEL(tree_store));
1788         renderer = gtk_cell_renderer_text_new();
1789         column = gtk_tree_view_column_new_with_attributes(_("Name"),  renderer, "text", 0,  NULL);
1790         gtk_tree_view_append_column(GTK_TREE_VIEW(viewer->index_list), column);         
1791         gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(viewer->index_list), FALSE);
1792         
1793         viewer->index_model = GTK_TREE_MODEL(tree_store);
1794
1795         gtk_tree_selection_set_mode(gtk_tree_view_get_selection(GTK_TREE_VIEW(viewer->index_list)), 
1796                                                                 GTK_SELECTION_SINGLE);
1797
1798         g_signal_connect(G_OBJECT(viewer->index_list), "row_activated",
1799                          G_CALLBACK(pdf_viewer_index_row_activated),
1800                                          viewer);
1801
1802         gtk_scrolled_window_set_policy(
1803                         GTK_SCROLLED_WINDOW(viewer->scrollwin_index), 
1804                         GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1805
1806         gtk_scrolled_window_set_shadow_type(
1807                         GTK_SCROLLED_WINDOW(viewer->scrollwin_index),
1808                         GTK_SHADOW_IN);
1809
1810         gtk_scrolled_window_add_with_viewport(
1811                         GTK_SCROLLED_WINDOW(viewer->scrollwin_index),
1812                         viewer->index_list);
1813
1814         /* end treeview */
1815
1816         stock_pixbuf_gdk(viewer->hbox, 
1817                         STOCK_PIXMAP_MIME_TEXT_PLAIN, 
1818                         &viewer->icon_pixbuf);
1819
1820         gtk_image_set_from_pixbuf(GTK_IMAGE(viewer->icon_type), 
1821                                 viewer->icon_pixbuf);
1822
1823         /* pack widgets*/
1824         gtk_box_pack_start(GTK_BOX(viewer->hbox), viewer->icon_type_ebox, FALSE, FALSE, 0);
1825         gtk_box_pack_start(GTK_BOX(viewer->hbox), viewer->widgets_table, FALSE, FALSE, 0);
1826
1827         gtk_container_add(GTK_CONTAINER(viewer->frame_index), viewer->scrollwin_index);
1828
1829         gtk_paned_pack1(GTK_PANED(viewer->doc_index_pane), viewer->frame_index, FALSE, FALSE);
1830         gtk_paned_pack2(GTK_PANED(viewer->doc_index_pane), viewer->scrollwin, FALSE, FALSE);
1831
1832         gtk_box_pack_start(GTK_BOX(viewer->vbox), viewer->hbox, FALSE, FALSE, 0);
1833         gtk_box_pack_start(GTK_BOX(viewer->vbox), viewer->doc_index_pane, TRUE, TRUE, 0);
1834         /* show widgets */
1835         gtk_widget_show(GTK_WIDGET(viewer->doc_index_pane));
1836         g_object_ref(GTK_WIDGET(viewer->doc_index_pane));
1837         gtk_widget_show(GTK_WIDGET(viewer->scrollwin));
1838         g_object_ref(GTK_WIDGET(viewer->scrollwin));
1839         gtk_widget_show(GTK_WIDGET(viewer->icon_type_ebox));
1840         g_object_ref(GTK_WIDGET(viewer->icon_type_ebox));
1841         gtk_widget_show(GTK_WIDGET(viewer->pdf_view_ebox));
1842         g_object_ref(GTK_WIDGET(viewer->pdf_view_ebox));
1843         gtk_widget_show(GTK_WIDGET(viewer->scrollwin_index));
1844         g_object_ref(GTK_WIDGET(viewer->scrollwin_index));
1845         gtk_widget_show(GTK_WIDGET(viewer->hbox));
1846         g_object_ref(GTK_WIDGET(viewer->hbox)); 
1847         gtk_widget_show(GTK_WIDGET(viewer->vbox));
1848         g_object_ref(GTK_WIDGET(viewer->vbox));
1849
1850         gtk_widget_show(GTK_WIDGET(viewer->widgets_table));
1851         g_object_ref(GTK_WIDGET(viewer->widgets_table));
1852
1853         gtk_widget_show(GTK_WIDGET(viewer->cur_page));
1854         g_object_ref(GTK_WIDGET(viewer->cur_page));
1855
1856         gtk_widget_show(GTK_WIDGET(viewer->first_page));
1857         g_object_ref(GTK_WIDGET(viewer->first_page));
1858
1859         gtk_widget_show(GTK_WIDGET(viewer->last_page));
1860         g_object_ref(GTK_WIDGET(viewer->last_page));
1861
1862         gtk_widget_show(GTK_WIDGET(viewer->prev_page));
1863         g_object_ref(GTK_WIDGET(viewer->prev_page));
1864
1865         gtk_widget_show(GTK_WIDGET(viewer->next_page));
1866         g_object_ref(GTK_WIDGET(viewer->next_page));
1867
1868         gtk_widget_show(GTK_WIDGET(viewer->zoom_in));
1869         g_object_ref(GTK_WIDGET(viewer->zoom_in));
1870         gtk_widget_show(GTK_WIDGET(viewer->zoom_out));
1871         g_object_ref(GTK_WIDGET(viewer->zoom_out));
1872         gtk_widget_show(GTK_WIDGET(viewer->zoom_fit));
1873         g_object_ref(GTK_WIDGET(viewer->zoom_fit));
1874         gtk_widget_show(GTK_WIDGET(viewer->zoom_width));
1875         g_object_ref(GTK_WIDGET(viewer->zoom_width));
1876
1877         gtk_widget_show(GTK_WIDGET(viewer->rotate_right));
1878         g_object_ref(GTK_WIDGET(viewer->rotate_right));
1879         gtk_widget_show(GTK_WIDGET(viewer->rotate_left));
1880         g_object_ref(GTK_WIDGET(viewer->rotate_left));
1881         gtk_widget_show(GTK_WIDGET(viewer->doc_info));
1882         g_object_ref(GTK_WIDGET(viewer->doc_info));
1883         gtk_widget_show(GTK_WIDGET(viewer->doc_index));
1884         g_object_ref(GTK_WIDGET(viewer->doc_index));
1885
1886         gtk_widget_show(GTK_WIDGET(viewer->doc_label));
1887         g_object_ref(GTK_WIDGET(viewer->doc_label));
1888         gtk_widget_show(GTK_WIDGET(viewer->icon_type));
1889         g_object_ref(GTK_WIDGET(viewer->icon_type));    
1890         gtk_widget_show(GTK_WIDGET(viewer->pdf_view));
1891         g_object_ref(GTK_WIDGET(viewer->pdf_view));
1892         gtk_widget_show(GTK_WIDGET(viewer->zoom_scroll));
1893         g_object_ref(GTK_WIDGET(viewer->zoom_scroll));
1894
1895         gtk_widget_show(GTK_WIDGET(viewer->index_list));
1896         g_object_ref(GTK_WIDGET(viewer->index_list));
1897
1898         /* Set Tooltips*/
1899         CLAWS_SET_TIP(viewer->first_page,
1900                                 _("First Page"));
1901
1902         CLAWS_SET_TIP(viewer->prev_page,
1903                                 _("Previous Page"));
1904
1905         CLAWS_SET_TIP(viewer->next_page,
1906                                 _("Next Page"));
1907         
1908         CLAWS_SET_TIP(viewer->last_page,
1909                                 _("Last Page"));
1910
1911         CLAWS_SET_TIP(viewer->zoom_in,
1912                                 _("Zoom In"));
1913         CLAWS_SET_TIP(viewer->zoom_out,
1914                                 _("Zoom Out"));
1915
1916         CLAWS_SET_TIP(viewer->zoom_fit,
1917                                 _("Fit Page"));
1918
1919         CLAWS_SET_TIP(viewer->zoom_width,
1920                                 _("Fit Page Width"));
1921
1922         CLAWS_SET_TIP(viewer->rotate_left,
1923                                 _("Rotate Left"));
1924
1925         CLAWS_SET_TIP(viewer->rotate_right,
1926                                 _("Rotate Right"));
1927
1928         CLAWS_SET_TIP(viewer->doc_info,
1929                                 _("Document Info"));
1930
1931         CLAWS_SET_TIP(viewer->doc_index,
1932                                 _("Document Index"));
1933         CLAWS_SET_TIP(viewer->cur_page,
1934                                 _("Page Number"));
1935         CLAWS_SET_TIP(viewer->zoom_scroll,
1936                                 _("Zoom Factor"));
1937         /* Connect Signals */
1938         g_signal_connect(G_OBJECT(viewer->cur_page), 
1939                                     "value-changed", 
1940                                     G_CALLBACK(pdf_viewer_spin_change_page_cb), 
1941                                    (gpointer) viewer);
1942
1943         g_signal_connect(G_OBJECT(viewer->first_page), 
1944                                     "clicked", 
1945                                     G_CALLBACK(pdf_viewer_button_first_page_cb), 
1946                                    (gpointer) viewer);
1947         g_signal_connect(G_OBJECT(viewer->prev_page), 
1948                                     "clicked", 
1949                                     G_CALLBACK(pdf_viewer_button_prev_page_cb), 
1950                                    (gpointer) viewer);
1951         g_signal_connect(G_OBJECT(viewer->next_page), 
1952                                     "clicked", 
1953                                     G_CALLBACK(pdf_viewer_button_next_page_cb), 
1954                                    (gpointer) viewer);
1955         g_signal_connect(G_OBJECT(viewer->last_page), 
1956                                     "clicked", 
1957                                     G_CALLBACK(pdf_viewer_button_last_page_cb), 
1958                                    (gpointer) viewer);
1959         g_signal_connect(G_OBJECT(viewer->zoom_in), 
1960                                     "clicked", 
1961                                     G_CALLBACK(pdf_viewer_button_zoom_in_cb), 
1962                                    (gpointer) viewer);
1963         g_signal_connect(G_OBJECT(viewer->zoom_out), 
1964                                     "clicked", 
1965                                     G_CALLBACK(pdf_viewer_button_zoom_out_cb), 
1966                                    (gpointer) viewer);
1967         g_signal_connect(G_OBJECT(viewer->zoom_scroll), 
1968                                     "value-changed", 
1969                                     G_CALLBACK(pdf_viewer_spin_zoom_scroll_cb), 
1970                                    (gpointer) viewer);
1971
1972         g_signal_connect(G_OBJECT(viewer->zoom_fit), 
1973                                    "clicked", 
1974                                     G_CALLBACK(pdf_viewer_button_zoom_fit_cb), 
1975                                    (gpointer) viewer);
1976
1977         g_signal_connect(G_OBJECT(viewer->zoom_width), 
1978                                     "clicked", 
1979                                     G_CALLBACK(pdf_viewer_button_zoom_width_cb), 
1980                                    (gpointer) viewer);
1981
1982         g_signal_connect(G_OBJECT(viewer->rotate_right), 
1983                                     "clicked", 
1984                                     G_CALLBACK(pdf_viewer_button_rotate_right_cb), 
1985                                    (gpointer) viewer);
1986         
1987         g_signal_connect(G_OBJECT(viewer->rotate_left), 
1988                                     "clicked", 
1989                                     G_CALLBACK(pdf_viewer_button_rotate_left_cb), 
1990                                    (gpointer) viewer);
1991         
1992         g_signal_connect(G_OBJECT(viewer->doc_info), 
1993                                     "clicked", 
1994                                     G_CALLBACK(pdf_viewer_button_document_info_cb), 
1995                                    (gpointer) viewer);  
1996         
1997         g_signal_connect(G_OBJECT(viewer->doc_index), 
1998                                     "clicked", 
1999                                     G_CALLBACK(pdf_viewer_show_document_index_cb), 
2000                                    (gpointer) viewer);
2001         g_signal_connect(G_OBJECT(viewer->scrollwin), 
2002                                     "scroll-event", 
2003                                     G_CALLBACK(pdf_viewer_scroll_cb), 
2004                                    (gpointer) viewer);
2005         g_signal_connect(G_OBJECT(viewer->pdf_view_ebox), 
2006                                     "button_press_event", 
2007                                     G_CALLBACK(pdf_viewer_button_press_events_cb), 
2008                                    (gpointer) viewer);
2009         g_signal_connect(G_OBJECT(viewer->pdf_view_ebox), 
2010                                     "button_release_event", 
2011                                     G_CALLBACK(pdf_viewer_mouse_scroll_destroy_cb), 
2012                                    (gpointer) viewer);
2013         g_signal_connect(G_OBJECT(viewer->pdf_view_ebox), 
2014                                     "motion_notify_event", 
2015                                     G_CALLBACK(pdf_viewer_move_events_cb), 
2016                                    (gpointer) viewer);
2017
2018         viewer->target_filename = NULL;
2019         viewer->filename = NULL;
2020         viewer->fsname = NULL;
2021
2022         return(MimeViewer *) viewer;
2023 }
2024
2025 #undef ADD_BUTTON_TO_TABLE
2026 #undef ADD_SEP_TO_TABLE
2027 #undef BUTTON_H_PADDING
2028 #undef SEP_H_PADDING
2029                 
2030 static MimeViewerFactory pdf_viewer_factory =
2031 {
2032         content_types,
2033         0,
2034         pdf_viewer_create,
2035 };
2036
2037 gint plugin_init(gchar **error)
2038 {
2039         gchar *gspath = NULL;
2040
2041         msg = g_strdup_printf(_("This plugin enables the viewing of PDF and PostScript "
2042                                 "attachments using the Poppler %s Lib and the gs tool.\n\n"
2043                                 "Any feedback is welcome: iwkse@claws-mail.org"
2044                                 ), poppler_get_version());
2045
2046         if (!check_plugin_version(MAKE_NUMERIC_VERSION(3,8,1,46),
2047                     VERSION_NUMERIC, _("PDF Viewer"), error)) return -1;
2048
2049         if ((gspath = g_find_program_in_path("gs")) == NULL) {
2050                 gchar *pmsg = msg;
2051                 msg = g_strdup_printf(_("Warning: could not find ghostscript binary (gs) required "
2052                                         "for %s plugin to process PostScript attachments, only PDF "
2053                                         "attachments will be displayed. To enable PostScript "
2054                                         "support please install gs program.\n\n%s"
2055                                         ), _("PDF Viewer"), pmsg);
2056                 g_free(pmsg);
2057         }
2058         else {
2059                 g_free(gspath);
2060         }
2061         
2062         mimeview_register_viewer_factory(&pdf_viewer_factory);
2063         return 0;
2064 }
2065
2066 gboolean plugin_done(void)
2067 {
2068         g_free(msg);    
2069         mimeview_unregister_viewer_factory(&pdf_viewer_factory);
2070         return TRUE;
2071 }
2072
2073 const gchar *plugin_name(void)
2074 {
2075         return _("PDF Viewer");
2076 }
2077
2078 const gchar *plugin_desc(void)
2079 {
2080         return msg;
2081 }
2082
2083 const gchar *plugin_type(void)
2084 {
2085         return "GTK2";
2086 }
2087
2088 const gchar *plugin_licence(void)
2089 {
2090         return "GPL3+";
2091 }
2092
2093 const gchar *plugin_version(void)
2094 {
2095         return VERSION;
2096 }
2097
2098 struct PluginFeature *plugin_provides(void)
2099 {
2100         static struct PluginFeature features[] = 
2101                 { {PLUGIN_MIMEVIEWER, "application/pdf"},
2102                   {PLUGIN_MIMEVIEWER, "application/postscript"},
2103                   {PLUGIN_NOTHING, NULL} };
2104         return features;
2105 }
2106