|
| 1 | +/* |
| 2 | + * This file is part of bitfireAT/synctools which is released under GPLv3. |
| 3 | + * Copyright © All Contributors. See the LICENSE and AUTHOR files in the root directory for details. |
| 4 | + * SPDX-License-Identifier: GPL-3.0-or-later |
| 5 | + */ |
| 6 | + |
| 7 | +package at.bitfire.synctools.mapping.jtx.builder |
| 8 | + |
| 9 | +import android.content.Entity |
| 10 | +import at.bitfire.synctools.icalendar.DatePropertyTzMapper.normalizedDate |
| 11 | +import at.bitfire.synctools.icalendar.dtStart |
| 12 | +import at.bitfire.synctools.icalendar.due |
| 13 | +import at.bitfire.synctools.mapping.jtx.builder.TimeZoneIdMapper.toTimeZoneId |
| 14 | +import at.bitfire.synctools.util.AndroidTimeUtils.toTimestamp |
| 15 | +import at.techbee.jtx.JtxContract |
| 16 | +import at.techbee.jtx.JtxContract.JtxICalObject.TZ_ALLDAY |
| 17 | +import net.fortuna.ical4j.model.Property |
| 18 | +import net.fortuna.ical4j.model.component.CalendarComponent |
| 19 | +import net.fortuna.ical4j.model.component.VJournal |
| 20 | +import net.fortuna.ical4j.model.component.VToDo |
| 21 | +import java.time.temporal.Temporal |
| 22 | +import java.util.logging.Logger |
| 23 | + |
| 24 | +/** |
| 25 | + * Handles the iCalendar properties: DTSTART, DTEND, DUE, DURATION |
| 26 | + */ |
| 27 | +class TimeFieldsBuilder : JtxEntityBuilder { |
| 28 | + private val logger |
| 29 | + get() = Logger.getLogger(javaClass.name) |
| 30 | + |
| 31 | + override fun build(from: CalendarComponent, main: CalendarComponent, to: Entity) { |
| 32 | + if (from is VJournal) { |
| 33 | + buildJournal(from, to) |
| 34 | + } else if (from is VToDo) { |
| 35 | + buildTask(from, to) |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + private fun buildJournal(from: VJournal, to: Entity) { |
| 40 | + buildStartDate(from, to) |
| 41 | + |
| 42 | + ignoreDtEnd(from, to) |
| 43 | + ignoreDue(from, to) |
| 44 | + ignoreDuration(from, to) |
| 45 | + } |
| 46 | + |
| 47 | + private fun buildTask(from: VToDo, to: Entity) { |
| 48 | + val startTimeZoneId = buildStartDate(from, to) |
| 49 | + val dueTimeZoneId = buildDueDate(from, to) |
| 50 | + cleanUpTimeZones(startTimeZoneId, dueTimeZoneId, to) |
| 51 | + |
| 52 | + warnIfDueDateAfterStartDate(from) |
| 53 | + |
| 54 | + buildDuration(from, to) |
| 55 | + |
| 56 | + ignoreDtEnd(from, to) |
| 57 | + } |
| 58 | + |
| 59 | + private fun buildStartDate(from: CalendarComponent, to: Entity): String? { |
| 60 | + val start = from.dtStart<Temporal>()?.normalizedDate() |
| 61 | + val timeZoneId = start?.toTimeZoneId() |
| 62 | + if (start != null) { |
| 63 | + to.entityValues.put(JtxContract.JtxICalObject.DTSTART, start.toTimestamp()) |
| 64 | + to.entityValues.put(JtxContract.JtxICalObject.DTSTART_TIMEZONE, timeZoneId) |
| 65 | + } else { |
| 66 | + to.entityValues.putNull(JtxContract.JtxICalObject.DTSTART) |
| 67 | + to.entityValues.putNull(JtxContract.JtxICalObject.DTSTART_TIMEZONE) |
| 68 | + } |
| 69 | + |
| 70 | + return timeZoneId |
| 71 | + } |
| 72 | + |
| 73 | + private fun buildDueDate(from: CalendarComponent, to: Entity): String? { |
| 74 | + val due = from.due<Temporal>()?.normalizedDate() |
| 75 | + val timeZoneId = due?.toTimeZoneId() |
| 76 | + if (due != null) { |
| 77 | + to.entityValues.put(JtxContract.JtxICalObject.DUE, due.toTimestamp()) |
| 78 | + to.entityValues.put(JtxContract.JtxICalObject.DUE_TIMEZONE, timeZoneId) |
| 79 | + } else { |
| 80 | + to.entityValues.putNull(JtxContract.JtxICalObject.DUE) |
| 81 | + to.entityValues.putNull(JtxContract.JtxICalObject.DUE_TIMEZONE) |
| 82 | + } |
| 83 | + |
| 84 | + return timeZoneId |
| 85 | + } |
| 86 | + |
| 87 | + private fun buildDuration(task: VToDo, to: Entity) { |
| 88 | + val durationValue = task.duration?.value |
| 89 | + |
| 90 | + if (durationValue != null && task.dtStart<Temporal>()?.date == null) { |
| 91 | + logger.warning("Found DURATION without DTSTART in VTODO; ignoring DURATION") |
| 92 | + to.entityValues.putNull(JtxContract.JtxICalObject.DURATION) |
| 93 | + } else if (durationValue != null && task.due<Temporal>()?.date != null) { |
| 94 | + logger.warning("Found DURATION and DUE in VTODO; ignoring DURATION") |
| 95 | + to.entityValues.putNull(JtxContract.JtxICalObject.DURATION) |
| 96 | + } else if (durationValue != null) { |
| 97 | + to.entityValues.put(JtxContract.JtxICalObject.DURATION, durationValue) |
| 98 | + } else { |
| 99 | + to.entityValues.putNull(JtxContract.JtxICalObject.DURATION) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private fun cleanUpTimeZones(startTimeZoneId: String?, dueTimeZoneId: String?, to: Entity) { |
| 104 | + if (startTimeZoneId == TZ_ALLDAY && dueTimeZoneId != null && dueTimeZoneId != TZ_ALLDAY) { |
| 105 | + logger.warning("DTSTART is DATE but DUE is DATE-TIME, rewriting DTSTART to DATE-TIME") |
| 106 | + to.entityValues.put(JtxContract.JtxICalObject.DTSTART_TIMEZONE, dueTimeZoneId) |
| 107 | + } else if (dueTimeZoneId == TZ_ALLDAY && startTimeZoneId != null && startTimeZoneId != TZ_ALLDAY) { |
| 108 | + logger.warning("DTSTART is DATE-TIME but DUE is DATE, rewriting DUE to DATE-TIME") |
| 109 | + to.entityValues.put(JtxContract.JtxICalObject.DUE_TIMEZONE, startTimeZoneId) |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private fun warnIfDueDateAfterStartDate(task: VToDo) { |
| 114 | + val start = task.dtStart<Temporal>()?.normalizedDate()?.toTimestamp() |
| 115 | + val due = task.due<Temporal>()?.normalizedDate()?.toTimestamp() |
| 116 | + |
| 117 | + // Previously DUE was dropped. Now reduced to a warning. |
| 118 | + // See also: https://github.com/bitfireAT/ical4android/issues/70 |
| 119 | + if (start != null && due != null && due < start) { |
| 120 | + logger.warning("Found invalid DUE < DTSTART") |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + private fun ignoreDtEnd(from: CalendarComponent, to: Entity) { |
| 125 | + if (from.hasProperty(Property.DTEND)) { |
| 126 | + logger.warning("DTEND must not be used with VJOURNAL, ignoring property.") |
| 127 | + } |
| 128 | + |
| 129 | + to.entityValues.putNull(JtxContract.JtxICalObject.DTEND) |
| 130 | + to.entityValues.putNull(JtxContract.JtxICalObject.DTEND_TIMEZONE) |
| 131 | + } |
| 132 | + |
| 133 | + private fun ignoreDue(from: CalendarComponent, to: Entity) { |
| 134 | + if (from.hasProperty(Property.DUE)) { |
| 135 | + logger.warning("DUE must not be used with VJOURNAL, ignoring property.") |
| 136 | + } |
| 137 | + |
| 138 | + to.entityValues.putNull(JtxContract.JtxICalObject.DUE) |
| 139 | + to.entityValues.putNull(JtxContract.JtxICalObject.DUE_TIMEZONE) |
| 140 | + } |
| 141 | + |
| 142 | + private fun ignoreDuration(from: CalendarComponent, to: Entity) { |
| 143 | + if (from.hasProperty(Property.DURATION)) { |
| 144 | + logger.warning("DURATION must not be used with VJOURNAL, ignoring property.") |
| 145 | + } |
| 146 | + |
| 147 | + to.entityValues.putNull(JtxContract.JtxICalObject.DURATION) |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +private fun CalendarComponent.hasProperty(name: String) = getProperty<Property>(name).isPresent |
0 commit comments