]> git.basschouten.com Git - openhab-addons.git/blob
ecb60ddcbbaa7ec4a54c48e24c53c63dd489af30
[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.config;
14
15 import static org.openhab.binding.upnpcontrol.internal.UpnpControlBindingConstants.DEFAULT_PATH;
16
17 import java.io.File;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.upnpcontrol.internal.util.UpnpControlUtil;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * Class containing the binding configuration parameters. Some helper methods take care of updating the relevant classes
26  * with parameter changes.
27  *
28  * @author Mark Herwege - Initial contribution
29  */
30 @NonNullByDefault
31 public class UpnpControlBindingConfiguration {
32     private final Logger logger = LoggerFactory.getLogger(UpnpControlBindingConfiguration.class);
33
34     public String path = DEFAULT_PATH;
35
36     public void update(UpnpControlBindingConfiguration newConfig) {
37         String newPath = newConfig.path;
38
39         if (newPath.isEmpty()) {
40             path = DEFAULT_PATH;
41         } else {
42             File file = new File(newPath);
43             if (!file.isDirectory()) {
44                 file = file.getParentFile();
45             }
46             if (file.exists()) {
47                 if (!(newPath.endsWith(File.separator) || newPath.endsWith("/"))) {
48                     newPath = newPath + File.separator;
49                 }
50                 path = newPath;
51             } else {
52                 path = DEFAULT_PATH;
53             }
54         }
55
56         logger.debug("Storage path updated to {}", path);
57
58         UpnpControlUtil.bindingConfigurationChanged(path);
59     }
60 }