aboutsummaryrefslogtreecommitdiffstats
path: root/mail/em-utils.c
blob: 0236f2e32952d2b2979d64b66dd126286fb6cb28 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 *  Authors: Jeffrey Stedfast <fejj@ximian.com>
 *
 *  Copyright 2003 Ximian, Inc. (www.ximian.com)
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <time.h>

#include <camel/camel-stream-fs.h>
#include <camel/camel-url-scanner.h>
#include <camel/camel-file-utils.h>

#include "em-filter-editor.h"

#include <bonobo/bonobo-listener.h>
#include <bonobo/bonobo-widget.h>
#include <bonobo/bonobo-event-source.h>

#include <libgnomevfs/gnome-vfs-mime.h>
#include <libgnomevfs/gnome-vfs-mime-utils.h>
#include <libgnomevfs/gnome-vfs-mime-handlers.h>
#include <libgnome/gnome-i18n.h>

#include "mail-component.h"
#include "mail-mt.h"
#include "mail-ops.h"
#include "mail-tools.h"
#include "mail-config.h"
#include "message-tag-followup.h"

#include <e-util/e-mktemp.h>
#include <e-util/e-account-list.h>
#include <e-util/e-dialog-utils.h>
#include "widgets/misc/e-error.h"

#include <gal/util/e-util.h>

#include "em-utils.h"
#include "em-composer-utils.h"
#include "em-format-quote.h"
#include "em-account-editor.h"

static void emu_save_part_done (CamelMimePart *part, char *name, int done, void *data);

extern struct _CamelSession *session;

#define d(x)

/**
 * em_utils_prompt_user:
 * @parent: parent window
 * @promptkey: gconf key to check if we should prompt the user or not.
 * @tag: e_error tag.
 * @arg0: The first of a NULL terminated list of arguments for the error.
 *
 * Convenience function to query the user with a Yes/No dialog and a
 * "Don't show this dialog again" checkbox. If the user checks that
 * checkbox, then @promptkey is set to %FALSE, otherwise it is set to
 * %TRUE.
 *
 * Returns %TRUE if the user clicks Yes or %FALSE otherwise.
 **/
gboolean
em_utils_prompt_user(GtkWindow *parent, const char *promptkey, const char *tag, const char *arg0, ...)
{
    GtkWidget *mbox, *check = NULL;
    va_list ap;
    int button;
    GConfClient *gconf = mail_config_get_gconf_client();

    if (promptkey
        && !gconf_client_get_bool(gconf, promptkey, NULL))
        return TRUE;

    va_start(ap, arg0);
    mbox = e_error_newv(parent, tag, arg0, ap);
    va_end(ap);

    if (promptkey) {
        check = gtk_check_button_new_with_label (_("Don't show this message again."));
        gtk_container_set_border_width((GtkContainer *)check, 12);
        gtk_box_pack_start ((GtkBox *)((GtkDialog *) mbox)->vbox, check, TRUE, TRUE, 0);
        gtk_widget_show (check);
    }
    
    button = gtk_dialog_run ((GtkDialog *) mbox);
    if (promptkey)
        gconf_client_set_bool(gconf, promptkey, !gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(check)), NULL);

    gtk_widget_destroy(mbox);
    
    return button == GTK_RESPONSE_YES;
}

/**
 * em_utils_uids_copy:
 * @uids: array of uids
 *
 * Duplicates the array of uids held by @uids into a new
 * GPtrArray. Use em_utils_uids_free() to free the resultant uid
 * array.
 *
 * Returns a duplicate copy of @uids.
 **/
GPtrArray *
em_utils_uids_copy (GPtrArray *uids)
{
    GPtrArray *copy;
    int i;
    
    copy = g_ptr_array_new ();
    g_ptr_array_set_size (copy, uids->len);
    
    for (i = 0; i < uids->len; i++)
        copy->pdata[i] = g_strdup (uids->pdata[i]);
    
    return copy;
}

/**
 * em_utils_uids_free:
 * @uids: array of uids
 *
 * Frees the array of uids pointed to by @uids back to the system.
 **/
void
em_utils_uids_free (GPtrArray *uids)
{
    int i;
    
    for (i = 0; i < uids->len; i++)
        g_free (uids->pdata[i]);
    
    g_ptr_array_free (uids, TRUE);
}

static void
druid_destroy_cb (gpointer user_data, GObject *deadbeef)
{
    gtk_main_quit ();
}

/**
 * em_utils_configure_account:
 * @parent: parent window for the druid to be a child of.
 *
 * Displays a druid allowing the user to configure an account. If
 * @parent is non-NULL, then the druid will be created as a child
 * window of @parent's toplevel window.
 *
 * Returns %TRUE if an account has been configured or %FALSE
 * otherwise.
 **/
gboolean
em_utils_configure_account (GtkWidget *parent)
{
    EMAccountEditor *emae;

    emae = em_account_editor_new(NULL, EMAE_DRUID);
    if (parent != NULL)
        e_dialog_set_transient_for((GtkWindow *)emae->editor, parent);

    g_object_weak_ref((GObject *)emae->editor, (GWeakNotify) druid_destroy_cb, NULL);
    gtk_widget_show(emae->editor);
    gtk_grab_add(emae->editor);
    gtk_main();
    
    return mail_config_is_configured();
}

/**
 * em_utils_check_user_can_send_mail:
 * @parent: parent window for the druid to be a child of.
 *
 * If no accounts have been configured, the user will be given a
 * chance to configure an account. In the case that no accounts are
 * configured, a druid will be created. If @parent is non-NULL, then
 * the druid will be created as a child window of @parent's toplevel
 * window.
 *
 * Returns %TRUE if the user has an account configured (to send mail)
 * or %FALSE otherwise.
 **/
gboolean
em_utils_check_user_can_send_mail (GtkWidget *parent)
{
    EAccount *account;
    
    if (!mail_config_is_configured ()) {
        if (!em_utils_configure_account (parent))
            return FALSE;
    }
    
    if (!(account = mail_config_get_default_account ()))
        return FALSE;
    
    /* Check for a transport */
    if (!account->transport->url)
        return FALSE;
    
    return TRUE;
}

/* Editing Filters/vFolders... */

static GtkWidget *filter_editor = NULL;

static void
em_filter_editor_response (GtkWidget *dialog, int button, gpointer user_data)
{
    EMFilterContext *fc;
    
    if (button == GTK_RESPONSE_OK) {
        char *user;
        
        fc = g_object_get_data ((GObject *) dialog, "context");
        user = g_strdup_printf ("%s/mail/filters.xml",
                    mail_component_peek_base_directory (mail_component_peek ()));
        rule_context_save ((RuleContext *) fc, user);
        g_free (user);
    }
    
    gtk_widget_destroy (dialog);
    
    filter_editor = NULL;
}

static const char *em_filter_source_element_names[] = {
    "incoming",
    "outgoing",
    NULL,
};

/**
 * em_utils_edit_filters:
 * @parent: parent window
 *
 * Opens or raises the filters editor dialog so that the user may edit
 * his/her filters. If @parent is non-NULL, then the dialog will be
 * created as a child window of @parent's toplevel window.
 **/
