7d1e244c8581e4eeb5cb445a81f568ddf6f0641c
[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         GdkColormap *cmap;
1228         gboolean success[2];
1229         GdkColor uri_color[2] = {{0, 0, 0, 0xffff}, {0, 0xffff, 0, 0}};
1230         gchar *local_url = NULL;
1231         if (!url)
1232                 return NULL;
1233
1234         gtkut_convert_int_to_gdk_color(prefs_common.color[COL_URI],
1235                                                &uri_color[0]);
1236         gtkut_convert_int_to_gdk_color(prefs_common.color[COL_URI],
1237                                                &uri_color[1]);
1238
1239         btn = gtk_button_new_with_label(label?label:url);
1240         gtk_button_set_relief(GTK_BUTTON(btn), GTK_RELIEF_NONE);
1241         btn_label = gtk_bin_get_child(GTK_BIN((btn)));
1242         cmap = gdk_drawable_get_colormap(gtk_widget_get_window(window));
1243         gdk_colormap_alloc_colors(cmap, uri_color, 2, FALSE, TRUE, success);
1244         if (success[0] == TRUE && success[1] == TRUE) {
1245                 GtkStyle *style;
1246                 gtk_widget_ensure_style(btn_label);
1247                 style = gtk_style_copy
1248                         (gtk_widget_get_style(btn_label));
1249                 style->fg[GTK_STATE_NORMAL]   = uri_color[0];
1250                 style->fg[GTK_STATE_ACTIVE]   = uri_color[1];
1251                 style->fg[GTK_STATE_PRELIGHT] = uri_color[0];
1252                 gtk_widget_set_style(btn_label, style);
1253                 g_object_unref(style);
1254         } else
1255                 g_warning("color allocation failed");
1256
1257         g_signal_connect(G_OBJECT(btn), "enter",
1258                          G_CALLBACK(link_btn_enter), window);
1259         g_signal_connect(G_OBJECT(btn), "leave",
1260                          G_CALLBACK(link_btn_leave), window);
1261         g_signal_connect(G_OBJECT(btn), "pressed",
1262                          G_CALLBACK(link_btn_pressed), window);
1263         g_signal_connect(G_OBJECT(btn), "released",
1264                          G_CALLBACK(link_btn_released), window);
1265                          
1266         local_url = g_strdup(url);
1267         g_signal_connect(G_OBJECT(btn), "clicked",
1268                          G_CALLBACK(link_btn_clicked), local_url);
1269         g_signal_connect(G_OBJECT(btn), "unrealize",
1270                          G_CALLBACK(link_btn_unrealize), local_url);
1271         return btn;
1272 }
1273
1274 static gboolean _combobox_separator_func(GtkTreeModel *model,
1275                 GtkTreeIter *iter, gpointer data)
1276 {
1277         gchar *txt = NULL;
1278
1279         cm_return_val_if_fail(model != NULL, FALSE);
1280
1281         gtk_tree_model_get(model, iter, COMBOBOX_TEXT, &txt, -1);
1282
1283         if( txt == NULL )
1284                 return TRUE;
1285         
1286         g_free(txt);
1287         return FALSE;
1288 }
1289
1290 GtkWidget *gtkut_sc_combobox_create(GtkWidget *eventbox, gboolean focus_on_click)
1291 {
1292         GtkWidget *combobox;
1293         GtkListStore *menu;
1294         GtkCellRenderer *rend;
1295
1296         menu = gtk_list_store_new(3, G_TYPE_STRING, G_TYPE_INT, G_TYPE_BOOLEAN);
1297
1298         combobox = gtk_combo_box_new_with_model(GTK_TREE_MODEL(menu));
1299
1300         rend = gtk_cell_renderer_text_new();
1301         gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combobox), rend, TRUE);
1302         gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combobox), rend,
1303                         "markup", COMBOBOX_TEXT,
1304                         "sensitive", COMBOBOX_SENS,
1305                         NULL);
1306
1307         if( eventbox != NULL )
1308                 gtk_container_add(GTK_CONTAINER(eventbox), combobox);
1309         gtk_combo_box_set_focus_on_click(GTK_COMBO_BOX(combobox), focus_on_click);
1310
1311         gtk_combo_box_set_row_separator_func(GTK_COMBO_BOX(combobox),
1312                         (GtkTreeViewRowSeparatorFunc)_combobox_separator_func, NULL, NULL);
1313
1314         return combobox;
1315 }
1316
1317 static void gtkutils_smooth_scroll_do(GtkWidget *widget, GtkAdjustment *vadj,
1318                                       gfloat old_value, gfloat last_value,
1319                                       gint step)
1320 {
1321         gint change_value;
1322         gboolean up;
1323         gint i;
1324
1325         if (old_value < last_value) {
1326                 change_value = last_value - old_value;
1327                 up = FALSE;
1328         } else {
1329                 change_value = old_value - last_value;
1330                 up = TRUE;
1331         }
1332
1333         for (i = step; i <= change_value; i += step) {
1334                 gtk_adjustment_set_value(vadj, old_value + (up ? -i : i));
1335                 g_signal_emit_by_name(G_OBJECT(vadj),
1336                                       "value_changed", 0);
1337         }
1338
1339         gtk_adjustment_set_value(vadj, last_value);
1340         g_signal_emit_by_name(G_OBJECT(vadj), "value_changed", 0);
1341
1342         gtk_widget_queue_draw(widget);
1343 }
1344
1345 static gboolean gtkutils_smooth_scroll_page(GtkWidget *widget, GtkAdjustment *vadj, gboolean up)
1346 {
1347         gfloat upper;
1348         gfloat page_incr;
1349         gfloat old_value;
1350         gfloat last_value;
1351
1352         page_incr = gtk_adjustment_get_page_increment(vadj);
1353         if (prefs_common.scroll_halfpage)
1354                 page_incr /= 2;
1355
1356         old_value = gtk_adjustment_get_value(vadj);
1357         if (!up) {
1358                 upper = gtk_adjustment_get_upper(vadj) - gtk_adjustment_get_page_size(vadj);
1359                 if (old_value < upper) {
1360                         last_value = old_value + page_incr;
1361                         last_value = MIN(last_value, upper);
1362
1363                         gtkutils_smooth_scroll_do(widget, vadj, old_value,
1364                                                   last_value,
1365                                                   prefs_common.scroll_step);
1366                 } else
1367                         return FALSE;
1368         } else {
1369                 if (old_value > 0.0) {
1370                         last_value = old_value - page_incr;
1371                         last_value = MAX(last_value, 0.0);
1372
1373                         gtkutils_smooth_scroll_do(widget, vadj, old_value,
1374                                                   last_value,
1375                                                   prefs_common.scroll_step);
1376                 } else
1377                         return FALSE;
1378         }
1379
1380         return TRUE;
1381 }
1382
1383 gboolean gtkutils_scroll_page(GtkWidget *widget, GtkAdjustment *vadj, gboolean up)
1384 {
1385         gfloat upper;
1386         gfloat page_incr;
1387         gfloat old_value;
1388
1389         if (prefs_common.enable_smooth_scroll)
1390                 return gtkutils_smooth_scroll_page(widget, vadj, up);
1391
1392         page_incr = gtk_adjustment_get_page_increment(vadj);
1393         if (prefs_common.scroll_halfpage)
1394                 page_incr /= 2;
1395
1396         old_value = gtk_adjustment_get_value(vadj);
1397         if (!up) {
1398                 upper = gtk_adjustment_get_upper(vadj) - gtk_adjustment_get_page_size(vadj);
1399                 if (old_value < upper) {
1400                         old_value += page_incr;
1401                         old_value = MIN(old_value, upper);
1402                         gtk_adjustment_set_value(vadj, old_value);
1403                         g_signal_emit_by_name(G_OBJECT(vadj),
1404                                               "value_changed", 0);
1405                 } else
1406                         return FALSE;
1407         } else {
1408                 if (old_value > 0.0) {
1409                         old_value -= page_incr;
1410                         old_value = MAX(old_value, 0.0);
1411                         gtk_adjustment_set_value(vadj, old_value);
1412                         g_signal_emit_by_name(G_OBJECT(vadj),
1413                                               "value_changed", 0);
1414                 } else
1415                         return FALSE;
1416         }
1417         return TRUE;
1418 }
1419
1420 static void gtkutils_smooth_scroll_one_line(GtkWidget *widget, GtkAdjustment *vadj, gboolean up)
1421 {
1422         gfloat upper;
1423         gfloat old_value;
1424         gfloat last_value;
1425
1426         old_value = gtk_adjustment_get_value(vadj);
1427         if (!up) {
1428                 upper = gtk_adjustment_get_upper(vadj) - gtk_adjustment_get_page_size(vadj);
1429                 if (old_value < upper) {
1430                         last_value = old_value + gtk_adjustment_get_step_increment(vadj);
1431                         last_value = MIN(last_value, upper);
1432
1433                         gtkutils_smooth_scroll_do(widget, vadj, old_value,
1434                                                   last_value,
1435                                                   prefs_common.scroll_step);
1436                 }
1437         } else {
1438                 if (old_value > 0.0) {
1439                         last_value = old_value - gtk_adjustment_get_step_increment(vadj);
1440                         last_value = MAX(last_value, 0.0);
1441
1442                         gtkutils_smooth_scroll_do(widget, vadj, old_value,
1443                                                   last_value,
1444                                                   prefs_common.scroll_step);
1445                 }
1446         }
1447 }
1448
1449 void gtkutils_scroll_one_line(GtkWidget *widget, GtkAdjustment *vadj, gboolean up)
1450 {
1451         gfloat upper;
1452         gfloat old_value;
1453
1454         if (prefs_common.enable_smooth_scroll) {
1455                 gtkutils_smooth_scroll_one_line(widget, vadj, up);
1456                 return;
1457         }
1458
1459         old_value = gtk_adjustment_get_value(vadj);
1460         if (!up) {
1461                 upper = gtk_adjustment_get_upper(vadj) - gtk_adjustment_get_page_size(vadj);
1462                 if (old_value < upper) {
1463                         old_value += gtk_adjustment_get_step_increment(vadj);
1464                         old_value = MIN(old_value, upper);
1465                         gtk_adjustment_set_value(vadj, old_value);
1466                         g_signal_emit_by_name(G_OBJECT(vadj),
1467                                               "value_changed", 0);
1468                 }
1469         } else {
1470                 if (old_value > 0.0) {
1471                         old_value -= gtk_adjustment_get_step_increment(vadj);
1472                         old_value = MAX(old_value, 0.0);
1473                         gtk_adjustment_set_value(vadj, old_value);
1474                         g_signal_emit_by_name(G_OBJECT(vadj),
1475                                               "value_changed", 0);
1476                 }
1477         }
1478 }
1479
1480 gboolean gtkut_tree_model_text_iter_prev(GtkTreeModel *model,
1481                                  GtkTreeIter *iter,
1482                                  const gchar* text)
1483 /* do the same as gtk_tree_model_iter_next, but _prev instead.
1484    to use with widgets with one text column (gtk_combo_box_text_new()
1485    and with GtkComboBoxEntry's for instance),
1486 */
1487 {
1488         GtkTreeIter cur_iter;
1489         gchar *cur_value;
1490         gboolean valid;
1491         gint count;
1492
1493         cm_return_val_if_fail(model != NULL, FALSE);
1494         cm_return_val_if_fail(iter != NULL, FALSE);
1495
1496         if (text == NULL || *text == '\0')
1497                 return FALSE;
1498
1499         valid = gtk_tree_model_get_iter_first(model, &cur_iter);
1500         count = 0;
1501         while (valid) {
1502                 gtk_tree_model_get(model, &cur_iter, 0, &cur_value, -1);
1503
1504                 if (strcmp(text, cur_value) == 0) {
1505                         g_free(cur_value);
1506                         if (count <= 0)
1507                                 return FALSE;
1508
1509                         return gtk_tree_model_iter_nth_child(model, iter, NULL, count - 1);
1510                 }
1511
1512                 g_free(cur_value);
1513                 valid = gtk_tree_model_iter_next(model, &cur_iter);
1514                 count++;
1515         }
1516         return FALSE;           
1517 }
1518
1519 gboolean gtkut_tree_model_get_iter_last(GtkTreeModel *model,
1520                                  GtkTreeIter *iter)
1521 /* do the same as gtk_tree_model_get_iter_first, but _last instead.
1522 */
1523 {
1524         gint count;
1525
1526         cm_return_val_if_fail(model != NULL, FALSE);
1527         cm_return_val_if_fail(iter != NULL, FALSE);
1528
1529         count = gtk_tree_model_iter_n_children(model, NULL);
1530
1531         if (count <= 0)
1532                 return FALSE;
1533
1534         return gtk_tree_model_iter_nth_child(model, iter, NULL, count - 1);
1535 }
1536
1537 GtkWidget *gtkut_window_new             (GtkWindowType   type,
1538                                          const gchar    *class)
1539 {
1540         GtkWidget *window = gtk_window_new(type);
1541         gtk_window_set_role(GTK_WINDOW(window), class);
1542         return window;
1543 }
1544
1545 static gboolean gtkut_tree_iter_comp(GtkTreeModel *model, 
1546                                      GtkTreeIter *iter1, 
1547                                      GtkTreeIter *iter2)
1548 {
1549         GtkTreePath *path1 = gtk_tree_model_get_path(model, iter1);
1550         GtkTreePath *path2 = gtk_tree_model_get_path(model, iter2);
1551         gboolean result;
1552
1553         result = gtk_tree_path_compare(path1, path2) == 0;
1554
1555         gtk_tree_path_free(path1);
1556         gtk_tree_path_free(path2);
1557         
1558         return result;
1559 }
1560
1561 /*!
1562  *\brief        Get selected row number.
1563  */
1564 gint gtkut_list_view_get_selected_row(GtkWidget *list_view)
1565 {
1566         GtkTreeView *view = GTK_TREE_VIEW(list_view);
1567         GtkTreeModel *model = gtk_tree_view_get_model(view);
1568         int n_rows = gtk_tree_model_iter_n_children(model, NULL);
1569         GtkTreeSelection *selection;
1570         GtkTreeIter iter;
1571         int row;
1572
1573         if (n_rows == 0) 
1574                 return -1;
1575         
1576         selection = gtk_tree_view_get_selection(view);
1577         if (!gtk_tree_selection_get_selected(selection, &model, &iter))
1578                 return -1;
1579         
1580         /* get all iterators and compare them... */
1581         for (row = 0; row < n_rows; row++) {
1582                 GtkTreeIter itern;
1583
1584                 if (gtk_tree_model_iter_nth_child(model, &itern, NULL, row)
1585                  && gtkut_tree_iter_comp(model, &iter, &itern))
1586                         return row;
1587         }
1588         
1589         return -1;
1590 }
1591
1592 /*!
1593  *\brief        Select a row by its number.
1594  */
1595 gboolean gtkut_list_view_select_row(GtkWidget *list, gint row)
1596 {
1597         GtkTreeView *list_view = GTK_TREE_VIEW(list);
1598         GtkTreeSelection *selection = gtk_tree_view_get_selection(list_view);
1599         GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1600         GtkTreeIter iter;
1601         GtkTreePath *path;
1602
1603         if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
1604                 return FALSE;
1605         
1606         gtk_tree_selection_select_iter(selection, &iter);
1607
1608         path = gtk_tree_model_get_path(model, &iter);
1609         gtk_tree_view_set_cursor(list_view, path, NULL, FALSE);
1610         gtk_tree_path_free(path);
1611         
1612         return TRUE;
1613 }
1614
1615 static GtkUIManager *gui_manager = NULL;
1616
1617 GtkUIManager *gtkut_create_ui_manager(void)
1618 {
1619         cm_return_val_if_fail(gui_manager == NULL, gui_manager);
1620         return (gui_manager = gtk_ui_manager_new());
1621 }
1622
1623 GtkUIManager *gtkut_ui_manager(void)
1624 {
1625         return gui_manager;
1626 }
1627
1628 typedef struct _ClawsIOClosure ClawsIOClosure;
1629
1630 struct _ClawsIOClosure
1631 {
1632   ClawsIOFunc function;
1633   GIOCondition condition;
1634   GDestroyNotify notify;
1635   gpointer data;
1636 };
1637
1638 static gboolean  
1639 claws_io_invoke (GIOChannel   *source,
1640                  GIOCondition  condition,
1641                  gpointer      data)
1642 {
1643   ClawsIOClosure *closure = data;
1644   int fd;
1645 #ifndef G_OS_WIN32
1646   fd = g_io_channel_unix_get_fd (source);
1647 #else
1648   fd = g_io_channel_win32_get_fd (source);
1649 #endif
1650   if (closure->condition & condition)
1651     closure->function (closure->data, fd, condition);
1652
1653   return TRUE;
1654 }
1655
1656 static void
1657 claws_io_destroy (gpointer data)
1658 {
1659   ClawsIOClosure *closure = data;
1660
1661   if (closure->notify)
1662     closure->notify (closure->data);
1663
1664   g_free (closure);
1665 }
1666
1667 gint
1668 claws_input_add    (gint              source,
1669                     GIOCondition      condition,
1670                     ClawsIOFunc       function,
1671                     gpointer          data,
1672                     gboolean          is_sock)
1673 {
1674   guint result;
1675   ClawsIOClosure *closure = g_new (ClawsIOClosure, 1);
1676   GIOChannel *channel;
1677
1678   closure->function = function;
1679   closure->condition = condition;
1680   closure->notify = NULL;
1681   closure->data = data;
1682
1683 #ifndef G_OS_WIN32
1684   channel = g_io_channel_unix_new (source);
1685 #else
1686   if (is_sock)
1687     channel = g_io_channel_win32_new_socket(source);
1688   else
1689     channel = g_io_channel_win32_new_fd(source);
1690 #endif
1691   result = g_io_add_watch_full (channel, G_PRIORITY_DEFAULT, condition, 
1692                                 claws_io_invoke,
1693                                 closure, claws_io_destroy);
1694   g_io_channel_unref (channel);
1695
1696   return result;
1697 }
1698
1699 /**
1700  * Load a pixbuf fitting inside the specified size. EXIF orientation is
1701  * respected if available.
1702  *
1703  * @param[in] filename          the file to load
1704  * @param[in] box_width         the max width (-1 for no resize)
1705  * @param[in] box_height        the max height (-1 for no resize)
1706  * @param[out] error            the possible load error
1707  *
1708  * @return a GdkPixbuf
1709  */
1710 GdkPixbuf *claws_load_pixbuf_fitting(GdkPixbuf *src_pixbuf, int box_width,
1711                                      int box_height)
1712 {
1713         gint w, h, orientation, angle;
1714         gint avail_width, avail_height;
1715         gboolean flip_horiz, flip_vert;
1716         const gchar *orient_str;
1717         GdkPixbuf *pixbuf, *t_pixbuf;
1718
1719         pixbuf = src_pixbuf;
1720
1721         if (pixbuf == NULL)
1722                 return NULL;
1723
1724         angle = 0;
1725         flip_horiz = flip_vert = FALSE;
1726
1727         /* EXIF orientation */
1728         orient_str = gdk_pixbuf_get_option(pixbuf, "orientation");
1729         if (orient_str != NULL && *orient_str != '\0') {
1730                 orientation = atoi(orient_str);
1731                 switch(orientation) {
1732                         /* See EXIF standard for different values */
1733                         case 1: break;
1734                         case 2: flip_horiz = 1;
1735                                 break;
1736                         case 3: angle = 180;
1737                                 break;
1738                         case 4: flip_vert = 1;
1739                                 break;
1740                         case 5: angle = 90;
1741                                 flip_horiz = 1;
1742                                 break;
1743                         case 6: angle = 270;
1744                                 break;
1745                         case 7: angle = 90;
1746                                 flip_vert = 1;
1747                                 break;
1748                         case 8: angle = 90;
1749                                 break;
1750                 }
1751         }
1752
1753         w = gdk_pixbuf_get_width(pixbuf);
1754         h = gdk_pixbuf_get_height(pixbuf);
1755
1756         if (angle == 90 || angle == 270) {
1757                 avail_height = box_width;
1758                 avail_width = box_height;
1759         } else {
1760                 avail_width = box_width;
1761                 avail_height = box_height;
1762         }
1763
1764         /* Scale first */
1765         if (box_width != -1 && box_height != -1 && avail_width - 100 > 0) {
1766                 if (w > avail_width) {
1767                         h = (avail_width * h) / w;
1768                         w = avail_width;
1769                 }
1770                 if (h > avail_height) {
1771                         w = (avail_height * w) / h;
1772                         h = avail_height;
1773                 }
1774                 t_pixbuf = gdk_pixbuf_scale_simple(pixbuf, 
1775                         w, h, GDK_INTERP_BILINEAR);
1776                 g_object_unref(pixbuf);
1777                 pixbuf = t_pixbuf;
1778         }
1779
1780         /* Rotate if needed */
1781         if (angle != 0) {
1782                 t_pixbuf = gdk_pixbuf_rotate_simple(pixbuf, angle);
1783                 g_object_unref(pixbuf);
1784                 pixbuf = t_pixbuf;
1785         }
1786
1787         /* Flip horizontally if needed */
1788         if (flip_horiz) {
1789                 t_pixbuf = gdk_pixbuf_flip(pixbuf, TRUE);
1790                 g_object_unref(pixbuf);
1791                 pixbuf = t_pixbuf;
1792         }
1793
1794         /* Flip vertically if needed */
1795         if (flip_vert) {
1796                 t_pixbuf = gdk_pixbuf_flip(pixbuf, FALSE);
1797                 g_object_unref(pixbuf);
1798                 pixbuf = t_pixbuf;
1799         }
1800
1801         return pixbuf;
1802 }
1803
1804 #if defined USE_GNUTLS
1805 static void auto_configure_done(const gchar *hostname, gint port, gboolean ssl, AutoConfigureData *data)
1806 {
1807         gboolean smtp = strcmp(data->tls_service, "submission") == 0 ? TRUE : FALSE;
1808
1809         if (hostname != NULL) {
1810                 if (data->hostname_entry)
1811                         gtk_entry_set_text(data->hostname_entry, hostname);
1812                 if (data->set_port)
1813                         gtk_toggle_button_set_active(data->set_port,
1814                                 (ssl && port != data->default_ssl_port) || (!ssl && port != data->default_port));
1815                 if (data->port)
1816                         gtk_spin_button_set_value(data->port, port);
1817                 else if (data->hostname_entry) {
1818                         if ((ssl && port != data->default_ssl_port) || (!ssl && port != data->default_port)) {
1819                                 gchar *tmp = g_strdup_printf("%s:%d", hostname, port);
1820                                 gtk_entry_set_text(data->hostname_entry, tmp);
1821                                 g_free(tmp);
1822                         } else
1823                                 gtk_entry_set_text(data->hostname_entry, hostname);
1824                 }
1825
1826                 if (ssl && data->ssl_checkbtn) {
1827                         gtk_toggle_button_set_active(data->ssl_checkbtn, TRUE);
1828                         gtk_toggle_button_set_active(data->tls_checkbtn, FALSE);
1829                 } else if (data->tls_checkbtn) {
1830                         if (!GTK_IS_RADIO_BUTTON(data->ssl_checkbtn)) {
1831                                 /* Wizard where TLS is [x]SSL + [x]TLS */
1832                                 gtk_toggle_button_set_active(data->ssl_checkbtn, TRUE);
1833                         }
1834
1835                         /* Even though technically this is against the RFCs,
1836                          * if a "_submission._tcp" SRV record uses port 465,
1837                          * it is safe to assume TLS-only service, instead of
1838                          * plaintext + STARTTLS one. */
1839                         if (smtp && port == 465)
1840                                 gtk_toggle_button_set_active(data->ssl_checkbtn, TRUE);
1841                         else
1842                                 gtk_toggle_button_set_active(data->tls_checkbtn, TRUE);
1843                 }
1844
1845                 /* Check authentication by default. This is probably required if
1846                  * auto-configuration worked.
1847                  */
1848                 if (data->auth_checkbtn)
1849                         gtk_toggle_button_set_active(data->auth_checkbtn, TRUE);
1850
1851                 /* Set user ID to full email address, which is used by the
1852                  * majority of providers where auto-configuration works.
1853                  */
1854                 if (data->uid_entry)
1855                         gtk_entry_set_text(data->uid_entry, data->address);
1856
1857                 gtk_label_set_text(data->info_label, _("Done."));
1858         } else {
1859                 gchar *msg;
1860                 switch (data->resolver_error) {
1861                 case G_RESOLVER_ERROR_NOT_FOUND:
1862                         msg = g_strdup(_("Failed: no service record found."));
1863                         break;
1864                 case G_RESOLVER_ERROR_TEMPORARY_FAILURE:
1865                         msg = g_strdup(_("Failed: network error."));
1866                         break;
1867                 default:
1868                         msg = g_strdup_printf(_("Failed: unknown error (%d)."), data->resolver_error);
1869                 }
1870                 gtk_label_set_text(data->info_label, msg);
1871                 g_free(msg);
1872         }
1873         gtk_widget_show(GTK_WIDGET(data->configure_button));
1874         gtk_widget_hide(GTK_WIDGET(data->cancel_button));
1875         g_free(data->address);
1876         g_free(data);
1877 }
1878
1879 static void resolve_done(GObject *source, GAsyncResult *result, gpointer user_data)
1880 {
1881         AutoConfigureData *data = (AutoConfigureData *)user_data;
1882         GResolver *resolver = (GResolver *)source;
1883         GError *error = NULL;
1884         gchar *hostname = NULL;
1885         guint16 port;
1886         GList *answers, *cur;
1887         gboolean found = FALSE;
1888         gboolean abort = FALSE;
1889
1890         answers = g_resolver_lookup_service_finish(resolver, result, &error);
1891
1892         if (answers) {
1893                 for (cur = g_srv_target_list_sort(answers); cur; cur = cur->next) {
1894                         GSrvTarget *target = (GSrvTarget *)cur->data;
1895                         const gchar *h = g_srv_target_get_hostname(target);
1896                         port = g_srv_target_get_port(target);
1897                         if (h && strcmp(h,"") && port > 0) {
1898                                 hostname = g_strdup(h);
1899                                 found = TRUE;
1900                                 break;
1901                         }
1902                 }
1903                 g_resolver_free_targets(answers);
1904         } else if (error) {
1905                 if (error->code == G_IO_ERROR_CANCELLED)
1906                         abort = TRUE;
1907                 else
1908                         data->resolver_error = error->code;
1909                 debug_print("error %s\n", error->message);
1910                 g_error_free(error);
1911         }
1912
1913         if (found) {
1914                 auto_configure_done(hostname, port, data->ssl_service != NULL, data);
1915         } else if (data->ssl_service && !abort) {
1916                 /* Fallback to TLS */
1917                 data->ssl_service = NULL;
1918                 auto_configure_service(data);
1919         } else {
1920                 auto_configure_done(NULL, 0, FALSE, data);
1921         }
1922         g_free(hostname);
1923         g_object_unref(resolver);
1924 }
1925
1926 void auto_configure_service(AutoConfigureData *data)
1927 {
1928         GResolver *resolver;
1929         const gchar *cur_service = data->ssl_service != NULL ? data->ssl_service : data->tls_service;
1930
1931         cm_return_if_fail(cur_service != NULL);
1932         cm_return_if_fail(data->address != NULL);
1933
1934         resolver = g_resolver_get_default();
1935         if (resolver != NULL) {
1936                 const gchar *domain = strchr(data->address, '@') + 1;
1937
1938                 gtk_label_set_text(data->info_label, _("Configuring..."));
1939                 gtk_widget_hide(GTK_WIDGET(data->configure_button));
1940                 gtk_widget_show(GTK_WIDGET(data->cancel_button));
1941                 g_resolver_lookup_service_async(resolver, cur_service, "tcp", domain,
1942                                         data->cancel, resolve_done, data);
1943         }
1944 }
1945
1946 gboolean auto_configure_service_sync(const gchar *service, const gchar *domain, gchar **srvhost, guint16 *srvport)
1947 {
1948         GResolver *resolver;
1949         GList *answers, *cur;
1950         GError *error = NULL;
1951         gboolean result = FALSE;
1952
1953         cm_return_val_if_fail(service != NULL, FALSE);
1954         cm_return_val_if_fail(domain != NULL, FALSE);
1955
1956         resolver = g_resolver_get_default();
1957         if (resolver == NULL)
1958                 return FALSE;
1959
1960         answers = g_resolver_lookup_service(resolver, service, "tcp", domain, NULL, &error);
1961
1962         *srvhost = NULL;
1963         *srvport = 0;
1964
1965         if (answers) {
1966                 for (cur = g_srv_target_list_sort(answers); cur; cur = cur->next) {
1967                         GSrvTarget *target = (GSrvTarget *)cur->data;
1968                         const gchar *hostname = g_srv_target_get_hostname(target);
1969                         guint16 port = g_srv_target_get_port(target);
1970                         if (hostname && strcmp(hostname,"") && port > 0) {
1971                                 result = TRUE;
1972                                 *srvhost = g_strdup(hostname);
1973                                 *srvport = port;
1974                                 break;
1975                         }
1976                 }
1977                 g_resolver_free_targets(answers);
1978         } else if (error) {
1979                 g_error_free(error);
1980         }
1981
1982         g_object_unref(resolver);
1983         return result;
1984 }
1985 #endif
1986
1987 gpointer gtkut_tree_view_get_selected_pointer(GtkTreeView *view,
1988                 gint column, GtkTreeModel **_model, GtkTreeSelection **_selection,
1989                 GtkTreeIter *_iter)
1990 {
1991         GtkTreeIter iter;
1992         GtkTreeModel *model;
1993         GtkTreeSelection *sel;
1994         gpointer ptr;
1995         GType type;
1996
1997         cm_return_val_if_fail(view != NULL, NULL);
1998         cm_return_val_if_fail(column >= 0, NULL);
1999
2000         model = gtk_tree_view_get_model(view);
2001         if (_model != NULL)
2002                 *_model = model;
2003
2004         sel = gtk_tree_view_get_selection(view);
2005         if (_selection != NULL)
2006                 *_selection = sel;
2007
2008         if (!gtk_tree_selection_get_selected(sel, NULL, &iter))
2009                 return NULL; /* No row selected */
2010
2011         if (_iter != NULL)
2012                 *_iter = iter;
2013
2014         if (gtk_tree_selection_count_selected_rows(sel) > 1)
2015                 return NULL; /* Can't work with multiselect */
2016
2017         cm_return_val_if_fail(
2018                         gtk_tree_model_get_n_columns(model) > column,
2019                         NULL);
2020
2021         type = gtk_tree_model_get_column_type(model, column);
2022         cm_return_val_if_fail(
2023                         type == G_TYPE_POINTER || type == G_TYPE_STRING,
2024                         NULL);
2025
2026         gtk_tree_model_get(model, &iter, column, &ptr, -1);
2027
2028         return ptr;
2029 }
2030
2031 static GList *get_predefined_times(void)
2032 {
2033         int h,m;
2034         GList *times = NULL;
2035         for (h = 0; h < 24; h++) {
2036                 for (m = 0; m < 60; m += 15) {
2037                         gchar *tmp = g_strdup_printf("%02d:%02d", h, m);
2038                         times = g_list_append(times, tmp);
2039                 }
2040         }
2041         return times;
2042 }
2043
2044 static int get_list_item_num(int h, int m)
2045 {
2046         if (m % 15 != 0)
2047                 return -1;
2048
2049         return (h*4 + m/15);
2050 }
2051
2052 GtkWidget *gtkut_time_select_combo_new()
2053 {
2054         GtkWidget *combo = gtk_combo_box_text_new_with_entry();
2055         GList *times = get_predefined_times();
2056
2057         gtk_combo_box_set_active(GTK_COMBO_BOX(combo), -1);
2058         combobox_set_popdown_strings(GTK_COMBO_BOX_TEXT(combo), times);
2059
2060         list_free_strings_full(times);
2061
2062         return combo;
2063 }
2064
2065
2066 void gtkut_time_select_select_by_time(GtkComboBox *combo, int hour, int minute)
2067 {
2068         gchar *time_text = g_strdup_printf("%02d:%02d", hour, minute);
2069         gint num = get_list_item_num(hour, minute);
2070
2071         if (num > -1)
2072                 combobox_select_by_text(combo, time_text);
2073         else
2074                 gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(combo))), time_text);
2075
2076         g_free(time_text);
2077 }
2078
2079 static void get_time_from_combo(GtkComboBox *combo, int *h, int *m)
2080 {
2081         gchar *tmp;
2082         gchar **parts;
2083
2084         if (!h || !m) 
2085                 return;
2086
2087         tmp = gtk_editable_get_chars(GTK_EDITABLE(gtk_bin_get_child(GTK_BIN(combo))), 0, -1);
2088         parts = g_strsplit(tmp, ":", 2);
2089         if (parts[0] && parts[1] && *parts[0] && *parts[1]) {
2090                 *h = atoi(parts[0]);
2091                 *m = atoi(parts[1]);
2092         }
2093         g_strfreev(parts);
2094         g_free(tmp);
2095 }
2096
2097 gboolean gtkut_time_select_get_time(GtkComboBox *combo, int *hour, int *minute)
2098 {
2099         const gchar *value = gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(combo))));
2100
2101         if (value == NULL || strlen(value) != 5)
2102                 return FALSE;
2103
2104         if (hour == NULL || minute == NULL)
2105                 return FALSE;
2106
2107         get_time_from_combo(combo, hour, minute);
2108
2109         if (*hour < 0 || *hour > 23)
2110                 return FALSE;
2111         if (*minute < 0 || *minute > 59)
2112                 return FALSE;
2113
2114         return TRUE;
2115 }
2116
2117 void gtk_calendar_select_today(GtkCalendar *calendar)
2118 {
2119         time_t t = time (NULL);
2120         struct tm buft;
2121         struct tm *lt = localtime_r (&t, &buft);
2122
2123         mktime(lt);
2124         gtk_calendar_select_day(calendar, lt->tm_mday);
2125         gtk_calendar_select_month(calendar, lt->tm_mon, lt->tm_year + 1900);
2126 }