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.scheduleReminder(event, this)
if (addToCalDAV && event.source != SOURCE_SIMPLE_CALENDAR) {
if (addToCalDAV && event.source != SOURCE_SIMPLE_CALENDAR && context.config.caldavSync) {
CalDAVHandler(context).insertCalDAVEvent(event)
}
@ -235,7 +235,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
context.updateWidgets()
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)
}
callback()
@ -408,7 +408,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
context.cancelNotification(it.toInt())
}
if (deleteFromCalDAV) {
if (deleteFromCalDAV && context.config.caldavSync) {
events.forEach {
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)
}
fun getImportIds(): ArrayList<String> {
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 getEventsWithImportIds() = getEvents("").filter { it.importId.trim().isNotEmpty() } as ArrayList<Event>
fun getEventWithId(id: Int): Event? {
val selection = "$MAIN_TABLE_NAME.$COL_ID = ?"

View File

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

View File

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