73120d448fb365ecff237449cfd993106eeac683
[claws.git] / src / image_viewer.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2007 Hiroyuki Yamamoto and 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
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <glib.h>
25 #include <glib/gi18n.h>
26 #include <gtk/gtk.h>
27
28 #include "procmime.h"
29 #include "utils.h"
30 #include "mimeview.h"
31
32 #include "prefs_common.h"
33
34 typedef struct _ImageViewer ImageViewer;
35
36 MimeViewerFactory image_viewer_factory;
37 void image_viewer_get_resized_size(gint w, gint h, gint aw, gint ah,
38                                           gint * sw, gint * sh);
39 static void image_viewer_clear_viewer(MimeViewer *imageviewer);
40 static void scrolledwin_resize_cb(GtkWidget *scrolledwin, GtkAllocation *alloc,
41                                   ImageViewer *imageviewer);
42 struct _ImageViewer
43 {
44         MimeViewer mimeviewer;
45
46         gchar     *file;
47         MimeInfo  *mimeinfo;
48         gboolean   resize_img;
49
50         GtkWidget *scrolledwin;
51         GtkWidget *image;
52         GtkWidget *notebook;
53         GtkWidget *filename;
54         GtkWidget *filesize;
55         GtkWidget *error_lbl;
56         GtkWidget *error_msg;
57         GtkWidget *content_type;
58         GtkWidget *load_button;
59 };
60
61 static GtkWidget *image_viewer_get_widget(MimeViewer *_mimeviewer)
62 {
63         ImageViewer *imageviewer = (ImageViewer *) _mimeviewer;
64
65         debug_print("image_viewer_get_widget\n");
66
67         return imageviewer->notebook;
68 }
69
70 static void image_viewer_load_file(ImageViewer *imageviewer, const gchar *imgfile)
71 {
72         GdkPixbufAnimation *animation = NULL;
73         GdkPixbuf *pixbuf = NULL;
74         gint avail_width;
75         gint avail_height;
76         GError *error = NULL;
77
78         debug_print("image_viewer_show_mimepart\n");
79
80         if (!imageviewer->resize_img) {
81                 animation = gdk_pixbuf_animation_new_from_file(imgfile, &error);
82         } else {
83                 gint w, h;
84                 gdk_pixbuf_get_file_info(imgfile, &w, &h);
85                 avail_width = imageviewer->notebook->parent->allocation.width;
86                 avail_height = imageviewer->notebook->parent->allocation.height;
87                 if (avail_width > 8) avail_width -= 8;
88                 if (avail_height > 8) avail_height -= 8;
89                 if (avail_width - 100 > 0 &&
90                     (w > avail_width || h > avail_height)) {
91                         pixbuf = gdk_pixbuf_new_from_file_at_scale(imgfile, avail_width,
92                                 avail_height, TRUE, &error);
93                 } else {
94                         animation = gdk_pixbuf_animation_new_from_file(imgfile, &error);
95                 }
96         }
97
98         if (error && !pixbuf && !animation) {
99                 gtk_label_set_text(GTK_LABEL(imageviewer->error_lbl), _("Error:"));
100                 gtk_label_set_text(GTK_LABEL(imageviewer->error_msg), error->message);
101                 gtk_notebook_set_current_page(GTK_NOTEBOOK(imageviewer->notebook), 0);
102                 gtk_widget_hide(imageviewer->load_button);
103                 g_error_free(error);
104         }
105         if (!pixbuf && !animation) {
106                 g_warning("Can't load the image.");     
107                 return;
108         }
109
110         if (animation)
111                 gtk_image_set_from_animation(GTK_IMAGE(imageviewer->image), animation);
112         else
113                 gtk_image_set_from_pixbuf(GTK_IMAGE(imageviewer->image), pixbuf);
114
115         g_signal_handlers_block_by_func(G_OBJECT(imageviewer->scrolledwin), 
116                          G_CALLBACK(scrolledwin_resize_cb), imageviewer);
117
118
119         gtk_widget_show(imageviewer->image);
120         GTK_EVENTS_FLUSH();
121         g_signal_handlers_unblock_by_func(G_OBJECT(imageviewer->scrolledwin), 
122                          G_CALLBACK(scrolledwin_resize_cb), imageviewer);
123
124         if (pixbuf)
125                 g_object_unref(pixbuf);
126         if (animation)
127                 g_object_unref(animation);
128 }
129
130 static void image_viewer_set_notebook_page(MimeViewer *_mimeviewer)
131 {
132         ImageViewer *imageviewer = (ImageViewer *) _mimeviewer;
133
134         if (!prefs_common.display_img)
135                 gtk_notebook_set_current_page(GTK_NOTEBOOK(imageviewer->notebook), 0);
136         else
137                 gtk_notebook_set_current_page(GTK_NOTEBOOK(imageviewer->notebook), 1);
138 }
139
140 static void image_viewer_load_image(ImageViewer *imageviewer)
141 {
142         gchar *imgfile;
143
144         if (imageviewer->mimeinfo == NULL)
145                 return;
146
147         imgfile = procmime_get_tmp_file_name(imageviewer->mimeinfo);
148         if (procmime_get_part(imgfile, imageviewer->mimeinfo) < 0) {
149                 g_warning("Can't get mimepart file");   
150                 g_free(imgfile);
151                 return;
152         }
153         image_viewer_load_file(imageviewer, imgfile);
154         claws_unlink(imgfile);
155         g_free(imgfile);
156 }
157
158 static void image_viewer_show_mimepart(MimeViewer *_mimeviewer, const gchar *file, MimeInfo *mimeinfo)
159 {
160         ImageViewer *imageviewer = (ImageViewer *) _mimeviewer;
161
162         image_viewer_clear_viewer(_mimeviewer);
163         g_free(imageviewer->file);
164         imageviewer->file = g_strdup(file);
165         imageviewer->mimeinfo = mimeinfo;
166
167         gtk_label_set_text(GTK_LABEL(imageviewer->filename),
168                            procmime_mimeinfo_get_parameter(mimeinfo, "name"));
169         gtk_label_set_text(GTK_LABEL(imageviewer->filesize), to_human_readable((goffset)mimeinfo->length));
170         gtk_label_set_text(GTK_LABEL(imageviewer->content_type), mimeinfo->subtype);
171         gtk_label_set_text(GTK_LABEL(imageviewer->error_lbl), "");
172         gtk_label_set_text(GTK_LABEL(imageviewer->error_msg), "");
173
174         if (prefs_common.display_img)
175                 image_viewer_load_image(imageviewer);
176 }
177
178 static void image_viewer_clear_viewer(MimeViewer *_mimeviewer)
179 {
180         ImageViewer *imageviewer = (ImageViewer *) _mimeviewer;
181         GtkAdjustment *hadj, *vadj;
182
183         debug_print("image_viewer_clear_viewer\n");
184
185         image_viewer_set_notebook_page(_mimeviewer);
186
187         if (imageviewer->scrolledwin) {
188                 hadj = gtk_scrolled_window_get_hadjustment
189                         (GTK_SCROLLED_WINDOW(imageviewer->scrolledwin));
190                 if (hadj) {
191                         gtk_adjustment_set_value(hadj, 0.0);
192                         gtk_adjustment_changed(hadj);
193                 }
194                 vadj = gtk_scrolled_window_get_vadjustment
195                         (GTK_SCROLLED_WINDOW(imageviewer->scrolledwin));
196                 if (vadj) {
197                         gtk_adjustment_set_value(vadj, 0.0);
198                         gtk_adjustment_changed(vadj);
199                 }
200         }
201         g_free(imageviewer->file);
202         imageviewer->file = NULL;
203         imageviewer->mimeinfo = NULL;
204         imageviewer->resize_img = prefs_common.resize_img;
205 }
206
207 static void image_viewer_destroy_viewer(MimeViewer *_mimeviewer)
208 {
209         ImageViewer *imageviewer = (ImageViewer *) _mimeviewer;
210
211         debug_print("image_viewer_destroy_viewer\n");
212
213         image_viewer_clear_viewer(_mimeviewer);
214         g_object_unref(imageviewer->notebook);
215         g_free(imageviewer);
216 }
217
218 void image_viewer_get_resized_size(gint w, gint h, gint aw, gint ah,
219                              gint *sw, gint *sh)
220 {
221         gfloat wratio = 1.0;
222         gfloat hratio = 1.0;
223         gfloat ratio  = 1.0;
224
225         if (w > aw)
226                 wratio = (gfloat)aw / (gfloat)w;
227         if (h > ah)
228                 hratio = (gfloat)ah / (gfloat)h;
229
230         ratio = (wratio > hratio) ? hratio : wratio;
231
232         *sw = (gint)(w * ratio);
233         *sh = (gint)(h * ratio);
234
235         /* be paranoid */
236         if (*sw <= 0 || *sh <= 0) {
237                 *sw = w;
238                 *sh = h;
239         }
240 }
241
242 static void load_cb(GtkButton *button, ImageViewer *imageviewer)
243 {
244         gtk_notebook_set_current_page(GTK_NOTEBOOK(imageviewer->notebook), 1);
245         image_viewer_load_image(imageviewer);
246 }
247
248 static gboolean scrolledwin_button_cb(GtkWidget *scrolledwin, GdkEventButton *event,
249                                       ImageViewer *imageviewer)
250 {
251         if (event->button == 1 && imageviewer->image) {
252                 imageviewer->resize_img = !imageviewer->resize_img;
253                 image_viewer_load_image(imageviewer);
254                 return TRUE;
255         }
256         return FALSE;
257 }
258
259 static void scrolledwin_resize_cb(GtkWidget *scrolledwin, GtkAllocation *alloc,
260                                   ImageViewer *imageviewer)
261 {
262         if (imageviewer->resize_img)
263                 image_viewer_load_image(imageviewer);
264 }
265
266 static MimeViewer *image_viewer_create(void)
267 {
268         ImageViewer *imageviewer;
269         /*
270          *  glade generated code (do not touch)
271          */
272         GtkWidget *notebook;
273         GtkWidget *table1;
274         GtkWidget *label3;
275         GtkWidget *label4;
276         GtkWidget *filename;
277         GtkWidget *filesize;
278         GtkWidget *load_button;
279         GtkWidget *label5;
280         GtkWidget *content_type;
281         GtkWidget *scrolledwin;
282         GtkWidget *error_lbl;
283         GtkWidget *error_msg;
284
285         notebook = gtk_notebook_new();
286         gtk_widget_show(notebook);
287         GTK_WIDGET_UNSET_FLAGS(notebook, GTK_CAN_FOCUS);
288         gtk_notebook_set_show_tabs(GTK_NOTEBOOK(notebook), FALSE);
289         gtk_notebook_set_show_border(GTK_NOTEBOOK(notebook), FALSE);
290
291         table1 = gtk_table_new(5, 3, FALSE);
292         gtk_widget_show(table1);
293         gtk_container_add(GTK_CONTAINER(notebook), table1);
294         gtk_container_set_border_width(GTK_CONTAINER(table1), 8);
295         gtk_table_set_row_spacings(GTK_TABLE(table1), 4);
296         gtk_table_set_col_spacings(GTK_TABLE(table1), 4);
297
298         label3 = gtk_label_new(_("Filename:"));
299         gtk_widget_show(label3);
300         gtk_table_attach(GTK_TABLE(table1), label3, 0, 1, 0, 1,
301                          (GtkAttachOptions) (GTK_FILL),
302                          (GtkAttachOptions) (0), 0, 0);
303         gtk_misc_set_alignment(GTK_MISC(label3), 0, 0.5);
304
305         label4 = gtk_label_new(_("Filesize:"));
306         gtk_widget_show(label4);
307         gtk_table_attach(GTK_TABLE(table1), label4, 0, 1, 1, 2,
308                          (GtkAttachOptions) (GTK_FILL),
309                          (GtkAttachOptions) (0), 0, 0);
310         gtk_misc_set_alignment(GTK_MISC(label4), 0, 0.5);
311
312         filename = gtk_label_new("");
313         gtk_widget_show(filename);
314         gtk_table_attach(GTK_TABLE(table1), filename, 1, 3, 0, 1,
315                          (GtkAttachOptions) (GTK_FILL),
316                          (GtkAttachOptions) (0), 0, 0);
317         gtk_misc_set_alignment(GTK_MISC(filename), 0, 0.5);
318
319         filesize = gtk_label_new("");
320         gtk_widget_show(filesize);
321         gtk_table_attach(GTK_TABLE(table1), filesize, 1, 3, 1, 2,
322                          (GtkAttachOptions) (GTK_FILL),
323                          (GtkAttachOptions) (0), 0, 0);
324         gtk_misc_set_alignment(GTK_MISC(filesize), 0, 0.5);
325
326         label5 = gtk_label_new(_("Content-Type:"));
327         gtk_widget_show(label5);
328         gtk_table_attach(GTK_TABLE(table1), label5, 0, 1, 2, 3,
329                          (GtkAttachOptions) (GTK_FILL),
330                          (GtkAttachOptions) (0), 0, 0);
331         gtk_misc_set_alignment(GTK_MISC(label5), 0, 0.5);
332
333         content_type = gtk_label_new("");
334         gtk_widget_show(content_type);
335         gtk_table_attach(GTK_TABLE(table1), content_type, 1, 3, 2, 3,
336                          (GtkAttachOptions) (GTK_FILL),
337                          (GtkAttachOptions) (0), 0, 0);
338         gtk_misc_set_alignment(GTK_MISC(content_type), 0, 0.5);
339
340         error_lbl = gtk_label_new("");
341         gtk_widget_show(error_lbl);
342         gtk_table_attach(GTK_TABLE(table1), error_lbl, 0, 1, 3, 4,
343                          (GtkAttachOptions) (GTK_FILL),
344                          (GtkAttachOptions) (0), 0, 0);
345         gtk_misc_set_alignment(GTK_MISC(error_lbl), 0, 0.5);
346
347         error_msg = gtk_label_new("");
348         gtk_widget_show(error_msg);
349         gtk_table_attach(GTK_TABLE(table1), error_msg, 1, 3, 3, 4,
350                          (GtkAttachOptions) (GTK_FILL),
351                          (GtkAttachOptions) (0), 0, 0);
352         gtk_misc_set_alignment(GTK_MISC(error_msg), 0, 0.5);
353
354         load_button = gtk_button_new_with_label(_("Load Image"));
355         gtk_widget_show(load_button);
356         gtk_table_attach(GTK_TABLE(table1), load_button, 0, 1, 4, 5,
357                          (GtkAttachOptions) (GTK_FILL),
358                          (GtkAttachOptions) (0), 0, 0);
359
360         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
361         gtk_widget_show(scrolledwin);
362         gtk_container_add(GTK_CONTAINER(notebook), scrolledwin);
363         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
364                                        GTK_POLICY_AUTOMATIC,
365                                        GTK_POLICY_AUTOMATIC);
366         /*
367          *  end of glade code
368          */
369
370         debug_print("Creating image view...\n");
371         imageviewer = g_new0(ImageViewer, 1);
372         imageviewer->mimeviewer.factory = &image_viewer_factory;
373
374         imageviewer->mimeviewer.get_widget = image_viewer_get_widget;
375         imageviewer->mimeviewer.show_mimepart = image_viewer_show_mimepart;
376         imageviewer->mimeviewer.clear_viewer = image_viewer_clear_viewer;
377         imageviewer->mimeviewer.destroy_viewer = image_viewer_destroy_viewer;
378         imageviewer->mimeviewer.get_selection = NULL;
379
380         imageviewer->resize_img   = prefs_common.resize_img;
381
382         imageviewer->scrolledwin  = scrolledwin;
383         imageviewer->image = gtk_image_new();
384         gtk_scrolled_window_add_with_viewport
385                 (GTK_SCROLLED_WINDOW(imageviewer->scrolledwin),
386                  imageviewer->image);
387         imageviewer->notebook     = notebook;
388         imageviewer->filename     = filename;
389         imageviewer->filesize     = filesize;
390         imageviewer->content_type = content_type;
391         imageviewer->error_msg    = error_msg;
392         imageviewer->error_lbl    = error_lbl;
393         imageviewer->load_button = load_button;
394
395         g_object_ref(notebook);
396
397         g_signal_connect(G_OBJECT(load_button), "clicked",
398                          G_CALLBACK(load_cb), imageviewer);
399         g_signal_connect(G_OBJECT(scrolledwin), "button-press-event",
400                          G_CALLBACK(scrolledwin_button_cb), imageviewer);
401         g_signal_connect(G_OBJECT(scrolledwin), "size-allocate",
402                          G_CALLBACK(scrolledwin_resize_cb), imageviewer);
403
404         image_viewer_set_notebook_page((MimeViewer *)imageviewer);
405
406         return (MimeViewer *) imageviewer;
407 }
408
409 static gchar *content_types[] =
410         {"image/*", NULL};
411
412 MimeViewerFactory image_viewer_factory =
413 {
414         content_types,
415         0,
416         
417         image_viewer_create,
418 };
419
420 void image_viewer_init(void)
421 {
422         mimeview_register_viewer_factory(&image_viewer_factory);
423 }
424
425 void image_viewer_done(void)
426 {
427         mimeview_unregister_viewer_factory(&image_viewer_factory);
428 }