Ticket #7027: preferences.xml.patch

File preferences.xml.patch, 5.7 KB (added by bastiK, 14 years ago)
  • src/org/openstreetmap/josm/data/Preferences.java

     
    7474    protected final SortedMap<String, String> defaults = new TreeMap<String, String>();
    7575    protected final SortedMap<String, String> colornames = new TreeMap<String, String>();
    7676
    77     /* NOTE: FIXME: Remove when saving XML enabled */
    78     private boolean loadedXML = true;
    79 
    8077    public interface PreferenceChangeEvent{
    8178        String getKey();
    8279        String getOldValue();
     
    163160    }
    164161
    165162    public File getPreferenceFile() {
     163        return new File(getPreferencesDirFile(), "preferences.xml");
     164    }
     165
     166    public File getOldPreferenceFile() {
    166167        return new File(getPreferencesDirFile(), "preferences");
    167168    }
    168169
     
    387388
    388389        final PrintWriter out = new PrintWriter(new OutputStreamWriter(
    389390                new FileOutputStream(prefFile + "_tmp"), "utf-8"), false);
    390         /* FIXME: NOTE: loadedXML - removed 01.12.2011 */
    391         if(loadedXML) {
    392             out.print(toXML(false));
    393         } else {
    394             for (final Entry<String, String> e : properties.entrySet()) {
    395                 String s = defaults.get(e.getKey());
    396                 /* don't save default values */
    397                 if(s == null || !s.equals(e.getValue())) {
    398                     out.println(e.getKey() + "=" + e.getValue());
    399                 }
    400               }
    401         }
     391        out.print(toXML(false));
    402392        out.close();
    403393
    404394        File tmpFile = new File(prefFile + "_tmp");
     
    445435        }
    446436    }
    447437
     438    public void loadOld() throws Exception {
     439        load(true);
     440    }
     441
    448442    public void load() throws Exception {
     443        load(false);
     444    }
     445
     446    private void load(boolean old) throws Exception {
    449447        properties.clear();
    450448        if(!Main.applet) {
    451449            final BufferedReader in = new BufferedReader(new InputStreamReader(
    452                     new FileInputStream(getPreferencesDir()+"preferences"), "utf-8"));
     450                    new FileInputStream(old ? getOldPreferenceFile() : getPreferenceFile()), "utf-8"));
    453451            /* FIXME: TODO: remove old style config file end of 2012 */
    454452            try {
    455                 in.mark(1);
    456                 int v = in.read();
    457                 in.reset();
    458                 if(v == '<') {
    459                     fromXML(in);
    460                 } else {
    461                     loadedXML = false;
     453                if (old) {
    462454                    int lineNumber = 0;
    463455                    ArrayList<Integer> errLines = new ArrayList<Integer>();
    464456                    for (String line = in.readLine(); line != null; line = in.readLine(), lineNumber++) {
     
    475467                    }
    476468                    if (!errLines.isEmpty())
    477469                        throw new IOException(tr("Malformed config file at lines {0}", errLines));
     470                } else {
     471                    fromXML(in);
    478472                }
    479473            } finally {
    480474                in.close();
     
    525519        File preferenceFile = getPreferenceFile();
    526520        try {
    527521            if (!preferenceFile.exists()) {
    528                 System.out.println(tr("Warning: Missing preference file ''{0}''. Creating a default preference file.", preferenceFile.getAbsoluteFile()));
    529                 resetToDefault();
    530                 save();
     522                File oldPreferenceFile = getOldPreferenceFile();
     523                if (!oldPreferenceFile.exists()) {
     524                    System.out.println(tr("Warning: Missing preference file ''{0}''. Creating a default preference file.", preferenceFile.getAbsoluteFile()));
     525                    resetToDefault();
     526                    save();
     527                } else {
     528                    try {
     529                        loadOld();
     530                    } catch (Exception e) {
     531                        e.printStackTrace();
     532                        File backupFile = new File(prefDir,"preferences.bak");
     533                        JOptionPane.showMessageDialog(
     534                                Main.parent,
     535                                tr("<html>Preferences file had errors.<br> Making backup of old one to <br>{0}<br> and creating a new default preference file.</html>", backupFile.getAbsoluteFile()),
     536                                tr("Error"),
     537                                JOptionPane.ERROR_MESSAGE
     538                        );
     539                        Main.platform.rename(oldPreferenceFile, backupFile);
     540                        try {
     541                            resetToDefault();
     542                            save();
     543                        } catch(IOException e1) {
     544                            e1.printStackTrace();
     545                            System.err.println(tr("Warning: Failed to initialize preferences. Failed to reset preference file to default: {0}", getPreferenceFile()));
     546                        }
     547                    }
     548                    return;
     549                }
    531550            } else if (reset) {
    532551                System.out.println(tr("Warning: Replacing existing preference file ''{0}'' with default preference file.", preferenceFile.getAbsoluteFile()));
    533552                resetToDefault();
     
    547566            load();
    548567        } catch (Exception e) {
    549568            e.printStackTrace();
    550             File backupFile = new File(prefDir,"preferences.bak");
     569            File backupFile = new File(prefDir,"preferences.xml.bak");
    551570            JOptionPane.showMessageDialog(
    552571                    Main.parent,
    553572                    tr("<html>Preferences file had errors.<br> Making backup of old one to <br>{0}<br> and creating a new default preference file.</html>", backupFile.getAbsoluteFile()),