void
em_utils_edit_filters (GtkWidget *parent)
{
    const char *base_directory = mail_component_peek_base_directory (mail_component_peek ());
    char *user, *system;
    EMFilterContext *fc;
    
    if (filter_editor) {
        gdk_window_raise (GTK_WIDGET (filter_editor)->window);
        return;
    }
    
    fc = em_filter_context_new ();
    user = g_strdup_printf ("%s/mail/filters.xml", base_directory);
    system = EVOLUTION_PRIVDATADIR "/filtertypes.xml";
    rule_context_load ((RuleContext *) fc, system, user);
    g_free (user);
    
    if (((RuleContext *) fc)->error) {
        e_error_run((GtkWindow *)parent, "mail:filter-load-error", ((RuleContext *)fc)->error, NULL);
        return;
    }
    
    filter_editor = (GtkWidget *) em_filter_editor_new (fc, em_filter_source_element_names);
    if (parent != NULL)
        e_dialog_set_transient_for ((GtkWindow *) filter_editor, parent);
    
    gtk_window_set_title (GTK_WINDOW (filter_editor), _("Filters"));
    g_object_set_data_full ((GObject *) filter_editor, "context", fc, (GtkDestroyNotify) g_object_unref);
    g_signal_connect (filter_editor, "response", G_CALLBACK (em_filter_editor_response), NULL);
    gtk_widget_show (GTK_WIDGET (filter_editor));
}

/* Saving messages... */

static GtkWidget *
emu_get_save_filesel (GtkWidget *parent, const char *title, const char *name)
{
    GtkWidget *filesel;
    const char *dir;
    char *realname, *filename, *gdir;
    GConfClient *gconf;

#ifdef USE_GTKFILECHOOSER
    filesel = gtk_file_chooser_dialog_new (title,
                           NULL,
                           GTK_FILE_CHOOSER_ACTION_SAVE,
                           GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                           GTK_STOCK_SAVE, GTK_RESPONSE_OK,
                           NULL);
    gtk_dialog_set_default_response (GTK_DIALOG (filesel), GTK_RESPONSE_OK);
#else
    filesel = gtk_file_selection_new (title);
#endif
    
    if (parent)
        e_dialog_set_transient_for((GtkWindow *)filesel, parent);

    gconf = gconf_client_get_default();
    dir = gdir = gconf_client_get_string(gconf, "/apps/evolution/mail/save_dir", NULL);
    g_object_unref(gconf);

    if (dir == NULL)
        dir = g_get_home_dir();

    if (name && name[0]) {
        realname = g_strdup (name);
        e_filename_make_safe (realname);
    } else {
        realname = NULL;
    }

#ifdef USE_GTKFILECHOOSER
    gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (filesel), dir);
    
    if (realname)
        gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (filesel), realname);
#else
    filename = g_build_filename (dir, G_DIR_SEPARATOR_S, realname, NULL);
    gtk_file_selection_set_filename (GTK_FILE_SELECTION (filesel), filename);
    g_free (filename);
#endif
    
    g_free (realname);
    g_free (gdir);

    return filesel;
}

static void
emu_update_save_path(const char *filename)
{
    char *dir = g_path_get_dirname(filename);
    GConfClient *gconf = gconf_client_get_default();

    gconf_client_set_string(gconf, "/apps/evolution/mail/save_dir", dir, NULL);
    g_object_unref(gconf);
    g_free(dir);
}

static gboolean
emu_can_save(GtkWindow *parent, const char *path)
{
    struct stat st;
    
    if (!path || path[0] == 0)
        return FALSE;

    /* make sure we can actually save to it... */
    if (stat (path, &st) != -1 && !S_ISREG (st.st_mode))
        return FALSE;
    
    if (access (path, F_OK) == 0) {
        if (access (path, W_OK) != 0) {
            e_error_run(parent, "mail:no-save-path", path, g_strerror(errno), NULL);
            return FALSE;
        }
        
        return e_error_run(parent, E_ERROR_ASK_FILE_EXISTS_OVERWRITE, path, NULL) == GTK_RESPONSE_OK;
    }
    
    return TRUE;
}

static void
emu_save_part_response(GtkWidget *filesel, int response, CamelMimePart *part)
{
    const char *path;
    
    if (response == GTK_RESPONSE_OK) {
#ifdef USE_GTKFILECHOOSER
        path = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (filesel));
#else
        path = gtk_file_selection_get_filename (GTK_FILE_SELECTION (filesel));
#endif
        
        if (!emu_can_save((GtkWindow *)filesel, path))
            return;

        emu_update_save_path(path);
        /* FIXME: popup error if it fails? */
        mail_save_part(part, path, NULL, NULL);
    }

    gtk_widget_destroy((GtkWidget *)filesel);
    camel_object_unref(part);
}

/**
 * em_utils_save_part:
 * @parent: parent window
 * @prompt: prompt string
 * @part: part to save
 *
 * Saves a mime part to disk (prompting the user for filename).
 **/
void
em_utils_save_part(GtkWidget *parent, const char *prompt, CamelMimePart *part)
{
    const char *name;
    GtkWidget *filesel;

    name = camel_mime_part_get_filename(part);
    if (name == NULL) {
        if (CAMEL_IS_MIME_MESSAGE(part)) {
            name = camel_mime_message_get_subject((CamelMimeMessage *)part);
            if (name == NULL)
                name = _("message");
        } else {
            name = _("attachment");
        }
    }

    filesel = emu_get_save_filesel(parent, prompt, name);
    camel_object_ref(part);
    g_signal_connect (filesel, "response", G_CALLBACK (emu_save_part_response), part);
    gtk_widget_show (filesel);
}

/**
 * em_utils_save_part_to_file:
 * @parent: parent window
 * @filename: filename to save to
 * @part: part to save
 * 
 * Save a part's content to a specific file
 * Creates all needed directories and overwrites without prompting
 *
 * Returns %TRUE if saving succeeded, %FALSE otherwise
 **/
gboolean
em_utils_save_part_to_file(GtkWidget *parent, const char *filename, CamelMimePart *part) 
{
    int done;
    char *dirname;
    struct stat st;
    
    if (filename[0] == 0)
        return FALSE;
    
    dirname = g_path_get_dirname(filename);
    if (camel_mkdir(dirname, 0777) == -1) {
        e_error_run((GtkWindow *)parent, "mail:no-create-path", filename, g_strerror(errno), NULL);
        g_free(dirname);
        return FALSE;
    }
    g_free(dirname);

    if (access(filename, F_OK) == 0) {
        if (access(filename, W_OK) != 0) {
            e_error_run((GtkWindow *)parent, E_ERROR_ASK_FILE_EXISTS_OVERWRITE, filename, NULL);
            return FALSE;
        }
    }
    
    if (stat(filename, &st) != -1 && !S_ISREG(st.st_mode)) {
        e_error_run((GtkWindow *)parent, "mail:no-write-path-notfile", filename, NULL);
        return FALSE;
    }
    
    /* FIXME: This doesn't handle default charsets */
    mail_msg_wait(mail_save_part(part, filename, emu_save_part_done, &done));
    
    return done;
}

struct _save_messages_data {
    CamelFolder *folder;
    GPtrArray *uids;
};

