/*
* 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.utils;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.Normalizer;
import javax.activation.MimetypesFileTypeMap;
import org.eclipse.jface.util.Util;
import org.jampa.controllers.Controller;
import org.jampa.engine.PlaybackEngine;
import org.jampa.logging.Log;
public class SystemUtils {
public enum PlaylistFormat {
XSPF,
M3U;
}
/**
* The current file separator character.
*/
public static final String fileSeparator = System.getProperty("file.separator"); //$NON-NLS-1$
public static final String currentDir = System.getProperty("user.dir"); //$NON-NLS-1$
public static String userHome = System.getProperty("user.home"); //$NON-NLS-1$
public static String applicationDirectory = userHome + fileSeparator + Constants.APP_DIRECTORY_NAME + fileSeparator; //$NON-NLS-1$s
public static String playlistDirectory = applicationDirectory + "playlists" + fileSeparator; //$NON-NLS-1$
public static String databaseDirectory = applicationDirectory + "database" + fileSeparator; //$NON-NLS-1$
public static String presetFile = applicationDirectory + "presets.properties"; //$NON-NLS-1$
public static String podcastDirectory = applicationDirectory + "podcasts" + fileSeparator; //$NON-NLS-1$
public static String podcastDowloadDirectory = podcastDirectory + "cache" + fileSeparator; //$NON-NLS-1$
public static String radioFile = applicationDirectory + "radiolist.xml"; //$NON-NLS-1$
public static final String playlistM3UExtension = ".m3u"; //$NON-NLS-1$
public static final String playlistXSPFExtension = ".xspf"; //$NON-NLS-1$
public static final String podcastExtension = ".xml"; //$NON-NLS-1$
public static final String lineTerminator = getSystemLineTerminator();
public static final String mplayerWindowsName = "mplayer.exe"; //$NON-NLS-1$
public static final String mplayerWindowsDirectory = "mplayer"; //$NON-NLS-1$
public static final String mplayerLinuxPath = "/usr/bin/mplayer"; //$NON-NLS-1$
public static void redefineUserHome(String newUserHome) {
userHome = newUserHome;
applicationDirectory = userHome + fileSeparator + Constants.APP_DIRECTORY_NAME + fileSeparator; //$NON-NLS-1$s
playlistDirectory = applicationDirectory + "playlists" + fileSeparator; //$NON-NLS-1$
databaseDirectory = applicationDirectory + "database" + fileSeparator; //$NON-NLS-1$
presetFile = applicationDirectory + "presets.properties"; //$NON-NLS-1$
podcastDirectory = applicationDirectory + "podcasts" + fileSeparator; //$NON-NLS-1$
podcastDowloadDirectory = podcastDirectory + "cache" + fileSeparator; //$NON-NLS-1$
radioFile = applicationDirectory + "radiolist.xml"; //$NON-NLS-1$
}
/**
* Gets the system line terminator.
*
* @return the system line terminator
*/
private static String getSystemLineTerminator() {
if (Util.isWindows()) {
return "\r\n"; //$NON-NLS-1$
}
return "\n"; //$NON-NLS-1$
}
/**
* Purge a directory on disk.
* @param directory The directory to purge.
*/
private static void purgeDirectory(File directory) {
if (directory.exists()) {
File[] files = directory.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
purgeDirectory(files[i]);
} else {
files[i].delete();
}
}
directory.delete();
}
}
/**
* Purge current configuration folder.
*/
public static void purgeConfiguration() {
Log.getInstance(SystemUtils.class).info("Purging configuration."); //$NON-NLS-1$
File appDir = new File(applicationDirectory);
purgeDirectory(appDir);
}
/**
* Create default configuration content.
*/
public static void createConfiguration() {
Log.getInstance(SystemUtils.class).info("Checking configuration."); //$NON-NLS-1$
File appDir = new File(applicationDirectory);
if (!appDir.exists())
appDir.mkdir();
File playlistDir = new File(playlistDirectory);
if (!playlistDir.exists())
playlistDir.mkdir();
File podcastsDir = new File(podcastDirectory);
if (!podcastsDir.exists())
podcastsDir.mkdir();
File podcastsDownloadDir = new File(podcastDowloadDirectory);
if (!podcastsDownloadDir.exists())
podcastsDownloadDir.mkdir();
File defaultPlaylist = new File(playlistDirectory + Constants.DEFAULT_PLAYLIST_ID + playlistXSPFExtension);
if (!defaultPlaylist.exists())
{
try {
defaultPlaylist.createNewFile();
FileWriter writer = new FileWriter(defaultPlaylist);
writer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><playlist xmlns=\"http://xspf.org/ns/0/\" version=\"1\"><trackList/></playlist>" + lineTerminator); //$NON-NLS-1$
writer.flush();
writer.close();
} catch (IOException e) {
Log.getInstance(SystemUtils.class).error("Unable to create default playlist file."); //$NON-NLS-1$
}
}
File preset = new File(presetFile);
if (!preset.exists()) {
try {
FileWriter writer = new FileWriter(preset);
writer.append("Classic=-3.8:0.0:0.0:0.0:0.0:0.0:-1.6:-1.6:-1.6:-2.2" + lineTerminator); //$NON-NLS-1$
writer.append("Club=0.0:0.0:0.7:1.2:1.2:1.2:0.7:0.0:0.0:0.0" + lineTerminator); //$NON-NLS-1$
writer.append("Dance=1.9:1.42:0.3:0.0:0.0:-1.42:-1.7:-1.7:0.0:0.0" + lineTerminator); //$NON-NLS-1$
writer.append("Bass=2.0:2.0:2.0:1.25:0.375:-0.875:-1.75:-2.125:-2.25:-2.25" + lineTerminator); //$NON-NLS-1$
writer.append("Bass and treble=1.5:1.25:0.125:-1.5:-1.0:0.375:1.75:2.25:2.5:2.5" + lineTerminator); //$NON-NLS-1$
writer.append("Treble=-2.0:-2.0:-2.0:-0.875:0.625:2.25:3.25:3.25:3.25:3.5" + lineTerminator); //$NON-NLS-1$
writer.append("Headphones=1.0:2.25:1.0:-0.75:-0.5:0.375:1.0:2.0:2.625:3.0" + lineTerminator); //$NON-NLS-1$
writer.append("Hall=2.125:2.125:1.25:1.25:0.125:-1.0:-1.0:-1.0:0.125:0.125" + lineTerminator); //$NON-NLS-1$
writer.append("Live=-1.0:0.125:0.875:1.125:1.25:1.25:0.875:0.625:0.625:0.5" + lineTerminator); //$NON-NLS-1$
writer.append("Party=1.5:1.5:0.125:0.125:0.125:0.125:0.125:0.125:1.5:1.5" + lineTerminator); //$NON-NLS-1$
writer.append("Pop=-0.375:1.0:1.5:1.625:1.125:-0.25:-0.5:-0.5:-0.375:-0.375" + lineTerminator); //$NON-NLS-1$
writer.append("Rock=1.625:1.0:-1.125:-1.625:-0.75:0.875:1.875:2.25:2.25:2.25" + lineTerminator); //$NON-NLS-1$
writer.append("Soft=1.0:0.375:-0.25:-0.5:-0.25:0.875:1.75:2.0:2.25:2.5" + lineTerminator); //$NON-NLS-1$
writer.append("Techno=1.625:1.25:0.125:-1.125:-1.0:0.125:1.625:2.0:2.0:1.875" + lineTerminator); //$NON-NLS-1$
writer.flush();
writer.close();
} catch (IOException e) {
Log.getInstance(SystemUtils.class).error("Unable to create preset file."); //$NON-NLS-1$
}
}
File radioList = new File(radioFile);
if (!radioList.exists()) {
// Write default file from resource.
InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("org/jampa/model/radio/io/radiolist.xml");
if (stream != null) {
try {
FileWriter writer = new FileWriter(radioList);
InputStreamReader streamReader = new InputStreamReader(stream);
BufferedReader buffer = new BufferedReader(streamReader);
String line = null;
while ( null != (line = buffer.readLine())){
writer.write(line + "\n");
}
writer.flush();
writer.close();
} catch (IOException e) {
Log.getInstance(SystemUtils.class).error("Unable to create radiolist file."); //$NON-NLS-1$
}
}
}
}
public static boolean isValidPlaylist(String fileName) {
fileName = fileName.toLowerCase();
return fileName.endsWith(playlistXSPFExtension) ||
fileName.endsWith(playlistM3UExtension);
}
public static boolean isFile(String fileName) {
File testFile = new File(fileName);
if ((testFile.exists()) &&
(testFile.isFile())) {
return true;
} else {
return false;
}
}
public static boolean isValidAudioFile(String fileName) {
fileName = fileName.toLowerCase();
if (Controller.getInstance().getEngine().getEngineType() == PlaybackEngine.MPLAYER) {
return fileName.endsWith(".mp3") || //$NON-NLS-1$
fileName.endsWith(".ogg") || //$NON-NLS-1$
fileName.endsWith(".m4a") || //$NON-NLS-1$
fileName.endsWith(".aac") || //$NON-NLS-1$
fileName.endsWith(".wav") || //$NON-NLS-1$
fileName.endsWith(".wma") || //$NON-NLS-1$
fileName.endsWith(".flac") || //$NON-NLS-1$
fileName.endsWith(".ape") || //$NON-NLS-1$
fileName.endsWith(".mpc") || //$NON-NLS-1$
fileName.endsWith(".ra") || //$NON-NLS-1$
fileName.endsWith(".rm") || //$NON-NLS-1$
fileName.endsWith(".mp+") || //$NON-NLS-1$
fileName.endsWith(".mac"); //$NON-NLS-1$
} else {
return fileName.endsWith(".mp3") || //$NON-NLS-1$
fileName.endsWith(".ogg") || //$NON-NLS-1$
fileName.endsWith(".wav"); //$NON-NLS-1$
}
}
public static String escapeAccent(String s) {
String temp = Normalizer.normalize(s, Normalizer.Form.NFKD);
return temp.replaceAll("[^\\p{ASCII}]", ""); //$NON-NLS-1$ //$NON-NLS-2$
}
public String getMIMEType(String fileName) {
return new MimetypesFileTypeMap().getContentType(fileName);
}
public String getMIMEType(File file) {
return new MimetypesFileTypeMap().getContentType(file);
}
}