fix #219, update events imported through .ics file if necessary

This commit is contained in:
tibbi 2017-09-24 17:35:09 +02:00
parent 63d70afae0
commit 3908a6e3db
3 changed files with 60 additions and 67 deletions

View File

@ -212,7 +212,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
context.updateWidgets() context.updateWidgets()
context.scheduleReminder(event, this) context.scheduleReminder(event, this)
if (addToCalDAV && event.source != SOURCE_SIMPLE_CALENDAR) { if (addToCalDAV && event.source != SOURCE_SIMPLE_CALENDAR && context.config.caldavSync) {
CalDAVHandler(context).insertCalDAVEvent(event) CalDAVHandler(context).insertCalDAVEvent(event)
} }
@ -235,7 +235,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
context.updateWidgets() context.updateWidgets()
context.scheduleReminder(event, this) context.scheduleReminder(event, this)
if (updateAtCalDAV && event.source != SOURCE_SIMPLE_CALENDAR) { if (updateAtCalDAV && event.source != SOURCE_SIMPLE_CALENDAR && context.config.caldavSync) {
CalDAVHandler(context).updateCalDAVEvent(event) CalDAVHandler(context).updateCalDAVEvent(event)
} }
callback() callback()
@ -408,7 +408,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
context.cancelNotification(it.toInt()) context.cancelNotification(it.toInt())
} }
if (deleteFromCalDAV) { if (deleteFromCalDAV && context.config.caldavSync) {
events.forEach { events.forEach {
CalDAVHandler(context).deleteCalDAVEvent(it) CalDAVHandler(context).deleteCalDAVEvent(it)
} }
@ -533,24 +533,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
mDb.update(MAIN_TABLE_NAME, values, selection, selectionArgs) mDb.update(MAIN_TABLE_NAME, values, selection, selectionArgs)
} }
fun getImportIds(): ArrayList<String> { fun getEventsWithImportIds() = getEvents("").filter { it.importId.trim().isNotEmpty() } as ArrayList<Event>
val ids = ArrayList<String>()
val columns = arrayOf(COL_IMPORT_ID)
val selection = "$COL_IMPORT_ID IS NOT NULL"
var cursor: Cursor? = null
try {
cursor = mDb.query(MAIN_TABLE_NAME, columns, selection, null, null, null, null)
if (cursor != null && cursor.moveToFirst()) {
do {
val id = cursor.getStringValue(COL_IMPORT_ID)
ids.add(id)
} while (cursor.moveToNext())
}
} finally {
cursor?.close()
}
return ids.filter { it.trim().isNotEmpty() } as ArrayList<String>
}
fun getEventWithId(id: Int): Event? { fun getEventWithId(id: Int): Event? {
val selection = "$MAIN_TABLE_NAME.$COL_ID = ?" val selection = "$MAIN_TABLE_NAME.$COL_ID = ?"

View File

@ -14,8 +14,8 @@ class IcsExporter {
EXPORT_FAIL, EXPORT_OK, EXPORT_PARTIAL EXPORT_FAIL, EXPORT_OK, EXPORT_PARTIAL
} }
var eventsExported = 0 private var eventsExported = 0
var eventsFailed = 0 private var eventsFailed = 0
fun exportEvents(activity: SimpleActivity, file: File, events: ArrayList<Event>, callback: (result: ExportResult) -> Unit) { fun exportEvents(activity: SimpleActivity, file: File, events: ArrayList<Event>, callback: (result: ExportResult) -> Unit) {
activity.getFileOutputStream(file) { activity.getFileOutputStream(file) {
@ -54,12 +54,11 @@ class IcsExporter {
out.writeLn(END_CALENDAR) out.writeLn(END_CALENDAR)
} }
callback(if (eventsExported == 0) {
EXPORT_FAIL callback(when {
} else if (eventsFailed > 0) { eventsExported == 0 -> EXPORT_FAIL
EXPORT_PARTIAL eventsFailed > 0 -> EXPORT_PARTIAL
} else { else -> EXPORT_OK
EXPORT_OK
}) })
} }
} }
@ -72,10 +71,12 @@ class IcsExporter {
private fun checkReminder(minutes: Int, out: BufferedWriter) { private fun checkReminder(minutes: Int, out: BufferedWriter) {
if (minutes != -1) { if (minutes != -1) {
out.writeLn(BEGIN_ALARM) out.apply {
out.writeLn("$ACTION$DISPLAY") writeLn(BEGIN_ALARM)
out.writeLn("$TRIGGER-${Parser().getDurationCode(minutes)}") writeLn("$ACTION$DISPLAY")
out.writeLn(END_ALARM) writeLn("$TRIGGER-${Parser().getDurationCode(minutes)}")
writeLn(END_ALARM)
}
} }
} }

View File

@ -16,28 +16,28 @@ class IcsImporter {
IMPORT_FAIL, IMPORT_OK, IMPORT_PARTIAL IMPORT_FAIL, IMPORT_OK, IMPORT_PARTIAL
} }
var curStart = -1 private var curStart = -1
var curEnd = -1 private var curEnd = -1
var curTitle = "" private var curTitle = ""
var curDescription = "" private var curDescription = ""
var curImportId = "" private var curImportId = ""
var curFlags = 0 private var curFlags = 0
var curReminderMinutes = ArrayList<Int>() private var curReminderMinutes = ArrayList<Int>()
var curRepeatExceptions = ArrayList<Int>() private var curRepeatExceptions = ArrayList<Int>()
var curRepeatInterval = 0 private var curRepeatInterval = 0
var curRepeatLimit = 0 private var curRepeatLimit = 0
var curRepeatRule = 0 private var curRepeatRule = 0
var curEventType = DBHelper.REGULAR_EVENT_TYPE_ID private var curEventType = DBHelper.REGULAR_EVENT_TYPE_ID
var curLastModified = 0L private var curLastModified = 0L
var isNotificationDescription = false private var isNotificationDescription = false
var lastReminderAction = "" private var lastReminderAction = ""
var eventsImported = 0 private var eventsImported = 0
var eventsFailed = 0 private var eventsFailed = 0
fun importEvents(activity: SimpleActivity, path: String, defaultEventType: Int): ImportResult { fun importEvents(activity: SimpleActivity, path: String, defaultEventType: Int): ImportResult {
try { try {
val importIDs = activity.dbHelper.getImportIds() val existingEvents = activity.dbHelper.getEventsWithImportIds()
var prevLine = "" var prevLine = ""
val inputStream = if (path.contains("/")) { val inputStream = if (path.contains("/")) {
@ -73,7 +73,7 @@ class IcsImporter {
} else if (line.startsWith(DESCRIPTION) && !isNotificationDescription) { } else if (line.startsWith(DESCRIPTION) && !isNotificationDescription) {
curDescription = line.substring(DESCRIPTION.length).replace("\\n", "\n") curDescription = line.substring(DESCRIPTION.length).replace("\\n", "\n")
} else if (line.startsWith(UID)) { } else if (line.startsWith(UID)) {
curImportId = line.substring(UID.length) curImportId = line.substring(UID.length).trim()
} else if (line.startsWith(RRULE)) { } else if (line.startsWith(RRULE)) {
val repeatRule = Parser().parseRepeatInterval(line.substring(RRULE.length), curStart) val repeatRule = Parser().parseRepeatInterval(line.substring(RRULE.length), curStart)
curRepeatRule = repeatRule.repeatRule curRepeatRule = repeatRule.repeatRule
@ -93,11 +93,16 @@ class IcsImporter {
} else if (line.startsWith(EXDATE)) { } else if (line.startsWith(EXDATE)) {
curRepeatExceptions.add(getTimestamp(line.substring(EXDATE.length))) curRepeatExceptions.add(getTimestamp(line.substring(EXDATE.length)))
} else if (line == END_EVENT) { } else if (line == END_EVENT) {
if (curTitle.isEmpty() || curStart == -1 || curEnd == -1 || importIDs.contains(curImportId)) if (curStart != -1 && curEnd == -1)
curEnd = curStart
if (curTitle.isEmpty() || curStart == -1)
continue continue
if (curImportId.isNotEmpty()) val eventToUpdate = existingEvents.firstOrNull { curImportId == it.importId }
importIDs.add(curImportId) if (eventToUpdate != null && eventToUpdate.lastUpdated >= curLastModified) {
continue
}
val event = Event(0, curStart, curEnd, curTitle, curDescription, curReminderMinutes.getOrElse(0, { -1 }), val event = Event(0, curStart, curEnd, curTitle, curDescription, curReminderMinutes.getOrElse(0, { -1 }),
curReminderMinutes.getOrElse(1, { -1 }), curReminderMinutes.getOrElse(2, { -1 }), curRepeatInterval, curReminderMinutes.getOrElse(1, { -1 }), curReminderMinutes.getOrElse(2, { -1 }), curRepeatInterval,
@ -108,28 +113,32 @@ class IcsImporter {
event.endTS -= DAY event.endTS -= DAY
} }
activity.dbHelper.insert(event, false) { if (eventToUpdate == null) {
for (exceptionTS in curRepeatExceptions) { activity.dbHelper.insert(event, false) {
activity.dbHelper.addEventRepeatException(it, exceptionTS) for (exceptionTS in curRepeatExceptions) {
activity.dbHelper.addEventRepeatException(it, exceptionTS)
}
existingEvents.add(event)
} }
eventsImported++ } else {
resetValues() event.id = eventToUpdate.id
activity.dbHelper.update(event, true) {}
} }
eventsImported++
resetValues()
} }
prevLine = line prevLine = line
} }
} }
} catch (e: Exception) { } catch (e: Exception) {
activity.showErrorToast(e.toString(), Toast.LENGTH_LONG) activity.showErrorToast(e, Toast.LENGTH_LONG)
eventsFailed++ eventsFailed++
} }
return if (eventsImported == 0) { return when {
IMPORT_FAIL eventsImported == 0 -> IMPORT_FAIL
} else if (eventsFailed > 0) { eventsFailed > 0 -> IMPORT_PARTIAL
IMPORT_PARTIAL else -> IMPORT_OK
} else {
IMPORT_OK
} }
} }