🔊 Add even more logging
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / PageToadletRegistry.kt
1 package net.pterodactylus.sone.web
2
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.*
8 import javax.inject.*
9
10 private const val soneMenu = "Navigation.Menu.Sone"
11 private const val soneMenuName = "$soneMenu.Name"
12
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
18 ) {
19
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)
25
26         fun addPage(page: Page<FreenetRequest>) {
27                 if (registered.get()) throw IllegalStateException()
28                 pages += page
29         }
30
31         fun addDebugPage(page: Page<FreenetRequest>) {
32                 if (registered.get()) throw IllegalStateException()
33                 debugPages += page
34         }
35
36         fun registerToadlets() {
37                 registered.set(true)
38                 pageMaker.addNavigationCategory("/Sone/index.html", soneMenuName, "$soneMenu.Tooltip", sonePlugin)
39                 addPages()
40         }
41
42         private fun addPages(pages: List<Page<FreenetRequest>> = this.pages) =
43                         pages
44                                         .map { pageToadletFactory.createPageToadlet(it) }
45                                         .onEach(registeredToadlets::plusAssign)
46                                         .forEach { pageToadlet ->
47                                                 if (pageToadlet.menuName == null) {
48                                                         registerToadletWithoutMenuname(pageToadlet)
49                                                 } else {
50                                                         registerToadletWithMenuname(pageToadlet)
51                                                 }
52                                         }
53
54         private fun registerToadletWithoutMenuname(pageToadlet: PageToadlet) =
55                         toadletContainer.register(pageToadlet, null, pageToadlet.path(), true, false)
56
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)
59
60         fun unregisterToadlets() {
61                 pageMaker.removeNavigationCategory(soneMenuName)
62                 registeredToadlets.forEach(toadletContainer::unregister)
63         }
64
65         fun activateDebugMode() {
66                 if (!debugActivated.get()) {
67                         addPages(debugPages)
68                         debugActivated.set(true)
69                 }
70         }
71
72 }