Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ProfileManager.java 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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.config.profiles;
  18. import com.dmdirc.events.ProfileAddedEvent;
  19. import com.dmdirc.events.ProfileDeletedEvent;
  20. import com.dmdirc.events.eventbus.EventBus;
  21. import com.dmdirc.util.SystemInfo;
  22. import com.google.common.collect.Iterables;
  23. import com.google.common.collect.Lists;
  24. import java.util.ArrayList;
  25. import java.util.Collection;
  26. import java.util.Collections;
  27. import java.util.List;
  28. import java.util.Optional;
  29. import javax.inject.Inject;
  30. import javax.inject.Singleton;
  31. /**
  32. * Manager for {@link Profile}s.
  33. */
  34. @Singleton
  35. public class ProfileManager {
  36. private final EventBus eventBus;
  37. private final List<Profile> profiles;
  38. private final Profile defaultProfile;
  39. @Inject
  40. public ProfileManager(final EventBus eventBus, final SystemInfo systemInfo) {
  41. this.eventBus = eventBus;
  42. profiles = new ArrayList<>();
  43. final String nick = systemInfo.getProperty("user.name").replace(' ', '_');
  44. defaultProfile = Profile.create(nick, nick, Optional.empty(), Lists.newArrayList(nick));
  45. }
  46. /**
  47. * Adds a profile to the manager.
  48. *
  49. * @param profile New profile to add
  50. */
  51. public void addProfile(final Profile profile) {
  52. profiles.add(profile);
  53. eventBus.publishAsync(new ProfileAddedEvent(profile));
  54. }
  55. /**
  56. * Removes a profile from the manager.
  57. *
  58. * @param profile Profile to be removed
  59. */
  60. public void deleteProfile(final Profile profile) {
  61. profiles.remove(profile);
  62. eventBus.publishAsync(new ProfileDeletedEvent(profile));
  63. }
  64. /**
  65. * Returns the available profiles in the client.
  66. *
  67. * @return Unmodifiable collection of profiles
  68. */
  69. public Collection<Profile> getProfiles() {
  70. return Collections.unmodifiableCollection(profiles);
  71. }
  72. /**
  73. * Returns the default profile if no custom profiles have been set.
  74. *
  75. * @return Default profile
  76. */
  77. public Profile getDefault() {
  78. return Iterables.getFirst(profiles, defaultProfile);
  79. }
  80. }