Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

CommandLineOptionsModule.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (c) 2006-2013 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.commandline;
  23. import java.io.File;
  24. import javax.inject.Qualifier;
  25. import javax.inject.Singleton;
  26. import dagger.Module;
  27. import dagger.Provides;
  28. /**
  29. * Provides options based on the command line.
  30. */
  31. @Module(library = true, complete = false)
  32. public class CommandLineOptionsModule {
  33. /**
  34. * Enumeration of directory types supported by the client.
  35. */
  36. public enum DirectoryType {
  37. /** The base directory, where everything else lives. */
  38. BASE,
  39. /** The directory that identities are stored in. */
  40. IDENTITIES,
  41. /** The directory that plugins are stored in. */
  42. PLUGINS,
  43. /** The directory that themes are stored in. */
  44. THEMES;
  45. }
  46. /**
  47. * Provides a mean of identifying the type of directory a class wants
  48. * injected.
  49. */
  50. @Qualifier
  51. public @interface Directory {
  52. DirectoryType value();
  53. }
  54. /**
  55. * Provides the base directory that all DMDirc user data is stored in.
  56. *
  57. * @param parser The parser to get the user-supplied directory from.
  58. * @return The base directory.
  59. */
  60. @Provides
  61. @Singleton
  62. @Directory(DirectoryType.BASE)
  63. public String getBaseDirectory(final CommandLineParser parser) {
  64. if (parser.getConfigDirectory() == null) {
  65. return getDefaultBaseDirectory();
  66. } else {
  67. return parser.getConfigDirectory();
  68. }
  69. }
  70. /**
  71. * Provides the path to the plugins directory.
  72. *
  73. * @param baseDirectory The base DMDirc directory.
  74. * @return The plugin directory.
  75. */
  76. @Provides
  77. @Singleton
  78. @Directory(DirectoryType.PLUGINS)
  79. public String getPluginsDirectory(final @Directory(DirectoryType.BASE) String baseDirectory) {
  80. return baseDirectory + "plugins" + File.separator;
  81. }
  82. /**
  83. * Provides the path to the themes directory.
  84. *
  85. * @param baseDirectory The base DMDirc directory.
  86. * @return The themes directory.
  87. */
  88. @Provides
  89. @Singleton
  90. @Directory(DirectoryType.THEMES)
  91. public String getThemesDirectory(final @Directory(DirectoryType.BASE) String baseDirectory) {
  92. return baseDirectory + "themes" + File.separator;
  93. }
  94. /**
  95. * Initialises the location of the configuration directory.
  96. */
  97. private String getDefaultBaseDirectory() {
  98. final String fs = System.getProperty("file.separator");
  99. final String osName = System.getProperty("os.name");
  100. String configdir;
  101. if (System.getenv("DMDIRC_HOME") != null) {
  102. configdir = System.getenv("DMDIRC_HOME");
  103. } else if (osName.startsWith("Mac OS")) {
  104. configdir = System.getProperty("user.home") + fs + "Library"
  105. + fs + "Preferences" + fs + "DMDirc" + fs;
  106. } else if (osName.startsWith("Windows")) {
  107. if (System.getenv("APPDATA") == null) {
  108. configdir = System.getProperty("user.home") + fs + "DMDirc" + fs;
  109. } else {
  110. configdir = System.getenv("APPDATA") + fs + "DMDirc" + fs;
  111. }
  112. } else {
  113. configdir = System.getProperty("user.home") + fs + ".DMDirc" + fs;
  114. final File testFile = new File(configdir);
  115. if (!testFile.exists()) {
  116. final String configHome = System.getenv("XDG_CONFIG_HOME");
  117. configdir = (configHome == null || configHome.isEmpty())
  118. ? System.getProperty("user.home") + fs + ".config" + fs
  119. : configHome;
  120. configdir += fs + "DMDirc" + fs;
  121. }
  122. }
  123. return new File(configdir).getAbsolutePath() + fs;
  124. }
  125. }