static void
emu_save_messages_response(GtkWidget *filesel, int response, struct _save_messages_data *data)
{
    const char *path;
    
    if (response == GTK_RESPONSE_OK) {
#ifdef USE_GTKFILECHOOSER
        path = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (filesel));
#else
        path = gtk_file_selection_get_filename (GTK_FILE_SELECTION (filesel));
#endif

        if (!emu_can_save((GtkWindow *)filesel, path))
            return;

        emu_update_save_path(path);
        mail_save_messages(data->folder, data->uids, path, NULL, NULL);
        data->uids = NULL;
    }

    camel_object_unref(data->folder);
    if (data->uids)
        em_utils_uids_free(data->uids);
    g_free(data);
    gtk_widget_destroy((GtkWidget *)filesel);
}

/**
 * em_utils_save_messages:
 * @parent: parent window
 * @folder: folder containing messages to save
 * @uids: uids of messages to save
 *
 * Saves a group of messages to disk in mbox format (prompting the
 * user for filename).
 **/
void
em_utils_save_messages (GtkWidget *parent, CamelFolder *folder, GPtrArray *uids)
{
    struct _save_messages_data *data;
    GtkWidget *filesel;

    g_return_if_fail (CAMEL_IS_FOLDER (folder));
    g_return_if_fail (uids != NULL);

    filesel = emu_get_save_filesel(parent, _("Save Message..."), NULL);
    camel_object_ref(folder);
    
    data = g_malloc(sizeof(struct _save_messages_data));
    data->folder = folder;
    data->uids = uids;

    g_signal_connect(filesel, "response", G_CALLBACK(emu_save_messages_response), data);
    gtk_widget_show((GtkWidget *)filesel);
}

/* ********************************************************************** */

static void
emu_add_address_cb(BonoboListener *listener, const char *name, const CORBA_any *any, CORBA_Environment *ev, void *data)
{
    char *type = bonobo_event_subtype(name);
    
    if (!strcmp(type, "Destroy"))
        gtk_widget_destroy((GtkWidget *)data);
    
    g_free(type);
}

/**
 * em_utils_add_address:
 * @parent: 
 * @email: 
 * 
 * Add address @email to the addressbook.
 **/
void em_utils_add_address(struct _GtkWidget *parent, const char *email)
{
    CamelInternetAddress *cia;
    GtkWidget *win;
    GtkWidget *control;
    /*GtkWidget *socket;*/
    char *buf;
    
    cia = camel_internet_address_new ();
    if (camel_address_decode ((CamelAddress *) cia, email) == -1) {
        camel_object_unref (cia);
        return;
    }
    
    buf = camel_address_format ((CamelAddress *) cia);
    camel_object_unref (cia);
    
    win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title((GtkWindow *)win, _("Add address"));
    gtk_window_set_transient_for((GtkWindow *)win, ((GtkWindow *)parent));
    gtk_window_set_position((GtkWindow *)win, GTK_WIN_POS_CENTER_ON_PARENT);
    gtk_window_set_type_hint((GtkWindow *)win, GDK_WINDOW_TYPE_HINT_DIALOG);

    control = bonobo_widget_new_control("OAFIID:GNOME_Evolution_Addressbook_AddressPopup:" BASE_VERSION, CORBA_OBJECT_NIL);
    bonobo_widget_set_property((BonoboWidget *)control, "email", TC_CORBA_string, buf, NULL);
    g_free (buf);
    
    bonobo_event_source_client_add_listener(bonobo_widget_get_objref((BonoboWidget *)control), emu_add_address_cb, NULL, NULL, win);
    
    /*socket = find_socket (GTK_CONTAINER (control));
      g_object_weak_ref ((GObject *) socket, (GWeakNotify) gtk_widget_destroy, win);*/

    gtk_container_add((GtkContainer *)win, control);
    gtk_widget_show_all(win);
}

/* ********************************************************************** */
/* Flag-for-Followup... */

/* tag-editor callback data */
struct ted_t {
    MessageTagEditor *editor;
    CamelFolder *folder;
    GPtrArray *uids;
};

static void
ted_free (struct ted_t *ted)
{
    camel_object_unref (ted->folder);
    em_utils_uids_free (ted->uids);
    g_free (ted);
}

static void
tag_editor_response (GtkWidget *dialog, int button, struct ted_t *ted)
{
    CamelFolder *folder;
    CamelTag *tags, *t;
    GPtrArray *uids;
    int i;
    
    if (button == GTK_RESPONSE_OK && (tags = message_tag_editor_get_tag_list (ted->editor))) {
        folder = ted->folder;
        uids = ted->uids;
        
        camel_folder_freeze (folder);
        for (i = 0; i < uids->len; i++) {
            for (t = tags; t; t = t->next)
                camel_folder_set_message_user_tag (folder, uids->pdata[i], t->name, t->value);
        }
        
        camel_folder_thaw (folder);
        camel_tag_list_free (&tags);
    }
    
    gtk_widget_destroy (dialog);
}

/**
 * em_utils_flag_for_followup:
 * @parent: parent window
 * @folder: folder containing messages to flag
 * @uids: uids of messages to flag
 *
 * Open the Flag-for-Followup editor for the messages specified by
 * @folder and @uids.
 **/
void
em_utils_flag_for_followup (GtkWidget *parent, CamelFolder *folder, GPtrArray *uids)
{
    GtkWidget *editor;
    struct ted_t *ted;
    int i;
    
    g_return_if_fail (CAMEL_IS_FOLDER (folder));
    g_return_if_fail (uids != NULL);
    
    editor = (GtkWidget *) message_tag_followup_new ();
    
    if (parent != NULL)
        e_dialog_set_transient_for ((GtkWindow *) editor, parent);
    
    camel_object_ref (folder);
    
    ted = g_new (struct ted_t, 1);
    ted->editor = MESSAGE_TAG_EDITOR (editor);
    ted->folder = folder;
    ted->uids = uids;
    
    for (i = 0; i < uids->len; i++) {
        CamelMessageInfo *info;
        
        info = camel_folder_get_message_info (folder, uids->pdata[i]);
        message_tag_followup_append_message (MESSAGE_TAG_FOLLOWUP (editor),
                             camel_message_info_from (info),
                             camel_message_info_subject (info));
    }
    
    /* special-case... */
    if (uids->len == 1) {
        CamelMessageInfo *info;
        
        info = camel_folder_get_message_info (folder, uids->pdata[0]);
        if (info) {
            const CamelTag *tags = camel_message_info_user_tags(info);

            if (tags)
                message_tag_editor_set_tag_list (MESSAGE_TAG_EDITOR (editor), tags);
            camel_folder_free_message_info (folder, info);
        }
    }
    
    g_signal_connect (editor, "response", G_CALLBACK (tag_editor_response), ted);
    g_object_weak_ref ((GObject *) editor, (GWeakNotify) ted_free, ted);
    
    gtk_widget_show (editor);
}

/**
 * em_utils_flag_for_followup_clear:
 * @parent: parent window
 * @folder: folder containing messages to unflag
 * @uids: uids of messages to unflag
 *
 * Clears the Flag-for-Followup flag on the messages referenced by
 * @folder and @uids.
 **/
void
em_utils_flag_for_followup_clear (GtkWidget *parent, CamelFolder *folder, GPtrArray *uids)
{
    int i;
    
    g_return_if_fail (CAMEL_IS_FOLDER (folder));
    g_return_if_fail (uids != NULL);
    
    camel_folder_freeze (folder);
    for (i = 0; i < uids->len; i++) {
        camel_folder_set_message_user_tag (folder, uids->pdata[i], "follow-up", "");
        camel_folder_set_message_user_tag (folder, uids->pdata[i], "due-by", "");
        camel_folder_set_message_user_tag (folder, uids->pdata[i], "completed-on", "");
    }
    camel_folder_thaw (folder);
    
    em_utils_uids_free (uids);
}

