forked from juddmon/jpilot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatebook_gui.c
More file actions
5683 lines (4953 loc) · 197 KB
/
datebook_gui.c
File metadata and controls
5683 lines (4953 loc) · 197 KB
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
/*******************************************************************************
* datebook_gui.c
* A module of J-Pilot http://jpilot.org
*
* Copyright (C) 1999-2014 by Judd Montgomery
*
* 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; version 2 of the License.
*
* 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
******************************************************************************/
/********************************* Includes ***********************************/
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/stat.h>
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
#include <pi-dlp.h>
#include "datebook.h"
#include "calendar.h"
#include "i18n.h"
#include "utils.h"
#include "todo.h"
#include "log.h"
#include "prefs.h"
#include "password.h"
#include "export.h"
#include "print.h"
#include "alarms.h"
#include "stock_buttons.h"
/********************************* Constants **********************************/
//#define EASTER
#define PAGE_NONE 0
#define PAGE_DAY 1
#define PAGE_WEEK 2
#define PAGE_MONTH 3
#define PAGE_YEAR 4
#define BEGIN_DATE_BUTTON 5
/* Maximum length of description (not including the null) */
/* todo check this before every record write */
#define MAX_DESC_LEN 255
#define DATEBOOK_MAX_COLUMN_LEN 80
#define DB_TIME_COLUMN 0
#define DB_NOTE_COLUMN 1
#define DB_ALARM_COLUMN 2
#ifdef ENABLE_DATEBK
# define DB_FLOAT_COLUMN 3
static int DB_APPT_COLUMN=4;
#else
static int DB_APPT_COLUMN=3;
#endif
#define NUM_DATEBOOK_CAT_ITEMS 16
#define NUM_EXPORT_TYPES 3
#define NUM_DBOOK_CSV_FIELDS 19
#define NUM_CALENDAR_CSV_FIELDS 20
/* RFCs use CRLF for Internet newline */
#define CRLF "\x0D\x0A"
#define BROWSE_OK 1
#define BROWSE_CANCEL 2
#define CONNECT_SIGNALS 400
#define DISCONNECT_SIGNALS 401
#define CAL_DAY_SELECTED 327
#define UPDATE_DATE_ENTRIES 0x01
#define UPDATE_DATE_MENUS 0x02
#define START_TIME_FLAG 0x00
#define END_TIME_FLAG 0x80
#define HOURS_FLAG 0x40
/* #define DAY_VIEW */
/******************************* Global vars **********************************/
/* Keeps track of whether code is using Datebook, or Calendar database
* 0 is Datebook, 1 is Calendar */
static long datebook_version=0;
/* This refers to the main jpilot window. This should probably
* be replaced somehow by a GTK call which works out what the
* top-level window is from the widget. Right now it relies
* on the fact that there is only one item titled "window" in
* the global name space */
extern GtkWidget *window;
extern GtkWidget *glob_date_label;
extern gint glob_date_timer_tag;
static GtkWidget *pane;
static GtkWidget *note_pane;
static GtkWidget *todo_pane;
static GtkWidget *todo_vbox;
static struct CalendarAppInfo dbook_app_info;
static int dbook_category = CATEGORY_ALL;
static struct sorted_cats sort_l[NUM_DATEBOOK_CAT_ITEMS];
static GtkWidget *main_calendar;
static GtkWidget *dow_label;
static GtkWidget *clist;
static GtkWidget *dbook_desc, *dbook_note;
static GObject *dbook_desc_buffer,*dbook_note_buffer;
/* Need two extra slots for the ALL category and Edit Categories... */
static GtkWidget *dbook_cat_menu_item1[NUM_DATEBOOK_CAT_ITEMS+2];
static GtkWidget *dbook_cat_menu_item2[NUM_DATEBOOK_CAT_ITEMS];
static GtkWidget *category_menu1;
static GtkWidget *category_menu2;
static GtkWidget *private_checkbox;
static GtkWidget *check_button_alarm;
static GtkWidget *check_button_day_endon;
static GtkWidget *check_button_week_endon;
static GtkWidget *check_button_mon_endon;
static GtkWidget *check_button_year_endon;
static GtkWidget *units_entry;
static GtkWidget *repeat_day_entry;
static GtkWidget *repeat_week_entry;
static GtkWidget *repeat_mon_entry;
static GtkWidget *repeat_year_entry;
static GtkWidget *radio_button_no_time;
static GtkWidget *radio_button_appt_time;
static GtkWidget *radio_button_alarm_min;
static GtkWidget *radio_button_alarm_hour;
static GtkWidget *radio_button_alarm_day;
static GtkWidget *location_entry;
static GtkWidget *glob_endon_day_button;
static struct tm glob_endon_day_tm;
static GtkWidget *glob_endon_week_button;
static struct tm glob_endon_week_tm;
static GtkWidget *glob_endon_mon_button;
static struct tm glob_endon_mon_tm;
static GtkWidget *glob_endon_year_button;
static struct tm glob_endon_year_tm;
static GtkWidget *toggle_button_repeat_days[7];
static GtkWidget *toggle_button_repeat_mon_byday;
static GtkWidget *toggle_button_repeat_mon_bydate;
static GtkWidget *notebook;
static int current_day; /* range 1-31 */
static int current_month; /* range 0-11 */
static int current_year; /* years since 1900 */
static int clist_row_selected;
static int record_changed;
#ifdef ENABLE_DATEBK
int datebk_category=0xFFFF; /* This is a bitmask */
static GtkWidget *datebk_entry;
#endif
static GtkWidget *hbox_alarm1, *hbox_alarm2;
static GtkWidget *scrolled_window;
static struct tm begin_date, end_date;
static GtkWidget *option1, *option2, *option3, *option4;
static GtkWidget *begin_date_button;
static GtkWidget *begin_time_entry, *end_time_entry;
static GtkWidget *new_record_button;
static GtkWidget *apply_record_button;
static GtkWidget *add_record_button;
static GtkWidget *delete_record_button;
static GtkWidget *undelete_record_button;
static GtkWidget *copy_record_button;
static GtkWidget *cancel_record_button;
static GtkAccelGroup *accel_group;
static CalendarEventList *glob_cel = NULL;
/* For todo list */
static GtkWidget *todo_clist;
static GtkWidget *show_todos_button;
static GtkWidget *todo_scrolled_window;
static ToDoList *datebook_todo_list=NULL;
/* For export GUI */
static GtkWidget *export_window;
static GtkWidget *save_as_entry;
static GtkWidget *export_radio_type[NUM_EXPORT_TYPES+1];
static int glob_export_type;
/****************************** Prototypes ************************************/
static void highlight_days(void);
static int datebook_find(void);
static int datebook_update_clist(void);
static void update_endon_button(GtkWidget *button, struct tm *t);
static void cb_clist_selection(GtkWidget *clist,
gint row,
gint column,
GdkEventButton *event,
gpointer data);
static void cb_add_new_record(GtkWidget *widget,
gpointer data);
static void set_new_button_to(int new_state);
static void connect_changed_signals(int con_or_dis);
static int datebook_export_gui(GtkWidget *main_window, int x, int y);
/****************************** Main Code *************************************/
static int datebook_to_text(struct CalendarEvent *cale, char *text, int len)
{
int i;
const char *short_date;
const char *pref_time;
char temp[255];
char text_time[200];
char str_begin_date[20];
char str_begin_time[20];
char str_end_time[20];
char text_repeat_type[40];
char text_repeat_day[200];
char text_end_date[200];
char text_repeat_freq[200];
char text_alarm[40];
char text_repeat_days[200];
char text_exceptions[65535];
char *adv_type[] = {
N_("Minutes"),
N_("Hours"),
N_("Days")
};
char *repeat_type[] = {
N_("Repeat Never"),
N_("Repeat Daily"),
N_("Repeat Weekly"),
N_("Repeat MonthlyByDay"),
N_("Repeat MonthlyByDate"),
N_("Repeat YearlyDate"),
N_("Repeat YearlyDay")
};
char *days[] = {
N_("Su"),
N_("Mo"),
N_("Tu"),
N_("We"),
N_("Th"),
N_("Fr"),
N_("Sa"),
N_("Su")
};
if ((cale->repeatWeekstart<0) ||(cale->repeatWeekstart>6)) {
cale->repeatWeekstart=0;
}
get_pref(PREF_SHORTDATE, NULL, &short_date);
get_pref(PREF_TIME, NULL, &pref_time);
/* Event date/time */
strftime(str_begin_date, sizeof(str_begin_date), short_date, &(cale->begin));
if (cale->event) {
sprintf(text_time, _("Start Date: %s\nTime: Event"),
str_begin_date);
} else {
strftime(str_begin_time, sizeof(str_begin_time), pref_time, &(cale->begin));
strftime(str_end_time, sizeof(str_end_time), pref_time, &(cale->end));
str_begin_date[19]='\0';
str_begin_time[19]='\0';
str_end_time[19]='\0';
sprintf(text_time, _("Start Date: %s\nTime: %s to %s"),
str_begin_date, str_begin_time, str_end_time);
}
/* Alarm */
if (cale->alarm) {
sprintf(text_alarm, " %d ", cale->advance);
i=cale->advanceUnits;
if ((i>-1) && (i<3)) {
strcat(text_alarm, adv_type[i]);
} else {
strcat(text_alarm, _("Unknown"));
}
} else {
text_alarm[0]='\0';
}
/* Repeat Type */
i=cale->repeatType;
if ((i > -1) && (i < 7)) {
strcpy(text_repeat_type, _(repeat_type[i]));
} else {
strcpy(text_repeat_type, _("Unknown"));
}
/* End Date */
strcpy(text_end_date, _("End Date: "));
if (cale->repeatForever) {
strcat(text_end_date, _("Never"));
} else {
strftime(temp, sizeof(temp), short_date, &(cale->repeatEnd));
strcat(text_end_date, temp);
}
strcat(text_end_date, "\n");
sprintf(text_repeat_freq, _("Repeat Frequency: %d\n"), cale->repeatFrequency);
if (cale->repeatType==calendarRepeatNone) {
text_end_date[0]='\0';
text_repeat_freq[0]='\0';
}
/* Repeat Day (for MonthlyByDay) */
text_repeat_day[0]='\0';
if (cale->repeatType==calendarRepeatMonthlyByDay) {
sprintf(text_repeat_day, _("Monthly Repeat Day %d\n"), cale->repeatDay);
}
/* Repeat Days (for weekly) */
text_repeat_days[0]='\0';
if (cale->repeatType==calendarRepeatWeekly) {
strcpy(text_repeat_days, _("Repeat on Days:"));
for (i=0; i<7; i++) {
if (cale->repeatDays[i]) {
strcat(text_repeat_days, " ");
strcat(text_repeat_days, _(days[i]));
}
}
strcat(text_repeat_days, "\n");
}
text_exceptions[0]='\0';
if (cale->exceptions > 0) {
sprintf(text_exceptions, _("Number of exceptions: %d"), cale->exceptions);
for (i=0; i<cale->exceptions; i++) {
strcat(text_exceptions, "\n");
strftime(temp, sizeof(temp), short_date, &(cale->exception[i]));
strcat(text_exceptions, temp);
if (strlen(text_exceptions)>65000) {
strcat(text_exceptions, _("\nmore..."));
break;
}
}
strcat(text_exceptions, "\n");
}
if (datebook_version==0) {
/* DateBook app */
g_snprintf(text, len,
"%s %s\n"
"%s %s\n"
"%s\n"
"%s %s%s\n"
"%s %s\n"
"%s"
"%s"
"%s %s\n"
"%s"
"%s"
"%s",
_("Description:"), cale->description,
_("Note:"), (cale->note ? cale->note : ""),
text_time,
_("Alarm:"), cale->alarm ? _("Yes"):_("No"), text_alarm,
_("Repeat Type:"), text_repeat_type,
text_repeat_freq,
text_end_date,
_("Start of Week:"), _(days[cale->repeatWeekstart]),
text_repeat_day,
text_repeat_days,
text_exceptions
);
} else {
/* Calendar app */
g_snprintf(text, len,
"%s %s\n"
"%s %s\n"
"%s %s\n"
"%s\n"
"%s %s%s\n"
"%s %s\n"
"%s"
"%s"
"%s %s\n"
"%s"
"%s"
"%s",
_("Description:"), cale->description,
_("Note:"), (cale->note ? cale->note : ""),
_("Location:"), (cale->location ? cale->location : ""),
text_time,
_("Alarm:"), cale->alarm ? _("Yes"):_("No"), text_alarm,
_("Repeat Type:"), text_repeat_type,
text_repeat_freq,
text_end_date,
_("Start of Week:"), _(days[cale->repeatWeekstart]),
text_repeat_day,
text_repeat_days,
text_exceptions
);
}
return EXIT_SUCCESS;
}
/*************** Start Import Code ***************/
static int cb_dbook_import(GtkWidget *parent_window, const char *file_path, int type)
{
FILE *in;
char text[65536];
char description[65536];
char note[65536];
char location[65536];
struct CalendarEvent new_cale;
unsigned char attrib;
int i, str_i, ret, index;
int import_all;
AppointmentList *alist;
CalendarEventList *celist;
CalendarEventList *temp_celist;
struct CategoryAppInfo cai;
char old_cat_name[32];
int suggested_cat_num;
int new_cat_num;
int priv;
int year, month, day, hour, minute;
in=fopen(file_path, "r");
if (!in) {
jp_logf(JP_LOG_WARN, _("Unable to open file: %s\n"), file_path);
return EXIT_FAILURE;
}
/* CSV */
if (type==IMPORT_TYPE_CSV) {
jp_logf(JP_LOG_DEBUG, "Datebook import CSV [%s]\n", file_path);
/* Get the first line containing the format and check for reasonableness */
if (fgets(text, sizeof(text), in) == NULL) {
jp_logf(JP_LOG_WARN, "fgets failed %s %d\n", __FILE__, __LINE__);
}
if (datebook_version==0) {
ret = verify_csv_header(text, NUM_DBOOK_CSV_FIELDS, file_path);
} else {
ret = verify_csv_header(text, NUM_CALENDAR_CSV_FIELDS, file_path);
}
if (EXIT_FAILURE == ret) return EXIT_FAILURE;
import_all=FALSE;
while (1) {
memset(&new_cale, 0, sizeof(new_cale));
/* Read the category field */
read_csv_field(in, text, sizeof(text));
if (feof(in)) break;
#ifdef JPILOT_DEBUG
printf("category is [%s]\n", text);
#endif
g_strlcpy(old_cat_name, text, 16);
/* Figure out what the best category number is */
suggested_cat_num=0;
for (i=0; i<NUM_DATEBOOK_CAT_ITEMS; i++) {
if (!dbook_app_info.category.name[i][0]) continue;
if (!strcmp(dbook_app_info.category.name[i], old_cat_name)) {
suggested_cat_num=i;
break;
}
}
/* Read the private field */
read_csv_field(in, text, sizeof(text));
#ifdef JPILOT_DEBUG
printf("private is [%s]\n", text);
#endif
sscanf(text, "%d", &priv);
/* Description */
read_csv_field(in, description, sizeof(description));
if (strlen(description) > 0) {
new_cale.description=description;
} else {
new_cale.description=NULL;
}
/* Note */
read_csv_field(in, note, sizeof(note));
if (strlen(note) > 0) {
new_cale.note=note;
} else {
new_cale.note=NULL;
}
if (datebook_version) {
/* Location */
read_csv_field(in, location, sizeof(location));
if (strlen(location) > 0) {
new_cale.location=location;
} else {
new_cale.location=NULL;
}
}
/* Event */
read_csv_field(in, text, sizeof(text));
sscanf(text, "%d", &(new_cale.event));
/* Begin Time */
memset(&(new_cale.begin), 0, sizeof(new_cale.begin));
read_csv_field(in, text, sizeof(text));
sscanf(text, "%d %d %d %d:%d", &year, &month, &day, &hour, &minute);
new_cale.begin.tm_year=year-1900;
new_cale.begin.tm_mon=month-1;
new_cale.begin.tm_mday=day;
new_cale.begin.tm_hour=hour;
new_cale.begin.tm_min=minute;
new_cale.begin.tm_isdst=-1;
mktime(&(new_cale.begin));
/* End Time */
memset(&(new_cale.end), 0, sizeof(new_cale.end));
read_csv_field(in, text, sizeof(text));
sscanf(text, "%d %d %d %d:%d", &year, &month, &day, &hour, &minute);
new_cale.end.tm_year=year-1900;
new_cale.end.tm_mon=month-1;
new_cale.end.tm_mday=day;
new_cale.end.tm_hour=hour;
new_cale.end.tm_min=minute;
new_cale.end.tm_isdst=-1;
mktime(&(new_cale.end));
/* Alarm */
read_csv_field(in, text, sizeof(text));
sscanf(text, "%d", &(new_cale.alarm));
/* Alarm Advance */
read_csv_field(in, text, sizeof(text));
sscanf(text, "%d", &(new_cale.advance));
/* Advance Units */
read_csv_field(in, text, sizeof(text));
sscanf(text, "%d", &(new_cale.advanceUnits));
/* Repeat Type */
read_csv_field(in, text, sizeof(text));
sscanf(text, "%d", &(i));
new_cale.repeatType=i;
/* Repeat Forever */
read_csv_field(in, text, sizeof(text));
sscanf(text, "%d", &(new_cale.repeatForever));
/* Repeat End */
memset(&(new_cale.repeatEnd), 0, sizeof(new_cale.repeatEnd));
read_csv_field(in, text, sizeof(text));
sscanf(text, "%d %d %d", &year, &month, &day);
new_cale.repeatEnd.tm_year=year-1900;
new_cale.repeatEnd.tm_mon=month-1;
new_cale.repeatEnd.tm_mday=day;
new_cale.repeatEnd.tm_isdst=-1;
mktime(&(new_cale.repeatEnd));
/* Repeat Frequency */
read_csv_field(in, text, sizeof(text));
sscanf(text, "%d", &(new_cale.repeatFrequency));
/* Repeat Day */
read_csv_field(in, text, sizeof(text));
sscanf(text, "%d", &(i));
new_cale.repeatDay=i;
/* Repeat Days */
read_csv_field(in, text, sizeof(text));
for (i=0; i<7; i++) {
new_cale.repeatDays[i]=(text[i]=='1');
}
/* Week Start */
read_csv_field(in, text, sizeof(text));
sscanf(text, "%d", &(new_cale.repeatWeekstart));
/* Number of Exceptions */
read_csv_field(in, text, sizeof(text));
sscanf(text, "%d", &(new_cale.exceptions));
/* Exceptions */
ret = read_csv_field(in, text, sizeof(text));
new_cale.exception=calloc(new_cale.exceptions, sizeof(struct tm));
for (str_i=0, i=0; i<new_cale.exceptions; i++) {
sscanf(&(text[str_i]), "%d %d %d", &year, &month, &day);
new_cale.exception[i].tm_year=year-1900;
new_cale.exception[i].tm_mon=month-1;
new_cale.exception[i].tm_mday=day;
new_cale.exception[i].tm_isdst=-1;
mktime(&(new_cale.exception[i]));
for (; (str_i<sizeof(text)) && (text[str_i]); str_i++) {
if (text[str_i]==',') {
str_i++;
break;
}
}
}
datebook_to_text(&new_cale, text, 65535);
if (!import_all) {
ret=import_record_ask(parent_window, pane,
text,
&(dbook_app_info.category),
old_cat_name,
priv,
suggested_cat_num,
&new_cat_num);
} else {
new_cat_num=suggested_cat_num;
}
if (ret==DIALOG_SAID_IMPORT_QUIT) break;
if (ret==DIALOG_SAID_IMPORT_SKIP) continue;
if (ret==DIALOG_SAID_IMPORT_ALL) import_all=TRUE;
attrib = (new_cat_num & 0x0F) |
(priv ? dlpRecAttrSecret : 0);
if ((ret==DIALOG_SAID_IMPORT_YES) || (import_all)) {
if (strlen(new_cale.description)+1 > MAX_DESC_LEN) {
new_cale.description[MAX_DESC_LEN+1]='\0';
jp_logf(JP_LOG_WARN, _("Appointment description text > %d, truncating to %d\n"), MAX_DESC_LEN, MAX_DESC_LEN);
}
pc_calendar_write(&new_cale, NEW_PC_REC, attrib, NULL);
}
}
}
/* Palm Desktop DAT format */
if (type==IMPORT_TYPE_DAT) {
jp_logf(JP_LOG_DEBUG, "Datebook import DAT [%s]\n", file_path);
if (dat_check_if_dat_file(in)!=DAT_DATEBOOK_FILE) {
dialog_generic_ok(notebook, _("Error"), DIALOG_ERROR,
_("File doesn't appear to be datebook.dat format\n"));
fclose(in);
return EXIT_FAILURE;
}
alist=NULL;
dat_get_appointments(in, &alist, &cai);
/* Copy this to a calendar event list */
copy_appointments_to_calendarEvents(alist, &celist);
free_AppointmentList(&alist);
import_all=FALSE;
for (temp_celist=celist; temp_celist; temp_celist=temp_celist->next) {
index=temp_celist->mcale.unique_id-1;
if (index<0) {
g_strlcpy(old_cat_name, _("Unfiled"), 16);
} else {
g_strlcpy(old_cat_name, cai.name[index], 16);
}
/* Figure out what category it was in the dat file */
index=temp_celist->mcale.unique_id-1;
suggested_cat_num=0;
if (index>-1) {
for (i=0; i<NUM_DATEBOOK_CAT_ITEMS; i++) {
if (!dbook_app_info.category.name[i][0]) continue;
if (!strcmp(dbook_app_info.category.name[i], old_cat_name)) {
suggested_cat_num=i;
break;
}
}
}
ret=0;
if (!import_all) {
datebook_to_text(&(temp_celist->mcale.cale), text, 65535);
ret=import_record_ask(parent_window, pane,
text,
&(dbook_app_info.category),
old_cat_name,
(temp_celist->mcale.attrib & 0x10),
suggested_cat_num,
&new_cat_num);
} else {
new_cat_num=suggested_cat_num;
}
if (ret==DIALOG_SAID_IMPORT_QUIT) break;
if (ret==DIALOG_SAID_IMPORT_SKIP) continue;
if (ret==DIALOG_SAID_IMPORT_ALL) import_all=TRUE;
attrib = (new_cat_num & 0x0F) |
((temp_celist->mcale.attrib & 0x10) ? dlpRecAttrSecret : 0);
if ((ret==DIALOG_SAID_IMPORT_YES) || (import_all)) {
pc_calendar_write(&(temp_celist->mcale.cale), NEW_PC_REC, attrib, NULL);
}
}
free_CalendarEventList(&celist);
}
datebook_refresh(FALSE, TRUE);
fclose(in);
return EXIT_SUCCESS;
}
int datebook_import(GtkWidget *window)
{
char *type_desc[] = {
N_("CSV (Comma Separated Values)"),
N_("DAT/DBA (Palm Archive Formats)"),
NULL
};
int type_int[] = {
IMPORT_TYPE_CSV,
IMPORT_TYPE_DAT,
0
};
/* Hide ABA import of CalendarDB until file format has been decoded */
if (datebook_version==1) {
type_desc[1] = NULL;
type_int[1] = 0;
}
import_gui(window, pane, type_desc, type_int, cb_dbook_import);
return EXIT_SUCCESS;
}
/*** End Import Code ***/
/*************** Start Export Code ***************/
/* TODO rename */
static void appt_export_ok(int type, const char *filename)
{
MyCalendarEvent *mcale;
CalendarEventList *cel, *temp_list;
FILE *out;
struct stat statb;
int i, r;
char *button_text[]={N_("OK")};
char *button_overwrite_text[]={N_("No"), N_("Yes")};
char text[1024];
char csv_text[65550];
char *p;
gchar *end;
time_t ltime;
struct tm *now = NULL;
struct tm ical_time;
long char_set;
char username[256];
char hostname[256];
const char *svalue;
long userid;
const char *short_date;
char pref_time[40];
char str1[256], str2[256];
char date_string[1024];
char *utf;
/* Open file for export, including corner cases where file exists or
* can't be opened */
if (!stat(filename, &statb)) {
if (S_ISDIR(statb.st_mode)) {
g_snprintf(text, sizeof(text), _("%s is a directory"), filename);
dialog_generic(GTK_WINDOW(export_window),
_("Error Opening File"),
DIALOG_ERROR, text, 1, button_text);
return;
}
g_snprintf(text, sizeof(text), _("Do you want to overwrite file %s?"), filename);
r = dialog_generic(GTK_WINDOW(export_window),
_("Overwrite File?"),
DIALOG_QUESTION, text, 2, button_overwrite_text);
if (r!=DIALOG_SAID_2) {
return;
}
}
out = fopen(filename, "w");
if (!out) {
g_snprintf(text, sizeof(text), _("Error opening file: %s"), filename);
dialog_generic(GTK_WINDOW(export_window),
_("Error Opening File"),
DIALOG_ERROR, text, 1, button_text);
return;
}
/* Write a header for TEXT file */
if (type == EXPORT_TYPE_TEXT) {
get_pref(PREF_SHORTDATE, NULL, &short_date);
get_pref_time_no_secs(pref_time);
time(<ime);
now = localtime(<ime);
strftime(str1, sizeof(str1), short_date, now);
strftime(str2, sizeof(str2), pref_time, now);
g_snprintf(date_string, sizeof(date_string), "%s %s", str1, str2);
if (datebook_version==0) {
fprintf(out, _("Datebook exported from %s %s on %s\n\n"),
PN,VERSION,date_string);
} else {
fprintf(out, _("Calendar exported from %s %s on %s\n\n"),
PN,VERSION,date_string);
}
}
/* Write a header to the CSV file */
if (type == EXPORT_TYPE_CSV) {
if (datebook_version==0) {
fprintf(out, "CSV datebook version "VERSION": Category, Private, "
"Description, Note, Event, Begin, End, Alarm, Advance, "
"Advance Units, Repeat Type, Repeat Forever, Repeat End, "
"Repeat Frequency, Repeat Day, Repeat Days, "
"Week Start, Number of Exceptions, Exceptions\n");
} else {
fprintf(out, "CSV calendar version "VERSION": Category, Private, "
"Description, Note, Location, "
"Event, Begin, End, Alarm, Advance, "
"Advance Units, Repeat Type, Repeat Forever, Repeat End, "
"Repeat Frequency, Repeat Day, Repeat Days, "
"Week Start, Number of Exceptions, Exceptions\n");
}
}
/* Special setup for ICAL export */
if (type == EXPORT_TYPE_ICALENDAR) {
get_pref(PREF_CHAR_SET, &char_set, NULL);
if (char_set < CHAR_SET_UTF) {
jp_logf(JP_LOG_WARN, _("Host character encoding is not UTF-8 based.\n"
" Exported ical file may not be standards-compliant\n"));
}
/* Convert User Name stored in Palm character set */
get_pref(PREF_USER, NULL, &svalue);
g_strlcpy(text, svalue, 128);
text[127] = '\0';
charset_p2j(text, 128, char_set);
str_to_ical_str(username, sizeof(username), text);
get_pref(PREF_USER_ID, &userid, NULL);
gethostname(text, sizeof(hostname));
text[sizeof(hostname)-1]='\0';
str_to_ical_str(hostname, sizeof(hostname), text);
time(<ime);
now = gmtime(<ime);
}
get_pref(PREF_CHAR_SET, &char_set, NULL);
cel=NULL;
get_days_calendar_events2(&cel, NULL, 2, 2, 2, CATEGORY_ALL, NULL);
mcale=NULL;
for (i=0, temp_list=cel; temp_list; temp_list = temp_list->next, i++) {
mcale = &(temp_list->mcale);
switch (type) {
case EXPORT_TYPE_TEXT:
csv_text[0]='\0';
datebook_to_text(&(mcale->cale), csv_text, sizeof(csv_text));
fprintf(out, "%s\n", csv_text);
break;
case EXPORT_TYPE_CSV:
if (datebook_version==0) {
fprintf(out, "\"\","); /* No category for Datebook */
} else {
utf = charset_p2newj(dbook_app_info.category.name[mcale->attrib & 0x0F], 16, char_set);
str_to_csv_str(csv_text, utf);
fprintf(out, "\"%s\",", csv_text);
g_free(utf);
}
fprintf(out, "\"%s\",", (mcale->attrib & dlpRecAttrSecret) ? "1":"0");
str_to_csv_str(csv_text, mcale->cale.description);
fprintf(out, "\"%s\",", csv_text);
str_to_csv_str(csv_text, mcale->cale.note);
fprintf(out, "\"%s\",", csv_text);
if (datebook_version) {
str_to_csv_str(csv_text, mcale->cale.location);
fprintf(out, "\"%s\",", csv_text);
}
fprintf(out, "\"%d\",", mcale->cale.event);
fprintf(out, "\"%d %02d %02d %02d:%02d\",",
mcale->cale.begin.tm_year+1900,
mcale->cale.begin.tm_mon+1,
mcale->cale.begin.tm_mday,
mcale->cale.begin.tm_hour,
mcale->cale.begin.tm_min);
fprintf(out, "\"%d %02d %02d %02d:%02d\",",
mcale->cale.end.tm_year+1900,
mcale->cale.end.tm_mon+1,
mcale->cale.end.tm_mday,
mcale->cale.end.tm_hour,
mcale->cale.end.tm_min);
fprintf(out, "\"%s\",", (mcale->cale.alarm) ? "1":"0");
fprintf(out, "\"%d\",", mcale->cale.advance);
fprintf(out, "\"%d\",", mcale->cale.advanceUnits);
fprintf(out, "\"%d\",", mcale->cale.repeatType);
if (mcale->cale.repeatType == calendarRepeatNone) {
/* Single events don't have valid repeat data fields so
* a standard output data template is used for them */
fprintf(out, "\"0\",\"1970 01 01\",\"0\",\"0\",\"0\",\"0\",\"0\",\"");
} else {
fprintf(out, "\"%d\",", mcale->cale.repeatForever);
if (mcale->cale.repeatForever) {
/* repeatForever events don't have valid end date fields
* so a standard output date is used for them */
fprintf(out, "\"1970 01 01\",");
} else {
fprintf(out, "\"%d %02d %02d\",",
mcale->cale.repeatEnd.tm_year+1900,
mcale->cale.repeatEnd.tm_mon+1,
mcale->cale.repeatEnd.tm_mday);
}
fprintf(out, "\"%d\",", mcale->cale.repeatFrequency);
fprintf(out, "\"%d\",", mcale->cale.repeatDay);
fprintf(out, "\"");
for (i=0; i<7; i++) {
fprintf(out, "%d", mcale->cale.repeatDays[i]);
}
fprintf(out, "\",");
fprintf(out, "\"%d\",", mcale->cale.repeatWeekstart);
fprintf(out, "\"%d\",", mcale->cale.exceptions);
fprintf(out, "\"");
if (mcale->cale.exceptions > 0) {
for (i=0; i<mcale->cale.exceptions; i++) {
if (i>0) {
fprintf(out, ",");
}
fprintf(out, "%d %02d %02d",
mcale->cale.exception[i].tm_year+1900,
mcale->cale.exception[i].tm_mon+1,
mcale->cale.exception[i].tm_mday);
}
} /* if for exceptions */
} /* else for repeat event */
fprintf(out, "\"\n");
break;
case EXPORT_TYPE_ICALENDAR:
/* RFC 2445: Internet Calendaring and Scheduling Core
* Object Specification */
if (i == 0) {
fprintf(out, "BEGIN:VCALENDAR"CRLF);
fprintf(out, "VERSION:2.0"CRLF);
fprintf(out, "PRODID:%s"CRLF, FPI_STRING);
}
fprintf(out, "BEGIN:VEVENT"CRLF);
/* XXX maybe if it's secret export a VFREEBUSY busy instead? */
if (mcale->attrib & dlpRecAttrSecret) {
fprintf(out, "CLASS:PRIVATE"CRLF);
}
fprintf(out, "UID:palm-datebook-%08x-%08lx-%s@%s"CRLF,
mcale->unique_id, userid, username, hostname);
fprintf(out, "DTSTAMP:%04d%02d%02dT%02d%02d%02dZ"CRLF,
now->tm_year+1900,
now->tm_mon+1,
now->tm_mday,
now->tm_hour,
now->tm_min,
now->tm_sec);
if (datebook_version) {
/* Calendar supports categories and locations */
utf = charset_p2newj(dbook_app_info.category.name[mcale->attrib & 0x0F], 16, char_set);
str_to_ical_str(text, sizeof(text), utf);
fprintf(out, "CATEGORIES:%s"CRLF, text);
g_free(utf);
if (mcale->cale.location) {
str_to_ical_str(text, sizeof(text), mcale->cale.location);
fprintf(out, "LOCATION:%s"CRLF, text);
}
}
/* Create truncated description for use in SUMMARY field */
if (mcale->cale.description) {
g_strlcpy(text, mcale->cale.description, 51);
/* truncate the string on a UTF-8 character boundary */
if (char_set > CHAR_SET_UTF) {
if (!g_utf8_validate(text, -1, (const gchar **)&end))
*end = 0;
}
} else {
/* Handle pathological case with null description. */
text[0] = '\0';
}
if ((p = strchr(text, '\n'))) {
*p = '\0';
}
str_to_ical_str(csv_text, sizeof(csv_text), text);
fprintf(out, "SUMMARY:%s%s"CRLF, csv_text,
strlen(text) > 49 ? "..." : "");
str_to_ical_str(csv_text, sizeof(csv_text), mcale->cale.description);
fprintf(out, "DESCRIPTION:%s", csv_text);
if (mcale->cale.note && mcale->cale.note[0]) {
str_to_ical_str(csv_text, sizeof(csv_text), mcale->cale.note);
fprintf(out, "\\n"CRLF" %s"CRLF, csv_text);
} else {
fprintf(out, CRLF);
}