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.

CoreNewServerDialogModel.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * Copyright (c) 2006-2015 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.ui.core.newserver;
  23. import com.dmdirc.ClientModule.UserConfig;
  24. import com.dmdirc.config.GlobalConfig;
  25. import com.dmdirc.config.profiles.Profile;
  26. import com.dmdirc.config.profiles.ProfileManager;
  27. import com.dmdirc.events.ProfileAddedEvent;
  28. import com.dmdirc.events.ProfileDeletedEvent;
  29. import com.dmdirc.interfaces.ConnectionManager;
  30. import com.dmdirc.interfaces.EventBus;
  31. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  32. import com.dmdirc.interfaces.config.ConfigProvider;
  33. import com.dmdirc.interfaces.ui.NewServerDialogModel;
  34. import com.dmdirc.interfaces.ui.NewServerDialogModelListener;
  35. import com.dmdirc.util.collections.ListenerList;
  36. import com.dmdirc.util.validators.*;
  37. import com.google.common.collect.ImmutableList;
  38. import net.engio.mbassy.listener.Handler;
  39. import javax.inject.Inject;
  40. import java.net.URI;
  41. import java.net.URISyntaxException;
  42. import java.util.ArrayList;
  43. import java.util.List;
  44. import java.util.Optional;
  45. import static com.google.common.base.Preconditions.checkNotNull;
  46. /**
  47. * Default implementation of a new server dialog model.
  48. */
  49. public class CoreNewServerDialogModel implements NewServerDialogModel {
  50. private final ListenerList listeners;
  51. private final AggregateConfigProvider globalConfig;
  52. private final ConfigProvider userConfig;
  53. private final ConnectionManager connectionManager;
  54. private final ProfileManager profileManager;
  55. private final List<Profile> profiles;
  56. private final EventBus eventBus;
  57. private Optional<Profile> selectedProfile;
  58. private Optional<String> hostname;
  59. private Optional<Integer> port;
  60. private Optional<String> password;
  61. private boolean ssl;
  62. private boolean saveAsDefault;
  63. @Inject
  64. public CoreNewServerDialogModel(
  65. @GlobalConfig final AggregateConfigProvider globalConfig,
  66. @UserConfig final ConfigProvider userConfig,
  67. final ProfileManager profileManager,
  68. final ConnectionManager connectionManager,
  69. final EventBus eventBus) {
  70. this.globalConfig = globalConfig;
  71. this.userConfig = userConfig;
  72. this.profileManager = profileManager;
  73. this.connectionManager = connectionManager;
  74. this.eventBus = eventBus;
  75. listeners = new ListenerList();
  76. profiles = new ArrayList<>(5);
  77. selectedProfile = Optional.empty();
  78. hostname = Optional.empty();
  79. port = Optional.empty();
  80. password = Optional.empty();
  81. ssl = false;
  82. saveAsDefault = false;
  83. }
  84. @Override
  85. public void loadModel() {
  86. eventBus.subscribe(this);
  87. profiles.addAll(profileManager.getProfiles());
  88. hostname = Optional.ofNullable(globalConfig.getOption("newserver", "hostname"));
  89. port = Optional.ofNullable(globalConfig.getOptionInt("newserver", "port"));
  90. password = Optional.ofNullable(globalConfig.getOption("newserver", "password"));
  91. ssl = globalConfig.getOptionBool("newserver", "ssl");
  92. saveAsDefault = false;
  93. listeners.getCallable(NewServerDialogModelListener.class).serverDetailsChanged(hostname,
  94. port, password, ssl, saveAsDefault);
  95. }
  96. @Override
  97. public List<Profile> getProfileList() {
  98. return ImmutableList.copyOf(profiles);
  99. }
  100. @Override
  101. public Optional<Profile> getSelectedProfile() {
  102. return selectedProfile;
  103. }
  104. @Override
  105. public void setSelectedProfile(final Optional<Profile> selectedProfile) {
  106. checkNotNull(selectedProfile);
  107. final Optional<Profile> oldSelectedProfile = this.selectedProfile;
  108. this.selectedProfile = selectedProfile;
  109. listeners.getCallable(NewServerDialogModelListener.class).selectedProfileChanged(
  110. oldSelectedProfile, selectedProfile);
  111. }
  112. @Override
  113. public boolean isProfileListValid() {
  114. return !getProfileListValidator().validate(getProfileList()).isFailure();
  115. }
  116. @Override
  117. public Validator<List<Profile>> getProfileListValidator() {
  118. return new ListNotEmptyValidator<>();
  119. }
  120. @Override
  121. public Optional<String> getHostname() {
  122. return hostname;
  123. }
  124. @Override
  125. public void setHostname(final Optional<String> hostname) {
  126. checkNotNull(hostname);
  127. this.hostname = hostname;
  128. listeners.getCallable(NewServerDialogModelListener.class).serverDetailsChanged(hostname,
  129. port, password, ssl, saveAsDefault);
  130. }
  131. @Override
  132. public boolean isHostnameValid() {
  133. return getHostname().isPresent() && !getHostnameValidator().validate(getHostname().get()).
  134. isFailure();
  135. }
  136. @Override
  137. public Validator<String> getHostnameValidator() {
  138. return new ServerNameValidator();
  139. }
  140. @Override
  141. public Optional<Integer> getPort() {
  142. return port;
  143. }
  144. @Override
  145. public void setPort(final Optional<Integer> port) {
  146. checkNotNull(port);
  147. this.port = port;
  148. listeners.getCallable(NewServerDialogModelListener.class).serverDetailsChanged(hostname,
  149. port, password, ssl, saveAsDefault);
  150. }
  151. @Override
  152. public boolean isPortValid() {
  153. return getPort().isPresent() && !getPortValidator().validate(getPort().get()).isFailure();
  154. }
  155. @Override
  156. public Validator<Integer> getPortValidator() {
  157. return new IntegerPortValidator();
  158. }
  159. @Override
  160. public Optional<String> getPassword() {
  161. return password;
  162. }
  163. @Override
  164. public void setPassword(final Optional<String> password) {
  165. checkNotNull(password);
  166. this.password = password;
  167. listeners.getCallable(NewServerDialogModelListener.class).serverDetailsChanged(hostname,
  168. port, password, ssl, saveAsDefault);
  169. }
  170. @Override
  171. public boolean isPasswordValid() {
  172. return !getPassword().isPresent() || !getPasswordValidator().validate(getPassword().get()).
  173. isFailure();
  174. }
  175. @Override
  176. public Validator<String> getPasswordValidator() {
  177. return new PermissiveValidator<>();
  178. }
  179. @Override
  180. public boolean getSSL() {
  181. return ssl;
  182. }
  183. @Override
  184. public void setSSL(final boolean ssl) {
  185. this.ssl = ssl;
  186. listeners.getCallable(NewServerDialogModelListener.class).serverDetailsChanged(hostname,
  187. port, password, ssl, saveAsDefault);
  188. }
  189. @Override
  190. public boolean getSaveAsDefault() {
  191. return saveAsDefault;
  192. }
  193. @Override
  194. public void setSaveAsDefault(final boolean saveAsDefault) {
  195. this.saveAsDefault = saveAsDefault;
  196. listeners.getCallable(NewServerDialogModelListener.class).serverDetailsChanged(hostname,
  197. port, password, ssl, saveAsDefault);
  198. }
  199. @Override
  200. public void save() {
  201. if (saveAsDefault) {
  202. userConfig.
  203. setOption("newserver", "hostname", hostname.isPresent() ? hostname.get() : "");
  204. userConfig.setOption("newserver", "port", port.isPresent() ? port.get() : 6667);
  205. userConfig.setOption("newserver", "password",
  206. password.isPresent() ? password.get() : "");
  207. userConfig.setOption("newserver", "ssl", ssl);
  208. }
  209. try {
  210. if (selectedProfile.isPresent()) {
  211. connectionManager.connectToAddress(getServerURI(), selectedProfile.get());
  212. } else {
  213. connectionManager.connectToAddress(getServerURI());
  214. }
  215. } catch (URISyntaxException ex) {
  216. //This is tested in isSaveAllowed, shouldn't happen here.
  217. }
  218. }
  219. @Override
  220. public boolean isSaveAllowed() {
  221. try {
  222. getServerURI();
  223. } catch (URISyntaxException ex) {
  224. return false;
  225. }
  226. return isHostnameValid() && isPortValid() && isPasswordValid() && isProfileListValid();
  227. }
  228. @Override
  229. public void addListener(final NewServerDialogModelListener listener) {
  230. checkNotNull(listener);
  231. listeners.add(NewServerDialogModelListener.class, listener);
  232. }
  233. @Override
  234. public void removeListener(final NewServerDialogModelListener listener) {
  235. checkNotNull(listener);
  236. listeners.remove(NewServerDialogModelListener.class, listener);
  237. }
  238. @Handler
  239. public void profileAdded(final ProfileAddedEvent event) {
  240. profiles.add(event.getProfile());
  241. listeners.getCallable(NewServerDialogModelListener.class).profileListChanged(
  242. ImmutableList.copyOf(profiles));
  243. }
  244. @Handler
  245. public void profileDeleted(final ProfileDeletedEvent event) {
  246. if (Optional.of(event.getProfile()).equals(selectedProfile)) {
  247. selectedProfile = Optional.empty();
  248. listeners.getCallable(NewServerDialogModelListener.class).selectedProfileChanged(
  249. Optional.of(event.getProfile()), selectedProfile);
  250. }
  251. profiles.remove(event.getProfile());
  252. listeners.getCallable(NewServerDialogModelListener.class).profileListChanged(
  253. ImmutableList.copyOf(profiles));
  254. }
  255. /**
  256. * Gets the URI for the details in the dialog.
  257. *
  258. * @return Returns the URI the details represent
  259. *
  260. * @throws URISyntaxException If the resulting URI is invalid
  261. */
  262. private URI getServerURI() throws URISyntaxException {
  263. return new URI("irc" + (ssl ? "s" : ""),
  264. password.orElse(""),
  265. hostname.orElse(""),
  266. port.orElse(6667),
  267. null, null, null);
  268. }
  269. }