Methods Summary |
---|
public void | addCategoryManagerListener(CategoryManagerListener l)
category_listeners.addListener( l );
|
public Category | createCategory(java.lang.String name)
makeSpecialCategories();
Category newCategory = getCategory(name);
if (newCategory == null) {
newCategory = new CategoryImpl(name, 0, 0);
categories.put(name, newCategory);
saveCategories();
category_listeners.dispatch( LDT_CATEGORY_ADDED, newCategory );
return (Category)categories.get(name);
}
return newCategory;
|
public Category[] | getCategories()
if (categories.size() > 0)
return (Category[])categories.values().toArray(new Category[categories.size()]);
return (new Category[0]);
|
public Category | getCategory(java.lang.String name)
return (Category)categories.get(name);
|
public Category | getCategory(int type)
if (type == Category.TYPE_ALL)
return catAll;
if (type == Category.TYPE_UNCATEGORIZED)
return catUncategorized;
return null;
|
public static org.gudy.azureus2.core3.category.impl.CategoryManagerImpl | getInstance()
try{
class_mon.enter();
if (catMan == null)
catMan = new CategoryManagerImpl();
return catMan;
}finally{
class_mon.exit();
}
|
protected void | loadCategories()
if (doneLoading)
return;
doneLoading = true;
FileInputStream fin = null;
BufferedInputStream bin = null;
makeSpecialCategories();
try {
//open the file
File configFile = FileUtil.getUserFile("categories.config");
fin = new FileInputStream(configFile);
bin = new BufferedInputStream(fin, 8192);
Map map = BDecoder.decode(bin);
List catList = (List) map.get("categories");
for (int i = 0; i < catList.size(); i++) {
Map mCategory = (Map) catList.get(i);
try {
String catName = new String((byte[]) mCategory.get("name"), Constants.DEFAULT_ENCODING);
Long l_maxup = (Long)mCategory.get( "maxup" );
Long l_maxdown = (Long)mCategory.get( "maxdown" );
categories.put(
catName,
new CategoryImpl(
catName,
l_maxup==null?0:l_maxup.intValue(),
l_maxdown==null?0:l_maxdown.intValue()));
}
catch (UnsupportedEncodingException e1) {
//Do nothing and process next.
}
}
}
catch (FileNotFoundException e) {
//Do nothing
}
catch (Exception e) {
Debug.printStackTrace( e );
}
finally {
try {
if (bin != null)
bin.close();
}
catch (Exception e) {}
try {
if (fin != null)
fin.close();
}
catch (Exception e) {}
}
|
private void | makeSpecialCategories()
if (catAll == null) {
catAll = new CategoryImpl("Categories.all", Category.TYPE_ALL);
categories.put("Categories.all", catAll);
}
if (catUncategorized == null) {
catUncategorized = new CategoryImpl("Categories.uncategorized", Category.TYPE_UNCATEGORIZED);
categories.put("Categories.uncategorized", catUncategorized);
}
|
public void | removeCategory(Category category)
if (categories.containsKey(category.getName())) {
categories.remove(category.getName());
saveCategories();
category_listeners.dispatch( LDT_CATEGORY_REMOVED, category );
}
|
public void | removeCategoryManagerListener(CategoryManagerListener l)
category_listeners.removeListener( l );
|
public void | saveCategories()
try{
categories_mon.enter();
Map map = new HashMap();
List list = new ArrayList(categories.size());
Iterator iter = categories.values().iterator();
while (iter.hasNext()) {
Category cat = (Category) iter.next();
// For now we are only putting in 1 thing. Maybe more later, so we use a map
if (cat.getType() == Category.TYPE_USER) {
Map catMap = new HashMap();
catMap.put( "name", cat.getName());
catMap.put( "maxup", new Long(cat.getUploadSpeed()));
catMap.put( "maxdown", new Long(cat.getUploadSpeed()));
list.add(catMap);
}
}
map.put("categories", list);
FileOutputStream fos = null;
try {
//encode the data
byte[] torrentData = BEncoder.encode(map);
File oldFile = FileUtil.getUserFile("categories.config");
File newFile = FileUtil.getUserFile("categories.config.new");
//write the data out
fos = new FileOutputStream(newFile);
fos.write(torrentData);
fos.flush();
fos.getFD().sync();
//close the output stream
fos.close();
fos = null;
//delete the old file
if ( !oldFile.exists() || oldFile.delete() ) {
//rename the new one
newFile.renameTo(oldFile);
}
}
catch (Exception e) {
Debug.printStackTrace( e );
}
finally {
try {
if (fos != null)
fos.close();
}
catch (Exception e) {}
}
}finally{
categories_mon.exit();
}
|