Skip to content
Snippets Groups Projects
Verified Commit 4910b1df authored by Janne Mareike Koschinski's avatar Janne Mareike Koschinski
Browse files

Fixes #135

parent 7a26345c
Branches
Tags
No related merge requests found
......@@ -163,6 +163,18 @@ class HighlightListFragment : SettingsFragment(), SettingsFragment.Savable,
private fun startIgnoreRuleDrag(holder: HighlightRuleAdapter.HighlightRuleViewHolder) =
ignoreRulesHelper.startDrag(holder)
private fun nextId(): Int {
val maxRuleId = rulesAdapter.list.maxBy { it.id }?.id
val maxIgnoreRuleId = ignoreRulesAdapter.list.maxBy { it.id }?.id
return when {
maxRuleId != null && maxIgnoreRuleId != null -> maxOf(maxRuleId, maxIgnoreRuleId) + 1
maxIgnoreRuleId != null -> maxIgnoreRuleId + 1
maxRuleId != null -> maxRuleId + 1
else -> 0
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (resultCode == Activity.RESULT_OK && data != null) {
when (requestCode) {
......@@ -178,7 +190,7 @@ class HighlightListFragment : SettingsFragment(), SettingsFragment.Savable,
val newRule = data.getSerializableExtra("new") as? HighlightRuleManager.HighlightRule
if (newRule != null) {
rulesAdapter.add(newRule)
rulesAdapter.add(newRule.copy(id = nextId()))
}
}
REQUEST_UPDATE_IGNORE_RULE -> {
......@@ -193,7 +205,7 @@ class HighlightListFragment : SettingsFragment(), SettingsFragment.Savable,
val newRule = data.getSerializableExtra("new") as? HighlightRuleManager.HighlightRule
if (newRule != null) {
ignoreRulesAdapter.add(newRule)
ignoreRulesAdapter.add(newRule.copy(id = nextId()))
}
}
}
......
......@@ -81,6 +81,7 @@ class HighlightRuleFragment : SettingsFragment(), SettingsFragment.Savable,
}
private fun applyChanges() = HighlightRuleManager.HighlightRule(
id = rule?.id ?: -1,
isInverse = rule?.isInverse ?: isInverse ?: false,
isEnabled = enabled.isChecked,
name = name.text.toString(),
......
......@@ -28,6 +28,7 @@ class HighlightRuleManager(
proxy: SignalProxy
) : SyncableObject(proxy, "HighlightRuleManager"), IHighlightRuleManager {
data class HighlightRule(
val id: Int,
val name: String,
val isRegEx: Boolean = false,
val isCaseSensitive: Boolean = false,
......@@ -38,14 +39,24 @@ class HighlightRuleManager(
) : Serializable
override fun toVariantMap(): QVariantMap = mapOf(
"HighlightRuleList" to QVariant.of(initHighlightRuleList(), Type.QVariantMap)
"HighlightRuleList" to QVariant.of(initHighlightRuleList(), Type.QVariantMap),
"highlightNick" to QVariant.of(_highlightNick.value, Type.Int),
"nicksCaseSensitive" to QVariant.of(_nicksCaseSensitive, Type.Bool)
)
override fun fromVariantMap(properties: QVariantMap) {
initSetHighlightRuleList(properties["HighlightRuleList"].valueOr(::emptyMap))
_highlightNick = properties["highlightNick"].value<Int>()?.let {
IHighlightRuleManager.HighlightNickType.of(it)
} ?: _highlightNick
_nicksCaseSensitive = properties["nicksCaseSensitive"].value(_nicksCaseSensitive)
}
override fun initHighlightRuleList(): QVariantMap = mapOf(
"id" to QVariant.of(_highlightRuleList.map {
QVariant.of(it.id, Type.Int)
}, Type.QVariantList),
"name" to QVariant.of(_highlightRuleList.map {
it.name
}, Type.QStringList),
......@@ -66,12 +77,11 @@ class HighlightRuleManager(
}, Type.QStringList),
"channel" to QVariant.of(_highlightRuleList.map {
it.channel
}, Type.QStringList),
"highlightNick" to QVariant.of(_highlightNick.value, Type.Int),
"nicksCaseSensitive" to QVariant.of(_nicksCaseSensitive, Type.Bool)
}, Type.QStringList)
)
override fun initSetHighlightRuleList(highlightRuleList: QVariantMap) {
val idList = highlightRuleList["id"].valueOr<QVariantList>(::emptyList)
val nameList = highlightRuleList["name"].valueOr<QStringList>(::emptyList)
val isRegExList = highlightRuleList["isRegEx"].valueOr<QVariantList>(::emptyList)
val isCaseSensitiveList = highlightRuleList["isCaseSensitive"].valueOr<QVariantList>(::emptyList)
......@@ -79,14 +89,15 @@ class HighlightRuleManager(
val isInverseList = highlightRuleList["isInverse"].valueOr<QVariantList>(::emptyList)
val senderList = highlightRuleList["sender"].valueOr<QStringList>(::emptyList)
val channelList = highlightRuleList["channel"].valueOr<QStringList>(::emptyList)
val size = nameList.size
if (isRegExList.size != size || isCaseSensitiveList.size != size ||
val size = idList.size
if (nameList.size != size || isRegExList.size != size || isCaseSensitiveList.size != size ||
isEnabledList.size != size || isInverseList.size != size || senderList.size != size ||
channelList.size != size)
return
_highlightRuleList = List(size, {
_highlightRuleList = List(size) {
HighlightRule(
id = idList[it].value(0),
name = nameList[it] ?: "",
isRegEx = isRegExList[it].value(false),
isCaseSensitive = isCaseSensitiveList[it].value(false),
......@@ -95,28 +106,24 @@ class HighlightRuleManager(
sender = senderList[it] ?: "",
channel = channelList[it] ?: ""
)
})
_highlightNick = highlightRuleList["highlightNick"].value<Int>()?.let {
IHighlightRuleManager.HighlightNickType.of(it)
} ?: _highlightNick
_nicksCaseSensitive = highlightRuleList["nicksCaseSensitive"].value(_nicksCaseSensitive)
}
}
override fun removeHighlightRule(highlightRule: String) = removeAt(indexOf(highlightRule))
override fun removeHighlightRule(highlightRule: Int) = removeAt(indexOf(highlightRule))
override fun toggleHighlightRule(highlightRule: String) {
override fun toggleHighlightRule(highlightRule: Int) {
_highlightRuleList = _highlightRuleList.map {
if (it.name == highlightRule) it.copy(isEnabled = !it.isEnabled) else it
if (it.id == highlightRule) it.copy(isEnabled = !it.isEnabled) else it
}
}
override fun addHighlightRule(name: String, isRegEx: Boolean, isCaseSensitive: Boolean,
override fun addHighlightRule(id: Int, name: String, isRegEx: Boolean, isCaseSensitive: Boolean,
isEnabled: Boolean, isInverse: Boolean, sender: String,
chanName: String) {
if (contains(name)) return
if (contains(id)) return
_highlightRuleList += HighlightRule(
name, isRegEx, isCaseSensitive, isEnabled, isInverse, sender, chanName
id, name, isRegEx, isCaseSensitive, isEnabled, isInverse, sender, chanName
)
}
......@@ -131,8 +138,8 @@ class HighlightRuleManager(
_nicksCaseSensitive = nicksCaseSensitive
}
fun indexOf(name: String): Int = _highlightRuleList.indexOfFirst { it.name == name }
fun contains(name: String) = _highlightRuleList.any { it.name == name }
fun indexOf(id: Int): Int = _highlightRuleList.indexOfFirst { it.id == id }
fun contains(id: Int) = _highlightRuleList.any { it.id == id }
fun isEmpty() = _highlightRuleList.isEmpty()
fun count() = _highlightRuleList.count()
......
......@@ -48,12 +48,12 @@ interface IHighlightRuleManager : ISyncableObject {
* @param highlightRule A valid ignore rule
*/
@Slot
fun requestRemoveHighlightRule(highlightRule: String) {
REQUEST("requestRemoveHighlightRule", ARG(highlightRule, Type.QString))
fun requestRemoveHighlightRule(highlightRule: Int) {
REQUEST("requestRemoveHighlightRule", ARG(highlightRule, Type.Int))
}
@Slot
fun removeHighlightRule(highlightRule: String)
fun removeHighlightRule(highlightRule: Int)
/**
* Request toggling of "isEnabled" flag of a given ignore rule.
......@@ -62,12 +62,12 @@ interface IHighlightRuleManager : ISyncableObject {
* @param highlightRule A valid ignore rule
*/
@Slot
fun requestToggleHighlightRule(highlightRule: String) {
REQUEST("requestToggleHighlightRule", ARG(highlightRule, Type.QString))
fun requestToggleHighlightRule(highlightRule: Int) {
REQUEST("requestToggleHighlightRule", ARG(highlightRule, Type.Int))
}
@Slot
fun toggleHighlightRule(highlightRule: String)
fun toggleHighlightRule(highlightRule: Int)
/**
* Request an HighlightRule to be added to the ignore list
......@@ -79,17 +79,17 @@ interface IHighlightRuleManager : ISyncableObject {
* @param chanName The channel in which the rule should apply
*/
@Slot
fun requestAddHighlightRule(name: String, isRegEx: Boolean, isCaseSensitive: Boolean,
isEnabled: Boolean,
isInverse: Boolean, sender: String, chanName: String) {
REQUEST("requestAddHighlightRule", ARG(name, Type.QString), ARG(isRegEx, Type.Bool),
ARG(isCaseSensitive, Type.Bool), ARG(isEnabled, Type.Bool), ARG(isInverse, Type.Bool),
ARG(sender, Type.QString), ARG(chanName, Type.QString))
fun requestAddHighlightRule(id: Int, name: String, isRegEx: Boolean, isCaseSensitive: Boolean,
isEnabled: Boolean, isInverse: Boolean, sender: String,
chanName: String) {
REQUEST("requestAddHighlightRule", ARG(id, Type.Int), ARG(name, Type.QString),
ARG(isRegEx, Type.Bool), ARG(isCaseSensitive, Type.Bool), ARG(isEnabled, Type.Bool),
ARG(isInverse, Type.Bool), ARG(sender, Type.QString), ARG(chanName, Type.QString))
}
@Slot
fun addHighlightRule(name: String, isRegEx: Boolean, isCaseSensitive: Boolean, isEnabled: Boolean,
isInverse: Boolean, sender: String, chanName: String)
fun addHighlightRule(id: Int, name: String, isRegEx: Boolean, isCaseSensitive: Boolean,
isEnabled: Boolean, isInverse: Boolean, sender: String, chanName: String)
@Slot
fun requestSetHighlightNick(highlightNick: Int) {
......
......@@ -22,10 +22,7 @@ package de.kuschku.libquassel.quassel.syncables
import de.kuschku.libquassel.protocol.primitive.serializer.VariantMapSerializer
import de.kuschku.libquassel.quassel.syncables.interfaces.IHighlightRuleManager
import de.kuschku.libquassel.session.SignalProxy
import de.kuschku.libquassel.util.randomBoolean
import de.kuschku.libquassel.util.randomOf
import de.kuschku.libquassel.util.randomString
import de.kuschku.libquassel.util.roundTrip
import de.kuschku.libquassel.util.*
import org.junit.Test
class HighlightRuleManagerTest {
......@@ -36,6 +33,7 @@ class HighlightRuleManagerTest {
original.setNicksCaseSensitive(randomBoolean())
original.setHighlightRuleList(listOf(
HighlightRuleManager.HighlightRule(
randomInt(),
randomString(),
randomBoolean(),
randomBoolean(),
......@@ -45,6 +43,7 @@ class HighlightRuleManagerTest {
randomString()
),
HighlightRuleManager.HighlightRule(
randomInt(),
randomString(),
randomBoolean(),
randomBoolean(),
......@@ -54,6 +53,7 @@ class HighlightRuleManagerTest {
randomString()
),
HighlightRuleManager.HighlightRule(
randomInt(),
randomString(),
randomBoolean(),
randomBoolean(),
......@@ -76,6 +76,7 @@ class HighlightRuleManagerTest {
original.setNicksCaseSensitive(randomBoolean())
original.setHighlightRuleList(listOf(
HighlightRuleManager.HighlightRule(
randomInt(),
randomString(),
randomBoolean(),
randomBoolean(),
......@@ -85,6 +86,7 @@ class HighlightRuleManagerTest {
randomString()
),
HighlightRuleManager.HighlightRule(
randomInt(),
randomString(),
randomBoolean(),
randomBoolean(),
......@@ -94,6 +96,7 @@ class HighlightRuleManagerTest {
randomString()
),
HighlightRuleManager.HighlightRule(
randomInt(),
randomString(),
randomBoolean(),
randomBoolean(),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment