You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CommandLineOptionsModule.java 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.commandline;
  18. import java.io.File;
  19. import java.nio.file.Path;
  20. import javax.inject.Qualifier;
  21. import javax.inject.Singleton;
  22. import dagger.Module;
  23. import dagger.Provides;
  24. /**
  25. * Provides options based on the command line.
  26. */
  27. @Module(library = true, complete = false)
  28. public class CommandLineOptionsModule {
  29. /**
  30. * Enumeration of directory types supported by the client.
  31. */
  32. public static final class DirectoryType {
  33. /**
  34. * The base directory, where everything else lives.
  35. */
  36. public static final String BASE = "base";
  37. /**
  38. * The directory that identities are stored in.
  39. */
  40. public static final String IDENTITIES = "identities";
  41. /**
  42. * The directory that plugins are stored in.
  43. */
  44. public static final String PLUGINS = "plugins";
  45. /**
  46. * The directory that themes are stored in.
  47. */
  48. public static final String THEMES = "themes";
  49. /**
  50. * The directory that actions are stored in.
  51. */
  52. public static final String ACTIONS = "actions";
  53. /**
  54. * The directory that error reports are stored in.
  55. */
  56. public static final String ERRORS = "errors";
  57. /**
  58. * The directory to use for temporary files (downloads in flight, caches, etc).
  59. */
  60. public static final String TEMPORARY = "temp";
  61. private DirectoryType() {
  62. }
  63. }
  64. /**
  65. * Provides a mean of identifying the type of directory a class wants injected.
  66. */
  67. @Qualifier
  68. public @interface Directory {
  69. String value();
  70. }
  71. /**
  72. * Provides the base directory that all DMDirc user data is stored in.
  73. *
  74. * @param parser The parser to get the user-supplied directory from.
  75. * @param locator Base directory locator to find default config directory.
  76. *
  77. * @return The base directory.
  78. */
  79. @Provides
  80. @Singleton
  81. @Directory(DirectoryType.BASE)
  82. public String getBaseDirectory(final CommandLineParser parser,
  83. final BaseDirectoryLocator locator) {
  84. if (parser.getConfigDirectory() == null) {
  85. return locator.getDefaultBaseDirectory();
  86. } else {
  87. return parser.getConfigDirectory();
  88. }
  89. }
  90. /**
  91. * Provides the path to the plugins directory.
  92. *
  93. * @param baseDirectory The base DMDirc directory.
  94. *
  95. * @return The plugin directory.
  96. */
  97. @Provides
  98. @Singleton
  99. @Directory(DirectoryType.PLUGINS)
  100. public String getPluginsDirectory(@Directory(DirectoryType.BASE) final String baseDirectory) {
  101. return baseDirectory + "plugins" + File.separator;
  102. }
  103. /**
  104. * Provides the path to the actions directory.
  105. *
  106. * @param baseDirectory The base DMDirc directory.
  107. *
  108. * @return The actions directory.
  109. */
  110. @Provides
  111. @Singleton
  112. @Directory(DirectoryType.ACTIONS)
  113. public String getActionsDirectory(@Directory(DirectoryType.BASE) final String baseDirectory) {
  114. return baseDirectory + "actions" + File.separator;
  115. }
  116. /**
  117. * Provides the path to the identities directory.
  118. *
  119. * @param baseDirectory The base DMDirc directory.
  120. *
  121. * @return The identities directory.
  122. */
  123. @Provides
  124. @Singleton
  125. @Directory(DirectoryType.IDENTITIES)
  126. public String getIdentitiesDirectory(@Directory(DirectoryType.BASE) final String baseDirectory) {
  127. return baseDirectory + "identities" + File.separator;
  128. }
  129. /**
  130. * Provides the path to the errors directory.
  131. *
  132. * @param baseDirectory The base DMDirc directory.
  133. *
  134. * @return The identities directory.
  135. */
  136. @Provides
  137. @Singleton
  138. @Directory(DirectoryType.ERRORS)
  139. public String getErrorsDirectory(@Directory(DirectoryType.BASE) final String baseDirectory) {
  140. return baseDirectory + "errors" + File.separator;
  141. }
  142. /**
  143. * Provides the path to the themes directory.
  144. *
  145. * @param baseDirectory The base DMDirc directory.
  146. *
  147. * @return The themes directory.
  148. */
  149. @Provides
  150. @Singleton
  151. @Directory(DirectoryType.THEMES)
  152. public String getThemesDirectory(@Directory(DirectoryType.BASE) final String baseDirectory) {
  153. return baseDirectory + "themes" + File.separator;
  154. }
  155. /**
  156. * Provides the path to a DMDirc pseudo-temporary directory. This is somewhere the client can
  157. * use to store limited-use files such as downloads not yet installed, or cached update
  158. * information. It is not automatically purged - items placed there must be cleaned up
  159. * explicitly.
  160. *
  161. * @param baseDirectory The base DMDirc directory.
  162. *
  163. * @return The temporary directory.
  164. */
  165. @Provides
  166. @Singleton
  167. @Directory(DirectoryType.TEMPORARY)
  168. public String getTempDirectory(@Directory(DirectoryType.BASE) final String baseDirectory) {
  169. return baseDirectory;
  170. }
  171. @Provides
  172. @Singleton
  173. @Directory(DirectoryType.TEMPORARY)
  174. public Path getTempPath(@Directory(DirectoryType.BASE) final Path baseDirectory) {
  175. return baseDirectory;
  176. }
  177. @Provides
  178. @Singleton
  179. @Directory(DirectoryType.BASE)
  180. public Path getBasePath(@Directory(DirectoryType.BASE) final String directory) {
  181. return new File(directory).toPath();
  182. }
  183. @Provides
  184. @Singleton
  185. @Directory(DirectoryType.PLUGINS)
  186. public Path getPluginsPath(@Directory(DirectoryType.PLUGINS) final String directory) {
  187. return new File(directory).toPath();
  188. }
  189. @Provides
  190. @Singleton
  191. @Directory(DirectoryType.ACTIONS)
  192. public Path getActionsPath(@Directory(DirectoryType.ACTIONS) final String directory) {
  193. return new File(directory).toPath();
  194. }
  195. @Provides
  196. @Singleton
  197. @Directory(DirectoryType.IDENTITIES)
  198. public Path getIdentitiesPath(@Directory(DirectoryType.IDENTITIES) final String directory) {
  199. return new File(directory).toPath();
  200. }
  201. @Provides
  202. @Singleton
  203. @Directory(DirectoryType.ERRORS)
  204. public Path getErrorsPath(@Directory(DirectoryType.ERRORS) final String directory) {
  205. return new File(directory).toPath();
  206. }
  207. @Provides
  208. @Singleton
  209. @Directory(DirectoryType.THEMES)
  210. public Path getThemesPath(@Directory(DirectoryType.THEMES) final String directory) {
  211. return new File(directory).toPath();
  212. }
  213. }