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.

IdentitiesProfileMigrator.java 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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.interfaces.Migrator;
  19. import com.dmdirc.config.provider.ConfigProvider;
  20. import com.dmdirc.interfaces.config.IdentityController;
  21. import com.google.common.collect.Lists;
  22. import java.io.IOException;
  23. import java.util.List;
  24. import java.util.Optional;
  25. import javax.inject.Inject;
  26. /**
  27. * Migrates identity based performs to config based performs.
  28. */
  29. public class IdentitiesProfileMigrator implements Migrator {
  30. private static final String DOMAIN_PROFILE = "profile";
  31. private final IdentityController identityManager;
  32. private final ProfileManager profileManager;
  33. @Inject
  34. public IdentitiesProfileMigrator(final IdentityController identityManager,
  35. final ProfileManager profileManager) {
  36. this.identityManager = identityManager;
  37. this.profileManager = profileManager;
  38. }
  39. @Override
  40. public boolean needsMigration() {
  41. return !identityManager.getProvidersByType("profile").isEmpty();
  42. }
  43. @Override
  44. public void migrate() {
  45. final List<ConfigProvider> profiles = Lists.newArrayList(
  46. identityManager.getProvidersByType("profile"));
  47. for(ConfigProvider p : profiles) {
  48. final Optional<String> ident;
  49. if (p.hasOptionString("profile", "ident")) {
  50. ident = Optional.of(p.getOption(DOMAIN_PROFILE, "ident"));
  51. } else {
  52. ident = Optional.empty();
  53. }
  54. profileManager.addProfile(
  55. Profile.create(p.getName(), p.getOption(DOMAIN_PROFILE, "realname"), ident,
  56. p.getOptionList(DOMAIN_PROFILE, "nicknames")));
  57. try {
  58. p.delete();
  59. } catch (IOException e) {
  60. //Can't do anything, ignore
  61. }
  62. }
  63. }
  64. }