Do not put enchant's CFLAGS into main CFLAGS make variable.
[claws.git] / src / headerview.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2016 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 #ifdef HAVE_CONFIG_H
20 #  include "config.h"
21 #include "claws-features.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <gtk/gtk.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <time.h>
32
33 #include <gdk-pixbuf/gdk-pixbuf.h>
34
35 #include "headerview.h"
36 #include "prefs_common.h"
37 #include "codeconv.h"
38 #include "gtkutils.h"
39 #include "utils.h"
40 #include "headers.h"
41 #include "addrindex.h"
42 #include "hooks.h"
43 #include "avatars.h"
44
45 static gint headerview_show_avatar      (HeaderView     *headerview,
46                                          MsgInfo        *msginfo);
47 static gint headerview_show_contact_pic (HeaderView     *headerview,
48                                          MsgInfo        *msginfo);
49 static void headerview_save_contact_pic (HeaderView     *headerview,
50                                          MsgInfo        *msginfo);
51
52 HeaderView *headerview_create(void)
53 {
54         HeaderView *headerview;
55         GtkWidget *hbox;
56         GtkWidget *vbox;
57         GtkWidget *hbox1;
58         GtkWidget *hbox2;
59         GtkWidget *hbox3;
60         GtkWidget *from_header_label;
61         GtkWidget *from_body_label;
62         GtkWidget *to_header_label;
63         GtkWidget *to_body_label;
64         GtkWidget *ng_header_label;
65         GtkWidget *ng_body_label;
66         GtkWidget *subject_header_label;
67         GtkWidget *subject_body_label;
68         GtkWidget *tags_header_label;
69         GtkWidget *tags_body_label;
70
71         debug_print("Creating header view...\n");
72         headerview = g_new0(HeaderView, 1);
73
74         hbox = gtk_hbox_new(FALSE, 0);
75         gtk_container_set_border_width(GTK_CONTAINER(hbox), 2);
76         vbox = gtk_vbox_new(FALSE, 2);
77         gtk_box_pack_start(GTK_BOX(hbox), vbox, TRUE, TRUE, 0);
78
79         hbox1 = gtk_hbox_new(FALSE, 4);
80         gtk_box_pack_start(GTK_BOX(vbox), hbox1, FALSE, FALSE, 0);
81         hbox2 = gtk_hbox_new(FALSE, 4);
82         gtk_box_pack_start(GTK_BOX(vbox), hbox2, FALSE, FALSE, 0);
83         hbox3 = gtk_hbox_new(FALSE, 4);
84         gtk_box_pack_start(GTK_BOX(vbox), hbox3, FALSE, FALSE, 0);
85
86         from_header_label    = gtk_label_new(prefs_common_translated_header_name("From:"));
87         from_body_label      = gtk_label_new("");
88         to_header_label      = gtk_label_new(prefs_common_translated_header_name("To:"));
89         to_body_label        = gtk_label_new("");
90         ng_header_label      = gtk_label_new(prefs_common_translated_header_name("Newsgroups:"));
91         ng_body_label        = gtk_label_new("");
92         subject_header_label = gtk_label_new(prefs_common_translated_header_name("Subject:"));
93         subject_body_label   = gtk_label_new("");
94         tags_header_label = gtk_label_new(_("Tags:"));
95         tags_body_label   = gtk_label_new("");
96
97         gtk_label_set_selectable(GTK_LABEL(from_body_label), TRUE);
98         gtk_label_set_selectable(GTK_LABEL(to_body_label), TRUE);
99         gtk_label_set_selectable(GTK_LABEL(ng_body_label), TRUE);
100         gtk_label_set_selectable(GTK_LABEL(subject_body_label), TRUE);
101         gtk_label_set_selectable(GTK_LABEL(tags_body_label), TRUE);
102
103         gtk_widget_set_can_focus(from_body_label, FALSE);
104         gtk_widget_set_can_focus(to_body_label, FALSE);
105         gtk_widget_set_can_focus(ng_body_label, FALSE);
106         gtk_widget_set_can_focus(subject_body_label, FALSE);
107         gtk_widget_set_can_focus(tags_body_label, FALSE);
108
109         gtk_box_pack_start(GTK_BOX(hbox1), from_header_label, FALSE, FALSE, 0);
110         gtk_box_pack_start(GTK_BOX(hbox1), from_body_label, FALSE, FALSE, 0);
111         gtk_box_pack_start(GTK_BOX(hbox1), to_header_label, FALSE, FALSE, 0);
112         gtk_box_pack_start(GTK_BOX(hbox1), to_body_label, TRUE, TRUE, 0);
113         gtk_box_pack_start(GTK_BOX(hbox1), ng_header_label, FALSE, FALSE, 0);
114         gtk_box_pack_start(GTK_BOX(hbox1), ng_body_label, TRUE, TRUE, 0);
115         gtk_box_pack_start(GTK_BOX(hbox2), subject_header_label, FALSE, FALSE, 0);
116         gtk_box_pack_start(GTK_BOX(hbox2), subject_body_label, TRUE, TRUE, 0);
117         gtk_box_pack_start(GTK_BOX(hbox3), tags_header_label, FALSE, FALSE, 0);
118         gtk_box_pack_start(GTK_BOX(hbox3), tags_body_label, TRUE, TRUE, 0);
119
120         gtk_misc_set_alignment(GTK_MISC(to_body_label), 0, 0.5);
121         gtk_misc_set_alignment(GTK_MISC(ng_body_label), 0, 0.5);
122         gtk_misc_set_alignment(GTK_MISC(subject_body_label), 0, 0.5);
123         gtk_misc_set_alignment(GTK_MISC(tags_body_label), 0, 0.5);
124         gtk_label_set_ellipsize(GTK_LABEL(to_body_label), PANGO_ELLIPSIZE_END);
125         gtk_label_set_ellipsize(GTK_LABEL(ng_body_label), PANGO_ELLIPSIZE_END);
126         gtk_label_set_ellipsize(GTK_LABEL(subject_body_label), PANGO_ELLIPSIZE_END);
127         gtk_label_set_ellipsize(GTK_LABEL(tags_body_label), PANGO_ELLIPSIZE_END);
128
129         headerview->hbox = hbox;
130         headerview->from_header_label    = from_header_label;
131         headerview->from_body_label      = from_body_label;
132         headerview->to_header_label      = to_header_label;
133         headerview->to_body_label        = to_body_label;
134         headerview->ng_header_label      = ng_header_label;
135         headerview->ng_body_label        = ng_body_label;
136         headerview->subject_header_label = subject_header_label;
137         headerview->subject_body_label   = subject_body_label;
138         headerview->tags_header_label = tags_header_label;
139         headerview->tags_body_label   = tags_body_label;
140         headerview->image = NULL;
141
142         gtk_widget_show_all(hbox);
143
144         return headerview;
145 }
146
147 void headerview_set_font(HeaderView *headerview)
148 {
149         PangoFontDescription *boldfont = NULL;
150         PangoFontDescription *normalfont = NULL;
151         
152         normalfont = pango_font_description_from_string(NORMAL_FONT);
153         if (normalfont) {
154                 gtk_widget_modify_font(headerview->from_body_label, normalfont);
155                 gtk_widget_modify_font(headerview->to_body_label, normalfont);
156                 gtk_widget_modify_font(headerview->ng_body_label, normalfont);
157                 gtk_widget_modify_font(headerview->subject_body_label, normalfont);
158                 gtk_widget_modify_font(headerview->tags_body_label, normalfont);
159                 pango_font_description_free(normalfont);
160         }
161
162         if (prefs_common.derive_from_normal_font || !BOLD_FONT) {
163                 boldfont = pango_font_description_from_string(NORMAL_FONT);
164                 pango_font_description_set_weight(boldfont, PANGO_WEIGHT_BOLD);
165         } else {
166                 boldfont = pango_font_description_from_string(BOLD_FONT);
167         }
168         if (boldfont) {
169                 gtk_widget_modify_font(headerview->from_header_label, boldfont);
170                 gtk_widget_modify_font(headerview->to_header_label, boldfont);
171                 gtk_widget_modify_font(headerview->ng_header_label, boldfont);
172                 gtk_widget_modify_font(headerview->subject_header_label, boldfont);
173                 gtk_widget_modify_font(headerview->tags_header_label, boldfont);
174                 pango_font_description_free(boldfont);
175         }
176 }
177
178 void headerview_init(HeaderView *headerview)
179 {
180         headerview_set_font(headerview);
181         headerview_clear(headerview);
182         headerview_set_visibility(headerview, prefs_common.display_header_pane);
183 }
184
185 void headerview_show(HeaderView *headerview, MsgInfo *msginfo)
186 {
187         gchar *tags = procmsg_msginfo_get_tags_str(msginfo);
188
189         headerview_clear(headerview);
190
191         gtk_label_set_text(GTK_LABEL(headerview->from_body_label),
192                            msginfo->from ? msginfo->from : _("(No From)"));
193         if (msginfo->to) {
194                 gtk_label_set_text(GTK_LABEL(headerview->to_body_label),
195                                    msginfo->to);
196                 gtk_widget_show(headerview->to_header_label);
197                 gtk_widget_show(headerview->to_body_label);
198         }
199         if (msginfo->newsgroups) {
200                 gtk_label_set_text(GTK_LABEL(headerview->ng_body_label),
201                                    msginfo->newsgroups);
202                 gtk_widget_show(headerview->ng_header_label);
203                 gtk_widget_show(headerview->ng_body_label);
204         }
205         gtk_label_set_text(GTK_LABEL(headerview->subject_body_label),
206                            msginfo->subject ? msginfo->subject :
207                            _("(No Subject)"));
208         if (tags) {
209                 gtk_label_set_text(GTK_LABEL(headerview->tags_body_label),
210                                    tags);
211                 gtk_widget_show(headerview->tags_header_label);
212                 gtk_widget_show(headerview->tags_body_label);
213                 g_free(tags);
214         }
215         if (!headerview_show_avatar(headerview, msginfo))
216                 return;
217
218         if (!headerview_show_contact_pic(headerview, msginfo))
219                 return;
220
221 }
222
223 static gint headerview_show_avatar (HeaderView *headerview, MsgInfo *msginfo)
224 {
225         AvatarRender *avatarr = avatars_avatarrender_new(msginfo);
226         GtkWidget *hbox = headerview->hbox;
227
228         hooks_invoke(AVATAR_IMAGE_RENDER_HOOKLIST, avatarr);
229
230         if (!avatarr->image) {
231                 if (headerview->image
232                                 && gtk_widget_get_visible(headerview->image)) {
233                         gtk_widget_hide(headerview->image);
234                         gtk_widget_queue_resize(hbox);
235                 }
236                 avatars_avatarrender_free(avatarr);
237                 return -1;
238         }
239         if (!gtk_widget_get_visible(hbox)) {
240                 avatars_avatarrender_free(avatarr);
241                 return -1;
242         }
243
244         if (headerview->image) {
245                 gtk_widget_destroy(headerview->image);
246                 headerview->image = NULL;
247         }
248
249         gtk_box_pack_start(GTK_BOX(hbox), avatarr->image, FALSE, FALSE, 0);
250         gtk_widget_show(avatarr->image);
251
252         headerview->image = avatarr->image;
253         avatarr->image = NULL; /* avoid destroying */
254         avatars_avatarrender_free(avatarr);
255
256         headerview_save_contact_pic(headerview, msginfo);
257         return 0;
258 }
259
260 static void headerview_save_contact_pic (HeaderView *headerview, MsgInfo *msginfo)
261 {
262 #ifndef USE_ALT_ADDRBOOK
263         gchar *filename = NULL;
264         GError *error = NULL;
265         GdkPixbuf *picture = NULL;
266
267         if (!gtk_widget_get_visible(headerview->hbox)) return;
268
269         if (headerview->image) {
270                 picture = gtk_image_get_pixbuf(GTK_IMAGE(headerview->image));
271         }
272         
273         filename = addrindex_get_picture_file(msginfo->from);
274         if (!filename)
275                 return;
276         if (!is_file_exist(filename)) {
277                 gdk_pixbuf_save(picture, filename, "png", &error, NULL);
278                 if (error) {
279                         g_warning("Failed to save image: %s",
280                                         error->message);
281                         g_error_free(error);
282                 }
283         }
284         g_free(filename);
285 #else
286         /* new address book */
287 #endif
288 }       
289
290 static gint headerview_show_contact_pic (HeaderView *headerview, MsgInfo *msginfo)
291 {
292 #ifndef USE_ALT_ADDRBOOK
293         GtkWidget *hbox = headerview->hbox;
294         GtkWidget *image;
295         gchar *filename = NULL;
296         GError *error = NULL;
297         GdkPixbuf *picture = NULL;
298         gint w, h;
299
300         if (!gtk_widget_get_visible(headerview->hbox)) return -1;
301
302         if (headerview->image) {
303                 gtk_widget_destroy(headerview->image);
304                 headerview->image = NULL;
305         }
306         
307         filename = addrindex_get_picture_file(msginfo->from);
308         
309         if (!filename)
310                 return -1;
311         if (!is_file_exist(filename)) {
312                 g_free(filename);
313                 return -1;
314         }
315         gdk_pixbuf_get_file_info(filename, &w, &h);
316         
317         if (w > 48 || h > 48)
318                 picture = gdk_pixbuf_new_from_file_at_scale(filename, 
319                                                 48, 48, TRUE, &error);
320         else
321                 picture = gdk_pixbuf_new_from_file(filename, &error);
322
323         g_free(filename);
324         if (error) {
325                 debug_print("Failed to import image: %s\n",
326                                 error->message);
327                 g_error_free(error);
328                 return -1;
329         }
330         if (picture)
331                 image = gtk_image_new_from_pixbuf(picture);
332         else 
333                 return -1;
334
335         g_object_unref(picture);
336         if (image) {
337                 gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 0);
338                 gtk_widget_show(image);
339         }
340
341         headerview->image = image;
342         if (image == NULL)
343                 return -1;
344         else 
345                 return 0;
346 #else
347         /* new address book */
348         return -1;
349 #endif
350 }
351
352 void headerview_clear(HeaderView *headerview)
353 {
354         if (headerview == NULL)
355                 return;
356
357         gtk_label_set_text(GTK_LABEL(headerview->from_body_label), "");
358         gtk_label_set_text(GTK_LABEL(headerview->to_body_label), "");
359         gtk_label_set_text(GTK_LABEL(headerview->ng_body_label), "");
360         gtk_label_set_text(GTK_LABEL(headerview->subject_body_label), "");
361         gtk_label_set_text(GTK_LABEL(headerview->tags_body_label), "");
362         gtk_widget_hide(headerview->to_header_label);
363         gtk_widget_hide(headerview->to_body_label);
364         gtk_widget_hide(headerview->ng_header_label);
365         gtk_widget_hide(headerview->ng_body_label);
366         gtk_widget_hide(headerview->tags_header_label);
367         gtk_widget_hide(headerview->tags_body_label);
368
369         if (headerview->image && gtk_widget_get_visible(headerview->image)) {
370                 gtk_widget_hide(headerview->image);
371                 gtk_widget_queue_resize(headerview->hbox);
372         }
373 }
374
375 void headerview_set_visibility(HeaderView *headerview, gboolean visibility)
376 {
377         if (visibility)
378                 gtk_widget_show(headerview->hbox);
379         else
380                 gtk_widget_hide(headerview->hbox);
381 }
382
383 void headerview_destroy(HeaderView *headerview)
384 {
385         g_free(headerview);
386 }