/**
 * em_utils_flag_for_followup_completed:
 * @parent: parent window
 * @folder: folder containing messages to 'complete'
 * @uids: uids of messages to 'complete'
 *
 * Sets the completed state (and date/time) for each message
 * referenced by @folder and @uids that is marked for
 * Flag-for-Followup.
 **/
void
em_utils_flag_for_followup_completed (GtkWidget *parent, CamelFolder *folder, GPtrArray *uids)
{
    char *now;
    int i;
    
    g_return_if_fail (CAMEL_IS_FOLDER (folder));
    g_return_if_fail (uids != NULL);
    
    now = camel_header_format_date (time (NULL), 0);
    
    camel_folder_freeze (folder);
    for (i = 0; i < uids->len; i++) {
        const char *tag;
        
        tag = camel_folder_get_message_user_tag (folder, uids->pdata[i], "follow-up");
        if (tag == NULL || *tag == '\0')
            continue;
        
        camel_folder_set_message_user_tag (folder, uids->pdata[i], "completed-on", now);
    }
    camel_folder_thaw (folder);
    
    g_free (now);
    
    em_utils_uids_free (uids);
}

#include "camel/camel-stream-mem.h"
#include "camel/camel-stream-filter.h"
#include "camel/camel-mime-filter-from.h"

/* This kind of sucks, because for various reasons most callers need to run synchronously
   in the gui thread, however this could take a long, blocking time, to run */
static int
em_utils_write_messages_to_stream(CamelFolder *folder, GPtrArray *uids, CamelStream *stream)
{
    CamelStreamFilter *filtered_stream;
    CamelMimeFilterFrom *from_filter;
    int i, res = 0;

    from_filter = camel_mime_filter_from_new();
    filtered_stream = camel_stream_filter_new_with_stream(stream);
    camel_stream_filter_add(filtered_stream, (CamelMimeFilter *)from_filter);
    camel_object_unref(from_filter);
    
    for (i=0; i<uids->len; i++) {
        CamelMimeMessage *message;
        char *from;

        message = camel_folder_get_message(folder, uids->pdata[i], NULL);
        if (message == NULL) {
            res = -1;
            break;
        }

        /* we need to flush after each stream write since we are writing to the same stream */
        from = camel_mime_message_build_mbox_from(message);

        if (camel_stream_write_string(stream, from) == -1
            || camel_stream_flush(stream) == -1
            || camel_data_wrapper_write_to_stream((CamelDataWrapper *)message, (CamelStream *)filtered_stream) == -1
            || camel_stream_flush((CamelStream *)filtered_stream) == -1)
            res = -1;
        
        g_free(from);
        camel_object_unref(message);

        if (res == -1)
            break;
    }

    camel_object_unref(filtered_stream);

    return res;
}

/* This kind of sucks, because for various reasons most callers need to run synchronously
   in the gui thread, however this could take a long, blocking time, to run */
static int
em_utils_read_messages_from_stream(CamelFolder *folder, CamelStream *stream)
{
    CamelException *ex = camel_exception_new();
    CamelMimeParser *mp = camel_mime_parser_new();
    int res = -1;

    camel_mime_parser_scan_from(mp, TRUE);
    camel_mime_parser_init_with_stream(mp, stream);

    while (camel_mime_parser_step(mp, 0, 0) == CAMEL_MIME_PARSER_STATE_FROM) {
        CamelMimeMessage *msg;

        /* NB: de-from filter, once written */
        msg = camel_mime_message_new();
        if (camel_mime_part_construct_from_parser((CamelMimePart *)msg, mp) == -1) {
            camel_object_unref(msg);
            break;
        }
        
        camel_folder_append_message(folder, msg, NULL, NULL, ex);
        camel_object_unref(msg);
        
        if (camel_exception_is_set (ex))
            break;
        
        camel_mime_parser_step(mp, 0, 0);
    }
    
    camel_object_unref(mp);
    if (!camel_exception_is_set(ex))
        res = 0;
    camel_exception_free(ex);

    return res;
}

/**
 * em_utils_selection_set_mailbox:
 * @data: selection data
 * @folder: folder containign messages to copy into the selection
 * @uids: uids of the messages to copy into the selection
 * 
 * Creates a mailbox-format selection.
 * Warning: Could be BIG!
 * Warning: This could block the ui for an extended period.
 **/
void
em_utils_selection_set_mailbox(GtkSelectionData *data, CamelFolder *folder, GPtrArray *uids)
{
    CamelStream *stream;

    stream = camel_stream_mem_new();
    if (em_utils_write_messages_to_stream(folder, uids, stream) == 0)
        gtk_selection_data_set(data, data->target, 8,
                       ((CamelStreamMem *)stream)->buffer->data,
                       ((CamelStreamMem *)stream)->buffer->len);

    camel_object_unref(stream);
}

/**
 * em_utils_selection_get_mailbox:
 * @data: selection data
 * @folder: 
 * 
 * Receive a mailbox selection/dnd
 * Warning: Could be BIG!
 * Warning: This could block the ui for an extended period.
 * FIXME: Exceptions?
 **/
void
em_utils_selection_get_mailbox(GtkSelectionData *data, CamelFolder *folder)
{
    CamelStream *stream;

    if (data->data == NULL || data->length == -1)
        return;

    /* TODO: a stream mem with read-only access to existing data? */
    /* NB: Although copying would let us run this async ... which it should */
    stream = camel_stream_mem_new_with_buffer(data->data, data->length);
    em_utils_read_messages_from_stream(folder, stream);
    camel_object_unref(stream);
}

/**
 * em_utils_selection_get_message:
 * @data: 
 * @folder: 
 * 
 * get a message/rfc822 data.
 **/
void
em_utils_selection_get_message(GtkSelectionData *data, CamelFolder *folder)
{
    CamelStream *stream;
    CamelException *ex;
    CamelMimeMessage *msg;

    if (data->data == NULL || data->length == -1)
        return;

    ex = camel_exception_new();
    stream = camel_stream_mem_new_with_buffer(data->data, data->length);
    msg = camel_mime_message_new();
    if (camel_data_wrapper_construct_from_stream((CamelDataWrapper *)msg, stream) == 0)
        camel_folder_append_message(folder, msg, NULL, NULL, ex);
    camel_object_unref(msg);
    camel_object_unref(stream);
    camel_exception_free(ex);
}

/**
 * em_utils_selection_set_uidlist:
 * @data: selection data
 * @uri:
 * @uids: 
 * 
 * Sets a "x-uid-list" format selection data.
 *
 * FIXME: be nice if this could take a folder argument rather than uri
 **/
void
em_utils_selection_set_uidlist(GtkSelectionData *data, const char *uri, GPtrArray *uids)
{
    GByteArray *array = g_byte_array_new();
    int i;

    /* format: "uri\0uid1\0uid2\0uid3\0...\0uidn\0" */
    
    g_byte_array_append(array, uri, strlen(uri)+1);

    for (i=0; i<uids->len; i++)
        g_byte_array_append(array, uids->pdata[i], strlen(uids->pdata[i])+1);
        
    gtk_selection_data_set(data, data->target, 8, array->data, array->len);
    g_byte_array_free(array, TRUE);
}

