-
Notifications
You must be signed in to change notification settings - Fork 4
/
viscal.c
2764 lines (2130 loc) · 58.5 KB
/
viscal.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
#include <cairo/cairo.h>
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
#include <libical/ical.h>
#include <assert.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <locale.h>
#include <stdbool.h>
#define ARRAY_SIZE(array) (sizeof((array))/sizeof((array)[0]))
#define sgn(x) (((x) > 0) - ((x) < 0))
#define clamp(val, low, high) (val < low ? low : (val > high ? high : val))
#define max(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a > _b ? _a : _b; })
#define min(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a < _b ? _a : _b; })
#define EDITBUF_MAX 32768
// TODO: heap-realloc events array
#define MAX_EVENTS 1024
#define SMALLEST_TIMEBLOCK 5
static icaltimezone *tz_utc;
static const double BGCOLOR = 0.35;
static const int DAY_SECONDS = 86400;
static const int TXTPAD = 11;
static const int EVPAD = 2;
static const int EVMARGIN = 1;
static const double ZOOM_MAX = 10;
static const double ZOOM_MIN = 1;
/* static const int DEF_LMARGIN = 20; */
enum event_flags {
EV_SELECTED = 1 << 0
, EV_HIGHLIGHTED = 1 << 1
, EV_DRAGGING = 1 << 2
, EV_IMMOVABLE = 1 << 3
};
enum cal_flags {
CAL_MDOWN = 1 << 0
, CAL_DRAGGING = 1 << 1
, CAL_SPLIT = 1 << 2
, CAL_CHANGING = 1 << 3
, CAL_INSERTING = 1 << 4
};
union rgba {
double rgba[4];
struct {
double r, g, b, a;
};
};
enum source {
SOURCE_CALDAV,
SOURCE_FILE
};
struct ical {
icalcomponent * calendar;
enum source source;
const char *source_location;
union rgba color;
bool visible;
};
struct event {
icalcomponent *vevent;
struct ical *ical;
int flags;
// set on draw
double width, height;
double x, y;
double dragx, dragy;
double dragx_off, dragy_off;
time_t drag_time;
};
// used for temporary storage when editing summaries, descriptions, etc
static char g_editbuf[EDITBUF_MAX] = {0};
static int g_editbuf_pos = 0;
// TODO: move or remove g_cal_tz
static icaltimezone *g_cal_tz;
static void print_timezone(icaltimezone *tz) {
printf("Timezone Name: %s\n", icaltimezone_get_tzid(tz));
printf("Timezone Location: %s\n", icaltimezone_get_location(tz));
printf("Timezone TZNAME properties (should include PST/PDT): %s\n", icaltimezone_get_tznames(tz));
}
struct cal {
GtkWidget *widget;
struct ical calendars[128];
int ncalendars;
struct event events[MAX_EVENTS];
int nevents;
char chord;
int repeat;
icalcomponent *select_after_sort;
// TODO: make multiple target selection
int target;
int selected_event_ind;
int selected_calendar_ind;
enum cal_flags flags;
int timeblock_size;
int timeblock_step;
int refresh_events;
int x, y, mx, my;
int gutter_height;
int font_size;
double zoom, zoom_at;
icaltimezone *tz;
time_t current; // current highlighted position
time_t today, start_at, scroll;
int height, width;
};
typedef void (chord_cmd)(struct cal *);
struct chord {
char *keys;
chord_cmd *cmd;
};
static void align_hour(struct cal *);
static void align_up(struct cal *);
static void align_down(struct cal *);
static void center_view(struct cal *);
static void top_view(struct cal *);
static void bottom_view(struct cal *);
static void zoom_in(struct cal *);
static void zoom_out(struct cal *);
static void select_down(struct cal *);
static void select_up(struct cal *);
static void delete_timeblock(struct cal *);
static struct chord chords[] = {
{ "ah", align_hour },
{ "ak", align_up },
{ "aj", align_down },
{ "zz", center_view },
{ "zt", top_view },
{ "zb", bottom_view },
{ "zi", zoom_in },
{ "zo", zoom_out },
{ "gj", select_down },
{ "gk", select_up },
{ "dd", delete_timeblock },
};
struct extra_data {
GtkWindow *win;
struct cal *cal;
};
static GdkCursor *cursor_default;
static GdkCursor *cursor_pointer;
static int g_lmargin = 18;
static int g_margin_time_w = 0;
static int margin_calculated = 0;
static union rgba g_text_color;
/* static union rgba g_timeline_color; */
static const double dashed[] = {1.0};
static void
calendar_create(struct cal *cal) {
time_t now;
time_t today, nowh;
struct tm nowtm;
now = time(NULL);
nowtm = *localtime(&now);
nowtm.tm_min = 0;
nowh = mktime(&nowtm);
nowtm.tm_hour = 0;
nowtm.tm_sec = 0;
today = mktime(&nowtm);
cal->selected_calendar_ind = 0;
cal->selected_event_ind = -1;
cal->select_after_sort = NULL;
cal->target = -1;
cal->chord = 0;
cal->gutter_height = 40;
cal->font_size = 16;
cal->timeblock_step = 15;
cal->timeblock_size = 30;
cal->flags = 0;
cal->ncalendars = 0;
cal->nevents = 0;
cal->start_at = nowh - today - 4*60*60;
cal->scroll = 0;
cal->current = nowh;
cal->repeat = 1;
cal->today = today;
cal->x = g_lmargin;
cal->y = cal->gutter_height;
cal->zoom = 5.0;
}
static void warn(const char *msg) {
printf("WARN %s\n", msg);
}
static void set_current_calendar(struct cal *cal, struct ical *ical)
{
for (int i = 0; i < cal->ncalendars; i++) {
if (&cal->calendars[i] == ical)
cal->selected_calendar_ind = i;
}
}
static struct ical *current_calendar(struct cal *cal) {
if (cal->ncalendars == 0)
return NULL;
return &cal->calendars[cal->selected_calendar_ind];
}
static time_t calendar_view_end(struct cal *cal)
{
return cal->today + cal->start_at + cal->scroll + DAY_SECONDS;
}
static time_t calendar_view_start(struct cal *cal)
{
return cal->today + cal->start_at + cal->scroll;
}
static int
span_overlaps(time_t start1, time_t end1, time_t start2, time_t end2) {
return max(0, min(end1, end2) - max(start1, start2));
}
static void vevent_span_timet(icalcomponent *vevent, time_t *st, time_t *et)
{
icaltimetype dtstart, dtend;
if (st) {
dtstart = icalcomponent_get_dtstart(vevent);
*st = icaltime_as_timet_with_zone(dtstart, icaltime_get_timezone(dtstart));
}
if (et) {
dtend = icalcomponent_get_dtend(vevent);
*et = icaltime_as_timet_with_zone(dtend, icaltime_get_timezone(dtend));
}
}
static void select_event(struct cal *cal, int ind)
{
time_t start;
struct event *ev;
cal->selected_event_ind = ind;
if (ind != -1) {
ev = &cal->events[ind];
vevent_span_timet(ev->vevent, &start, NULL);
cal->current = start;
}
}
/* static int */
/* vevent_in_span(icalcomponent *vevent, time_t start, time_t end) { */
/* /\* printf("vevent_in_span span.start %d span.end %d start %d end %d\n", *\/ */
/* /\* span.start, span.end, start, end); *\/ */
/* time_t st, et; */
/* vevent_span_timet(vevent, &st, &et); */
/* return span_overlaps(st, et, start, end); */
/* } */
static int sort_event(const void *a, const void*b) {
time_t st_a, st_b;
icaltimetype dtstart;
struct event *ea = (struct event *)a;
struct event *eb = (struct event *)b;
dtstart = icalcomponent_get_dtstart(ea->vevent);
st_a = icaltime_as_timet_with_zone(dtstart, dtstart.zone);
dtstart = icalcomponent_get_dtstart(eb->vevent);
st_b = icaltime_as_timet_with_zone(dtstart, dtstart.zone);
if (st_a < st_b)
return -1;
else if (st_a == st_b)
return 0;
else
return 1;
}
static time_t get_vevent_start(icalcomponent *vevent)
{
icaltimetype dtstart = icalcomponent_get_dtstart(vevent);
return icaltime_as_timet_with_zone(dtstart, dtstart.zone);
}
static int first_event_starting_at(struct cal *cal, time_t starting_at)
{
time_t st;
if (cal->nevents == 0)
return -1;
for (int i = cal->nevents - 1; i >= 0; i--) {
vevent_span_timet(cal->events[i].vevent, &st, NULL);
if (st >= starting_at)
continue;
else if (i == cal->nevents - 1)
return -1;
return i + 1;
}
assert(!"unpossible");
}
// seconds_range = 0 implies: do something reasonable (DAY_SECONDS/4)
static int find_event_within(struct cal *cal, time_t target, int seconds_range)
{
struct event *ev;
time_t evtime, diff, prev;
if (seconds_range == 0)
seconds_range = DAY_SECONDS/4;
prev = target;
if (cal->nevents == 0)
return -1;
else if (cal->nevents == 1)
return 0;
for (int i = cal->nevents-1; i >= 0; i--) {
ev = &cal->events[i];
vevent_span_timet(ev->vevent, &evtime, NULL);
diff = abs(target - evtime);
if (diff > prev) {
if (prev > seconds_range)
return -1;
return i+1;
}
prev = diff;
}
assert(!"shouldn't get here");
}
/* static void select_closest_to_now(struct cal *cal) */
/* { */
/* time_t now = time(NULL); */
/* cal->selected_event_ind = find_event_within(cal, now, 0); */
/* } */
static void set_edit_buffer(const char *src)
{
char *dst = g_editbuf;
int n = 0;
int c = EDITBUF_MAX;
while (c-- && (*dst++ = *src++))
n++;
g_editbuf_pos = n;
}
static struct ical *get_selected_calendar(struct cal *cal)
{
if (cal->ncalendars == 0 || cal->selected_calendar_ind == -1)
return NULL;
return &cal->calendars[cal->selected_calendar_ind];
}
static struct event *get_selected_event(struct cal *cal)
{
if (cal->nevents == 0 || cal->selected_event_ind == -1)
return NULL;
return &cal->events[cal->selected_event_ind];
}
enum edit_mode_flags {
EDIT_CLEAR = 1 << 1,
};
static void edit_mode(struct cal *cal, int flags)
{
// TODO: STATUS BAR for edit mode
struct event *event =
get_selected_event(cal);
// don't enter edit mode if we're not selecting any event
if (!event)
return;
cal->flags |= CAL_CHANGING;
if (flags & EDIT_CLEAR)
return set_edit_buffer("");
const char *summary =
icalcomponent_get_summary(event->vevent);
// TODO: what are we editing? for now assume summary
// copy current summary to edit buffer
set_edit_buffer(summary);
}
static void print_flags(struct cal *cal)
{
int i;
printf("flags: ");
for (i = 0; i < cal->nevents; i++) {
printf("%d ", cal->events[i].flags);
}
printf("\n");
}
static void events_for_view(struct cal *cal, time_t start, time_t end)
{
int i;
struct event *event;
icalcomponent *vevent;
struct ical *calendar;
icalcomponent *ical;
cal->nevents = 0;
for (i = 0; i < cal->ncalendars; ++i) {
calendar = &cal->calendars[i];
ical = calendar->calendar;
for (vevent = icalcomponent_get_first_component(ical, ICAL_VEVENT_COMPONENT);
vevent != NULL && cal->nevents < MAX_EVENTS;
vevent = icalcomponent_get_next_component(ical, ICAL_VEVENT_COMPONENT))
{
// NOTE: re-add me when we care about filtering
/* if (vevent_in_span(vevent, start, end)) { */
event = &cal->events[cal->nevents++];
/* printf("event in view %s\n", icalcomponent_get_summary(vevent)); */
event->vevent = vevent;
event->ical = calendar;
/* } */
}
}
print_flags(cal);
printf("DEBUG sorting\n");
qsort(cal->events, cal->nevents, sizeof(struct event), sort_event);
print_flags(cal);
// useful for selecting a new event after insertion
if (cal->select_after_sort) {
for (i = 0; i < cal->nevents; i++) {
if (cal->events[i].vevent == cal->select_after_sort) {
select_event(cal, i);
// HACK: we might not always want to do this...
edit_mode(cal, 0);
break;
}
}
}
cal->select_after_sort = NULL;
}
static void on_change_view(struct cal *cal) {
events_for_view(cal, calendar_view_start(cal), calendar_view_end(cal));
}
/* static void */
/* calendar_print_state(struct cal *cal) { */
/* static int c = 0; */
/* printf("%f %d %d %s %s %d\r", */
/* cal->zoom, cal->mx, cal->my, */
/* (cal->flags & CAL_DRAGGING) != 0 ? "D " : " ", */
/* (cal->flags & CAL_MDOWN) != 0 ? "M " : " ", */
/* c++ */
/* ); */
/* fflush(stdout); */
/* } */
static void calendar_refresh_events(struct cal *cal) {
cal->refresh_events = 1;
gtk_widget_queue_draw(cal->widget);
}
static int on_state_change(GtkWidget *widget, GdkEvent *ev, gpointer user_data) {
struct extra_data *data = (struct extra_data*)user_data;
struct cal *cal = data->cal;
/* calendar_refresh_events(cal); */
gtk_widget_queue_draw(cal->widget);
/* calendar_print_state(cal); */
return 1;
}
static char * file_load(char *path) {
FILE *f = fopen(path, "rb");
if (!f) return NULL;
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET);
char *string = malloc(fsize);
int res = fread(string, fsize, 1, f);
if (!res) return NULL;
fclose(f);
return string;
}
static struct ical * calendar_load_ical(struct cal *cal, char *path) {
// TODO: don't load duplicate calendars
struct ical* ical;
// TODO: free icalcomponent somewhere
const char *str = file_load(path);
if (str == NULL) {
printf("failed to read calendar\n");
return NULL;
}
icalcomponent *calendar = icalparser_parse_string(str);
if (!calendar) {
printf("failed to parse calendar\n");
return NULL;
}
// TODO: support >128 calendars
if (ARRAY_SIZE(cal->calendars) == cal->ncalendars) {
printf("calendar too big (well its not too big, viscal just sucks\n");
return NULL;
}
ical = &cal->calendars[cal->ncalendars++];
ical->calendar = calendar;
ical->visible = true;
free((void*)str);
return ical;
}
/* static void */
/* event_set_start(struct event *ev, time_t time, const icaltimezone *zone) { */
/* if (zone == NULL) */
/* zone = g_timezone; */
/* icaltimetype ictime = icaltime_from_timet_with_zone_with_zone(time, 1, zone); */
/* icalcomponent_set_dtstart(ev->vevent, ictime); */
/* } */
/* static void */
/* event_set_end(struct event *ev, time_t time, const icaltimezone *zone) { */
/* if (zone == NULL) */
/* zone = g_timezone; */
/* icaltimetype ictime = icaltime_from_timet_with_zone_with_zone(time, 1, zone); */
/* icalcomponent_set_dtend(ev->vevent, ictime); */
/* } */
static struct event *get_target(struct cal *cal) {
if (cal->target == -1)
return NULL;
return &cal->events[cal->target];
}
static icaltimetype icaltime_from_timet_ours(time_t time, int is_date,
struct cal *cal)
{
icaltimezone *tz;
tz = cal == NULL ? g_cal_tz : cal->tz;
return icaltime_from_timet_with_zone(time, is_date, tz_utc);
}
static void calendar_drop(struct cal *cal, double mx, double my) {
struct event *ev = get_target(cal);
if (!ev)
return;
icaltime_span span = icalcomponent_get_span(ev->vevent);
// TODO: use default event ARRAY_SIZE when dragging from gutter?
time_t len = span.end - span.start;
// XXX: should dragging timezone be the local timezone?
// XXX: this will probably destroy the timezone, we don't want that
// TODO: convert timezone on drag?
icaltimetype startt =
icaltime_from_timet_ours(ev->drag_time, 0, cal);
icalcomponent_set_dtstart(ev->vevent, startt);
icaltimetype endt =
icaltime_from_timet_ours(ev->drag_time + len, 0, cal);
icalcomponent_set_dtend(ev->vevent, endt);
}
static time_t location_to_time(time_t start, time_t end, double loc) {
return (time_t)((double)start) + (loc * (end - start));
}
static time_t calendar_pos_to_time(struct cal *cal, double y) {
// TODO: this is wrong wrt. zoom
return location_to_time(calendar_view_start(cal),
calendar_view_end(cal),
y/((double)cal->height * cal->zoom));
}
static time_t calendar_loc_to_time(struct cal *cal, double y) {
return location_to_time(calendar_view_start(cal),
calendar_view_end(cal),
y/cal->zoom);
}
static void event_click(struct cal *cal, struct event *event, int mx, int my) {
printf("clicked %s\n", icalcomponent_get_summary(event->vevent));
calendar_pos_to_time(cal, my);
}
// TODO: this should handle zh_CN and others as well
void time_remove_seconds(char *time, int n) {
int len = strlen(time);
int count = 0;
char *ws;
for (int i = 0; i < len; ++i) {
if (count == n) {
ws = &time[i];
while (*ws != '\0' &&
(*ws == ':' ||
(*ws >= '0' && *ws <= '9'))) ws++;
len = strlen(ws);
memcpy(&time[i-1], ws, len);
time[i-1+len] = '\0';
return;
}
// FIXME: instead of (==':'), we want (!= 0..9), in a unicode-enumerated way
count += time[i] == ':' ? 1 : 0;
}
}
static char *format_locale_time(char *buffer, int bsize, struct tm *tm) {
strftime(buffer, bsize, "%X", tm);
time_remove_seconds(buffer, 2);
return buffer;
}
static char *format_locale_timet(char *buffer, int bsize, time_t time) {
struct tm lt;
lt = *localtime(&time);
return format_locale_time(buffer, bsize, <);
}
static icalcomponent *create_event(struct cal *cal, time_t start, time_t end,
icalcomponent *ical) {
static const char *default_event_summary = "";
icalcomponent *vevent;
icaltimetype dtstart = icaltime_from_timet_ours(start, 0, cal);
icaltimetype dtend = icaltime_from_timet_ours(end, 0, cal);
vevent = icalcomponent_new(ICAL_VEVENT_COMPONENT);
icalcomponent_set_summary(vevent, default_event_summary);
icalcomponent_set_dtstart(vevent, dtstart);
icalcomponent_set_dtend(vevent, dtend);
icalcomponent_add_component(ical, vevent);
calendar_refresh_events(cal);
cal->select_after_sort = vevent;
return vevent;
}
static icalcomponent *calendar_def_cal(struct cal *cal) {
// TODO: configurable default calendar
if (cal->ncalendars > 0)
return cal->calendars[0].calendar;
return NULL;
}
static time_t closest_timeblock_for_timet(time_t st, int timeblock_size) {
struct tm lt;
lt = *localtime(&st);
lt.tm_min = round(lt.tm_min / timeblock_size) * timeblock_size;
lt.tm_sec = 0; // removes jitter
return mktime(<);
}
static time_t closest_timeblock(struct cal *cal, int y) {
time_t st = calendar_pos_to_time(cal, y);
return closest_timeblock_for_timet(st, cal->timeblock_size);
}
static int event_hit (struct event *ev, double mx, double my) {
return
mx >= ev->x
&& mx <= (ev->x + ev->width)
&& my >= ev->y
&& my <= (ev->y + ev->height);
}
static int events_hit (struct event *events, int nevents,
double mx, double my)
{
for (int i = 0; i < nevents; ++i) {
if (event_hit(&events[i], mx, my))
return i;
}
return -1;
}
static void zoom(struct cal *cal, double amt)
{
double newzoom = cal->zoom - amt * max(0.1, log(cal->zoom)) * 0.5;
if (newzoom < ZOOM_MIN) {
newzoom = ZOOM_MIN;
}
else if (newzoom > ZOOM_MAX) {
newzoom = ZOOM_MAX;
}
cal->zoom = newzoom;
cal->zoom_at = cal->my;
}
static int event_minutes(struct event *event)
{
time_t st, et;
vevent_span_timet(event->vevent, &st, &et);
return (et - st) / 60;
}
static int timeblock_size(struct cal *cal)
{
if (cal->selected_event_ind != -1) {
struct event *ev = get_selected_event(cal);
return event_minutes(ev);
}
return cal->timeblock_size;
}
static int find_closest_event(struct cal *cal, time_t near, int rel)
{
struct event *ev;
int is_up, ind;
time_t start, end, prev;
is_up = rel == -1;
prev = near;
if (cal->nevents == 0)
return -1;
else if (cal->nevents == 1)
return 0;
for (int i = cal->nevents-1; i >= 0; i--) {
ev = &cal->events[i];
vevent_span_timet(ev->vevent, &start, &end);
if (end <= near) {
ind = is_up ? i : i+1;
return ind == cal->nevents ? -1 : ind;
}
}
return 0;
}
static inline int relative_selection(struct cal *cal, int rel)
{
if (cal->selected_event_ind == -1) {
return find_closest_event(cal, cal->current, rel);
}
return clamp(cal->selected_event_ind + rel, 0, cal->nevents - 1);
}
static time_t get_hour(time_t current)
{
struct tm current_tm;
current_tm = *localtime(¤t);
current_tm.tm_min = 0;
current_tm.tm_sec = 0;
return mktime(¤t_tm);
}
static int get_minute(time_t current)
{
struct tm current_tm;
current_tm = *localtime(¤t);
return current_tm.tm_min;
}
static time_t get_smallest_closest_timeblock(time_t current, int round_by)
{
struct tm current_tm;
current_tm = *localtime(¤t);
current_tm.tm_min =
round(current_tm.tm_min / (double)round_by) * round_by;
current_tm.tm_sec = 0;
return mktime(¤t_tm);
}
static void align_down(struct cal *cal)
{
/* assert(!"implement me"); */
struct event *event =
get_selected_event(cal);
(void)event;
}
static void align_up(struct cal *cal)
{
/* assert(!"implement me"); */
struct event *event =
get_selected_event(cal);
(void)event;
}
static void align_hour(struct cal *cal)
{
struct tm current_tm;
time_t hour;
current_tm = *localtime(&cal->current);
current_tm.tm_min =
round(current_tm.tm_min / 60.0) * 60;
current_tm.tm_sec = 0;
hour = mktime(¤t_tm);
printf("tm_min %d\n", current_tm.tm_min);
cal->current = hour;
}
static void move_event_to(struct cal *cal, struct event *event, time_t to)
{
time_t st, et;
vevent_span_timet(event->vevent, &st, &et);
icaltimetype dtstart =
icaltime_from_timet_ours(to, 0, cal);
icaltimetype dtend =
icaltime_from_timet_ours(to + (et - st), 0, cal);
char *dtstart_str, *dtend_str;
dtstart_str = icaltime_as_ical_string(icalcomponent_get_dtstart(event->vevent));
dtend_str = icaltime_as_ical_string(icalcomponent_get_dtend(event->vevent));
printf("before moving start:%s end:%s\n", dtstart_str, dtend_str);
icalcomponent_set_dtstart(event->vevent, dtstart);
icalcomponent_set_dtend(event->vevent, dtend);
dtstart = icalcomponent_get_dtstart(event->vevent);
dtend = icalcomponent_get_dtend(event->vevent);
dtstart_str = icaltime_as_ical_string(dtstart);
dtend_str = icaltime_as_ical_string(dtend);
printf("after moving start:%s end:%s\n", dtstart_str, dtend_str);
calendar_refresh_events(cal);
}
static void move_event_now(struct cal *cal)
{
struct event *event =
get_selected_event(cal);
if (event == NULL)
return;
time_t closest =
get_smallest_closest_timeblock(time(NULL), SMALLEST_TIMEBLOCK);
move_event_to(cal, event, closest);
}
static int time_in_view(struct cal *cal, time_t time) {
time_t st = calendar_loc_to_time(cal, 0);
time_t et = calendar_loc_to_time(cal, 1.0);
return time >= st && time <= et;
}
static int timeline_in_view(struct cal *cal)
{
return time_in_view(cal, cal->current);
}
static void deselect(struct cal *cal)
{
cal->selected_event_ind = -1;
}
static void move_now(struct cal *cal)
{
deselect(cal);
cal->current =
get_smallest_closest_timeblock(time(NULL), SMALLEST_TIMEBLOCK);
if (!timeline_in_view(cal))
center_view(cal);
}
static void insert_event(struct cal *cal, time_t st, time_t et,
struct ical *ical)
{
cal->flags |= CAL_INSERTING;
create_event(cal, st, et, ical->calendar);
}
static void insert_event_action_with(struct cal *cal, time_t st)
{
// we should eventually always have a calendar
// at least a temporary one
if (cal->ncalendars == 0)
return;
time_t et = st + cal->timeblock_size * 60;
insert_event(cal, st, et, current_calendar(cal));
}
static void insert_event_after_action(struct cal *cal)
{
time_t start = cal->current + cal->timeblock_size * 60;
insert_event_action_with(cal, start);
}
static void insert_event_action(struct cal *cal)
{