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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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.interfaces.LifecycleController;
  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.ui.UIController;
  30. import com.dmdirc.logger.DMDircExceptionHandler;
  31. import com.dmdirc.logger.ErrorLevel;
  32. import com.dmdirc.logger.Logger;
  33. import com.dmdirc.messages.MessageSinkManager;
  34. import com.dmdirc.plugins.PluginManager;
  35. import com.dmdirc.plugins.Service;
  36. import com.dmdirc.plugins.ServiceProvider;
  37. import com.dmdirc.ui.WarningDialog;
  38. import com.dmdirc.ui.themes.ThemeManager;
  39. import java.awt.GraphicsEnvironment;
  40. import java.util.Collection;
  41. import java.util.HashSet;
  42. import java.util.List;
  43. import java.util.Timer;
  44. import java.util.TimerTask;
  45. import javax.inject.Inject;
  46. import dagger.ObjectGraph;
  47. /**
  48. * Main class, handles initialisation.
  49. */
  50. public class Main implements LifecycleController {
  51. /** Feedback nag delay. */
  52. private final int FEEDBACK_DELAY = 30 * 60 * 1000;
  53. /** The UI to use for the client. */
  54. private final Collection<UIController> CONTROLLERS = new HashSet<>();
  55. /** The identity manager the client will use. */
  56. private final IdentityManager identityManager;
  57. /** The server manager the client will use. */
  58. private final ServerManager serverManager;
  59. /** The action manager the client will use. */
  60. private final ActionManager actionManager;
  61. /** The command-line parser used for this instance. */
  62. private final CommandLineParser commandLineParser;
  63. /** The plugin manager the client will use. */
  64. private final PluginManager pluginManager;
  65. /** The extractor to use for core plugins. */
  66. private final CorePluginExtractor corePluginExtractor;
  67. /** Instance of main, protected to allow subclasses direct access. */
  68. @Deprecated
  69. public static Main mainInstance;
  70. /**
  71. * Creates a new instance of {@link Main}.
  72. *
  73. * @param identityManager The identity manager the client will use.
  74. * @param serverManager The server manager the client will use.
  75. * @param actionManager The action manager the client will use.
  76. * @param commandLineParser The command-line parser used for this instance.
  77. * @param pluginManager The plugin manager the client will use.
  78. * @param commandManager Unused for now - TODO: remove me when it's injected somewhere sensible.
  79. * @param messageSinkManager Unused for now - TODO: remove me when it's injected somewhere sensible.
  80. * @param themeManager Unused for now - TODO: remove me when it's injected somewhere sensible.
  81. * @param aliasWrapper Alias wrapper to provide to the actions manager.
  82. * @param corePluginExtractor Extractor to use for core plugins.
  83. */
  84. @Inject
  85. public Main(
  86. final IdentityManager identityManager,
  87. final ServerManager serverManager,
  88. final ActionManager actionManager,
  89. final CommandLineParser commandLineParser,
  90. final PluginManager pluginManager,
  91. final CommandManager commandManager,
  92. final MessageSinkManager messageSinkManager,
  93. final ThemeManager themeManager,
  94. final CorePluginExtractor corePluginExtractor) {
  95. this.identityManager = identityManager;
  96. this.serverManager = serverManager;
  97. this.actionManager = actionManager;
  98. this.commandLineParser = commandLineParser;
  99. this.pluginManager = pluginManager;
  100. this.corePluginExtractor = corePluginExtractor;
  101. }
  102. /**
  103. * Entry procedure.
  104. *
  105. * @param args the command line arguments
  106. */
  107. @SuppressWarnings("PMD.AvoidCatchingThrowable")
  108. public static void main(final String[] args) {
  109. Thread.setDefaultUncaughtExceptionHandler(new DMDircExceptionHandler());
  110. try {
  111. final ObjectGraph graph = ObjectGraph.create(new ClientModule());
  112. final CommandLineParser parser = graph.get(CommandLineParser.class);
  113. parser.parse(args);
  114. mainInstance = graph.get(Main.class);
  115. mainInstance.init();
  116. } catch (Throwable ex) {
  117. Logger.appError(ErrorLevel.FATAL, "Exception while initialising",
  118. ex);
  119. }
  120. }
  121. /**
  122. * Initialises the client.
  123. *
  124. * @param args The command line arguments
  125. */
  126. public void init() {
  127. loadUIs(pluginManager);
  128. doFirstRun();
  129. actionManager.initialise();
  130. pluginManager.doAutoLoad();
  131. actionManager.loadUserActions();
  132. actionManager.triggerEvent(CoreActionType.CLIENT_OPENED, null);
  133. commandLineParser.processArguments(serverManager);
  134. GlobalWindow.init();
  135. Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
  136. /** {@inheritDoc} */
  137. @Override
  138. public void run() {
  139. actionManager.triggerEvent(CoreActionType.CLIENT_CLOSED, null);
  140. serverManager.disconnectAll("Unexpected shutdown");
  141. identityManager.saveAll();
  142. }
  143. }, "Shutdown thread"));
  144. }
  145. /**
  146. * Get the plugin manager for this instance of main.
  147. *
  148. * @return PluginManager in use.
  149. * @Deprecated Global state is bad.
  150. */
  151. @Deprecated
  152. public PluginManager getPluginManager() {
  153. return pluginManager;
  154. }
  155. /**
  156. * Get our ServerManager
  157. *
  158. * @return ServerManager controlled by this Main.
  159. * @Deprecated Global state is bad.
  160. */
  161. @Deprecated
  162. public ServerManager getServerManager() {
  163. return serverManager;
  164. }
  165. /**
  166. * Called when the UI has failed to initialise correctly. This method
  167. * attempts to extract any and all UI plugins bundled with the client, and
  168. * requests a restart. If this has already been attempted, it shows an error
  169. * and exits.
  170. */
  171. private void handleMissingUI() {
  172. // Check to see if we have already tried this
  173. if (identityManager.getGlobalConfiguration().hasOptionBool("debug", "uiFixAttempted")) {
  174. System.out.println("DMDirc is unable to load any compatible UI plugins.");
  175. if (!GraphicsEnvironment.isHeadless()) {
  176. new WarningDialog(WarningDialog.NO_COMPAT_UIS_TITLE,
  177. WarningDialog.NO_RECOV_UIS).displayBlocking();
  178. }
  179. identityManager.getGlobalConfigIdentity().unsetOption("debug", "uiFixAttempted");
  180. System.exit(1);
  181. } else {
  182. // Try to extract the UIs again incase they changed between versions
  183. // and the user didn't update the UI plugin.
  184. corePluginExtractor.extractCorePlugins("ui_");
  185. System.out.println("DMDirc has updated the UI plugins and needs to restart.");
  186. if (!GraphicsEnvironment.isHeadless()) {
  187. new WarningDialog(WarningDialog.NO_COMPAT_UIS_TITLE,
  188. WarningDialog.NO_COMPAT_UIS_BODY).displayBlocking();
  189. }
  190. // Allow the rebooted DMDirc to know that we have attempted restarting.
  191. identityManager.getGlobalConfigIdentity().setOption("debug", "uiFixAttempted", "true");
  192. // Tell the launcher to restart!
  193. System.exit(42);
  194. }
  195. }
  196. /**
  197. * Attempts to find and activate a service which provides a UI that we
  198. * can use.
  199. *
  200. * @param pm The plugin manager to use to load plugins
  201. */
  202. protected void loadUIs(final PluginManager pm) {
  203. final List<Service> uis = pm.getServicesByType("ui");
  204. // First try: go for our desired service type
  205. for (Service service : uis) {
  206. if (service.activate()) {
  207. final ServiceProvider provider = service.getActiveProvider();
  208. final Object export = provider.getExportedService("getController").execute();
  209. if (export != null) {
  210. CONTROLLERS.add((UIController) export);
  211. }
  212. }
  213. }
  214. if (CONTROLLERS.isEmpty()) {
  215. handleMissingUI();
  216. } else {
  217. // The fix worked!
  218. if (identityManager.getGlobalConfiguration().hasOptionBool("debug", "uiFixAttempted")) {
  219. identityManager.getGlobalConfigIdentity().unsetOption("debug", "uiFixAttempted");
  220. }
  221. }
  222. }
  223. /**
  224. * Executes the first run or migration wizards as required.
  225. */
  226. private void doFirstRun() {
  227. if (identityManager.getGlobalConfiguration().getOptionBool("general", "firstRun")) {
  228. identityManager.getGlobalConfigIdentity().setOption("general", "firstRun", "false");
  229. for (UIController controller : CONTROLLERS) {
  230. controller.showFirstRunWizard();
  231. }
  232. new Timer().schedule(new TimerTask() {
  233. /** {@inheritDoc} */
  234. @Override
  235. public void run() {
  236. for (UIController controller : CONTROLLERS) {
  237. controller.showFeedbackNag();
  238. }
  239. }
  240. }, FEEDBACK_DELAY);
  241. }
  242. }
  243. /**
  244. * {@inheritDoc}
  245. *
  246. * @deprecated Use a proper {@link LifecycleController}.
  247. */
  248. @Override
  249. @Deprecated
  250. public void quit() {
  251. quit(0);
  252. }
  253. /**
  254. * {@inheritDoc}
  255. *
  256. * @deprecated Use a proper {@link LifecycleController}.
  257. */
  258. @Override
  259. @Deprecated
  260. public void quit(final int exitCode) {
  261. quit(identityManager.getGlobalConfiguration().getOption("general",
  262. "closemessage"), exitCode);
  263. }
  264. /**
  265. * {@inheritDoc}
  266. *
  267. * @deprecated Use a proper {@link LifecycleController}.
  268. */
  269. @Override
  270. @Deprecated
  271. public void quit(final String reason) {
  272. quit(reason, 0);
  273. }
  274. /**
  275. * {@inheritDoc}
  276. *
  277. * @deprecated Use a proper {@link LifecycleController}.
  278. */
  279. @Override
  280. @Deprecated
  281. public void quit(final String reason, final int exitCode) {
  282. serverManager.disconnectAll(reason);
  283. System.exit(exitCode);
  284. }
  285. /**
  286. * Retrieves the UI controller that's being used by the client.
  287. *
  288. * @return The client's UI controller
  289. * @deprecated Shouldn't be used. There may be multiple or no controllers.
  290. */
  291. @Deprecated
  292. public UIController getUI() {
  293. return CONTROLLERS.iterator().next();
  294. }
  295. /**
  296. * Extracts plugins bundled with DMDirc to the user's profile's plugin
  297. * directory.
  298. *
  299. * @param prefix If non-null, only plugins whose file name starts with
  300. * this prefix will be extracted.
  301. * @deprecated Go via a {@link CorePluginExtractor}.
  302. */
  303. @Deprecated
  304. public void extractCorePlugins(final String prefix) {
  305. corePluginExtractor.extractCorePlugins(prefix);
  306. }
  307. }