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.

CoreProfilesDialogModel.java 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*
  2. * Copyright (c) 2006-2014 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.profiles;
  23. import com.dmdirc.config.profiles.Profile;
  24. import com.dmdirc.interfaces.config.ConfigProvider;
  25. import com.dmdirc.interfaces.config.IdentityController;
  26. import com.dmdirc.interfaces.config.IdentityFactory;
  27. import com.dmdirc.interfaces.ui.ProfilesDialogModel;
  28. import com.dmdirc.interfaces.ui.ProfilesDialogModelListener;
  29. import com.dmdirc.util.collections.ListenerList;
  30. import com.dmdirc.util.validators.ListNotEmptyValidator;
  31. import com.dmdirc.util.validators.NotEmptyValidator;
  32. import com.dmdirc.util.validators.PermissiveValidator;
  33. import com.dmdirc.util.validators.Validator;
  34. import com.google.common.collect.ImmutableList;
  35. import com.google.common.collect.Iterables;
  36. import com.google.common.collect.Lists;
  37. import java.io.IOException;
  38. import java.util.HashMap;
  39. import java.util.List;
  40. import java.util.Map;
  41. import java.util.Optional;
  42. import javax.inject.Inject;
  43. import static com.google.common.base.Preconditions.checkArgument;
  44. import static com.google.common.base.Preconditions.checkNotNull;
  45. import static com.google.common.base.Preconditions.checkState;
  46. public class CoreProfilesDialogModel implements ProfilesDialogModel {
  47. private final ListenerList listeners;
  48. private final IdentityFactory identityFactory;
  49. private final IdentityController identityController;
  50. private final Map<String, Profile> profiles;
  51. private Optional<Profile> selectedProfile = Optional.empty();
  52. private Optional<String> name = Optional.empty();
  53. private Optional<List<String>> nicknames = Optional.empty();
  54. private Optional<String> selectedNickname = Optional.empty();
  55. private Optional<String> realname = Optional.empty();
  56. private Optional<String> ident = Optional.empty();
  57. @Inject
  58. public CoreProfilesDialogModel(final IdentityController identityController,
  59. final IdentityFactory identityFactory) {
  60. this.identityFactory = identityFactory;
  61. this.identityController = identityController;
  62. listeners = new ListenerList();
  63. profiles = new HashMap<>(0);
  64. selectedProfile = Optional.empty();
  65. }
  66. @Override
  67. public void loadModel() {
  68. profiles.clear();
  69. final List<ConfigProvider> identities = identityController.getProvidersByType("profile");
  70. for (ConfigProvider identity : identities) {
  71. final Profile profile = getProfile(identity);
  72. profiles.put(identity.getName(), profile);
  73. listeners.getCallable(ProfilesDialogModelListener.class).profileAdded(profile);
  74. }
  75. setSelectedProfile(Optional.ofNullable(Iterables.getFirst(profiles.values(), null)));
  76. }
  77. private Profile getProfile(final ConfigProvider configProvider) {
  78. final Profile newProfile = new Profile(configProvider.getName(), identityFactory);
  79. newProfile.setName(configProvider.getOption("identity", "name"));
  80. newProfile.setRealname(configProvider.getOption("profile", "realname"));
  81. newProfile.setIdent(configProvider.getOption("profile", "ident"));
  82. newProfile.setNicknames(configProvider.getOptionList("profile", "nicknames"));
  83. this.name = Optional.ofNullable(configProvider.getOption("identity", "name"));
  84. this.nicknames = Optional.ofNullable(configProvider.getOptionList("profile", "nicknames"));
  85. this.realname = Optional.ofNullable(configProvider.getOption("profile", "realname"));
  86. this.ident = Optional.ofNullable(configProvider.getOption("profile", "ident"));
  87. return newProfile;
  88. }
  89. @Override
  90. public List<Profile> getProfileList() {
  91. return ImmutableList.copyOf(profiles.values());
  92. }
  93. @Override
  94. public Optional<Profile> getProfile(final String name) {
  95. checkNotNull(name, "Name cannot be null");
  96. return Optional.ofNullable(profiles.get(name));
  97. }
  98. @Override
  99. public boolean isProfileListValid() {
  100. return !getProfileListValidator().validate(getProfileList()).isFailure();
  101. }
  102. @Override
  103. public Validator<List<Profile>> getProfileListValidator() {
  104. return new ListNotEmptyValidator<>();
  105. }
  106. @Override
  107. public void addProfile(final String name, final String realname, final String ident,
  108. final List<String> nicknames) {
  109. checkNotNull(name, "Name cannot be null");
  110. checkArgument(!profiles.containsKey(name), "Name cannot already exist");
  111. final Profile profile = new Profile(name, identityFactory);
  112. profile.setRealname(realname);
  113. profile.setIdent(ident);
  114. profile.setNicknames(Lists.newArrayList(nicknames));
  115. profiles.put(name, profile);
  116. listeners.getCallable(ProfilesDialogModelListener.class).profileAdded(profile);
  117. }
  118. @Override
  119. public void editProfile(final String name, final String realname, final String ident,
  120. final List<String> nicknames) {
  121. checkNotNull(name, "Name cannot be null");
  122. checkArgument(profiles.containsKey(name), "Name must already exist");
  123. final Profile profile = profiles.get(name);
  124. profile.setRealname(realname);
  125. profile.setIdent(ident);
  126. profile.setNicknames(Lists.newArrayList(nicknames));
  127. listeners.getCallable(ProfilesDialogModelListener.class).profileEdited(profile, profile);
  128. }
  129. @Override
  130. public void renameProfile(final String oldName, final String newName) {
  131. renameProfile(oldName, newName, false);
  132. }
  133. public void renameProfile(final String oldName, final String newName, final boolean selection) {
  134. checkNotNull(oldName, "Oldname cannot be null");
  135. checkNotNull(newName, "Newname cannot be null");
  136. checkArgument(profiles.containsKey(oldName), "Old name must exist");
  137. checkArgument(!profiles.containsKey(newName), "New name must not exist");
  138. final Profile profile = profiles.get(oldName);
  139. final Profile newProfile = new Profile(newName, identityFactory);
  140. profile.setRealname(profile.getRealname());
  141. profile.setIdent(profile.getIdent());
  142. profile.setNicknames(Lists.newArrayList(profile.getNicknames()));
  143. final Profile oldProfile = profiles.remove(oldName);
  144. profiles.put(newName, newProfile);
  145. listeners.getCallable(ProfilesDialogModelListener.class).profileRenamed(oldProfile,
  146. newProfile);
  147. }
  148. @Override
  149. public void removeProfile(final String name) {
  150. checkNotNull(name, "Name cannot be null");
  151. checkArgument(profiles.containsKey(name), "profile must exist in list");
  152. final Profile profile = profiles.remove(name);
  153. if (getSelectedProfile().isPresent() && getSelectedProfile().get().equals(profile)) {
  154. setSelectedProfile(Optional.<Profile>empty());
  155. }
  156. listeners.getCallable(ProfilesDialogModelListener.class).profileRemoved(profile);
  157. }
  158. @Override
  159. public void save() {
  160. setSelectedProfile(Optional.<Profile>empty());
  161. final List<ConfigProvider> identities = Lists.newArrayList(
  162. identityController.getProvidersByType("profile"));
  163. for (ConfigProvider identity : identities) {
  164. try {
  165. identity.delete();
  166. } catch (IOException ex) {
  167. //Can't handle and will be dealt with when profiles are redone.
  168. }
  169. }
  170. for (Profile profile : profiles.values()) {
  171. profile.save();
  172. }
  173. }
  174. @Override
  175. public void setSelectedProfile(final Optional<Profile> profile) {
  176. checkNotNull(profile, "profile cannot be null");
  177. if (profile.isPresent()) {
  178. checkArgument(profiles.containsValue(profile.get()), "Profile must exist in list");
  179. }
  180. if (selectedProfile.isPresent()) {
  181. if (!Optional.ofNullable(selectedProfile.get().getRealname()).equals(realname)
  182. || !Optional.ofNullable(selectedProfile.get().getIdent()).equals(ident)
  183. || !Optional.ofNullable(selectedProfile.get().getNicknames()).equals
  184. (nicknames)) {
  185. editProfile(selectedProfile.get().getName(), realname.get(),
  186. ident.get(), nicknames.get());
  187. }
  188. if (!Optional.ofNullable(selectedProfile.get().getName()).equals(name)) {
  189. renameProfile(selectedProfile.get().getName(), name.get(), true);
  190. }
  191. }
  192. selectedProfile = profile;
  193. if (selectedProfile.isPresent()) {
  194. name = Optional.ofNullable(selectedProfile.get().getName());
  195. realname = Optional.ofNullable(selectedProfile.get().getRealname());
  196. ident = Optional.ofNullable(selectedProfile.get().getIdent());
  197. nicknames = Optional.ofNullable(selectedProfile.get().getNicknames());
  198. } else {
  199. name = Optional.empty();
  200. realname = Optional.empty();
  201. ident = Optional.empty();
  202. nicknames = Optional.empty();
  203. }
  204. listeners.getCallable(ProfilesDialogModelListener.class).profileSelectionChanged(profile);
  205. }
  206. @Override
  207. public Optional<Profile> getSelectedProfile() {
  208. return selectedProfile;
  209. }
  210. @Override
  211. public Optional<String> getSelectedProfileName() {
  212. if (selectedProfile.isPresent()) {
  213. return name;
  214. }
  215. return Optional.empty();
  216. }
  217. @Override
  218. public void setSelectedProfileName(final Optional<String> name) {
  219. checkNotNull(name, "Name cannot be null");
  220. checkState(selectedProfile.isPresent(), "There must be a profile selected");
  221. this.name = name;
  222. listeners.getCallable(ProfilesDialogModelListener.class)
  223. .selectedProfileEdited(name, realname, ident, nicknames);
  224. }
  225. @Override
  226. public Validator<String> getSelectedProfileNameValidator() {
  227. return new EditSelectedProfileNameValidator(this);
  228. }
  229. @Override
  230. public Validator<String> getNewProfileNameValidator() {
  231. return new NewProfileNameValidator(this);
  232. }
  233. @Override
  234. public boolean isSelectedProfileNameValid() {
  235. return !getSelectedProfileName().isPresent() ||
  236. !getSelectedProfileNameValidator().validate(getSelectedProfileName().get())
  237. .isFailure();
  238. }
  239. @Override
  240. public Optional<String> getSelectedProfileRealname() {
  241. if (selectedProfile.isPresent()) {
  242. return realname;
  243. }
  244. return Optional.empty();
  245. }
  246. @Override
  247. public void setSelectedProfileRealname(final Optional<String> realname) {
  248. checkNotNull(realname, "Realname cannot be null");
  249. checkState(selectedProfile.isPresent(), "There must be a profile selected");
  250. this.realname = realname;
  251. listeners.getCallable(ProfilesDialogModelListener.class)
  252. .selectedProfileEdited(name, realname, ident, nicknames);
  253. }
  254. @Override
  255. public Validator<String> getSelectedProfileRealnameValidator() {
  256. return new NotEmptyValidator();
  257. }
  258. @Override
  259. public boolean isSelectedProfileRealnameValid() {
  260. return !getSelectedProfileRealname().isPresent() ||
  261. !getSelectedProfileRealnameValidator().validate(getSelectedProfileRealname().get())
  262. .isFailure();
  263. }
  264. @Override
  265. public Optional<String> getSelectedProfileIdent() {
  266. if (selectedProfile.isPresent()) {
  267. return ident;
  268. }
  269. return Optional.empty();
  270. }
  271. @Override
  272. public void setSelectedProfileIdent(final Optional<String> ident) {
  273. checkNotNull(ident, "Ident cannot be null");
  274. checkState(selectedProfile.isPresent(), "There must be a profile selected");
  275. this.ident = ident;
  276. listeners.getCallable(ProfilesDialogModelListener.class)
  277. .selectedProfileEdited(name, realname, ident, nicknames);
  278. }
  279. @Override
  280. public Validator<String> getSelectedProfileIdentValidator() {
  281. return new PermissiveValidator<>();
  282. }
  283. @Override
  284. public boolean isSelectedProfileIdentValid() {
  285. return !getSelectedProfileIdent().isPresent() ||
  286. !getSelectedProfileIdentValidator().validate(getSelectedProfileIdent().get())
  287. .isFailure();
  288. }
  289. @Override
  290. public Optional<List<String>> getSelectedProfileNicknames() {
  291. if (selectedProfile.isPresent()) {
  292. return nicknames;
  293. }
  294. return Optional.empty();
  295. }
  296. @Override
  297. public void setSelectedProfileNicknames(final Optional<List<String>> nicknames) {
  298. checkNotNull(nicknames, "nicknames cannot be null");
  299. checkState(selectedProfile.isPresent(), "There must be a profile selected");
  300. if (nicknames.isPresent()) {
  301. this.nicknames = nicknames;
  302. } else {
  303. this.nicknames = Optional.ofNullable((List<String>) Lists.
  304. newArrayList(nicknames.get()));
  305. }
  306. listeners.getCallable(ProfilesDialogModelListener.class)
  307. .selectedProfileEdited(name, realname, ident, nicknames);
  308. }
  309. @Override
  310. public Validator<List<String>> getSelectedProfileNicknamesValidator() {
  311. return new ListNotEmptyValidator<>();
  312. }
  313. @Override
  314. public boolean isSelectedProfileNicknamesValid() {
  315. return !getSelectedProfileNicknames().isPresent() || !getSelectedProfileNicknamesValidator()
  316. .validate(getSelectedProfileNicknames().get()).isFailure();
  317. }
  318. @Override
  319. public void addSelectedProfileNickname(final String nickname) {
  320. checkNotNull(nickname, "Nickname cannot be null");
  321. checkState(selectedProfile.isPresent(), "There must be a profile selected");
  322. checkState(nicknames.isPresent(), "There must be nicknames present");
  323. checkArgument(!nicknames.get().contains(nickname), "New nickname must not exist");
  324. nicknames.get().add(nickname);
  325. listeners.getCallable(ProfilesDialogModelListener.class).selectedProfileNicknameAdded(
  326. nickname);
  327. }
  328. @Override
  329. public void removeSelectedProfileNickname(final String nickname) {
  330. checkNotNull(nickname, "Nickname cannot be null");
  331. checkState(selectedProfile.isPresent(), "There must be a profile selected");
  332. checkState(nicknames.isPresent(), "There must be nicknames present");
  333. checkArgument(nicknames.get().contains(nickname), "Nickname must exist");
  334. nicknames.get().remove(nickname);
  335. listeners.getCallable(ProfilesDialogModelListener.class).selectedProfileNicknameRemoved(
  336. nickname);
  337. }
  338. @Override
  339. public Validator<String> getSelectedProfileAddNicknameValidator() {
  340. return new AddNicknameValidator(this);
  341. }
  342. @Override
  343. public void editSelectedProfileNickname(final String oldName, final String newName) {
  344. checkNotNull(oldName, "Nickname cannot be null");
  345. checkNotNull(newName, "Nickname cannot be null");
  346. checkState(selectedProfile.isPresent(), "There must be a profile selected");
  347. checkState(nicknames.isPresent(), "There must be nicknames present");
  348. checkArgument(nicknames.get().contains(oldName), "Old nickname must exist");
  349. checkArgument(!nicknames.get().contains(newName), "New nickname must not exist");
  350. final int index = nicknames.get().indexOf(oldName);
  351. nicknames.get().set(index, newName);
  352. listeners.getCallable(ProfilesDialogModelListener.class).selectedProfileNicknameEdited(
  353. oldName, newName);
  354. }
  355. @Override
  356. public Validator<String> getSelectedProfileEditNicknameValidator() {
  357. return new EditSelectedNicknameValidator(this);
  358. }
  359. @Override
  360. public Optional<String> getSelectedProfileSelectedNickname() {
  361. if (selectedProfile.isPresent()) {
  362. return selectedNickname;
  363. }
  364. return Optional.empty();
  365. }
  366. @Override
  367. public void setSelectedProfileSelectedNickname(final Optional<String> selectedNickname) {
  368. checkNotNull(selectedNickname, "Nickname cannot be null");
  369. checkState(selectedProfile.isPresent(), "There must be a profile selected");
  370. checkState(nicknames.isPresent(), "There must be nicknames present");
  371. if (selectedNickname.isPresent()) {
  372. checkArgument(nicknames.get().contains(selectedNickname.get()),
  373. "Nickname must exist in nicknames list");
  374. }
  375. this.selectedNickname = selectedNickname;
  376. listeners.getCallable(ProfilesDialogModelListener.class)
  377. .selectedNicknameChanged(selectedNickname);
  378. }
  379. @Override
  380. public void addListener(final ProfilesDialogModelListener listener) {
  381. checkNotNull(listener, "Listener must not be null");
  382. listeners.add(ProfilesDialogModelListener.class, listener);
  383. }
  384. @Override
  385. public void removeListener(final ProfilesDialogModelListener listener) {
  386. checkNotNull(listener, "Listener must not be null");
  387. listeners.remove(ProfilesDialogModelListener.class, listener);
  388. }
  389. @Override
  390. public boolean canSwitchProfiles() {
  391. return !selectedProfile.isPresent() ||
  392. isSelectedProfileIdentValid() && isSelectedProfileNameValid() &&
  393. isSelectedProfileNicknamesValid() && isSelectedProfileRealnameValid();
  394. }
  395. @Override
  396. public boolean isSaveAllowed() {
  397. return isProfileListValid()
  398. && isSelectedProfileIdentValid()
  399. && isSelectedProfileNameValid()
  400. && isSelectedProfileNicknamesValid()
  401. && isSelectedProfileRealnameValid();
  402. }
  403. }