Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Main.java 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /*
  2. * Copyright (c) 2006-2011 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.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.ConfigManager;
  28. import com.dmdirc.config.IdentityManager;
  29. import com.dmdirc.config.InvalidIdentityFileException;
  30. import com.dmdirc.logger.DMDircExceptionHandler;
  31. import com.dmdirc.logger.ErrorLevel;
  32. import com.dmdirc.logger.Logger;
  33. import com.dmdirc.plugins.PluginInfo;
  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.interfaces.UIController;
  39. import com.dmdirc.ui.themes.ThemeManager;
  40. import com.dmdirc.updater.UpdateChecker;
  41. import com.dmdirc.updater.Version;
  42. import com.dmdirc.util.resourcemanager.ResourceManager;
  43. import java.awt.GraphicsEnvironment;
  44. import java.io.File;
  45. import java.io.IOException;
  46. import java.text.SimpleDateFormat;
  47. import java.util.Collection;
  48. import java.util.Date;
  49. import java.util.HashSet;
  50. import java.util.List;
  51. import java.util.Map;
  52. import java.util.Timer;
  53. import java.util.TimerTask;
  54. import java.util.logging.Handler;
  55. import java.util.logging.Level;
  56. /**
  57. * Main class, handles initialisation.
  58. */
  59. public final class Main {
  60. /** Feedback nag delay. */
  61. private static final int FEEDBACK_DELAY = 30 * 60 * 1000;
  62. /** The UI to use for the client. */
  63. private static final Collection<UIController> CONTROLLERS = new HashSet<UIController>();
  64. /** The config dir to use for the client. */
  65. private static String configdir;
  66. /**
  67. * Prevents creation of main.
  68. */
  69. private Main() {
  70. }
  71. /**
  72. * Entry procedure.
  73. *
  74. * @param args the command line arguments
  75. */
  76. @SuppressWarnings("PMD.AvoidCatchingThrowable")
  77. public static void main(final String[] args) {
  78. try {
  79. init(args);
  80. } catch (Throwable ex) {
  81. Logger.appError(ErrorLevel.FATAL, "Exception while initialising",
  82. ex);
  83. }
  84. }
  85. /**
  86. * Initialises the client.
  87. *
  88. * @param args The command line arguments
  89. */
  90. private static void init(final String[] args) {
  91. Thread.setDefaultUncaughtExceptionHandler(new DMDircExceptionHandler());
  92. for (Handler handler : java.util.logging.Logger.getLogger("").getHandlers()) {
  93. handler.setLevel(Level.OFF); // Needs to be changed to enable debugging
  94. }
  95. // Enable finer debugging for specific components like so:
  96. //java.util.logging.Logger.getLogger("com.dmdirc.plugins").setLevel(Level.ALL);
  97. IdentityManager.loadVersion();
  98. final CommandLineParser clp = new CommandLineParser(args);
  99. try {
  100. IdentityManager.load();
  101. } catch (InvalidIdentityFileException iife) {
  102. handleInvalidConfigFile();
  103. }
  104. final PluginManager pm = PluginManager.getPluginManager();
  105. checkBundledPlugins(pm, IdentityManager.getGlobalConfig());
  106. ThemeManager.loadThemes();
  107. clp.applySettings();
  108. CommandManager.getCommandManager().initCommands();
  109. for (String service : new String[]{"ui", "tabcompletion", "parser"}) {
  110. ensureExists(pm, service);
  111. }
  112. // The user may have an existing parser plugin (e.g. twitter) which
  113. // will satisfy the service existance check above, but will render the
  114. // client pretty useless, so we'll force IRC extraction for now.
  115. extractCorePlugins("parser_irc");
  116. pm.refreshPlugins();
  117. loadUIs(pm);
  118. doFirstRun();
  119. ActionManager.getActionManager().initialise();
  120. pm.doAutoLoad();
  121. ActionManager.getActionManager().loadUserActions();
  122. ActionManager.getActionManager().triggerEvent(
  123. CoreActionType.CLIENT_OPENED, null);
  124. UpdateChecker.init();
  125. clp.processArguments();
  126. GlobalWindow.init();
  127. Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
  128. /** {@inheritDoc} */
  129. @Override
  130. public void run() {
  131. ActionManager.getActionManager().triggerEvent(
  132. CoreActionType.CLIENT_CLOSED, null);
  133. ServerManager.getServerManager().disconnectAll("Unexpected shutdown");
  134. IdentityManager.save();
  135. }
  136. }, "Shutdown thread"));
  137. }
  138. /**
  139. * Called when the UI has failed to initialise correctly. This method
  140. * attempts to extract any and all UI plugins bundled with the client, and
  141. * requests a restart. If this has already been attempted, it shows an error
  142. * and exits.
  143. */
  144. private static void handleMissingUI() {
  145. // Check to see if we have already tried this
  146. if (IdentityManager.getGlobalConfig().hasOptionBool("debug", "uiFixAttempted")) {
  147. System.out.println("DMDirc is unable to load any compatible UI plugins.");
  148. if (!GraphicsEnvironment.isHeadless()) {
  149. new WarningDialog(WarningDialog.NO_COMPAT_UIS_TITLE,
  150. WarningDialog.NO_RECOV_UIS).displayBlocking();
  151. }
  152. IdentityManager.getConfigIdentity().unsetOption("debug", "uiFixAttempted");
  153. System.exit(1);
  154. } else {
  155. // Try to extract the UIs again incase they changed between versions
  156. // and the user didn't update the UI plugin.
  157. extractCorePlugins("ui_");
  158. System.out.println("DMDirc has updated the UI plugins and needs to restart.");
  159. if (!GraphicsEnvironment.isHeadless()) {
  160. new WarningDialog(WarningDialog.NO_COMPAT_UIS_TITLE,
  161. WarningDialog.NO_COMPAT_UIS_BODY).displayBlocking();
  162. }
  163. // Allow the rebooted DMDirc to know that we have attempted restarting.
  164. IdentityManager.getConfigIdentity().setOption("debug", "uiFixAttempted", "true");
  165. // Tell the launcher to restart!
  166. System.exit(42);
  167. }
  168. }
  169. /**
  170. * Called when the global config cannot be loaded due to an error. This
  171. * method informs the user of the problem and installs a new default config
  172. * file, backing up the old one.
  173. */
  174. private static void handleInvalidConfigFile() {
  175. final String date = new SimpleDateFormat("yyyyMMddkkmmss").format(new Date());
  176. final String message = "DMDirc has detected that your config file "
  177. + "has become corrupted.<br><br>DMDirc will now backup "
  178. + "your current config and try restarting with a default "
  179. + "config.<br><br>Your old config will be saved as:<br>"
  180. + "dmdirc.config." + date;
  181. if (!GraphicsEnvironment.isHeadless()) {
  182. new WarningDialog("Invalid Config File", message).displayBlocking();
  183. }
  184. // Let command-line users know what is happening.
  185. System.out.println(message.replace("<br>", "\n"));
  186. final File configFile = new File(getConfigDir() + "dmdirc.config");
  187. final File newConfigFile = new File(getConfigDir() + "dmdirc.config." + date);
  188. if (configFile.renameTo(newConfigFile)) {
  189. try {
  190. IdentityManager.load();
  191. } catch (InvalidIdentityFileException iife2) {
  192. // This shouldn't happen!
  193. Logger.appError(ErrorLevel.FATAL, "Unable to load global config", iife2);
  194. }
  195. } else {
  196. final String newMessage = "DMDirc was unable to rename the "
  197. + "global config file and is unable to fix this issue.";
  198. if (!GraphicsEnvironment.isHeadless()) {
  199. new WarningDialog("Invalid Config File", newMessage).displayBlocking();
  200. }
  201. System.out.println(newMessage.replace("<br>", "\n"));
  202. System.exit(1);
  203. }
  204. }
  205. /**
  206. * Ensures that there is at least one provider of the specified
  207. * service type by extracting matching core plugins. Plugins must be named
  208. * so that their file name starts with the service type, and then an
  209. * underscore.
  210. *
  211. * @param pm The plugin manager to use to access services
  212. * @param serviceType The type of service that should exist
  213. */
  214. public static void ensureExists(final PluginManager pm, final String serviceType) {
  215. if (pm.getServicesByType(serviceType).isEmpty()) {
  216. extractCorePlugins(serviceType + "_");
  217. pm.refreshPlugins();
  218. }
  219. }
  220. /**
  221. * Checks whether the plugins bundled with this release of DMDirc are newer
  222. * than the plugins known by the specified {@link PluginManager}. If the
  223. * bundled plugins are newer, they are automatically extracted.
  224. *
  225. * @param pm The plugin manager to use to check plugins
  226. * @param config The configuration source for bundled versions
  227. */
  228. private static void checkBundledPlugins(final PluginManager pm, final ConfigManager config) {
  229. for (PluginInfo plugin : pm.getPluginInfos()) {
  230. if (config.hasOptionString("bundledplugins_versions", plugin.getName())) {
  231. final Version bundled = new Version(config.getOption("bundledplugins_versions",
  232. plugin.getName()));
  233. final Version installed = plugin.getVersion();
  234. if (installed.compareTo(bundled) < 0) {
  235. extractCorePlugins(plugin.getName());
  236. PluginManager.getPluginManager().reloadPlugin(plugin.getFilename());
  237. }
  238. }
  239. }
  240. }
  241. /**
  242. * Attempts to find and activate a service which provides a UI that we
  243. * can use.
  244. *
  245. * @param pm The plugin manager to use to load plugins
  246. */
  247. protected static void loadUIs(final PluginManager pm) {
  248. final List<Service> uis = pm.getServicesByType("ui");
  249. // First try: go for our desired service type
  250. for (Service service : uis) {
  251. if (service.activate()) {
  252. final ServiceProvider provider = service.getActiveProvider();
  253. final Object export = provider.getExportedService("getController").execute();
  254. if (export != null) {
  255. CONTROLLERS.add((UIController) export);
  256. }
  257. }
  258. }
  259. if (CONTROLLERS.isEmpty()) {
  260. handleMissingUI();
  261. } else {
  262. // The fix worked!
  263. if (IdentityManager.getGlobalConfig().hasOptionBool("debug", "uiFixAttempted")) {
  264. IdentityManager.getConfigIdentity().unsetOption("debug", "uiFixAttempted");
  265. }
  266. }
  267. }
  268. /**
  269. * Executes the first run or migration wizards as required.
  270. */
  271. private static void doFirstRun() {
  272. if (IdentityManager.getGlobalConfig().getOptionBool("general", "firstRun")) {
  273. IdentityManager.getConfigIdentity().setOption("general", "firstRun", "false");
  274. for (UIController controller : CONTROLLERS) {
  275. controller.showFirstRunWizard();
  276. }
  277. new Timer().schedule(new TimerTask() {
  278. /** {@inheritDoc} */
  279. @Override
  280. public void run() {
  281. for (UIController controller : CONTROLLERS) {
  282. controller.showFeedbackNag();
  283. }
  284. }
  285. }, FEEDBACK_DELAY);
  286. }
  287. }
  288. /**
  289. * Quits the client nicely, with the default closing message.
  290. */
  291. public static void quit() {
  292. quit(0);
  293. }
  294. /**
  295. * Quits the client nicely, with the default closing message.
  296. *
  297. * @param exitCode This is the exit code that will be returned to the
  298. * operating system when the client exits
  299. */
  300. public static void quit(final int exitCode) {
  301. quit(IdentityManager.getGlobalConfig().getOption("general",
  302. "closemessage"), exitCode);
  303. }
  304. /**
  305. * Quits the client nicely.
  306. *
  307. * @param reason The quit reason to send
  308. */
  309. public static void quit(final String reason) {
  310. quit(reason, 0);
  311. }
  312. /**
  313. * Quits the client nicely.
  314. *
  315. * @param reason The quit reason to send
  316. * @param exitCode This is the exit code that will be returned to the
  317. * operating system when the client exits
  318. */
  319. public static void quit(final String reason, final int exitCode) {
  320. ServerManager.getServerManager().disconnectAll(reason);
  321. System.exit(exitCode);
  322. }
  323. /**
  324. * Retrieves the UI controller that's being used by the client.
  325. *
  326. * @return The client's UI controller
  327. * @deprecated Shouldn't be used. There may be multiple or no controllers.
  328. */
  329. @Deprecated
  330. public static UIController getUI() {
  331. return CONTROLLERS.iterator().next();
  332. }
  333. /**
  334. * Returns the application's config directory.
  335. *
  336. * @return configuration directory
  337. */
  338. public static String getConfigDir() {
  339. if (configdir == null) {
  340. initialiseConfigDir();
  341. }
  342. return configdir;
  343. }
  344. /**
  345. * Initialises the location of the configuration directory.
  346. */
  347. protected static void initialiseConfigDir() {
  348. final String fs = System.getProperty("file.separator");
  349. final String osName = System.getProperty("os.name");
  350. if (System.getenv("DMDIRC_HOME") != null) {
  351. configdir = System.getenv("DMDIRC_HOME");
  352. } else if (osName.startsWith("Mac OS")) {
  353. configdir = System.getProperty("user.home") + fs + "Library"
  354. + fs + "Preferences" + fs + "DMDirc" + fs;
  355. } else if (osName.startsWith("Windows")) {
  356. if (System.getenv("APPDATA") == null) {
  357. configdir = System.getProperty("user.home") + fs + "DMDirc" + fs;
  358. } else {
  359. configdir = System.getenv("APPDATA") + fs + "DMDirc" + fs;
  360. }
  361. } else {
  362. configdir = System.getProperty("user.home") + fs + ".DMDirc" + fs;
  363. final File testFile = new File(configdir);
  364. if (!testFile.exists()) {
  365. final String configHome = System.getenv("XDG_CONFIG_HOME");
  366. configdir = (configHome == null || configHome.isEmpty())
  367. ? System.getProperty("user.home") + fs + ".config" + fs
  368. : configHome;
  369. configdir += fs + "DMDirc" + fs;
  370. }
  371. }
  372. configdir = new File(configdir).getAbsolutePath() + fs;
  373. }
  374. /**
  375. * Sets the config directory for this client.
  376. *
  377. * @param newdir The new configuration directory
  378. */
  379. public static void setConfigDir(final String newdir) {
  380. configdir = newdir;
  381. }
  382. /**
  383. * Extracts plugins bundled with DMDirc to the user's profile's plugin
  384. * directory.
  385. *
  386. * @param prefix If non-null, only plugins whose file name starts with
  387. * this prefix will be extracted.
  388. */
  389. public static void extractCorePlugins(final String prefix) {
  390. final Map<String, byte[]> resources = ResourceManager.getResourceManager()
  391. .getResourcesStartingWithAsBytes("plugins");
  392. for (Map.Entry<String, byte[]> resource : resources.entrySet()) {
  393. try {
  394. final String resourceName = Main.getConfigDir() + "plugins"
  395. + resource.getKey().substring(7);
  396. if (prefix != null && !resource.getKey().substring(8).startsWith(prefix)) {
  397. continue;
  398. }
  399. final File newDir = new File(resourceName.substring(0,
  400. resourceName.lastIndexOf('/')) + "/");
  401. if (!newDir.exists()) {
  402. newDir.mkdirs();
  403. }
  404. final File newFile = new File(newDir,
  405. resourceName.substring(resourceName.lastIndexOf('/') + 1,
  406. resourceName.length()));
  407. if (!newFile.isDirectory()) {
  408. final PluginManager pm = PluginManager.getPluginManager();
  409. ResourceManager.getResourceManager().
  410. resourceToFile(resource.getValue(), newFile);
  411. final PluginInfo plugin = pm.getPluginInfo(newFile
  412. .getAbsolutePath().substring(pm.getDirectory().length()));
  413. if (plugin != null) {
  414. plugin.pluginUpdated();
  415. }
  416. }
  417. } catch (IOException ex) {
  418. Logger.userError(ErrorLevel.LOW, "Failed to extract plugins", ex);
  419. }
  420. }
  421. }
  422. }