/**
 * em_utils_selection_get_uidlist:
 * @data: selection data
 * @move: do we delete the messages.
 * 
 * Convert a uid list into a copy/move operation.
 * 
 * Warning: Could take some time to run.
 **/
void
em_utils_selection_get_uidlist(GtkSelectionData *data, CamelFolder *dest, int move, CamelException *ex)
{
    /* format: "uri\0uid1\0uid2\0uid3\0...\0uidn" */
    char *inptr, *inend;
    GPtrArray *uids;
    CamelFolder *folder;

    if (data == NULL || data->data == NULL || data->length == -1)
        return;
    
    uids = g_ptr_array_new();

    inptr = data->data;
    inend = data->data + data->length;
    while (inptr < inend) {
        char *start = inptr;

        while (inptr < inend && *inptr)
            inptr++;

        if (start > (char *)data->data)
            g_ptr_array_add(uids, g_strndup(start, inptr-start));

        inptr++;
    }

    if (uids->len == 0) {
        g_ptr_array_free(uids, TRUE);
        return;
    }

    folder = mail_tool_uri_to_folder(data->data, 0, ex);
    if (folder) {
        camel_folder_transfer_messages_to(folder, uids, dest, NULL, move, ex);
        camel_object_unref(folder);
    }

    em_utils_uids_free(uids);
}

/**
 * em_utils_selection_set_urilist:
 * @data: 
 * @folder: 
 * @uids: 
 * 
 * Set the selection data @data to a uri which points to a file, which is
 * a berkely mailbox format mailbox.  The file is automatically cleaned
 * up when the application quits.
 **/
void
em_utils_selection_set_urilist(GtkSelectionData *data, CamelFolder *folder, GPtrArray *uids)
{
    char *tmpdir;
    CamelStream *fstream;
    char *uri, *p, *file = NULL;
    int fd;
    CamelMessageInfo *info;

    tmpdir = e_mkdtemp("drag-n-drop-XXXXXX");
    if (tmpdir == NULL)
        return;

    /* Try to get the drop filename from the message or folder */
    if (uids->len == 1) {
        info = camel_folder_get_message_info(folder, uids->pdata[0]);
        if (info) {
            file = g_strdup(camel_message_info_subject(info));
            camel_folder_free_message_info(folder, info);
        }
    }

    /* TODO: Handle conflicts? */
    if (file == NULL) {
        /* Drop filename for messages from a mailbox */
        file = g_strdup_printf(_("Messages from %s"), folder->name);
    }

    e_filename_make_safe(file);

    p = uri = g_alloca (strlen (tmpdir) + strlen(file) + 16);
    p += sprintf (uri, "file:///%s/%s", tmpdir, file);
    g_free(tmpdir);
    g_free(file);
    
    fd = open(uri + 7, O_WRONLY | O_CREAT | O_EXCL, 0666);
    if (fd == -1)
        return;
    
    fstream = camel_stream_fs_new_with_fd(fd);
    if (fstream) {
        /* terminate with \r\n to be compliant with the spec */
        strcpy (p, "\r\n");
        
        if (em_utils_write_messages_to_stream(folder, uids, fstream) == 0)
            gtk_selection_data_set(data, data->target, 8, uri, strlen(uri));

        camel_object_unref(fstream);
    }
}

/**
 * em_utils_selection_set_urilist:
 * @data: 
 * @folder: 
 * @uids: 
 * 
 * Get the selection data @data from a uri list which points to a
 * file, which is a berkely mailbox format mailbox.  The file is
 * automatically cleaned up when the application quits.
 **/
void
em_utils_selection_get_urilist(GtkSelectionData *data, CamelFolder *folder)
{
    CamelStream *stream;
    CamelURL *url;
    int fd, i, res = 0;
    char *tmp, **uris;

    d(printf(" * drop uri list\n"));

    tmp = g_strndup(data->data, data->length);
    uris = g_strsplit(tmp, "\n", 0);
    g_free(tmp);
    for (i=0;res == 0 && uris[i];i++) {
        g_strstrip(uris[i]);
        if (uris[i][0] == '#')
            continue;

        url = camel_url_new(uris[i], NULL);
        if (url == NULL)
            continue;

        if (strcmp(url->protocol, "file") == 0
            && (fd = open(url->path, O_RDONLY)) != -1) {
            stream = camel_stream_fs_new_with_fd(fd);
            res = em_utils_read_messages_from_stream(folder, stream);
            camel_object_unref(stream);
        }
        camel_url_free(url);
    }

    g_strfreev(uris);
}

static void
emu_save_part_done(CamelMimePart *part, char *name, int done, void *data)
{
    ((int *)data)[0] = done;
}

/**
 * em_utils_temp_save_part:
 * @parent: 
 * @part: 
 * 
 * Save a part's content to a temporary file, and return the
 * filename.
 * 
 * Return value: NULL if anything failed.
 **/
char *
em_utils_temp_save_part(GtkWidget *parent, CamelMimePart *part)
{
    const char *filename;
    char *tmpdir, *path, *mfilename = NULL;
    int done;

    tmpdir = e_mkdtemp("evolution-tmp-XXXXXX");
    if (tmpdir == NULL) {
        e_error_run((GtkWindow *)parent, "mail:no-create-tmp-path", g_strerror(errno), NULL);
        return NULL;
    }

    filename = camel_mime_part_get_filename (part);
    if (filename == NULL) {
        /* This is the default filename used for temporary file creation */
        filename = _("Unknown");
    } else {
        mfilename = g_strdup(filename);
        e_filename_make_safe(mfilename);
        filename = mfilename;
    }

    path = g_build_filename(tmpdir, filename, NULL);
    g_free(tmpdir);
    g_free(mfilename);

    /* FIXME: This doesn't handle default charsets */
    mail_msg_wait(mail_save_part(part, path, emu_save_part_done, &done));

    if (!done) {
        /* mail_save_part should popup an error box automagically */
        g_free(path);
        path = NULL;
    }

    return path;
}


/**
 * em_utils_folder_is_drafts:
 * @folder: folder
 * @uri: uri for this folder, if known
 *
 * Decides if @folder is a Drafts folder.
 * 
 * Returns %TRUE if this is a Drafts folder or %FALSE otherwise.
 **/
gboolean
em_utils_folder_is_drafts(CamelFolder *folder, const char *uri)
{
    EAccountList *accounts;
    EAccount *account;
    EIterator *iter;
    int is = FALSE;
    char *drafts_uri;
    
    if (folder == mail_component_get_folder(NULL, MAIL_COMPONENT_FOLDER_DRAFTS))
        return TRUE;

    if (uri == NULL)
        return FALSE;
    
    accounts = mail_config_get_accounts();
    iter = e_list_get_iterator((EList *)accounts);
    while (e_iterator_is_valid(iter)) {
        account = (EAccount *)e_iterator_get(iter);
        
        if (account->drafts_folder_uri) {
            drafts_uri = em_uri_to_camel (account->drafts_folder_uri);
            if (camel_store_folder_uri_equal (folder->parent_store, drafts_uri, uri)) {
                g_free (drafts_uri);
                is = TRUE;
                break;
            }
            g_free (drafts_uri);
        }
        
        e_iterator_next(iter);
    }
    
    g_object_unref(iter);
    
    return is;
}

