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 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (c) 2006-2008 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. import java.util.Timer;
  38. import java.util.TimerTask;
  39. /**
  40. * Main class, handles initialisation.
  41. *
  42. * @author chris
  43. */
  44. public final class Main {
  45. /** Stores the current textual program version. */
  46. public static final String VERSION = "SVN";
  47. /** The SVN revision that this build was made from. */
  48. public static final int SVN_REVISION = 3628; // 3601;
  49. /** Stores the update channel that this version came from, if any. */
  50. public static final UpdateChannel UPDATE_CHANNEL = UpdateChannel.NONE;
  51. /** Feedback nag delay. */
  52. private static final int FEEDBACK_DELAY = 30 * 60 * 1000;
  53. /** The UI to use for the client. */
  54. private static UIController controller;
  55. /** The config dir to use for the client. */
  56. private static String configdir;
  57. /**
  58. * Prevents creation of main.
  59. */
  60. private Main() {
  61. }
  62. /**
  63. * Entry procedure.
  64. *
  65. * @param args the command line arguments
  66. */
  67. public static void main(final String[] args) {
  68. Thread.setDefaultUncaughtExceptionHandler(new DMDircExceptionHandler());
  69. final CommandLineParser clp = new CommandLineParser(args);
  70. IdentityManager.load();
  71. clp.applySettings();
  72. CommandManager.initCommands();
  73. getUI().initUISettings();
  74. if (IdentityManager.getGlobalConfig().getOptionBool("general", "firstRun", true)) {
  75. IdentityManager.getConfigIdentity().setOption("general", "firstRun", "false");
  76. getUI().showFirstRunWizard();
  77. new Timer().schedule(new TimerTask(){
  78. /** {@inheritDoc} */
  79. @Override
  80. public void run() {
  81. getUI().showFeedbackNag();
  82. }
  83. }, FEEDBACK_DELAY);
  84. } else if (IdentityManager.getGlobalConfig().hasOption("general", "addonrevision")) {
  85. // @Deprecated - can be removed after 0.6 is released
  86. IdentityManager.getConfigIdentity().unsetOption("general", "addonrevision");
  87. getUI().showMigrationWizard();
  88. }
  89. ActionManager.init();
  90. PluginManager.getPluginManager();
  91. ActionManager.loadActions();
  92. new ThemeManager().loadDefaultTheme();
  93. getUI().getMainWindow();
  94. ActionManager.processEvent(CoreActionType.CLIENT_OPENED, null);
  95. UpdateChecker.init();
  96. clp.processArguments();
  97. if (IdentityManager.getGlobalConfig().getOptionBool("general", "showglobalwindow", false)) {
  98. new GlobalWindow();
  99. }
  100. Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
  101. public void run() {
  102. ActionManager.processEvent(CoreActionType.CLIENT_CLOSED, null);
  103. ServerManager.getServerManager().disconnectAll("Unexpected shutdown");
  104. IdentityManager.save();
  105. }
  106. }, "Shutdown thread"));
  107. }
  108. /**
  109. * Quits the client nicely, with the default closing message.
  110. */
  111. public static void quit() {
  112. quit(IdentityManager.getGlobalConfig().getOption("general", "closemessage"));
  113. }
  114. /**
  115. * Quits the client nicely.
  116. *
  117. * @param reason The quit reason to send
  118. */
  119. public static void quit(final String reason) {
  120. ServerManager.getServerManager().disconnectAll(reason);
  121. System.exit(0);
  122. }
  123. /**
  124. * Retrieves the UI controller that's being used by the client.
  125. *
  126. * @return The client's UI controller
  127. */
  128. public static UIController getUI() {
  129. if (controller == null) {
  130. if (GraphicsEnvironment.isHeadless()) {
  131. controller = new DummyController();
  132. } else {
  133. controller = new SwingController();
  134. }
  135. }
  136. return controller;
  137. }
  138. /**
  139. * Sets the UI controller that should be used by this client.
  140. *
  141. * @param newController The new UI Controller
  142. */
  143. public static void setUI(final UIController newController) {
  144. controller = newController;
  145. }
  146. /**
  147. * Returns the application's config directory.
  148. *
  149. * @return configuration directory
  150. */
  151. public static String getConfigDir() {
  152. if (configdir == null) {
  153. final String fs = System.getProperty("file.separator");
  154. final String osName = System.getProperty("os.name");
  155. if (osName.startsWith("Mac OS")) {
  156. configdir = System.getProperty("user.home") + fs + "Library" + fs + "Preferences" + fs + "DMDirc" + fs;
  157. } else if (osName.startsWith("Windows")) {
  158. if (System.getenv("APPDATA") == null) {
  159. configdir = System.getProperty("user.home") + fs + "DMDirc" + fs;
  160. } else {
  161. configdir = System.getenv("APPDATA") + fs + "DMDirc" + fs;
  162. }
  163. } else {
  164. configdir = System.getProperty("user.home") + fs + ".DMDirc" + fs;
  165. }
  166. }
  167. return configdir;
  168. }
  169. /**
  170. * Sets the config directory for this client.
  171. *
  172. * @param newdir The new configuration directory
  173. */
  174. public static void setConfigDir(final String newdir) {
  175. configdir = newdir;
  176. }
  177. }