/*
* Configuration.java
*
* Copyright (C) 2008 Gimme
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.sf.gimme;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
/**
*
* @author aiden
*/
public class Configuration
{
public static final File DOT_GIMME =
new File(System.getProperty("user.home") + File.separator + ".gimme");
public static final File PARTS_FOLDER = new File(DOT_GIMME, "parts");
public static final File PREFERENCES_FILE = new File(DOT_GIMME, "preferences.xml");
private Properties preferences = new Properties();
private static Configuration conf;
public Configuration()
{
if(DOT_GIMME.exists())
{
try
{
load();
}
catch(IOException ex)
{
Logger.post(Logger.Level.WARNING, "Failed to load preferences");
setDefaults();
}
}
else
{
DOT_GIMME.mkdir();
Logger.post(Logger.Level.INFO, "Created configuration directory: " + DOT_GIMME);
setDefaults();
}
if(!PARTS_FOLDER.exists())
{
PARTS_FOLDER.mkdir();
}
Configuration.conf = this;
}
private void load()
throws IOException
{
preferences.loadFromXML(new FileInputStream(PREFERENCES_FILE));
}
public static Configuration get()
{
return conf;
}
public List<Download> getCurrentDownloads()
{
List<Download> downloads = new ArrayList<Download>();
for(File file : PARTS_FOLDER.listFiles())
{
if(file.isDirectory())
{
try
{
Properties props = new Properties();
props.loadFromXML(new FileInputStream(new File(file, "index.xml")));
downloads.add(new Download(new URL(props.getProperty("url")), Integer.parseInt(props.getProperty("segments"))));
}
catch(IOException ex)
{
ex.printStackTrace();
}
}
}
return downloads;
}
public int getNumberOfSegments()
{
String strSegs = preferences.getProperty("segments");
return Integer.parseInt(strSegs);
}
public void setNumberOfSegments(int nSegs)
{
preferences.setProperty("segments", String.valueOf(nSegs));
try
{
preferences.storeToXML(new FileOutputStream(PREFERENCES_FILE), null);
}
catch(IOException ex)
{
Logger.post(Logger.Level.WARNING, "Failed to save preferences");
}
}
private void setDefaults()
{
preferences.setProperty("segments", String.valueOf(2));
}
}