package dropbox;
import error.ConfigFileNotFoundException;
import java.io.*;
public final class Config implements Serializable {
private File configFile;
private ConfigValues cfg;
public Config(File path) throws IOException, ClassNotFoundException, ConfigFileNotFoundException {
cfg = new ConfigValues();
this.configFile = path;
if (!this.configFile.exists()) {
cfg.messagePath = null;
cfg.refreshInterval = 30000;
cfg.autoLoginUsername = "";
this.save();
} else {
this.load();
}
}
public void setMessagePath(File path) throws IOException {
if (path.exists() && path.canWrite() && path.canRead()) {
cfg.messagePath = path;
this.save();
}
}
public File getMessagePath() {
return cfg.messagePath;
}
public void setAutoLoginUsername(String un) throws IOException {
if (!un.contains(" ")) {
cfg.autoLoginUsername = un;
this.save();
}
}
public String getAutoLoginUsername() {
return cfg.autoLoginUsername;
}
public void setRefreshInterval(long intv) throws IOException {
cfg.refreshInterval = intv;
this.save();
}
public long getRefreshInterval() {
return cfg.refreshInterval;
}
private void save() throws IOException {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(this.configFile.getCanonicalPath()));
out.writeObject(cfg);
out.close();
}
private void load() throws IOException, ClassNotFoundException, ConfigFileNotFoundException {
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream(this.configFile));
ConfigValues config;
config = (ConfigValues) in.readObject();
cfg.messagePath = config.messagePath;
cfg.refreshInterval = config.refreshInterval;
cfg.autoLoginUsername = config.autoLoginUsername;
in.close();
} catch (FileNotFoundException e) {
throw new ConfigFileNotFoundException();
}
}
private class ConfigValues implements Serializable {
public File messagePath;
public String autoLoginUsername;
public long refreshInterval;
}
}