🔀 Merge 'feature/sone-locked-notification' into 'next'
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / main / SoneModule.kt
1 package net.pterodactylus.sone.main
2
3 import com.codahale.metrics.*
4 import com.google.common.base.*
5 import com.google.common.eventbus.*
6 import com.google.inject.*
7 import com.google.inject.matcher.*
8 import com.google.inject.name.Names.*
9 import com.google.inject.spi.*
10 import freenet.l10n.*
11 import net.pterodactylus.sone.database.*
12 import net.pterodactylus.sone.database.memory.*
13 import net.pterodactylus.sone.freenet.*
14 import net.pterodactylus.sone.freenet.wot.*
15 import net.pterodactylus.util.config.*
16 import net.pterodactylus.util.config.ConfigurationException
17 import net.pterodactylus.util.version.Version
18 import java.io.*
19
20 open class SoneModule(private val sonePlugin: SonePlugin, private val eventBus: EventBus) : AbstractModule() {
21
22         override fun configure() {
23                 val sonePropertiesFile = File("sone.properties")
24                 val firstStart = !sonePropertiesFile.exists()
25                 var newConfig = false
26                 val configuration = try {
27                         Configuration(MapConfigurationBackend(sonePropertiesFile, false))
28                 } catch (ce: ConfigurationException) {
29                         sonePropertiesFile.delete()
30                         newConfig = true
31                         Configuration(MapConfigurationBackend(sonePropertiesFile, true))
32                 }
33                 val context = Context("Sone")
34                 val loaders = configuration.getStringValue("Developer.LoadFromFilesystem")
35                                 .getValue(null)
36                                 ?.let {
37                                         configuration.getStringValue("Developer.FilesystemPath")
38                                                         .getValue(null)
39                                                         ?.let { DebugLoaders(it) }
40                                 }
41
42                 bind(Configuration::class.java).toInstance(configuration)
43                 bind(EventBus::class.java).toInstance(eventBus)
44                 bind(Boolean::class.java).annotatedWith(named("FirstStart")).toInstance(firstStart)
45                 bind(Boolean::class.java).annotatedWith(named("NewConfig")).toInstance(newConfig)
46                 bind(Context::class.java).toInstance(context)
47                 bind(object : TypeLiteral<Optional<Context>>() {}).toInstance(Optional.of(context))
48                 bind(SonePlugin::class.java).toInstance(sonePlugin)
49                 bind(Version::class.java).toInstance(sonePlugin.version.drop(1).parseVersion())
50                 bind(PluginVersion::class.java).toInstance(PluginVersion(sonePlugin.version))
51                 bind(PluginYear::class.java).toInstance(PluginYear(sonePlugin.year))
52                 bind(PluginHomepage::class.java).toInstance(PluginHomepage(sonePlugin.homepage))
53                 bind(Database::class.java).to(MemoryDatabase::class.java).`in`(Singleton::class.java)
54                 bind(Translation::class.java).toInstance(BaseL10nTranslation(sonePlugin.l10n().base))
55                 loaders?.let { bind(Loaders::class.java).toInstance(it) }
56                 bind(MetricRegistry::class.java).`in`(Singleton::class.java)
57                 bind(WebOfTrustConnector::class.java).to(PluginWebOfTrustConnector::class.java).`in`(Singleton::class.java)
58
59                 bindListener(Matchers.any(), object : TypeListener {
60                         override fun <I> hear(typeLiteral: TypeLiteral<I>, typeEncounter: TypeEncounter<I>) {
61                                 typeEncounter.register(InjectionListener { injectee -> eventBus.register(injectee) })
62                         }
63                 })
64         }
65
66 }
67
68 private fun String.parseVersion(): Version = Version.parse(this)