2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.upnpcontrol.internal.util;
15 import static org.openhab.binding.upnpcontrol.internal.UpnpControlBindingConstants.*;
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.Collections;
21 import java.util.List;
23 import java.util.concurrent.CopyOnWriteArraySet;
24 import java.util.stream.Collectors;
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.openhab.binding.upnpcontrol.internal.queue.UpnpPlaylistsListener;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * Class with some static utility methods for the upnpcontrol binding.
35 * @author Mark Herwege - Initial contribution
39 public final class UpnpControlUtil {
41 private static final Logger LOGGER = LoggerFactory.getLogger(UpnpControlUtil.class);
43 private static volatile List<String> playlistList = new ArrayList<>();
44 private static final Set<UpnpPlaylistsListener> PLAYLIST_SUBSCRIPTIONS = new CopyOnWriteArraySet<>();
46 public static void updatePlaylistsList(@Nullable String path) {
47 playlistList = list(path, PLAYLIST_FILE_EXTENSION);
48 PLAYLIST_SUBSCRIPTIONS.forEach(UpnpPlaylistsListener::playlistsListChanged);
51 public static void playlistsSubscribe(UpnpPlaylistsListener listener) {
52 PLAYLIST_SUBSCRIPTIONS.add(listener);
55 public static void playlistsUnsubscribe(UpnpPlaylistsListener listener) {
56 PLAYLIST_SUBSCRIPTIONS.remove(listener);
59 public static void bindingConfigurationChanged(@Nullable String path) {
60 updatePlaylistsList(path);
64 * Get names of saved playlists.
68 public static List<String> playlists() {
73 * Delete a saved playlist.
75 * @param name of playlist to delete
76 * @param path of playlist directory
78 public static void deletePlaylist(String name, @Nullable String path) {
79 delete(name, path, PLAYLIST_FILE_EXTENSION);
83 * Get names of saved favorites.
85 * @param path of favorite directory
88 public static List<String> favorites(@Nullable String path) {
89 return list(path, FAVORITE_FILE_EXTENSION);
93 * Delete a saved favorite.
95 * @param name of favorite to delete
96 * @param path of favorite directory
98 public static void deleteFavorite(String name, @Nullable String path) {
99 delete(name, path, FAVORITE_FILE_EXTENSION);
102 private static List<String> list(@Nullable String path, String extension) {
104 LOGGER.debug("No path set for {} files", extension);
105 return Collections.emptyList();
108 File directory = new File(path);
109 File[] files = directory.listFiles((dir, name) -> name.toLowerCase().endsWith(extension));
112 LOGGER.debug("No {} files in {}", extension, path);
113 return Collections.emptyList();
116 List<String> result = (Arrays.asList(files)).stream().map(p -> p.getName().replace(extension, ""))
117 .collect(Collectors.toList());
121 private static void delete(String name, @Nullable String path, String extension) {
126 File file = new File(path + name + extension);