/** Loads a playlist. */ public void importM3U(Playlist playlist) { File parentFile = FileChooserHandler.getLastInputDirectory(); if (parentFile == null) parentFile = CommonUtils.getCurrentDirectory(); final File selFile = FileChooserHandler.getInputFile( GUIMediator.getAppFrame(), I18nMarker.marktr("Open Playlist (.m3u)"), parentFile, new PlaylistListFileFilter()); // nothing selected? exit. if (selFile == null || !selFile.isFile()) return; String path = selFile.getPath(); try { path = FileUtils.getCanonicalPath(selFile); } catch (IOException ignored) { // LOG.warn("unable to get canonical path for file: " + selFile, ignored); } // create a new thread off of the event queue to process reading the files from // disk loadM3U(playlist, selFile, path); }
/** Saves a playlist. */ public void exportM3U(Playlist playlist) { if (playlist == null) { return; } String suggestedName = CommonUtils.convertFileName(playlist.getName()); // get the user to select a new one.... avoid FrostWire installation folder. File suggested; File suggestedDirectory = FileChooserHandler.getLastInputDirectory(); if (suggestedDirectory.equals(CommonUtils.getCurrentDirectory())) { suggestedDirectory = new File(CommonUtils.getUserHomeDir(), "Desktop"); } suggested = new File(suggestedDirectory, suggestedName + ".m3u"); File selFile = FileChooserHandler.getSaveAsFile( GUIMediator.getAppFrame(), I18nMarker.marktr("Save Playlist As"), suggested, new PlaylistListFileFilter()); // didn't select a file? nothing we can do. if (selFile == null) { return; } // if the file already exists and not the one just opened, ask if it should be // overwritten. // TODO: this should be handled in the jfilechooser if (selFile.exists()) { DialogOption choice = GUIMediator.showYesNoMessage( I18n.tr( "Warning: a file with the name {0} already exists in the folder. Overwrite this file?", selFile.getName()), QuestionsHandler.PLAYLIST_OVERWRITE_OK, DialogOption.NO); if (choice != DialogOption.YES) return; } String path = selFile.getPath(); try { path = FileUtils.getCanonicalPath(selFile); } catch (IOException ignored) { // LOG.warn("unable to get canonical path for file: " + selFile, ignored); } // force m3u on the end. if (!path.toLowerCase().endsWith(".m3u")) path += ".m3u"; // create a new thread to handle saving the playlist to disk saveM3U(playlist, path); }
/** * Returns the last directory that was used in a FileChooser. * * @return */ public static File getLastInputDirectory() { File dir = ApplicationSettings.LAST_FILECHOOSER_DIRECTORY.getValue(); if (dir == null || dir.getPath().equals("") || !dir.exists() || !dir.isDirectory()) return CommonUtils.getCurrentDirectory(); else return dir; }
private void copyPlaylistFilesToFolder(Playlist playlist) { if (playlist == null || playlist.getItems().isEmpty()) { return; } File suggestedDirectory = FileChooserHandler.getLastInputDirectory(); if (suggestedDirectory.equals(CommonUtils.getCurrentDirectory())) { suggestedDirectory = new File(CommonUtils.getUserHomeDir(), "Desktop"); } final File selFolder = FileChooserHandler.getSaveAsDir( GUIMediator.getAppFrame(), I18nMarker.marktr("Where do you want the playlist files copied to?"), suggestedDirectory); if (selFolder == null) { return; } // let's make a copy of the list in case the playlist will be modified during the copying. final List<PlaylistItem> playlistItems = new ArrayList<>(playlist.getItems()); BackgroundExecutorService.schedule( new Thread("Library-copy-playlist-files") { @Override public void run() { int n = 0; int total = playlistItems.size(); String targetName = selFolder.getName(); for (PlaylistItem item : playlistItems) { File f = new File(item.getFilePath()); if (f.isFile() && f.exists() && f.canRead()) { try { Path source = f.toPath(); Path target = FileSystems.getDefault().getPath(selFolder.getAbsolutePath(), f.getName()); Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING); n++; // invoked on UI thread later String status = String.format("Copied %d of %d to %s", n, total, targetName); LibraryMediator.instance().getLibrarySearch().pushStatus(status); } catch (IOException e) { e.printStackTrace(); } } } GUIMediator.launchExplorer(selFolder); // and clear the output try { Thread.sleep(2000); LibraryMediator.instance().getLibrarySearch().pushStatus(""); } catch (InterruptedException e) { } } }); }