diff --git a/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/notification/AbstractNotificationProvider.kt b/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/notification/AbstractNotificationProvider.kt new file mode 100644 index 0000000000000000000000000000000000000000..c71c81bbf55f254af762145447a0187ef3c04dfa --- /dev/null +++ b/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/notification/AbstractNotificationProvider.kt @@ -0,0 +1,40 @@ +/* + * Briar Desktop + * Copyright (C) 2021-2022 The Briar Project + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ +package org.briarproject.briar.desktop.notification + +import org.briarproject.briar.desktop.utils.InternationalizationUtils.i18nF +import org.briarproject.briar.desktop.utils.InternationalizationUtils.i18nP + +abstract class AbstractNotificationProvider : VisualNotificationProvider { + + internal abstract fun sendNotification(text: String) + + override fun notifyPrivateMessages(num: Int, contacts: Int) = sendNotification( + if (contacts == 1) + i18nP("notifications.message.private.one_chat", num) + else + i18nF("notifications.message.private.several_chats", num, contacts) + ) + + override fun notifyForumPosts(num: Int, forums: Int) = sendNotification( + if (forums == 1) + i18nP("notifications.message.forum.one_forum", num) + else + i18nF("notifications.message.forum.several_forums", num, forums) + ) +} diff --git a/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/notification/linux/LibnotifyNotificationProvider.kt b/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/notification/linux/LibnotifyNotificationProvider.kt index 0e91e5d8f0b4f431accc834c7cd5a1753f0d8dea..3b9846cc7d924edbd799439481f08fe3f0e506f6 100644 --- a/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/notification/linux/LibnotifyNotificationProvider.kt +++ b/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/notification/linux/LibnotifyNotificationProvider.kt @@ -22,14 +22,12 @@ import com.sun.jna.Native import com.sun.jna.Pointer import com.sun.jna.ptr.PointerByReference import mu.KotlinLogging -import org.briarproject.briar.desktop.notification.VisualNotificationProvider +import org.briarproject.briar.desktop.notification.AbstractNotificationProvider import org.briarproject.briar.desktop.utils.InternationalizationUtils.i18n -import org.briarproject.briar.desktop.utils.InternationalizationUtils.i18nF -import org.briarproject.briar.desktop.utils.InternationalizationUtils.i18nP import org.briarproject.briar.desktop.utils.KLoggerUtils.e import org.briarproject.briar.desktop.utils.KLoggerUtils.i -object LibnotifyNotificationProvider : VisualNotificationProvider { +object LibnotifyNotificationProvider : AbstractNotificationProvider() { private val LOG = KotlinLogging.logger {} @@ -82,7 +80,7 @@ object LibnotifyNotificationProvider : VisualNotificationProvider { } } - private fun sendNotification(text: String) { + override fun sendNotification(text: String) { if (!available) return /** @@ -131,20 +129,6 @@ object LibnotifyNotificationProvider : VisualNotificationProvider { } } - override fun notifyPrivateMessages(num: Int, contacts: Int) = sendNotification( - if (contacts == 1) - i18nP("notifications.message.private.one_chat", num) - else - i18nF("notifications.message.private.several_chats", num, contacts) - ) - - override fun notifyForumPosts(num: Int, forums: Int) = sendNotification( - if (forums == 1) - i18nP("notifications.message.forum.one_forum", num) - else - i18nF("notifications.message.forum.several_forums", num, forums) - ) - /** * Functions as defined in the source code at * https://www.freedesktop.org/software/gstreamer-sdk/data/docs/latest/glib/glib-GVariant.html diff --git a/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/notification/windows/Toast4jNotificationProvider.kt b/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/notification/windows/Toast4jNotificationProvider.kt index 0690e220208a4d3581ade2bda8a146ad71d14540..3409b0ff981926086f57bc33898ab8c5aba84832 100644 --- a/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/notification/windows/Toast4jNotificationProvider.kt +++ b/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/notification/windows/Toast4jNotificationProvider.kt @@ -23,13 +23,11 @@ import de.mobanisto.toast4j.Toaster import de.mobanisto.wintoast.WinToastTemplate.WinToastTemplateType import mu.KotlinLogging import org.briarproject.briar.desktop.BuildData -import org.briarproject.briar.desktop.notification.VisualNotificationProvider +import org.briarproject.briar.desktop.notification.AbstractNotificationProvider import org.briarproject.briar.desktop.utils.InternationalizationUtils.i18n -import org.briarproject.briar.desktop.utils.InternationalizationUtils.i18nF -import org.briarproject.briar.desktop.utils.InternationalizationUtils.i18nP import org.briarproject.briar.desktop.utils.KLoggerUtils.e -object Toast4jNotificationProvider : VisualNotificationProvider { +object Toast4jNotificationProvider : AbstractNotificationProvider() { private val LOG = KotlinLogging.logger {} @@ -62,27 +60,13 @@ object Toast4jNotificationProvider : VisualNotificationProvider { currentToast?.hide() } - var currentToast: ToastHandle? = null + private var currentToast: ToastHandle? = null - private fun sendNotification(text: String) { + override fun sendNotification(text: String) { currentToast?.hide() currentToast = toaster.showToast( ToastBuilder(WinToastTemplateType.ToastText01).setSilent() .setLine1(text).build() ) } - - override fun notifyPrivateMessages(num: Int, contacts: Int) = sendNotification( - if (contacts == 1) - i18nP("notifications.message.private.one_chat", num) - else - i18nF("notifications.message.private.several_chats", num, contacts) - ) - - override fun notifyForumPosts(num: Int, forums: Int) = sendNotification( - if (forums == 1) - i18nP("notifications.message.forum.one_forum", num) - else - i18nF("notifications.message.forum.several_forums", num, forums) - ) }