1 package net.pterodactylus.sone.web
3 import freenet.clients.http.*
4 import net.pterodactylus.sone.main.*
5 import net.pterodactylus.sone.web.page.*
6 import net.pterodactylus.util.web.*
7 import java.util.concurrent.atomic.*
10 private const val soneMenu = "Navigation.Menu.Sone"
11 private const val soneMenuName = "$soneMenu.Name"
13 class PageToadletRegistry @Inject constructor(
14 private val pageMaker: PageMaker,
15 private val toadletContainer: ToadletContainer,
16 private val sonePlugin: SonePlugin,
17 private val pageToadletFactory: PageToadletFactory
20 private val pages = mutableListOf<Page<FreenetRequest>>()
21 private val debugPages = mutableListOf<Page<FreenetRequest>>()
22 private val registeredToadlets = mutableListOf<PageToadlet>()
23 private val registered = AtomicBoolean(false)
24 private val debugActivated = AtomicBoolean(false)
26 fun addPage(page: Page<FreenetRequest>) {
27 if (registered.get()) throw IllegalStateException()
31 fun addDebugPage(page: Page<FreenetRequest>) {
32 if (registered.get()) throw IllegalStateException()
36 fun registerToadlets() {
38 pageMaker.addNavigationCategory("/Sone/index.html", soneMenuName, "$soneMenu.Tooltip", sonePlugin)
42 private fun addPages(pages: List<Page<FreenetRequest>> = this.pages) =
44 .map { pageToadletFactory.createPageToadlet(it) }
45 .onEach(registeredToadlets::plusAssign)
46 .forEach { pageToadlet ->
47 if (pageToadlet.menuName == null) {
48 registerToadletWithoutMenuname(pageToadlet)
50 registerToadletWithMenuname(pageToadlet)
54 private fun registerToadletWithoutMenuname(pageToadlet: PageToadlet) =
55 toadletContainer.register(pageToadlet, null, pageToadlet.path(), true, false)
57 private fun registerToadletWithMenuname(pageToadlet: PageToadlet) =
58 toadletContainer.register(pageToadlet, soneMenuName, pageToadlet.path(), true, "$soneMenu.Item.${pageToadlet.menuName}.Name", "$soneMenu.Item.${pageToadlet.menuName}.Tooltip", false, pageToadlet)
60 fun unregisterToadlets() {
61 pageMaker.removeNavigationCategory(soneMenuName)
62 registeredToadlets.forEach(toadletContainer::unregister)
65 fun activateDebugMode() {
66 if (!debugActivated.get()) {
68 debugActivated.set(true)