/*
* Jampa
* Copyright (C) 2008-2009 J. Devauchelle and contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 3 as published by the Free Software Foundation.
*
* 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.
*/
package org.jampa.controllers.core;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.jampa.controllers.Controller;
import org.jampa.logging.Log;
import org.jampa.model.radio.CategoryRadioItem;
import org.jampa.model.radio.IRadioItem;
import org.jampa.model.radio.RadioItem;
import org.jampa.model.radio.io.RadioReader;
import org.jampa.model.radio.io.RadioWriter;
import org.jampa.preferences.PreferenceConstants;
import org.jampa.preferences.pages.editors.RegexPatternListEditor;
import org.jampa.utils.SystemUtils;
/**
* RadioController. Handle the categories and radios list.
* @author Anasthase
*
*/
public class RadioController {
private CategoryRadioItem _rootCategory = null;
/**
* The pattern list used to determine if a radio need MPlayer to add the "-playlist" switch.
*/
private List<Pattern> _radioPatternList;
public RadioController() {
loadRadioList();
loadPatterns();
Controller.getInstance().getPreferenceStore().addPropertyChangeListener(new IPropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent event) {
// Rebuild pattern list if changed.
if (event.getProperty().equals(PreferenceConstants.RADIO_PLAYLIST_PATTERN_LIST)) {
loadPatterns();
}
}
});
Log.getInstance(RadioController.class).debug("RadioController initialized.");
}
public CategoryRadioItem getRootCategory() {
return _rootCategory;
}
private void loadPatterns() {
_radioPatternList = new ArrayList<Pattern>();
Pattern pattern;
String patternString = null;
StringTokenizer st = new StringTokenizer(Controller.getInstance().getPreferenceStore().getString(PreferenceConstants.RADIO_PLAYLIST_PATTERN_LIST), RegexPatternListEditor.SEPARATOR);
while (st.hasMoreTokens()) {
try {
patternString = st.nextToken();
pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE);
_radioPatternList.add(pattern);
} catch (PatternSyntaxException e) {
Log.getInstance(RadioController.class).warn("Invalid pattern: " + patternString);
}
}
}
/**
* Add a new radio to the radio list.
* @param categoryName The category to which the radio will be added. Created if it does not exists.
* @param radioName The name of the radio.
* @param radioUrl The url of the radio.
*/
public void addRadio(String categoryName, String radioName, String radioUrl) {
CategoryRadioItem parentCategory = (CategoryRadioItem) _rootCategory.getChild(categoryName);
if (parentCategory == null) {
parentCategory = new CategoryRadioItem(_rootCategory, categoryName);
_rootCategory.add(parentCategory);
}
RadioItem newRadio = new RadioItem(parentCategory, radioName, radioUrl);
parentCategory.add(newRadio);
}
/**
* Remove a radio from the radio list.
* @param item The RadioItem to remove.
*/
public void removeRadioItem(IRadioItem item) {
if (item instanceof CategoryRadioItem) {
IRadioItem categoryToRemove = _rootCategory.getChild(item.getName());
if (categoryToRemove != null) {
_rootCategory.getChildren().remove(categoryToRemove);
}
} else if (item instanceof RadioItem) {
IRadioItem parentCategory = item.getParent();
parentCategory.getChildren().remove(item);
}
}
/**
* Move a radio to another category.
* @param item The radio to move.
* @param newCategoryName The new category. Created if it does not exists.
*/
public void changeItemCategory(IRadioItem item, String newCategoryName) {
IRadioItem currentCategory = _rootCategory.getChild(item.getParent().getName());
if (currentCategory != null) {
currentCategory.getChildren().remove(item);
IRadioItem newCategory = _rootCategory.getChild(newCategoryName);
if (newCategory == null) {
newCategory = new CategoryRadioItem(_rootCategory, newCategoryName);
_rootCategory.add(newCategory);
}
item.setParent(newCategory);
newCategory.getChildren().add(item);
}
}
/**
* Load radio list.
*/
public void loadRadioList() {
RadioReader reader = new RadioReader(SystemUtils.radioFile);
_rootCategory = reader.read();
}
/**
* Write radio list.
*/
public void writeRadioList() {
RadioWriter writer = new RadioWriter(SystemUtils.radioFile, _rootCategory);
writer.write();
}
/**
* Check if a RadioItem need MPlayer to add the "-playlist" switch, based on its url.
* @param item The RadioItem played
* @return True if the RadioItem need the "-playlist" switch.
*/
public boolean needPlaylistSwitch(RadioItem item) {
boolean result = false;
Pattern pattern;
Matcher matcher;
Iterator<Pattern> iter = _radioPatternList.iterator();
while ((iter.hasNext()) &&
(!result)) {
pattern = iter.next();
matcher = pattern.matcher(item.getUrl());
if (matcher.find()) {
result = true;
}
}
return result;
}
}