    001// License: GPL. For details, see LICENSE file.
    002package org.openstreetmap.josm.plugins.streetside.cache;
    003
    004
    005import java.io.IOException;
    006
    007import org.apache.log4j.Logger;
    008import org.openstreetmap.josm.data.cache.CacheEntry;
    009import org.openstreetmap.josm.data.cache.CacheEntryAttributes;
    010import org.openstreetmap.josm.data.cache.ICachedLoaderListener;
    011import org.openstreetmap.josm.plugins.streetside.StreetsideImage;
    012import org.openstreetmap.josm.plugins.streetside.cubemap.CubemapBuilder;
    013
    014/**
    015* Utility methods for working with cache.
    016*
    017* @author nokutu
    018*
    019*/
    020public final class CacheUtils {
    021
    022final static Logger logger = Logger.getLogger(CacheUtils.class);
    023
    024private static IgnoreDownload ignoreDownload = new IgnoreDownload();
    025
    026/** Picture quality */
    027public enum PICTURE {
    028 /** Thumbnail quality picture (320 p) */
    029 THUMBNAIL,
    030 /** Full quality picture (2048 p) */
    031 FULL_IMAGE,
    032 /** Both of them */
    033 BOTH,
    034 /** Streetside cubemap */
    035 CUBEMAP
    036}
    037
    038private CacheUtils() {
    039 // Private constructor to avoid instantiation
    040}
    041
    042/**
    043* Downloads the the thumbnail and the full resolution picture of the given
    044* image. Does nothing if it is already in cache.
    045*
    046* @param img
    047*          The image whose picture is going to be downloaded.
    048*/
    049public static void downloadPicture(StreetsideImage img) {
    050 downloadPicture(img, PICTURE.BOTH);
    051}
    052
    053/**
    054* Downloads the the thumbnail and the full resolution picture of the given
    055* image. Does nothing if it is already in cache.
    056*
    057* @param cm
    058*          The image whose picture is going to be downloaded.
    059*/
    060public static void downloadCubemap(StreetsideImage cm) {
    061        downloadPicture(cm, PICTURE.CUBEMAP);
    062}
    063
    064/**
    065* Downloads the picture of the given image. Does nothing when it is already
    066* in cache.
    067*
    068* @param img
    069*          The image to be downloaded.
    070* @param pic
    071*          The picture type to be downloaded (full quality, thumbnail or
    072*          both.)
    073*/
    074public static void downloadPicture(StreetsideImage img, PICTURE pic) {
    075 switch (pic) {
    076   case BOTH:
    077     if (new StreetsideCache(img.getId(), StreetsideCache.Type.THUMBNAIL).get() == null)
    078       submit(img.getId(), StreetsideCache.Type.THUMBNAIL, ignoreDownload);
    079     if (new StreetsideCache(img.getId(), StreetsideCache.Type.FULL_IMAGE).get() == null)
    080       submit(img.getId(), StreetsideCache.Type.FULL_IMAGE, ignoreDownload);
    081     break;
    082   case THUMBNAIL:
    083     submit(img.getId(), StreetsideCache.Type.THUMBNAIL, ignoreDownload);
    084     break;
    085   case FULL_IMAGE:
    086     // not used (relic from Mapillary)
    087     break;
    088   case CUBEMAP:
    089           if(img.getId()==null) {
    090                   logger.error("Download cancelled. Image id is null.");
    091           } else {
    092                   CubemapBuilder.getInstance().downloadCubemapImages(img.getId());
    093           }
    094           break;
    095   default:
    096     submit(img.getId(), StreetsideCache.Type.FULL_IMAGE, ignoreDownload);
    097     break;
    098 }
    099}
    100
    101/**
    102* Requests the picture with the given key and quality and uses the given
    103* listener.
    104*
    105* @param key
    106*          The key of the picture to be requested.
    107* @param type
    108*          The quality of the picture to be requested.
    109* @param lis
    110*          The listener that is going to receive the picture.
    111*/
    112public static void submit(String key, StreetsideCache.Type type,
    113   ICachedLoaderListener lis) {
    114 try {
    115   new StreetsideCache(key, type).submit(lis, false);
    116 } catch (IOException e) {
    117   logger.error(e);
    118 }
    119}
    120
    121  private static class IgnoreDownload implements ICachedLoaderListener {
    122
    123    @Override
    124    public void loadingFinished(CacheEntry arg0, CacheEntryAttributes arg1, LoadResult arg2) {
    125      // Ignore download
    126    }
    127  }
    128}