]> git.basschouten.com Git - openhab-addons.git/blob
711af4e278a60847db1fee3ac361428e6b825b7a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.upnpcontrol.internal.util;
14
15 import static org.openhab.binding.upnpcontrol.internal.UpnpControlBindingConstants.*;
16
17 import java.io.File;
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.Collections;
21 import java.util.List;
22 import java.util.Set;
23 import java.util.concurrent.CopyOnWriteArraySet;
24 import java.util.stream.Collectors;
25
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;
31
32 /**
33  * Class with some static utility methods for the upnpcontrol binding.
34  *
35  * @author Mark Herwege - Initial contribution
36  *
37  */
38 @NonNullByDefault
39 public final class UpnpControlUtil {
40
41     private static final Logger LOGGER = LoggerFactory.getLogger(UpnpControlUtil.class);
42
43     private static volatile List<String> playlistList = new ArrayList<>();
44     private static final Set<UpnpPlaylistsListener> PLAYLIST_SUBSCRIPTIONS = new CopyOnWriteArraySet<>();
45
46     public static void updatePlaylistsList(@Nullable String path) {
47         playlistList = list(path, PLAYLIST_FILE_EXTENSION);
48         PLAYLIST_SUBSCRIPTIONS.forEach(UpnpPlaylistsListener::playlistsListChanged);
49     }
50
51     public static void playlistsSubscribe(UpnpPlaylistsListener listener) {
52         PLAYLIST_SUBSCRIPTIONS.add(listener);
53     }
54
55     public static void playlistsUnsubscribe(UpnpPlaylistsListener listener) {
56         PLAYLIST_SUBSCRIPTIONS.remove(listener);
57     }
58
59     public static void bindingConfigurationChanged(@Nullable String path) {
60         updatePlaylistsList(path);
61     }
62
63     /**
64      * Get names of saved playlists.
65      *
66      * @return playlists
67      */
68     public static List<String> playlists() {
69         return playlistList;
70     }
71
72     /**
73      * Delete a saved playlist.
74      *
75      * @param name of playlist to delete
76      * @param path of playlist directory
77      */
78     public static void deletePlaylist(String name, @Nullable String path) {
79         delete(name, path, PLAYLIST_FILE_EXTENSION);
80     }
81
82     /**
83      * Get names of saved favorites.
84      *
85      * @param path of favorite directory
86      * @return favorites
87      */
88     public static List<String> favorites(@Nullable String path) {
89         return list(path, FAVORITE_FILE_EXTENSION);
90     }
91
92     /**
93      * Delete a saved favorite.
94      *
95      * @param name of favorite to delete
96      * @param path of favorite directory
97      */
98     public static void deleteFavorite(String name, @Nullable String path) {
99         delete(name, path, FAVORITE_FILE_EXTENSION);
100     }
101
102     private static List<String> list(@Nullable String path, String extension) {
103         if (path == null) {
104             LOGGER.debug("No path set for {} files", extension);
105             return Collections.emptyList();
106         }
107
108         File directory = new File(path);
109         File[] files = directory.listFiles((dir, name) -> name.toLowerCase().endsWith(extension));
110
111         if (files == null) {
112             LOGGER.debug("No {} files in {}", extension, path);
113             return Collections.emptyList();
114         }
115
116         List<String> result = (Arrays.asList(files)).stream().map(p -> p.getName().replace(extension, ""))
117                 .collect(Collectors.toList());
118         return result;
119     }
120
121     private static void delete(String name, @Nullable String path, String extension) {
122         if (path == null) {
123             return;
124         }
125
126         File file = new File(path + name + extension);
127         file.delete();
128     }
129 }