Remove or move java.awt dependencies
[nikiroo-utils.git] / src / be / nikiroo / fanfix / DataLoader.java
index 0f3cad00de0b7a19d11e9ffd95b6421202f7b64a..222c61463c815e8000b6e491fe0234598071a05b 100644 (file)
@@ -6,12 +6,11 @@ import java.io.InputStream;
 import java.net.URL;
 import java.util.Map;
 
-import javax.imageio.ImageIO;
-
 import be.nikiroo.fanfix.bundles.Config;
 import be.nikiroo.fanfix.supported.BasicSupport;
 import be.nikiroo.utils.Cache;
 import be.nikiroo.utils.Downloader;
+import be.nikiroo.utils.Image;
 import be.nikiroo.utils.ImageUtils;
 
 /**
@@ -49,7 +48,9 @@ public class DataLoader {
        public DataLoader(File dir, String UA, int hoursChanging, int hoursStable)
                        throws IOException {
                cache = new Cache(dir, hoursChanging, hoursStable);
+               cache.setTraceHandler(Instance.getTraceHandler());
                downloader = new Downloader(UA);
+               downloader.setTraceHandler(Instance.getTraceHandler());
        }
 
        /**
@@ -99,8 +100,8 @@ public class DataLoader {
                // MUST NOT return null
                try {
                        InputStream in = cache.load(originalUrl, false, stable);
-                       Instance.trace("Cache " + (in != null ? "hit" : "miss") + ": "
-                                       + url);
+                       Instance.getTraceHandler().trace(
+                                       "Cache " + (in != null ? "hit" : "miss") + ": " + url);
 
                        if (in == null) {
                                try {
@@ -215,27 +216,49 @@ public class DataLoader {
 
        /**
         * Save the given resource as an image on disk using the default image
-        * format for content.
+        * format for content or cover -- will automatically add the extension, too.
         * 
-        * @param url
+        * @param img
         *            the resource
         * @param target
-        *            the target file
+        *            the target file without extension
+        * @param cover
+        *            use the cover image format instead of the content image format
         * 
         * @throws IOException
         *             in case of I/O error
         */
-       public void saveAsImage(URL url, File target) throws IOException {
-               InputStream in = open(url, null, true);
-               try {
-                       ImageIO.write(ImageUtils.fromStream(in), Instance.getConfig()
-                                       .getString(Config.IMAGE_FORMAT_CONTENT).toLowerCase(),
-                                       target);
-               } catch (IOException e) {
-                       throw new IOException("Cannot write image " + url, e);
-               } finally {
-                       in.close();
+       public void saveAsImage(Image img, File target, boolean cover)
+                       throws IOException {
+               String format;
+               if (cover) {
+                       format = Instance.getConfig().getString(Config.IMAGE_FORMAT_COVER)
+                                       .toLowerCase();
+               } else {
+                       format = Instance.getConfig()
+                                       .getString(Config.IMAGE_FORMAT_CONTENT).toLowerCase();
                }
+               saveAsImage(img, new File(target.toString() + "." + format), format);
+       }
+
+       /**
+        * Save the given resource as an image on disk using the given image format
+        * for content, or with "png" format if it fails.
+        * 
+        * @param img
+        *            the resource
+        * @param target
+        *            the target file
+        * @param format
+        *            the file format ("png", "jpeg", "bmp"...)
+        * 
+        * @throws IOException
+        *             in case of I/O error
+        */
+       public void saveAsImage(Image img, File target, String format)
+                       throws IOException {
+               ImageUtils.getInstance().saveAsImage(img, target, format);
+
        }
 
        /**
@@ -268,6 +291,18 @@ public class DataLoader {
                return cache.load(uniqueID, true, true);
        }
 
+       /**
+        * Remove the given resource from the cache.
+        * 
+        * @param uniqueID
+        *            a unique ID used to locate the cached resource
+        * 
+        * @return TRUE if it was removed
+        */
+       public boolean removeFromCache(String uniqueID) {
+               return cache.remove(uniqueID);
+       }
+
        /**
         * Clean the cache (delete the cached items).
         *