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
|
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* pst-importer.c
*
* Author: Chris Halls <chris.halls@credativ.co.uk>
* Bharath Acharya <abharath@novell.com>
*
* Copyright (C) 2006 Chris Halls
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) version 3.
*
* 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with the program; if not, see <http://www.gnu.org/licenses/>
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#define G_LOG_DOMAIN "eplugin-readpst"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <glib/gi18n-lib.h>
#include <glib/gstdio.h>
#include <glib/gprintf.h>
#include <gtk/gtk.h>
#include <e-util/e-import.h>
#include <e-util/e-plugin.h>
#include <libebook/e-contact.h>
#include <libebook/e-book-client.h>
#include <libecal/e-cal-client.h>
#include <libecal/e-cal-component.h>
#include <libedataserver/e-data-server-util.h>
#include <libedataserverui/e-source-selector-dialog.h>
#include <mail/e-mail-backend.h>
#include <mail/e-mail-local.h>
#include <mail/em-folder-selection-button.h>
#include <mail/mail-mt.h>
#include <mail/mail-tools.h>
#include <mail/em-utils.h>
#include <shell/e-shell.h>
#include <libpst/libpst.h>
#include <libpst/timeconv.h>
#ifdef WIN32
#ifdef gmtime_r
#undef gmtime_r
#endif
#define gmtime_r(tp,tmp) (gmtime(tp)?(*(tmp)=*gmtime(tp),(tmp)):0)
#endif
typedef struct _PstImporter PstImporter;
gint pst_init (pst_file *pst, gchar *filename);
gchar *get_pst_rootname (pst_file *pst, gchar *filename);
static void pst_error_msg (const gchar *fmt, ...);
static void pst_import_folders (PstImporter *m, pst_desc_tree *topitem);
static void pst_process_item (PstImporter *m, pst_desc_tree *d_ptr);
static void pst_process_folder (PstImporter *m, pst_item *item);
static void pst_process_email (PstImporter *m, pst_item *item);
static void pst_process_contact (PstImporter *m, pst_item *item);
static void pst_process_appointment (PstImporter *m, pst_item *item);
static void pst_process_task (PstImporter *m, pst_item *item);
static void pst_process_journal (PstImporter *m, pst_item *item);
static void pst_import_file (PstImporter *m);
gchar *foldername_to_utf8 (const gchar *pstname);
gchar *string_to_utf8 (const gchar *string);
void contact_set_date (EContact *contact, EContactField id, FILETIME *date);
struct icaltimetype get_ical_date (FILETIME *date, gboolean is_date);
gchar *rfc2445_datetime_format (FILETIME *ft);
gboolean org_credativ_evolution_readpst_supported (EPlugin *epl, EImportTarget *target);
GtkWidget *org_credativ_evolution_readpst_getwidget (EImport *ei, EImportTarget *target, EImportImporter *im);
void org_credativ_evolution_readpst_import (EImport *ei, EImportTarget *target, EImportImporter *im);
void org_credativ_evolution_readpst_cancel (EImport *ei, EImportTarget *target, EImportImporter *im);
gint e_plugin_lib_enable (EPlugin *ep, gint enable);
/* em-folder-selection-button.h is private, even though other internal evo plugins use it!
so declare the functions here
TODO: sort out whether this should really be private
*/
static guchar pst_signature[] = { '!', 'B', 'D', 'N' };
struct _PstImporter {
MailMsg base;
EImport *import;
EImportTarget *target;
GMutex *status_lock;
gchar *status_what;
gint status_pc;
gint status_timeout_id;
GCancellable *cancellable;
pst_file pst;
CamelFolder *folder;
gchar *parent_uri;
gchar *folder_name;
gchar *folder_uri;
gint folder_count;
gint current_item;
EBookClient *addressbook;
ECalClient *calendar;
ECalClient *tasks;
ECalClient *journal;
};
gboolean
org_credativ_evolution_readpst_supported (EPlugin *epl, EImportTarget *target)
{
gchar signature[sizeof (pst_signature)];
gboolean ret = FALSE;
gint fd, n;
EImportTargetURI *s;
gchar *filename;
if (target->type != E_IMPORT_TARGET_URI) {
return FALSE;
}
s = (EImportTargetURI *) target;
if (s->uri_src == NULL) {
return TRUE;
}
if (strncmp (s->uri_src, "file:///", strlen ("file:///")) != 0) {
return FALSE;
}
filename = g_filename_from_uri (s->uri_src, NULL, NULL);
fd = g_open (filename, O_RDONLY, 0);
g_free (filename);
if (fd != -1) {
n = read (fd, signature, sizeof (pst_signature));
ret = n == sizeof (pst_signature) && memcmp (signature, pst_signature, sizeof (pst_signature)) == 0;
close (fd);
}
return ret;
}
static void
checkbox_mail_toggle_cb (GtkToggleButton *tb, EImportTarget *target)
{
g_datalist_set_data (&target->data, "pst-do-mail", GINT_TO_POINTER (gtk_toggle_button_get_active (tb)));
}
static void
checkbox_addr_toggle_cb (GtkToggleButton *tb, EImportTarget *target)
{
g_datalist_set_data (&target->data, "pst-do-addr", GINT_TO_POINTER (gtk_toggle_button_get_active (tb)));
}
static void
checkbox_appt_toggle_cb (GtkToggleButton *tb, EImportTarget *target)
{
g_datalist_set_data (&target->data, "pst-do-appt", GINT_TO_POINTER (gtk_toggle_button_get_active (tb)));
}
static void
checkbox_task_toggle_cb (GtkToggleButton *tb, EImportTarget *target)
{
g_datalist_set_data (&target->data, "pst-do-task", GINT_TO_POINTER (gtk_toggle_button_get_active (tb)));
}
static void
checkbox_journal_toggle_cb (GtkToggleButton *tb, EImportTarget *target)
{
g_datalist_set_data (&target->data, "pst-do-journal", GINT_TO_POINTER (gtk_toggle_button_get_active (tb)));
}
static void
folder_selected (EMFolderSelectionButton *button, EImportTargetURI *target)
{
g_free (target->uri_dest);
target->uri_dest = g_strdup (em_folder_selection_button_get_selection (button));
}
/**
* Suggest a folder to import data into
*/
static gchar *
get_suggested_foldername (EImportTargetURI *target)
{
EShell *shell;
EShellBackend *shell_backend;
EMailSession *session;
const gchar *inbox;
gchar *delim, *filename;
gchar *rootname = NULL;
GString *foldername;
pst_file pst;
/* XXX Dig up the EMailSession from the default EShell.
* Since the EImport framework doesn't allow for user
* data, I don't see how else to get to it. */
shell = e_shell_get_default ();
shell_backend = e_shell_get_backend_by_name (shell, "mail");
session = e_mail_backend_get_session (E_MAIL_BACKEND (shell_backend));
/* Suggest a folder that is in the same mail storage as the users' inbox,
with a name derived from the .PST file */
inbox = e_mail_local_get_folder_uri (E_MAIL_LOCAL_FOLDER_INBOX);
delim = g_strrstr (inbox, "#");
if (delim != NULL) {
foldername = g_string_new_len (inbox, delim-inbox);
} else {
foldername = g_string_new (inbox);
}
g_string_append_c (foldername, '#');
filename = g_filename_from_uri (target->uri_src, NULL, NULL);
if (pst_init (&pst, filename) == 0) {
rootname = get_pst_rootname (&pst, filename);
}
g_free (filename);
if (rootname != NULL) {
gchar *utf8name;
utf8name = foldername_to_utf8 (rootname);
g_string_append (foldername, utf8name);
g_free (utf8name);
g_free (rootname);
} else {
g_string_append (foldername, "outlook_data");
}
/* FIXME Leaking a CamelFolder reference here. */
/* FIXME Not passing a GCancellable or GError here. */
if (e_mail_session_uri_to_folder_sync (
session, foldername->str, 0, NULL, NULL) != NULL) {
CamelFolder *folder;
/* Folder exists - add a number */
gint i, len;
len = foldername->len;
for (i=1; i<10000; i++) {
g_string_truncate (foldername, len);
g_string_append_printf (foldername, "_%d", i);
/* FIXME Not passing a GCancellable or GError here. */
if ((folder = e_mail_session_uri_to_folder_sync (
session, foldername->str, 0, NULL, NULL)) == NULL) {
/* Folder does not exist */
break;
}
}
if (folder != NULL) {
pst_error_msg ("Error searching for an unused folder name. uri=%s", foldername);
}
}
return g_string_free (foldername, FALSE);
}
GtkWidget *
org_credativ_evolution_readpst_getwidget (EImport *ei, EImportTarget *target, EImportImporter *im)
{
EShell *shell;
EShellBackend *shell_backend;
GtkWidget *hbox, *framebox, *w;
gchar *foldername;
g_datalist_set_data (&target->data, "pst-do-mail", GINT_TO_POINTER (TRUE));
g_datalist_set_data (&target->data, "pst-do-addr", GINT_TO_POINTER (TRUE));
g_datalist_set_data (&target->data, "pst-do-appt", GINT_TO_POINTER (TRUE));
g_datalist_set_data (&target->data, "pst-do-task", GINT_TO_POINTER (TRUE));
g_datalist_set_data (&target->data, "pst-do-journal", GINT_TO_POINTER (TRUE));
framebox = gtk_vbox_new (FALSE, 2);
/* Mail */
hbox = gtk_hbox_new (FALSE, 0);
w = gtk_check_button_new_with_mnemonic (_("_Mail"));
gtk_toggle_button_set_active ((GtkToggleButton *) w, TRUE);
g_signal_connect (w, "toggled", G_CALLBACK (checkbox_mail_toggle_cb), target);
gtk_box_pack_start ((GtkBox *) hbox, w, FALSE, FALSE, 0);
shell = e_shell_get_default ();
shell_backend = e_shell_get_backend_by_name (shell, "mail");
w = em_folder_selection_button_new (
E_MAIL_BACKEND (shell_backend),
_("Select folder"),
_("Select folder to import into"));
foldername = get_suggested_foldername ((EImportTargetURI *) target);
((EImportTargetURI *) target)->uri_dest = g_strdup (foldername);
em_folder_selection_button_set_selection ((EMFolderSelectionButton *) w, foldername);
g_signal_connect (w, "selected", G_CALLBACK (folder_selected), target);
gtk_box_pack_end ((GtkBox *) hbox, w, FALSE, FALSE, 0);
w = gtk_label_new (_("Destination folder:"));
gtk_box_pack_end ((GtkBox *) hbox, w, FALSE, TRUE, 6);
gtk_box_pack_start ((GtkBox *) framebox, hbox, FALSE, FALSE, 0);
/* Address book */
w = gtk_check_button_new_with_mnemonic (_("_Address Book"));
gtk_toggle_button_set_active ((GtkToggleButton *) w, FALSE);
/*gtk_widget_set_sensitive ((GtkWidget *)w, FALSE);*/ /* Disable until implemented */
g_signal_connect (w, "toggled", G_CALLBACK (checkbox_addr_toggle_cb), target);
gtk_box_pack_start ((GtkBox *) framebox, w, FALSE, FALSE, 0);
/* Appointments */
w = gtk_check_button_new_with_mnemonic (_("A_ppointments"));
gtk_toggle_button_set_active ((GtkToggleButton *) w, FALSE);
g_signal_connect (w, "toggled", G_CALLBACK (checkbox_appt_toggle_cb), target);
gtk_box_pack_start ((GtkBox *) framebox, w, FALSE, FALSE, 0);
/* Tasks */
w = gtk_check_button_new_with_mnemonic (_("_Tasks"));
gtk_toggle_button_set_active ((GtkToggleButton *) w, FALSE);
g_signal_connect (w, "toggled", G_CALLBACK (checkbox_task_toggle_cb), target);
gtk_box_pack_start ((GtkBox *) framebox, w, FALSE, FALSE, 0);
/* Journal */
w = gtk_check_button_new_with_mnemonic (_("_Journal entries"));
gtk_toggle_button_set_active ((GtkToggleButton *) w, FALSE);
g_signal_connect (w, "toggled", G_CALLBACK (checkbox_journal_toggle_cb), target);
gtk_box_pack_start ((GtkBox *) framebox, w, FALSE, FALSE, 0);
gtk_widget_show_all (framebox);
g_free (foldername);
return framebox;
}
static gchar *
pst_import_describe (PstImporter *m, gint complete)
{
return g_strdup (_("Importing Outlook data"));
}
static ECalClient *
open_ecal (ECalClientSourceType type, const gchar *name)
{
/* Hack - grab the first calendar we can find
TODO - add a selection mechanism in get_widget */
ESource *primary;
ESourceList *source_list;
ECalClient *cal;
GError *error = NULL;
if ((e_cal_client_get_sources (&source_list, type, &error)) == 0) {
g_debug ("%s: Could not get any sources of type %s: %s", G_STRFUNC, name, error ? error->message : "Unknown error");
if (error)
g_error_free (error);
return NULL;
}
primary = e_source_list_peek_source_any (source_list);
if ((cal = e_cal_client_new (primary, type, &error)) == NULL) {
g_debug ("%s: Could not create %s: %s", G_STRFUNC, name, error ? error->message : "Unknown error");
if (error)
g_error_free (error);
g_object_unref (source_list);
return NULL;
}
if (!e_client_open_sync (E_CLIENT (cal), TRUE, NULL, &error)) {
g_debug ("%s: Failed to open %s: %s", G_STRFUNC, name, error ? error->message : "Unknown error");
if (error)
g_error_free (error);
g_object_unref (cal);
cal = NULL;
}
g_object_unref (primary);
g_object_unref (source_list);
return cal;
}
static void
pst_import_import (PstImporter *m,
GCancellable *cancellable,
GError **error)
{
if (GPOINTER_TO_INT (g_datalist_get_data (&m->target->data, "pst-do-addr"))) {
/* Hack - grab the first address book we can find
TODO - add a selection mechanism in get_widget */
ESource *primary;
ESourceList *source_list;
GError *error = NULL;
if (e_book_client_get_sources (&source_list, &error)) {
primary = e_source_list_peek_source_any (source_list);
if ((m->addressbook = e_book_client_new (primary, &error))) {
if (!e_client_open_sync (E_CLIENT (m->addressbook), TRUE, NULL, &error)) {
g_debug ("%s: Failed to open book client: %s", G_STRFUNC, error ? error->message : "Unknown error");
}
g_object_unref (primary);
g_object_unref (source_list);
} else {
g_debug ("%s: Could not create book client: %s", G_STRFUNC, error ? error->message : "Unknown error");
}
} else {
g_debug ("%s: Could not get address books: %s", G_STRFUNC, error ? error->message : "Unknown error");
}
if (error)
g_error_free (error);
}
if (GPOINTER_TO_INT (g_datalist_get_data (&m->target->data, "pst-do-appt"))) {
m->calendar = open_ecal (E_CAL_CLIENT_SOURCE_TYPE_EVENTS, "calendar");
}
if (GPOINTER_TO_INT (g_datalist_get_data (&m->target->data, "pst-do-task"))) {
m->tasks = open_ecal (E_CAL_CLIENT_SOURCE_TYPE_TASKS, "task list");
}
if (GPOINTER_TO_INT (g_datalist_get_data (&m->target->data, "pst-do-journal"))) {
m->journal = open_ecal (E_CAL_CLIENT_SOURCE_TYPE_MEMOS, "journal");
}
pst_import_file (m);
/* FIXME: Crashes often in here.
if (m->addressbook) {
g_object_unref (m->addressbook);
}
if (m->calendar) {
g_object_unref (m->calendar);
}
if (m->tasks) {
g_object_unref (m->tasks);
}
if (m->journal) {
g_object_unref (m->journal);
}
*/
}
static void
pst_import_file (PstImporter *m)
{
EShell *shell;
EShellBackend *shell_backend;
EMailSession *session;
GCancellable *cancellable;
gint ret;
gchar *filename;
pst_item *item = NULL;
pst_desc_tree *d_ptr;
cancellable = e_activity_get_cancellable (m->base.activity);
/* XXX Dig up the EMailSession from the default EShell.
* Since the EImport framework doesn't allow for user
* data, I don't see how else to get to it. */
shell = e_shell_get_default ();
shell_backend = e_shell_get_backend_by_name (shell, "mail");
session = e_mail_backend_get_session (E_MAIL_BACKEND (shell_backend));
filename = g_filename_from_uri (((EImportTargetURI *) m->target)->uri_src, NULL, NULL);
m->parent_uri = g_strdup (((EImportTargetURI *) m->target)->uri_dest); /* Destination folder, was set in our widget */
camel_operation_push_message (NULL, _("Importing '%s'"), filename);
if (GPOINTER_TO_INT (g_datalist_get_data (&m->target->data, "pst-do-mail"))) {
e_mail_session_uri_to_folder_sync (
session, m->parent_uri, CAMEL_STORE_FOLDER_CREATE,
cancellable, &m->base.error);
}
ret = pst_init (&m->pst, filename);
if (ret < 0) {
g_free (filename);
camel_operation_pop_message (NULL);
return;
}
g_free (filename);
camel_operation_progress (NULL, 1);
if ((item = pst_parse_item (&m->pst, m->pst.d_head, NULL)) == NULL) {
pst_error_msg ("Could not get root record");
return;
}
camel_operation_progress (NULL, 2);
if ((d_ptr = pst_getTopOfFolders (&m->pst, item)) == NULL) {
pst_error_msg ("Top of folders record not found. Cannot continue");
return;
}
camel_operation_progress (NULL, 3);
pst_import_folders (m, d_ptr);
camel_operation_progress (NULL, 4);
camel_operation_pop_message (NULL);
pst_freeItem (item);
}
static void
pst_import_folders (PstImporter *m, pst_desc_tree *topitem)
{
pst_desc_tree *d_ptr;
gchar *seperator;
d_ptr = topitem->child;
/* Walk through folder tree */
while (d_ptr != NULL && (camel_operation_cancel_check (NULL) == FALSE)) {
pst_process_item (m, d_ptr);
if (d_ptr->child != NULL) {
if (m->folder) {
g_object_unref (m->folder);
m->folder = NULL;
}
g_free (m->parent_uri);
m->parent_uri = g_strdup (m->folder_uri);
d_ptr = d_ptr->child;
} else if (d_ptr->next != NULL) {
d_ptr = d_ptr->next;
} else {
while (d_ptr != topitem && d_ptr->next == NULL) {
if (m->folder_uri && g_str_equal (m->folder_uri, m->parent_uri)) {
/* this is for cases where folder has only messages */
seperator = g_strrstr (m->parent_uri, "/");
if (seperator != NULL)
*seperator = '\0';
}
if (m->folder_uri) {
g_free (m->folder_uri);
}
m->folder_uri = g_strdup (m->parent_uri);
seperator = g_strrstr (m->parent_uri, "/");
if (seperator != NULL) {
*seperator = '\0'; /* Truncate uri */
}
if (m->folder) {
g_object_unref (m->folder);
m->folder = NULL;
}
d_ptr = d_ptr->parent;
}
if (d_ptr == topitem) {
return;
}
d_ptr = d_ptr->next;
}
}
}
static void
pst_process_item (PstImporter *m, pst_desc_tree *d_ptr)
{
pst_item *item = NULL;
if (d_ptr->desc == NULL)
return;
item = pst_parse_item (&m->pst, d_ptr, NULL);
if (item == NULL)
return;
if (item->message_store != NULL) {
pst_error_msg ("A second message_store has been found - ignored");
pst_freeItem (item);
return;
}
if (item->folder != NULL) {
pst_process_folder (m, item);
camel_operation_push_message (
NULL, _("Importing '%s'"), item->file_as.str);
} else {
if (m->folder_count && (m->current_item < m->folder_count)) {
camel_operation_progress (NULL, (m->current_item * 100) / m->folder_count);
} else {
camel_operation_progress (NULL, 100);
}
switch (item->type) {
case PST_TYPE_CONTACT:
if (item->contact && m->addressbook && GPOINTER_TO_INT (g_datalist_get_data (&m->target->data, "pst-do-addr")))
pst_process_contact (m, item);
break;
case PST_TYPE_APPOINTMENT:
if (item->appointment && m->calendar && GPOINTER_TO_INT (g_datalist_get_data (&m->target->data, "pst-do-appt")))
pst_process_appointment (m, item);
break;
case PST_TYPE_TASK:
if (item->appointment && m->tasks && GPOINTER_TO_INT (g_datalist_get_data (&m->target->data, "pst-do-task")))
pst_process_task (m, item);
break;
case PST_TYPE_JOURNAL:
if (item->appointment && m->journal && GPOINTER_TO_INT (g_datalist_get_data (&m->target->data, "pst-do-journal")))
pst_process_journal (m, item);
break;
case PST_TYPE_NOTE:
case PST_TYPE_SCHEDULE:
case PST_TYPE_REPORT:
if (item->email && GPOINTER_TO_INT (g_datalist_get_data (&m->target->data, "pst-do-mail")))
pst_process_email (m, item);
break;
}
m->current_item++;
}
pst_freeItem (item);
if (d_ptr->next == NULL) {
camel_operation_pop_message (NULL);
}
}
/**
* string_to_utf8:
* @string: String from PST file
*
* Convert string to utf8. Currently we just use the locale, but maybe
* there is encoding information hidden somewhere in the PST file?
*
* Returns: utf8 representation (caller should free), or NULL for error.
*/
gchar *
string_to_utf8 (const gchar *string)
{
gchar *utf8;
if (g_utf8_validate (string, -1, NULL))
return g_strdup (string);
utf8 = g_locale_to_utf8 (string, -1, NULL, NULL, NULL);
return utf8;
}
/**
* foldername_to_utf8:
* @foldername: from PST file
*
* Convert foldername to utf8 and escape characters if needed
*
* Returns: converted folder name, or NULL for error. Caller should free
*/
gchar *
foldername_to_utf8 (const gchar *pstname)
{
gchar *utf8name, *folder_name;
utf8name = string_to_utf8 (pstname);
if (utf8name == NULL) {
folder_name = camel_url_encode (pstname, NULL);
g_warning ("foldername_to_utf8: Cannot convert to utf8! foldername=%s", folder_name);
} else {
/* Encode using the current locale */
folder_name = camel_url_encode (utf8name, NULL);
g_free (utf8name);
}
g_strdelimit (folder_name, "/", '_');
g_strescape (folder_name, NULL);
return folder_name;
}
static void
pst_process_folder (PstImporter *m, pst_item *item)
{
gchar *uri;
g_free (m->folder_name);
g_free (m->folder_uri);
if (item->file_as.str != NULL) {
m->folder_name = foldername_to_utf8 (item->file_as.str);
} else {
g_critical ("Folder: No name! item->file_as=%s", item->file_as.str);
m->folder_name = g_strdup ("unknown_name");
}
uri = g_strjoin ("/", m->parent_uri, m->folder_name, NULL);
m->folder_uri = uri;
if (m->folder) {
g_object_unref (m->folder);
m->folder = NULL;
}
m->folder_count = item->folder->item_count;
m->current_item = 0;
}
/**
* pst_create_folder:
* @m: PstImporter set to current folder
*
* Create current folder in mail hierarchy. Parent folders will also be
* created.
*/
static void
pst_create_folder (PstImporter *m)
{
EShell *shell;
EShellBackend *shell_backend;
EMailSession *session;
GCancellable *cancellable;
const gchar *parent;
gchar *dest, *dest_end, *pos;
gint dest_len;
cancellable = e_activity_get_cancellable (m->base.activity);
/* XXX Dig up the EMailSession from the default EShell.
* Since the EImport framework doesn't allow for user
* data, I don't see how else to get to it. */
shell = e_shell_get_default ();
shell_backend = e_shell_get_backend_by_name (shell, "mail");
session = e_mail_backend_get_session (E_MAIL_BACKEND (shell_backend));
parent = ((EImportTargetURI *) m->target)->uri_dest;
dest = g_strdup (m->folder_uri);
g_assert (g_str_has_prefix (dest, parent));
if (m->folder) {
g_object_unref (m->folder);
m->folder = NULL;
}
dest_len = strlen (dest);
dest_end = dest + dest_len;
pos = dest + strlen (parent);
while (pos != NULL && pos < dest_end) {
pos = g_strstr_len (pos+1, dest_end-pos, "/");
if (pos != NULL) {
CamelFolder *folder;
*pos = '\0';
folder = e_mail_session_uri_to_folder_sync (
session, dest, CAMEL_STORE_FOLDER_CREATE,
cancellable, &m->base.error);
if (folder)
g_object_unref (folder);
else
break;
*pos = '/';
}
}
g_free (dest);
if (!m->base.error)
m->folder = e_mail_session_uri_to_folder_sync (
session, m->folder_uri, CAMEL_STORE_FOLDER_CREATE,
cancellable, &m->base.error);
}
/**
* attachment_to_part:
* @m: a #PstImporter
* @attach: attachment to convert
*
* Create a #CamelMimePart from given PST attachment
*
* Returns: #CamelMimePart containing data and mime type
*/
static CamelMimePart *
attachment_to_part (PstImporter *m, pst_item_attach *attach)
{
CamelMimePart *part;
const gchar *mimetype;
part = camel_mime_part_new ();
if (attach->filename2.str || attach->filename1.str) {
camel_mime_part_set_filename (part, (attach->filename2.str ? attach->filename2.str : attach->filename1.str));
camel_mime_part_set_disposition (part, "attachment");
camel_mime_part_set_encoding (part, CAMEL_TRANSFER_ENCODING_BASE64);
} else {
camel_mime_part_set_disposition (part, "inline");
}
if (attach->mimetype.str != NULL) {
mimetype = attach->mimetype.str;
} else {
mimetype = "application/octet-stream";
}
if (attach->data.data != NULL) {
camel_mime_part_set_content (part, attach->data.data, attach->data.size, mimetype);
} else {
pst_binary attach_rc;
attach_rc = pst_attach_to_mem (&m->pst, attach);
camel_mime_part_set_content (part, (gchar *) attach_rc.data, attach_rc.size, mimetype);
free (attach_rc.data);
}
return part;
}
static void
pst_process_email (PstImporter *m, pst_item *item)
{
CamelMimeMessage *msg;
CamelInternetAddress *addr;
CamelMultipart *mp;
CamelMimePart *part;
CamelMessageInfo *info;
pst_item_attach *attach;
gboolean success;
if (m->folder == NULL) {
pst_create_folder (m);
if (!m->folder)
return;
}
camel_folder_freeze (m->folder);
msg = camel_mime_message_new ();
if (item->subject.str != NULL) {
gchar *subj;
subj = string_to_utf8 (item->subject.str);
if (subj == NULL) {
g_warning ("Could not convert email subject to utf8: %s", item->subject.str);
camel_mime_message_set_subject (msg, "(lost subject)");
} else {
camel_mime_message_set_subject (msg, subj);
g_free (subj);
}
}
addr = camel_internet_address_new ();
if (item->email->outlook_sender_name.str != NULL && item->email->outlook_sender.str != NULL) {
camel_internet_address_add (addr, item->email->outlook_sender_name.str, item->email->outlook_sender.str);
} else if (item->email->outlook_sender_name.str != NULL) {
camel_address_decode (CAMEL_ADDRESS (addr), item->email->outlook_sender_name.str);
} else if (item->email->outlook_sender.str != NULL) {
camel_address_decode (CAMEL_ADDRESS (addr), item->email->outlook_sender.str);
} else {
/* Evo prints a warning if no from is set, so supply an empty address */
camel_internet_address_add (addr, "", "");
}
camel_mime_message_set_from (msg, addr);
g_object_unref (addr);
if (item->email->sent_date != NULL) {
camel_mime_message_set_date (msg, pst_fileTimeToUnixTime (item->email->sent_date), 0);
}
if (item->email->messageid.str != NULL) {
camel_mime_message_set_message_id (msg, item->email->messageid.str);
}
if (item->email->header.str != NULL) {
/* Use mime parser to read headers */
CamelStream *stream;
/*g_debug (" Email headers length=%zd", strlen (item->email->header));*/
/*g_message (" Email headers... %s...", item->email->header);*/
stream = camel_stream_mem_new_with_buffer (item->email->header.str, strlen (item->email->header.str));
if (!camel_data_wrapper_construct_from_stream_sync ((CamelDataWrapper *) msg, stream, NULL, NULL))
g_warning ("Error reading headers, skipped");
} else {
if (item->email->sentto_address.str != NULL) {
addr = camel_internet_address_new ();
if (camel_address_decode (CAMEL_ADDRESS (addr), item->email->sentto_address.str) > 0)
camel_mime_message_set_recipients (msg, "To", addr);
g_object_unref (addr);
}
if (item->email->cc_address.str != NULL) {
addr = camel_internet_address_new ();
if (camel_address_decode (CAMEL_ADDRESS (addr), item->email->cc_address.str) > 0)
camel_mime_message_set_recipients (msg, "CC", addr);
g_object_unref (addr);
}
}
mp = camel_multipart_new ();
if (item->attach != NULL) {
camel_data_wrapper_set_mime_type (CAMEL_DATA_WRAPPER (mp), "multipart/mixed");
} else if (item->email->htmlbody.str && item->body.str) {
camel_data_wrapper_set_mime_type (CAMEL_DATA_WRAPPER (mp), "multipart/alternate");
} else if (item->email->htmlbody.str) {
camel_data_wrapper_set_mime_type (CAMEL_DATA_WRAPPER (mp), "text/html");
}
camel_multipart_set_boundary (mp, NULL);
if (item->body.str != NULL) {
/* Read internet headers */
/*g_debug (" Email body length=%zd", strlen (item->email->body));
g_message (" Email body %100s...", item->email->body);*/
part = camel_mime_part_new ();
camel_mime_part_set_content (part, item->body.str, strlen (item->body.str), "text/plain");
camel_multipart_add_part (mp, part);
g_object_unref (part);
}
if (item->email->htmlbody.str != NULL) {
/*g_debug (" HTML body length=%zd", strlen (item->email->htmlbody));*/
part = camel_mime_part_new ();
camel_mime_part_set_content (part, item->email->htmlbody.str, strlen (item->email->htmlbody.str), "text/html");
camel_multipart_add_part (mp, part);
g_object_unref (part);
}
for (attach = item->attach; attach; attach = attach->next) {
if (attach->data.data || attach->i_id) {
part = attachment_to_part (m, attach);
camel_multipart_add_part (mp, part);
g_object_unref (part);
}
}
/*camel_mime_message_dump (msg, TRUE);*/
if (item->email->htmlbody.str || item->attach) {
camel_medium_set_content (CAMEL_MEDIUM (msg), CAMEL_DATA_WRAPPER (mp));
} else if (item->body.str) {
camel_mime_part_set_content (CAMEL_MIME_PART (msg), item->body.str, strlen (item->body.str), "text/plain");
} else {
g_warning ("Email without body. Subject:%s",
(item->subject.str ? item->subject.str : "(empty)"));
camel_mime_part_set_content (CAMEL_MIME_PART (msg), "\n", 1, "text/plain");
}
info = camel_message_info_new (NULL);
/* Read message flags (see comments in libpst.c */
if (item->flags & 0x01)
camel_message_info_set_flags (info, CAMEL_MESSAGE_SEEN, ~0);
if (item->email->importance == 2)
camel_message_info_set_flags (info, CAMEL_MESSAGE_FLAGGED, ~0);
if (item->flags & 0x08)
camel_message_info_set_flags (info, CAMEL_MESSAGE_DRAFT, ~0);
/* FIXME Not passing a GCancellable or GError here. */
success = camel_folder_append_message_sync (
m->folder, msg, info, NULL, NULL, NULL);
camel_message_info_free (info);
g_object_unref (msg);
/* FIXME Not passing a GCancellable or GError here. */
camel_folder_synchronize_sync (m->folder, FALSE, NULL, NULL);
camel_folder_thaw (m->folder);
if (!success) {
g_critical ("Exception!");
return;
}
}
static void
contact_set_string (EContact *contact, EContactField id, gchar *string)
{
if (string != NULL) {
e_contact_set (contact, id, string);
}
}
static void
unknown_field (EContact *contact, GString *notes, const gchar *name, gchar *string)
{
/* Field could not be mapped directly so add to notes field */
if (string != NULL) {
g_string_append_printf (notes, "%s: %s\n", name, string);
}
}
static void
contact_set_address (EContact *contact, EContactField id, gchar *address, gchar *city, gchar *country, gchar *po_box, gchar *postal_code, gchar *state, gchar *street)
{
EContactAddress *eaddress;
if (address || city || country || po_box || postal_code || state || street) {
eaddress = g_new0 (EContactAddress, 1);
if (po_box) {
eaddress->po = g_strdup (po_box);
}
//eaddress->ext =
if (street) {
eaddress->street = g_strdup (street);
}
if (city) {
eaddress->locality = g_strdup (city);
}
if (state) {
eaddress->region = g_strdup (state);
}
if (postal_code) {
eaddress->code = g_strdup (postal_code);
}
if (country) {
eaddress->country = g_strdup (country);
}
e_contact_set (contact, id, eaddress);
}
}
void
contact_set_date (EContact *contact, EContactField id, FILETIME *date)
{
if (date && (date->dwLowDateTime || date->dwHighDateTime) ) {
time_t t1;
struct tm tm;
EContactDate *bday;
bday = e_contact_date_new ();
t1 = pst_fileTimeToUnixTime (date);
gmtime_r (&t1, &tm);
bday->year = tm.tm_year + 1900;
bday->month = tm.tm_mon + 1;
bday->day = tm.tm_mday;
e_contact_set (contact, id, bday);
}
}
static void
pst_process_contact (PstImporter *m, pst_item *item)
{
pst_item_contact *c;
EContact *ec;
GString *notes;
gchar *uid = NULL;
GError *error = NULL;
c = item->contact;
notes = g_string_sized_new (2048);
ec = e_contact_new ();
/* pst's fullname field only contains first, middle, surname */
if (c->display_name_prefix.str || c->suffix.str) {
GString *name = g_string_sized_new (128);
if (c->display_name_prefix.str) {
g_string_assign (name, c->display_name_prefix.str);
}
if (c->first_name.str) {
g_string_append_printf (name, "%s%s", (name->len ? " " : ""), c->first_name.str);
}
if (c->middle_name.str) {
g_string_append_printf (name, "%s%s", (name->len ? " " : ""), c->middle_name.str);
}
if (c->surname.str) {
g_string_append_printf (name, "%s%s", (name->len ? " " : ""), c->surname.str);
}
if (c->surname.str) {
g_string_append_printf (name, "%s%s", (name->len ? " " : ""), c->surname.str);
}
contact_set_string (ec, E_CONTACT_FULL_NAME, name->str);
g_string_free (name, TRUE);
} else {
contact_set_string (ec, E_CONTACT_FULL_NAME, c->fullname.str);
}
/* unknown_field (ec, notes, "initials", c->initials); */
contact_set_string (ec, E_CONTACT_NICKNAME, c->nickname.str);
contact_set_string (ec, E_CONTACT_ORG, c->company_name.str);
contact_set_string (ec, E_CONTACT_ORG_UNIT, c->department.str);
contact_set_string (ec, E_CONTACT_TITLE, c->job_title.str);
contact_set_address (ec,E_CONTACT_ADDRESS_WORK,
c->business_address.str, c->business_city.str, c->business_country.str,
c->business_po_box.str, c->business_postal_code.str, c->business_state.str, c->business_street.str);
contact_set_address (ec,E_CONTACT_ADDRESS_HOME,
c->home_address.str, c->home_city.str, c->home_country.str,
c->home_po_box.str, c->home_postal_code.str, c->home_state.str, c->home_street.str);
contact_set_address (ec,E_CONTACT_ADDRESS_OTHER,
c->other_address.str, c->other_city.str, c->other_country.str,
c->other_po_box.str, c->other_postal_code.str, c->other_state.str, c->other_street.str);
contact_set_string (ec, E_CONTACT_PHONE_ASSISTANT, c->assistant_phone.str);
contact_set_string (ec, E_CONTACT_PHONE_BUSINESS_FAX, c->business_fax.str);
contact_set_string (ec, E_CONTACT_PHONE_BUSINESS, c->business_phone.str);
contact_set_string (ec, E_CONTACT_PHONE_BUSINESS_2, c->business_phone2.str);
contact_set_string (ec, E_CONTACT_PHONE_CALLBACK, c->callback_phone.str);
contact_set_string (ec, E_CONTACT_PHONE_CAR, c->car_phone.str);
contact_set_string (ec, E_CONTACT_PHONE_COMPANY, c->company_main_phone.str);
contact_set_string (ec, E_CONTACT_PHONE_HOME_FAX, c->home_fax.str);
contact_set_string (ec, E_CONTACT_PHONE_HOME, c->home_phone.str);
contact_set_string (ec, E_CONTACT_PHONE_HOME_2, c->home_phone2.str);
contact_set_string (ec, E_CONTACT_PHONE_ISDN, c->isdn_phone.str);
contact_set_string (ec, E_CONTACT_PHONE_MOBILE, c->mobile_phone.str);
contact_set_string (ec, E_CONTACT_PHONE_OTHER_FAX, c->primary_fax.str); /* ? */
contact_set_string (ec, E_CONTACT_PHONE_PAGER, c->pager_phone.str);
contact_set_string (ec, E_CONTACT_PHONE_PRIMARY, c->primary_phone.str);
contact_set_string (ec, E_CONTACT_PHONE_RADIO, c->radio_phone.str);
contact_set_string (ec, E_CONTACT_PHONE_TTYTDD, c->ttytdd_phone.str);
contact_set_string (ec, E_CONTACT_PHONE_TELEX, c->telex.str);
unknown_field (ec, notes, "account_name", c->account_name.str);
contact_set_date (ec, E_CONTACT_ANNIVERSARY, c->wedding_anniversary);
contact_set_string (ec, E_CONTACT_ASSISTANT, c->assistant_name.str);
unknown_field (ec, notes, "billing_information", c->billing_information.str);
contact_set_date (ec, E_CONTACT_BIRTH_DATE, c->birthday);
/* contact_set_string (ec, E_CONTACT_CATEGORIES, c->??); */
contact_set_string (ec, E_CONTACT_EMAIL_1 , c->address1.str);
contact_set_string (ec, E_CONTACT_EMAIL_2 , c->address2.str);
contact_set_string (ec, E_CONTACT_EMAIL_3 , c->address3.str);
/*unknown_field (ec, notes, "address1_desc" , c->address1_desc);
unknown_field (ec, notes, "address1_transport" , c->address1_transport);
unknown_field (ec, notes, "address2_desc" , c->address2_desc);
unknown_field (ec, notes, "address2_transport" , c->address2_transport);
unknown_field (ec, notes, "address3_desc" , c->address3_desc);
unknown_field (ec, notes, "address3_transport" , c->address3_transport);*/
/*unknown_field (ec, notes, "def_postal_address", c->def_postal_address);*/
/* unknown_field (ec, ??, c->gender); */
unknown_field (ec, notes, "gov_id", c->gov_id.str);
unknown_field (ec, notes, "customer_id", c->customer_id.str);
unknown_field (ec, notes, "hobbies", c->hobbies.str);
unknown_field (ec, notes, "followup", c->followup.str);
contact_set_string (ec, E_CONTACT_FREEBUSY_URL , c->free_busy_address.str);
unknown_field (ec, notes, "keyword", c->keyword.str);
unknown_field (ec, notes, "language", c->language.str);
unknown_field (ec, notes, "location", c->location.str);
contact_set_string (ec, E_CONTACT_OFFICE, c->office_loc.str);
unknown_field (ec, notes, "computer_name", c->computer_name.str);
unknown_field (ec, notes, "ftp_site", c->ftp_site.str);
contact_set_string (ec, E_CONTACT_MANAGER , c->manager_name.str);
unknown_field (ec, notes, "mileage", c->mileage.str);
unknown_field (ec, notes, "org_id", c->org_id.str);
contact_set_string (ec, E_CONTACT_ROLE, c->profession.str);
contact_set_string (ec, E_CONTACT_SPOUSE , c->spouse_name.str);
if (c->personal_homepage.str) {
contact_set_string (ec, E_CONTACT_HOMEPAGE_URL , c->personal_homepage.str);
if (c->business_homepage.str) {
unknown_field (ec, notes, "business_homepage", c->business_homepage.str);
}
} else if (c->business_homepage.str) {
contact_set_string (ec, E_CONTACT_HOMEPAGE_URL , c->business_homepage.str);
}
if (item->comment.str) {
g_string_append_printf (notes, "%s\n", item->comment.str);
}
if (item->email && item->body.str) {
g_string_append_printf (notes, "%s\n", item->body.str);
}
contact_set_string (ec, E_CONTACT_NOTE, notes->str);
g_string_free (notes, TRUE);
if (!e_book_client_add_contact_sync (m->addressbook, ec, &uid, NULL, &error))
uid = NULL;
g_object_unref (ec);
g_free (uid);
if (error) {
g_debug ("%s: Failed to add contact: %s", G_STRFUNC, error->message);
g_error_free (error);
}
}
/**
* get_ical_date:
* @date: time value from libpst
* @is_date: treat as date only (all day event)?
*
* Convert pst time to icaltimetype
*
* Returns: converted date
*/
struct icaltimetype
get_ical_date (FILETIME *date, gboolean is_date)
{
if (date && (date->dwLowDateTime || date->dwHighDateTime) ) {
time_t t;
t = pst_fileTimeToUnixTime (date);
return icaltime_from_timet_with_zone (t, is_date, NULL);
} else {
return icaltime_null_date ();
}
}
static void
set_cal_attachments (ECalClient *cal, ECalComponent *ec, PstImporter *m, pst_item_attach *attach)
{
GSList *list = NULL;
const gchar *uid;
gchar *store_dir;
if (attach == NULL) {
return;
}
e_cal_component_get_uid (ec, &uid);
store_dir = g_filename_from_uri (e_cal_client_get_local_attachment_store (cal), NULL, NULL);
while (attach != NULL) {
const gchar * orig_filename;
gchar *filename, *tmp, *path, *dirname, *uri;
CamelMimePart *part;
CamelDataWrapper *content;
CamelStream *stream;
struct stat st;
part = attachment_to_part (m, attach);
orig_filename = camel_mime_part_get_filename (part);
if (orig_filename == NULL) {
g_warning("Ignoring unnamed attachment");
attach = attach->next;
continue; /* Ignore unnamed attachments */
}
tmp = camel_file_util_safe_filename (orig_filename);
filename = g_strdup_printf ("%s-%s", uid, tmp);
path = g_build_filename (store_dir, filename, NULL);
g_free (tmp);
g_free (filename);
dirname = g_path_get_dirname (path);
if (g_mkdir_with_parents (dirname, 0777) == -1) {
g_warning("Could not create directory %s: %s", dirname, g_strerror(errno));
g_free (dirname);
attach = attach->next;
continue;
}
g_free (dirname);
if (g_access (path, F_OK) == 0) {
if (g_access (path, W_OK) != 0) {
g_warning("Could not write file %s - file exists", path);
attach = attach->next;
continue;
}
}
if (g_stat (path, &st) != -1 && !S_ISREG (st.st_mode)) {
g_warning("Could not write file %s - not a file", path);
attach = attach->next;
continue;
}
if (!(stream = camel_stream_fs_new_with_name (path, O_WRONLY | O_CREAT | O_TRUNC, 0666, NULL))) {
g_warning ("Could not create stream for file %s - %s", path, g_strerror (errno));
attach = attach->next;
continue;
}
content = camel_medium_get_content (CAMEL_MEDIUM (part));
if (camel_data_wrapper_decode_to_stream_sync (content, stream, NULL, NULL) == -1
|| camel_stream_flush (stream, NULL, NULL) == -1)
{
g_warning ("Could not write attachment to %s: %s", path, g_strerror (errno));
g_object_unref (stream);
attach = attach->next;
continue;
}
g_object_unref (stream);
uri = g_filename_to_uri (path, NULL, NULL);
list = g_slist_append (list, g_strdup (uri));
g_free (uri);
g_object_unref (part);
g_free (path);
attach = attach->next;
}
g_free (store_dir);
e_cal_component_set_attachment_list (ec, list);
}
static void
fill_calcomponent (PstImporter *m, pst_item *item, ECalComponent *ec, const gchar *type)
{
pst_item_appointment *a;
pst_item_email *e;
ECalComponentText text;
struct icaltimetype tt_start, tt_end;
ECalComponentDateTime dt_start, dt_end;
a = item->appointment;
e = item->email;
g_return_if_fail (item->appointment != NULL);
if (item->create_date) {
struct icaltimetype tt;
tt = get_ical_date (item->create_date, FALSE);
e_cal_component_set_created (ec, &tt);
}
if (item->modify_date) {
struct icaltimetype tt;
tt = get_ical_date (item->modify_date, FALSE);
e_cal_component_set_last_modified (ec, &tt);
}
if (e) {
if (item->subject.str || e->processed_subject.str) {
if (item->subject.str) {
text.value = item->subject.str;
} else if (e->processed_subject.str) {
text.value = e->processed_subject.str;
}
text.altrep = NULL; /* email->proc_subject? */
e_cal_component_set_summary (ec, &text);
}
if (item->body.str) {
GSList l;
text.value = item->body.str;
text.altrep = NULL;
l.data = &text;
l.next = NULL;
e_cal_component_set_description_list (ec, &l);
}
} else {
g_warning ("%s without subject / body!", type);
}
if (a->location.str) {
e_cal_component_set_location (ec, a->location.str);
}
if (a->start) {
tt_start = get_ical_date (a->start, a->all_day);
dt_start.value = &tt_start;
dt_start.tzid = NULL;
e_cal_component_set_dtstart (ec, &dt_start);
}
if (a->end) {
tt_end = get_ical_date (a->end, a->all_day);
dt_end.value = &tt_end;
dt_end.tzid = NULL;
e_cal_component_set_dtend (ec, &dt_end);
}
switch (a->showas) {
case PST_FREEBUSY_TENTATIVE:
e_cal_component_set_status (ec, ICAL_STATUS_TENTATIVE);
break;
case PST_FREEBUSY_FREE:
// mark as transparent and as confirmed
e_cal_component_set_transparency (ec, E_CAL_COMPONENT_TRANSP_TRANSPARENT);
case PST_FREEBUSY_BUSY:
case PST_FREEBUSY_OUT_OF_OFFICE:
e_cal_component_set_status (ec, ICAL_STATUS_CONFIRMED);
break;
}
switch (a->label) {
case PST_APP_LABEL_NONE:
break;
case PST_APP_LABEL_IMPORTANT:
e_cal_component_set_categories (ec, "Important"); break;
case PST_APP_LABEL_BUSINESS:
e_cal_component_set_categories (ec, "Business"); break;
case PST_APP_LABEL_PERSONAL:
e_cal_component_set_categories (ec, "Personal"); break;
case PST_APP_LABEL_VACATION:
e_cal_component_set_categories (ec, "Vacation"); break;
case PST_APP_LABEL_MUST_ATTEND:
e_cal_component_set_categories (ec, "Must-attend"); break;
case PST_APP_LABEL_TRAVEL_REQ:
e_cal_component_set_categories (ec, "Travel-required"); break;
case PST_APP_LABEL_NEEDS_PREP:
e_cal_component_set_categories (ec, "Needs-preparation"); break;
case PST_APP_LABEL_BIRTHDAY:
e_cal_component_set_categories (ec, "Birthday"); break;
case PST_APP_LABEL_ANNIVERSARY:
e_cal_component_set_categories (ec, "Anniversary"); break;
case PST_APP_LABEL_PHONE_CALL:
e_cal_component_set_categories (ec, "Phone-call"); break;
}
if (a->alarm || a->alarm_minutes) {
ECalComponentAlarm *alarm;
ECalComponentAlarmTrigger trigger;
alarm = e_cal_component_alarm_new ();
if (a->alarm_minutes) {
trigger.type = E_CAL_COMPONENT_ALARM_TRIGGER_RELATIVE_START;
trigger.u.rel_duration = icaldurationtype_from_int (- (a->alarm_minutes)*60);
e_cal_component_alarm_set_trigger (alarm, trigger);
}
if (a->alarm) {
if (a->alarm_filename.str) {
e_cal_component_alarm_set_action (alarm, E_CAL_COMPONENT_ALARM_AUDIO);
} else {
e_cal_component_alarm_set_action (alarm, E_CAL_COMPONENT_ALARM_DISPLAY);
}
}
e_cal_component_add_alarm (ec, alarm);
e_cal_component_alarm_free (alarm);
}
if (a->recurrence_description.str != PST_APP_RECUR_NONE) {
struct icalrecurrencetype r;
GSList recur_list;
icalrecurrencetype_clear (&r);
r.interval = 1; /* Interval not implemented in libpst */
if (a->recurrence_end) {
r.until = get_ical_date (a->recurrence_end, FALSE);
}
switch (a->recurrence_type) {
case PST_APP_RECUR_DAILY:
r.freq = ICAL_DAILY_RECURRENCE; break;
case PST_APP_RECUR_WEEKLY:
r.freq = ICAL_WEEKLY_RECURRENCE; break;
case PST_APP_RECUR_MONTHLY:
r.freq = ICAL_MONTHLY_RECURRENCE; break;
case PST_APP_RECUR_YEARLY:
r.freq = ICAL_YEARLY_RECURRENCE; break;
default:
r.freq = ICAL_NO_RECURRENCE;
}
recur_list.data = &r;
recur_list.next = NULL;
e_cal_component_set_rrule_list (ec, &recur_list);
}
}
static void
pst_process_component (PstImporter *m, pst_item *item, const gchar *comp_type, ECalClient *cal)
{
ECalComponent *ec;
gchar *uid = NULL;
GError *error = NULL;
g_return_if_fail (item->appointment != NULL);
ec = e_cal_component_new ();
e_cal_component_set_new_vtype (ec, E_CAL_COMPONENT_EVENT);
fill_calcomponent (m, item, ec, comp_type);
set_cal_attachments (cal, ec, m, item->attach);
if (!e_cal_client_create_object_sync (cal, e_cal_component_get_icalcomponent (ec), &uid, NULL, &error)) {
uid = NULL;
g_warning ("Creation of %s failed: %s", comp_type, error ? error->message : "Unknown error");
}
g_object_unref (ec);
g_free (uid);
if (error)
g_error_free (error);
}
static void
pst_process_appointment (PstImporter *m, pst_item *item)
{
pst_process_component (m, item, "appointment", m->calendar);
}
static void
pst_process_task (PstImporter *m, pst_item *item)
{
pst_process_component (m, item, "task", m->tasks);
}
static void
pst_process_journal (PstImporter *m, pst_item *item)
{
pst_process_component (m, item, "journal", m->journal);
}
/* Print an error message - maybe later bring up an error dialog? */
static void
pst_error_msg (const gchar *fmt, ...)
{
va_list ap;
va_start (ap, fmt);
g_critical (fmt, ap);
va_end (ap);
}
static void
pst_import_imported (PstImporter *m)
{
e_import_complete (m->target->import, (EImportTarget *) m->target);
}
static void
pst_import_free (PstImporter *m)
{
// pst_close (&m->pst);
g_object_unref (m->cancellable);
g_free (m->status_what);
g_mutex_free (m->status_lock);
g_source_remove (m->status_timeout_id);
m->status_timeout_id = 0;
g_free (m->folder_name);
g_free (m->folder_uri);
g_free (m->parent_uri);
g_object_unref (m->import);
}
static MailMsgInfo pst_import_info = {
sizeof (PstImporter),
(MailMsgDescFunc) pst_import_describe,
(MailMsgExecFunc) pst_import_import,
(MailMsgDoneFunc) pst_import_imported,
(MailMsgFreeFunc) pst_import_free,
};
static gboolean
pst_status_timeout (gpointer data)
{
PstImporter *importer = data;
gint pc;
gchar *what;
if (importer->status_what) {
g_mutex_lock (importer->status_lock);
what = importer->status_what;
importer->status_what = NULL;
pc = importer->status_pc;
g_mutex_unlock (importer->status_lock);
e_import_status (importer->target->import, (EImportTarget *) importer->target, what, pc);
}
return TRUE;
}
static void
pst_status (CamelOperation *op, const gchar *what, gint pc, gpointer data)
{
PstImporter *importer = data;
g_mutex_lock (importer->status_lock);
g_free (importer->status_what);
importer->status_what = g_strdup (what);
importer->status_pc = pc;
g_mutex_unlock (importer->status_lock);
}
static gint
pst_import (EImport *ei, EImportTarget *target)
{
PstImporter *m;
gint id;
m = mail_msg_new (&pst_import_info);
g_datalist_set_data (&target->data, "pst-msg", m);
m->import = ei;
g_object_ref (m->import);
m->target = target;
m->parent_uri = NULL;
m->folder_name = NULL;
m->folder_uri = NULL;
m->addressbook = NULL;
m->calendar = NULL;
m->tasks = NULL;
m->journal = NULL;
m->status_timeout_id = g_timeout_add (100, pst_status_timeout, m);
/*m->status_timeout_id = NULL;*/
m->status_lock = g_mutex_new ();
m->cancellable = camel_operation_new ();
g_signal_connect (
m->cancellable, "status",
G_CALLBACK (pst_status), m);
id = m->base.seq;
mail_msg_unordered_push (m);
return id;
}
/* Start the main import operation */
void
org_credativ_evolution_readpst_import (EImport *ei, EImportTarget *target, EImportImporter *im)
{
if (GPOINTER_TO_INT (g_datalist_get_data (&target->data, "pst-do-mail"))
|| GPOINTER_TO_INT (g_datalist_get_data (&target->data, "pst-do-addr"))
|| GPOINTER_TO_INT (g_datalist_get_data (&target->data, "pst-do-appt"))
|| GPOINTER_TO_INT (g_datalist_get_data (&target->data, "pst-do-task"))
|| GPOINTER_TO_INT (g_datalist_get_data (&target->data, "pst-do-journal"))) {
pst_import (ei, target);
} else {
e_import_complete (target->import, target);
}
}
void
org_credativ_evolution_readpst_cancel (EImport *ei, EImportTarget *target, EImportImporter *im)
{
PstImporter *m = g_datalist_get_data (&target->data, "pst-msg");
if (m) {
g_cancellable_cancel (m->cancellable);
}
}
gint
e_plugin_lib_enable (EPlugin *ep, gint enable)
{
return 0;
}
/**
* pst_init:
* @pst: pst_file structure to be used by libpst
* @filename: path to file
*
* Open PST file and determine root folder name
*
* Returns: 0 for sucess, -1 for failure
*/
gint
pst_init (pst_file *pst, gchar *filename)
{
#if 0
gchar *d_log = "readpst.log";
/* initialize log file */
DEBUG_INIT (d_log);
DEBUG_REGISTER_CLOSE ();
#endif
if (pst_open (pst, filename) < 0) {
pst_error_msg ("Error opening PST file %s", filename);
return -1;
}
if (pst_load_index (pst) < 0) {
pst_error_msg ("Error loading indexes");
return -1;
}
if (pst_load_extended_attributes (pst) < 0) {
pst_error_msg ("Error loading file items");
return -1;
}
return 0;
}
/**
* get_pst_rootname:
* @pst: pst_file structure to be used by libpst
* @filename: if non %NULL, fallback to this name if folder name is not
* available
*
* Open determine root folder name of PST file
*
* Returns: pointer to name of root folder (should be freed by caller),
* or %NULL if error
*/
gchar *
get_pst_rootname (pst_file *pst, gchar *filename)
{
pst_item *item = NULL;
gchar *rootname = NULL;
if ((item = pst_parse_item (pst, pst->d_head, NULL)) == NULL) {
pst_error_msg ("Could not get root record");
return NULL;
}
if (item->message_store == NULL) {
pst_error_msg ("Could not get root message store");
pst_freeItem (item);
return NULL;
}
/* default the file_as to the same as the main filename if it doesn't exist */
if (item->file_as.str == NULL) {
if (filename == NULL) {
pst_freeItem (item);
return NULL;
}
rootname = g_path_get_basename (filename);
} else {
rootname = g_strdup (item->file_as.str);
}
pst_freeItem (item);
return rootname;
}
|