culendar 4.48 KB
Newer Older
mit's avatar
mit committed
1 2
#! /usr/bin/env python3
# coding: utf-8
mit's avatar
mit committed
3 4 5
import curses
import datetime
import icalendar
6
from cul import config, culendar, calendar, helpscr, confscr, culdav, getkey
mit's avatar
mit committed
7

mit's avatar
mit committed
8

mit's avatar
mit committed
9 10 11 12 13 14
def main(stdscr):
    # configialize ncurses and get conf
    conf = config.Config()

    # the current day
    day = datetime.date.today()
mit's avatar
mit committed
15 16 17
    if (day.isoweekday() in [6, 7]) & (conf.WE == 0):
        # if we are during an unshown day, go to the previous Friday
        while day.isoweekday() in [6, 7]:
mit's avatar
mit committed
18 19
            day = datetime.date.fromordinal(day.toordinal() - 1)

mit's avatar
mit committed
20 21
    # get data and todo and caldavs urls
    cal, todo = calendar.load(conf.datafile, conf.todofile)
22
    cdav = culdav.Cdav(conf.caldav, stdscr, conf)
23

mit's avatar
mit committed
24
    # include first drawing
mit's avatar
mit committed
25
    cul = culendar.Culendar(stdscr, conf, day, cal, cdav, todo)
mit's avatar
mit committed
26 27 28 29

    # main loop
    key = ""
    while not(key in conf.keys['quit']):
30
        key = getkey.getkey(cul.screen, conf.debug)
mit's avatar
mit committed
31 32 33 34

        if key == 'KEY_RESIZE':
            cul.update()

35 36
        # repeat action at least 1, or the entered count value
        repetitions = max(1, cul.count)
mit's avatar
mit committed
37 38
        # changing day or week or year
        if key in conf.keys['prevday']:
39
            cul.addday(-repetitions)
mit's avatar
mit committed
40
        if key in conf.keys['nextday']:
41
            cul.addday(+repetitions)
mit's avatar
mit committed
42
        if key in conf.keys['nextweek']:
43
            cul.addday(+7*repetitions)
mit's avatar
mit committed
44
        if key in conf.keys['prevweek']:
45
            cul.addday(-7*repetitions)
mit's avatar
mit committed
46
        if key in conf.keys['nextmonth']:
47
            cul.addmonth(+repetitions)
mit's avatar
mit committed
48
        if key in conf.keys['prevmonth']:
49
            cul.addmonth(-repetitions)
mit's avatar
mit committed
50
        if key in conf.keys['nextyear']:
51
            cul.addyear(+repetitions)
mit's avatar
mit committed
52
        if key in conf.keys['prevyear']:
53
            cul.addyear(-repetitions)
mit's avatar
mit committed
54 55 56 57 58 59 60 61 62 63 64 65 66
        if key in conf.keys['today']:
            cul.day = datetime.date.today()
            cul.draw_week()
        if key in conf.keys['setday']:
            cul.changeday()
        if key in conf.keys['startweek']:
            cul.addday(-cul.day.isoweekday()+1)
        if key in conf.keys['endweek']:
            cul.addday(-cul.day.isoweekday()+cul.daynb)

        # change highlighted event of current day
        if key in conf.keys['nextevent']:
            cul.sameday()
67
            cul.changehl(+repetitions)
mit's avatar
mit committed
68 69 70
            cul.draw_hl()
        if key in conf.keys['prevevent']:
            cul.sameday()
71
            cul.changehl(-repetitions)
mit's avatar
mit committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
            cul.draw_hl()

        # delete, edit, add (highlighted) event (if existing)
        if key in conf.keys['delevent']:
            cul.del_event()
        if key in conf.keys['editevent']:
            cul.edit_event()
        if key in conf.keys['addevent']:
            cul.add_event()
        if key in conf.keys['tagevent']:
            cul.tag_event()

        # toggle week-end display
        if key in conf.keys['toggleWE']:
            cul.toggle_WE()

mit's avatar
mit committed
88 89 90 91
        # toggle Todo bar focus (appear/disappear)
        if key in conf.keys['toggletodo']:
            cul.toggle_TODO()

mit's avatar
mit committed
92 93 94
        if key in conf.keys['redraw']:
            cul.clear_cal()
            cul.draw_cal()
95 96 97
        if key in conf.keys['sync']:
            cul.sync_caldav()

mit's avatar
mit committed
98 99 100 101 102 103 104 105 106 107 108 109
        if key in conf.keys['help']:
            H = helpscr.Help(cul.screen, cul.conf)
            H.help_screen()
            # redraw, works even in case of KEY_RESIZE inside help
            cul.update()
        if key in conf.keys['save']:
            cul.inform(_("Saving configuration and calendar…"))
            conf.save()
            cul.save()
            cul.inform(_("Configuration and calendar saved!"))
        if key in conf.keys['setconfig']:
            C = confscr.Conf(cul.screen, cul.conf)
110 111
            # real copy to keep a backup
            old_caldav_conf = [conf for conf in cul.conf._caldav]
mit's avatar
mit committed
112 113
            # conf_screen returns a possibly modified conf
            cul.conf = C.conf_screen()
114 115
            if old_caldav_conf != cul.conf._caldav:
                cul.caldav.update(old_caldav_conf)
mit's avatar
mit committed
116 117 118 119 120 121 122
            # redraw, works even in case of KEY_RESIZE inside help
            cul.update()
        if key in conf.keys['import']:
            cul.importcal()
        if key in conf.keys['export']:
            cul.exportcal()

123 124 125 126 127 128 129
        # define the buffer and the repetition of actions
        try:  # is the key an integer?
            # type 1 then 2, get 12
            cul.count = cul.count*10 + int(key)
        except:  # not a number, back to zero
            cul.count = 0

mit's avatar
mit committed
130 131 132 133 134
    # autosave on quit
    if conf.autosave:
        cul.save()
        conf.save()

mit's avatar
mit committed
135

mit's avatar
mit committed
136 137
# here starts culendar!
curses.wrapper(main)