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.

Main.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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;
  23. import com.dmdirc.actions.ActionManager;
  24. import com.dmdirc.actions.CoreActionType;
  25. import com.dmdirc.commandline.CommandLineParser;
  26. import com.dmdirc.commandparser.CommandManager;
  27. import com.dmdirc.config.IdentityManager;
  28. import com.dmdirc.logger.DMDircExceptionHandler;
  29. import com.dmdirc.plugins.PluginManager;
  30. import com.dmdirc.themes.ThemeManager;
  31. import com.dmdirc.ui.dummy.DummyController;
  32. import com.dmdirc.ui.swing.SwingController;
  33. import com.dmdirc.ui.interfaces.UIController;
  34. import com.dmdirc.updater.UpdateChannel;
  35. import com.dmdirc.updater.UpdateChecker;
  36. import java.awt.GraphicsEnvironment;
  37. /**
  38. * Main class, handles initialisation.
  39. *
  40. * @author chris
  41. */
  42. public final class Main {
  43. /**
  44. * Stores the current textual program version.
  45. */
  46. public static final String VERSION = "0.5.1";
  47. /**
  48. * Stores the release date of this version.
  49. */
  50. public static final int RELEASE_DATE = 20071007;
  51. /**
  52. * Stores the update channel that this version came from, if any.
  53. */
  54. public static final UpdateChannel UPDATE_CHANNEL = UpdateChannel.STABLE;
  55. /**
  56. * A revision number for actions and core plugins. If this is increased,
  57. * users will be prompted to re-extract them.
  58. */
  59. private static final int ADDON_REVISION = 4;
  60. /**
  61. * The UI to use for the client.
  62. */
  63. private static UIController controller;
  64. /**
  65. * The config dir to use for the client.
  66. */
  67. private static String configdir;
  68. /**
  69. * Prevents creation of main.
  70. */
  71. private Main() {
  72. }
  73. /**
  74. * Entry procedure.
  75. *
  76. * @param args the command line arguments
  77. */
  78. public static void main(final String[] args) {
  79. Thread.setDefaultUncaughtExceptionHandler(new DMDircExceptionHandler());
  80. final CommandLineParser clp = new CommandLineParser(args);
  81. IdentityManager.load();
  82. clp.applySettings();
  83. CommandManager.initCommands();
  84. getUI().initUISettings();
  85. ActionManager.init();
  86. PluginManager.getPluginManager();
  87. ActionManager.loadActions();
  88. new ThemeManager().loadDefaultTheme();
  89. getUI().getMainWindow();
  90. if (!IdentityManager.getGlobalConfig().hasOption("general", "firstRun")
  91. || IdentityManager.getGlobalConfig().getOptionBool("general", "firstRun")) {
  92. IdentityManager.getConfigIdentity().setOption("general", "firstRun", "false");
  93. IdentityManager.getConfigIdentity().setOption("general", "addonrevision", ADDON_REVISION);
  94. getUI().showFirstRunWizard();
  95. } else if (IdentityManager.getGlobalConfig().getOptionInt("general", "addonrevision", -1) < ADDON_REVISION) {
  96. IdentityManager.getConfigIdentity().setOption("general", "addonrevision", ADDON_REVISION);
  97. getUI().showMigrationWizard();
  98. }
  99. ActionManager.processEvent(CoreActionType.CLIENT_OPENED, null);
  100. UpdateChecker.init();
  101. clp.processArguments();
  102. if (IdentityManager.getGlobalConfig().getOptionBool("general", "showglobalwindow")) {
  103. new GlobalWindow();
  104. }
  105. Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
  106. public void run() {
  107. ActionManager.processEvent(CoreActionType.CLIENT_CLOSED, null);
  108. ServerManager.getServerManager().disconnectAll("Unexpected shutdown");
  109. IdentityManager.save();
  110. }
  111. }, "Shutdown thread"));
  112. }
  113. /**
  114. * Quits the client nicely, with the default closing message.
  115. */
  116. public static void quit() {
  117. quit(IdentityManager.getGlobalConfig().getOption("general", "closemessage"));
  118. }
  119. /**
  120. * Quits the client nicely.
  121. *
  122. * @param reason The quit reason to send
  123. */
  124. public static void quit(final String reason) {
  125. ServerManager.getServerManager().disconnectAll(reason);
  126. System.exit(0);
  127. }
  128. /**
  129. * Retrieves the UI controller that's being used by the client.
  130. *
  131. * @return The client's UI controller
  132. */
  133. public static UIController getUI() {
  134. if (controller == null) {
  135. if (GraphicsEnvironment.isHeadless()) {
  136. controller = new DummyController();
  137. } else {
  138. controller = new SwingController();
  139. }
  140. }
  141. return controller;
  142. }
  143. /**
  144. * Sets the UI controller that should be used by this client.
  145. *
  146. * @param newController The new UI Controller
  147. */
  148. public static void setUI(final UIController newController) {
  149. controller = newController;
  150. }
  151. /**
  152. * Returns the application's config directory.
  153. *
  154. * @return configuration directory
  155. */
  156. public static String getConfigDir() {
  157. if (configdir == null) {
  158. final String fs = System.getProperty("file.separator");
  159. final String osName = System.getProperty("os.name");
  160. if (osName.startsWith("Mac OS")) {
  161. configdir = System.getProperty("user.home") + fs + "Library" + fs + "Preferences" + fs + "DMDirc" + fs;
  162. } else if (osName.startsWith("Windows")) {
  163. if (System.getenv("APPDATA") == null) {
  164. configdir = System.getProperty("user.home") + fs + "DMDirc" + fs;
  165. } else {
  166. configdir = System.getenv("APPDATA") + fs + "DMDirc" + fs;
  167. }
  168. } else {
  169. configdir = System.getProperty("user.home") + fs + ".DMDirc" + fs;
  170. }
  171. }
  172. return configdir;
  173. }
  174. /**
  175. * Sets the config directory for this client.
  176. *
  177. * @param newdir The new configuration directory
  178. */
  179. public static void setConfigDir(final String newdir) {
  180. configdir = newdir;
  181. }
  182. }