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.

SwingFirstRunWizard.java 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Copyright (c) 2006-2010 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.addons.ui_swing.wizard.firstrun;
  23. import com.dmdirc.Main;
  24. import com.dmdirc.actions.ActionManager;
  25. import com.dmdirc.actions.CoreActionType;
  26. import com.dmdirc.actions.interfaces.ActionType;
  27. import com.dmdirc.addons.ui_swing.MainFrame;
  28. import com.dmdirc.config.IdentityManager;
  29. import com.dmdirc.interfaces.ActionListener;
  30. import com.dmdirc.logger.ErrorLevel;
  31. import com.dmdirc.logger.Logger;
  32. import com.dmdirc.ui.interfaces.FirstRunWizard;
  33. import com.dmdirc.addons.ui_swing.Apple;
  34. import com.dmdirc.addons.ui_swing.dialogs.profiles.ProfileManagerDialog;
  35. import com.dmdirc.addons.ui_swing.wizard.Step;
  36. import com.dmdirc.addons.ui_swing.wizard.WizardDialog;
  37. import com.dmdirc.addons.ui_swing.wizard.WizardListener;
  38. import com.dmdirc.ui.IconManager;
  39. import com.dmdirc.util.resourcemanager.ResourceManager;
  40. import java.awt.Dimension;
  41. import java.io.File;
  42. import java.io.IOException;
  43. import java.util.ArrayList;
  44. import java.util.Map;
  45. import java.util.Map.Entry;
  46. /** First run wizard, used to initially setup the client for the user. */
  47. public final class SwingFirstRunWizard implements WizardListener,
  48. FirstRunWizard {
  49. /** Wizard dialog. */
  50. private WizardDialog wizardDialog;
  51. /** First run or update. */
  52. private boolean firstRun = true;
  53. /** Instatiate the wizard. */
  54. public SwingFirstRunWizard() {
  55. this(true);
  56. }
  57. /**
  58. * Instantiate the wizard.
  59. *
  60. * @param firstRun is this the first run or an update?
  61. */
  62. public SwingFirstRunWizard(final boolean firstRun) {
  63. this.firstRun = firstRun;
  64. wizardDialog =
  65. new WizardDialog("DMDirc: " + (firstRun ? "Setup wizard" :
  66. "Migration wizard"), new ArrayList<Step>(), this, null);
  67. wizardDialog.setIconImage(IconManager.getIconManager().getImage("icon"));
  68. wizardDialog.addWizardListener(this);
  69. if(Apple.isAppleUI()) {
  70. wizardDialog.setMinimumSize(new Dimension(400, 425));
  71. } else {
  72. wizardDialog.setMinimumSize(new Dimension(400, 400));
  73. }
  74. }
  75. /** {@inheritDoc} */
  76. @Override
  77. public void wizardFinished() {
  78. if (ResourceManager.getResourceManager() == null) {
  79. return;
  80. }
  81. if (((ExtractionStep) wizardDialog.getStep(0)).getPluginsState()) {
  82. extractPlugins();
  83. }
  84. if (((ExtractionStep) wizardDialog.getStep(0)).getActionsState()) {
  85. extractActions();
  86. }
  87. if (firstRun) {
  88. IdentityManager.getConfigIdentity().setOption("updater", "enable",
  89. ((CommunicationStep) wizardDialog.getStep(1)).checkUpdates());
  90. IdentityManager.getConfigIdentity().setOption("general", "submitErrors",
  91. ((CommunicationStep) wizardDialog.getStep(1)).checkErrors());
  92. }
  93. if (firstRun &&
  94. ((ProfileStep) wizardDialog.getStep(2)).getProfileManagerState()) {
  95. ActionManager.addListener(new ActionListener() {
  96. /** {@inheritDoc} */
  97. @Override
  98. public void processEvent(final ActionType type,
  99. final StringBuffer format, final Object... arguments) {
  100. ProfileManagerDialog.showProfileManagerDialog((MainFrame) Main.getUI().getMainWindow());
  101. }
  102. }, CoreActionType.CLIENT_OPENED);
  103. }
  104. wizardDialog.dispose();
  105. }
  106. /** {@inheritDoc} */
  107. @Override
  108. public void wizardCancelled() {
  109. wizardDialog.dispose();
  110. }
  111. /** {@inheritDoc} */
  112. @Override
  113. public void extractPlugins() {
  114. extractCorePlugins();
  115. }
  116. /** Extracts the core plugins. */
  117. public static void extractCorePlugins() {
  118. //Copy actions
  119. final Map<String, byte[]> resources =
  120. ResourceManager.getResourceManager().
  121. getResourcesStartingWithAsBytes("plugins");
  122. for (Entry<String, byte[]> resource : resources.entrySet()) {
  123. try {
  124. final String resourceName =
  125. Main.getConfigDir() + "plugins" +
  126. resource.getKey().
  127. substring(7, resource.getKey().length());
  128. final File newDir =
  129. new File(resourceName.substring(0,
  130. resourceName.lastIndexOf('/')) + "/");
  131. if (!newDir.exists()) {
  132. newDir.mkdirs();
  133. }
  134. final File newFile =
  135. new File(newDir,
  136. resourceName.substring(resourceName.lastIndexOf('/') + 1,
  137. resourceName.length()));
  138. if (!newFile.isDirectory()) {
  139. ResourceManager.getResourceManager().
  140. resourceToFile(resource.getValue(), newFile);
  141. }
  142. } catch (IOException ex) {
  143. Logger.userError(ErrorLevel.LOW, "Failed to extract plugins");
  144. }
  145. }
  146. }
  147. /** {@inheritDoc} */
  148. @Override
  149. public void extractActions() {
  150. extractCoreActions();
  151. }
  152. /** Extracts the core actions. */
  153. public static void extractCoreActions() {
  154. //Copy actions
  155. final Map<String, byte[]> resources =
  156. ResourceManager.getResourceManager().
  157. getResourcesStartingWithAsBytes("com/dmdirc/actions/defaults");
  158. for (Entry<String, byte[]> resource : resources.entrySet()) {
  159. try {
  160. final String resourceName =
  161. Main.getConfigDir() + "actions" +
  162. resource.getKey().
  163. substring(27, resource.getKey().length());
  164. final File newDir =
  165. new File(resourceName.substring(0,
  166. resourceName.lastIndexOf('/')) + "/");
  167. if (!newDir.exists()) {
  168. newDir.mkdirs();
  169. }
  170. final File newFile =
  171. new File(newDir,
  172. resourceName.substring(resourceName.lastIndexOf('/') + 1,
  173. resourceName.length()));
  174. if (!newFile.isDirectory()) {
  175. ResourceManager.getResourceManager().
  176. resourceToFile(resource.getValue(), newFile);
  177. }
  178. } catch (IOException ex) {
  179. Logger.userError(ErrorLevel.LOW, "Failed to extract actions");
  180. }
  181. }
  182. }
  183. /** {@inheritDoc} */
  184. @Override
  185. public void display() {
  186. if (firstRun) {
  187. wizardDialog.addStep(new FirstRunExtractionStep());
  188. wizardDialog.addStep(new CommunicationStep());
  189. wizardDialog.addStep(new ProfileStep());
  190. } else {
  191. wizardDialog.addStep(new MigrationExtrationStep());
  192. }
  193. wizardDialog.display();
  194. }
  195. /**
  196. * Returns the dialog associated with this wizard.
  197. *
  198. * @return Associated wizard dialog
  199. */
  200. public WizardDialog getWizardDialog() {
  201. return wizardDialog;
  202. }
  203. }