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.

Profile.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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.actions.wrappers;
  23. import com.dmdirc.interfaces.config.ConfigProvider;
  24. import com.dmdirc.interfaces.config.IdentityFactory;
  25. import com.google.common.base.Preconditions;
  26. import java.io.IOException;
  27. import java.util.ArrayList;
  28. import java.util.Arrays;
  29. import java.util.List;
  30. import java.util.Objects;
  31. import javax.annotation.Nonnull;
  32. /**
  33. * Profile wrapper class.
  34. */
  35. public class Profile {
  36. /** Identity backing this profile. */
  37. private ConfigProvider configProvider;
  38. /** Profile Name, must be a sanitised filename. */
  39. private String name;
  40. /** Real name. */
  41. private String realname;
  42. /** Ident. */
  43. private String ident;
  44. /** Nicknames. */
  45. private List<String> nicknames;
  46. /** Has this profile been marked deleted? */
  47. private boolean deleted;
  48. /** Factory to use to create profiles when saving. */
  49. private final IdentityFactory identityFactory;
  50. /**
  51. * Creates a new profile.
  52. *
  53. * @param name Profile name
  54. * @param identityFactory The factory to use to create the profile's config file when saving.
  55. */
  56. public Profile(final String name, final IdentityFactory identityFactory) {
  57. this.identityFactory = identityFactory;
  58. this.configProvider = null;
  59. this.name = name;
  60. this.nicknames = new ArrayList<>(Arrays.asList(name.replaceAll(" ", "")));
  61. this.realname = name;
  62. this.ident = "";
  63. }
  64. /**
  65. * Creates a new profile based off the specified Identity.
  66. *
  67. * @param identityFactory The factory to use to create the profile's config file when saving.
  68. * @param configProvider Provider to read existing profile from.
  69. */
  70. public Profile(final IdentityFactory identityFactory,
  71. @Nonnull final ConfigProvider configProvider) {
  72. Preconditions.checkNotNull(configProvider);
  73. this.identityFactory = identityFactory;
  74. this.configProvider = configProvider;
  75. this.name = configProvider.getOption("identity", "name");
  76. this.nicknames = configProvider.getOptionList("profile", "nicknames");
  77. this.realname = configProvider.getOption("profile", "realname");
  78. this.ident = configProvider.getOption("profile", "ident");
  79. }
  80. public String getName() {
  81. return name;
  82. }
  83. public void setName(final String name) {
  84. this.name = name;
  85. }
  86. public String getRealname() {
  87. return realname;
  88. }
  89. public void setRealname(final String realname) {
  90. this.realname = realname;
  91. }
  92. public String getIdent() {
  93. return ident;
  94. }
  95. public void setIdent(final String ident) {
  96. this.ident = ident;
  97. }
  98. public List<String> getNicknames() {
  99. return nicknames;
  100. }
  101. public void setNicknames(final List<String> nicknames) {
  102. this.nicknames = nicknames;
  103. }
  104. public boolean isDeleted() {
  105. return deleted;
  106. }
  107. public void setDeleted(final boolean deleted) {
  108. this.deleted = deleted;
  109. }
  110. /**
  111. * Adds a nickname to this profile.
  112. *
  113. * @param nickname A new nickname for the profile
  114. */
  115. public void addNickname(final String nickname) {
  116. if (!nicknames.contains(nickname)) {
  117. nicknames.add(nickname);
  118. }
  119. }
  120. /**
  121. * Adds a nickname to this profile.
  122. *
  123. * @param nickname A new nickname for the profile
  124. * @param position Position for the new alternate nickname
  125. */
  126. public void addNickname(final String nickname, final int position) {
  127. if (!nicknames.contains(nickname)) {
  128. nicknames.add(position, nickname);
  129. }
  130. }
  131. /**
  132. * Deletes a nickname from this profile.
  133. *
  134. * @param nickname An existing nickname from the profile
  135. */
  136. public void delNickname(final String nickname) {
  137. nicknames.remove(nickname);
  138. }
  139. /**
  140. * Edits a nickname in the list.
  141. *
  142. * @param nickname Nickname to edit
  143. * @param newNickname Edited nickname
  144. */
  145. public void editNickname(final String nickname, final String newNickname) {
  146. if (nickname.isEmpty() || newNickname.isEmpty()) {
  147. return;
  148. }
  149. if (!nickname.equals(newNickname)) {
  150. final int index = nicknames.indexOf(nickname);
  151. nicknames.remove(nickname);
  152. nicknames.add(index, newNickname);
  153. }
  154. }
  155. /** Saves this profile. */
  156. public void save() {
  157. if (configProvider == null) {
  158. configProvider = identityFactory.createProfileConfig(name);
  159. }
  160. configProvider.setOption("identity", "name", name);
  161. configProvider.setOption("profile", "nicknames", nicknames);
  162. configProvider.setOption("profile", "realname", realname);
  163. configProvider.setOption("profile", "ident", ident);
  164. }
  165. /**
  166. * Deletes the profile.
  167. *
  168. * @throws IOException if unable to delete the profile
  169. */
  170. public void delete() throws IOException {
  171. if (configProvider == null) {
  172. return;
  173. }
  174. configProvider.delete();
  175. }
  176. @Override
  177. public int hashCode() {
  178. int hash = 7;
  179. hash = 59 * hash + Objects.hashCode(this.name);
  180. hash = 59 * hash + Objects.hashCode(this.realname);
  181. hash = 59 * hash + Objects.hashCode(this.ident);
  182. hash = 59 * hash + Objects.hashCode(this.nicknames);
  183. return hash;
  184. }
  185. @Override
  186. public boolean equals(final Object obj) {
  187. if (obj == null) {
  188. return false;
  189. }
  190. if (getClass() != obj.getClass()) {
  191. return false;
  192. }
  193. final Profile other = (Profile) obj;
  194. return Objects.equals(this.name, other.name) &&
  195. Objects.equals(this.realname, other.realname) &&
  196. Objects.equals(this.ident, other.ident) &&
  197. Objects.equals(this.nicknames, other.nicknames);
  198. }
  199. @Override
  200. public String toString() {
  201. return "Profile{" + "name=" + name + ", realname=" + realname
  202. + ", ident=" + ident + ", nicknames=" + nicknames + ", deleted=" + deleted + '}';
  203. }
  204. }