forked from juddmon/jpilot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo_gui.c
2529 lines (2171 loc) · 81.3 KB
/
todo_gui.c
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
/*******************************************************************************
* todo_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/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
#include <pi-dlp.h>
#include "todo.h"
#include "i18n.h"
#include "utils.h"
#include "log.h"
#include "prefs.h"
#include "password.h"
#include "print.h"
#include "export.h"
#include "stock_buttons.h"
/********************************* Constants **********************************/
#define TODO_MAX_COLUMN_LEN 80
#define MAX_RADIO_BUTTON_LEN 100
#define NUM_TODO_PRIORITIES 5
#define NUM_TODO_CAT_ITEMS 16
#define NUM_TODO_CSV_FIELDS 8
#define CONNECT_SIGNALS 400
#define DISCONNECT_SIGNALS 401
/* RFCs use CRLF for Internet newline */
#define CRLF "\x0D\x0A"
/******************************* Global vars **********************************/
/* Keeps track of whether code is using ToDo, or Tasks database
* 0 is ToDo, 1 is Tasks */
static long todo_version=0;
extern GtkWidget *glob_date_label;
extern int glob_date_timer_tag;
static GtkWidget *clist;
static GtkWidget *todo_desc, *todo_note;
static GObject *todo_desc_buffer, *todo_note_buffer;
static GtkWidget *todo_completed_checkbox;
static GtkWidget *private_checkbox;
static struct tm due_date;
static GtkWidget *due_date_button;
static GtkWidget *todo_no_due_date_checkbox;
static GtkWidget *radio_button_todo[NUM_TODO_PRIORITIES];
/* Need two extra slots for the ALL category and Edit Categories... */
static GtkWidget *todo_cat_menu_item1[NUM_TODO_CAT_ITEMS+2];
static GtkWidget *todo_cat_menu_item2[NUM_TODO_CAT_ITEMS];
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 GtkWidget *category_menu1;
static GtkWidget *category_menu2;
static GtkWidget *pane;
static GtkWidget *note_pane;
static ToDoList *glob_todo_list=NULL;
static ToDoList *export_todo_list=NULL;
static struct sorted_cats sort_l[NUM_TODO_CAT_ITEMS];
static struct ToDoAppInfo todo_app_info;
static int todo_category=CATEGORY_ALL;
static int clist_col_selected;
static int clist_row_selected;
static int record_changed;
/****************************** Prototypes ************************************/
static int todo_clear_details(void);
static int todo_clist_redraw(void);
static int todo_find(void);
static void cb_add_new_record(GtkWidget *widget, gpointer data);
static void connect_changed_signals(int con_or_dis);
/****************************** Main Code *************************************/
/* Called once on initialization of GUI */
static void init(void)
{
time_t ltime;
struct tm *now;
long ivalue;
time(<ime);
now = localtime(<ime);
memcpy(&due_date, now, sizeof(struct tm));
get_pref(PREF_TODO_DAYS_TILL_DUE, &ivalue, NULL);
add_days_to_date(&due_date, ivalue);
clist_row_selected = 0;
clist_col_selected = 1;
record_changed = CLEAR_FLAG;
}
static void update_due_button(GtkWidget *button, struct tm *t)
{
const char *short_date;
char str[255];
if (t) {
get_pref(PREF_SHORTDATE, NULL, &short_date);
strftime(str, sizeof(str), short_date, t);
gtk_label_set_text(GTK_LABEL(GTK_BIN(button)->child), str);
} else {
gtk_label_set_text(GTK_LABEL(GTK_BIN(button)->child), _("No Date"));
}
}
static void cb_cal_dialog(GtkWidget *widget,
gpointer data)
{
long fdow;
int r = 0;
struct tm t;
GtkWidget *Pcheck_button;
GtkWidget *Pbutton;
Pcheck_button = todo_no_due_date_checkbox;
memcpy(&t, &due_date, sizeof(t));
Pbutton = due_date_button;
get_pref(PREF_FDOW, &fdow, NULL);
r = cal_dialog(GTK_WINDOW(gtk_widget_get_toplevel(widget)), _("Due Date"), fdow,
&(t.tm_mon),
&(t.tm_mday),
&(t.tm_year));
if (r==CAL_DONE) {
mktime(&t);
memcpy(&due_date, &t, sizeof(due_date));
if (Pcheck_button) {
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(Pcheck_button), FALSE);
/* The above call sets due_date forward by n days, so we correct it */
memcpy(&due_date, &t, sizeof(due_date));
update_due_button(Pbutton, &t);
}
if (Pbutton) {
update_due_button(Pbutton, &t);
}
}
}
int todo_print(void)
{
long this_many;
MyToDo *mtodo;
ToDoList *todo_list;
ToDoList todo_list1;
get_pref(PREF_PRINT_THIS_MANY, &this_many, NULL);
todo_list=NULL;
if (this_many==1) {
mtodo = gtk_clist_get_row_data(GTK_CLIST(clist), clist_row_selected);
if (mtodo < (MyToDo *)CLIST_MIN_DATA) {
return EXIT_FAILURE;
}
memcpy(&(todo_list1.mtodo), mtodo, sizeof(MyToDo));
todo_list1.next=NULL;
todo_list = &todo_list1;
}
if (this_many==2) {
get_todos2(&todo_list, SORT_ASCENDING, 2, 2, 2, 2, todo_category);
}
if (this_many==3) {
get_todos2(&todo_list, SORT_ASCENDING, 2, 2, 2, 2, CATEGORY_ALL);
}
print_todos(todo_list, PN);
if ((this_many==2) || (this_many==3)) {
free_ToDoList(&todo_list);
}
return EXIT_SUCCESS;
}
static void set_new_button_to(int new_state)
{
jp_logf(JP_LOG_DEBUG, "set_new_button_to new %d old %d\n", new_state, record_changed);
if (record_changed==new_state) {
return;
}
switch (new_state) {
case MODIFY_FLAG:
gtk_widget_show(cancel_record_button);
gtk_widget_show(copy_record_button);
gtk_widget_show(apply_record_button);
gtk_widget_hide(add_record_button);
gtk_widget_hide(delete_record_button);
gtk_widget_hide(new_record_button);
gtk_widget_hide(undelete_record_button);
break;
case NEW_FLAG:
gtk_widget_show(cancel_record_button);
gtk_widget_show(add_record_button);
gtk_widget_hide(apply_record_button);
gtk_widget_hide(copy_record_button);
gtk_widget_hide(delete_record_button);
gtk_widget_hide(new_record_button);
gtk_widget_hide(undelete_record_button);
break;
case CLEAR_FLAG:
gtk_widget_show(delete_record_button);
gtk_widget_show(copy_record_button);
gtk_widget_show(new_record_button);
gtk_widget_hide(add_record_button);
gtk_widget_hide(apply_record_button);
gtk_widget_hide(cancel_record_button);
gtk_widget_hide(undelete_record_button);
break;
case UNDELETE_FLAG:
gtk_widget_show(undelete_record_button);
gtk_widget_show(copy_record_button);
gtk_widget_show(new_record_button);
gtk_widget_hide(add_record_button);
gtk_widget_hide(apply_record_button);
gtk_widget_hide(cancel_record_button);
gtk_widget_hide(delete_record_button);
break;
default:
return;
}
record_changed=new_state;
}
static void cb_record_changed(GtkWidget *widget,
gpointer data)
{
jp_logf(JP_LOG_DEBUG, "cb_record_changed\n");
if (record_changed==CLEAR_FLAG) {
connect_changed_signals(DISCONNECT_SIGNALS);
if (GTK_CLIST(clist)->rows > 0) {
set_new_button_to(MODIFY_FLAG);
} else {
set_new_button_to(NEW_FLAG);
}
}
else if (record_changed==UNDELETE_FLAG)
{
jp_logf(JP_LOG_INFO|JP_LOG_GUI,
_("This record is deleted.\n"
"Undelete it or copy it to make changes.\n"));
}
}
static void connect_changed_signals(int con_or_dis)
{
int i;
static int connected=0;
/* CONNECT */
if ((con_or_dis==CONNECT_SIGNALS) && (!connected)) {
connected=1;
for (i=0; i<NUM_TODO_CAT_ITEMS; i++) {
if (todo_cat_menu_item2[i]) {
gtk_signal_connect(GTK_OBJECT(todo_cat_menu_item2[i]), "toggled",
GTK_SIGNAL_FUNC(cb_record_changed), NULL);
}
}
for (i=0; i<NUM_TODO_PRIORITIES; i++) {
if (radio_button_todo[i]) {
gtk_signal_connect(GTK_OBJECT(radio_button_todo[i]), "toggled",
GTK_SIGNAL_FUNC(cb_record_changed), NULL);
}
}
g_signal_connect(todo_desc_buffer, "changed",
GTK_SIGNAL_FUNC(cb_record_changed), NULL);
g_signal_connect(todo_note_buffer, "changed",
GTK_SIGNAL_FUNC(cb_record_changed), NULL);
gtk_signal_connect(GTK_OBJECT(todo_completed_checkbox), "toggled",
GTK_SIGNAL_FUNC(cb_record_changed), NULL);
gtk_signal_connect(GTK_OBJECT(private_checkbox), "toggled",
GTK_SIGNAL_FUNC(cb_record_changed), NULL);
gtk_signal_connect(GTK_OBJECT(todo_no_due_date_checkbox), "toggled",
GTK_SIGNAL_FUNC(cb_record_changed), NULL);
gtk_signal_connect(GTK_OBJECT(due_date_button), "pressed",
GTK_SIGNAL_FUNC(cb_record_changed), NULL);
}
/* DISCONNECT */
if ((con_or_dis==DISCONNECT_SIGNALS) && (connected)) {
connected=0;
for (i=0; i<NUM_TODO_CAT_ITEMS; i++) {
if (todo_cat_menu_item2[i]) {
gtk_signal_disconnect_by_func(GTK_OBJECT(todo_cat_menu_item2[i]),
GTK_SIGNAL_FUNC(cb_record_changed), NULL);
}
}
for (i=0; i<NUM_TODO_PRIORITIES; i++) {
gtk_signal_disconnect_by_func(GTK_OBJECT(radio_button_todo[i]),
GTK_SIGNAL_FUNC(cb_record_changed), NULL);
}
g_signal_handlers_disconnect_by_func(todo_desc_buffer,
GTK_SIGNAL_FUNC(cb_record_changed), NULL);
g_signal_handlers_disconnect_by_func(todo_note_buffer,
GTK_SIGNAL_FUNC(cb_record_changed), NULL);
gtk_signal_disconnect_by_func(GTK_OBJECT(todo_completed_checkbox),
GTK_SIGNAL_FUNC(cb_record_changed), NULL);
gtk_signal_disconnect_by_func(GTK_OBJECT(private_checkbox),
GTK_SIGNAL_FUNC(cb_record_changed), NULL);
gtk_signal_disconnect_by_func(GTK_OBJECT(todo_no_due_date_checkbox),
GTK_SIGNAL_FUNC(cb_record_changed), NULL);
gtk_signal_disconnect_by_func(GTK_OBJECT(due_date_button),
GTK_SIGNAL_FUNC(cb_record_changed), NULL);
}
}
static int todo_to_text(struct ToDo *todo, char *text, int len)
{
char yes[]="Yes";
char no[]="No";
char empty[]="";
char *complete;
char *description;
char *note;
char due[20];
const char *short_date;
if (todo->indefinite) {
strcpy(due, "Never");
} else {
get_pref(PREF_SHORTDATE, NULL, &short_date);
strftime(due, sizeof(due), short_date, &(todo->due));
}
complete=todo->complete ? yes : no;
description=todo->description ? todo->description : empty;
note=todo->note ? todo->note : empty;
g_snprintf(text, len, "Due: %s\nPriority: %d\nComplete: %s\n\
Description: %s\nNote: %s\n", due, todo->priority, complete,
description, note);
return EXIT_SUCCESS;
}
/*
* Start Import Code
*/
static int cb_todo_import(GtkWidget *parent_window,
const char *file_path, int type)
{
FILE *in;
char text[65536];
char description[65536];
char note[65536];
struct ToDo new_todo;
unsigned char attrib;
int i, ret, index;
int import_all;
ToDoList *todolist;
ToDoList *temp_todolist;
struct CategoryAppInfo cai;
char old_cat_name[32];
int suggested_cat_num;
int new_cat_num;
int priv, indefinite, priority, completed;
int year, month, day;
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, "Todo 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__);
}
ret = verify_csv_header(text, NUM_TODO_CSV_FIELDS, file_path);
if (EXIT_FAILURE == ret) return EXIT_FAILURE;
import_all=FALSE;
while (1) {
/* Read the category field */
ret = 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);
attrib=0;
/* Figure out what the best category number is */
suggested_cat_num=0;
for (i=0; i<NUM_TODO_CAT_ITEMS; i++) {
if (todo_app_info.category.name[i][0]=='\0') continue;
if (!strcmp(todo_app_info.category.name[i], old_cat_name)) {
suggested_cat_num=i;
break;
}
}
/* Read the private field */
ret = read_csv_field(in, text, sizeof(text));
#ifdef JPILOT_DEBUG
printf("private is [%s]\n", text);
#endif
sscanf(text, "%d", &priv);
/* Read the indefinite field */
ret = read_csv_field(in, text, sizeof(text));
#ifdef JPILOT_DEBUG
printf("indefinite is [%s]\n", text);
#endif
sscanf(text, "%d", &indefinite);
/* Read the Due Date field */
ret = read_csv_field(in, text, sizeof(text));
#ifdef JPILOT_DEBUG
printf("due date is [%s]\n", text);
#endif
sscanf(text, "%d/%d/%d", &year, &month, &day);
/* Read the Priority field */
ret = read_csv_field(in, text, sizeof(text));
#ifdef JPILOT_DEBUG
printf("priority is [%s]\n", text);
#endif
sscanf(text, "%d", &priority);
/* Read the Completed field */
ret = read_csv_field(in, text, sizeof(text));
#ifdef JPILOT_DEBUG
printf("completed is [%s]\n", text);
#endif
sscanf(text, "%d", &completed);
/* Read the Description field */
ret = read_csv_field(in, description, sizeof(text));
#ifdef JPILOT_DEBUG
printf("todo description [%s]\n", description);
#endif
/* Read the Note field */
ret = read_csv_field(in, note, sizeof(text));
#ifdef JPILOT_DEBUG
printf("todo note [%s]\n", note);
#endif
new_todo.indefinite=indefinite;
memset(&(new_todo.due), 0, sizeof(new_todo.due));
new_todo.due.tm_year=year-1900;
new_todo.due.tm_mon=month-1;
new_todo.due.tm_mday=day;
new_todo.priority=priority;
new_todo.complete=completed;
new_todo.description=description;
new_todo.note=note;
todo_to_text(&new_todo, text, sizeof(text));
if (!import_all) {
ret=import_record_ask(parent_window, pane,
text,
&(todo_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)) {
pc_todo_write(&new_todo, NEW_PC_REC, attrib, NULL);
}
}
}
/* Palm Desktop DAT format */
if (type==IMPORT_TYPE_DAT) {
jp_logf(JP_LOG_DEBUG, "Todo import DAT [%s]\n", file_path);
if (dat_check_if_dat_file(in)!=DAT_TODO_FILE) {
jp_logf(JP_LOG_WARN, _("File doesn't appear to be todo.dat format\n"));
fclose(in);
return EXIT_FAILURE;
}
todolist=NULL;
dat_get_todos(in, &todolist, &cai);
import_all=FALSE;
for (temp_todolist=todolist; temp_todolist; temp_todolist=temp_todolist->next) {
index=temp_todolist->mtodo.unique_id-1;
if (index<0) {
g_strlcpy(old_cat_name, _("Unfiled"), 16);
index=0;
} else {
g_strlcpy(old_cat_name, cai.name[index], 16);
}
/* Figure out what category it was in the dat file */
index=temp_todolist->mtodo.unique_id-1;
suggested_cat_num=0;
if (index>-1) {
for (i=0; i<NUM_TODO_CAT_ITEMS; i++) {
if (todo_app_info.category.name[i][0]=='\0') continue;
if (!strcmp(todo_app_info.category.name[i], old_cat_name)) {
suggested_cat_num=i;
break;
}
}
}
ret=0;
todo_to_text(&(temp_todolist->mtodo.todo), text, sizeof(text));
if (!import_all) {
ret=import_record_ask(parent_window, pane,
text,
&(todo_app_info.category),
old_cat_name,
(temp_todolist->mtodo.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_todolist->mtodo.attrib & 0x10) ? dlpRecAttrSecret : 0);
if ((ret==DIALOG_SAID_IMPORT_YES) || (import_all)) {
pc_todo_write(&(temp_todolist->mtodo.todo), NEW_PC_REC,
attrib, NULL);
}
}
free_ToDoList(&todolist);
}
todo_refresh();
fclose(in);
return EXIT_SUCCESS;
}
int todo_import(GtkWidget *window)
{
char *type_desc[] = {
N_("CSV (Comma Separated Values)"),
N_("DAT/TDA (Palm Archive Formats)"),
NULL
};
int type_int[] = {
IMPORT_TYPE_CSV,
IMPORT_TYPE_DAT,
0
};
/* Hide ABA import of TaskDB until file format has been decoded */
/* FIXME: Uncomment when support for Tasks has been added
if (todo_version==1) {
type_desc[1] = NULL;
type_int[1] = 0;
}
*/
import_gui(window, pane, type_desc, type_int, cb_todo_import);
return EXIT_SUCCESS;
}
/*
* End Import Code
*/
/*
* Start Export code
*/
static void cb_todo_export_ok(GtkWidget *export_window, GtkWidget *clist,
int type, const char *filename)
{
MyToDo *mtodo;
GList *list, *temp_list;
FILE *out;
struct stat statb;
int i, r;
const char *short_date;
time_t ltime;
struct tm *now = NULL;
char *button_text[]={N_("OK")};
char *button_overwrite_text[]={N_("No"), N_("Yes")};
char text[1024];
char date_string[1024];
char str1[256], str2[256];
char pref_time[40];
char csv_text[65550];
char *p;
gchar *end;
char username[256];
char hostname[256];
const char *svalue;
long userid;
long char_set;
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);
fprintf(out, _("ToDo exported from %s %s on %s\n\n"),
PN,VERSION,date_string);
}
/* Write a header to the CSV file */
if (type == EXPORT_TYPE_CSV) {
fprintf(out, "CSV todo version "VERSION": Category, Private, Indefinite, Due Date, Priority, Completed, ToDo Text, Note\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"));
}
get_pref(PREF_USER, NULL, &svalue);
/* Convert User Name stored in Palm character set */
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);
list=GTK_CLIST(clist)->selection;
for (i=0, temp_list=list; temp_list; temp_list = temp_list->next, i++) {
mtodo = gtk_clist_get_row_data(GTK_CLIST(clist), GPOINTER_TO_INT(temp_list->data));
if (!mtodo) {
continue;
jp_logf(JP_LOG_WARN, _("Can't export todo %d\n"), (long) temp_list->data + 1);
}
switch (type) {
case EXPORT_TYPE_CSV:
utf = charset_p2newj(todo_app_info.category.name[mtodo->attrib & 0x0F], 16, char_set);
str_to_csv_str(csv_text, utf);
fprintf(out, "\"%s\",", csv_text);
g_free(utf);
fprintf(out, "\"%s\",", (mtodo->attrib & dlpRecAttrSecret) ? "1":"0");
fprintf(out, "\"%s\",", mtodo->todo.indefinite ? "1":"0");
if (mtodo->todo.indefinite) {
fprintf(out, "\"\",");
} else {
strftime(text, sizeof(text), "%Y/%02m/%02d", &(mtodo->todo.due));
fprintf(out, "\"%s\",", text);
}
fprintf(out, "\"%d\",", mtodo->todo.priority);
fprintf(out, "\"%s\",", mtodo->todo.complete ? "1":"0");
if (mtodo->todo.description) {
str_to_csv_str(csv_text, mtodo->todo.description);
fprintf(out, "\"%s\",", csv_text);
} else {
fprintf(out, "\"\",");
}
if (mtodo->todo.note) {
str_to_csv_str(csv_text, mtodo->todo.note);
fprintf(out, "\"%s\"\n", csv_text);
} else {
fprintf(out, "\"\",");
}
break;
case EXPORT_TYPE_TEXT:
utf = charset_p2newj(todo_app_info.category.name[mtodo->attrib & 0x0F], 16, char_set);
fprintf(out, _("Category: %s\n"), utf);
g_free(utf);
fprintf(out, _("Private: %s\n"),
(mtodo->attrib & dlpRecAttrSecret) ? _("Yes"):_("No"));
if (mtodo->todo.indefinite) {
fprintf(out, _("Due Date: None\n"));
} else {
strftime(text, sizeof(text), short_date, &(mtodo->todo.due));
fprintf(out, _("Due Date: %s\n"), text);
}
fprintf(out, _("Priority: %d\n"), mtodo->todo.priority);
fprintf(out, _("Completed: %s\n"), mtodo->todo.complete ? _("Yes"):_("No"));
if (mtodo->todo.description) {
fprintf(out, _("Description: %s\n"), mtodo->todo.description);
}
if (mtodo->todo.note) {
fprintf(out, _("Note: %s\n\n"), mtodo->todo.note);
}
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:VTODO"CRLF);
if (mtodo->attrib & dlpRecAttrSecret) {
fprintf(out, "CLASS:PRIVATE"CRLF);
}
fprintf(out, "UID:palm-todo-%08x-%08lx-%s@%s"CRLF,
mtodo->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);
str_to_ical_str(text, sizeof(text),
todo_app_info.category.name[mtodo->attrib & 0x0F]);
fprintf(out, "CATEGORIES:%s"CRLF, text);
if (mtodo->todo.description) {
g_strlcpy(str1, mtodo->todo.description, 51);
/* truncate the string on a UTF-8 character boundary */
if (char_set > CHAR_SET_UTF) {
if (!g_utf8_validate(str1, -1, (const gchar **)&end))
*end = 0;
}
} else {
/* Handle pathological case with null description. */
str1[0] = '\0';
}
if ((p = strchr(str1, '\n'))) {
*p = '\0';
}
str_to_ical_str(text, sizeof(text), str1);
fprintf(out, "SUMMARY:%s%s"CRLF, text,
strlen(str1) > 49 ? "..." : "");
str_to_ical_str(text, sizeof(text), mtodo->todo.description);
fprintf(out, "DESCRIPTION:%s", text);
if (mtodo->todo.note && mtodo->todo.note[0]) {
str_to_ical_str(text, sizeof(text), mtodo->todo.note);
fprintf(out, "\\n"CRLF" %s"CRLF, text);
} else {
fprintf(out, ""CRLF);
}
fprintf(out, "STATUS:%s"CRLF, mtodo->todo.complete ? "COMPLETED" : "NEEDS-ACTION");
fprintf(out, "PRIORITY:%d"CRLF, mtodo->todo.priority);
if (!mtodo->todo.indefinite) {
fprintf(out, "DUE;VALUE=DATE:%04d%02d%02d"CRLF,
mtodo->todo.due.tm_year+1900,
mtodo->todo.due.tm_mon+1,
mtodo->todo.due.tm_mday);
}
fprintf(out, "END:VTODO"CRLF);
if (temp_list->next == NULL) {
fprintf(out, "END:VCALENDAR"CRLF);
}
break;
default:
jp_logf(JP_LOG_WARN, _("Unknown export type\n"));
}
}
if (out) {
fclose(out);
}
}
static void cb_todo_update_clist(GtkWidget *clist, int category)
{
todo_update_clist(clist, NULL, &export_todo_list, category, FALSE);
}
static void cb_todo_export_done(GtkWidget *widget, const char *filename)
{
free_ToDoList(&export_todo_list);
set_pref(PREF_TODO_EXPORT_FILENAME, 0, filename, TRUE);
}
int todo_export(GtkWidget *window)
{
int w, h, x, y;
char *type_text[]={N_("Text"),
N_("CSV"),
N_("iCalendar"),
NULL};
int type_int[]={EXPORT_TYPE_TEXT, EXPORT_TYPE_CSV, EXPORT_TYPE_ICALENDAR};
gdk_window_get_size(window->window, &w, &h);
gdk_window_get_root_origin(window->window, &x, &y);
w = gtk_paned_get_position(GTK_PANED(pane));
x+=40;
export_gui(window,
w, h, x, y, 5, sort_l,
PREF_TODO_EXPORT_FILENAME,
type_text,
type_int,
cb_todo_update_clist,
cb_todo_export_done,
cb_todo_export_ok
);
return EXIT_SUCCESS;
}
/*
* End Export Code
*/
/* Find position of category in sorted category array
* via its assigned category number */
static int find_sort_cat_pos(int cat)
{
int i;
for (i=0; i<NUM_TODO_CAT_ITEMS; i++) {
if (sort_l[i].cat_num==cat) {
return i;
}
}
return -1;
}
/* Find a category's position in the category menu.
* This is equal to the category number except for the Unfiled category.
* The Unfiled category is always in the last position which changes as
* the number of categories changes */
static int find_menu_cat_pos(int cat)
{
int i;
if (cat != NUM_TODO_CAT_ITEMS-1) {
return cat;
} else { /* Unfiled category */
/* Count how many category entries are filled */
for (i=0; i<NUM_TODO_CAT_ITEMS; i++) {
if (!sort_l[i].Pcat[0]) {
return i;
}
}
return 0;
}
}
static void cb_delete_todo(GtkWidget *widget,
gpointer data)
{
MyToDo *mtodo;
int flag;
int show_priv;
long char_set;
mtodo = gtk_clist_get_row_data(GTK_CLIST(clist), clist_row_selected);
if (mtodo < (MyToDo *)CLIST_MIN_DATA) {
return;
}
/* Convert to Palm character set */
get_pref(PREF_CHAR_SET, &char_set, NULL);
if (char_set != CHAR_SET_LATIN1) {
if (mtodo->todo.description)
charset_j2p(mtodo->todo.description, strlen(mtodo->todo.description)+1, char_set);
if (mtodo->todo.note)
charset_j2p(mtodo->todo.note, strlen(mtodo->todo.note)+1, char_set);
}
/* Do masking like Palm OS 3.5 */
show_priv = show_privates(GET_PRIVATES);
if ((show_priv != SHOW_PRIVATES) &&
(mtodo->attrib & dlpRecAttrSecret)) {
return;
}
/* End Masking */
flag = GPOINTER_TO_INT(data);
if ((flag==MODIFY_FLAG) || (flag==DELETE_FLAG)) {
jp_logf(JP_LOG_DEBUG, "calling delete_pc_record\n");
delete_pc_record(TODO, mtodo, flag);
if (flag==DELETE_FLAG) {
/* when we redraw we want to go to the line above the deleted one */
if (clist_row_selected>0) {
clist_row_selected--;
}
}
}
if (flag == DELETE_FLAG) {
todo_clist_redraw();
}
}
static void cb_undelete_todo(GtkWidget *widget,
gpointer data)
{
MyToDo *mtodo;
int flag;
int show_priv;
mtodo = gtk_clist_get_row_data(GTK_CLIST(clist), clist_row_selected);
if (mtodo < (MyToDo *)CLIST_MIN_DATA) {
return;
}
/* Do masking like Palm OS 3.5 */
show_priv = show_privates(GET_PRIVATES);
if ((show_priv != SHOW_PRIVATES) &&
(mtodo->attrib & dlpRecAttrSecret)) {
return;
}
/* End Masking */