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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.ui.core.newserver;
  18. import com.dmdirc.config.GlobalConfig;
  19. import com.dmdirc.config.UserConfig;
  20. import com.dmdirc.config.profiles.Profile;
  21. import com.dmdirc.config.profiles.ProfileManager;
  22. import com.dmdirc.events.ProfileAddedEvent;
  23. import com.dmdirc.events.ProfileDeletedEvent;
  24. import com.dmdirc.interfaces.ConnectionManager;
  25. import com.dmdirc.events.eventbus.EventBus;
  26. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  27. import com.dmdirc.interfaces.config.ConfigProvider;
  28. import com.dmdirc.interfaces.ui.NewServerDialogModel;
  29. import com.dmdirc.interfaces.ui.NewServerDialogModelListener;
  30. import com.dmdirc.util.collections.ListenerList;
  31. import com.dmdirc.util.validators.*;
  32. import com.google.common.collect.ImmutableList;
  33. import net.engio.mbassy.listener.Handler;
  34. import javax.inject.Inject;
  35. import java.net.URI;
  36. import java.net.URISyntaxException;
  37. import java.util.ArrayList;
  38. import java.util.List;
  39. import java.util.Optional;
  40. import static com.google.common.base.Preconditions.checkNotNull;
  41. /**
  42. * Default implementation of a new server dialog model.
  43. */
  44. public class CoreNewServerDialogModel implements NewServerDialogModel {
  45. private final ListenerList listeners;
  46. private final AggregateConfigProvider globalConfig;
  47. private final ConfigProvider userConfig;
  48. private final ConnectionManager connectionManager;
  49. private final ProfileManager profileManager;
  50. private final List<Profile> profiles;
  51. private final EventBus eventBus;
  52. private Optional<Profile> selectedProfile;
  53. private Optional<String> hostname;
  54. private Optional<Integer> port;
  55. private Optional<String> password;
  56. private boolean ssl;
  57. private boolean saveAsDefault;
  58. @Inject
  59. public CoreNewServerDialogModel(
  60. @GlobalConfig final AggregateConfigProvider globalConfig,
  61. @UserConfig final ConfigProvider userConfig,
  62. final ProfileManager profileManager,
  63. final ConnectionManager connectionManager,
  64. final EventBus eventBus) {
  65. this.globalConfig = globalConfig;
  66. this.userConfig = userConfig;
  67. this.profileManager = profileManager;
  68. this.connectionManager = connectionManager;
  69. this.eventBus = eventBus;
  70. listeners = new ListenerList();
  71. profiles = new ArrayList<>(5);
  72. selectedProfile = Optional.empty();
  73. hostname = Optional.empty();
  74. port = Optional.empty();
  75. password = Optional.empty();
  76. ssl = false;
  77. saveAsDefault = false;
  78. }
  79. @Override
  80. public void loadModel() {
  81. eventBus.subscribe(this);
  82. profiles.addAll(profileManager.getProfiles());
  83. hostname = Optional.ofNullable(globalConfig.getOption("newserver", "hostname"));
  84. port = Optional.ofNullable(globalConfig.getOptionInt("newserver", "port"));
  85. password = Optional.ofNullable(globalConfig.getOption("newserver", "password"));
  86. ssl = globalConfig.getOptionBool("newserver", "ssl");
  87. saveAsDefault = false;
  88. listeners.getCallable(NewServerDialogModelListener.class).serverDetailsChanged(hostname,
  89. port, password, ssl, saveAsDefault);
  90. }
  91. @Override
  92. public List<Profile> getProfileList() {
  93. return ImmutableList.copyOf(profiles);
  94. }
  95. @Override
  96. public Optional<Profile> getSelectedProfile() {
  97. return selectedProfile;
  98. }
  99. @Override
  100. public void setSelectedProfile(final Optional<Profile> selectedProfile) {
  101. checkNotNull(selectedProfile);
  102. final Optional<Profile> oldSelectedProfile = this.selectedProfile;
  103. this.selectedProfile = selectedProfile;
  104. listeners.getCallable(NewServerDialogModelListener.class).selectedProfileChanged(
  105. oldSelectedProfile, selectedProfile);
  106. }
  107. @Override
  108. public boolean isProfileListValid() {
  109. return !getProfileListValidator().validate(getProfileList()).isFailure();
  110. }
  111. @Override
  112. public Validator<List<Profile>> getProfileListValidator() {
  113. return new ListNotEmptyValidator<>();
  114. }
  115. @Override
  116. public Optional<String> getHostname() {
  117. return hostname;
  118. }
  119. @Override
  120. public void setHostname(final Optional<String> hostname) {
  121. checkNotNull(hostname);
  122. this.hostname = hostname;
  123. listeners.getCallable(NewServerDialogModelListener.class).serverDetailsChanged(hostname,
  124. port, password, ssl, saveAsDefault);
  125. }
  126. @Override
  127. public boolean isHostnameValid() {
  128. return getHostname().isPresent() && !getHostnameValidator().validate(getHostname().get()).
  129. isFailure();
  130. }
  131. @Override
  132. public Validator<String> getHostnameValidator() {
  133. return new ServerNameValidator();
  134. }
  135. @Override
  136. public Optional<Integer> getPort() {
  137. return port;
  138. }
  139. @Override
  140. public void setPort(final Optional<Integer> port) {
  141. checkNotNull(port);
  142. this.port = port;
  143. listeners.getCallable(NewServerDialogModelListener.class).serverDetailsChanged(hostname,
  144. port, password, ssl, saveAsDefault);
  145. }
  146. @Override
  147. public boolean isPortValid() {
  148. return getPort().isPresent() && !getPortValidator().validate(getPort().get()).isFailure();
  149. }
  150. @Override
  151. public Validator<Integer> getPortValidator() {
  152. return new IntegerPortValidator();
  153. }
  154. @Override
  155. public Optional<String> getPassword() {
  156. return password;
  157. }
  158. @Override
  159. public void setPassword(final Optional<String> password) {
  160. checkNotNull(password);
  161. this.password = password;
  162. listeners.getCallable(NewServerDialogModelListener.class).serverDetailsChanged(hostname,
  163. port, password, ssl, saveAsDefault);
  164. }
  165. @Override
  166. public boolean isPasswordValid() {
  167. return !getPassword().isPresent() || !getPasswordValidator().validate(getPassword().get()).
  168. isFailure();
  169. }
  170. @Override
  171. public Validator<String> getPasswordValidator() {
  172. return new PermissiveValidator<>();
  173. }
  174. @Override
  175. public boolean getSSL() {
  176. return ssl;
  177. }
  178. @Override
  179. public void setSSL(final boolean ssl) {
  180. this.ssl = ssl;
  181. listeners.getCallable(NewServerDialogModelListener.class).serverDetailsChanged(hostname,
  182. port, password, ssl, saveAsDefault);
  183. }
  184. @Override
  185. public boolean getSaveAsDefault() {
  186. return saveAsDefault;
  187. }
  188. @Override
  189. public void setSaveAsDefault(final boolean saveAsDefault) {
  190. this.saveAsDefault = saveAsDefault;
  191. listeners.getCallable(NewServerDialogModelListener.class).serverDetailsChanged(hostname,
  192. port, password, ssl, saveAsDefault);
  193. }
  194. @Override
  195. public void save() {
  196. if (saveAsDefault) {
  197. userConfig.
  198. setOption("newserver", "hostname", hostname.isPresent() ? hostname.get() : "");
  199. userConfig.setOption("newserver", "port", port.isPresent() ? port.get() : 6667);
  200. userConfig.setOption("newserver", "password",
  201. password.isPresent() ? password.get() : "");
  202. userConfig.setOption("newserver", "ssl", ssl);
  203. }
  204. try {
  205. if (selectedProfile.isPresent()) {
  206. connectionManager.connectToAddress(getServerURI(), selectedProfile.get());
  207. } else {
  208. connectionManager.connectToAddress(getServerURI());
  209. }
  210. } catch (URISyntaxException ex) {
  211. //This is tested in isSaveAllowed, shouldn't happen here.
  212. }
  213. }
  214. @Override
  215. public boolean isSaveAllowed() {
  216. try {
  217. getServerURI();
  218. } catch (URISyntaxException ex) {
  219. return false;
  220. }
  221. return isHostnameValid() && isPortValid() && isPasswordValid() && isProfileListValid();
  222. }
  223. @Override
  224. public void addListener(final NewServerDialogModelListener listener) {
  225. checkNotNull(listener);
  226. listeners.add(NewServerDialogModelListener.class, listener);
  227. }
  228. @Override
  229. public void removeListener(final NewServerDialogModelListener listener) {
  230. checkNotNull(listener);
  231. listeners.remove(NewServerDialogModelListener.class, listener);
  232. }
  233. @Handler
  234. public void profileAdded(final ProfileAddedEvent event) {
  235. profiles.add(event.getProfile());
  236. listeners.getCallable(NewServerDialogModelListener.class).profileListChanged(
  237. ImmutableList.copyOf(profiles));
  238. }
  239. @Handler
  240. public void profileDeleted(final ProfileDeletedEvent event) {
  241. if (Optional.of(event.getProfile()).equals(selectedProfile)) {
  242. selectedProfile = Optional.empty();
  243. listeners.getCallable(NewServerDialogModelListener.class).selectedProfileChanged(
  244. Optional.of(event.getProfile()), selectedProfile);
  245. }
  246. profiles.remove(event.getProfile());
  247. listeners.getCallable(NewServerDialogModelListener.class).profileListChanged(
  248. ImmutableList.copyOf(profiles));
  249. }
  250. /**
  251. * Gets the URI for the details in the dialog.
  252. *
  253. * @return Returns the URI the details represent
  254. *
  255. * @throws URISyntaxException If the resulting URI is invalid
  256. */
  257. private URI getServerURI() throws URISyntaxException {
  258. return new URI("irc" + (ssl ? "s" : ""),
  259. password.orElse(""),
  260. hostname.orElse(""),
  261. port.orElse(6667),
  262. null, null, null);
  263. }
  264. }