From: David ‘Bombe’ Roden Date: Thu, 25 May 2017 11:27:33 +0000 (+0200) Subject: Replace options page with Kotlin version X-Git-Tag: 0.9.7^2~194 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=effe93c96b6555d9e4f5263ce0933daab365706e;p=Sone.git Replace options page with Kotlin version --- diff --git a/src/main/java/net/pterodactylus/sone/web/pages/OptionsPage.java b/src/main/java/net/pterodactylus/sone/web/pages/OptionsPage.java deleted file mode 100644 index 7b65c33..0000000 --- a/src/main/java/net/pterodactylus/sone/web/pages/OptionsPage.java +++ /dev/null @@ -1,166 +0,0 @@ -/* - * Sone - OptionsPage.java - Copyright © 2010–2016 David Roden - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package net.pterodactylus.sone.web.pages; - -import static net.pterodactylus.sone.utils.NumberParsers.parseInt; - -import java.util.ArrayList; -import java.util.List; - -import net.pterodactylus.sone.core.Preferences; -import net.pterodactylus.sone.data.Sone; -import net.pterodactylus.sone.data.SoneOptions.LoadExternalContent; -import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired; -import net.pterodactylus.sone.web.WebInterface; -import net.pterodactylus.sone.web.page.FreenetRequest; -import net.pterodactylus.util.template.Template; -import net.pterodactylus.util.template.TemplateContext; -import net.pterodactylus.util.web.Method; - -/** - * This page lets the user edit the options of the Sone plugin. - * - * @author David ‘Bombe’ Roden - */ -public class OptionsPage extends SoneTemplatePage { - - /** - * Creates a new options page. - * - * @param template - * The template to render - * @param webInterface - * The Sone web interface - */ - public OptionsPage(Template template, WebInterface webInterface) { - super("options.html", template, "Page.Options.Title", webInterface, false); - } - - // - // TEMPLATEPAGE METHODS - // - - /** - * {@inheritDoc} - */ - @Override - protected void handleRequest(FreenetRequest request, TemplateContext templateContext) throws RedirectException { - Preferences preferences = webInterface.getCore().getPreferences(); - Sone currentSone = webInterface.getCurrentSoneWithoutCreatingSession(request.getToadletContext()); - if (request.getMethod() == Method.POST) { - List fieldErrors = new ArrayList(); - if (currentSone != null) { - boolean autoFollow = request.getHttpRequest().isPartSet("auto-follow"); - currentSone.getOptions().setAutoFollow(autoFollow); - boolean enableSoneInsertNotifications = request.getHttpRequest().isPartSet("enable-sone-insert-notifications"); - currentSone.getOptions().setSoneInsertNotificationEnabled(enableSoneInsertNotifications); - boolean showNotificationNewSones = request.getHttpRequest().isPartSet("show-notification-new-sones"); - currentSone.getOptions().setShowNewSoneNotifications(showNotificationNewSones); - boolean showNotificationNewPosts = request.getHttpRequest().isPartSet("show-notification-new-posts"); - currentSone.getOptions().setShowNewPostNotifications(showNotificationNewPosts); - boolean showNotificationNewReplies = request.getHttpRequest().isPartSet("show-notification-new-replies"); - currentSone.getOptions().setShowNewReplyNotifications(showNotificationNewReplies); - String showCustomAvatars = request.getHttpRequest().getPartAsStringFailsafe("show-custom-avatars", 32); - currentSone.getOptions().setShowCustomAvatars(LoadExternalContent.valueOf(showCustomAvatars)); - String loadLinkedImages = request.getHttpRequest().getPartAsStringFailsafe("load-linked-images", 32); - currentSone.getOptions().setLoadLinkedImages(LoadExternalContent.valueOf(loadLinkedImages)); - webInterface.getCore().touchConfiguration(); - } - Integer insertionDelay = parseInt(request.getHttpRequest().getPartAsStringFailsafe("insertion-delay", 16), null); - if (!preferences.validateInsertionDelay(insertionDelay)) { - fieldErrors.add("insertion-delay"); - } else { - preferences.setInsertionDelay(insertionDelay); - } - Integer postsPerPage = parseInt(request.getHttpRequest().getPartAsStringFailsafe("posts-per-page", 4), null); - if (!preferences.validatePostsPerPage(postsPerPage)) { - fieldErrors.add("posts-per-page"); - } else { - preferences.setPostsPerPage(postsPerPage); - } - Integer imagesPerPage = parseInt(request.getHttpRequest().getPartAsStringFailsafe("images-per-page", 4), null); - if (!preferences.validateImagesPerPage(imagesPerPage)) { - fieldErrors.add("images-per-page"); - } else { - preferences.setImagesPerPage(imagesPerPage); - } - Integer charactersPerPost = parseInt(request.getHttpRequest().getPartAsStringFailsafe("characters-per-post", 10), null); - if (!preferences.validateCharactersPerPost(charactersPerPost)) { - fieldErrors.add("characters-per-post"); - } else { - preferences.setCharactersPerPost(charactersPerPost); - } - Integer postCutOffLength = parseInt(request.getHttpRequest().getPartAsStringFailsafe("post-cut-off-length", 10), null); - if (!preferences.validatePostCutOffLength(postCutOffLength)) { - fieldErrors.add("post-cut-off-length"); - } else { - preferences.setPostCutOffLength(postCutOffLength); - } - boolean requireFullAccess = request.getHttpRequest().isPartSet("require-full-access"); - preferences.setRequireFullAccess(requireFullAccess); - Integer positiveTrust = parseInt(request.getHttpRequest().getPartAsStringFailsafe("positive-trust", 3), null); - if (!preferences.validatePositiveTrust(positiveTrust)) { - fieldErrors.add("positive-trust"); - } else { - preferences.setPositiveTrust(positiveTrust); - } - Integer negativeTrust = parseInt(request.getHttpRequest().getPartAsStringFailsafe("negative-trust", 4), null); - if (!preferences.validateNegativeTrust(negativeTrust)) { - fieldErrors.add("negative-trust"); - } else { - preferences.setNegativeTrust(negativeTrust); - } - String trustComment = request.getHttpRequest().getPartAsStringFailsafe("trust-comment", 256); - if (trustComment.trim().length() == 0) { - trustComment = null; - } - preferences.setTrustComment(trustComment); - boolean fcpInterfaceActive = request.getHttpRequest().isPartSet("fcp-interface-active"); - preferences.setFcpInterfaceActive(fcpInterfaceActive); - Integer fcpFullAccessRequiredInteger = parseInt(request.getHttpRequest().getPartAsStringFailsafe("fcp-full-access-required", 1), preferences.getFcpFullAccessRequired().ordinal()); - FullAccessRequired fcpFullAccessRequired = FullAccessRequired.values()[fcpFullAccessRequiredInteger]; - preferences.setFcpFullAccessRequired(fcpFullAccessRequired); - webInterface.getCore().touchConfiguration(); - if (fieldErrors.isEmpty()) { - throw new RedirectException(getPath()); - } - templateContext.set("fieldErrors", fieldErrors); - } - if (currentSone != null) { - templateContext.set("auto-follow", currentSone.getOptions().isAutoFollow()); - templateContext.set("enable-sone-insert-notifications", currentSone.getOptions().isSoneInsertNotificationEnabled()); - templateContext.set("show-notification-new-sones", currentSone.getOptions().isShowNewSoneNotifications()); - templateContext.set("show-notification-new-posts", currentSone.getOptions().isShowNewPostNotifications()); - templateContext.set("show-notification-new-replies", currentSone.getOptions().isShowNewReplyNotifications()); - templateContext.set("show-custom-avatars", currentSone.getOptions().getShowCustomAvatars().name()); - templateContext.set("load-linked-images", currentSone.getOptions().getLoadLinkedImages().name()); - } - templateContext.set("insertion-delay", preferences.getInsertionDelay()); - templateContext.set("posts-per-page", preferences.getPostsPerPage()); - templateContext.set("images-per-page", preferences.getImagesPerPage()); - templateContext.set("characters-per-post", preferences.getCharactersPerPost()); - templateContext.set("post-cut-off-length", preferences.getPostCutOffLength()); - templateContext.set("require-full-access", preferences.isRequireFullAccess()); - templateContext.set("positive-trust", preferences.getPositiveTrust()); - templateContext.set("negative-trust", preferences.getNegativeTrust()); - templateContext.set("trust-comment", preferences.getTrustComment()); - templateContext.set("fcp-interface-active", preferences.isFcpInterfaceActive()); - templateContext.set("fcp-full-access-required", preferences.getFcpFullAccessRequired().ordinal()); - } - -} diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/OptionsPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/OptionsPage.kt new file mode 100644 index 0000000..c8295ac --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/OptionsPage.kt @@ -0,0 +1,104 @@ +package net.pterodactylus.sone.web.pages + +import net.pterodactylus.sone.core.Preferences +import net.pterodactylus.sone.data.SoneOptions.LoadExternalContent +import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired +import net.pterodactylus.sone.utils.emptyToNull +import net.pterodactylus.sone.utils.isPOST +import net.pterodactylus.sone.utils.parameters +import net.pterodactylus.sone.web.WebInterface +import net.pterodactylus.sone.web.page.FreenetRequest +import net.pterodactylus.util.template.Template +import net.pterodactylus.util.template.TemplateContext + +/** + * This page lets the user edit the options of the Sone plugin. + */ +class OptionsPage(template: Template, webInterface: WebInterface): + SoneTemplatePage("options.html", template, "Page.Options.Title", webInterface, false) { + + override fun handleRequest(request: FreenetRequest, templateContext: TemplateContext) { + if (request.isPOST) { + val fieldsWithErrors = mutableListOf() + getCurrentSone(request.toadletContext)?.options?.let { options -> + val autoFollow = "auto-follow" in request.parameters + val loadLinkedImages = request.parameters["load-linked-images"].emptyToNull + val showCustomAvatars = request.parameters["show-custom-avatars"].emptyToNull + val enableSoneInsertNotification = "enable-sone-insert-notifications" in request.parameters + val showNewSoneNotification = "show-notification-new-sones" in request.parameters + val showNewPostNotification = "show-notification-new-posts" in request.parameters + val showNewReplyNotification = "show-notification-new-replies" in request.parameters + + options.isAutoFollow = autoFollow + options.isSoneInsertNotificationEnabled = enableSoneInsertNotification + options.isShowNewSoneNotifications = showNewSoneNotification + options.isShowNewPostNotifications = showNewPostNotification + options.isShowNewReplyNotifications = showNewReplyNotification + loadLinkedImages?.also { if (cantSetOption { options.loadLinkedImages = LoadExternalContent.valueOf(loadLinkedImages) }) fieldsWithErrors += "load-linked-images" } + showCustomAvatars?.also { if (cantSetOption { options.showCustomAvatars = LoadExternalContent.valueOf(showCustomAvatars) }) fieldsWithErrors += "show-custom-avatars" } + } + val fullAccessRequired = "require-full-access" in request.parameters + val fcpInterfaceActive = "fcp-interface-active" in request.parameters + + webInterface.core.preferences.isRequireFullAccess = fullAccessRequired + webInterface.core.preferences.isFcpInterfaceActive = fcpInterfaceActive + + val postsPerPage = request.parameters["posts-per-page"]?.toIntOrNull() + val charactersPerPost = request.parameters["characters-per-post"]?.toIntOrNull() + val postCutOffLength = request.parameters["post-cut-off-length"]?.toIntOrNull() + val imagesPerPage = request.parameters["images-per-page"]?.toIntOrNull() + val insertionDelay = request.parameters["insertion-delay"]?.toIntOrNull() + val fcpFullAccessRequired = request.parameters["fcp-full-access-required"]?.toIntOrNull() + val negativeTrust = request.parameters["negative-trust"]?.toIntOrNull() + val positiveTrust = request.parameters["positive-trust"]?.toIntOrNull() + val trustComment = request.parameters["trust-comment"]?.emptyToNull + + if (cantSetOption { it.setPostsPerPage(postsPerPage) }) fieldsWithErrors += "posts-per-page" + if (cantSetOption { it.setCharactersPerPost(charactersPerPost) }) fieldsWithErrors += "characters-per-post" + if (cantSetOption { it.setPostCutOffLength(postCutOffLength) }) fieldsWithErrors += "post-cut-off-length" + if (cantSetOption { it.setImagesPerPage(imagesPerPage) }) fieldsWithErrors += "images-per-page" + if (cantSetOption { it.setInsertionDelay(insertionDelay) }) fieldsWithErrors += "insertion-delay" + fcpFullAccessRequired?.also { if (cantSetOption { it.fcpFullAccessRequired = FullAccessRequired.values()[fcpFullAccessRequired] }) fieldsWithErrors += "fcp-full-access-required" } + if (cantSetOption { it.setNegativeTrust(negativeTrust) }) fieldsWithErrors += "negative-trust" + if (cantSetOption { it.setPositiveTrust(positiveTrust) }) fieldsWithErrors += "positive-trust" + if (cantSetOption { it.trustComment = trustComment }) fieldsWithErrors += "trust-comment" + + if (fieldsWithErrors.isEmpty()) { + webInterface.core.touchConfiguration() + throw RedirectException("options.html") + } + templateContext["fieldErrors"] = fieldsWithErrors + } + getCurrentSone(request.toadletContext)?.options?.let { options -> + templateContext["auto-follow"] = options.isAutoFollow + templateContext["show-notification-new-sones"] = options.isShowNewSoneNotifications + templateContext["show-notification-new-posts"] = options.isShowNewPostNotifications + templateContext["show-notification-new-replies"] = options.isShowNewReplyNotifications + templateContext["enable-sone-insert-notifications"] = options.isSoneInsertNotificationEnabled + templateContext["load-linked-images"] = options.loadLinkedImages.toString() + templateContext["show-custom-avatars"] = options.showCustomAvatars.toString() + } + webInterface.core.preferences.let { preferences -> + templateContext["insertion-delay"] = preferences.insertionDelay + templateContext["characters-per-post"] = preferences.charactersPerPost + templateContext["fcp-full-access-required"] = preferences.fcpFullAccessRequired.ordinal + templateContext["images-per-page"] = preferences.imagesPerPage + templateContext["fcp-interface-active"] = preferences.isFcpInterfaceActive + templateContext["require-full-access"] = preferences.isRequireFullAccess + templateContext["negative-trust"] = preferences.negativeTrust + templateContext["positive-trust"] = preferences.positiveTrust + templateContext["post-cut-off-length"] = preferences.postCutOffLength + templateContext["posts-per-page"] = preferences.postsPerPage + templateContext["trust-comment"] = preferences.trustComment + } + } + + private fun cantSetOption(setter: (Preferences) -> Unit) = + try { + setter(webInterface.core.preferences) + false + } catch (iae: IllegalArgumentException) { + true + } + +}