1998-09-09 00:52:38 +00:00
|
|
|
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil -*-
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License
|
|
|
|
* Version 1.0 (the "License"); you may not use this file except in
|
|
|
|
* compliance with the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS"
|
|
|
|
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
|
|
|
|
* the License for the specific language governing rights and limitations
|
|
|
|
* under the License.
|
|
|
|
*
|
|
|
|
* The Original Code is the Grendel mail/news client.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape Communications
|
|
|
|
* Corporation. Portions created by Netscape are Copyright (C) 1997
|
|
|
|
* Netscape Communications Corporation. All Rights Reserved.
|
1999-06-15 11:44:40 +00:00
|
|
|
*
|
|
|
|
* Contributors: Edwin Woudt <edwin@woudt.nl>
|
1998-09-09 00:52:38 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package calypso.util;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.io.IOException;
|
1999-06-16 05:15:31 +00:00
|
|
|
import java.io.RandomAccessFile;
|
1998-09-09 00:52:38 +00:00
|
|
|
import java.util.Properties;
|
|
|
|
import java.util.MissingResourceException;
|
|
|
|
import java.util.Enumeration;
|
|
|
|
|
1999-06-15 11:44:40 +00:00
|
|
|
import grendel.util.Constants;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Contains the File handling logic of the prefs.
|
|
|
|
*/
|
|
|
|
|
1998-09-09 00:52:38 +00:00
|
|
|
class PreferencesBase extends Properties implements Preferences {
|
|
|
|
|
1999-06-15 11:44:40 +00:00
|
|
|
static final File gPrefsPath = Constants.HOMEDIR;
|
|
|
|
static final String gPrefsFile = "grendel.pref";
|
1998-09-09 00:52:38 +00:00
|
|
|
|
|
|
|
PreferencesBase() {
|
|
|
|
super();
|
|
|
|
|
1999-06-15 11:44:40 +00:00
|
|
|
// create the dir if it doesn't exist
|
|
|
|
gPrefsPath.mkdirs();
|
|
|
|
|
|
|
|
File infile = new File(gPrefsPath, gPrefsFile);
|
1998-09-09 00:52:38 +00:00
|
|
|
InputStream in = null;
|
1999-06-16 05:15:31 +00:00
|
|
|
if (infile.exists() && infile.canRead() && infile.canWrite()) {
|
|
|
|
try {
|
|
|
|
in = new FileInputStream(infile);
|
|
|
|
load(in);
|
|
|
|
in.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
RandomAccessFile newPrefsFile = new RandomAccessFile(infile, "rw");
|
|
|
|
newPrefsFile.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
1998-09-09 00:52:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void writePrefs() {
|
1999-06-15 11:44:40 +00:00
|
|
|
File outfile = new File(gPrefsPath, gPrefsFile);
|
1998-09-09 00:52:38 +00:00
|
|
|
OutputStream out = null;
|
|
|
|
try {
|
|
|
|
out = new FileOutputStream(outfile);
|
1999-06-15 11:44:40 +00:00
|
|
|
save(out, "Grendel User Preferences. Do not directly modify this file!");
|
1998-09-09 00:52:38 +00:00
|
|
|
out.close();
|
|
|
|
} catch (IOException e) {
|
1999-06-15 11:44:40 +00:00
|
|
|
e.printStackTrace();
|
1998-09-09 00:52:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getString(String name, String defaultValue) {
|
|
|
|
return getProperty(name, defaultValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getString(String name)
|
|
|
|
throws MissingResourceException {
|
|
|
|
String result = getProperty(name);
|
|
|
|
if(result == null) {
|
|
|
|
throw new MissingResourceException(name + " is missing!", "", name);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getInt(String name, int defaultValue) {
|
|
|
|
String str = getString(name, null);
|
|
|
|
int result = defaultValue;
|
|
|
|
if (str != null) {
|
|
|
|
try {
|
|
|
|
result = Integer.parseInt(str);
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getInt(String name)
|
|
|
|
throws MissingResourceException, NumberFormatException {
|
|
|
|
String str;
|
|
|
|
try {
|
|
|
|
str = getString(name);
|
|
|
|
} catch(MissingResourceException e) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
int result;
|
|
|
|
try {
|
|
|
|
result = Integer.parseInt(str);
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean getBoolean(String name, boolean defaultValue) {
|
|
|
|
String str = getString(name, null);
|
|
|
|
boolean result = defaultValue;
|
|
|
|
if (str != null) {
|
|
|
|
result = Boolean.valueOf(str).booleanValue();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean getBoolean(String name)
|
|
|
|
throws MissingResourceException {
|
|
|
|
String str;
|
|
|
|
try {
|
|
|
|
str = getString(name);
|
|
|
|
} catch(MissingResourceException e) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
return Boolean.valueOf(str).booleanValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
public File getFile(String name, File defaultValue) {
|
|
|
|
String str = getString(name, null);
|
|
|
|
File result = defaultValue;
|
|
|
|
if (str != null) {
|
|
|
|
result = new File(str);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Assign a String value to the given preference. */
|
|
|
|
public void putString(String prefName, String value) {
|
|
|
|
put(prefName, value);
|
|
|
|
writePrefs();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Assign an int value to the given preference. */
|
|
|
|
public void putInt(String prefName, int value) {
|
|
|
|
put(prefName, (String)(""+value));
|
|
|
|
writePrefs();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Assign a boolean value to the given preference. */
|
|
|
|
public void putBoolean(String prefName, boolean value) {
|
|
|
|
put(prefName, (String)(""+value));
|
|
|
|
writePrefs();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return the preferences as a Properties object */
|
|
|
|
public Properties getAsProperties() {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|