Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

CoreNewServerDialogModel.java 11KB

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