]> git.basschouten.com Git - openhab-addons.git/commitdiff
[freeboxos] FreeboxOsIconProvider should only provide icons for its own icon set...
authorGaël L'hopital <gael@lhopital.org>
Fri, 24 May 2024 15:30:17 +0000 (17:30 +0200)
committerGitHub <noreply@github.com>
Fri, 24 May 2024 15:30:17 +0000 (17:30 +0200)
* FreeboxOsIconProvider should only provide icons for its icon set

Signed-off-by: Gaël L'hopital <gael@lhopital.org>
bundles/org.openhab.binding.freeboxos/pom.xml
bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/FreeboxOsIconProvider.java [new file with mode: 0644]
bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/api/FreeboxOsIconProvider.java [deleted file]
bundles/org.openhab.binding.freeboxos/src/main/resources/OH-INF/i18n/freeboxos.properties

index a7b68abedfe996db11249f5c9062ea4eebd41e73..08d96abc5bfd1df7236d585571f64271ed26ee42 100644 (file)
@@ -18,7 +18,7 @@
     <dependency>
       <groupId>com.github.seancfoley</groupId>
       <artifactId>ipaddress</artifactId>
-      <version>5.4.0</version>
+      <version>5.4.2</version>
     </dependency>
   </dependencies>
 
diff --git a/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/FreeboxOsIconProvider.java b/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/FreeboxOsIconProvider.java
new file mode 100644 (file)
index 0000000..04914b6
--- /dev/null
@@ -0,0 +1,119 @@
+/**
+ * Copyright (c) 2010-2024 Contributors to the openHAB project
+ *
+ * See the NOTICE file(s) distributed with this work for additional
+ * information.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License 2.0 which is available at
+ * http://www.eclipse.org/legal/epl-2.0
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ */
+package org.openhab.binding.freeboxos.internal;
+
+import static org.openhab.binding.freeboxos.internal.FreeboxOsBindingConstants.BINDING_ID;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.net.URI;
+import java.util.Locale;
+import java.util.Set;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+import javax.ws.rs.core.UriBuilder;
+
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
+import org.eclipse.jetty.client.HttpClient;
+import org.eclipse.jetty.client.api.ContentResponse;
+import org.eclipse.jetty.client.api.Request;
+import org.eclipse.jetty.http.HttpMethod;
+import org.eclipse.jetty.http.HttpStatus;
+import org.eclipse.jetty.http.HttpStatus.Code;
+import org.openhab.binding.freeboxos.internal.api.FreeboxTlsCertificateProvider;
+import org.openhab.core.i18n.TranslationProvider;
+import org.openhab.core.io.net.http.HttpClientFactory;
+import org.openhab.core.ui.icon.AbstractResourceIconProvider;
+import org.openhab.core.ui.icon.IconProvider;
+import org.openhab.core.ui.icon.IconSet;
+import org.openhab.core.ui.icon.IconSet.Format;
+import org.osgi.framework.BundleContext;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * The {@link FreeboxOsIconProvider} delivers icons provided by FreeboxOS
+ *
+ * @author Gaël L'hopital - Initial contribution
+ */
+@NonNullByDefault
+@Component(immediate = true, service = { IconProvider.class })
+public class FreeboxOsIconProvider extends AbstractResourceIconProvider {
+    private static final String ICONSET_PREFIX = "iconset.%s";
+    private static final String DEFAULT_DESCRIPTION = "Icons provided by FreeboxOS itself";
+    private static final String DEFAULT_LABEL = "FreeboxOS Icons";
+    private static final int REQUEST_TIMEOUT_MS = 8000;
+
+    private final Logger logger = LoggerFactory.getLogger(FreeboxOsIconProvider.class);
+
+    private final HttpClient httpClient;
+    private final UriBuilder uriBuilder;
+    private final BundleContext context;
+
+    @Activate
+    public FreeboxOsIconProvider(final BundleContext context, final @Reference TranslationProvider i18nProvider,
+            final @Reference HttpClientFactory httpClientFactory) {
+        super(i18nProvider);
+        this.context = context;
+        this.httpClient = httpClientFactory.getCommonHttpClient();
+        this.uriBuilder = UriBuilder.fromPath("/").scheme("http").host(FreeboxTlsCertificateProvider.DEFAULT_NAME)
+                .path("resources/images/home/pictos");
+    }
+
+    @Override
+    public Set<IconSet> getIconSets(@Nullable Locale locale) {
+        String label = getText("label", DEFAULT_LABEL, locale);
+        String description = getText("decription", DEFAULT_DESCRIPTION, locale);
+
+        return Set.of(new IconSet(BINDING_ID, label, description, Set.of(Format.PNG)));
+    }
+
+    private String getText(String entry, String defaultValue, @Nullable Locale locale) {
+        String text = i18nProvider.getText(context.getBundle(), ICONSET_PREFIX.formatted(entry), defaultValue, locale);
+        return text == null ? defaultValue : text;
+    }
+
+    @Override
+    protected Integer getPriority() {
+        return 4;
+    }
+
+    @Override
+    protected @Nullable InputStream getResource(String iconSetId, String resourceName) {
+        URI uri = uriBuilder.clone().path(resourceName).build();
+        Request request = httpClient.newRequest(uri).method(HttpMethod.GET).timeout(REQUEST_TIMEOUT_MS,
+                TimeUnit.MILLISECONDS);
+
+        try {
+            ContentResponse response = request.send();
+            if (HttpStatus.getCode(response.getStatus()) == Code.OK) {
+                return new ByteArrayInputStream(response.getContent());
+            }
+        } catch (InterruptedException | TimeoutException | ExecutionException e) {
+            logger.warn("Error retrieving icon {}: {}", resourceName, e.getMessage());
+        }
+        return null;
+    }
+
+    @Override
+    protected boolean hasResource(String iconSetId, String resourceName) {
+        return iconSetId.equals(BINDING_ID) && resourceName.endsWith("png")
+                && getResource(iconSetId, resourceName) != null;
+    }
+}
diff --git a/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/api/FreeboxOsIconProvider.java b/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/api/FreeboxOsIconProvider.java
deleted file mode 100644 (file)
index 0ad6ffa..0000000
+++ /dev/null
@@ -1,100 +0,0 @@
-/**
- * Copyright (c) 2010-2024 Contributors to the openHAB project
- *
- * See the NOTICE file(s) distributed with this work for additional
- * information.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License 2.0 which is available at
- * http://www.eclipse.org/legal/epl-2.0
- *
- * SPDX-License-Identifier: EPL-2.0
- */
-package org.openhab.binding.freeboxos.internal.api;
-
-import java.io.ByteArrayInputStream;
-import java.io.InputStream;
-import java.net.URI;
-import java.util.Locale;
-import java.util.Set;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.TimeoutException;
-
-import javax.ws.rs.core.UriBuilder;
-
-import org.eclipse.jdt.annotation.NonNullByDefault;
-import org.eclipse.jdt.annotation.Nullable;
-import org.eclipse.jetty.client.HttpClient;
-import org.eclipse.jetty.client.api.ContentResponse;
-import org.eclipse.jetty.client.api.Request;
-import org.eclipse.jetty.http.HttpMethod;
-import org.eclipse.jetty.http.HttpStatus;
-import org.eclipse.jetty.http.HttpStatus.Code;
-import org.openhab.core.i18n.TranslationProvider;
-import org.openhab.core.io.net.http.HttpClientFactory;
-import org.openhab.core.ui.icon.AbstractResourceIconProvider;
-import org.openhab.core.ui.icon.IconProvider;
-import org.openhab.core.ui.icon.IconSet;
-import org.osgi.service.component.annotations.Activate;
-import org.osgi.service.component.annotations.Component;
-import org.osgi.service.component.annotations.Reference;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * The {@link FreeboxOsIconProvider} delivers icons provided by FreeboxOS
- *
- * @author Gaël L'hopital - Initial contribution
- */
-@NonNullByDefault
-@Component(immediate = true, service = { IconProvider.class })
-public class FreeboxOsIconProvider extends AbstractResourceIconProvider {
-
-    private final Logger logger = LoggerFactory.getLogger(FreeboxOsIconProvider.class);
-    private static final int REQUEST_TIMEOUT_MS = 8000;
-
-    private final HttpClient httpClient;
-    private final UriBuilder uriBuilder;
-
-    @Activate
-    public FreeboxOsIconProvider(final @Reference TranslationProvider i18nProvider,
-            final @Reference HttpClientFactory httpClientFactory) {
-        super(i18nProvider);
-        this.httpClient = httpClientFactory.getCommonHttpClient();
-        this.uriBuilder = UriBuilder.fromPath("/").scheme("http").host(FreeboxTlsCertificateProvider.DEFAULT_NAME)
-                .path("resources/images/home/pictos");
-    }
-
-    @Override
-    public Set<IconSet> getIconSets(@Nullable Locale locale) {
-        return Set.of();
-    }
-
-    @Override
-    protected Integer getPriority() {
-        return 4;
-    }
-
-    @Override
-    protected @Nullable InputStream getResource(String iconSetId, String resourceName) {
-        URI uri = uriBuilder.clone().path(resourceName).build();
-        Request request = httpClient.newRequest(uri).method(HttpMethod.GET).timeout(REQUEST_TIMEOUT_MS,
-                TimeUnit.MILLISECONDS);
-
-        try {
-            ContentResponse response = request.send();
-            if (HttpStatus.getCode(response.getStatus()) == Code.OK) {
-                return new ByteArrayInputStream(response.getContent());
-            }
-        } catch (InterruptedException | TimeoutException | ExecutionException e) {
-            logger.warn("Error getting icon {}: {}", resourceName, e.getMessage());
-        }
-        return null;
-    }
-
-    @Override
-    protected boolean hasResource(String iconSetId, String resourceName) {
-        return resourceName.contains(".png") && getResource(iconSetId, resourceName) != null;
-    }
-}
index a71e59d6b03e28c60ea43f8c02654aac4786433e..0c2cab27160423ba0878ef79752d05f659d5377e 100644 (file)
@@ -362,3 +362,8 @@ channel-type.freeboxos.wifi-status.description = Indicates whether the wifi netw
 # messages
 
 info-conf-pending = Please accept pairing request directly on your freebox
+
+# iconprovider
+
+iconset.label = FreeboxOS Icons
+iconset.description = Icons provided by FreeboxOS itself