1 /*
2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2020 the Claws Mail team and Hiroyuki Yamamoto
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
gtkut_get_font_size(GtkWidget * widget,gint * width,gint * height)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
gtkut_widget_set_small_font_size(GtkWidget * widget)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
gtkut_convert_int_to_gdk_color(gint rgbvalue,GdkColor * color)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
gtkut_convert_gdk_color_to_int(GdkColor * color)117 gint gtkut_convert_gdk_color_to_int(GdkColor *color)
118 {
119 return RGB_FROM_GDK_COLOR(color);
120 }
121
gtkut_stock_button_add_help(GtkWidget * bbox,GtkWidget ** help_btn)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
gtkut_stock_button_set_create_with_help(GtkWidget ** bbox,GtkWidget ** help_button,GtkWidget ** button1,const gchar * label1,GtkWidget ** button2,const gchar * label2,GtkWidget ** button3,const gchar * label3)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
gtkut_stock_button_set_create(GtkWidget ** bbox,GtkWidget ** button1,const gchar * label1,GtkWidget ** button2,const gchar * label2,GtkWidget ** button3,const gchar * label3)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
gtkut_stock_with_text_button_set_create(GtkWidget ** bbox,GtkWidget ** button1,const gchar * label1,const gchar * text1,GtkWidget ** button2,const gchar * label2,const gchar * text2,GtkWidget ** button3,const gchar * label3,const gchar * text3)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
gtkut_ctree_node_move_if_on_the_edge(GtkCMCTree * ctree,GtkCMCTreeNode * node,gint _row)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
gtkut_ctree_get_nth_from_node(GtkCMCTree * ctree,GtkCMCTreeNode * node)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 */
gtkut_ctree_node_next(GtkCMCTree * ctree,GtkCMCTreeNode * node)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 */
gtkut_ctree_node_prev(GtkCMCTree * ctree,GtkCMCTreeNode * node)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
gtkut_ctree_node_is_selected(GtkCMCTree * ctree,GtkCMCTreeNode * node)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
gtkut_ctree_find_collapsed_parent(GtkCMCTree * ctree,GtkCMCTreeNode * node)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
gtkut_ctree_expand_parent_all(GtkCMCTree * ctree,GtkCMCTreeNode * node)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
gtkut_ctree_node_is_parent(GtkCMCTreeNode * parent,GtkCMCTreeNode * node)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
gtkut_ctree_set_focus_row(GtkCMCTree * ctree,GtkCMCTreeNode * node)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
gtkut_clist_set_focus_row(GtkCMCList * clist,gint row)389 void gtkut_clist_set_focus_row(GtkCMCList *clist, gint row)
390 {
391 clist->focus_row = row;
392 GTKUT_CTREE_REFRESH(clist);
393 }
394
gtkut_container_remove(GtkContainer * container,GtkWidget * widget)395 void gtkut_container_remove(GtkContainer *container, GtkWidget *widget)
396 {
397 gtk_container_remove(container, widget);
398 }
399
gtkut_text_buffer_match_string(GtkTextBuffer * textbuf,const GtkTextIter * iter,gunichar * wcs,gint len,gboolean case_sens)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
gtkut_text_buffer_find(GtkTextBuffer * buffer,const GtkTextIter * iter,const gchar * str,gboolean case_sens,GtkTextIter * match_pos)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
gtkut_text_buffer_find_backward(GtkTextBuffer * buffer,const GtkTextIter * iter,const gchar * str,gboolean case_sens,GtkTextIter * match_pos)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
gtkut_text_view_get_selection(GtkTextView * textview)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
gtkut_text_view_set_position(GtkTextView * text,gint pos)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
gtkut_text_view_search_string(GtkTextView * text,const gchar * str,gboolean case_sens)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
gtkut_text_view_search_string_backward(GtkTextView * text,const gchar * str,gboolean case_sens)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
gtkut_window_popup(GtkWidget * window)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
gtkut_widget_get_uposition(GtkWidget * widget,gint * px,gint * py)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
gtkut_widget_draw_now(GtkWidget * widget)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
gtkut_clist_bindings_add(GtkWidget * clist)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
gtkut_widget_init(void)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
gtkut_widget_set_app_icon(GtkWidget * widget)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
gtkut_widget_set_composer_icon(GtkWidget * widget)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
move_bar_cb(gpointer data)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 gtk_progress_bar_set_pulse_step(GTK_PROGRESS_BAR(w), 0.1);
761 gtk_progress_bar_pulse(GTK_PROGRESS_BAR(w));
762 GTK_EVENTS_FLUSH();
763 return TRUE;
764 }
765
label_window_create(const gchar * str)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
label_window_destroy(GtkWidget * window)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
gtkut_account_menu_new(GList * ac_list,GCallback callback,gpointer data)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
gtkut_set_widget_bgcolor_rgb(GtkWidget * widget,guint rgbvalue)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 */
gtkut_get_focused_child(GtkContainer * parent)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 */
gtkut_get_browse_file_btn(const gchar * button_label)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 */
gtkut_get_browse_directory_btn(const gchar * button_label)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
gtkut_get_replace_btn(const gchar * button_label)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 */
gtkut_get_options_frame(GtkWidget * box,GtkWidget ** pframe,const gchar * frame_label)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
create_xpm_from_xface(gchar * xpm[],const gchar * xface)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
get_tag_range(GtkTextIter * iter,GtkTextTag * tag,GtkTextIter * start_iter,GtkTextIter * end_iter)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
xface_get_from_header(const gchar * o_xface)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
1093 if (o_xface == NULL)
1094 return NULL;
1095
1096 strncpy(xface, o_xface, sizeof(xface) - 1);
1097 xface[sizeof(xface) - 1] = '\0';
1098
1099 if (uncompface(xface) < 0) {
1100 g_warning("uncompface failed");
1101 return NULL;
1102 }
1103
1104 if (xpm_xface_init) {
1105 gint i;
1106
1107 for (i = 0; i < XPM_XFACE_HEIGHT; i++) {
1108 xpm_xface[i] = g_malloc(WIDTH + 1);
1109 *xpm_xface[i] = '\0';
1110 }
1111 xpm_xface_init = FALSE;
1112 }
1113
1114 create_xpm_from_xface(xpm_xface, xface);
1115
1116 return gtk_image_new_from_pixbuf(
1117 gdk_pixbuf_new_from_xpm_data((const char **)xpm_xface));
1118 }
1119 #endif
1120
face_get_from_header(const gchar * o_face)1121 GtkWidget *face_get_from_header(const gchar *o_face)
1122 {
1123 gchar face[2048];
1124 gchar *face_png;
1125 gsize pngsize;
1126 GdkPixbuf *pixbuf;
1127 GError *error = NULL;
1128 GdkPixbufLoader *loader = gdk_pixbuf_loader_new ();
1129 GtkWidget *image;
1130
1131 if (o_face == NULL || strlen(o_face) == 0)
1132 return NULL;
1133
1134 strncpy2(face, o_face, sizeof(face));
1135
1136 unfold_line(face); /* strip all whitespace and linebreaks */
1137 remove_space(face);
1138
1139 face_png = g_base64_decode(face, &pngsize);
1140 debug_print("---------------------- loaded face png\n");
1141
1142 if (!gdk_pixbuf_loader_write (loader, face_png, pngsize, &error) ||
1143 !gdk_pixbuf_loader_close (loader, &error)) {
1144 g_warning("loading face failed");
1145 g_object_unref(loader);
1146 g_free(face_png);
1147 return NULL;
1148 }
1149 g_free(face_png);
1150
1151 pixbuf = g_object_ref(gdk_pixbuf_loader_get_pixbuf(loader));
1152
1153 g_object_unref(loader);
1154
1155 if ((gdk_pixbuf_get_width(pixbuf) != 48) || (gdk_pixbuf_get_height(pixbuf) != 48)) {
1156 g_object_unref(pixbuf);
1157 g_warning("wrong_size");
1158 return NULL;
1159 }
1160
1161 image = gtk_image_new_from_pixbuf(pixbuf);
1162 g_object_unref(pixbuf);
1163 return image;
1164 }
1165
1166 static GdkCursor *hand_cursor = NULL;
1167
link_btn_enter(GtkButton * button,gpointer data)1168 static void link_btn_enter(GtkButton *button, gpointer data)
1169 {
1170 GdkWindow *gdkwin;
1171 GtkWidget *window = (GtkWidget *)data;
1172
1173 gdkwin = gtk_widget_get_window(window);
1174
1175 if (!hand_cursor)
1176 hand_cursor = gdk_cursor_new(GDK_HAND2);
1177 if (window && gdkwin)
1178 gdk_window_set_cursor(gdkwin, hand_cursor);
1179
1180 gtk_button_set_relief(button, GTK_RELIEF_NONE);
1181 gtk_widget_set_state(GTK_WIDGET(button), GTK_STATE_NORMAL);
1182
1183 }
1184
link_btn_leave(GtkButton * button,gpointer data)1185 static void link_btn_leave(GtkButton *button, gpointer data)
1186 {
1187 GdkWindow *gdkwin;
1188 GtkWidget *window = (GtkWidget *)data;
1189
1190 gdkwin = gtk_widget_get_window(window);
1191
1192 if (window && gdkwin)
1193 gdk_window_set_cursor(gdkwin, NULL);
1194
1195 gtk_button_set_relief(button, GTK_RELIEF_NONE);
1196 gtk_widget_set_state(GTK_WIDGET(button), GTK_STATE_NORMAL);
1197 }
1198
link_btn_pressed(GtkButton * button,gpointer data)1199 static void link_btn_pressed(GtkButton *button, gpointer data)
1200 {
1201 gtk_button_set_relief(button, GTK_RELIEF_NONE);
1202 gtk_widget_set_state(GTK_WIDGET(button), GTK_STATE_NORMAL);
1203 }
1204
link_btn_released(GtkButton * button,gpointer data)1205 static void link_btn_released(GtkButton *button, gpointer data)
1206 {
1207 gtk_button_set_relief(button, GTK_RELIEF_NONE);
1208 gtk_widget_set_state(GTK_WIDGET(button), GTK_STATE_NORMAL);
1209 }
1210
link_btn_clicked(GtkButton * button,gpointer data)1211 static void link_btn_clicked(GtkButton *button, gpointer data)
1212 {
1213 gchar *url = (gchar *)data;
1214 gtk_button_set_relief(button, GTK_RELIEF_NONE);
1215 gtk_widget_set_state(GTK_WIDGET(button), GTK_STATE_NORMAL);
1216 open_uri(url, prefs_common_get_uri_cmd());
1217 }
1218
link_btn_unrealize(GtkButton * button,gpointer data)1219 static void link_btn_unrealize(GtkButton *button, gpointer data)
1220 {
1221 gchar *url = (gchar *)data;
1222 g_signal_handlers_disconnect_by_func(G_OBJECT(button),
1223 G_CALLBACK(link_btn_clicked), url);
1224 g_free(url);
1225 }
1226
gtkut_get_link_btn(GtkWidget * window,const gchar * url,const gchar * label)1227 GtkWidget *gtkut_get_link_btn(GtkWidget *window, const gchar *url, const gchar *label)
1228 {
1229 GtkWidget *btn;
1230 GtkWidget *btn_label;
1231 GdkColormap *cmap;
1232 gboolean success[2];
1233 GdkColor uri_color[2] = {{0, 0, 0, 0xffff}, {0, 0xffff, 0, 0}};
1234 gchar *local_url = NULL;
1235 if (!url)
1236 return NULL;
1237
1238 gtkut_convert_int_to_gdk_color(prefs_common.color[COL_URI],
1239 &uri_color[0]);
1240 gtkut_convert_int_to_gdk_color(prefs_common.color[COL_URI],
1241 &uri_color[1]);
1242
1243 btn = gtk_button_new_with_label(label?label:url);
1244 gtk_button_set_relief(GTK_BUTTON(btn), GTK_RELIEF_NONE);
1245 btn_label = gtk_bin_get_child(GTK_BIN((btn)));
1246 cmap = gdk_drawable_get_colormap(gtk_widget_get_window(window));
1247 gdk_colormap_alloc_colors(cmap, uri_color, 2, FALSE, TRUE, success);
1248 if (success[0] == TRUE && success[1] == TRUE) {
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 } else
1259 g_warning("color allocation failed");
1260
1261 g_signal_connect(G_OBJECT(btn), "enter",
1262 G_CALLBACK(link_btn_enter), window);
1263 g_signal_connect(G_OBJECT(btn), "leave",
1264 G_CALLBACK(link_btn_leave), window);
1265 g_signal_connect(G_OBJECT(btn), "pressed",
1266 G_CALLBACK(link_btn_pressed), window);
1267 g_signal_connect(G_OBJECT(btn), "released",
1268 G_CALLBACK(link_btn_released), window);
1269
1270 local_url = g_strdup(url);
1271 g_signal_connect(G_OBJECT(btn), "clicked",
1272 G_CALLBACK(link_btn_clicked), local_url);
1273 g_signal_connect(G_OBJECT(btn), "unrealize",
1274 G_CALLBACK(link_btn_unrealize), local_url);
1275 return btn;
1276 }
1277
_combobox_separator_func(GtkTreeModel * model,GtkTreeIter * iter,gpointer data)1278 static gboolean _combobox_separator_func(GtkTreeModel *model,
1279 GtkTreeIter *iter, gpointer data)
1280 {
1281 gchar *txt = NULL;
1282
1283 cm_return_val_if_fail(model != NULL, FALSE);
1284
1285 gtk_tree_model_get(model, iter, COMBOBOX_TEXT, &txt, -1);
1286
1287 if( txt == NULL )
1288 return TRUE;
1289
1290 g_free(txt);
1291 return FALSE;
1292 }
1293
gtkut_sc_combobox_create(GtkWidget * eventbox,gboolean focus_on_click)1294 GtkWidget *gtkut_sc_combobox_create(GtkWidget *eventbox, gboolean focus_on_click)
1295 {
1296 GtkWidget *combobox;
1297 GtkListStore *menu;
1298 GtkCellRenderer *rend;
1299
1300 menu = gtk_list_store_new(3, G_TYPE_STRING, G_TYPE_INT, G_TYPE_BOOLEAN);
1301
1302 combobox = gtk_combo_box_new_with_model(GTK_TREE_MODEL(menu));
1303
1304 rend = gtk_cell_renderer_text_new();
1305 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combobox), rend, TRUE);
1306 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combobox), rend,
1307 "markup", COMBOBOX_TEXT,
1308 "sensitive", COMBOBOX_SENS,
1309 NULL);
1310
1311 if( eventbox != NULL )
1312 gtk_container_add(GTK_CONTAINER(eventbox), combobox);
1313 gtk_combo_box_set_focus_on_click(GTK_COMBO_BOX(combobox), focus_on_click);
1314
1315 gtk_combo_box_set_row_separator_func(GTK_COMBO_BOX(combobox),
1316 (GtkTreeViewRowSeparatorFunc)_combobox_separator_func, NULL, NULL);
1317
1318 return combobox;
1319 }
1320
gtkutils_smooth_scroll_do(GtkWidget * widget,GtkAdjustment * vadj,gfloat old_value,gfloat last_value,gint step)1321 static void gtkutils_smooth_scroll_do(GtkWidget *widget, GtkAdjustment *vadj,
1322 gfloat old_value, gfloat last_value,
1323 gint step)
1324 {
1325 gint change_value;
1326 gboolean up;
1327 gint i;
1328
1329 if (old_value < last_value) {
1330 change_value = last_value - old_value;
1331 up = FALSE;
1332 } else {
1333 change_value = old_value - last_value;
1334 up = TRUE;
1335 }
1336
1337 for (i = step; i <= change_value; i += step) {
1338 gtk_adjustment_set_value(vadj, old_value + (up ? -i : i));
1339 g_signal_emit_by_name(G_OBJECT(vadj),
1340 "value_changed", 0);
1341 }
1342
1343 gtk_adjustment_set_value(vadj, last_value);
1344 g_signal_emit_by_name(G_OBJECT(vadj), "value_changed", 0);
1345
1346 gtk_widget_queue_draw(widget);
1347 }
1348
gtkutils_smooth_scroll_page(GtkWidget * widget,GtkAdjustment * vadj,gboolean up)1349 static gboolean gtkutils_smooth_scroll_page(GtkWidget *widget, GtkAdjustment *vadj, gboolean up)
1350 {
1351 gfloat upper;
1352 gfloat page_incr;
1353 gfloat old_value;
1354 gfloat last_value;
1355
1356 page_incr = gtk_adjustment_get_page_increment(vadj);
1357 if (prefs_common.scroll_halfpage)
1358 page_incr /= 2;
1359
1360 old_value = gtk_adjustment_get_value(vadj);
1361 if (!up) {
1362 upper = gtk_adjustment_get_upper(vadj) - gtk_adjustment_get_page_size(vadj);
1363 if (old_value < upper) {
1364 last_value = old_value + page_incr;
1365 last_value = MIN(last_value, upper);
1366
1367 gtkutils_smooth_scroll_do(widget, vadj, old_value,
1368 last_value,
1369 prefs_common.scroll_step);
1370 } else
1371 return FALSE;
1372 } else {
1373 if (old_value > 0.0) {
1374 last_value = old_value - page_incr;
1375 last_value = MAX(last_value, 0.0);
1376
1377 gtkutils_smooth_scroll_do(widget, vadj, old_value,
1378 last_value,
1379 prefs_common.scroll_step);
1380 } else
1381 return FALSE;
1382 }
1383
1384 return TRUE;
1385 }
1386
gtkutils_scroll_page(GtkWidget * widget,GtkAdjustment * vadj,gboolean up)1387 gboolean gtkutils_scroll_page(GtkWidget *widget, GtkAdjustment *vadj, gboolean up)
1388 {
1389 gfloat upper;
1390 gfloat page_incr;
1391 gfloat old_value;
1392
1393 if (prefs_common.enable_smooth_scroll)
1394 return gtkutils_smooth_scroll_page(widget, vadj, up);
1395
1396 page_incr = gtk_adjustment_get_page_increment(vadj);
1397 if (prefs_common.scroll_halfpage)
1398 page_incr /= 2;
1399
1400 old_value = gtk_adjustment_get_value(vadj);
1401 if (!up) {
1402 upper = gtk_adjustment_get_upper(vadj) - gtk_adjustment_get_page_size(vadj);
1403 if (old_value < upper) {
1404 old_value += page_incr;
1405 old_value = MIN(old_value, upper);
1406 gtk_adjustment_set_value(vadj, old_value);
1407 g_signal_emit_by_name(G_OBJECT(vadj),
1408 "value_changed", 0);
1409 } else
1410 return FALSE;
1411 } else {
1412 if (old_value > 0.0) {
1413 old_value -= page_incr;
1414 old_value = MAX(old_value, 0.0);
1415 gtk_adjustment_set_value(vadj, old_value);
1416 g_signal_emit_by_name(G_OBJECT(vadj),
1417 "value_changed", 0);
1418 } else
1419 return FALSE;
1420 }
1421 return TRUE;
1422 }
1423
gtkutils_smooth_scroll_one_line(GtkWidget * widget,GtkAdjustment * vadj,gboolean up)1424 static void gtkutils_smooth_scroll_one_line(GtkWidget *widget, GtkAdjustment *vadj, gboolean up)
1425 {
1426 gfloat upper;
1427 gfloat old_value;
1428 gfloat last_value;
1429
1430 old_value = gtk_adjustment_get_value(vadj);
1431 if (!up) {
1432 upper = gtk_adjustment_get_upper(vadj) - gtk_adjustment_get_page_size(vadj);
1433 if (old_value < upper) {
1434 last_value = old_value + gtk_adjustment_get_step_increment(vadj);
1435 last_value = MIN(last_value, upper);
1436
1437 gtkutils_smooth_scroll_do(widget, vadj, old_value,
1438 last_value,
1439 prefs_common.scroll_step);
1440 }
1441 } else {
1442 if (old_value > 0.0) {
1443 last_value = old_value - gtk_adjustment_get_step_increment(vadj);
1444 last_value = MAX(last_value, 0.0);
1445
1446 gtkutils_smooth_scroll_do(widget, vadj, old_value,
1447 last_value,
1448 prefs_common.scroll_step);
1449 }
1450 }
1451 }
1452
gtkutils_scroll_one_line(GtkWidget * widget,GtkAdjustment * vadj,gboolean up)1453 void gtkutils_scroll_one_line(GtkWidget *widget, GtkAdjustment *vadj, gboolean up)
1454 {
1455 gfloat upper;
1456 gfloat old_value;
1457
1458 if (prefs_common.enable_smooth_scroll) {
1459 gtkutils_smooth_scroll_one_line(widget, vadj, up);
1460 return;
1461 }
1462
1463 old_value = gtk_adjustment_get_value(vadj);
1464 if (!up) {
1465 upper = gtk_adjustment_get_upper(vadj) - gtk_adjustment_get_page_size(vadj);
1466 if (old_value < upper) {
1467 old_value += gtk_adjustment_get_step_increment(vadj);
1468 old_value = MIN(old_value, upper);
1469 gtk_adjustment_set_value(vadj, old_value);
1470 g_signal_emit_by_name(G_OBJECT(vadj),
1471 "value_changed", 0);
1472 }
1473 } else {
1474 if (old_value > 0.0) {
1475 old_value -= gtk_adjustment_get_step_increment(vadj);
1476 old_value = MAX(old_value, 0.0);
1477 gtk_adjustment_set_value(vadj, old_value);
1478 g_signal_emit_by_name(G_OBJECT(vadj),
1479 "value_changed", 0);
1480 }
1481 }
1482 }
1483
gtkut_tree_model_text_iter_prev(GtkTreeModel * model,GtkTreeIter * iter,const gchar * text)1484 gboolean gtkut_tree_model_text_iter_prev(GtkTreeModel *model,
1485 GtkTreeIter *iter,
1486 const gchar* text)
1487 /* do the same as gtk_tree_model_iter_next, but _prev instead.
1488 to use with widgets with one text column (gtk_combo_box_text_new()
1489 and with GtkComboBoxEntry's for instance),
1490 */
1491 {
1492 GtkTreeIter cur_iter;
1493 gchar *cur_value;
1494 gboolean valid;
1495 gint count;
1496
1497 cm_return_val_if_fail(model != NULL, FALSE);
1498 cm_return_val_if_fail(iter != NULL, FALSE);
1499
1500 if (text == NULL || *text == '\0')
1501 return FALSE;
1502
1503 valid = gtk_tree_model_get_iter_first(model, &cur_iter);
1504 count = 0;
1505 while (valid) {
1506 gtk_tree_model_get(model, &cur_iter, 0, &cur_value, -1);
1507
1508 if (strcmp(text, cur_value) == 0) {
1509 g_free(cur_value);
1510 if (count <= 0)
1511 return FALSE;
1512
1513 return gtk_tree_model_iter_nth_child(model, iter, NULL, count - 1);
1514 }
1515
1516 g_free(cur_value);
1517 valid = gtk_tree_model_iter_next(model, &cur_iter);
1518 count++;
1519 }
1520 return FALSE;
1521 }
1522
gtkut_tree_model_get_iter_last(GtkTreeModel * model,GtkTreeIter * iter)1523 gboolean gtkut_tree_model_get_iter_last(GtkTreeModel *model,
1524 GtkTreeIter *iter)
1525 /* do the same as gtk_tree_model_get_iter_first, but _last instead.
1526 */
1527 {
1528 gint count;
1529
1530 cm_return_val_if_fail(model != NULL, FALSE);
1531 cm_return_val_if_fail(iter != NULL, FALSE);
1532
1533 count = gtk_tree_model_iter_n_children(model, NULL);
1534
1535 if (count <= 0)
1536 return FALSE;
1537
1538 return gtk_tree_model_iter_nth_child(model, iter, NULL, count - 1);
1539 }
1540
gtkut_window_new(GtkWindowType type,const gchar * class)1541 GtkWidget *gtkut_window_new (GtkWindowType type,
1542 const gchar *class)
1543 {
1544 GtkWidget *window = gtk_window_new(type);
1545 gtk_window_set_role(GTK_WINDOW(window), class);
1546 return window;
1547 }
1548
gtkut_tree_iter_comp(GtkTreeModel * model,GtkTreeIter * iter1,GtkTreeIter * iter2)1549 static gboolean gtkut_tree_iter_comp(GtkTreeModel *model,
1550 GtkTreeIter *iter1,
1551 GtkTreeIter *iter2)
1552 {
1553 GtkTreePath *path1 = gtk_tree_model_get_path(model, iter1);
1554 GtkTreePath *path2 = gtk_tree_model_get_path(model, iter2);
1555 gboolean result;
1556
1557 result = gtk_tree_path_compare(path1, path2) == 0;
1558
1559 gtk_tree_path_free(path1);
1560 gtk_tree_path_free(path2);
1561
1562 return result;
1563 }
1564
1565 /*!
1566 *\brief Get selected row number.
1567 */
gtkut_list_view_get_selected_row(GtkWidget * list_view)1568 gint gtkut_list_view_get_selected_row(GtkWidget *list_view)
1569 {
1570 GtkTreeView *view = GTK_TREE_VIEW(list_view);
1571 GtkTreeModel *model = gtk_tree_view_get_model(view);
1572 int n_rows = gtk_tree_model_iter_n_children(model, NULL);
1573 GtkTreeSelection *selection;
1574 GtkTreeIter iter;
1575 int row;
1576
1577 if (n_rows == 0)
1578 return -1;
1579
1580 selection = gtk_tree_view_get_selection(view);
1581 if (!gtk_tree_selection_get_selected(selection, &model, &iter))
1582 return -1;
1583
1584 /* get all iterators and compare them... */
1585 for (row = 0; row < n_rows; row++) {
1586 GtkTreeIter itern;
1587
1588 if (gtk_tree_model_iter_nth_child(model, &itern, NULL, row)
1589 && gtkut_tree_iter_comp(model, &iter, &itern))
1590 return row;
1591 }
1592
1593 return -1;
1594 }
1595
1596 /*!
1597 *\brief Select a row by its number.
1598 */
gtkut_list_view_select_row(GtkWidget * list,gint row)1599 gboolean gtkut_list_view_select_row(GtkWidget *list, gint row)
1600 {
1601 GtkTreeView *list_view = GTK_TREE_VIEW(list);
1602 GtkTreeSelection *selection = gtk_tree_view_get_selection(list_view);
1603 GtkTreeModel *model = gtk_tree_view_get_model(list_view);
1604 GtkTreeIter iter;
1605 GtkTreePath *path;
1606
1607 if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, row))
1608 return FALSE;
1609
1610 gtk_tree_selection_select_iter(selection, &iter);
1611
1612 path = gtk_tree_model_get_path(model, &iter);
1613 gtk_tree_view_set_cursor(list_view, path, NULL, FALSE);
1614 gtk_tree_path_free(path);
1615
1616 return TRUE;
1617 }
1618
1619 static GtkUIManager *gui_manager = NULL;
1620
gtkut_create_ui_manager(void)1621 GtkUIManager *gtkut_create_ui_manager(void)
1622 {
1623 cm_return_val_if_fail(gui_manager == NULL, gui_manager);
1624 return (gui_manager = gtk_ui_manager_new());
1625 }
1626
gtkut_ui_manager(void)1627 GtkUIManager *gtkut_ui_manager(void)
1628 {
1629 return gui_manager;
1630 }
1631
1632 typedef struct _ClawsIOClosure ClawsIOClosure;
1633
1634 struct _ClawsIOClosure
1635 {
1636 ClawsIOFunc function;
1637 GIOCondition condition;
1638 GDestroyNotify notify;
1639 gpointer data;
1640 };
1641
1642 static gboolean
claws_io_invoke(GIOChannel * source,GIOCondition condition,gpointer data)1643 claws_io_invoke (GIOChannel *source,
1644 GIOCondition condition,
1645 gpointer data)
1646 {
1647 ClawsIOClosure *closure = data;
1648 int fd;
1649 #ifndef G_OS_WIN32
1650 fd = g_io_channel_unix_get_fd (source);
1651 #else
1652 fd = g_io_channel_win32_get_fd (source);
1653 #endif
1654 if (closure->condition & condition)
1655 closure->function (closure->data, fd, condition);
1656
1657 return TRUE;
1658 }
1659
1660 static void
claws_io_destroy(gpointer data)1661 claws_io_destroy (gpointer data)
1662 {
1663 ClawsIOClosure *closure = data;
1664
1665 if (closure->notify)
1666 closure->notify (closure->data);
1667
1668 g_free (closure);
1669 }
1670
1671 gint
claws_input_add(gint source,GIOCondition condition,ClawsIOFunc function,gpointer data,gboolean is_sock)1672 claws_input_add (gint source,
1673 GIOCondition condition,
1674 ClawsIOFunc function,
1675 gpointer data,
1676 gboolean is_sock)
1677 {
1678 guint result;
1679 ClawsIOClosure *closure = g_new (ClawsIOClosure, 1);
1680 GIOChannel *channel;
1681
1682 closure->function = function;
1683 closure->condition = condition;
1684 closure->notify = NULL;
1685 closure->data = data;
1686
1687 #ifndef G_OS_WIN32
1688 channel = g_io_channel_unix_new (source);
1689 #else
1690 if (is_sock)
1691 channel = g_io_channel_win32_new_socket(source);
1692 else
1693 channel = g_io_channel_win32_new_fd(source);
1694 #endif
1695 result = g_io_add_watch_full (channel, G_PRIORITY_DEFAULT, condition,
1696 claws_io_invoke,
1697 closure, claws_io_destroy);
1698 g_io_channel_unref (channel);
1699
1700 return result;
1701 }
1702
1703 /**
1704 * Load a pixbuf fitting inside the specified size. EXIF orientation is
1705 * respected if available.
1706 *
1707 * @param[in] filename the file to load
1708 * @param[in] box_width the max width (-1 for no resize)
1709 * @param[in] box_height the max height (-1 for no resize)
1710 * @param[out] error the possible load error
1711 *
1712 * @return a GdkPixbuf
1713 */
claws_load_pixbuf_fitting(GdkPixbuf * src_pixbuf,gboolean inline_img,gboolean fit_img_height,int box_width,int box_height)1714 GdkPixbuf *claws_load_pixbuf_fitting(GdkPixbuf *src_pixbuf, gboolean inline_img,
1715 gboolean fit_img_height, int box_width, int box_height)
1716 {
1717 gint w, h, orientation, angle;
1718 gint avail_width, avail_height;
1719 gboolean flip_horiz, flip_vert;
1720 const gchar *orient_str;
1721 GdkPixbuf *pixbuf, *t_pixbuf;
1722
1723 pixbuf = src_pixbuf;
1724
1725 if (pixbuf == NULL)
1726 return NULL;
1727
1728 angle = 0;
1729 flip_horiz = flip_vert = FALSE;
1730
1731 /* EXIF orientation */
1732 orient_str = gdk_pixbuf_get_option(pixbuf, "orientation");
1733 if (orient_str != NULL && *orient_str != '\0') {
1734 orientation = atoi(orient_str);
1735 switch(orientation) {
1736 /* See EXIF standard for different values */
1737 case 1: break;
1738 case 2: flip_horiz = 1;
1739 break;
1740 case 3: angle = 180;
1741 break;
1742 case 4: flip_vert = 1;
1743 break;
1744 case 5: angle = 90;
1745 flip_horiz = 1;
1746 break;
1747 case 6: angle = 270;
1748 break;
1749 case 7: angle = 90;
1750 flip_vert = 1;
1751 break;
1752 case 8: angle = 90;
1753 break;
1754 }
1755 }
1756
1757
1758 /* Rotate if needed */
1759 if (angle != 0) {
1760 t_pixbuf = gdk_pixbuf_rotate_simple(pixbuf, angle);
1761 g_object_unref(pixbuf);
1762 pixbuf = t_pixbuf;
1763 }
1764
1765 /* Flip horizontally if needed */
1766 if (flip_horiz) {
1767 t_pixbuf = gdk_pixbuf_flip(pixbuf, TRUE);
1768 g_object_unref(pixbuf);
1769 pixbuf = t_pixbuf;
1770 }
1771
1772 /* Flip vertically if needed */
1773 if (flip_vert) {
1774 t_pixbuf = gdk_pixbuf_flip(pixbuf, FALSE);
1775 g_object_unref(pixbuf);
1776 pixbuf = t_pixbuf;
1777 }
1778
1779 w = gdk_pixbuf_get_width(pixbuf);
1780 h = gdk_pixbuf_get_height(pixbuf);
1781
1782 avail_width = box_width-32;
1783 avail_height = box_height;
1784
1785 if (box_width != -1 && box_height != -1 && avail_width - 100 > 0) {
1786 if (inline_img || fit_img_height) {
1787 if (w > avail_width) {
1788 h = (avail_width * h) / w;
1789 w = avail_width;
1790 }
1791 if (h > avail_height) {
1792 w = (avail_height * w) / h;
1793 h = avail_height;
1794 }
1795 } else {
1796 if (w > avail_width || h > avail_height) {
1797 h = (avail_width * h) / w;
1798 w = avail_width;
1799 }
1800 }
1801 t_pixbuf = gdk_pixbuf_scale_simple(pixbuf,
1802 w, h, GDK_INTERP_BILINEAR);
1803 g_object_unref(pixbuf);
1804 pixbuf = t_pixbuf;
1805 }
1806
1807 return pixbuf;
1808 }
1809
1810 #if defined USE_GNUTLS
auto_configure_done(const gchar * hostname,gint port,gboolean ssl,AutoConfigureData * data)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
resolve_done(GObject * source,GAsyncResult * result,gpointer user_data)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
auto_configure_service(AutoConfigureData * data)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
auto_configure_service_sync(const gchar * service,const gchar * domain,gchar ** srvhost,guint16 * srvport)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
gtkut_tree_view_get_selected_pointer(GtkTreeView * view,gint column,GtkTreeModel ** _model,GtkTreeSelection ** _selection,GtkTreeIter * _iter)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 }
2036
get_predefined_times(void)2037 static GList *get_predefined_times(void)
2038 {
2039 int h,m;
2040 GList *times = NULL;
2041 for (h = 0; h < 24; h++) {
2042 for (m = 0; m < 60; m += 15) {
2043 gchar *tmp = g_strdup_printf("%02d:%02d", h, m);
2044 times = g_list_append(times, tmp);
2045 }
2046 }
2047 return times;
2048 }
2049
get_list_item_num(int h,int m)2050 static int get_list_item_num(int h, int m)
2051 {
2052 if (m % 15 != 0)
2053 return -1;
2054
2055 return (h*4 + m/15);
2056 }
2057
gtkut_time_select_combo_new()2058 GtkWidget *gtkut_time_select_combo_new()
2059 {
2060 GtkWidget *combo = gtk_combo_box_text_new_with_entry();
2061 GList *times = get_predefined_times();
2062
2063 gtk_combo_box_set_active(GTK_COMBO_BOX(combo), -1);
2064 combobox_set_popdown_strings(GTK_COMBO_BOX_TEXT(combo), times);
2065
2066 list_free_strings_full(times);
2067
2068 return combo;
2069 }
2070
2071
gtkut_time_select_select_by_time(GtkComboBox * combo,int hour,int minute)2072 void gtkut_time_select_select_by_time(GtkComboBox *combo, int hour, int minute)
2073 {
2074 gchar *time_text = g_strdup_printf("%02d:%02d", hour, minute);
2075 gint num = get_list_item_num(hour, minute);
2076
2077 if (num > -1)
2078 combobox_select_by_text(combo, time_text);
2079 else
2080 gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(combo))), time_text);
2081
2082 g_free(time_text);
2083 }
2084
get_time_from_combo(GtkComboBox * combo,int * h,int * m)2085 static void get_time_from_combo(GtkComboBox *combo, int *h, int *m)
2086 {
2087 gchar *tmp;
2088 gchar **parts;
2089
2090 if (!h || !m)
2091 return;
2092
2093 tmp = gtk_editable_get_chars(GTK_EDITABLE(gtk_bin_get_child(GTK_BIN(combo))), 0, -1);
2094 parts = g_strsplit(tmp, ":", 2);
2095 if (parts[0] && parts[1] && *parts[0] && *parts[1]) {
2096 *h = atoi(parts[0]);
2097 *m = atoi(parts[1]);
2098 }
2099 g_strfreev(parts);
2100 g_free(tmp);
2101 }
2102
gtkut_time_select_get_time(GtkComboBox * combo,int * hour,int * minute)2103 gboolean gtkut_time_select_get_time(GtkComboBox *combo, int *hour, int *minute)
2104 {
2105 const gchar *value = gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(combo))));
2106
2107 if (value == NULL || strlen(value) != 5)
2108 return FALSE;
2109
2110 if (hour == NULL || minute == NULL)
2111 return FALSE;
2112
2113 get_time_from_combo(combo, hour, minute);
2114
2115 if (*hour < 0 || *hour > 23)
2116 return FALSE;
2117 if (*minute < 0 || *minute > 59)
2118 return FALSE;
2119
2120 return TRUE;
2121 }
2122
gtk_calendar_select_today(GtkCalendar * calendar)2123 void gtk_calendar_select_today(GtkCalendar *calendar)
2124 {
2125 time_t t = time (NULL);
2126 struct tm buft;
2127 struct tm *lt = localtime_r (&t, &buft);
2128
2129 mktime(lt);
2130 gtk_calendar_select_day(calendar, lt->tm_mday);
2131 gtk_calendar_select_month(calendar, lt->tm_mon, lt->tm_year + 1900);
2132 }
2133