Fix some copyright headers
[claws.git] / src / gtk / gtkutils.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2018 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 <glib.h>
25 #include <glib/gi18n.h>
26 #include <gdk/gdkkeysyms.h>
27 #include <gdk/gdk.h>
28 #include <gtk/gtk.h>
29 #include "gtk/gtksctree.h"
30 #include <stdlib.h>
31 #include <stdarg.h>
32 #include <sys/stat.h>
33
34 #include "combobox.h"
35
36 #if HAVE_LIBCOMPFACE
37 #  include <compface.h>
38 #endif
39
40 #if HAVE_LIBCOMPFACE
41 #define XPM_XFACE_HEIGHT        (HEIGHT + 3)  /* 3 = 1 header + 2 colors */
42 #endif
43
44 #if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
45 #  include <wchar.h>
46 #  include <wctype.h>
47 #endif
48
49 #include "defs.h"
50 #include "gtkutils.h"
51 #include "utils.h"
52 #include "gtksctree.h"
53 #include "codeconv.h"
54 #include "stock_pixmap.h"
55 #include "menu.h"
56 #include "prefs_account.h"
57 #include "prefs_common.h"
58 #include "manage_window.h"
59 #include "manual.h"
60 #include "combobox.h"
61
62 gboolean gtkut_get_font_size(GtkWidget *widget,
63                              gint *width, gint *height)
64 {
65         PangoLayout *layout;
66         const gchar *str = "Abcdef";
67
68         cm_return_val_if_fail(GTK_IS_WIDGET(widget), FALSE);
69
70         layout = gtk_widget_create_pango_layout(widget, str);
71         cm_return_val_if_fail(layout, FALSE);
72         pango_layout_get_pixel_size(layout, width, height);
73         if (width)
74                 *width = *width / g_utf8_strlen(str, -1);
75         g_object_unref(layout);
76
77         return TRUE;
78 }
79
80 void gtkut_widget_set_small_font_size(GtkWidget *widget)
81 {
82         PangoFontDescription *font_desc;
83         gint size;
84
85         cm_return_if_fail(widget != NULL);
86         cm_return_if_fail(gtk_widget_get_style(widget) != NULL);
87
88         if (prefs_common.derive_from_normal_font || !SMALL_FONT) {
89                 font_desc = pango_font_description_from_string(NORMAL_FONT);
90                 size = pango_font_description_get_size(font_desc);
91                 pango_font_description_set_size(font_desc, size * PANGO_SCALE_SMALL);
92                 gtk_widget_modify_font(widget, font_desc);
93                 pango_font_description_free(font_desc);
94         } else {
95                 font_desc = pango_font_description_from_string(SMALL_FONT);
96                 gtk_widget_modify_font(widget, font_desc);
97                 pango_font_description_free(font_desc);
98         }
99 }
100
101 void gtkut_convert_int_to_gdk_color(gint rgbvalue, GdkColor *color)
102 {
103         cm_return_if_fail(color != NULL);
104
105         color->pixel = 0L;
106         color->red   = (int) (((gdouble)((rgbvalue & 0xff0000) >> 16) / 255.0) * 65535.0);
107         color->green = (int) (((gdouble)((rgbvalue & 0x00ff00) >>  8) / 255.0) * 65535.0);
108         color->blue  = (int) (((gdouble) (rgbvalue & 0x0000ff)        / 255.0) * 65535.0);
109 }
110
111 #define CL(x)   (((gulong) (x) >> (gulong) 8) & 0xFFUL)
112 #define RGB_FROM_GDK_COLOR(c) \
113         ((CL(c->red)   << (gulong) 16) | \
114          (CL(c->green) << (gulong)  8) | \
115          (CL(c->blue)))
116
117 gint gtkut_convert_gdk_color_to_int(GdkColor *color)
118 {
119         return RGB_FROM_GDK_COLOR(color);
120 }
121
122 void gtkut_stock_button_add_help(GtkWidget *bbox, GtkWidget **help_btn)
123 {
124         cm_return_if_fail(bbox != NULL);
125
126         *help_btn = gtk_button_new_from_stock(GTK_STOCK_HELP);
127
128         gtkut_widget_set_can_default(*help_btn, TRUE);
129         gtk_box_pack_end(GTK_BOX (bbox), *help_btn, TRUE, TRUE, 0);
130         gtk_button_box_set_child_secondary(GTK_BUTTON_BOX (bbox),
131                         *help_btn, TRUE);
132         gtk_widget_set_sensitive(*help_btn,
133                         manual_available(MANUAL_MANUAL_CLAWS));
134         gtk_widget_show(*help_btn);
135 }
136
137 void gtkut_stock_button_set_create_with_help(GtkWidget **bbox,
138                 GtkWidget **help_button,
139                 GtkWidget **button1, const gchar *label1,
140                 GtkWidget **button2, const gchar *label2,
141                 GtkWidget **button3, const gchar *label3)
142 {
143         cm_return_if_fail(bbox != NULL);
144         cm_return_if_fail(button1 != NULL);
145
146         gtkut_stock_button_set_create(bbox, button1, label1,
147                         button2, label2, button3, label3);
148
149         gtkut_stock_button_add_help(*bbox, help_button);
150 }
151
152 void gtkut_stock_button_set_create(GtkWidget **bbox,
153                                    GtkWidget **button1, const gchar *label1,
154                                    GtkWidget **button2, const gchar *label2,
155                                    GtkWidget **button3, const gchar *label3)
156 {
157         cm_return_if_fail(bbox != NULL);
158         cm_return_if_fail(button1 != NULL);
159
160         *bbox = gtk_hbutton_box_new();
161         gtk_button_box_set_layout(GTK_BUTTON_BOX(*bbox), GTK_BUTTONBOX_END);
162         gtk_box_set_spacing(GTK_BOX(*bbox), 5);
163
164         *button1 = gtk_button_new_from_stock(label1);
165         gtkut_widget_set_can_default(*button1, TRUE);
166         gtk_box_pack_start(GTK_BOX(*bbox), *button1, TRUE, TRUE, 0);
167         gtk_widget_show(*button1);
168
169         if (button2) {
170                 *button2 = gtk_button_new_from_stock(label2);
171                 gtkut_widget_set_can_default(*button2, TRUE);
172                 gtk_box_pack_start(GTK_BOX(*bbox), *button2, TRUE, TRUE, 0);
173                 gtk_widget_show(*button2);
174         }
175
176         if (button3) {
177                 *button3 = gtk_button_new_from_stock(label3);
178                 gtkut_widget_set_can_default(*button3, TRUE);
179                 gtk_box_pack_start(GTK_BOX(*bbox), *button3, TRUE, TRUE, 0);
180                 gtk_widget_show(*button3);
181         }
182 }
183
184 void gtkut_stock_with_text_button_set_create(GtkWidget **bbox,
185                                    GtkWidget **button1, const gchar *label1, const gchar *text1,
186                                    GtkWidget **button2, const gchar *label2, const gchar *text2,
187                                    GtkWidget **button3, const gchar *label3, const gchar *text3)
188 {
189         cm_return_if_fail(bbox != NULL);
190         cm_return_if_fail(button1 != NULL);
191
192         *bbox = gtk_hbutton_box_new();
193         gtk_button_box_set_layout(GTK_BUTTON_BOX(*bbox), GTK_BUTTONBOX_END);
194         gtk_box_set_spacing(GTK_BOX(*bbox), 5);
195
196         *button1 = gtk_button_new_with_mnemonic(text1);
197         gtk_button_set_image(GTK_BUTTON(*button1),
198                 gtk_image_new_from_stock(label1, GTK_ICON_SIZE_BUTTON));
199         gtkut_widget_set_can_default(*button1, TRUE);
200         gtk_box_pack_start(GTK_BOX(*bbox), *button1, TRUE, TRUE, 0);
201         gtk_widget_show(*button1);
202
203         if (button2) {
204                 *button2 = gtk_button_new_with_mnemonic(text2);
205                 gtk_button_set_image(GTK_BUTTON(*button2),
206                         gtk_image_new_from_stock(label2, GTK_ICON_SIZE_BUTTON));
207                 gtkut_widget_set_can_default(*button2, TRUE);
208                 gtk_box_pack_start(GTK_BOX(*bbox), *button2, TRUE, TRUE, 0);
209                 gtk_widget_show(*button2);
210         }
211
212         if (button3) {
213                 *button3 = gtk_button_new_with_mnemonic(text3);
214                 gtk_button_set_image(GTK_BUTTON(*button3),
215                         gtk_image_new_from_stock(label3, GTK_ICON_SIZE_BUTTON));
216                 gtkut_widget_set_can_default(*button3, TRUE);
217                 gtk_box_pack_start(GTK_BOX(*bbox), *button3, TRUE, TRUE, 0);
218                 gtk_widget_show(*button3);
219         }
220 }
221
222 #define CELL_SPACING 1
223 #define ROW_TOP_YPIXEL(clist, row) (((clist)->row_height * (row)) + \
224                                     (((row) + 1) * CELL_SPACING) + \
225                                     (clist)->voffset)
226 #define ROW_FROM_YPIXEL(clist, y) (((y) - (clist)->voffset) / \
227                                    ((clist)->row_height + CELL_SPACING))
228
229 void gtkut_ctree_node_move_if_on_the_edge(GtkCMCTree *ctree, GtkCMCTreeNode *node, gint _row)
230 {
231         GtkCMCList *clist = GTK_CMCLIST(ctree);
232         gint row;
233         GtkVisibility row_visibility, prev_row_visibility, next_row_visibility;
234         gfloat row_align;
235
236         cm_return_if_fail(ctree != NULL);
237         cm_return_if_fail(node != NULL);
238
239         row = (_row != -1 ? _row : g_list_position(clist->row_list, (GList *)node));
240
241         if (row < 0 || row >= clist->rows || clist->row_height == 0) return;
242         row_visibility = gtk_cmclist_row_is_visible(clist, row);
243         prev_row_visibility = gtk_cmclist_row_is_visible(clist, row - 1);
244         next_row_visibility = gtk_cmclist_row_is_visible(clist, row + 1);
245
246         if (row_visibility == GTK_VISIBILITY_NONE) {
247                 row_align = 0.5;
248                 if (gtk_cmclist_row_is_above_viewport(clist, row))
249                         row_align = 0.2;
250                 else if (gtk_cmclist_row_is_below_viewport(clist, row))
251                         row_align = 0.8;
252                 gtk_cmclist_moveto(clist, row, -1, row_align, 0);
253                 return;
254         }
255         if (row_visibility == GTK_VISIBILITY_FULL &&
256             prev_row_visibility == GTK_VISIBILITY_FULL &&
257             next_row_visibility == GTK_VISIBILITY_FULL)
258                 return;
259         if (prev_row_visibility != GTK_VISIBILITY_FULL &&
260             next_row_visibility != GTK_VISIBILITY_FULL)
261                 return;
262
263         if (prev_row_visibility != GTK_VISIBILITY_FULL) {
264                 gtk_cmclist_moveto(clist, row, -1, 0.2, 0);
265                 return;
266         }
267         if (next_row_visibility != GTK_VISIBILITY_FULL) {
268                 gtk_cmclist_moveto(clist, row, -1, 0.8, 0);
269                 return;
270         }
271 }
272
273 #undef CELL_SPACING
274 #undef ROW_TOP_YPIXEL
275 #undef ROW_FROM_YPIXEL
276
277 gint gtkut_ctree_get_nth_from_node(GtkCMCTree *ctree, GtkCMCTreeNode *node)
278 {
279         cm_return_val_if_fail(ctree != NULL, -1);
280         cm_return_val_if_fail(node != NULL, -1);
281
282         return g_list_position(GTK_CMCLIST(ctree)->row_list, (GList *)node);
283 }
284
285 /* get the next node, including the invisible one */
286 GtkCMCTreeNode *gtkut_ctree_node_next(GtkCMCTree *ctree, GtkCMCTreeNode *node)
287 {
288         GtkCMCTreeNode *parent;
289
290         if (!node) return NULL;
291
292         if (GTK_CMCTREE_ROW(node)->children)
293                 return GTK_CMCTREE_ROW(node)->children;
294
295         if (GTK_CMCTREE_ROW(node)->sibling)
296                 return GTK_CMCTREE_ROW(node)->sibling;
297
298         for (parent = GTK_CMCTREE_ROW(node)->parent; parent != NULL;
299              parent = GTK_CMCTREE_ROW(parent)->parent) {
300                 if (GTK_CMCTREE_ROW(parent)->sibling)
301                         return GTK_CMCTREE_ROW(parent)->sibling;
302         }
303
304         return NULL;
305 }
306
307 /* get the previous node, including the invisible one */
308 GtkCMCTreeNode *gtkut_ctree_node_prev(GtkCMCTree *ctree, GtkCMCTreeNode *node)
309 {
310         GtkCMCTreeNode *prev;
311         GtkCMCTreeNode *child;
312
313         if (!node) return NULL;
314
315         prev = GTK_CMCTREE_NODE_PREV(node);
316         if (prev == GTK_CMCTREE_ROW(node)->parent)
317                 return prev;
318
319         child = prev;
320         while (GTK_CMCTREE_ROW(child)->children != NULL) {
321                 child = GTK_CMCTREE_ROW(child)->children;
322                 while (GTK_CMCTREE_ROW(child)->sibling != NULL)
323                         child = GTK_CMCTREE_ROW(child)->sibling;
324         }
325
326         return child;
327 }
328
329 gboolean gtkut_ctree_node_is_selected(GtkCMCTree *ctree, GtkCMCTreeNode *node)
330 {
331         GtkCMCList *clist = GTK_CMCLIST(ctree);
332         GList *cur;
333
334         for (cur = clist->selection; cur != NULL; cur = cur->next) {
335                 if (node == GTK_CMCTREE_NODE(cur->data))
336                         return TRUE;
337         }
338
339         return FALSE;
340 }
341
342 GtkCMCTreeNode *gtkut_ctree_find_collapsed_parent(GtkCMCTree *ctree,
343                                                 GtkCMCTreeNode *node)
344 {
345         if (!node) return NULL;
346
347         while ((node = GTK_CMCTREE_ROW(node)->parent) != NULL) {
348                 if (!GTK_CMCTREE_ROW(node)->expanded)
349                         return node;
350         }
351
352         return NULL;
353 }
354
355 void gtkut_ctree_expand_parent_all(GtkCMCTree *ctree, GtkCMCTreeNode *node)
356 {
357         gtk_cmclist_freeze(GTK_CMCLIST(ctree));
358
359         while ((node = gtkut_ctree_find_collapsed_parent(ctree, node)) != NULL)
360                 gtk_cmctree_expand(ctree, node);
361
362         gtk_cmclist_thaw(GTK_CMCLIST(ctree));
363 }
364
365 gboolean gtkut_ctree_node_is_parent(GtkCMCTreeNode *parent, GtkCMCTreeNode *node)
366 {
367         GtkCMCTreeNode *tmp;
368         cm_return_val_if_fail(node != NULL, FALSE);
369         cm_return_val_if_fail(parent != NULL, FALSE);
370         tmp = node;
371         
372         while (tmp) {
373                 if(GTK_CMCTREE_ROW(tmp)->parent && GTK_CMCTREE_ROW(tmp)->parent == parent)
374                         return TRUE;
375                 tmp = GTK_CMCTREE_ROW(tmp)->parent;
376         }
377         
378         return FALSE;
379 }
380
381 void gtkut_ctree_set_focus_row(GtkCMCTree *ctree, GtkCMCTreeNode *node)
382 {
383         if (node == NULL)
384                 return;
385         gtkut_clist_set_focus_row(GTK_CMCLIST(ctree),
386                                   gtkut_ctree_get_nth_from_node(ctree, node));
387 }
388
389 void gtkut_clist_set_focus_row(GtkCMCList *clist, gint row)
390 {
391         clist->focus_row = row;
392         GTKUT_CTREE_REFRESH(clist);
393 }
394
395 void gtkut_container_remove(GtkContainer *container, GtkWidget *widget)
396 {
397         gtk_container_remove(container, widget);
398 }
399
400 static gboolean gtkut_text_buffer_match_string(GtkTextBuffer *textbuf,
401                                         const GtkTextIter *iter,
402                                         gunichar *wcs, gint len,
403                                         gboolean case_sens)
404 {
405         GtkTextIter start_iter, end_iter;
406         gchar *utf8str, *p;
407         gint match_count;
408
409         start_iter = end_iter = *iter;
410         gtk_text_iter_forward_chars(&end_iter, len);
411
412         utf8str = gtk_text_buffer_get_text(textbuf, &start_iter, &end_iter,
413                                            FALSE);
414         if (!utf8str) return FALSE;
415
416         if ((gint)g_utf8_strlen(utf8str, -1) != len) {
417                 g_free(utf8str);
418                 return FALSE;
419         }
420
421         for (p = utf8str, match_count = 0;
422              *p != '\0' && match_count < len;
423              p = g_utf8_next_char(p), match_count++) {
424                 gunichar wc;
425
426                 wc = g_utf8_get_char(p);
427
428                 if (case_sens) {
429                         if (wc != wcs[match_count])
430                                 break;
431                 } else {
432                         if (g_unichar_tolower(wc) !=
433                             g_unichar_tolower(wcs[match_count]))
434                                 break;
435                 }
436         }
437
438         g_free(utf8str);
439
440         if (match_count == len)
441                 return TRUE;
442         else
443                 return FALSE;
444 }
445
446 static gboolean gtkut_text_buffer_find(GtkTextBuffer *buffer, const GtkTextIter *iter,
447                                 const gchar *str, gboolean case_sens,
448                                 GtkTextIter *match_pos)
449 {
450         gunichar *wcs;
451         gint len;
452         glong items_read = 0, items_written = 0;
453         GError *error = NULL;
454         GtkTextIter iter_;
455         gboolean found = FALSE;
456
457         wcs = g_utf8_to_ucs4(str, -1, &items_read, &items_written, &error);
458         if (error != NULL) {
459                 g_warning("An error occurred while converting a string from UTF-8 to UCS-4: %s",
460                           error->message);
461                 g_error_free(error);
462         }
463         if (!wcs || items_written <= 0) return FALSE;
464         len = (gint)items_written;
465
466         iter_ = *iter;
467         do {
468                 found = gtkut_text_buffer_match_string
469                         (buffer, &iter_, wcs, len, case_sens);
470                 if (found) {
471                         *match_pos = iter_;
472                         break;
473                 }
474         } while (gtk_text_iter_forward_char(&iter_));
475
476         g_free(wcs);
477
478         return found;
479 }
480
481 static gboolean gtkut_text_buffer_find_backward(GtkTextBuffer *buffer,
482                                          const GtkTextIter *iter,
483                                          const gchar *str, gboolean case_sens,
484                                          GtkTextIter *match_pos)
485 {
486         gunichar *wcs;
487         gint len;
488         glong items_read = 0, items_written = 0;
489         GError *error = NULL;
490         GtkTextIter iter_;
491         gboolean found = FALSE;
492
493         wcs = g_utf8_to_ucs4(str, -1, &items_read, &items_written, &error);
494         if (error != NULL) {
495                 g_warning("An error occurred while converting a string from UTF-8 to UCS-4: %s",
496                           error->message);
497                 g_error_free(error);
498         }
499         if (!wcs || items_written <= 0) return FALSE;
500         len = (gint)items_written;
501
502         iter_ = *iter;
503         while (gtk_text_iter_backward_char(&iter_)) {
504                 found = gtkut_text_buffer_match_string
505                         (buffer, &iter_, wcs, len, case_sens);
506                 if (found) {
507                         *match_pos = iter_;
508                         break;
509                 }
510         }
511
512         g_free(wcs);
513
514         return found;
515 }
516
517 gchar *gtkut_text_view_get_selection(GtkTextView *textview)
518 {
519         GtkTextBuffer *buffer;
520         GtkTextIter start_iter, end_iter;
521         gboolean found;
522
523         cm_return_val_if_fail(GTK_IS_TEXT_VIEW(textview), NULL);
524
525         buffer = gtk_text_view_get_buffer(textview);
526         found = gtk_text_buffer_get_selection_bounds(buffer,
527                                                      &start_iter,
528                                                      &end_iter);
529         if (found)
530                 return gtk_text_buffer_get_text(buffer, &start_iter, &end_iter,
531                                                 FALSE);
532         else
533                 return NULL;
534 }
535
536
537 void gtkut_text_view_set_position(GtkTextView *text, gint pos)
538 {
539         GtkTextBuffer *buffer;
540         GtkTextIter iter;
541         GtkTextMark *mark;
542
543         cm_return_if_fail(text != NULL);
544
545         buffer = gtk_text_view_get_buffer(text);
546
547         gtk_text_buffer_get_iter_at_offset(buffer, &iter, pos);
548         gtk_text_buffer_place_cursor(buffer, &iter);
549         mark = gtk_text_buffer_create_mark(buffer, NULL, &iter, TRUE);
550         gtk_text_view_scroll_to_mark(text, mark, 0.0, FALSE, 0.0, 0.0);
551 }
552
553 gboolean gtkut_text_view_search_string(GtkTextView *text, const gchar *str,
554                                         gboolean case_sens)
555 {
556         GtkTextBuffer *buffer;
557         GtkTextIter iter, match_pos;
558         GtkTextMark *mark;
559         gint len;
560
561         cm_return_val_if_fail(text != NULL, FALSE);
562         cm_return_val_if_fail(str != NULL, FALSE);
563
564         buffer = gtk_text_view_get_buffer(text);
565
566         len = g_utf8_strlen(str, -1);
567         cm_return_val_if_fail(len >= 0, FALSE);
568
569         mark = gtk_text_buffer_get_insert(buffer);
570         gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
571
572         if (gtkut_text_buffer_find(buffer, &iter, str, case_sens,
573                                    &match_pos)) {
574                 GtkTextIter end = match_pos;
575
576                 gtk_text_iter_forward_chars(&end, len);
577                 /* place "insert" at the last character */
578                 gtk_text_buffer_select_range(buffer, &end, &match_pos);
579                 gtk_text_view_scroll_to_mark(text, mark, 0.0, TRUE, 0.0, 0.5);
580                 return TRUE;
581         }
582
583         return FALSE;
584 }
585
586 gboolean gtkut_text_view_search_string_backward(GtkTextView *text, const gchar *str,
587                                         gboolean case_sens)
588 {
589         GtkTextBuffer *buffer;
590         GtkTextIter iter, match_pos;
591         GtkTextMark *mark;
592         gint len;
593
594         cm_return_val_if_fail(text != NULL, FALSE);
595         cm_return_val_if_fail(str != NULL, FALSE);
596
597         buffer = gtk_text_view_get_buffer(text);
598
599         len = g_utf8_strlen(str, -1);
600         cm_return_val_if_fail(len >= 0, FALSE);
601
602         mark = gtk_text_buffer_get_insert(buffer);
603         gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
604
605         if (gtkut_text_buffer_find_backward(buffer, &iter, str, case_sens,
606                                             &match_pos)) {
607                 GtkTextIter end = match_pos;
608
609                 gtk_text_iter_forward_chars(&end, len);
610                 gtk_text_buffer_select_range(buffer, &match_pos, &end);
611                 gtk_text_view_scroll_to_mark(text, mark, 0.0, TRUE, 0.0, 0.5);
612                 return TRUE;
613         }
614
615         return FALSE;
616 }
617
618 void gtkut_window_popup(GtkWidget *window)
619 {
620         GdkWindow *gdkwin;
621         gint x, y, sx, sy, new_x, new_y;
622
623         gdkwin = gtk_widget_get_window(window);
624
625         cm_return_if_fail(window != NULL);
626         cm_return_if_fail(gdkwin != NULL);
627
628         sx = gdk_screen_width();
629         sy = gdk_screen_height();
630
631         gdk_window_get_origin(gdkwin, &x, &y);
632         new_x = x % sx; if (new_x < 0) new_x = 0;
633         new_y = y % sy; if (new_y < 0) new_y = 0;
634         if (new_x != x || new_y != y)
635                 gdk_window_move(gdkwin, new_x, new_y);
636
637         gtk_window_set_skip_taskbar_hint(GTK_WINDOW(window), FALSE);
638         gtk_window_present_with_time(GTK_WINDOW(window), time(NULL));
639 }
640
641 void gtkut_widget_get_uposition(GtkWidget *widget, gint *px, gint *py)
642 {
643         GdkWindow *gdkwin;
644         gint x, y;
645         gint sx, sy;
646
647         gdkwin = gtk_widget_get_window(widget);
648
649         cm_return_if_fail(widget != NULL);
650         cm_return_if_fail(gdkwin != NULL);
651
652         sx = gdk_screen_width();
653         sy = gdk_screen_height();
654
655         /* gdk_window_get_root_origin ever return *rootwindow*'s position */
656         gdk_window_get_root_origin(gdkwin, &x, &y);
657
658         x %= sx; if (x < 0) x = 0;
659         y %= sy; if (y < 0) y = 0;
660         *px = x;
661         *py = y;
662 }
663
664 void gtkut_widget_draw_now(GtkWidget *widget)
665 {
666         if (widget && gtk_widget_get_visible(widget) && gtk_widget_is_drawable(widget))
667                 gdk_window_process_updates(gtk_widget_get_window(widget), FALSE);
668 }
669
670 static void gtkut_clist_bindings_add(GtkWidget *clist)
671 {
672         GtkBindingSet *binding_set;
673
674         binding_set = gtk_binding_set_by_class
675                 (GTK_CMCLIST_GET_CLASS(clist));
676
677         gtk_binding_entry_add_signal(binding_set, GDK_KEY_n, GDK_CONTROL_MASK,
678                                      "scroll_vertical", 2,
679                                      G_TYPE_ENUM, GTK_SCROLL_STEP_FORWARD,
680                                      G_TYPE_FLOAT, 0.0);
681         gtk_binding_entry_add_signal(binding_set, GDK_KEY_p, GDK_CONTROL_MASK,
682                                      "scroll_vertical", 2,
683                                      G_TYPE_ENUM, GTK_SCROLL_STEP_BACKWARD,
684                                      G_TYPE_FLOAT, 0.0);
685 }
686
687 void gtkut_widget_init(void)
688 {
689         GtkWidget *clist;
690
691         clist = gtk_cmclist_new(1);
692         g_object_ref(G_OBJECT(clist));
693         g_object_ref_sink (G_OBJECT(clist));
694         gtkut_clist_bindings_add(clist);
695         g_object_unref(G_OBJECT(clist));
696
697         clist = gtk_cmctree_new(1, 0);
698         g_object_ref(G_OBJECT(clist));
699         g_object_ref_sink (G_OBJECT(clist));
700         gtkut_clist_bindings_add(clist);
701         g_object_unref(G_OBJECT(clist));
702
703         clist = gtk_sctree_new_with_titles(1, 0, NULL);
704         g_object_ref(G_OBJECT(clist));
705         g_object_ref_sink (G_OBJECT(clist));
706         gtkut_clist_bindings_add(clist);
707         g_object_unref(G_OBJECT(clist));
708 }
709
710 void gtkut_widget_set_app_icon(GtkWidget *widget)
711 {
712         static GList *icon_list = NULL;
713
714         cm_return_if_fail(widget != NULL);
715         cm_return_if_fail(gtk_widget_get_window(widget) != NULL);
716         if (!icon_list) {
717                 GdkPixbuf *icon = NULL, *big_icon = NULL;
718                 stock_pixbuf_gdk(STOCK_PIXMAP_CLAWS_MAIL_ICON, &icon);
719                 stock_pixbuf_gdk(STOCK_PIXMAP_CLAWS_MAIL_LOGO, &big_icon);
720                 if (icon)
721                         icon_list = g_list_append(icon_list, icon);
722                 if (big_icon)
723                         icon_list = g_list_append(icon_list, big_icon);
724         }
725         if (icon_list)
726                 gtk_window_set_icon_list(GTK_WINDOW(widget), icon_list);
727 }
728
729 void gtkut_widget_set_composer_icon(GtkWidget *widget)
730 {
731         static GList *icon_list = NULL;
732
733         cm_return_if_fail(widget != NULL);
734         cm_return_if_fail(gtk_widget_get_window(widget) != NULL);
735         if (!icon_list) {
736                 GdkPixbuf *icon = NULL, *big_icon = NULL;
737                 stock_pixbuf_gdk(STOCK_PIXMAP_MAIL_COMPOSE, &icon);
738                 stock_pixbuf_gdk(STOCK_PIXMAP_MAIL_COMPOSE_LOGO, &big_icon);
739                 if (icon)
740                         icon_list = g_list_append(icon_list, icon);
741                 if (big_icon)
742                         icon_list = g_list_append(icon_list, big_icon);
743         }
744         if (icon_list)
745                 gtk_window_set_icon_list(GTK_WINDOW(widget), icon_list);
746 }
747
748 static gboolean move_bar = FALSE;
749 static gint move_bar_id = -1;
750
751 static gboolean move_bar_cb(gpointer data)
752 {
753         GtkWidget *w = (GtkWidget *)data;
754         if (!move_bar)
755                 return FALSE;
756
757         if (!GTK_IS_PROGRESS_BAR(w)) {
758                 return FALSE;
759         }
760
761         gtk_progress_bar_pulse(GTK_PROGRESS_BAR(w));
762         GTK_EVENTS_FLUSH();
763         return TRUE;
764 }
765
766 GtkWidget *label_window_create(const gchar *str)
767 {
768         GtkWidget *window;
769         GtkWidget *label, *vbox, *hbox;
770         GtkWidget *wait_progress = gtk_progress_bar_new();
771
772         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "gtkutils");
773         gtk_widget_set_size_request(window, 380, 70);
774         gtk_container_set_border_width(GTK_CONTAINER(window), 8);
775         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
776         gtk_window_set_title(GTK_WINDOW(window), str);
777         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
778         gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
779         manage_window_set_transient(GTK_WINDOW(window));
780
781         label = gtk_label_new(str);
782         
783         vbox = gtk_vbox_new(FALSE, 6);
784         hbox = gtk_hbox_new(FALSE, 6);
785         gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, FALSE, 0);
786         gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, FALSE, 0);
787         hbox = gtk_hbox_new(FALSE, 6);
788         gtk_box_pack_start(GTK_BOX(hbox), wait_progress, TRUE, FALSE, 0);
789         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
790         
791         gtk_container_add(GTK_CONTAINER(window), vbox);
792         gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
793         gtk_misc_set_alignment(GTK_MISC(label), 0.5, 0.5);
794         gtk_widget_show_all(vbox);
795
796         gtk_widget_show_now(window);
797         
798         if (move_bar_id == -1) {
799                 move_bar_id = g_timeout_add(200, move_bar_cb, wait_progress);
800                 move_bar = TRUE;
801         }
802
803         GTK_EVENTS_FLUSH();
804
805         return window;
806 }
807
808 void label_window_destroy(GtkWidget *window)
809 {
810         move_bar = FALSE;
811         g_source_remove(move_bar_id);
812         move_bar_id = -1;
813         GTK_EVENTS_FLUSH();
814         gtk_widget_destroy(window);     
815 }
816
817 GtkWidget *gtkut_account_menu_new(GList                 *ac_list,
818                                         GCallback               callback,
819                                   gpointer              data)
820 {
821         GList *cur_ac;
822         GtkWidget *optmenu;
823         GtkListStore *menu;
824         GtkTreeIter iter;
825         PrefsAccount *account;
826         gchar *name;
827         
828         cm_return_val_if_fail(ac_list != NULL, NULL);
829
830         optmenu = gtkut_sc_combobox_create(NULL, FALSE);
831         menu = GTK_LIST_STORE(gtk_combo_box_get_model(GTK_COMBO_BOX(optmenu)));
832
833         for (cur_ac = ac_list; cur_ac != NULL; cur_ac = cur_ac->next) {
834                 account = (PrefsAccount *) cur_ac->data;
835                 if (account->name)
836                         name = g_strdup_printf("%s: %s <%s>",
837                                                account->account_name,
838                                                account->name,
839                                                account->address);
840                 else
841                         name = g_strdup_printf("%s: %s",
842                                                account->account_name,
843                                                account->address);
844                 COMBOBOX_ADD_ESCAPED(menu, name, account->account_id);
845                 g_free(name);
846         }
847         gtk_combo_box_set_active(GTK_COMBO_BOX(optmenu), 0);
848
849         if( callback != NULL )
850                 g_signal_connect(G_OBJECT(optmenu), "changed", callback, data);
851
852         return optmenu;
853 }
854
855 void gtkut_set_widget_bgcolor_rgb(GtkWidget *widget, guint rgbvalue)
856 {
857         GtkStyle *newstyle;
858         GdkColor gdk_color;
859
860         gtkut_convert_int_to_gdk_color(rgbvalue, &gdk_color);
861         newstyle = gtk_style_copy(gtk_widget_get_default_style());
862         newstyle->bg[GTK_STATE_NORMAL]   = gdk_color;
863         newstyle->bg[GTK_STATE_PRELIGHT] = gdk_color;
864         newstyle->bg[GTK_STATE_ACTIVE]   = gdk_color;
865         gtk_widget_set_style(widget, newstyle);
866         gtk_style_unref(newstyle);
867 }
868   
869 /*!
870  *\brief        Tries to find a focused child using a lame strategy
871  */
872 GtkWidget *gtkut_get_focused_child(GtkContainer *parent)
873 {
874         GtkWidget *result = NULL;
875         GList *child_list = NULL;
876         GList *c;
877
878         cm_return_val_if_fail(parent, NULL);
879
880         /* Get children list and see which has the focus. */
881         child_list = gtk_container_get_children(parent);
882         if (!child_list)
883                 return NULL;
884
885         for (c = child_list; c != NULL; c = g_list_next(c)) {
886                 if (c->data && GTK_IS_WIDGET(c->data)) {
887                         if (gtk_widget_has_focus(GTK_WIDGET(c->data))) {
888                                 result = GTK_WIDGET(c->data);
889                                 break;
890                         }
891                 }
892         }
893         
894         /* See if the returned widget is a container itself; if it is,
895          * see if one of its children is focused. If the focused 
896          * container has no focused child, it is itself a focusable 
897          * child, and has focus. */
898         if (result && GTK_IS_CONTAINER(result)) {
899                 GtkWidget *tmp =  gtkut_get_focused_child(GTK_CONTAINER(result)); 
900                 
901                 if (tmp) 
902                         result = tmp;
903         } else {
904                 /* Try the same for each container in the chain */
905                 for (c = child_list; c != NULL && !result; c = g_list_next(c)) {
906                         if (c->data && GTK_IS_WIDGET(c->data) 
907                         &&  GTK_IS_CONTAINER(c->data)) {
908                                 result = gtkut_get_focused_child
909                                         (GTK_CONTAINER(c->data));
910                         }
911                 }
912         
913         }
914         
915         g_list_free(child_list);
916                 
917         return result;
918 }
919
920 /*!
921  *\brief        Create a Browse (file) button based on GTK+ stock
922  */
923 GtkWidget *gtkut_get_browse_file_btn(const gchar *button_label)
924 {
925         GtkWidget *button;
926
927         button = gtk_button_new_with_mnemonic(button_label);
928         gtk_button_set_image(GTK_BUTTON(button),
929                 gtk_image_new_from_stock(GTK_STOCK_DIRECTORY, GTK_ICON_SIZE_BUTTON));
930
931         return button;
932 }
933
934 /*!
935  *\brief        Create a Browse (directory) button based on GTK+ stock
936  */
937 GtkWidget *gtkut_get_browse_directory_btn(const gchar *button_label)
938 {
939         GtkWidget *button;
940
941         button = gtk_button_new_with_mnemonic(button_label);
942         gtk_button_set_image(GTK_BUTTON(button),
943                 gtk_image_new_from_stock(GTK_STOCK_DIRECTORY, GTK_ICON_SIZE_BUTTON));
944
945         return button;
946 }
947
948 GtkWidget *gtkut_get_replace_btn(const gchar *button_label)
949 {
950         GtkWidget *button;
951
952         button = gtk_button_new_with_mnemonic(button_label);
953         gtk_button_set_image(GTK_BUTTON(button),
954                 gtk_image_new_from_stock(GTK_STOCK_REFRESH, GTK_ICON_SIZE_BUTTON));
955
956         return button;
957 }
958
959 /**
960  * merge some part of code into one function : it creates a frame and add
961  *      these into gtk box widget passed in param.
962  * \param box gtk box where adding new created frame.
963  * \param pframe pointer with which to assign the frame. If NULL, no pointer
964  *      is assigned but the frame is anyway created and added to @box.
965  * \param frame_label frame label of new created frame.
966  */
967 GtkWidget *gtkut_get_options_frame(GtkWidget *box, GtkWidget **pframe,
968                 const gchar *frame_label)
969 {
970         GtkWidget *vbox;
971         GtkWidget *frame;
972
973         frame = gtk_frame_new(frame_label);
974         gtk_widget_show(frame);
975         gtk_box_pack_start(GTK_BOX(box), frame, FALSE, TRUE, 0);
976         gtk_frame_set_label_align(GTK_FRAME(frame), 0.01, 0.5);
977
978         vbox = gtk_vbox_new (FALSE, 4);
979         gtk_widget_show(vbox);
980         gtk_container_add(GTK_CONTAINER (frame), vbox);
981         gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
982
983         if (pframe != NULL)
984                 *pframe = frame;
985
986         return vbox;
987 }
988
989 #if HAVE_LIBCOMPFACE
990 static gint create_xpm_from_xface(gchar *xpm[], const gchar *xface)
991 {
992         static gchar *bit_pattern[] = {
993                 "....",
994                 "...#",
995                 "..#.",
996                 "..##",
997                 ".#..",
998                 ".#.#",
999                 ".##.",
1000                 ".###",
1001                 "#...",
1002                 "#..#",
1003                 "#.#.",
1004                 "#.##",
1005                 "##..",
1006                 "##.#",
1007                 "###.",
1008                 "####"
1009         };
1010
1011         static gchar *xface_header = "48 48 2 1";
1012         static gchar *xface_black  = "# c #000000";
1013         static gchar *xface_white  = ". c #ffffff";
1014
1015         gint i, line = 0;
1016         const guchar *p;
1017         gchar buf[WIDTH * 4 + 1];  /* 4 = strlen("0x0000") */
1018
1019         p = xface;
1020
1021         strcpy(xpm[line++], xface_header);
1022         strcpy(xpm[line++], xface_black);
1023         strcpy(xpm[line++], xface_white);
1024
1025         for (i = 0; i < HEIGHT; i++) {
1026                 gint col;
1027
1028                 buf[0] = '\0';
1029      
1030                 for (col = 0; col < 3; col++) {
1031                         gint figure;
1032
1033                         p += 2;  /* skip '0x' */
1034
1035                         for (figure = 0; figure < 4; figure++) {
1036                                 gint n = 0;
1037
1038                                 if ('0' <= *p && *p <= '9') {
1039                                         n = *p - '0';
1040                                 } else if ('a' <= *p && *p <= 'f') {
1041                                         n = *p - 'a' + 10;
1042                                 } else if ('A' <= *p && *p <= 'F') {
1043                                         n = *p - 'A' + 10;
1044                                 }
1045
1046                                 strcat(buf, bit_pattern[n]);
1047                                 p++;  /* skip ',' */
1048                         }
1049
1050                         p++;  /* skip '\n' */
1051                 }
1052
1053                 strcpy(xpm[line++], buf);
1054                 p++;
1055         }
1056
1057         return 0;
1058 }
1059 #endif
1060
1061 gboolean get_tag_range(GtkTextIter *iter,
1062                                        GtkTextTag *tag,
1063                                        GtkTextIter *start_iter,
1064                                        GtkTextIter *end_iter)
1065 {
1066         GtkTextIter _start_iter, _end_iter;
1067
1068         _end_iter = *iter;
1069         if (!gtk_text_iter_forward_to_tag_toggle(&_end_iter, tag)) {
1070                 debug_print("Can't find end.\n");
1071                 return FALSE;
1072         }
1073
1074         _start_iter = _end_iter;
1075         if (!gtk_text_iter_backward_to_tag_toggle(&_start_iter, tag)) {
1076                 debug_print("Can't find start.\n");
1077                 return FALSE;
1078         }
1079
1080         *start_iter = _start_iter;
1081         *end_iter = _end_iter;
1082
1083         return TRUE;
1084 }
1085
1086 #if HAVE_LIBCOMPFACE
1087 GtkWidget *xface_get_from_header(const gchar *o_xface)
1088 {
1089         static gchar *xpm_xface[XPM_XFACE_HEIGHT];
1090         static gboolean xpm_xface_init = TRUE;
1091         gchar xface[2048];
1092         strncpy(xface, o_xface, sizeof(xface) - 1);
1093         xface[sizeof(xface) - 1] = '\0';
1094
1095         if (uncompface(xface) < 0) {
1096                 g_warning("uncompface failed");
1097                 return NULL;
1098         }
1099
1100         if (xpm_xface_init) {
1101                 gint i;
1102
1103                 for (i = 0; i < XPM_XFACE_HEIGHT; i++) {
1104                         xpm_xface[i] = g_malloc(WIDTH + 1);
1105                         *xpm_xface[i] = '\0';
1106                 }
1107                 xpm_xface_init = FALSE;
1108         }
1109
1110         create_xpm_from_xface(xpm_xface, xface);
1111
1112         return gtk_image_new_from_pixbuf(
1113                 gdk_pixbuf_new_from_xpm_data((const char **)xpm_xface));
1114 }
1115 #endif
1116
1117 GtkWidget *face_get_from_header(const gchar *o_face)
1118 {
1119         gchar face[2048];
1120         gchar *face_png;
1121         gsize pngsize;
1122         GdkPixbuf *pixbuf;
1123         GError *error = NULL;
1124         GdkPixbufLoader *loader = gdk_pixbuf_loader_new ();
1125         GtkWidget *image;
1126         
1127         if (o_face == NULL || strlen(o_face) == 0)
1128                 return NULL;
1129
1130         strncpy2(face, o_face, sizeof(face));
1131
1132         unfold_line(face); /* strip all whitespace and linebreaks */
1133         remove_space(face);
1134
1135         face_png = g_base64_decode(face, &pngsize);
1136         debug_print("---------------------- loaded face png\n");
1137
1138         if (!gdk_pixbuf_loader_write (loader, face_png, pngsize, &error) ||
1139             !gdk_pixbuf_loader_close (loader, &error)) {
1140                 g_warning("loading face failed");
1141                 g_object_unref(loader);
1142                 g_free(face_png);
1143                 return NULL;
1144         }
1145         g_free(face_png);
1146
1147         pixbuf = g_object_ref(gdk_pixbuf_loader_get_pixbuf(loader));
1148
1149         g_object_unref(loader);
1150
1151         if ((gdk_pixbuf_get_width(pixbuf) != 48) || (gdk_pixbuf_get_height(pixbuf) != 48)) {
1152                 g_object_unref(pixbuf);
1153                 g_warning("wrong_size");
1154                 return NULL;
1155         }
1156
1157         image = gtk_image_new_from_pixbuf(pixbuf);
1158         g_object_unref(pixbuf);
1159         return image;
1160 }
1161
1162 static GdkCursor *hand_cursor = NULL;
1163
1164 static void link_btn_enter(GtkButton *button, gpointer data)
1165 {
1166         GdkWindow *gdkwin;
1167         GtkWidget *window = (GtkWidget *)data;
1168
1169         gdkwin = gtk_widget_get_window(window);
1170
1171         if (!hand_cursor)
1172                 hand_cursor = gdk_cursor_new(GDK_HAND2);
1173         if (window && gdkwin)
1174                 gdk_window_set_cursor(gdkwin, hand_cursor);
1175
1176         gtk_button_set_relief(button, GTK_RELIEF_NONE);
1177         gtk_widget_set_state(GTK_WIDGET(button), GTK_STATE_NORMAL);
1178         
1179 }
1180
1181 static void link_btn_leave(GtkButton *button, gpointer data)
1182 {
1183         GdkWindow *gdkwin;
1184         GtkWidget *window = (GtkWidget *)data;
1185
1186         gdkwin = gtk_widget_get_window(window);
1187
1188         if (window && gdkwin)
1189                 gdk_window_set_cursor(gdkwin, NULL);
1190
1191         gtk_button_set_relief(button, GTK_RELIEF_NONE);
1192         gtk_widget_set_state(GTK_WIDGET(button), GTK_STATE_NORMAL);
1193 }
1194
1195 static void link_btn_pressed(GtkButton *button, gpointer data)
1196 {
1197         gtk_button_set_relief(button, GTK_RELIEF_NONE);
1198         gtk_widget_set_state(GTK_WIDGET(button), GTK_STATE_NORMAL);
1199 }
1200
1201 static void link_btn_released(GtkButton *button, gpointer data)
1202 {
1203         gtk_button_set_relief(button, GTK_RELIEF_NONE);
1204         gtk_widget_set_state(GTK_WIDGET(button), GTK_STATE_NORMAL);
1205 }
1206
1207 static void link_btn_clicked(GtkButton *button, gpointer data)
1208 {
1209         gchar *url = (gchar *)data;
1210         gtk_button_set_relief(button, GTK_RELIEF_NONE);
1211         gtk_widget_set_state(GTK_WIDGET(button), GTK_STATE_NORMAL);
1212         open_uri(url, prefs_common_get_uri_cmd());
1213 }
1214
1215 static void link_btn_unrealize(GtkButton *button, gpointer data)
1216 {
1217         gchar *url = (gchar *)data;
1218         g_signal_handlers_disconnect_by_func(G_OBJECT(button), 
1219                          G_CALLBACK(link_btn_clicked), url);
1220         g_free(url);
1221 }
1222
1223 GtkWidget *gtkut_get_link_btn(GtkWidget *window, const gchar *url, const gchar *label)
1224 {
1225         GtkWidget *btn;
1226         GtkWidget *btn_label;
1227 #if !GTK_CHECK_VERSION(3, 0, 0)
1228         GdkColormap *cmap;
1229         gboolean success[2];
1230 #endif
1231         GdkColor uri_color[2] = {{0, 0, 0, 0xffff}, {0, 0xffff, 0, 0}};
1232         gchar *local_url = NULL;
1233         if (!url)
1234                 return NULL;
1235
1236         gtkut_convert_int_to_gdk_color(prefs_common.color[COL_URI],
1237                                                &uri_color[0]);
1238         gtkut_convert_int_to_gdk_color(prefs_common.color[COL_URI],
1239                                                &uri_color[1]);
1240
1241         btn = gtk_button_new_with_label(label?label:url);
1242         gtk_button_set_relief(GTK_BUTTON(btn), GTK_RELIEF_NONE);
1243         btn_label = gtk_bin_get_child(GTK_BIN((btn)));
1244 #if !GTK_CHECK_VERSION(3, 0, 0)
1245         cmap = gdk_drawable_get_colormap(gtk_widget_get_window(window));
1246         gdk_colormap_alloc_colors(cmap, uri_color, 2, FALSE, TRUE, success);
1247         if (success[0] == TRUE && success[1] == TRUE) {
1248 #endif
1249                 GtkStyle *style;
1250                 gtk_widget_ensure_style(btn_label);
1251                 style = gtk_style_copy
1252                         (gtk_widget_get_style(btn_label));
1253                 style->fg[GTK_STATE_NORMAL]   = uri_color[0];
1254                 style->fg[GTK_STATE_ACTIVE]   = uri_color[1];
1255                 style->fg[GTK_STATE_PRELIGHT] = uri_color[0];
1256                 gtk_widget_set_style(btn_label, style);
1257                 gtk_style_unref(style);
1258 #if !GTK_CHECK_VERSION(3, 0, 0)
1259         } else
1260                 g_warning("color allocation failed");
1261 #endif
1262
1263         g_signal_connect(G_OBJECT(btn), "enter",
1264                          G_CALLBACK(link_btn_enter), window);
1265         g_signal_connect(G_OBJECT(btn), "leave",
1266                          G_CALLBACK(link_btn_leave), window);
1267         g_signal_connect(G_OBJECT(btn), "pressed",
1268                          G_CALLBACK(link_btn_pressed), window);
1269         g_signal_connect(G_OBJECT(btn), "released",
1270                          G_CALLBACK(link_btn_released), window);
1271                          
1272         local_url = g_strdup(url);
1273         g_signal_connect(G_OBJECT(btn), "clicked",
1274                          G_CALLBACK(link_btn_clicked), local_url);
1275         g_signal_connect(G_OBJECT(btn), "unrealize",
1276                          G_CALLBACK(link_btn_unrealize), local_url);
1277         return btn;
1278 }
1279
1280 static gboolean _combobox_separator_func(GtkTreeModel *model,
1281                 GtkTreeIter *iter, gpointer data)
1282 {
1283         gchar *txt = NULL;
1284
1285         cm_return_val_if_fail(model != NULL, FALSE);
1286
1287         gtk_tree_model_get(model, iter, COMBOBOX_TEXT, &txt, -1);
1288
1289         if( txt == NULL )
1290                 return TRUE;
1291         
1292         g_free(txt);
1293         return FALSE;
1294 }
1295
1296 GtkWidget *gtkut_sc_combobox_create(GtkWidget *eventbox, gboolean focus_on_click)
1297 {
1298         GtkWidget *combobox;
1299         GtkListStore *menu;
1300         GtkCellRenderer *rend;
1301
1302         menu = gtk_list_store_new(3, G_TYPE_STRING, G_TYPE_INT, G_TYPE_BOOLEAN);
1303
1304         combobox = gtk_combo_box_new_with_model(GTK_TREE_MODEL(menu));
1305
1306         rend = gtk_cell_renderer_text_new();
1307         gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combobox), rend, TRUE);
1308         gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combobox), rend,
1309                         "markup", COMBOBOX_TEXT,
1310                         "sensitive", COMBOBOX_SENS,
1311                         NULL);
1312
1313         if( eventbox != NULL )
1314                 gtk_container_add(GTK_CONTAINER(eventbox), combobox);
1315         gtk_combo_box_set_focus_on_click(GTK_COMBO_BOX(combobox), focus_on_click);
1316
1317         gtk_combo_box_set_row_separator_func(GTK_COMBO_BOX(combobox),
1318                         (GtkTreeViewRowSeparatorFunc)_combobox_separator_func, NULL, NULL);
1319
1320         return combobox;
1321 }
1322
1323 static void gtkutils_smooth_scroll_do(GtkWidget *widget, GtkAdjustment *vadj,
1324                                       gfloat old_value, gfloat last_value,
1325                                       gint step)
1326 {
1327         gint change_value;
1328         gboolean up;
1329         gint i;
1330
1331         if (old_value < last_value) {
1332                 change_value = last_value - old_value;
1333                 up = FALSE;
1334         } else {
1335                 change_value = old_value - last_value;
1336                 up = TRUE;
1337         }
1338
1339         for (i = step; i <= change_value; i += step) {
1340                 gtk_adjustment_set_value(vadj, old_value + (up ? -i : i));
1341                 g_signal_emit_by_name(G_OBJECT(vadj),
1342                                       "value_changed", 0);
1343         }
1344
1345         gtk_adjustment_set_value(vadj, last_value);
1346         g_signal_emit_by_name(G_OBJECT(vadj), "value_changed", 0);
1347
1348         gtk_widget_queue_draw(widget);
1349 }
1350
1351 static gboolean gtkutils_smooth_scroll_page(GtkWidget *widget, GtkAdjustment *vadj, gboolean up)
1352 {
1353         gfloat upper;
1354         gfloat page_incr;
1355         gfloat old_value;
1356         gfloat last_value;
1357
1358         page_incr = gtk_adjustment_get_page_increment(vadj);
1359         if (prefs_common.scroll_halfpage)
1360                 page_incr /= 2;
1361
1362         old_value = gtk_adjustment_get_value(vadj);
1363         if (!up) {
1364                 upper = gtk_adjustment_get_upper(vadj) - gtk_adjustment_get_page_size(vadj);
1365                 if (old_value < upper) {
1366                         last_value = old_value + page_incr;
1367                         last_value = MIN(last_value, upper);
1368
1369                         gtkutils_smooth_scroll_do(widget, vadj, old_value,
1370                                                   last_value,
1371                                                   prefs_common.scroll_step);
1372                 } else
1373                         return FALSE;
1374         } else {
1375                 if (old_value > 0.0) {
1376                         last_value = old_value - page_incr;
1377                         last_value = MAX(last_value, 0.0);
1378
1379                         gtkutils_smooth_scroll_do(widget, vadj, old_value,
1380                                                   last_value,
1381                                                   prefs_common.scroll_step);
1382                 } else
1383                         return FALSE;
1384         }
1385
1386         return TRUE;
1387 }
1388
1389 gboolean gtkutils_scroll_page(GtkWidget *widget, GtkAdjustment *vadj, gboolean up)
1390 {
1391         gfloat upper;
1392         gfloat page_incr;
1393         gfloat old_value;
1394
1395         if (prefs_common.enable_smooth_scroll)
1396                 return gtkutils_smooth_scroll_page(widget, vadj, up);
1397
1398         page_incr = gtk_adjustment_get_page_increment(vadj);
1399         if (prefs_common.scroll_halfpage)
1400                 page_incr /= 2;
1401
1402         old_value = gtk_adjustment_get_value(vadj);
1403         if (!up) {
1404                 upper = gtk_adjustment_get_upper(vadj) - gtk_adjustment_get_page_size(vadj);
1405                 if (old_value < upper) {
1406                         old_value += page_incr;
1407                         old_value = MIN(old_value, upper);
1408                         gtk_adjustment_set_value(vadj, old_value);
1409                         g_signal_emit_by_name(G_OBJECT(vadj),
1410                                               "value_changed", 0);
1411                 } else
1412                         return FALSE;
1413         } else {
1414                 if (old_value > 0.0) {
1415                         old_value -= page_incr;
1416                         old_value = MAX(old_value, 0.0);
1417                         gtk_adjustment_set_value(vadj, old_value);
1418                         g_signal_emit_by_name(G_OBJECT(vadj),
1419                                               "value_changed", 0);
1420                 } else
1421                         return FALSE;
1422         }
1423         return TRUE;
1424 }
1425
1426 static void gtkutils_smooth_scroll_one_line(GtkWidget *widget, GtkAdjustment *vadj, gboolean up)
1427 {
1428         gfloat upper;
1429         gfloat old_value;
1430         gfloat last_value;
1431
1432         old_value = gtk_adjustment_get_value(vadj);
1433         if (!up) {
1434                 upper = gtk_adjustment_get_upper(vadj) - gtk_adjustment_get_page_size(vadj);
1435                 if (old_value < upper) {
1436                         last_value = old_value + gtk_adjustment_get_step_increment(vadj);
1437                         last_value = MIN(last_value, upper);
1438
1439                         gtkutils_smooth_scroll_do(widget, vadj, old_value,
1440                                                   last_value,
1441                                                   prefs_common.scroll_step);
1442                 }
1443         } else {
1444                 if (old_value > 0.0) {
1445                         last_value = old_value - gtk_adjustment_get_step_increment(vadj);
1446                         last_value = MAX(last_value, 0.0);
1447
1448                         gtkutils_smooth_scroll_do(widget, vadj, old_value,
1449                                                   last_value,
1450                                                   prefs_common.scroll_step);
1451                 }
1452         }
1453 }
1454
1455 void gtkutils_scroll_one_line(GtkWidget *widget, GtkAdjustment *vadj, gboolean up)
1456 {
1457         gfloat upper;
1458         gfloat old_value;
1459
1460         if (prefs_common.enable_smooth_scroll) {
1461                 gtkutils_smooth_scroll_one_line(widget, vadj, up);
1462                 return;
1463         }
1464
1465         old_value = gtk_adjustment_get_value(vadj);
1466         if (!up) {
1467                 upper = gtk_adjustment_get_upper(vadj) - gtk_adjustment_get_page_size(vadj);
1468                 if (old_value < upper) {
1469                         old_value += gtk_adjustment_get_step_increment(vadj);
1470                         old_value = MIN(old_value, upper);
1471                         gtk_adjustment_set_value(vadj, old_value);
1472                         g_signal_emit_by_name(G_OBJECT(vadj),
1473                                               "value_changed", 0);
1474                 }
1475         } else {
1476                 if (old_value > 0.0) {
1477                         old_value -= gtk_adjustment_get_step_increment(vadj);
1478                         old_value = MAX(old_value, 0.0);
1479                         gtk_adjustment_set_value(vadj, old_value);
1480                         g_signal_emit_by_name(G_OBJECT(vadj),
1481                                               "value_changed", 0);
1482                 }
1483         }
1484 }
1485
1486 gboolean gtkut_tree_model_text_iter_prev(GtkTreeModel *model,
1487                                  GtkTreeIter *iter,
1488                                  const gchar* text)
1489 /* do the same as gtk_tree_model_iter_next, but _prev instead.
1490    to use with widgets with one text column (gtk_combo_box_new_text()
1491    and with GtkComboBoxEntry's for instance),
1492 */
1493 {
1494         GtkTreeIter cur_iter;
1495         gchar *cur_value;
1496         gboolean valid;
1497         gint count;
1498
1499         cm_return_val_if_fail(model != NULL, FALSE);
1500         cm_return_val_if_fail(iter != NULL, FALSE);
1501
1502         if (text == NULL || *text == '\0')
1503                 return FALSE;
1504
1505         valid = gtk_tree_model_get_iter_first(model, &cur_iter);
1506         count = 0;
1507         while (valid) {
1508                 gtk_tree_model_get(model, &cur_iter, 0, &cur_value, -1);
1509
1510                 if (strcmp(text, cur_value) == 0) {
1511                         if (count <= 0)
1512                                 return FALSE;
1513
1514                         return gtk_tree_model_iter_nth_child(model, iter, NULL, count - 1);
1515                 }
1516
1517                 valid = gtk_tree_model_iter_next(model, &cur_iter);
1518                 count++;
1519         }
1520         return FALSE;           
1521 }
1522
1523 gboolean gtkut_tree_model_get_iter_last(GtkTreeModel *model,
1524                                  GtkTreeIter *iter)
1525 /* do the same as gtk_tree_model_get_iter_first, but _last instead.
1526 */
1527 {
1528         gint count;
1529
1530         cm_return_val_if_fail(model != NULL, FALSE);
1531         cm_return_val_if_fail(iter != NULL, FALSE);
1532
1533         count = gtk_tree_model_iter_n_children(model, NULL);
1534
1535         if (count <= 0)
1536                 return FALSE;
1537
1538         return gtk_tree_model_iter_nth_child(model, iter, NULL, count - 1);
1539 }
1540
1541 GtkWidget *gtkut_window_new             (GtkWindowType   type,
1542                                          const gchar    *class)
1543 {
1544         GtkWidget *window = gtk_window_new(type);
1545         gtk_window_set_role(GTK_WINDOW(window), class);
1546         return window;
1547 }
1548
1549 static gboolean gtkut_tree_iter_comp(GtkTreeModel *model, 
1550                                      GtkTreeIter *iter1, 
1551                                      GtkTreeIter *iter2)
1552 {
1553         GtkTreePath *path1 = gtk_tree_model_get_path(model, iter1);
1554         GtkTreePath *path2 = gtk_tree_model_get_path(model, iter2);
1555         gboolean result;
1556
1557         result = gtk_tree_path_compare(path1, path2) == 0;
1558
1559         gtk_tree_path_free(path1);
1560         gtk_tree_path_free(path2);
1561         
1562         return result;
1563 }
1564
1565 /*!
1566  *\brief        Get selected row number.
1567  */
1568 gint gtkut_list_view_get_selected_row(GtkWidget *list_view)
1569 {
1570         GtkTreeView *view = GTK_TREE_VIEW(list_view);
1571         GtkTreeModel *model = gtk_tree_view_get_model(view);
1572         int n_rows = gtk_tree_model_iter_n_children(model, NULL);
1573         GtkTreeSelection *selection;
1574         GtkTreeIter iter;
1575         int row;
1576
1577         if (n_rows == 0) 
1578                 return -1;
1579         
1580         selection = gtk_tree_view_get_selection(view);
1581         if (!gtk_tree_selection_get_selected(selection, &model, &iter))
1582                 return -1;
1583         
1584         /* get all iterators and compare them... */
1585         for (row = 0; row < n_rows; row++) {
1586                 GtkTreeIter itern;
1587
1588                 if (gtk_tree_model_iter_nth_child(model, &itern, NULL, row)
1589                  && gtkut_tree_iter_comp(model, &iter, &itern))
1590                         return row;
1591         }
1592         
1593         return -1;
1594 }
1595
1596 /*!
1597  *\brief        Select a row by its number.
1598  */
1599 gboolean gtkut_list_view_select_row(GtkWidget *list, gint row)
1600 {
1601         GtkTreeView *list_view = GTK_TREE_VIEW(list);
1602         GtkTreeSelection *selection = gtk_tree_view_get_selection(list_view);
1603         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1604         GtkTreeIter iter;
1605         GtkTreePath *path;
1606
1607         if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
1608                 return FALSE;
1609         
1610         gtk_tree_selection_select_iter(selection, &iter);
1611
1612         path = gtk_tree_model_get_path(model, &iter);
1613         gtk_tree_view_set_cursor(list_view, path, NULL, FALSE);
1614         gtk_tree_path_free(path);
1615         
1616         return TRUE;
1617 }
1618
1619 static GtkUIManager *gui_manager = NULL;
1620
1621 GtkUIManager *gtkut_create_ui_manager(void)
1622 {
1623         cm_return_val_if_fail(gui_manager == NULL, gui_manager);
1624         return (gui_manager = gtk_ui_manager_new());
1625 }
1626
1627 GtkUIManager *gtkut_ui_manager(void)
1628 {
1629         return gui_manager;
1630 }
1631
1632 typedef struct _ClawsIOClosure ClawsIOClosure;
1633
1634 struct _ClawsIOClosure
1635 {
1636   ClawsIOFunc function;
1637   GIOCondition condition;
1638   GDestroyNotify notify;
1639   gpointer data;
1640 };
1641
1642 static gboolean  
1643 claws_io_invoke (GIOChannel   *source,
1644                  GIOCondition  condition,
1645                  gpointer      data)
1646 {
1647   ClawsIOClosure *closure = data;
1648   int fd;
1649 #ifndef G_OS_WIN32
1650   fd = g_io_channel_unix_get_fd (source);
1651 #else
1652   fd = g_io_channel_win32_get_fd (source);
1653 #endif
1654   if (closure->condition & condition)
1655     closure->function (closure->data, fd, condition);
1656
1657   return TRUE;
1658 }
1659
1660 static void
1661 claws_io_destroy (gpointer data)
1662 {
1663   ClawsIOClosure *closure = data;
1664
1665   if (closure->notify)
1666     closure->notify (closure->data);
1667
1668   g_free (closure);
1669 }
1670
1671 gint
1672 claws_input_add    (gint              source,
1673                     GIOCondition      condition,
1674                     ClawsIOFunc       function,
1675                     gpointer          data,
1676                     gboolean          is_sock)
1677 {
1678   guint result;
1679   ClawsIOClosure *closure = g_new (ClawsIOClosure, 1);
1680   GIOChannel *channel;
1681
1682   closure->function = function;
1683   closure->condition = condition;
1684   closure->notify = NULL;
1685   closure->data = data;
1686
1687 #ifndef G_OS_WIN32
1688   channel = g_io_channel_unix_new (source);
1689 #else
1690   if (is_sock)
1691     channel = g_io_channel_win32_new_socket(source);
1692   else
1693     channel = g_io_channel_win32_new_fd(source);
1694 #endif
1695   result = g_io_add_watch_full (channel, G_PRIORITY_DEFAULT, condition, 
1696                                 claws_io_invoke,
1697                                 closure, claws_io_destroy);
1698   g_io_channel_unref (channel);
1699
1700   return result;
1701 }
1702
1703 void gtkut_widget_set_mapped(GtkWidget *widget, gboolean mapped)
1704 {
1705 #if GTK_CHECK_VERSION(2,20,0)
1706         gtk_widget_set_mapped(widget, mapped);
1707 #else
1708         if (mapped)
1709                 GTK_WIDGET_SET_FLAGS(widget, GTK_MAPPED);
1710         else
1711                 GTK_WIDGET_UNSET_FLAGS(widget, GTK_MAPPED);
1712 #endif
1713 }
1714
1715 void gtkut_widget_set_realized(GtkWidget *widget, gboolean realized)
1716 {
1717 #if GTK_CHECK_VERSION(2,20,0)
1718         gtk_widget_set_realized(widget, realized);
1719 #else
1720         if (realized)
1721                 GTK_WIDGET_SET_FLAGS(widget, GTK_REALIZED);
1722         else
1723                 GTK_WIDGET_UNSET_FLAGS(widget, GTK_REALIZED);
1724 #endif
1725 }
1726
1727 void gtkut_widget_set_can_default(GtkWidget *widget, gboolean can_default)
1728 {
1729 #if GTK_CHECK_VERSION(2,20,0)
1730         gtk_widget_set_can_default(widget, can_default);
1731 #else
1732         if (can_default)
1733                 GTK_WIDGET_SET_FLAGS(widget, GTK_CAN_DEFAULT);
1734         else
1735                 GTK_WIDGET_UNSET_FLAGS(widget, GTK_CAN_DEFAULT);
1736 #endif
1737 }
1738
1739 void gtkut_widget_set_receives_default(GtkWidget *widget, gboolean receives_default)
1740 {
1741 #if GTK_CHECK_VERSION(2,20,0)
1742         gtk_widget_set_receives_default(widget, receives_default);
1743 #else
1744         if (receives_default)
1745                 GTK_WIDGET_SET_FLAGS(widget, GTK_RECEIVES_DEFAULT);
1746         else
1747                 GTK_WIDGET_UNSET_FLAGS(widget, GTK_RECEIVES_DEFAULT);
1748 #endif
1749 }
1750
1751 void gtkut_widget_set_can_focus(GtkWidget *widget, gboolean can_focus)
1752 {
1753 #if GTK_CHECK_VERSION(2,20,0)
1754         gtk_widget_set_can_focus(widget, can_focus);
1755 #else
1756         if (can_focus)
1757                 GTK_WIDGET_SET_FLAGS(widget, GTK_CAN_FOCUS);
1758         else
1759                 GTK_WIDGET_UNSET_FLAGS(widget, GTK_CAN_FOCUS);
1760 #endif
1761 }
1762
1763 void gtkut_widget_set_has_window(GtkWidget *widget, gboolean has_window)
1764 {
1765 #if GTK_CHECK_VERSION(2,20,0)
1766         gtk_widget_set_has_window(widget, has_window);
1767 #else
1768         if (has_window) /* Inverted logic there */
1769                 GTK_WIDGET_UNSET_FLAGS(widget, GTK_NO_WINDOW);
1770         else
1771                 GTK_WIDGET_SET_FLAGS(widget, GTK_NO_WINDOW);
1772 #endif
1773 }
1774
1775 /**
1776  * Load a pixbuf fitting inside the specified size. EXIF orientation is
1777  * respected if available.
1778  *
1779  * @param[in] filename          the file to load
1780  * @param[in] box_width         the max width (-1 for no resize)
1781  * @param[in] box_height        the max height (-1 for no resize)
1782  * @param[out] error            the possible load error
1783  *
1784  * @return a GdkPixbuf
1785  */
1786 GdkPixbuf *claws_load_pixbuf_fitting(GdkPixbuf *src_pixbuf, int box_width,
1787                                      int box_height)
1788 {
1789         gint w, h, orientation, angle;
1790         gint avail_width, avail_height;
1791         gboolean flip_horiz, flip_vert;
1792         const gchar *orient_str;
1793         GdkPixbuf *pixbuf, *t_pixbuf;
1794
1795         pixbuf = src_pixbuf;
1796
1797         if (pixbuf == NULL)
1798                 return NULL;
1799
1800         angle = 0;
1801         flip_horiz = flip_vert = FALSE;
1802
1803         /* EXIF orientation */
1804         orient_str = gdk_pixbuf_get_option(pixbuf, "orientation");
1805         if (orient_str != NULL && *orient_str != '\0') {
1806                 orientation = atoi(orient_str);
1807                 switch(orientation) {
1808                         /* See EXIF standard for different values */
1809                         case 1: break;
1810                         case 2: flip_horiz = 1;
1811                                 break;
1812                         case 3: angle = 180;
1813                                 break;
1814                         case 4: flip_vert = 1;
1815                                 break;
1816                         case 5: angle = 90;
1817                                 flip_horiz = 1;
1818                                 break;
1819                         case 6: angle = 270;
1820                                 break;
1821                         case 7: angle = 90;
1822                                 flip_vert = 1;
1823                                 break;
1824                         case 8: angle = 90;
1825                                 break;
1826                 }
1827         }
1828
1829         w = gdk_pixbuf_get_width(pixbuf);
1830         h = gdk_pixbuf_get_height(pixbuf);
1831
1832         if (angle == 90 || angle == 270) {
1833                 avail_height = box_width;
1834                 avail_width = box_height;
1835         } else {
1836                 avail_width = box_width;
1837                 avail_height = box_height;
1838         }
1839
1840         /* Scale first */
1841         if (box_width != -1 && box_height != -1 && avail_width - 100 > 0) {
1842                 if (w > avail_width) {
1843                         h = (avail_width * h) / w;
1844                         w = avail_width;
1845                 }
1846                 if (h > avail_height) {
1847                         w = (avail_height * w) / h;
1848                         h = avail_height;
1849                 }
1850                 t_pixbuf = gdk_pixbuf_scale_simple(pixbuf, 
1851                         w, h, GDK_INTERP_BILINEAR);
1852                 g_object_unref(pixbuf);
1853                 pixbuf = t_pixbuf;
1854         }
1855
1856         /* Rotate if needed */
1857         if (angle != 0) {
1858                 t_pixbuf = gdk_pixbuf_rotate_simple(pixbuf, angle);
1859                 g_object_unref(pixbuf);
1860                 pixbuf = t_pixbuf;
1861         }
1862
1863         /* Flip horizontally if needed */
1864         if (flip_horiz) {
1865                 t_pixbuf = gdk_pixbuf_flip(pixbuf, TRUE);
1866                 g_object_unref(pixbuf);
1867                 pixbuf = t_pixbuf;
1868         }
1869
1870         /* Flip vertically if needed */
1871         if (flip_vert) {
1872                 t_pixbuf = gdk_pixbuf_flip(pixbuf, FALSE);
1873                 g_object_unref(pixbuf);
1874                 pixbuf = t_pixbuf;
1875         }
1876
1877         return pixbuf;
1878 }
1879
1880 #if (defined USE_GNUTLS && GLIB_CHECK_VERSION(2,22,0))
1881 static void auto_configure_done(const gchar *hostname, gint port, gboolean ssl, AutoConfigureData *data)
1882 {
1883         gboolean smtp = strcmp(data->tls_service, "submission") == 0 ? TRUE : FALSE;
1884
1885         if (hostname != NULL) {
1886                 if (data->hostname_entry)
1887                         gtk_entry_set_text(data->hostname_entry, hostname);
1888                 if (data->set_port)
1889                         gtk_toggle_button_set_active(data->set_port,
1890                                 (ssl && port != data->default_ssl_port) || (!ssl && port != data->default_port));
1891                 if (data->port)
1892                         gtk_spin_button_set_value(data->port, port);
1893                 else if (data->hostname_entry) {
1894                         if ((ssl && port != data->default_ssl_port) || (!ssl && port != data->default_port)) {
1895                                 gchar *tmp = g_strdup_printf("%s:%d", hostname, port);
1896                                 gtk_entry_set_text(data->hostname_entry, tmp);
1897                                 g_free(tmp);
1898                         } else
1899                                 gtk_entry_set_text(data->hostname_entry, hostname);
1900                 }
1901
1902                 if (ssl && data->ssl_checkbtn) {
1903                         gtk_toggle_button_set_active(data->ssl_checkbtn, TRUE);
1904                         gtk_toggle_button_set_active(data->tls_checkbtn, FALSE);
1905                 } else if (data->tls_checkbtn) {
1906                         if (!GTK_IS_RADIO_BUTTON(data->ssl_checkbtn)) {
1907                                 /* Wizard where TLS is [x]SSL + [x]TLS */
1908                                 gtk_toggle_button_set_active(data->ssl_checkbtn, TRUE);
1909                         }
1910
1911                         /* Even though technically this is against the RFCs,
1912                          * if a "_submission._tcp" SRV record uses port 465,
1913                          * it is safe to assume TLS-only service, instead of
1914                          * plaintext + STARTTLS one. */
1915                         if (smtp && port == 465)
1916                                 gtk_toggle_button_set_active(data->ssl_checkbtn, TRUE);
1917                         else
1918                                 gtk_toggle_button_set_active(data->tls_checkbtn, TRUE);
1919                 }
1920
1921                 /* Check authentication by default. This is probably required if
1922                  * auto-configuration worked.
1923                  */
1924                 if (data->auth_checkbtn)
1925                         gtk_toggle_button_set_active(data->auth_checkbtn, TRUE);
1926
1927                 /* Set user ID to full email address, which is used by the
1928                  * majority of providers where auto-configuration works.
1929                  */
1930                 if (data->uid_entry)
1931                         gtk_entry_set_text(data->uid_entry, data->address);
1932
1933                 gtk_label_set_text(data->info_label, _("Done."));
1934         } else {
1935                 gchar *msg;
1936                 switch (data->resolver_error) {
1937                 case G_RESOLVER_ERROR_NOT_FOUND:
1938                         msg = g_strdup(_("Failed: no service record found."));
1939                         break;
1940                 case G_RESOLVER_ERROR_TEMPORARY_FAILURE:
1941                         msg = g_strdup(_("Failed: network error."));
1942                         break;
1943                 default:
1944                         msg = g_strdup_printf(_("Failed: unknown error (%d)."), data->resolver_error);
1945                 }
1946                 gtk_label_set_text(data->info_label, msg);
1947                 g_free(msg);
1948         }
1949         gtk_widget_show(GTK_WIDGET(data->configure_button));
1950         gtk_widget_hide(GTK_WIDGET(data->cancel_button));
1951         g_free(data->address);
1952         g_free(data);
1953 }
1954
1955 static void resolve_done(GObject *source, GAsyncResult *result, gpointer user_data)
1956 {
1957         AutoConfigureData *data = (AutoConfigureData *)user_data;
1958         GResolver *resolver = (GResolver *)source;
1959         GError *error = NULL;
1960         gchar *hostname = NULL;
1961         guint16 port;
1962         GList *answers, *cur;
1963         gboolean found = FALSE;
1964         gboolean abort = FALSE;
1965
1966         answers = g_resolver_lookup_service_finish(resolver, result, &error);
1967
1968         if (answers) {
1969                 for (cur = g_srv_target_list_sort(answers); cur; cur = cur->next) {
1970                         GSrvTarget *target = (GSrvTarget *)cur->data;
1971                         const gchar *h = g_srv_target_get_hostname(target);
1972                         port = g_srv_target_get_port(target);
1973                         if (h && strcmp(h,"") && port > 0) {
1974                                 hostname = g_strdup(h);
1975                                 found = TRUE;
1976                                 break;
1977                         }
1978                 }
1979                 g_resolver_free_targets(answers);
1980         } else if (error) {
1981                 if (error->code == G_IO_ERROR_CANCELLED)
1982                         abort = TRUE;
1983                 else
1984                         data->resolver_error = error->code;
1985                 debug_print("error %s\n", error->message);
1986                 g_error_free(error);
1987         }
1988
1989         if (found) {
1990                 auto_configure_done(hostname, port, data->ssl_service != NULL, data);
1991         } else if (data->ssl_service && !abort) {
1992                 /* Fallback to TLS */
1993                 data->ssl_service = NULL;
1994                 auto_configure_service(data);
1995         } else {
1996                 auto_configure_done(NULL, 0, FALSE, data);
1997         }
1998         g_free(hostname);
1999         g_object_unref(resolver);
2000 }
2001
2002 void auto_configure_service(AutoConfigureData *data)
2003 {
2004         GResolver *resolver;
2005         const gchar *cur_service = data->ssl_service != NULL ? data->ssl_service : data->tls_service;
2006
2007         cm_return_if_fail(cur_service != NULL);
2008         cm_return_if_fail(data->address != NULL);
2009
2010         resolver = g_resolver_get_default();
2011         if (resolver != NULL) {
2012                 const gchar *domain = strchr(data->address, '@') + 1;
2013
2014                 gtk_label_set_text(data->info_label, _("Configuring..."));
2015                 gtk_widget_hide(GTK_WIDGET(data->configure_button));
2016                 gtk_widget_show(GTK_WIDGET(data->cancel_button));
2017                 g_resolver_lookup_service_async(resolver, cur_service, "tcp", domain,
2018                                         data->cancel, resolve_done, data);
2019         }
2020 }
2021
2022 gboolean auto_configure_service_sync(const gchar *service, const gchar *domain, gchar **srvhost, guint16 *srvport)
2023 {
2024         GResolver *resolver;
2025         GList *answers, *cur;
2026         GError *error = NULL;
2027         gboolean result = FALSE;
2028
2029         cm_return_val_if_fail(service != NULL, FALSE);
2030         cm_return_val_if_fail(domain != NULL, FALSE);
2031
2032         resolver = g_resolver_get_default();
2033         if (resolver == NULL)
2034                 return FALSE;
2035
2036         answers = g_resolver_lookup_service(resolver, service, "tcp", domain, NULL, &error);
2037
2038         *srvhost = NULL;
2039         *srvport = 0;
2040
2041         if (answers) {
2042                 for (cur = g_srv_target_list_sort(answers); cur; cur = cur->next) {
2043                         GSrvTarget *target = (GSrvTarget *)cur->data;
2044                         const gchar *hostname = g_srv_target_get_hostname(target);
2045                         guint16 port = g_srv_target_get_port(target);
2046                         if (hostname && strcmp(hostname,"") && port > 0) {
2047                                 result = TRUE;
2048                                 *srvhost = g_strdup(hostname);
2049                                 *srvport = port;
2050                                 break;
2051                         }
2052                 }
2053                 g_resolver_free_targets(answers);
2054         } else if (error) {
2055                 g_error_free(error);
2056         }
2057
2058         g_object_unref(resolver);
2059         return result;
2060 }
2061 #endif