Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

Main.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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;
  23. import com.dmdirc.GlobalWindow.GlobalWindowManager;
  24. import com.dmdirc.actions.ActionManager;
  25. import com.dmdirc.actions.CoreActionType;
  26. import com.dmdirc.commandline.CommandLineParser;
  27. import com.dmdirc.commandparser.CommandManager;
  28. import com.dmdirc.config.IdentityManager;
  29. import com.dmdirc.interfaces.CommandController.CommandDetails;
  30. import com.dmdirc.interfaces.ui.UIController;
  31. import com.dmdirc.logger.DMDircExceptionHandler;
  32. import com.dmdirc.logger.ErrorLevel;
  33. import com.dmdirc.logger.Logger;
  34. import com.dmdirc.messages.MessageSinkManager;
  35. import com.dmdirc.plugins.PluginManager;
  36. import com.dmdirc.plugins.Service;
  37. import com.dmdirc.plugins.ServiceProvider;
  38. import com.dmdirc.ui.WarningDialog;
  39. import com.dmdirc.ui.themes.ThemeManager;
  40. import com.dmdirc.util.URLBuilder;
  41. import java.awt.GraphicsEnvironment;
  42. import java.util.Collection;
  43. import java.util.HashSet;
  44. import java.util.List;
  45. import java.util.Set;
  46. import java.util.Timer;
  47. import java.util.TimerTask;
  48. import javax.inject.Inject;
  49. import dagger.ObjectGraph;
  50. /**
  51. * Main class, handles initialisation.
  52. */
  53. public class Main {
  54. /** Feedback nag delay. */
  55. private final int FEEDBACK_DELAY = 30 * 60 * 1000;
  56. /** The UI to use for the client. */
  57. private final Collection<UIController> CONTROLLERS = new HashSet<>();
  58. /** The identity manager the client will use. */
  59. private final IdentityManager identityManager;
  60. /** The server manager the client will use. */
  61. private final ServerManager serverManager;
  62. /** The action manager the client will use. */
  63. private final ActionManager actionManager;
  64. /** The command-line parser used for this instance. */
  65. private final CommandLineParser commandLineParser;
  66. /** The plugin manager the client will use. */
  67. private final PluginManager pluginManager;
  68. /** The extractor to use for core plugins. */
  69. private final CorePluginExtractor corePluginExtractor;
  70. /** The command manager to use. */
  71. private final CommandManager commandManager;
  72. /** The global window manager to use. */
  73. private final GlobalWindowManager globalWindowManager;
  74. /** The commands to load into the command manager. */
  75. private final Set<CommandDetails> commands;
  76. /**
  77. * Creates a new instance of {@link Main}.
  78. *
  79. * @param identityManager The identity manager the client will use.
  80. * @param serverManager The server manager the client will use.
  81. * @param actionManager The action manager the client will use.
  82. * @param commandLineParser The command-line parser used for this instance.
  83. * @param pluginManager The plugin manager the client will use.
  84. * @param commandManager The command manager the client will use.
  85. * @param messageSinkManager Unused for now - TODO: remove me when it's injected somewhere sensible.
  86. * @param themeManager Unused for now - TODO: remove me when it's injected somewhere sensible.
  87. * @param corePluginExtractor Extractor to use for core plugins.
  88. * @param urlBuilder URL builder to use as a singleton.
  89. * @param globalWindowManager Global window manager to use.
  90. * @param commands The commands to be loaded into the command manager.
  91. */
  92. @Inject
  93. public Main(
  94. final IdentityManager identityManager,
  95. final ServerManager serverManager,
  96. final ActionManager actionManager,
  97. final CommandLineParser commandLineParser,
  98. final PluginManager pluginManager,
  99. final CommandManager commandManager,
  100. final MessageSinkManager messageSinkManager,
  101. final ThemeManager themeManager,
  102. final CorePluginExtractor corePluginExtractor,
  103. final URLBuilder urlBuilder,
  104. final GlobalWindowManager globalWindowManager,
  105. final Set<CommandDetails> commands) {
  106. this.identityManager = identityManager;
  107. this.serverManager = serverManager;
  108. this.actionManager = actionManager;
  109. this.commandLineParser = commandLineParser;
  110. this.pluginManager = pluginManager;
  111. this.corePluginExtractor = corePluginExtractor;
  112. this.commandManager = commandManager;
  113. this.globalWindowManager = globalWindowManager;
  114. this.commands = commands;
  115. URLBuilder.setInstance(urlBuilder);
  116. }
  117. /**
  118. * Entry procedure.
  119. *
  120. * @param args the command line arguments
  121. */
  122. @SuppressWarnings("PMD.AvoidCatchingThrowable")
  123. public static void main(final String[] args) {
  124. Thread.setDefaultUncaughtExceptionHandler(new DMDircExceptionHandler());
  125. try {
  126. final ObjectGraph graph = ObjectGraph.create(new ClientModule());
  127. final CommandLineParser parser = graph.get(CommandLineParser.class);
  128. parser.parse(args);
  129. graph.get(Main.class).init();
  130. } catch (Throwable ex) {
  131. Logger.appError(ErrorLevel.FATAL, "Exception while initialising",
  132. ex);
  133. }
  134. }
  135. /**
  136. * Initialises the client.
  137. */
  138. public void init() {
  139. for (CommandDetails command : commands) {
  140. commandManager.registerCommand(command.getCommand(), command.getInfo());
  141. }
  142. loadUIs(pluginManager);
  143. doFirstRun();
  144. actionManager.initialise();
  145. pluginManager.doAutoLoad();
  146. actionManager.loadUserActions();
  147. actionManager.triggerEvent(CoreActionType.CLIENT_OPENED, null);
  148. commandLineParser.processArguments(serverManager);
  149. globalWindowManager.init();
  150. Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
  151. /** {@inheritDoc} */
  152. @Override
  153. public void run() {
  154. actionManager.triggerEvent(CoreActionType.CLIENT_CLOSED, null);
  155. serverManager.disconnectAll("Unexpected shutdown");
  156. identityManager.saveAll();
  157. }
  158. }, "Shutdown thread"));
  159. }
  160. /**
  161. * Called when the UI has failed to initialise correctly. This method
  162. * attempts to extract any and all UI plugins bundled with the client, and
  163. * requests a restart. If this has already been attempted, it shows an error
  164. * and exits.
  165. */
  166. private void handleMissingUI() {
  167. // Check to see if we have already tried this
  168. if (identityManager.getGlobalConfiguration().hasOptionBool("debug", "uiFixAttempted")) {
  169. System.out.println("DMDirc is unable to load any compatible UI plugins.");
  170. if (!GraphicsEnvironment.isHeadless()) {
  171. new WarningDialog(WarningDialog.NO_COMPAT_UIS_TITLE,
  172. WarningDialog.NO_RECOV_UIS).displayBlocking();
  173. }
  174. identityManager.getUserSettings().unsetOption("debug", "uiFixAttempted");
  175. System.exit(1);
  176. } else {
  177. // Try to extract the UIs again incase they changed between versions
  178. // and the user didn't update the UI plugin.
  179. corePluginExtractor.extractCorePlugins("ui_");
  180. System.out.println("DMDirc has updated the UI plugins and needs to restart.");
  181. if (!GraphicsEnvironment.isHeadless()) {
  182. new WarningDialog(WarningDialog.NO_COMPAT_UIS_TITLE,
  183. WarningDialog.NO_COMPAT_UIS_BODY).displayBlocking();
  184. }
  185. // Allow the rebooted DMDirc to know that we have attempted restarting.
  186. identityManager.getUserSettings().setOption("debug", "uiFixAttempted", "true");
  187. // Tell the launcher to restart!
  188. System.exit(42);
  189. }
  190. }
  191. /**
  192. * Attempts to find and activate a service which provides a UI that we
  193. * can use.
  194. *
  195. * @param pm The plugin manager to use to load plugins
  196. */
  197. protected void loadUIs(final PluginManager pm) {
  198. final List<Service> uis = pm.getServicesByType("ui");
  199. // First try: go for our desired service type
  200. for (Service service : uis) {
  201. if (service.activate()) {
  202. final ServiceProvider provider = service.getActiveProvider();
  203. final Object export = provider.getExportedService("getController").execute();
  204. if (export != null) {
  205. CONTROLLERS.add((UIController) export);
  206. }
  207. }
  208. }
  209. if (CONTROLLERS.isEmpty()) {
  210. handleMissingUI();
  211. } else {
  212. // The fix worked!
  213. if (identityManager.getGlobalConfiguration().hasOptionBool("debug", "uiFixAttempted")) {
  214. identityManager.getUserSettings().unsetOption("debug", "uiFixAttempted");
  215. }
  216. }
  217. }
  218. /**
  219. * Executes the first run or migration wizards as required.
  220. */
  221. private void doFirstRun() {
  222. if (identityManager.getGlobalConfiguration().getOptionBool("general", "firstRun")) {
  223. identityManager.getUserSettings().setOption("general", "firstRun", "false");
  224. for (UIController controller : CONTROLLERS) {
  225. controller.showFirstRunWizard();
  226. }
  227. new Timer().schedule(new TimerTask() {
  228. /** {@inheritDoc} */
  229. @Override
  230. public void run() {
  231. for (UIController controller : CONTROLLERS) {
  232. controller.showFeedbackNag();
  233. }
  234. }
  235. }, FEEDBACK_DELAY);
  236. }
  237. }
  238. }