]> git.basschouten.com Git - openhab-addons.git/blob
d219a7887734d91ae1cab9ce9cc246e9c9a3bad5
[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.bosesoundtouch.internal;
14
15 import java.util.HashMap;
16 import java.util.Map;
17 import java.util.Objects;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.core.types.StateOption;
22
23 import com.google.gson.annotations.Expose;
24
25 /**
26  * The {@link ContentItem} class manages a ContentItem
27  *
28  * @author Christian Niessner - Initial contribution
29  * @author Thomas Traunbauer - Initial contribution
30  */
31 @NonNullByDefault
32 public class ContentItem {
33
34     private String source = "";
35     private @Nullable String sourceAccount;
36     private @Nullable String location;
37     private boolean presetable = false;
38     private @Nullable String itemName;
39     private int presetID = 0;
40     private @Nullable String containerArt;
41     @Expose
42     private final Map<String, String> additionalAttributes = new HashMap<>();
43
44     /**
45      * Returns true if this ContentItem is defined as Preset
46      *
47      * @return true if this ContentItem is defined as Preset
48      */
49     public boolean isPreset() {
50         if (presetable) {
51             return presetID > 0;
52         } else {
53             return false;
54         }
55     }
56
57     /**
58      * Returns true if all necessary stats are set
59      *
60      * @return true if all necessary stats are set
61      */
62     public boolean isValid() {
63         if (getOperationMode() == OperationModeType.STANDBY) {
64             return true;
65         } else {
66             String localItemName = itemName;
67             if (localItemName != null) {
68                 return !(localItemName.isEmpty() || source.isEmpty());
69             } else {
70                 return false;
71             }
72         }
73     }
74
75     /**
76      * Returns true if source, sourceAccount, location, itemName, and presetable are equal
77      *
78      * @return true if source, sourceAccount, location, itemName, and presetable are equal
79      */
80     @Override
81     public boolean equals(@Nullable Object obj) {
82         if (obj instanceof ContentItem) {
83             ContentItem other = (ContentItem) obj;
84             return Objects.equals(other.source, this.source) || Objects.equals(other.sourceAccount, this.sourceAccount)
85                     || other.presetable == this.presetable || Objects.equals(other.location, this.location)
86                     || Objects.equals(other.itemName, this.itemName);
87         }
88         return super.equals(obj);
89     }
90
91     /**
92      * Returns the operation Mode, depending on the stats that are set
93      *
94      * @return the operation Mode, depending on the stats that are set
95      */
96     public OperationModeType getOperationMode() {
97         OperationModeType operationMode = OperationModeType.OTHER;
98         if ("".equals(source)) {
99             return OperationModeType.OTHER;
100         }
101         if (source.contains("PRODUCT")) {
102             String localSourceAccount = sourceAccount;
103             if (localSourceAccount != null) {
104                 if (localSourceAccount.contains("TV")) {
105                     operationMode = OperationModeType.TV;
106                 }
107                 if (localSourceAccount.contains("HDMI")) {
108                     operationMode = OperationModeType.HDMI1;
109                 }
110             }
111             return operationMode;
112         }
113         try {
114             operationMode = OperationModeType.valueOf(source);
115             return operationMode;
116         } catch (IllegalArgumentException iae) {
117             return OperationModeType.OTHER;
118         }
119     }
120
121     public void setSource(String source) {
122         this.source = source;
123     }
124
125     public void setSourceAccount(String sourceAccount) {
126         this.sourceAccount = sourceAccount;
127     }
128
129     public void setLocation(String location) {
130         this.location = location;
131     }
132
133     public void setItemName(String itemName) {
134         this.itemName = itemName;
135     }
136
137     public void setAdditionalAttribute(String name, String value) {
138         this.additionalAttributes.put(name, value);
139     }
140
141     public void setPresetable(boolean presetable) {
142         this.presetable = presetable;
143     }
144
145     public void setPresetID(int presetID) {
146         this.presetID = presetID;
147     }
148
149     public void setContainerArt(String containerArt) {
150         this.containerArt = containerArt;
151     }
152
153     public String getSource() {
154         return source;
155     }
156
157     public @Nullable String getSourceAccount() {
158         return sourceAccount;
159     }
160
161     public @Nullable String getLocation() {
162         return location;
163     }
164
165     public @Nullable String getItemName() {
166         return itemName;
167     }
168
169     public boolean isPresetable() {
170         return presetable;
171     }
172
173     public int getPresetID() {
174         return presetID;
175     }
176
177     public @Nullable String getContainerArt() {
178         return containerArt;
179     }
180
181     /**
182      * Simple method to escape XML special characters in String.
183      * There are five XML Special characters which needs to be escaped :
184      * & - &amp;
185      * < - &lt;
186      * > - &gt;
187      * " - &quot;
188      * ' - &apos;
189      */
190     private String escapeXml(String xml) {
191         xml = xml.replaceAll("&", "&amp;");
192         xml = xml.replaceAll("<", "&lt;");
193         xml = xml.replaceAll(">", "&gt;");
194         xml = xml.replaceAll("\"", "&quot;");
195         xml = xml.replaceAll("'", "&apos;");
196         return xml;
197     }
198
199     /**
200      * Returns the XML Code that is needed to switch to this ContentItem
201      *
202      * @return the XML Code that is needed to switch to this ContentItem
203      */
204     public String generateXML() {
205         String xml;
206         switch (getOperationMode()) {
207             case BLUETOOTH:
208                 xml = "<ContentItem source=\"BLUETOOTH\"></ContentItem>";
209                 break;
210             case AUX:
211             case AUX1:
212             case AUX2:
213             case AUX3:
214                 xml = "<ContentItem source=\"AUX\" sourceAccount=\"" + sourceAccount + "\"></ContentItem>";
215                 break;
216             case TV:
217                 xml = "<ContentItem source=\"PRODUCT\" sourceAccount=\"TV\" isPresetable=\"false\" />";
218                 break;
219             case HDMI1:
220                 xml = "<ContentItem source=\"PRODUCT\" sourceAccount=\"HDMI_1\" isPresetable=\"false\" />";
221                 break;
222             default:
223                 StringBuilder sbXml = new StringBuilder("<ContentItem");
224
225                 sbXml.append(" source=\"").append(escapeXml(source)).append("\"");
226
227                 String localLocation = location;
228                 if (localLocation != null) {
229                     sbXml.append(" location=\"").append(escapeXml(localLocation)).append("\"");
230                 }
231                 String localSourceAccount = sourceAccount;
232                 if (localSourceAccount != null) {
233                     sbXml.append(" sourceAccount=\"").append(escapeXml(localSourceAccount)).append("\"");
234                 }
235                 sbXml.append(" isPresetable=\"").append(presetable).append("\"");
236                 for (Map.Entry<String, String> aae : additionalAttributes.entrySet()) {
237                     sbXml.append(" ").append(aae.getKey()).append("=\"").append(escapeXml(aae.getValue())).append("\"");
238                 }
239                 sbXml.append(">");
240                 if (itemName != null) {
241                     sbXml.append("<itemName>").append(itemName).append("</itemName>");
242                 }
243                 if (containerArt != null) {
244                     sbXml.append("<containerArt>").append(containerArt).append("</containerArt>");
245                 }
246                 sbXml.append("</ContentItem>");
247                 xml = sbXml.toString();
248                 break;
249         }
250         return xml;
251     }
252
253     public StateOption toStateOption() {
254         String stateOptionLabel = String.valueOf(presetID) + ": " + itemName;
255         return new StateOption(String.valueOf(presetID), stateOptionLabel);
256     }
257
258     @Override
259     public String toString() {
260         // if (presetID >= 1 && presetID <= 6) {
261         // StringBuilder buffer = new StringBuilder();
262         // buffer.append("PRESET_");
263         // buffer.append(presetID);
264         // return buffer.toString();
265         // }
266         String localString = itemName;
267         return (localString != null) ? localString : "";
268     }
269 }