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
|
/* $calcurse: io.c,v 1.31 2008/08/10 09:24:46 culot Exp $ */
/*
* Calcurse - text-based organizer
* Copyright (c) 2004-2008 Frederic Culot
*
* 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 Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Send your feedback or comments to : calcurse@culot.org
* Calcurse home page : http://culot.org/calcurse
*
*/
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <math.h>
#include <unistd.h>
#include <errno.h>
#include "i18n.h"
#include "utils.h"
#include "custom.h"
#include "todo.h"
#include "event.h"
#include "apoint.h"
#include "io.h"
typedef enum
{
PROGRESS_BAR_SAVE,
PROGRESS_BAR_LOAD,
PROGRESS_BAR_EXPORT
} progress_bar_t;
/* Type definition for callbacks to multiple-mode export functions. */
typedef void (*cb_export_t)(FILE *);
typedef void (*cb_dump_t)(FILE *, long, long, char *);
/* Static functions used to add export functionalities. */
static void ical_export_header (FILE *);
static void ical_export_recur_events (FILE *);
static void ical_export_events (FILE *);
static void ical_export_recur_apoints (FILE *);
static void ical_export_apoints (FILE *);
static void ical_export_todo (FILE *);
static void ical_export_footer (FILE *);
static void pcal_export_header (FILE *);
static void pcal_export_recur_events (FILE *);
static void pcal_export_events (FILE *);
static void pcal_export_recur_apoints (FILE *);
static void pcal_export_apoints (FILE *);
static void pcal_export_todo (FILE *);
static void pcal_export_footer (FILE *);
cb_export_t cb_export_header[IO_EXPORT_NBTYPES] =
{ical_export_header, pcal_export_header};
cb_export_t cb_export_recur_events[IO_EXPORT_NBTYPES] =
{ical_export_recur_events, pcal_export_recur_events};
cb_export_t cb_export_events[IO_EXPORT_NBTYPES] =
{ical_export_events, pcal_export_events};
cb_export_t cb_export_recur_apoints[IO_EXPORT_NBTYPES] =
{ical_export_recur_apoints, pcal_export_recur_apoints};
cb_export_t cb_export_apoints[IO_EXPORT_NBTYPES] =
{ical_export_apoints, pcal_export_apoints};
cb_export_t cb_export_todo[IO_EXPORT_NBTYPES] =
{ical_export_todo, pcal_export_todo};
cb_export_t cb_export_footer[IO_EXPORT_NBTYPES] =
{ical_export_footer, pcal_export_footer};
static char *ical_recur_type[RECUR_TYPES] =
{ "", "DAILY", "WEEKLY", "MONTHLY", "YEARLY" };
/* Draw a progress bar while saving, loading or exporting data. */
static void
progress_bar (progress_bar_t type, int progress)
{
#define SLEEPTIME 125000
#define STEPS 3
#define LABELENGTH 15
int i, step;
char *mesg_sav = _("Saving...");
char *mesg_load = _("Loading...");
char *mesg_export = _("Exporting...");
char *barchar = "|";
char file[STEPS][LABELENGTH] = {
"[ conf ]",
"[ todo ]",
"[ apts ]"
};
char data[STEPS][LABELENGTH] = {
"[ events ]",
"[appointments]",
"[ todo ]"
};
int ipos = LABELENGTH + 2;
int epos[STEPS];
/* progress bar length init. */
ipos = LABELENGTH + 2;
step = floor (col / (STEPS + 1));
for (i = 0; i < STEPS - 1; i++)
epos[i] = (i + 2) * step;
epos[STEPS - 1] = col - 2;
switch (type)
{
case PROGRESS_BAR_SAVE:
status_mesg (mesg_sav, file[progress]);
break;
case PROGRESS_BAR_LOAD:
status_mesg (mesg_load, file[progress]);
break;
case PROGRESS_BAR_EXPORT:
status_mesg (mesg_export, data[progress]);
break;
}
/* Draw the progress bar. */
mvwprintw (win[STA].p, 1, ipos, barchar);
mvwprintw (win[STA].p, 1, epos[STEPS - 1], barchar);
custom_apply_attr (win[STA].p, ATTR_HIGHEST);
for (i = ipos + 1; i < epos[progress]; i++)
mvwaddch (win[STA].p, 1, i, ' ' | A_REVERSE);
custom_remove_attr (win[STA].p, ATTR_HIGHEST);
wmove (win[STA].p, 0, 0);
wrefresh (win[STA].p);
usleep (SLEEPTIME);
}
/* Ask user for a file name to export data to. */
static FILE *
get_export_stream (export_type_t type)
{
FILE *stream;
int cancel;
char *home, *stream_name;
char *question = _("Choose the file used to export calcurse data:");
char *wrong_name =
_("The file cannot be accessed, please enter another file name.");
char *press_enter = _("Press [ENTER] to continue.");
const char *file_ext[IO_EXPORT_NBTYPES] = {"ical", "txt"};
stream = NULL;
stream_name = (char *) malloc (BUFSIZ);
if ((home = getenv ("HOME")) != NULL)
snprintf (stream_name, BUFSIZ, "%s/calcurse.%s", home, file_ext[type]);
else
snprintf (stream_name, BUFSIZ, "/tmp/calcurse.%s", file_ext[type]);
while (stream == NULL)
{
status_mesg (question, "");
cancel = updatestring (win[STA].p, &stream_name, 0, 1);
if (cancel)
{
free (stream_name);
return (NULL);
}
stream = fopen (stream_name, "w");
if (stream == NULL)
{
status_mesg (wrong_name, press_enter);
wgetch (win[STA].p);
}
}
free (stream_name);
return (stream);
}
/* Travel through each occurence of an item, and execute the given callback
* ( mainly used to export data).
*/
static void
foreach_date_dump (const long date_end, struct rpt_s *rpt, struct days_s *exc,
long item_first_date, long item_dur, char *item_mesg,
cb_dump_t cb_dump, FILE *stream)
{
long date;
date = item_first_date;
while (date <= date_end && date <= rpt->until)
{
if (!recur_day_is_exc (date, exc))
{
(*cb_dump)(stream, date, item_dur, item_mesg);
}
switch (rpt->type)
{
case RECUR_DAILY:
date = date_sec_change (date, 0, rpt->freq);
break;
case RECUR_WEEKLY:
date = date_sec_change (date, 0, rpt->freq * WEEKINDAYS);
break;
case RECUR_MONTHLY:
date = date_sec_change (date, rpt->freq, 0);
break;
case RECUR_YEARLY:
date = date_sec_change (date, rpt->freq * 12, 0);
break;
default:
fputs (_("FATAL ERROR in foreach_date_dump: "
"incoherent repetition type\n"), stderr);
exit (EXIT_FAILURE);
/* NOTREACHED */
break;
}
}
}
/* iCal alarm notification. */
static void
ical_export_valarm (FILE *stream)
{
fprintf (stream, "BEGIN:VALARM\n");
pthread_mutex_lock (&nbar->mutex);
fprintf (stream, "TRIGGER:-P%dS\n", nbar->cntdwn);
pthread_mutex_unlock (&nbar->mutex);
fprintf (stream, "ACTION:DISPLAY\n");
fprintf (stream, "END:VALARM\n");
}
/* Export header. */
static void
ical_export_header (FILE *stream)
{
fprintf (stream, "BEGIN:VCALENDAR\n");
fprintf (stream, "PRODID:-//calcurse//NONSGML v%s//EN\n", VERSION);
fprintf (stream, "VERSION:2.0\n");
}
static void
pcal_export_header (FILE *stream)
{
fprintf (stream, "# calcurse pcal export\n");
fprintf (stream, "\n# =======\n# options\n# =======\n");
fprintf (stream, "opt -A -K -l -m -F %s\n",
calendar_week_begins_on_monday () ?
"Monday" : "Sunday");
fprintf (stream, "# Display week number (i.e. 1-52) on every Monday\n");
fprintf (stream, "all monday in all %s %%w\n", _("Week"));
fprintf (stream, "\n");
}
/* Export footer. */
static void
ical_export_footer (FILE *stream)
{
fprintf (stream, "END:VCALENDAR\n");
}
static void
pcal_export_footer (FILE *stream)
{
}
/* Export recurrent events. */
static void
ical_export_recur_events (FILE *stream)
{
struct recur_event_s *i;
struct days_s *day;
char ical_date[BUFSIZ];
for (i = recur_elist; i != 0; i = i->next)
{
date_sec2ical_date (i->day, ical_date);
fprintf (stream, "BEGIN:VEVENT\n");
fprintf (stream, "DTSTART:%s\n", ical_date);
fprintf (stream, "RRULE:FREQ=%s;INTERVAL=%d",
ical_recur_type[i->rpt->type], i->rpt->freq);
if (i->rpt->until != 0)
{
date_sec2ical_date (i->rpt->until, ical_date);
fprintf (stream, ";UNTIL=%s\n", ical_date);
}
else
fprintf (stream, "\n");
if (i->exc != NULL)
{
date_sec2ical_date (i->exc->st, ical_date);
fprintf (stream, "EXDATE:%s", ical_date);
for (day = i->exc->next; day; day = day->next)
{
date_sec2ical_date (day->st, ical_date);
fprintf (stream, ",%s", ical_date);
}
fprintf (stream, "\n");
}
fprintf (stream, "SUMMARY:%s\n", i->mesg);
fprintf (stream, "END:VEVENT\n");
}
}
/* Format and dump event data to a pcal formatted file. */
static void
pcal_dump_event (FILE *stream, long event_date, long event_dur,
char *event_mesg)
{
char pcal_date[BUFSIZ];
date_sec2date_fmt (event_date, "%b %d", pcal_date);
fprintf (stream, "%s %s\n", pcal_date, event_mesg);
}
/* Format and dump appointment data to a pcal formatted file. */
static void
pcal_dump_apoint (FILE *stream, long apoint_date, long apoint_dur,
char *apoint_mesg)
{
char pcal_date[BUFSIZ], pcal_beg[BUFSIZ], pcal_end[BUFSIZ];
date_sec2date_fmt (apoint_date, "%b %d", pcal_date);
date_sec2date_fmt (apoint_date, "%R", pcal_beg);
date_sec2date_fmt (apoint_date + apoint_dur, "%R", pcal_end);
fprintf (stream, "%s ", pcal_date);
fprintf (stream, "(%s -> %s) %s\n", pcal_beg, pcal_end, apoint_mesg);
}
static void
pcal_export_recur_events (FILE *stream)
{
struct recur_event_s *i;
char pcal_date[BUFSIZ];
fprintf (stream, "\n# =============");
fprintf (stream, "\n# Recur. Events");
fprintf (stream, "\n# =============\n");
fprintf (stream,
"# (pcal does not support from..until dates specification\n");
for (i = recur_elist; i != 0; i = i->next)
{
if (i->rpt->until == 0 && i->rpt->freq == 1)
{
switch (i->rpt->type)
{
case RECUR_DAILY:
date_sec2date_fmt (i->day, "%b %d", pcal_date);
fprintf (stream, "all day on_or_after %s %s\n",
pcal_date, i->mesg);
break;
case RECUR_WEEKLY:
date_sec2date_fmt (i->day, "%a", pcal_date);
fprintf (stream, "all %s on_or_after ", pcal_date);
date_sec2date_fmt (i->day, "%b %d", pcal_date);
fprintf (stream, "%s %s\n", pcal_date, i->mesg);
break;
case RECUR_MONTHLY:
date_sec2date_fmt (i->day, "%d", pcal_date);
fprintf (stream, "day on all %s %s\n", pcal_date, i->mesg);
break;
case RECUR_YEARLY:
date_sec2date_fmt (i->day, "%b %d", pcal_date);
fprintf (stream, "%s %s\n", pcal_date, i->mesg);
break;
default:
fputs (_("FATAL ERROR in pcal_export_recur_events: "
"incoherent repetition type\n"), stderr);
exit (EXIT_FAILURE);
break;
}
}
else
{
const long YEAR_START = calendar_start_of_year ();
const long YEAR_END = calendar_end_of_year ();
if (i->day < YEAR_END && i->day > YEAR_START)
foreach_date_dump (YEAR_END, i->rpt, i->exc, i->day, 0, i->mesg,
(cb_dump_t) pcal_dump_event, stream);
}
}
}
/* Export events. */
static void
ical_export_events (FILE *stream)
{
struct event_s *i;
char ical_date[BUFSIZ];
for (i = eventlist; i != 0; i = i->next)
{
date_sec2ical_date (i->day, ical_date);
fprintf (stream, "BEGIN:VEVENT\n");
fprintf (stream, "DTSTART:%s\n", ical_date);
fprintf (stream, "SUMMARY:%s\n", i->mesg);
fprintf (stream, "END:VEVENT\n");
}
}
static void
pcal_export_events (FILE *stream)
{
struct event_s *i;
fprintf (stream, "\n# ======\n# Events\n# ======\n");
for (i = eventlist; i != 0; i = i->next)
pcal_dump_event (stream, i->day, 0, i->mesg);
fprintf (stream, "\n");
}
/* Export recurrent appointments. */
static void
ical_export_recur_apoints (FILE *stream)
{
recur_apoint_llist_node_t *i;
struct days_s *day;
char ical_datetime[BUFSIZ];
char ical_date[BUFSIZ];
pthread_mutex_lock (&(recur_alist_p->mutex));
for (i = recur_alist_p->root; i != 0; i = i->next)
{
date_sec2ical_datetime (i->start, ical_datetime);
fprintf (stream, "BEGIN:VEVENT\n");
fprintf (stream, "DTSTART:%s\n", ical_datetime);
fprintf (stream, "DURATION:P%ldS\n", i->dur);
fprintf (stream, "RRULE:FREQ=%s;INTERVAL=%d",
ical_recur_type[i->rpt->type], i->rpt->freq);
if (i->rpt->until != 0)
{
date_sec2ical_date (i->rpt->until + HOURINSEC, ical_date);
fprintf (stream, ";UNTIL=%s\n", ical_date);
}
else
fprintf (stream, "\n");
if (i->exc != NULL)
{
date_sec2ical_date (i->exc->st, ical_date);
fprintf (stream, "EXDATE:%s", ical_date);
for (day = i->exc->next; day; day = day->next)
{
date_sec2ical_date (day->st, ical_date);
fprintf (stream, ",%s", ical_date);
}
fprintf (stream, "\n");
}
fprintf (stream, "SUMMARY:%s\n", i->mesg);
if (i->state & APOINT_NOTIFY)
ical_export_valarm (stream);
fprintf (stream, "END:VEVENT\n");
}
pthread_mutex_unlock (&(recur_alist_p->mutex));
}
static void
pcal_export_recur_apoints (FILE *stream)
{
recur_apoint_llist_node_t *i;
char pcal_date[BUFSIZ], pcal_beg[BUFSIZ], pcal_end[BUFSIZ];
fprintf (stream, "\n# ==============");
fprintf (stream, "\n# Recur. Apoints");
fprintf (stream, "\n# ==============\n");
fprintf (stream,
"# (pcal does not support from..until dates specification\n");
for (i = recur_alist_p->root; i != 0; i = i->next)
{
if (i->rpt->until == 0 && i->rpt->freq == 1)
{
date_sec2date_fmt (i->start, "%R", pcal_beg);
date_sec2date_fmt (i->start + i->dur, "%R", pcal_end);
switch (i->rpt->type)
{
case RECUR_DAILY:
date_sec2date_fmt (i->start, "%b %d", pcal_date);
fprintf (stream, "all day on_or_after %s (%s -> %s) %s\n",
pcal_date, pcal_beg, pcal_end, i->mesg);
break;
case RECUR_WEEKLY:
date_sec2date_fmt (i->start, "%a", pcal_date);
fprintf (stream, "all %s on_or_after ", pcal_date);
date_sec2date_fmt (i->start, "%b %d", pcal_date);
fprintf (stream, "%s (%s -> %s) %s\n", pcal_date,
pcal_beg, pcal_end, i->mesg);
break;
case RECUR_MONTHLY:
date_sec2date_fmt (i->start, "%d", pcal_date);
fprintf (stream, "day on all %s (%s -> %s) %s\n", pcal_date,
pcal_beg, pcal_end, i->mesg);
break;
case RECUR_YEARLY:
date_sec2date_fmt (i->start, "%b %d", pcal_date);
fprintf (stream, "%s (%s -> %s) %s\n", pcal_date,
pcal_beg, pcal_end, i->mesg);
break;
default:
fputs (_("FATAL ERROR in pcal_export_recur_apoints: "
"incoherent repetition type\n"), stderr);
exit (EXIT_FAILURE);
break;
}
}
else
{
const long YEAR_START = calendar_start_of_year ();
const long YEAR_END = calendar_end_of_year ();
if (i->start < YEAR_END && i->start > YEAR_START)
foreach_date_dump (YEAR_END, i->rpt, i->exc, i->start, i->dur,
i->mesg, (cb_dump_t)pcal_dump_apoint, stream);
}
}
}
/* Export appointments. */
static void
ical_export_apoints (FILE *stream)
{
apoint_llist_node_t *i;
char ical_datetime[BUFSIZ];
pthread_mutex_lock (&(alist_p->mutex));
for (i = alist_p->root; i != 0; i = i->next)
{
date_sec2ical_datetime (i->start, ical_datetime);
fprintf (stream, "BEGIN:VEVENT\n");
fprintf (stream, "DTSTART:%s\n", ical_datetime);
fprintf (stream, "DURATION:P%ldS\n", i->dur);
fprintf (stream, "SUMMARY:%s\n", i->mesg);
if (i->state & APOINT_NOTIFY)
ical_export_valarm (stream);
fprintf (stream, "END:VEVENT\n");
}
pthread_mutex_unlock (&(alist_p->mutex));
}
static void
pcal_export_apoints (FILE *stream)
{
fprintf (stream, "\n# ============\n# Appointments\n# ============\n");
apoint_llist_node_t *i;
pthread_mutex_lock (&(alist_p->mutex));
for (i = alist_p->root; i != 0; i = i->next)
pcal_dump_apoint (stream, i->start, i->dur, i->mesg);
pthread_mutex_unlock (&(alist_p->mutex));
fprintf (stream, "\n");
}
/* Export todo items. */
static void
ical_export_todo (FILE *stream)
{
struct todo_s *i;
for (i = todolist; i != 0; i = i->next)
{
fprintf (stream, "BEGIN:VTODO\n");
fprintf (stream, "PRIORITY:%d\n", i->id);
fprintf (stream, "SUMMARY:%s\n", i->mesg);
fprintf (stream, "END:VTODO\n");
}
}
static void
pcal_export_todo (FILE *stream)
{
struct todo_s *i;
fprintf (stream, "#\n# Todos\n#\n");
for (i = todolist; i != 0; i = i->next)
{
fprintf (stream, "note all ");
fprintf (stream, "%d. %s\n", i->id, i->mesg);
}
fprintf (stream, "\n");
}
/*
* Initialization of data paths. The cfile argument is the variable
* which contains the calendar file. If none is given, then the default
* one (~/.calcurse/apts) is taken. If the one given does not exist, it
* is created.
* The datadir argument can be use to specify an alternative data root dir.
*/
void
io_init (char *cfile, char *datadir)
{
FILE *data_file;
char *home;
char apts_file[BUFSIZ] = "";
int ch;
if (datadir != NULL)
{
home = datadir;
snprintf (path_dir, BUFSIZ, "%s", home);
snprintf (path_todo, BUFSIZ, "%s/" TODO_PATH_NAME, home);
snprintf (path_conf, BUFSIZ, "%s/" CONF_PATH_NAME, home);
snprintf (path_notes, BUFSIZ, "%s/" NOTES_DIR_NAME, home);
snprintf (path_apts, BUFSIZ, "%s/" APTS_PATH_NAME, home);
}
else
{
home = getenv ("HOME");
if (home == NULL)
{
home = ".";
}
snprintf (path_dir, BUFSIZ, "%s", home);
snprintf (path_todo, BUFSIZ, "%s/" TODO_PATH, home);
snprintf (path_conf, BUFSIZ, "%s/" CONF_PATH, home);
snprintf (path_notes, BUFSIZ, "%s/" NOTES_DIR, home);
if (cfile == NULL)
{
snprintf (path_apts, BUFSIZ, "%s/" APTS_PATH, home);
}
else
{
snprintf (apts_file, BUFSIZ, "%s", cfile);
strncpy (path_apts, apts_file, BUFSIZ);
/* check if the file exists, otherwise create it */
data_file = fopen (path_apts, "r");
if (data_file == NULL)
{
printf (_("%s does not exist, create it now [y or n] ? "),
path_apts);
ch = getchar ();
switch (ch)
{
case 'N':
case 'n':
printf (_("aborting...\n"));
exit (EXIT_FAILURE);
break;
case 'Y':
case 'y':
data_file = fopen (path_apts, "w");
if (data_file == NULL)
{
perror (path_apts);
exit (EXIT_FAILURE);
}
else
{
printf (_("%s successfully created\n"), path_apts);
printf (_("starting interactive mode...\n"));
}
break;
default:
printf (_("aborting...\n"));
exit (EXIT_FAILURE);
break;
}
}
fclose (data_file);
}
}
}
/* get data from file */
void
io_extract_data (char *dst_data, const char *org, int len)
{
for (;;)
{
if (*org == '\n' || *org == '\0')
break;
*dst_data++ = *org++;
}
*dst_data = '\0';
}
/* Save the calendar data */
void
io_save_cal (conf_t *conf)
{
FILE *data_file;
struct event_s *k;
apoint_llist_node_t *j;
struct todo_s *i;
char theme_name[BUFSIZ];
char *access_pb = _("Problems accessing data file ...");
char *config_txt =
"#\n"
"# Calcurse configuration file\n#\n"
"# This file sets the configuration options used by Calcurse. These\n"
"# options are usually set from within Calcurse. A line beginning with \n"
"# a space or tab is considered to be a continuation of the previous "
"line.\n"
"# For a variable to be unset its value must be blank.\n"
"# To set a variable to the empty string its value should be \"\".\n"
"# Lines beginning with \"#\" are comments, and ignored by Calcurse.\n";
char *save_success = _("The data files were successfully saved");
char *enter = _("Press [ENTER] to continue");
bool show_bar = false;
if (!conf->skip_progress_bar)
show_bar = true;
/* Save the user configuration. */
if (show_bar)
progress_bar (PROGRESS_BAR_SAVE, 0);
data_file = fopen (path_conf, "w");
if (data_file == (FILE *) 0)
status_mesg (access_pb, "");
else
{
custom_color_theme_name (theme_name);
fprintf (data_file, "%s\n", config_txt);
fprintf (data_file,
"# If this option is set to yes, "
"automatic save is done when quitting\n");
fprintf (data_file, "auto_save=\n");
fprintf (data_file, "%s\n", (conf->auto_save) ? "yes" : "no");
fprintf (data_file,
"\n# If this option is set to yes, "
"confirmation is required before quitting\n");
fprintf (data_file, "confirm_quit=\n");
fprintf (data_file, "%s\n", (conf->confirm_quit) ? "yes" : "no");
fprintf (data_file,
"\n# If this option is set to yes, "
"confirmation is required before deleting an event\n");
fprintf (data_file, "confirm_delete=\n");
fprintf (data_file, "%s\n", (conf->confirm_delete) ? "yes" : "no");
fprintf (data_file,
"\n# If this option is set to yes, "
"messages about loaded and saved data will not be displayed\n");
fprintf (data_file, "skip_system_dialogs=\n");
fprintf (data_file, "%s\n", (conf->skip_system_dialogs) ? "yes" : "no");
fprintf (data_file,
"\n# If this option is set to yes, progress bar appearing "
"when saving data will not be displayed\n");
fprintf (data_file, "skip_progress_bar=\n");
fprintf (data_file, "%s\n", (conf->skip_progress_bar) ? "yes" : "no");
fprintf (data_file,
"\n# If this option is set to yes, "
"monday is the first day of the week, else it is sunday\n");
fprintf (data_file, "week_begins_on_monday=\n");
fprintf (data_file, "%s\n",
(calendar_week_begins_on_monday ())? "yes" : "no");
fprintf (data_file, "\n# This is the color theme used for menus :\n");
fprintf (data_file, "color-theme=\n");
fprintf (data_file, "%s\n", theme_name);
fprintf (data_file, "\n# This is the layout of the calendar :\n");
fprintf (data_file, "layout=\n");
fprintf (data_file, "%d\n", wins_layout ());
pthread_mutex_lock (&nbar->mutex);
fprintf (data_file,
"\n# If this option is set to yes, "
"notify-bar will be displayed :\n");
fprintf (data_file, "notify-bar_show=\n");
fprintf (data_file, "%s\n", (nbar->show) ? "yes" : "no");
fprintf (data_file,
"\n# Format of the date to be displayed inside notify-bar :\n");
fprintf (data_file, "notify-bar_date=\n");
fprintf (data_file, "%s\n", nbar->datefmt);
fprintf (data_file,
"\n# Format of the time to be displayed inside notify-bar :\n");
fprintf (data_file, "notify-bar_clock=\n");
fprintf (data_file, "%s\n", nbar->timefmt);
fprintf (data_file,
"\n# Warn user if he has an appointment within next "
"'notify-bar_warning' seconds :\n");
fprintf (data_file, "notify-bar_warning=\n");
fprintf (data_file, "%d\n", nbar->cntdwn);
fprintf (data_file,
"\n# Command used to notify user of "
"an upcoming appointment :\n");
fprintf (data_file, "notify-bar_command=\n");
fprintf (data_file, "%s\n", nbar->cmd);
fprintf (data_file,
"\n# Format of the date to be displayed "
"in non-interactive mode :\n");
fprintf (data_file, "output_datefmt=\n");
fprintf (data_file, "%s\n", conf->output_datefmt);
fprintf (data_file,
"\n# Format to be used when entering a date "
"(1-mm/dd/yyyy, 2-dd/mm/yyyy, 3-yyyy/mm/dd) :\n");
fprintf (data_file, "input_datefmt=\n");
fprintf (data_file, "%d\n", conf->input_datefmt);
pthread_mutex_unlock (&nbar->mutex);
fclose (data_file);
}
/* Save the todo data file. */
if (show_bar)
progress_bar (PROGRESS_BAR_SAVE, 1);
data_file = fopen (path_todo, "w");
if (data_file == (FILE *) 0)
status_mesg (access_pb, "");
else
{
for (i = todolist; i != 0; i = i->next)
{
if (i->note != NULL)
fprintf (data_file, "[%d]>%s %s\n", i->id, i->note, i->mesg);
else
fprintf (data_file, "[%d] %s\n", i->id, i->mesg);
}
fclose (data_file);
}
/*
* Save the apts data file, which contains the
* appointments first, and then the events.
* Recursive items are written first.
*/
if (show_bar)
progress_bar (PROGRESS_BAR_SAVE, 2);
data_file = fopen (path_apts, "w");
if (data_file == (FILE *) 0)
status_mesg (access_pb, "");
else
{
recur_save_data (data_file);
pthread_mutex_lock (&(alist_p->mutex));
for (j = alist_p->root; j != 0; j = j->next)
apoint_write (j, data_file);
pthread_mutex_unlock (&(alist_p->mutex));
for (k = eventlist; k != 0; k = k->next)
event_write (k, data_file);
fclose (data_file);
}
/* Print a message telling data were saved */
if (!conf->skip_system_dialogs)
{
status_mesg (save_success, enter);
wgetch (win[STA].p);
}
}
/*
* Check what type of data is written in the appointment file,
* and then load either: a new appointment, a new event, or a new
* recursive item (which can also be either an event or an appointment).
*/
void
io_load_app (void)
{
FILE *data_file;
int c, is_appointment, is_event, is_recursive;
struct tm start, end, until, *lt;
struct days_s *exc = NULL;
time_t t;
int id = 0;
int freq;
char type, state = 0L;
char note[NOTESIZ + 1], *notep;
char *error =
_("FATAL ERROR in io_load_app: wrong format in the appointment or event\n");
t = time (NULL);
lt = localtime (&t);
start = end = until = *lt;
data_file = fopen (path_apts, "r");
for (;;)
{
is_appointment = is_event = is_recursive = 0;
c = getc (data_file);
if (c == EOF)
break;
ungetc (c, data_file);
/* Read the date first: it is common to both events
* and appointments.
*/
if (fscanf (data_file, "%u / %u / %u ",
&start.tm_mon, &start.tm_mday, &start.tm_year) != 3)
{
fputs (_("FATAL ERROR in io_load_app: "
"syntax error in the item date\n"), stderr);
exit (EXIT_FAILURE);
}
/* Read the next character : if it is an '@' then we have
* an appointment, else if it is an '[' we have en event.
*/
c = getc (data_file);
if (c == '@')
is_appointment = 1;
else if (c == '[')
is_event = 1;
else
{
fputs (_("FATAL ERROR in io_load_app: "
"no event nor appointment found\n"), stderr);
exit (EXIT_FAILURE);
}
ungetc (c, data_file);
/* Read the remaining informations. */
if (is_appointment)
{
fscanf (data_file, "@ %u : %u -> %u / %u / %u @ %u : %u ",
&start.tm_hour, &start.tm_min,
&end.tm_mon, &end.tm_mday, &end.tm_year,
&end.tm_hour, &end.tm_min);
}
else if (is_event)
{
fscanf (data_file, "[%d] ", &id);
}
else
{ /* NOT REACHED */
fputs (error, stderr);
exit (EXIT_FAILURE);
}
/* Check if we have a recursive item. */
c = getc (data_file);
if (c == '{')
{
ungetc (c, data_file);
is_recursive = 1;
fscanf (data_file, "{ %d%c ", &freq, &type);
c = getc (data_file);
if (c == '}')
{ /* endless recurrent item */
ungetc (c, data_file);
fscanf (data_file, "} ");
until.tm_year = 0;
}
else if (c == '-')
{
ungetc (c, data_file);
fscanf (data_file, " -> %u / %u / %u ",
&until.tm_mon, &until.tm_mday, &until.tm_year);
c = getc (data_file);
if (c == '!')
{
ungetc (c, data_file);
exc = recur_exc_scan (data_file);
c = getc (data_file);
}
else
{
ungetc (c, data_file);
fscanf (data_file, "} ");
}
}
else if (c == '!')
{ // endless item with exceptions
ungetc (c, data_file);
exc = recur_exc_scan (data_file);
c = getc (data_file);
until.tm_year = 0;
}
else
{ /* NOT REACHED */
fputs (error, stderr);
exit (EXIT_FAILURE);
}
}
else
ungetc (c, data_file);
/* Check if a note is attached to the item. */
c = getc (data_file);
if (c == '>')
{
fgets (note, NOTESIZ + 1, data_file);
note[NOTESIZ] = '\0';
notep = note;
getc (data_file);
}
else
{
notep = NULL;
ungetc (c, data_file);
}
/*
* Last: read the item description and load it into its
* corresponding linked list, depending on the item type.
*/
if (is_appointment)
{
c = getc (data_file);
if (c == '!')
{
ungetc (c, data_file);
fscanf (data_file, " ! ");
state |= APOINT_NOTIFY;
}
else
{
ungetc (c, data_file);
fscanf (data_file, " | ");
state = 0L;
}
if (is_recursive)
{
recur_apoint_scan (data_file, start, end,
type, freq, until, notep, exc, state);
}
else
{
apoint_scan (data_file, start, end, state, notep);
}
}
else if (is_event)
{
if (is_recursive)
{
recur_event_scan (data_file, start, id, type,
freq, until, notep, exc);
}
else
{
event_scan (data_file, start, id, notep);
}
}
else
{ /* NOT REACHED */
fputs (error, stderr);
exit (EXIT_FAILURE);
}
}
fclose (data_file);
}
/* Load the todo data */
void
io_load_todo (void)
{
FILE *data_file;
char *mesg_line1 = _("Failed to open todo file");
char *mesg_line2 = _("Press [ENTER] to continue");
char *nl;
int nb_tod = 0;
int c, id;
char buf[BUFSIZ], e_todo[BUFSIZ], note[NOTESIZ + 1];
data_file = fopen (path_todo, "r");
if (data_file == NULL)
{
status_mesg (mesg_line1, mesg_line2);
wgetch (win[STA].p);
}
for (;;)
{
c = getc (data_file);
if (c == EOF)
{
break;
}
else if (c == '[')
{ /* new style with id */
fscanf (data_file, "%d]", &id);
}
else
{
id = 9;
ungetc (c, data_file);
}
/* Now read the attached note, if any. */
c = getc (data_file);
if (c == '>')
{
fgets (note, NOTESIZ + 1, data_file);
note[NOTESIZ] = '\0';
getc (data_file);
}
else
note[0] = '\0';
/* Then read todo description. */
fgets (buf, BUFSIZ, data_file);
nl = strchr (buf, '\n');
if (nl)
{
*nl = '\0';
}
io_extract_data (e_todo, buf, strlen (buf));
todo_add (e_todo, id, note);
++nb_tod;
}
fclose (data_file);
todo_set_nb (nb_tod);
}
static void
check_directory (char *dir, int *missing)
{
errno = 0;
if (mkdir (dir, 0700) != 0)
{
if (errno != EEXIST)
{
fprintf (stderr, _("FATAL ERROR: could not create %s: %s\n"),
dir, strerror (errno));
exit (EXIT_FAILURE);
}
}
else
(*missing)++;
}
static void
check_file (char *file, int *missing)
{
FILE *fd;
errno = 0;
if ((fd = fopen (file, "r")) == NULL)
{
(*missing)++;
if ((fd = fopen (file, "w")) == NULL)
{
fprintf (stderr, _("FATAL ERROR: could not create %s: %s\n"),
file, strerror (errno));
exit (EXIT_FAILURE);
}
}
fclose (fd);
}
/*
* Checks if data files exist. If not, create them.
* The following structure has to be created:
*
* $HOME/.calcurse/
* |
* +--- notes/
* |___ conf
* |___ apts
* |___ todo
*/
int
io_check_data_files (void)
{
int missing;
missing = 0;
errno = 0;
check_directory (path_dir, &missing);
check_directory (path_notes, &missing);
check_file (path_todo, &missing);
check_file (path_apts, &missing);
check_file (path_conf, &missing);
return (missing);
}
/* Draw the startup screen */
void
io_startup_screen (bool skip_dialogs, int no_data_file)
{
char *welcome_mesg =
_("Welcome to Calcurse. Missing data files were created.");
char *data_mesg = _("Data files found. Data will be loaded now.");
char *enter = _("Press [ENTER] to continue");
if (no_data_file != 0)
{
status_mesg (welcome_mesg, enter);
wgetch (win[STA].p);
}
else if (!skip_dialogs)
{
status_mesg (data_mesg, enter);
wgetch (win[STA].p);
}
}
/* Export calcurse data. */
void
io_export_data (export_mode_t mode, export_type_t type, conf_t *conf)
{
FILE *stream;
char *wrong_mode = _("FATAL ERROR in io_export_data: wrong export mode\n");
char *wrong_type = _("FATAL ERROR in io_export_data: unknown export type\n");
char *success = _("The data were successfully exported");
char *enter = _("Press [ENTER] to continue");
if (type < IO_EXPORT_ICAL || type >= IO_EXPORT_NBTYPES)
{
fputs (wrong_type, stderr);
exit (EXIT_FAILURE);
}
switch (mode)
{
case IO_EXPORT_NONINTERACTIVE:
stream = stdout;
break;
case IO_EXPORT_INTERACTIVE:
stream = get_export_stream (type);
break;
default:
fputs (wrong_mode, stderr);
exit (EXIT_FAILURE);
/* NOTREACHED */
}
if (stream == NULL)
return;
cb_export_header[type] (stream);
if (!conf->skip_progress_bar && mode == IO_EXPORT_INTERACTIVE)
progress_bar (PROGRESS_BAR_EXPORT, 0);
cb_export_recur_events[type] (stream);
cb_export_events[type] (stream);
if (!conf->skip_progress_bar && mode == IO_EXPORT_INTERACTIVE)
progress_bar (PROGRESS_BAR_EXPORT, 1);
cb_export_recur_apoints[type] (stream);
cb_export_apoints[type] (stream);
if (!conf->skip_progress_bar && mode == IO_EXPORT_INTERACTIVE)
progress_bar (PROGRESS_BAR_EXPORT, 2);
cb_export_todo[type] (stream);
cb_export_footer[type] (stream);
if (stream != stdout)
fclose (stream);
if (!conf->skip_system_dialogs && mode == IO_EXPORT_INTERACTIVE)
{
status_mesg (success, enter);
wgetch (win[STA].p);
}
}
/* Draws the export format selection bar */
void
io_export_bar (void)
{
int smlspc, spc;
smlspc = 2;
spc = 15;
custom_apply_attr (win[STA].p, ATTR_HIGHEST);
mvwprintw (win[STA].p, 0, 2, "Q");
mvwprintw (win[STA].p, 1, 2, "I");
mvwprintw (win[STA].p, 0, 2 + spc, "P");
custom_remove_attr (win[STA].p, ATTR_HIGHEST);
mvwprintw (win[STA].p, 0, 2 + smlspc, _("Exit"));
mvwprintw (win[STA].p, 1, 2 + smlspc, _("Ical"));
mvwprintw (win[STA].p, 0, 2 + spc + smlspc, _("Pcal"));
wnoutrefresh (win[STA].p);
wmove (win[STA].p, 0, 0);
doupdate ();
}
|