/**
 * em_utils_folder_is_sent:
 * @folder: folder
 * @uri: uri for this folder, if known
 *
 * Decides if @folder is a Sent folder
 * 
 * Returns %TRUE if this is a Sent folder or %FALSE otherwise.
 **/
gboolean
em_utils_folder_is_sent(CamelFolder *folder, const char *uri)
{
    EAccountList *accounts;
    EAccount *account;
    EIterator *iter;
    int is = FALSE;
    char *sent_uri;
    
    if (folder == mail_component_get_folder(NULL, MAIL_COMPONENT_FOLDER_SENT))
        return TRUE;
    
    if (uri == NULL)
        return FALSE;
    
    accounts = mail_config_get_accounts();
    iter = e_list_get_iterator((EList *)accounts);
    while (e_iterator_is_valid(iter)) {
        account = (EAccount *)e_iterator_get(iter);
        
        if (account->sent_folder_uri) {
            sent_uri = em_uri_to_camel (account->sent_folder_uri);
            if (camel_store_folder_uri_equal (folder->parent_store, sent_uri, uri)) {
                g_free (sent_uri);
                is = TRUE;
                break;
            }
            g_free (sent_uri);
        }
        
        e_iterator_next(iter);
    }
    
    g_object_unref(iter);
    
    return is;
}

/**
 * em_utils_folder_is_outbox:
 * @folder: folder
 * @uri: uri for this folder, if known
 *
 * Decides if @folder is an Outbox folder
 * 
 * Returns %TRUE if this is an Outbox folder or %FALSE otherwise.
 **/
gboolean
em_utils_folder_is_outbox(CamelFolder *folder, const char *uri)
{
    /* <Highlander>There can be only one.</Highlander> */
    return folder == mail_component_get_folder(NULL, MAIL_COMPONENT_FOLDER_OUTBOX);
}

/**
 * em_utils_adjustment_page:
 * @adj: 
 * @down: 
 * 
 * Move an adjustment up/down forward/back one page.
 **/
void
em_utils_adjustment_page(GtkAdjustment *adj, gboolean down)
{
    float page_size = adj->page_size - adj->step_increment;

    if (down) {
        if (adj->value < adj->upper - adj->page_size - page_size)
            adj->value += page_size;
        else if (adj->upper >= adj->page_size)
            adj->value = adj->upper - adj->page_size;
        else
            adj->value = adj->lower;
    } else {
        if (adj->value > adj->lower + page_size)
            adj->value -= page_size;
        else
            adj->value = adj->lower;
    }

    gtk_adjustment_value_changed(adj);
}

/* ********************************************************************** */
static char *emu_proxy_uri;
static int emu_proxy_init = 0;
static pthread_mutex_t emu_proxy_lock = PTHREAD_MUTEX_INITIALIZER;

static void
emu_set_proxy(GConfClient *client, int needlock)
{
    char *server, *uri = NULL;
    int port;

    if (gconf_client_get_bool(client, "/system/http_proxy/use_http_proxy", NULL)) {
        server = gconf_client_get_string(client, "/system/http_proxy/host", NULL);
        port = gconf_client_get_int(client, "/system/http_proxy/port", NULL);

        if (server && server[0]) {
            if (gconf_client_get_bool(client, "/system/http_proxy/use_authentication", NULL)) {
                char *user = gconf_client_get_string(client, "/system/http_proxy/authentication_user", NULL);
                char *pass = gconf_client_get_string(client, "/system/http_proxy/authentication_password", NULL);

                uri = g_strdup_printf("http://%s:%s@%s:%d", user, pass, server, port);
                g_free(user);
                g_free(pass);
            } else {
                uri = g_strdup_printf("http://%s:%d", server, port);
            }
        }

        g_free(server);
    }

    if (needlock)
        pthread_mutex_lock(&emu_proxy_lock);

    g_free(emu_proxy_uri);
    emu_proxy_uri = uri;

    if (needlock)
        pthread_mutex_unlock(&emu_proxy_lock);

}

static void
emu_proxy_changed(GConfClient *client, guint32 cnxn_id, GConfEntry *entry, gpointer user_data)
{
    emu_set_proxy(client, TRUE);
}

static void *
emu_proxy_setup(void *data)
{
    GConfClient *client = gconf_client_get_default();

    gconf_client_add_dir(client, "/system/http_proxy", GCONF_CLIENT_PRELOAD_ONELEVEL, NULL);
    gconf_client_notify_add(client, "/system/http_proxy", emu_proxy_changed, NULL, NULL, NULL);
    emu_set_proxy(client, FALSE);
    g_object_unref(client);

    return NULL;
}

/**
 * em_utils_get_proxy_uri:
 * 
 * Get the system proxy uri.
 * 
 * Return value: Must be freed when finished with.
 **/
char *
em_utils_get_proxy_uri(void)
{
    char *uri;

    pthread_mutex_lock(&emu_proxy_lock);

    if (!emu_proxy_init) {
        mail_call_main(MAIL_CALL_p_p, emu_proxy_setup, NULL);
        emu_proxy_init = TRUE;
    }

    uri = g_strdup(emu_proxy_uri);

    pthread_mutex_unlock(&emu_proxy_lock);

    return uri;
}

/**
 * em_utils_part_to_html:
 * @part:
 *
 * Converts a mime part's contents into html text.  If @credits is given,
 * then it will be used as an attribution string, and the
 * content will be cited.  Otherwise no citation or attribution
 * will be performed.
 * 
 * Return Value: The part in displayable html format.
 **/
char *
em_utils_part_to_html(CamelMimePart *part, ssize_t *len, EMFormat *source)
{
    EMFormatQuote *emfq;
    CamelStreamMem *mem;
    GByteArray *buf;
    char *text;
    
    buf = g_byte_array_new ();
    mem = (CamelStreamMem *) camel_stream_mem_new ();
    camel_stream_mem_set_byte_array (mem, buf);
    
    emfq = em_format_quote_new(NULL, (CamelStream *)mem, 0);
    em_format_set_session((EMFormat *)emfq, session);
    if (source) {
        /* copy over things we can, other things are internal, perhaps need different api than 'clone' */
        if (source->default_charset)
            em_format_set_default_charset((EMFormat *)emfq, source->default_charset);
        if (source->charset)
            em_format_set_default_charset((EMFormat *)emfq, source->charset);
    }
    em_format_part((EMFormat *) emfq, (CamelStream *)mem, part);
    g_object_unref(emfq);

    camel_stream_write((CamelStream *) mem, "", 1);
    camel_object_unref(mem);

    text = buf->data;
    if (len)
        *len = buf->len-1;
    g_byte_array_free (buf, FALSE);

    return text;
}

/**
 * em_utils_message_to_html:
 * @message:
 * @credits: 
 * @flags: EMFormatQuote flags
 * @len:
 * @source:
 *
 * Convert a message to html, quoting if the @credits attribution
 * string is given.
 * 
 * Return value: The html version.
 **/
