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