Browse Source

Issue 1703: Construct new identities with maps not properties


git-svn-id: http://svn.dmdirc.com/trunk@4826 00569f92-eb28-0410-84fd-f71c24880f
tags/0.6.3m1rc1
Chris Smith 16 years ago
parent
commit
8aad6231ed
1 changed files with 38 additions and 27 deletions
  1. 38
    27
      src/com/dmdirc/config/Identity.java

+ 38
- 27
src/com/dmdirc/config/Identity.java View File

@@ -576,35 +576,40 @@ public class Identity extends ConfigSource implements Serializable,
576 576
     /**
577 577
      * Creates a new identity containing the specified properties.
578 578
      *
579
-     * @param properties The properties to be included in the identity
579
+     * @param settings The settings to populate the identity with
580 580
      * @return A new identity containing the specified properties
581 581
      */
582
-    protected static Identity createIdentity(final Properties properties) {
583
-        if (!properties.containsKey(DOMAIN + ".name")
584
-                || properties.getProperty(DOMAIN + ".name").isEmpty()) {
585
-            Logger.appError(ErrorLevel.LOW, "createIdentity caleld with invalid identity",
582
+    protected static Identity createIdentity(final Map<String, Map<String, String>> settings) {
583
+        if (!settings.containsKey(DOMAIN) || !settings.get(DOMAIN).containsKey("name")
584
+                || settings.get(DOMAIN).get("name").isEmpty()) {
585
+            Logger.appError(ErrorLevel.LOW, "createIdentity called with invalid identity",
586 586
                     new InvalidIdentityFileException("identity.name is not set"));
587 587
             return null;
588 588
         }
589 589
 
590 590
         final String fs = System.getProperty("file.separator");
591 591
         final String location = Main.getConfigDir() + "identities" + fs;
592
-        final String name = properties.getProperty(DOMAIN + ".name");
592
+        final String name = settings.get(DOMAIN).get("name");
593 593
 
594
-        final File file = new File(location + name);
594
+        File file = new File(location + name);
595
+        int attempt = 0;
595 596
 
596
-        if (!file.exists()) {
597
-            final FileWriter writer;
597
+        while (file.exists()) {
598
+            file = new File(location + name + "-" + ++attempt);
599
+        }
598 600
 
599
-            try {
600
-                writer = new FileWriter(location + name);
601
-                properties.store(writer, "");
602
-                writer.close();
603
-            } catch (IOException ex) {
604
-                Logger.userError(ErrorLevel.MEDIUM,
605
-                        "Unable to write new identity file: " + ex.getMessage());
606
-                return null;
607
-            }
601
+        final ConfigFile configFile = new ConfigFile(file);
602
+
603
+        for (Map.Entry<String, Map<String, String>> entry : settings.entrySet()) {
604
+            configFile.addDomain(entry.getKey(), entry.getValue());
605
+        }
606
+
607
+        try {
608
+            configFile.write();
609
+        } catch (IOException ex) {
610
+            Logger.userError(ErrorLevel.MEDIUM,
611
+                    "Unable to write new identity file: " + ex.getMessage());
612
+            return null;
608 613
         }
609 614
 
610 615
         try {
@@ -634,11 +639,13 @@ public class Identity extends ConfigSource implements Serializable,
634 639
      * @return An empty identity for the specified target
635 640
      */
636 641
     public static Identity buildIdentity(final ConfigTarget target) {
637
-        final Properties properties = new Properties();
638
-        properties.setProperty(DOMAIN + ".name", target.getData());
639
-        properties.setProperty(DOMAIN + "." + target.getTypeName(), target.getData());
642
+        final Map<String, Map<String, String>> settings
643
+                = new HashMap<String, Map<String, String>>();
644
+        settings.put(DOMAIN, new HashMap<String, String>(2));
645
+        settings.get(DOMAIN).put("name", target.getData());
646
+        settings.get(DOMAIN).put(target.getTypeName(), target.getData());
640 647
 
641
-        return createIdentity(properties);
648
+        return createIdentity(settings);
642 649
     }
643 650
 
644 651
     /**
@@ -649,14 +656,18 @@ public class Identity extends ConfigSource implements Serializable,
649 656
      * @return A new profile with the specified name
650 657
      */
651 658
     public static Identity buildProfile(final String name) {
652
-        final Properties properties = new Properties();
659
+        final Map<String, Map<String, String>> settings
660
+                = new HashMap<String, Map<String, String>>();
661
+        settings.put(DOMAIN, new HashMap<String, String>(1));
662
+        settings.put("profile", new HashMap<String, String>(2));
663
+
653 664
         final String nick = System.getProperty("user.name").replace(' ', '_');
654 665
         
655
-        properties.setProperty(DOMAIN + ".name", name);
656
-        properties.setProperty("profile.nickname", nick);
657
-        properties.setProperty("profile.realname", nick);
666
+        settings.get(DOMAIN).put("name", name);
667
+        settings.get("profile").put("nickname", nick);
668
+        settings.get("profile").put("realname", nick);
658 669
 
659
-        return createIdentity(properties);
670
+        return createIdentity(settings);
660 671
     }
661 672
 
662 673
 }

Loading…
Cancel
Save