Code cleanup around glib version check (2.28 minimum).
[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         gtk_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         gtk_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                 gtk_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                 gtk_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         gtk_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                 gtk_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                 gtk_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         g_object_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                 g_object_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_text_new()
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                         g_free(cur_value);
1512                         if (count <= 0)
1513                                 return FALSE;
1514
1515                         return gtk_tree_model_iter_nth_child(model, iter, NULL, count - 1);
1516                 }
1517
1518                 g_free(cur_value);
1519                 valid = gtk_tree_model_iter_next(model, &cur_iter);
1520                 count++;
1521         }
1522         return FALSE;           
1523 }
1524
1525 gboolean gtkut_tree_model_get_iter_last(GtkTreeModel *model,
1526                                  GtkTreeIter *iter)
1527 /* do the same as gtk_tree_model_get_iter_first, but _last instead.
1528 */
1529 {
1530         gint count;
1531
1532         cm_return_val_if_fail(model != NULL, FALSE);
1533         cm_return_val_if_fail(iter != NULL, FALSE);
1534
1535         count = gtk_tree_model_iter_n_children(model, NULL);
1536
1537         if (count <= 0)
1538                 return FALSE;
1539
1540         return gtk_tree_model_iter_nth_child(model, iter, NULL, count - 1);
1541 }
1542
1543 GtkWidget *gtkut_window_new             (GtkWindowType   type,
1544                                          const gchar    *class)
1545 {
1546         GtkWidget *window = gtk_window_new(type);
1547         gtk_window_set_role(GTK_WINDOW(window), class);
1548         return window;
1549 }
1550
1551 static gboolean gtkut_tree_iter_comp(GtkTreeModel *model, 
1552                                      GtkTreeIter *iter1, 
1553                                      GtkTreeIter *iter2)
1554 {
1555         GtkTreePath *path1 = gtk_tree_model_get_path(model, iter1);
1556         GtkTreePath *path2 = gtk_tree_model_get_path(model, iter2);
1557         gboolean result;
1558
1559         result = gtk_tree_path_compare(path1, path2) == 0;
1560
1561         gtk_tree_path_free(path1);
1562         gtk_tree_path_free(path2);
1563         
1564         return result;
1565 }
1566
1567 /*!
1568  *\brief        Get selected row number.
1569  */
1570 gint gtkut_list_view_get_selected_row(GtkWidget *list_view)
1571 {
1572         GtkTreeView *view = GTK_TREE_VIEW(list_view);
1573         GtkTreeModel *model = gtk_tree_view_get_model(view);
1574         int n_rows = gtk_tree_model_iter_n_children(model, NULL);
1575         GtkTreeSelection *selection;
1576         GtkTreeIter iter;
1577         int row;
1578
1579         if (n_rows == 0) 
1580                 return -1;
1581         
1582         selection = gtk_tree_view_get_selection(view);
1583         if (!gtk_tree_selection_get_selected(selection, &model, &iter))
1584                 return -1;
1585         
1586         /* get all iterators and compare them... */
1587         for (row = 0; row < n_rows; row++) {
1588                 GtkTreeIter itern;
1589
1590                 if (gtk_tree_model_iter_nth_child(model, &itern, NULL, row)
1591                  && gtkut_tree_iter_comp(model, &iter, &itern))
1592                         return row;
1593         }
1594         
1595         return -1;
1596 }
1597
1598 /*!
1599  *\brief        Select a row by its number.
1600  */
1601 gboolean gtkut_list_view_select_row(GtkWidget *list, gint row)
1602 {
1603         GtkTreeView *list_view = GTK_TREE_VIEW(list);
1604         GtkTreeSelection *selection = gtk_tree_view_get_selection(list_view);
1605         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1606         GtkTreeIter iter;
1607         GtkTreePath *path;
1608
1609         if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
1610                 return FALSE;
1611         
1612         gtk_tree_selection_select_iter(selection, &iter);
1613
1614         path = gtk_tree_model_get_path(model, &iter);
1615         gtk_tree_view_set_cursor(list_view, path, NULL, FALSE);
1616         gtk_tree_path_free(path);
1617         
1618         return TRUE;
1619 }
1620
1621 static GtkUIManager *gui_manager = NULL;
1622
1623 GtkUIManager *gtkut_create_ui_manager(void)
1624 {
1625         cm_return_val_if_fail(gui_manager == NULL, gui_manager);
1626         return (gui_manager = gtk_ui_manager_new());
1627 }
1628
1629 GtkUIManager *gtkut_ui_manager(void)
1630 {
1631         return gui_manager;
1632 }
1633
1634 typedef struct _ClawsIOClosure ClawsIOClosure;
1635
1636 struct _ClawsIOClosure
1637 {
1638   ClawsIOFunc function;
1639   GIOCondition condition;
1640   GDestroyNotify notify;
1641   gpointer data;
1642 };
1643
1644 static gboolean  
1645 claws_io_invoke (GIOChannel   *source,
1646                  GIOCondition  condition,
1647                  gpointer      data)
1648 {
1649   ClawsIOClosure *closure = data;
1650   int fd;
1651 #ifndef G_OS_WIN32
1652   fd = g_io_channel_unix_get_fd (source);
1653 #else
1654   fd = g_io_channel_win32_get_fd (source);
1655 #endif
1656   if (closure->condition & condition)
1657     closure->function (closure->data, fd, condition);
1658
1659   return TRUE;
1660 }
1661
1662 static void
1663 claws_io_destroy (gpointer data)
1664 {
1665   ClawsIOClosure *closure = data;
1666
1667   if (closure->notify)
1668     closure->notify (closure->data);
1669
1670   g_free (closure);
1671 }
1672
1673 gint
1674 claws_input_add    (gint              source,
1675                     GIOCondition      condition,
1676                     ClawsIOFunc       function,
1677                     gpointer          data,
1678                     gboolean          is_sock)
1679 {
1680   guint result;
1681   ClawsIOClosure *closure = g_new (ClawsIOClosure, 1);
1682   GIOChannel *channel;
1683
1684   closure->function = function;
1685   closure->condition = condition;
1686   closure->notify = NULL;
1687   closure->data = data;
1688
1689 #ifndef G_OS_WIN32
1690   channel = g_io_channel_unix_new (source);
1691 #else
1692   if (is_sock)
1693     channel = g_io_channel_win32_new_socket(source);
1694   else
1695     channel = g_io_channel_win32_new_fd(source);
1696 #endif
1697   result = g_io_add_watch_full (channel, G_PRIORITY_DEFAULT, condition, 
1698                                 claws_io_invoke,
1699                                 closure, claws_io_destroy);
1700   g_io_channel_unref (channel);
1701
1702   return result;
1703 }
1704
1705 /**
1706  * Load a pixbuf fitting inside the specified size. EXIF orientation is
1707  * respected if available.
1708  *
1709  * @param[in] filename          the file to load
1710  * @param[in] box_width         the max width (-1 for no resize)
1711  * @param[in] box_height        the max height (-1 for no resize)
1712  * @param[out] error            the possible load error
1713  *
1714  * @return a GdkPixbuf
1715  */
1716 GdkPixbuf *claws_load_pixbuf_fitting(GdkPixbuf *src_pixbuf, int box_width,
1717                                      int box_height)
1718 {
1719         gint w, h, orientation, angle;
1720         gint avail_width, avail_height;
1721         gboolean flip_horiz, flip_vert;
1722         const gchar *orient_str;
1723         GdkPixbuf *pixbuf, *t_pixbuf;
1724
1725         pixbuf = src_pixbuf;
1726
1727         if (pixbuf == NULL)
1728                 return NULL;
1729
1730         angle = 0;
1731         flip_horiz = flip_vert = FALSE;
1732
1733         /* EXIF orientation */
1734         orient_str = gdk_pixbuf_get_option(pixbuf, "orientation");
1735         if (orient_str != NULL && *orient_str != '\0') {
1736                 orientation = atoi(orient_str);
1737                 switch(orientation) {
1738                         /* See EXIF standard for different values */
1739                         case 1: break;
1740                         case 2: flip_horiz = 1;
1741                                 break;
1742                         case 3: angle = 180;
1743                                 break;
1744                         case 4: flip_vert = 1;
1745                                 break;
1746                         case 5: angle = 90;
1747                                 flip_horiz = 1;
1748                                 break;
1749                         case 6: angle = 270;
1750                                 break;
1751                         case 7: angle = 90;
1752                                 flip_vert = 1;
1753                                 break;
1754                         case 8: angle = 90;
1755                                 break;
1756                 }
1757         }
1758
1759         w = gdk_pixbuf_get_width(pixbuf);
1760         h = gdk_pixbuf_get_height(pixbuf);
1761
1762         if (angle == 90 || angle == 270) {
1763                 avail_height = box_width;
1764                 avail_width = box_height;
1765         } else {
1766                 avail_width = box_width;
1767                 avail_height = box_height;
1768         }
1769
1770         /* Scale first */
1771         if (box_width != -1 && box_height != -1 && avail_width - 100 > 0) {
1772                 if (w > avail_width) {
1773                         h = (avail_width * h) / w;
1774                         w = avail_width;
1775                 }
1776                 if (h > avail_height) {
1777                         w = (avail_height * w) / h;
1778                         h = avail_height;
1779                 }
1780                 t_pixbuf = gdk_pixbuf_scale_simple(pixbuf, 
1781                         w, h, GDK_INTERP_BILINEAR);
1782                 g_object_unref(pixbuf);
1783                 pixbuf = t_pixbuf;
1784         }
1785
1786         /* Rotate if needed */
1787         if (angle != 0) {
1788                 t_pixbuf = gdk_pixbuf_rotate_simple(pixbuf, angle);
1789                 g_object_unref(pixbuf);
1790                 pixbuf = t_pixbuf;
1791         }
1792
1793         /* Flip horizontally if needed */
1794         if (flip_horiz) {
1795                 t_pixbuf = gdk_pixbuf_flip(pixbuf, TRUE);
1796                 g_object_unref(pixbuf);
1797                 pixbuf = t_pixbuf;
1798         }
1799
1800         /* Flip vertically if needed */
1801         if (flip_vert) {
1802                 t_pixbuf = gdk_pixbuf_flip(pixbuf, FALSE);
1803                 g_object_unref(pixbuf);
1804                 pixbuf = t_pixbuf;
1805         }
1806
1807         return pixbuf;
1808 }
1809
1810 #if defined USE_GNUTLS
1811 static void auto_configure_done(const gchar *hostname, gint port, gboolean ssl, AutoConfigureData *data)
1812 {
1813         gboolean smtp = strcmp(data->tls_service, "submission") == 0 ? TRUE : FALSE;
1814
1815         if (hostname != NULL) {
1816                 if (data->hostname_entry)
1817                         gtk_entry_set_text(data->hostname_entry, hostname);
1818                 if (data->set_port)
1819                         gtk_toggle_button_set_active(data->set_port,
1820                                 (ssl && port != data->default_ssl_port) || (!ssl && port != data->default_port));
1821                 if (data->port)
1822                         gtk_spin_button_set_value(data->port, port);
1823                 else if (data->hostname_entry) {
1824                         if ((ssl && port != data->default_ssl_port) || (!ssl && port != data->default_port)) {
1825                                 gchar *tmp = g_strdup_printf("%s:%d", hostname, port);
1826                                 gtk_entry_set_text(data->hostname_entry, tmp);
1827                                 g_free(tmp);
1828                         } else
1829                                 gtk_entry_set_text(data->hostname_entry, hostname);
1830                 }
1831
1832                 if (ssl && data->ssl_checkbtn) {
1833                         gtk_toggle_button_set_active(data->ssl_checkbtn, TRUE);
1834                         gtk_toggle_button_set_active(data->tls_checkbtn, FALSE);
1835                 } else if (data->tls_checkbtn) {
1836                         if (!GTK_IS_RADIO_BUTTON(data->ssl_checkbtn)) {
1837                                 /* Wizard where TLS is [x]SSL + [x]TLS */
1838                                 gtk_toggle_button_set_active(data->ssl_checkbtn, TRUE);
1839                         }
1840
1841                         /* Even though technically this is against the RFCs,
1842                          * if a "_submission._tcp" SRV record uses port 465,
1843                          * it is safe to assume TLS-only service, instead of
1844                          * plaintext + STARTTLS one. */
1845                         if (smtp && port == 465)
1846                                 gtk_toggle_button_set_active(data->ssl_checkbtn, TRUE);
1847                         else
1848                                 gtk_toggle_button_set_active(data->tls_checkbtn, TRUE);
1849                 }
1850
1851                 /* Check authentication by default. This is probably required if
1852                  * auto-configuration worked.
1853                  */
1854                 if (data->auth_checkbtn)
1855                         gtk_toggle_button_set_active(data->auth_checkbtn, TRUE);
1856
1857                 /* Set user ID to full email address, which is used by the
1858                  * majority of providers where auto-configuration works.
1859                  */
1860                 if (data->uid_entry)
1861                         gtk_entry_set_text(data->uid_entry, data->address);
1862
1863                 gtk_label_set_text(data->info_label, _("Done."));
1864         } else {
1865                 gchar *msg;
1866                 switch (data->resolver_error) {
1867                 case G_RESOLVER_ERROR_NOT_FOUND:
1868                         msg = g_strdup(_("Failed: no service record found."));
1869                         break;
1870                 case G_RESOLVER_ERROR_TEMPORARY_FAILURE:
1871                         msg = g_strdup(_("Failed: network error."));
1872                         break;
1873                 default:
1874                         msg = g_strdup_printf(_("Failed: unknown error (%d)."), data->resolver_error);
1875                 }
1876                 gtk_label_set_text(data->info_label, msg);
1877                 g_free(msg);
1878         }
1879         gtk_widget_show(GTK_WIDGET(data->configure_button));
1880         gtk_widget_hide(GTK_WIDGET(data->cancel_button));
1881         g_free(data->address);
1882         g_free(data);
1883 }
1884
1885 static void resolve_done(GObject *source, GAsyncResult *result, gpointer user_data)
1886 {
1887         AutoConfigureData *data = (AutoConfigureData *)user_data;
1888         GResolver *resolver = (GResolver *)source;
1889         GError *error = NULL;
1890         gchar *hostname = NULL;
1891         guint16 port;
1892         GList *answers, *cur;
1893         gboolean found = FALSE;
1894         gboolean abort = FALSE;
1895
1896         answers = g_resolver_lookup_service_finish(resolver, result, &error);
1897
1898         if (answers) {
1899                 for (cur = g_srv_target_list_sort(answers); cur; cur = cur->next) {
1900                         GSrvTarget *target = (GSrvTarget *)cur->data;
1901                         const gchar *h = g_srv_target_get_hostname(target);
1902                         port = g_srv_target_get_port(target);
1903                         if (h && strcmp(h,"") && port > 0) {
1904                                 hostname = g_strdup(h);
1905                                 found = TRUE;
1906                                 break;
1907                         }
1908                 }
1909                 g_resolver_free_targets(answers);
1910         } else if (error) {
1911                 if (error->code == G_IO_ERROR_CANCELLED)
1912                         abort = TRUE;
1913                 else
1914                         data->resolver_error = error->code;
1915                 debug_print("error %s\n", error->message);
1916                 g_error_free(error);
1917         }
1918
1919         if (found) {
1920                 auto_configure_done(hostname, port, data->ssl_service != NULL, data);
1921         } else if (data->ssl_service && !abort) {
1922                 /* Fallback to TLS */
1923                 data->ssl_service = NULL;
1924                 auto_configure_service(data);
1925         } else {
1926                 auto_configure_done(NULL, 0, FALSE, data);
1927         }
1928         g_free(hostname);
1929         g_object_unref(resolver);
1930 }
1931
1932 void auto_configure_service(AutoConfigureData *data)
1933 {
1934         GResolver *resolver;
1935         const gchar *cur_service = data->ssl_service != NULL ? data->ssl_service : data->tls_service;
1936
1937         cm_return_if_fail(cur_service != NULL);
1938         cm_return_if_fail(data->address != NULL);
1939
1940         resolver = g_resolver_get_default();
1941         if (resolver != NULL) {
1942                 const gchar *domain = strchr(data->address, '@') + 1;
1943
1944                 gtk_label_set_text(data->info_label, _("Configuring..."));
1945                 gtk_widget_hide(GTK_WIDGET(data->configure_button));
1946                 gtk_widget_show(GTK_WIDGET(data->cancel_button));
1947                 g_resolver_lookup_service_async(resolver, cur_service, "tcp", domain,
1948                                         data->cancel, resolve_done, data);
1949         }
1950 }
1951
1952 gboolean auto_configure_service_sync(const gchar *service, const gchar *domain, gchar **srvhost, guint16 *srvport)
1953 {
1954         GResolver *resolver;
1955         GList *answers, *cur;
1956         GError *error = NULL;
1957         gboolean result = FALSE;
1958
1959         cm_return_val_if_fail(service != NULL, FALSE);
1960         cm_return_val_if_fail(domain != NULL, FALSE);
1961
1962         resolver = g_resolver_get_default();
1963         if (resolver == NULL)
1964                 return FALSE;
1965
1966         answers = g_resolver_lookup_service(resolver, service, "tcp", domain, NULL, &error);
1967
1968         *srvhost = NULL;
1969         *srvport = 0;
1970
1971         if (answers) {
1972                 for (cur = g_srv_target_list_sort(answers); cur; cur = cur->next) {
1973                         GSrvTarget *target = (GSrvTarget *)cur->data;
1974                         const gchar *hostname = g_srv_target_get_hostname(target);
1975                         guint16 port = g_srv_target_get_port(target);
1976                         if (hostname && strcmp(hostname,"") && port > 0) {
1977                                 result = TRUE;
1978                                 *srvhost = g_strdup(hostname);
1979                                 *srvport = port;
1980                                 break;
1981                         }
1982                 }
1983                 g_resolver_free_targets(answers);
1984         } else if (error) {
1985                 g_error_free(error);
1986         }
1987
1988         g_object_unref(resolver);
1989         return result;
1990 }
1991 #endif
1992
1993 gpointer gtkut_tree_view_get_selected_pointer(GtkTreeView *view,
1994                 gint column, GtkTreeModel **_model, GtkTreeSelection **_selection,
1995                 GtkTreeIter *_iter)
1996 {
1997         GtkTreeIter iter;
1998         GtkTreeModel *model;
1999         GtkTreeSelection *sel;
2000         gpointer ptr;
2001         GType type;
2002
2003         cm_return_val_if_fail(view != NULL, NULL);
2004         cm_return_val_if_fail(column >= 0, NULL);
2005
2006         model = gtk_tree_view_get_model(view);
2007         if (_model != NULL)
2008                 *_model = model;
2009
2010         sel = gtk_tree_view_get_selection(view);
2011         if (_selection != NULL)
2012                 *_selection = sel;
2013
2014         if (!gtk_tree_selection_get_selected(sel, NULL, &iter))
2015                 return NULL; /* No row selected */
2016
2017         if (_iter != NULL)
2018                 *_iter = iter;
2019
2020         if (gtk_tree_selection_count_selected_rows(sel) > 1)
2021                 return NULL; /* Can't work with multiselect */
2022
2023         cm_return_val_if_fail(
2024                         gtk_tree_model_get_n_columns(model) > column,
2025                         NULL);
2026
2027         type = gtk_tree_model_get_column_type(model, column);
2028         cm_return_val_if_fail(
2029                         type == G_TYPE_POINTER || type == G_TYPE_STRING,
2030                         NULL);
2031
2032         gtk_tree_model_get(model, &iter, column, &ptr, -1);
2033
2034         return ptr;
2035 }