]> git.basschouten.com Git - openhab-addons.git/blob
2c25cd8a63be349784442cb0bcb81747a5d987b9
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.queue;
14
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
19 import java.util.stream.Collectors;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.upnpcontrol.internal.util.StringUtils;
24
25 /**
26  *
27  * @author Mark Herwege - Initial contribution
28  * @author Karel Goderis - Based on UPnP logic in Sonos binding
29  */
30 @NonNullByDefault
31 public class UpnpEntry {
32
33     private static final String DIRECTORY_ROOT = "0";
34
35     private static final Pattern CONTAINER_PATTERN = Pattern.compile("object.container");
36
37     private String id;
38     private String refId;
39     private String parentId;
40     private String upnpClass;
41     private String title = "";
42     private List<UpnpEntryRes> resList = new ArrayList<>();
43     private String album = "";
44     private String albumArtUri = "";
45     private String creator = "";
46     private String artist = "";
47     private String publisher = "";
48     private String genre = "";
49     private @Nullable Integer originalTrackNumber;
50
51     private boolean isContainer;
52
53     public UpnpEntry() {
54         this("", "", "", "");
55     }
56
57     public UpnpEntry(String id, String refId, String parentId, String upnpClass) {
58         this.id = id;
59         this.refId = refId;
60         this.parentId = parentId;
61         this.upnpClass = upnpClass;
62
63         Matcher matcher = CONTAINER_PATTERN.matcher(upnpClass);
64         isContainer = matcher.find();
65     }
66
67     public UpnpEntry withTitle(String title) {
68         this.title = title;
69         return this;
70     }
71
72     public UpnpEntry withAlbum(String album) {
73         this.album = album;
74         return this;
75     }
76
77     public UpnpEntry withAlbumArtUri(String albumArtUri) {
78         this.albumArtUri = albumArtUri;
79         return this;
80     }
81
82     public UpnpEntry withCreator(String creator) {
83         this.creator = creator;
84         return this;
85     }
86
87     public UpnpEntry withArtist(String artist) {
88         this.artist = artist;
89         return this;
90     }
91
92     public UpnpEntry withPublisher(String publisher) {
93         this.publisher = publisher;
94         return this;
95     }
96
97     public UpnpEntry withGenre(String genre) {
98         this.genre = genre;
99         return this;
100     }
101
102     public UpnpEntry withResList(List<UpnpEntryRes> resList) {
103         this.resList = resList;
104         return this;
105     }
106
107     public UpnpEntry withTrackNumber(@Nullable Integer originalTrackNumber) {
108         this.originalTrackNumber = originalTrackNumber;
109         return this;
110     }
111
112     /**
113      * @return the title of the entry.
114      */
115     @Override
116     public String toString() {
117         return title;
118     }
119
120     /**
121      * @return the unique identifier of this entry.
122      */
123     public String getId() {
124         return id;
125     }
126
127     /**
128      * @return the title of the entry.
129      */
130     public String getTitle() {
131         return title;
132     }
133
134     /**
135      * @return the identifier of the entry this reference intry refers to.
136      */
137     public String getRefId() {
138         return refId;
139     }
140
141     /**
142      * @return the unique identifier of the parent of this entry.
143      */
144     public String getParentId() {
145         return parentId.isEmpty() ? DIRECTORY_ROOT : parentId;
146     }
147
148     /**
149      * @return a URI for this entry. Thumbnail resources are not considered.
150      */
151     public String getRes() {
152         return resList.stream().filter(res -> !res.isThumbnailRes()).map(UpnpEntryRes::getRes).findAny().orElse("");
153     }
154
155     public List<String> getProtocolList() {
156         return resList.stream().map(UpnpEntryRes::getProtocolInfo).collect(Collectors.toList());
157     }
158
159     /**
160      * @return the UPnP classname for this entry.
161      */
162     public String getUpnpClass() {
163         return upnpClass;
164     }
165
166     public boolean isContainer() {
167         return isContainer;
168     }
169
170     /**
171      * @return the name of the album.
172      */
173     public String getAlbum() {
174         return album;
175     }
176
177     /**
178      * @return the URI for the album art.
179      */
180     public String getAlbumArtUri() {
181         return StringUtils.unEscapeXml(albumArtUri);
182     }
183
184     /**
185      * @return the name of the artist who created the entry.
186      */
187     public String getCreator() {
188         return creator;
189     }
190
191     public String getArtist() {
192         return artist;
193     }
194
195     public String getPublisher() {
196         return publisher;
197     }
198
199     public String getGenre() {
200         return genre;
201     }
202
203     public @Nullable Integer getOriginalTrackNumber() {
204         return originalTrackNumber;
205     }
206 }