mirror of
https://github.com/DSheirer/sdrtrunk.git
synced 2026-11-12 16:47:31 -05:00
444 Expose Application Directories via User Preferences (#458)
* Refactors DirectoryPreference from PLaylistPreference. Adds editor to PreferenceEditor for directories. Organizes preference editor tree. * Adds recording directory preference to editor and updates recording manager to dynamically use this property for recordings. * Adds event logs directory preference to editor and updates event log manager to dynamically use this property for event logging. * Adds application log directory preference. Updates logging to use user preference for application log directory. Moves logging initialization into the main application class and removes file based logging configs from the logback.xml configuration file. * Refactors application logging into its own class and adds prefeence update monitoring to dynamically stop/start the logger in the new directory location. * Adds streaming and screen capture directories to the user preferences editor and updates streaming and screen captures to dynamically adapt to user changes.
This commit is contained in:
@@ -26,7 +26,7 @@ package io.github.dsheirer.preference;
|
||||
public enum PreferenceType
|
||||
{
|
||||
DECODE_EVENT,
|
||||
FILES,
|
||||
DIRECTORY,
|
||||
IDENTIFIER,
|
||||
TUNER;
|
||||
}
|
||||
|
||||
@@ -21,19 +21,34 @@
|
||||
package io.github.dsheirer.preference;
|
||||
|
||||
import io.github.dsheirer.eventbus.MyEventBus;
|
||||
import io.github.dsheirer.preference.directory.DirectoryPreference;
|
||||
import io.github.dsheirer.preference.event.DecodeEventPreference;
|
||||
import io.github.dsheirer.preference.identifier.TalkgroupFormatPreference;
|
||||
import io.github.dsheirer.preference.playlist.FilePreferences;
|
||||
import io.github.dsheirer.preference.playlist.PlaylistPreference;
|
||||
import io.github.dsheirer.preference.tuner.TunerPreference;
|
||||
import io.github.dsheirer.sample.Listener;
|
||||
|
||||
/**
|
||||
* User Preferences. A collection of preferences that can be accessed by preference type.
|
||||
*
|
||||
* Note: user preference updates are broadcast throughout the system using the Google Guava Event Bus. Each component
|
||||
* can register with the event bus and annotate a method to receive updates:
|
||||
*
|
||||
* To register a component to receive events, add this in the constructor:
|
||||
* MyEventBus.getEventBus().register(this);
|
||||
*
|
||||
* To receive preference update notifications, annotate a method with @Subscribe and use a PreferenceType argument:
|
||||
*
|
||||
* @Subscribe
|
||||
* public void preferenceUpdated(PreferenceType preferenceType)
|
||||
* {
|
||||
* }
|
||||
*/
|
||||
public class UserPreferences implements Listener<PreferenceType>
|
||||
{
|
||||
private DecodeEventPreference mDecodeEventPreference;
|
||||
private FilePreferences mFilePreferences;
|
||||
private DirectoryPreference mDirectoryPreference;
|
||||
private PlaylistPreference mPlaylistPreference;
|
||||
private TalkgroupFormatPreference mTalkgroupFormatPreference;
|
||||
private TunerPreference mTunerPreference;
|
||||
|
||||
@@ -53,12 +68,20 @@ public class UserPreferences implements Listener<PreferenceType>
|
||||
return mDecodeEventPreference;
|
||||
}
|
||||
|
||||
/**
|
||||
* Directory preferences
|
||||
*/
|
||||
public DirectoryPreference getDirectoryPreference()
|
||||
{
|
||||
return mDirectoryPreference;
|
||||
}
|
||||
|
||||
/**
|
||||
* Playlist preferences
|
||||
*/
|
||||
public FilePreferences getFilePreferences()
|
||||
public PlaylistPreference getPlaylistPreference()
|
||||
{
|
||||
return mFilePreferences;
|
||||
return mPlaylistPreference;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -83,7 +106,8 @@ public class UserPreferences implements Listener<PreferenceType>
|
||||
private void loadPreferenceTypes()
|
||||
{
|
||||
mDecodeEventPreference = new DecodeEventPreference(this);
|
||||
mFilePreferences = new FilePreferences(this::receive);
|
||||
mDirectoryPreference = new DirectoryPreference(this::receive);
|
||||
mPlaylistPreference = new PlaylistPreference(this, mDirectoryPreference);
|
||||
mTalkgroupFormatPreference = new TalkgroupFormatPreference(this);
|
||||
mTunerPreference = new TunerPreference(this);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,444 @@
|
||||
/*
|
||||
* ******************************************************************************
|
||||
* sdrtrunk
|
||||
* Copyright (C) 2014-2018 Dennis Sheirer
|
||||
*
|
||||
* 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 io.github.dsheirer.preference.directory;
|
||||
|
||||
import io.github.dsheirer.preference.Preference;
|
||||
import io.github.dsheirer.preference.PreferenceType;
|
||||
import io.github.dsheirer.sample.Listener;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.prefs.Preferences;
|
||||
|
||||
/**
|
||||
* User preferences for directories
|
||||
*/
|
||||
public class DirectoryPreference extends Preference
|
||||
{
|
||||
private final static Logger mLog = LoggerFactory.getLogger(DirectoryPreference.class);
|
||||
private Preferences mPreferences = Preferences.userNodeForPackage(DirectoryPreference.class);
|
||||
|
||||
private static final String DIRECTORY_APPLICATION_ROOT = "SDRTrunk";
|
||||
private static final String DIRECTORY_APPLICATION_LOG = "logs";
|
||||
private static final String DIRECTORY_EVENT_LOG = "event_logs";
|
||||
private static final String DIRECTORY_PLAYLIST = "playlist";
|
||||
private static final String DIRECTORY_RECORDING = "recordings";
|
||||
private static final String DIRECTORY_SCREEN_CAPTURE = "screen_captures";
|
||||
private static final String DIRECTORY_STREAMING = "streaming";
|
||||
|
||||
private static final String PREFERENCE_KEY_DIRECTORY_APPLICATION_ROOT = "directory.application.root";
|
||||
private static final String PREFERENCE_KEY_DIRECTORY_APPLICATION_LOGS = "directory.application.logs";
|
||||
private static final String PREFERENCE_KEY_DIRECTORY_EVENT_LOGS = "directory.event.logs";
|
||||
private static final String PREFERENCE_KEY_DIRECTORY_PLAYLIST = "directory.playlist";
|
||||
private static final String PREFERENCE_KEY_DIRECTORY_RECORDING = "directory.recording";
|
||||
private static final String PREFERENCE_KEY_DIRECTORY_SCREEN_CAPTURE = "directory.screen.capture";
|
||||
private static final String PREFERENCE_KEY_DIRECTORY_STREAMING = "directory.streaming";
|
||||
|
||||
private Path mDirectoryApplicationRoot;
|
||||
private Path mDirectoryApplicationLogs;
|
||||
private Path mDirectoryEventLogs;
|
||||
private Path mDirectoryPlaylist;
|
||||
private Path mDirectoryRecording;
|
||||
private Path mDirectoryScreenCapture;
|
||||
private Path mDirectoryStreaming;
|
||||
|
||||
/**
|
||||
* Constructs this preference with an update listener
|
||||
* @param updateListener to receive notifications whenever these preferences change
|
||||
*/
|
||||
public DirectoryPreference(Listener<PreferenceType> updateListener)
|
||||
{
|
||||
super(updateListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PreferenceType getPreferenceType()
|
||||
{
|
||||
return PreferenceType.DIRECTORY;
|
||||
}
|
||||
|
||||
/**
|
||||
* Path to the application root directory
|
||||
*/
|
||||
public Path getDirectoryApplicationRoot()
|
||||
{
|
||||
if(mDirectoryApplicationRoot == null)
|
||||
{
|
||||
mDirectoryApplicationRoot = getPath(PREFERENCE_KEY_DIRECTORY_APPLICATION_ROOT, getDefaultApplicationDirectory());
|
||||
createDirectory(mDirectoryApplicationRoot);
|
||||
}
|
||||
|
||||
return mDirectoryApplicationRoot;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the path to the application root directory
|
||||
*/
|
||||
public void setDirectoryApplicationRoot(Path path)
|
||||
{
|
||||
mDirectoryApplicationRoot = path;
|
||||
mPreferences.put(PREFERENCE_KEY_DIRECTORY_APPLICATION_ROOT, path.toString());
|
||||
|
||||
//Set the child paths to null so that they'll be recreated on next access. If the user has explicitly set a
|
||||
//path override for any of these directories, then it will be retained.
|
||||
nullifyApplicationChildDirectories();
|
||||
|
||||
notifyPreferenceUpdated();
|
||||
}
|
||||
|
||||
/**
|
||||
* Nullifies each of the application root child directories so that they can be re-created from application root
|
||||
* or re-created from a persisted override path.
|
||||
*/
|
||||
private void nullifyApplicationChildDirectories()
|
||||
{
|
||||
mDirectoryApplicationLogs = null;
|
||||
mDirectoryEventLogs = null;
|
||||
mDirectoryPlaylist = null;
|
||||
mDirectoryRecording = null;
|
||||
mDirectoryScreenCapture = null;
|
||||
mDirectoryStreaming = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a stored recording directory preference so that the default path can be used again
|
||||
*/
|
||||
public void resetDirectoryApplicationRoot()
|
||||
{
|
||||
mDirectoryApplicationRoot = null;
|
||||
mPreferences.remove(PREFERENCE_KEY_DIRECTORY_APPLICATION_ROOT);
|
||||
|
||||
//Set the child paths to null so that they'll be recreated on next access. If the user has explicitly set a
|
||||
//path override for any of these directories, then it will be retained.
|
||||
nullifyApplicationChildDirectories();
|
||||
|
||||
notifyPreferenceUpdated();
|
||||
}
|
||||
|
||||
/**
|
||||
* Path to the folder for storing application logs
|
||||
*/
|
||||
public Path getDirectoryApplicationLog()
|
||||
{
|
||||
if(mDirectoryApplicationLogs == null)
|
||||
{
|
||||
mDirectoryApplicationLogs = getPath(PREFERENCE_KEY_DIRECTORY_APPLICATION_LOGS, getDefaultApplicationLogsDirectory());
|
||||
createDirectory(mDirectoryApplicationLogs);
|
||||
}
|
||||
|
||||
return mDirectoryApplicationLogs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the path to the application logs folder
|
||||
*/
|
||||
public void setDirectoryApplicationLogs(Path path)
|
||||
{
|
||||
mDirectoryApplicationLogs = path;
|
||||
mPreferences.put(PREFERENCE_KEY_DIRECTORY_APPLICATION_LOGS, path.toString());
|
||||
notifyPreferenceUpdated();
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a stored application logs directory preference so that the default path can be used again
|
||||
*/
|
||||
public void resetDirectoryApplicationLogs()
|
||||
{
|
||||
mPreferences.remove(PREFERENCE_KEY_DIRECTORY_APPLICATION_LOGS);
|
||||
mDirectoryApplicationLogs = null;
|
||||
notifyPreferenceUpdated();
|
||||
}
|
||||
|
||||
/**
|
||||
* Path to the folder for storing event logs
|
||||
*/
|
||||
public Path getDirectoryEventLog()
|
||||
{
|
||||
if(mDirectoryEventLogs == null)
|
||||
{
|
||||
mDirectoryEventLogs = getPath(PREFERENCE_KEY_DIRECTORY_EVENT_LOGS, getDefaultEventLogsDirectory());
|
||||
createDirectory(mDirectoryEventLogs);
|
||||
}
|
||||
|
||||
return mDirectoryEventLogs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the path to the event logs folder
|
||||
*/
|
||||
public void setDirectoryEventLogs(Path path)
|
||||
{
|
||||
mDirectoryEventLogs = path;
|
||||
mPreferences.put(PREFERENCE_KEY_DIRECTORY_EVENT_LOGS, path.toString());
|
||||
notifyPreferenceUpdated();
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a stored playlist directory preference so that the default path can be used again
|
||||
*/
|
||||
public void resetDirectoryEventLogs()
|
||||
{
|
||||
mPreferences.remove(PREFERENCE_KEY_DIRECTORY_EVENT_LOGS);
|
||||
mDirectoryEventLogs = null;
|
||||
notifyPreferenceUpdated();
|
||||
}
|
||||
|
||||
/**
|
||||
* Path to the folder for storing playlist
|
||||
*/
|
||||
public Path getDirectoryPlaylist()
|
||||
{
|
||||
if(mDirectoryPlaylist == null)
|
||||
{
|
||||
mDirectoryPlaylist = getPath(PREFERENCE_KEY_DIRECTORY_PLAYLIST, getDefaultPlaylistDirectory());
|
||||
createDirectory(mDirectoryPlaylist);
|
||||
}
|
||||
|
||||
return mDirectoryPlaylist;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the path to the playlist folder
|
||||
*/
|
||||
public void setDirectoryPlaylist(Path path)
|
||||
{
|
||||
mDirectoryPlaylist = path;
|
||||
mPreferences.put(PREFERENCE_KEY_DIRECTORY_PLAYLIST, path.toString());
|
||||
notifyPreferenceUpdated();
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a stored playlist directory preference so that the default path can be used again
|
||||
*/
|
||||
public void resetDirectoryPlaylist()
|
||||
{
|
||||
mPreferences.remove(PREFERENCE_KEY_DIRECTORY_PLAYLIST);
|
||||
mDirectoryPlaylist = null;
|
||||
notifyPreferenceUpdated();
|
||||
}
|
||||
|
||||
/**
|
||||
* Path to the folder for storing recordings
|
||||
*/
|
||||
public Path getDirectoryRecording()
|
||||
{
|
||||
if(mDirectoryRecording == null)
|
||||
{
|
||||
mDirectoryRecording = getPath(PREFERENCE_KEY_DIRECTORY_RECORDING, getDefaultRecordingDirectory());
|
||||
createDirectory(mDirectoryRecording);
|
||||
}
|
||||
|
||||
return mDirectoryRecording;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the path to the playlist folder
|
||||
*/
|
||||
public void setDirectoryRecording(Path path)
|
||||
{
|
||||
mDirectoryRecording = path;
|
||||
mPreferences.put(PREFERENCE_KEY_DIRECTORY_RECORDING, path.toString());
|
||||
notifyPreferenceUpdated();
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a stored recording directory preference so that the default path can be used again
|
||||
*/
|
||||
public void resetDirectoryRecording()
|
||||
{
|
||||
mPreferences.remove(PREFERENCE_KEY_DIRECTORY_RECORDING);
|
||||
mDirectoryRecording = null;
|
||||
notifyPreferenceUpdated();
|
||||
}
|
||||
|
||||
/**
|
||||
* Path to the folder for storing screen capture
|
||||
*/
|
||||
public Path getDirectoryScreenCapture()
|
||||
{
|
||||
if(mDirectoryScreenCapture == null)
|
||||
{
|
||||
mDirectoryScreenCapture = getPath(PREFERENCE_KEY_DIRECTORY_SCREEN_CAPTURE, getDefaultScreenCaptureDirectory());
|
||||
createDirectory(mDirectoryScreenCapture);
|
||||
}
|
||||
|
||||
return mDirectoryScreenCapture;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the path to the screen capture folder
|
||||
*/
|
||||
public void setDirectoryScreenCapture(Path path)
|
||||
{
|
||||
mDirectoryScreenCapture = path;
|
||||
mPreferences.put(PREFERENCE_KEY_DIRECTORY_SCREEN_CAPTURE, path.toString());
|
||||
notifyPreferenceUpdated();
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a stored screen capture directory preference so that the default path can be used again
|
||||
*/
|
||||
public void resetDirectoryScreenCapture()
|
||||
{
|
||||
mPreferences.remove(PREFERENCE_KEY_DIRECTORY_SCREEN_CAPTURE);
|
||||
mDirectoryScreenCapture = null;
|
||||
notifyPreferenceUpdated();
|
||||
}
|
||||
|
||||
/**
|
||||
* Path to the folder for storing streaming temporary recordings
|
||||
*/
|
||||
public Path getDirectoryStreaming()
|
||||
{
|
||||
if(mDirectoryStreaming == null)
|
||||
{
|
||||
mDirectoryStreaming = getPath(PREFERENCE_KEY_DIRECTORY_STREAMING, getDefaultStreamingDirectory());
|
||||
createDirectory(mDirectoryStreaming);
|
||||
}
|
||||
|
||||
return mDirectoryStreaming;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the path to the streaming folder
|
||||
*/
|
||||
public void setDirectoryStreaming(Path path)
|
||||
{
|
||||
mDirectoryStreaming = path;
|
||||
mPreferences.put(PREFERENCE_KEY_DIRECTORY_STREAMING, path.toString());
|
||||
notifyPreferenceUpdated();
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a stored streaming directory preference so that the default path can be used again
|
||||
*/
|
||||
public void resetDirectoryStreaming()
|
||||
{
|
||||
mPreferences.remove(PREFERENCE_KEY_DIRECTORY_STREAMING);
|
||||
mDirectoryStreaming = null;
|
||||
notifyPreferenceUpdated();
|
||||
}
|
||||
|
||||
/**
|
||||
* Default application root directory
|
||||
*/
|
||||
private Path getDefaultApplicationDirectory()
|
||||
{
|
||||
return Paths.get(System.getProperty("user.home"), DIRECTORY_APPLICATION_ROOT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Default event logs directory
|
||||
*/
|
||||
public Path getDefaultApplicationLogsDirectory()
|
||||
{
|
||||
return getDirectoryApplicationRoot().resolve(DIRECTORY_APPLICATION_LOG);
|
||||
}
|
||||
|
||||
/**
|
||||
* Default event logs directory
|
||||
*/
|
||||
public Path getDefaultEventLogsDirectory()
|
||||
{
|
||||
return getDirectoryApplicationRoot().resolve(DIRECTORY_EVENT_LOG);
|
||||
}
|
||||
|
||||
/**
|
||||
* Default playlist directory
|
||||
*/
|
||||
public Path getDefaultPlaylistDirectory()
|
||||
{
|
||||
return getDirectoryApplicationRoot().resolve(DIRECTORY_PLAYLIST);
|
||||
}
|
||||
|
||||
/**
|
||||
* Default playlist directory
|
||||
*/
|
||||
public Path getDefaultRecordingDirectory()
|
||||
{
|
||||
return getDirectoryApplicationRoot().resolve(DIRECTORY_RECORDING);
|
||||
}
|
||||
|
||||
/**
|
||||
* Default screen capture directory
|
||||
*/
|
||||
public Path getDefaultScreenCaptureDirectory()
|
||||
{
|
||||
return getDirectoryApplicationRoot().resolve(DIRECTORY_SCREEN_CAPTURE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Default streaming directory
|
||||
*/
|
||||
public Path getDefaultStreamingDirectory()
|
||||
{
|
||||
return getDirectoryApplicationRoot().resolve(DIRECTORY_STREAMING);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the path stored in preferences and referenced by the key argument if it exists, otherwise returns the
|
||||
* default path.
|
||||
*
|
||||
* Note: the default path is not checked for existence.
|
||||
*
|
||||
* @param key to the preferences service for the path
|
||||
* @param defaultPath to use if the preferred path does not exist
|
||||
* @return requested path
|
||||
*/
|
||||
private Path getPath(String key, Path defaultPath)
|
||||
{
|
||||
String stringPath = mPreferences.get(key, defaultPath.toString());
|
||||
|
||||
if(stringPath != null && !stringPath.isEmpty())
|
||||
{
|
||||
Path temp = Paths.get(stringPath);
|
||||
|
||||
if(Files.exists(temp))
|
||||
{
|
||||
return temp;
|
||||
}
|
||||
}
|
||||
|
||||
return defaultPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a directory if it does not already exist
|
||||
*/
|
||||
private void createDirectory(Path directory)
|
||||
{
|
||||
if(!Files.exists(directory))
|
||||
{
|
||||
try
|
||||
{
|
||||
Files.createDirectory(directory);
|
||||
mLog.info("Created directory [" + directory.toString() + "]");
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
mLog.error("Error creating directory [" + directory.toString() + "]");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,7 @@ package io.github.dsheirer.preference.playlist;
|
||||
|
||||
import io.github.dsheirer.preference.Preference;
|
||||
import io.github.dsheirer.preference.PreferenceType;
|
||||
import io.github.dsheirer.preference.directory.DirectoryPreference;
|
||||
import io.github.dsheirer.sample.Listener;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -34,39 +35,37 @@ import java.util.prefs.Preferences;
|
||||
/**
|
||||
* User preferences for playlists
|
||||
*/
|
||||
public class FilePreferences extends Preference
|
||||
public class PlaylistPreference extends Preference
|
||||
{
|
||||
private final static Logger mLog = LoggerFactory.getLogger(FilePreferences.class);
|
||||
private Preferences mPreferences = Preferences.userNodeForPackage(FilePreferences.class);
|
||||
private final static Logger mLog = LoggerFactory.getLogger(PlaylistPreference.class);
|
||||
private Preferences mPreferences = Preferences.userNodeForPackage(PlaylistPreference.class);
|
||||
|
||||
private static final String FOLDER_APPLICATION_ROOT = "SDRTrunk";
|
||||
private static final String FOLDER_PLAYLIST = "playlist";
|
||||
private static final String FILE_PLAYLIST = "default.xml";
|
||||
private static final String FILE_LEGACY_PLAYLIST = "playlist_v2.xml";
|
||||
|
||||
private static final String PREFERENCE_KEY_PLAYLIST_FOLDER = "playlist.folder";
|
||||
private static final String PREFERENCE_KEY_PLAYLIST_LAST_ACCESSED = "playlist.last.accessed";
|
||||
private static final String PREFERENCE_KEY_PLAYLIST_STARTUP = "playlist.default";
|
||||
private static final String PREFERENCE_KEY_PLAYLIST_USE_LAST_ACCESSED = "playlist.use.last.accessed";
|
||||
|
||||
private Path mPlaylistFolder;
|
||||
private Path mPlaylistStartupPath;
|
||||
private Path mPlaylistLastAccessedPath;
|
||||
private Boolean mUsePlaylistLastAccessedByDefault = true;
|
||||
private DirectoryPreference mDirectoryPreference;
|
||||
|
||||
/**
|
||||
* Constructs this preference with an update listener
|
||||
* @param updateListener to receive notifications whenever these preferences change
|
||||
*/
|
||||
public FilePreferences(Listener<PreferenceType> updateListener)
|
||||
public PlaylistPreference(Listener<PreferenceType> updateListener, DirectoryPreference directoryPreference)
|
||||
{
|
||||
super(updateListener);
|
||||
mDirectoryPreference = directoryPreference;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PreferenceType getPreferenceType()
|
||||
{
|
||||
return PreferenceType.FILES;
|
||||
return PreferenceType.DIRECTORY;
|
||||
}
|
||||
|
||||
|
||||
@@ -90,7 +89,7 @@ public class FilePreferences extends Preference
|
||||
*/
|
||||
public Path getLegacyPlaylist()
|
||||
{
|
||||
return getPlaylistFolder().resolve(FILE_LEGACY_PLAYLIST);
|
||||
return mDirectoryPreference.getDirectoryPlaylist().resolve(FILE_LEGACY_PLAYLIST);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -157,29 +156,6 @@ public class FilePreferences extends Preference
|
||||
notifyPreferenceUpdated();
|
||||
}
|
||||
|
||||
/**
|
||||
* Path to the folder for storing playlist
|
||||
*/
|
||||
public Path getPlaylistFolder()
|
||||
{
|
||||
if(mPlaylistFolder == null)
|
||||
{
|
||||
mPlaylistFolder = getPath(PREFERENCE_KEY_PLAYLIST_FOLDER, getDefaultPlaylistFolder());
|
||||
}
|
||||
|
||||
return mPlaylistFolder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the path to the playlist folder
|
||||
*/
|
||||
public void setPlaylistFolder(Path path)
|
||||
{
|
||||
mPlaylistFolder = path;
|
||||
mPreferences.put(PREFERENCE_KEY_PLAYLIST_FOLDER, path.toString());
|
||||
notifyPreferenceUpdated();
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates the playlist to load on startup, either the last accessed playlist (true) or the default playlist (false).
|
||||
*/
|
||||
@@ -204,32 +180,12 @@ public class FilePreferences extends Preference
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Default application root path
|
||||
*/
|
||||
private Path getDefaultApplicationFolder()
|
||||
{
|
||||
Path defaultApplicationPath = Paths.get(System.getProperty("user.home"), FOLDER_APPLICATION_ROOT);
|
||||
createDirectory(defaultApplicationPath);
|
||||
return defaultApplicationPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Default playlist folder
|
||||
*/
|
||||
public Path getDefaultPlaylistFolder()
|
||||
{
|
||||
Path defaultPlaylistFolder = getDefaultApplicationFolder().resolve(FOLDER_PLAYLIST);
|
||||
createDirectory(defaultPlaylistFolder);
|
||||
return defaultPlaylistFolder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Default playlist
|
||||
*/
|
||||
private Path getDefaultPlaylistPath()
|
||||
{
|
||||
return getDefaultPlaylistFolder().resolve(FILE_PLAYLIST);
|
||||
return mDirectoryPreference.getDefaultPlaylistDirectory().resolve(FILE_PLAYLIST);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -244,7 +200,7 @@ public class FilePreferences extends Preference
|
||||
*/
|
||||
private Path getPath(String key, Path defaultPath)
|
||||
{
|
||||
String stringPath = mPreferences.get(PREFERENCE_KEY_PLAYLIST_STARTUP, defaultPath.toString());
|
||||
String stringPath = mPreferences.get(key, defaultPath.toString());
|
||||
|
||||
if(stringPath != null && !stringPath.isEmpty())
|
||||
{
|
||||
@@ -257,23 +213,4 @@ public class FilePreferences extends Preference
|
||||
|
||||
return defaultPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a directory if it does not already exist
|
||||
*/
|
||||
private void createDirectory(Path directory)
|
||||
{
|
||||
if(!Files.exists(directory) && Files.isDirectory(directory))
|
||||
{
|
||||
try
|
||||
{
|
||||
Files.createDirectory(directory);
|
||||
mLog.info("Created directory [" + directory.toString() + "]");
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
mLog.error("Error creating directory [" + directory.toString() + "]");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user