char *
em_utils_message_to_html(CamelMimeMessage *message, const char *credits, guint32 flags, ssize_t *len, EMFormat *source)
{
    EMFormatQuote *emfq;
    CamelStreamMem *mem;
    GByteArray *buf;
    char *text;

    buf = g_byte_array_new ();
    mem = (CamelStreamMem *) camel_stream_mem_new ();
    camel_stream_mem_set_byte_array (mem, buf);

    emfq = em_format_quote_new(credits, (CamelStream *)mem, flags);
    em_format_set_session((EMFormat *)emfq, session);
    
    if (!source) {
        GConfClient *gconf;
        char *charset;
        
        /* FIXME: we should be getting this from the current view, not the global setting. */
        gconf = gconf_client_get_default ();
        charset = gconf_client_get_string (gconf, "/apps/evolution/mail/display/charset", NULL);
        em_format_set_default_charset ((EMFormat *) emfq, charset);
        g_object_unref (gconf);
        g_free (charset);
    }
    
    em_format_format_clone((EMFormat *)emfq, NULL, NULL, message, source);
    g_object_unref (emfq);

    camel_stream_write((CamelStream *)mem, "", 1);
    camel_object_unref(mem);

    text = buf->data;
    if (len)
        *len = buf->len-1;
    g_byte_array_free(buf, FALSE);

    return text;
}

/* ********************************************************************** */

/**
 * em_utils_expunge_folder:
 * @parent: parent window
 * @folder: folder to expunge
 *
 * Expunges @folder.
 **/
void
em_utils_expunge_folder (GtkWidget *parent, CamelFolder *folder)
{
    char *name;

    camel_object_get(folder, NULL, CAMEL_OBJECT_DESCRIPTION, &name, 0);

    if (!em_utils_prompt_user ((GtkWindow *) parent, "/apps/evolution/mail/prompts/expunge", "mail:ask-expunge", name, NULL))
        return;
    
    mail_expunge_folder(folder, NULL, NULL);
}

/**
 * em_utils_empty_trash:
 * @parent: parent window
 *
 * Empties all Trash folders.
 **/
void
em_utils_empty_trash (GtkWidget *parent)
{
    CamelProvider *provider;
    EAccountList *accounts;
    EAccount *account;
    EIterator *iter;
    CamelException ex;
    
    if (!em_utils_prompt_user((GtkWindow *) parent, "/apps/evolution/mail/prompts/empty_trash", "mail:ask-empty-trash", NULL))
        return;
    
    camel_exception_init (&ex);
    
    /* expunge all remote stores */
    accounts = mail_config_get_accounts ();
    iter = e_list_get_iterator ((EList *) accounts);
    while (e_iterator_is_valid (iter)) {
        account = (EAccount *) e_iterator_get (iter);
        
        /* make sure this is a valid source */
        if (account->enabled && account->source->url) {
            provider = camel_provider_get(account->source->url, &ex);
            if (provider) {
                /* make sure this store is a remote store */
                if (provider->flags & CAMEL_PROVIDER_IS_STORAGE &&
                    provider->flags & CAMEL_PROVIDER_IS_REMOTE) {
                    mail_empty_trash (account, NULL, NULL);
                }
            }
            
            /* clear the exception for the next round */
            camel_exception_clear (&ex);
        }
        
        e_iterator_next (iter);
    }
    
    g_object_unref (iter);
    
    /* Now empty the local trash folder */
    mail_empty_trash (NULL, NULL, NULL);
}

char *
em_utils_folder_name_from_uri (const char *uri)
{
    CamelURL *url;
    char *folder_name = NULL;
    
    if (uri == NULL || (url = camel_url_new (uri, NULL)) == NULL)
        return NULL;
    
    if (url->fragment)
        folder_name = url->fragment;
    else if (url->path)
        folder_name = url->path + 1;
    
    if (folder_name == NULL) {
        camel_url_free (url);
        return NULL;
    }
    
    folder_name = g_strdup (folder_name);
    camel_url_free (url);
    
    return folder_name;
}

/* email: uri's are based on the account, with special cases for local
 * stores, vfolder and local mail.
 * e.g.
 *  imap account imap://user@host/ -> email://accountid@accountid.host/
 *  vfolder      vfolder:/storage/path#folder -> email://vfolder@local/folder
 *  local        local:/storage/path#folder   -> email://local@local/folder
 */

char *em_uri_from_camel(const char *curi)
{
    CamelURL *curl;
    EAccount *account;
    const char *uid, *path;
    char *euri, *tmp;
    CamelProvider *provider;
    CamelException ex;

    /* Easiest solution to code that shouldnt be calling us */
    if (!strncmp(curi, "email:", 6))
        return g_strdup(curi);

    camel_exception_init(&ex);
    provider = camel_provider_get(curi, &ex);
    if (provider == NULL) {
        camel_exception_clear(&ex);
        d(printf("em uri from camel failed '%s'\n", curi));
        return g_strdup(curi);
    }

    curl = camel_url_new(curi, &ex);
    camel_exception_clear(&ex);
    if (curl == NULL)
        return g_strdup(curi);

    if (strcmp(curl->protocol, "vfolder") == 0)
        uid = "vfolder@local";
    else if ((account = mail_config_get_account_by_source_url(curi)) == NULL)
        uid = "local@local";
    else
        uid = account->uid;
    path = (provider->url_flags & CAMEL_URL_FRAGMENT_IS_PATH)?curl->fragment:curl->path;
    if (path) {
        if (path[0] == '/')
            path++;

        tmp = camel_url_encode(path, ";?");
        euri = g_strdup_printf("email://%s/%s", uid, tmp);
        g_free(tmp);
    } else {
        euri = g_strdup_printf("email://%s/", uid);
    }
    
    d(printf("em uri from camel '%s' -> '%s'\n", curi, euri));

    camel_url_free(curl);

    return euri;
}

char *em_uri_to_camel(const char *euri)
{
    EAccountList *accounts;
    const EAccount *account;
    EAccountService *service;
    CamelProvider *provider;
    CamelURL *eurl, *curl;
    char *uid, *curi;

    if (strncmp(euri, "email:", 6) != 0) {
        d(printf("em uri to camel not euri '%s'\n", euri));
        return g_strdup(euri);
    }

    eurl = camel_url_new(euri, NULL);
    if (eurl == NULL)
        return g_strdup(euri);

    g_assert(eurl->host != NULL);

    if (eurl->user != NULL) {
        /* Sigh, shoul'dve used mbox@local for mailboxes, not local@local */
        if (strcmp(eurl->host, "local") == 0
            && (strcmp(eurl->user, "local") == 0 || strcmp(eurl->user, "vfolder") == 0)) {
            char *base;

            if (strcmp(eurl->user, "vfolder") == 0)
                curl = camel_url_new("vfolder:", NULL);
            else
                curl = camel_url_new("mbox:", NULL);

            base = g_strdup_printf("%s/.evolution/mail/%s", g_get_home_dir(), eurl->user);
            camel_url_set_path(curl, base);
            g_free(base);
            camel_url_set_fragment(curl, eurl->path[0]=='/'?eurl->path+1:eurl->path);
            curi = camel_url_to_string(curl, 0);
            camel_url_free(curl);
            camel_url_free(eurl);

            d(printf("em uri to camel local '%s' -> '%s'\n", euri, curi));
            return curi;
        }

        uid = g_strdup_printf("%s@%s", eurl->user, eurl->host);
    } else {
        uid = g_strdup(eurl->host);
    }

    accounts = mail_config_get_accounts();
    account = e_account_list_find(accounts, E_ACCOUNT_FIND_UID, uid);
    g_free(uid);

    if (account == NULL) {
        camel_url_free(eurl);
        d(printf("em uri to camel no account '%s' -> '%s'\n", euri, euri));
        return g_strdup(euri);
    }

    service = account->source;
    if (!(provider = camel_provider_get (service->url, NULL)))
        return g_strdup (euri);
    
    curl = camel_url_new(service->url, NULL);
    if (provider->url_flags & CAMEL_URL_FRAGMENT_IS_PATH)
        camel_url_set_fragment(curl, eurl->path[0]=='/'?eurl->path+1:eurl->path);
    else
        camel_url_set_path(curl, eurl->path);

    curi = camel_url_to_string(curl, 0);

    camel_url_free(eurl);
    camel_url_free(curl);

    d(printf("em uri to camel '%s' -> '%s'\n", euri, curi));

    return curi;
}

