Added methods to delete songs and song groups.
- OsuGroupList.get().deleteSongGroup() will delete a song group from the list. - OsuGroupList.get().deleteSong() will delete an individual song from the list (must be expanded), and will delete the song group if there are no remaining songs in the group. - If possible, deleted beatmap files are moved to system trash (using JNA); otherwise, they are deleted immediately. This is done through Utils.deleteToTrash(). Signed-off-by: Jeffrey Han <itdelatrisu@gmail.com>
This commit is contained in:
@@ -25,6 +25,7 @@ import itdelatrisu.opsu.downloads.DownloadNode;
|
||||
import java.awt.Font;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.IntBuffer;
|
||||
import java.text.SimpleDateFormat;
|
||||
@@ -55,6 +56,8 @@ import org.newdawn.slick.state.StateBasedGame;
|
||||
import org.newdawn.slick.util.Log;
|
||||
import org.newdawn.slick.util.ResourceLoader;
|
||||
|
||||
import com.sun.jna.platform.FileUtils;
|
||||
|
||||
/**
|
||||
* Contains miscellaneous utilities.
|
||||
*/
|
||||
@@ -738,4 +741,60 @@ public class Utils {
|
||||
}
|
||||
return cleanName.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a file or directory. If a system trash directory is available,
|
||||
* the file or directory will be moved there instead.
|
||||
* @param file the file or directory to delete
|
||||
* @return true if moved to trash, and false if deleted
|
||||
* @throws IOException if given file does not exist
|
||||
*/
|
||||
public static boolean deleteToTrash(File file) throws IOException {
|
||||
if (file == null)
|
||||
throw new IOException("File cannot be null.");
|
||||
if (!file.exists())
|
||||
throw new IOException(String.format("File '%s' does not exist.", file.getAbsolutePath()));
|
||||
|
||||
// move to system trash, if possible
|
||||
FileUtils fileUtils = FileUtils.getInstance();
|
||||
if (fileUtils.hasTrash()) {
|
||||
try {
|
||||
fileUtils.moveToTrash(new File[] { file });
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
Log.warn(String.format("Failed to move file '%s' to trash.", file.getAbsolutePath()), e);
|
||||
}
|
||||
}
|
||||
|
||||
// delete otherwise
|
||||
if (file.isDirectory())
|
||||
deleteDirectory(file);
|
||||
else
|
||||
file.delete();
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively deletes all files and folders in a directory, then
|
||||
* deletes the directory itself.
|
||||
* @param dir the directory to delete
|
||||
*/
|
||||
private static void deleteDirectory(File dir) {
|
||||
if (dir == null || !dir.isDirectory())
|
||||
return;
|
||||
|
||||
// recursively delete contents of directory
|
||||
File[] files = dir.listFiles();
|
||||
if (files != null && files.length > 0) {
|
||||
for (File file : files) {
|
||||
if (file.isDirectory())
|
||||
deleteDirectory(file);
|
||||
else
|
||||
file.delete();
|
||||
}
|
||||
}
|
||||
|
||||
// delete the directory
|
||||
dir.delete();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user