/* ********************************************************************** */
#include <libebook/e-book.h>

struct _addr_node {
    char *addr;
    time_t stamp;
    int found;
};

#define EMU_ADDR_CACHE_TIME (60*30) /* in seconds */

static pthread_mutex_t emu_addr_lock = PTHREAD_MUTEX_INITIALIZER;
static ESourceList *emu_addr_list;
static GHashTable *emu_addr_cache;

/* runs sync, in main thread */
static void *
emu_addr_setup(void *dummy)
{
    GError *err = NULL;

    emu_addr_cache = g_hash_table_new(g_str_hash, g_str_equal);

    if (!e_book_get_addressbooks(&emu_addr_list, &err))
        g_error_free(err);

    return NULL;
}

static void
emu_addr_cancel_book(void *data)
{
    EBook *book = data;
    GError *err = NULL;

    /* we dunna care if this fails, its just the best we can try */
    e_book_cancel(book, &err);
    g_clear_error(&err);
}

gboolean
em_utils_in_addressbook(CamelInternetAddress *iaddr)
{
    GError *err = NULL;
    GSList *s, *g, *addr_sources = NULL;
    int stop = FALSE, found = FALSE;
    EBookQuery *query;
    const char *addr;
    struct _addr_node *node;
    time_t now;

    /* TODO: check all addresses? */
    if (!camel_internet_address_get(iaddr, 0, NULL, &addr))
        return FALSE;

    pthread_mutex_lock(&emu_addr_lock);

    if (emu_addr_cache == NULL) {
        mail_call_main(MAIL_CALL_p_p, emu_addr_setup, NULL);
    }

    if (emu_addr_list == NULL) {
        pthread_mutex_unlock(&emu_addr_lock);
        return FALSE;
    }

    now = time(0);

    d(printf("Checking '%s' is in addressbook", addr));

    node = g_hash_table_lookup(emu_addr_cache, addr);
    if (node) {
        d(printf(" -> cached, found %s\n", node->found?"yes":"no"));
        if (node->stamp + EMU_ADDR_CACHE_TIME > now) {
            found = node->found;
            pthread_mutex_unlock(&emu_addr_lock);
            return found;
        }
        d(printf("    but expired!\n"));
    } else {
        d(printf(" -> not found in cache\n"));
        node = g_malloc0(sizeof(*node));
        node->addr = g_strdup(addr);
        g_hash_table_insert(emu_addr_cache, node->addr, node);
    }

    query = e_book_query_field_test(E_CONTACT_EMAIL, E_BOOK_QUERY_IS, addr);

    /* FIXME: this aint threadsafe by any measure, but what can you do eh??? */

    for (g = e_source_list_peek_groups(emu_addr_list);g;g=g_slist_next(g)) {
        for (s = e_source_group_peek_sources((ESourceGroup *)g->data);s;s=g_slist_next(s)) {
            ESource *src = s->data;
            const char *completion = e_source_get_property (src, "completion");

            if (completion && !g_ascii_strcasecmp (completion, "true")) {
                addr_sources = g_slist_prepend(addr_sources, src);
                g_object_ref(src);
            }
        }
    }

    for (s = addr_sources;!stop && !found && s;s=g_slist_next(s)) {
        ESource *source = s->data;
        GList *contacts;
        EBook *book;
        void *hook;

        d(printf(" checking '%s'\n", e_source_get_uri(source)));

        /* could this take a while?  no way to cancel it? */
        book = e_book_new(source, &err);

        if (book == NULL) {
            g_warning("Unable to create addressbook: %s", err->message);
            g_clear_error(&err);
            continue;
        }

        hook = mail_cancel_hook_add(emu_addr_cancel_book, book);

        /* ignore errors, but cancellation errors we don't try to go further either */
        if (!e_book_open(book, TRUE, &err)
            || !e_book_get_contacts(book, query, &contacts, &err)) {
            stop = err->domain == E_BOOK_ERROR && err->code == E_BOOK_ERROR_CANCELLED;
            mail_cancel_hook_remove(hook);
            g_object_unref(book);
            g_warning("Can't get contacts: %s", err->message);
            g_clear_error(&err);
            continue;
        }

        mail_cancel_hook_remove(hook);

        if (contacts != NULL) {
            found = TRUE;
            g_list_foreach(contacts, (GFunc)g_object_unref, NULL);
            g_list_free(contacts);
        }

        d(printf(" %s\n", stop?"found":"not found"));

        g_object_unref(book);
    }

    g_slist_free(addr_sources);

    if (!stop) {
        node->found = found;
        node->stamp = now;
    }

    e_book_query_unref(query);

    pthread_mutex_unlock(&emu_addr_lock);

    return found;
}

/**
 * em_utils_snoop_type:
 * @part: 
 * 
 * Tries to snoop the mime type of a part.
 * 
 * Return value: NULL if unknown (more likely application/octet-stream).
 **/
const char *
em_utils_snoop_type(CamelMimePart *part)
{
    const char *filename, *name_type = NULL, *magic_type = NULL;
    CamelDataWrapper *dw;
    
    filename = camel_mime_part_get_filename (part);
    if (filename) {
        /* GNOME-VFS will misidentify TNEF attachments as MPEG */
        if (!strcmp (filename, "winmail.dat"))
            return "application/vnd.ms-tnef";
        
        name_type = gnome_vfs_mime_type_from_name(filename);
    }
    
    dw = camel_medium_get_content_object((CamelMedium *)part);
    if (!camel_data_wrapper_is_offline(dw)) {
        CamelStreamMem *mem = (CamelStreamMem *)camel_stream_mem_new();

        if (camel_data_wrapper_decode_to_stream(dw, (CamelStream *)mem) > 0)
            magic_type = gnome_vfs_get_mime_type_for_data(mem->buffer->data, mem->buffer->len);
        camel_object_unref(mem);
    }

    d(printf("snooped part, magic_type '%s' name_type '%s'\n", magic_type, name_type));

    /* If GNOME-VFS doesn't recognize the data by magic, but it
     * contains English words, it will call it text/plain. If the
     * filename-based check came up with something different, use
     * that instead and if it returns "application/octet-stream"
     * try to do better with the filename check.
     */
    
    if (magic_type) {
        if (name_type
            && (!strcmp(magic_type, "text/plain")
            || !strcmp(magic_type, "application/octet-stream")))
            return name_type;
        else
            return magic_type;
    } else
        return name_type;

    /* We used to load parts to check their type, we dont anymore,
       see bug #11778 for some discussion */
}