    001// License: GPL. For details, see LICENSE file.
    002package org.openstreetmap.josm.plugins.streetside.cache;
    003
    004
    005import java.io.IOException;
    006
    007import org.openstreetmap.josm.data.cache.CacheEntry;
    008import org.openstreetmap.josm.data.cache.CacheEntryAttributes;
    009import org.openstreetmap.josm.data.cache.ICachedLoaderListener;
    010import org.openstreetmap.josm.plugins.streetside.StreetsideImage;
    011import org.openstreetmap.josm.plugins.streetside.cubemap.CubemapBuilder;
    012import org.openstreetmap.josm.tools.Logging;
    013
    014/**
    015* Utility methods for working with cache.
    016*
    017* @author nokutu
    018*
    019*/
    020public final class CacheUtils {
    021
    022private static IgnoreDownload ignoreDownload = new IgnoreDownload();
    023
    024/** Picture quality */
    025public enum PICTURE {
    026 /** Thumbnail quality picture (320 p) */
    027 THUMBNAIL,
    028 /** Full quality picture (2048 p) */
    029 FULL_IMAGE,
    030 /** Both of them */
    031 BOTH,
    032 /** Streetside cubemap */
    033 CUBEMAP
    034}
    035
    036private CacheUtils() {
    037 // Private constructor to avoid instantiation
    038}
    039
    040/**
    041* Downloads the the thumbnail and the full resolution picture of the given
    042* image. Does nothing if it is already in cache.
    043*
    044* @param img
    045*          The image whose picture is going to be downloaded.
    046*/
    047public static void downloadPicture(StreetsideImage img) {
    048 downloadPicture(img, PICTURE.BOTH);
    049}
    050
    051/**
    052* Downloads the the thumbnail and the full resolution picture of the given
    053* image. Does nothing if it is already in cache.
    054*
    055* @param cm
    056*          The image whose picture is going to be downloaded.
    057*/
    058public static void downloadCubemap(StreetsideImage cm) {
    059        // TODO: causes errors. Analyze @rrh
    060        //downloadPicture(cm, PICTURE.CUBEMAP);
    061}
    062
    063/**
    064* Downloads the picture of the given image. Does nothing when it is already
    065* in cache.
    066*
    067* @param img
    068*          The image to be downloaded.
    069* @param pic
    070*          The picture type to be downloaded (full quality, thumbnail or
    071*          both.)
    072*/
    073public static void downloadPicture(StreetsideImage img, PICTURE pic) {
    074 switch (pic) {
    075   case BOTH:
    076     if (new StreetsideCache(img.getId(), StreetsideCache.Type.THUMBNAIL).get() == null)
    077       submit(img.getId(), StreetsideCache.Type.THUMBNAIL, ignoreDownload);
    078     if (new StreetsideCache(img.getId(), StreetsideCache.Type.FULL_IMAGE).get() == null)
    079       submit(img.getId(), StreetsideCache.Type.FULL_IMAGE, ignoreDownload);
    080     break;
    081   case THUMBNAIL:
    082     submit(img.getId(), StreetsideCache.Type.THUMBNAIL, ignoreDownload);
    083     break;
    084   case FULL_IMAGE:
    085   // TODO: is this still useful? @rrh
    086   case CUBEMAP:
    087           if(img.getId()==null) {
    088                   Logging.error("Download cancelled. Image id is null.");
    089           } else {
    090                   CubemapBuilder.getInstance().downloadCubemapImages(img.getId());
    091           }
    092           break;
    093   default:
    094     submit(img.getId(), StreetsideCache.Type.FULL_IMAGE, ignoreDownload);
    095     break;
    096 }
    097}
    098
    099/**
    100* Downloads the picture of the given image. Does nothing when it is already
    101* in cache.
    102*
    103* @param cm
    104*          The cubemap to be downloaded.
    105* @param pic
    106*          The picture type to be downloaded (full quality, thumbnail, both, or cubemap.)
    107*/
    108/*public static void downloadCubemapFront(StreetsideImage cm, PICTURE pic) {
    109 switch (pic) {
    110   case CUBEMAP:
    111           for (int i = 0; i < 6; i++) {
    112                        for (int j = 0; j < 4; j++) {
    113                                for (int k = 0; k < 4; k++) {
    114                                        String cubeface = CubemapUtils.getFaceNumberForCount(i);
    115                                        String tileNr = CubemapUtils.rowCol2StreetsideCellAddressMap
    116                                                        .get(Integer.toString(j) + Integer.toString(k));
    117                                        long tileId = Long.parseLong(cm.getId() + cubeface + tileNr);
    118
    119                                        submit(cm.getId(), StreetsideCache.Type.CUBEMAP_FRONT, ignoreDownload);
    120
    121                                }
    122                        }
    123                }
    124           break;
    125   default:
    126     submit(cm.getId(), StreetsideCache.Type.CUBEMAP_FRONT, ignoreDownload);
    127     break;
    128 }
    129}*/
    130
    131/*public static void downloadCubemapRight(StreetsideImage cm, PICTURE pic) {
    132         switch (pic) {
    133           case CUBEMAP:
    134                   for (int i = 0; i < 6; i++) {
    135                                for (int j = 0; j < 4; j++) {
    136                                        for (int k = 0; k < 4; k++) {
    137                                                String cubeface = CubemapUtils.getFaceNumberForCount(i);
    138                                                String tileNr = CubemapUtils.rowCol2StreetsideCellAddressMap
    139                                                                .get(Integer.toString(j) + Integer.toString(k));
    140                                                long tileId = Long.parseLong(cm.getId() + cubeface + tileNr);
    141
    142                                                submit(cm.getId(), StreetsideCache.Type.CUBEMAP_RIGHT, ignoreDownload);
    143
    144                                        }
    145                                }
    146                        }
    147                   break;
    148           default:
    149             submit(cm.getId(), StreetsideCache.Type.CUBEMAP_RIGHT, ignoreDownload);
    150             break;
    151         }
    152        }*/
    153
    154/*public static void downloadCubemapBack(StreetsideImage cm, PICTURE pic) {
    155         switch (pic) {
    156           case CUBEMAP:
    157                   for (int i = 0; i < 6; i++) {
    158                                for (int j = 0; j < 4; j++) {
    159                                        for (int k = 0; k < 4; k++) {
    160                                                String cubeface = CubemapUtils.getFaceNumberForCount(i);
    161                                                String tileNr = CubemapUtils.rowCol2StreetsideCellAddressMap
    162                                                                .get(Integer.toString(j) + Integer.toString(k));
    163                                                long tileId = Long.parseLong(cm.getId() + cubeface + tileNr);
    164
    165                                                submit(cm.getId(), StreetsideCache.Type.CUBEMAP_BACK, ignoreDownload);
    166
    167                                        }
    168                                }
    169                        }
    170                   break;
    171           default:
    172             submit(cm.getId(), StreetsideCache.Type.CUBEMAP_BACK, ignoreDownload);
    173             break;
    174         }
    175        }*/
    176
    177/*public static void downloadCubemapLeft(StreetsideImage cm, PICTURE pic) {
    178         switch (pic) {
    179           case CUBEMAP:
    180                   for (int i = 0; i < 6; i++) {
    181                                for (int j = 0; j < 4; j++) {
    182                                        for (int k = 0; k < 4; k++) {
    183                                                String cubeface = CubemapUtils.getFaceNumberForCount(i);
    184                                                String tileNr = CubemapUtils.rowCol2StreetsideCellAddressMap
    185                                                                .get(Integer.toString(j) + Integer.toString(k));
    186                                                long tileId = Long.parseLong(cm.getId() + cubeface + tileNr);
    187
    188                                                submit(cm.getId(), StreetsideCache.Type.CUBEMAP_LEFT, ignoreDownload);
    189
    190                                        }
    191                                }
    192                        }
    193                   break;
    194           default:
    195             submit(cm.getId(), StreetsideCache.Type.CUBEMAP_LEFT, ignoreDownload);
    196             break;
    197         }
    198        }*/
    199
    200/*public static void downloadCubemapUp(StreetsideImage cm, PICTURE pic) {
    201         switch (pic) {
    202           case CUBEMAP:
    203                   for (int i = 0; i < 6; i++) {
    204                                for (int j = 0; j < 4; j++) {
    205                                        for (int k = 0; k < 4; k++) {
    206                                                String cubeface = CubemapUtils.getFaceNumberForCount(i);
    207                                                String tileNr = CubemapUtils.rowCol2StreetsideCellAddressMap
    208                                                                .get(Integer.toString(j) + Integer.toString(k));
    209                                                long tileId = Long.parseLong(cm.getId() + cubeface + tileNr);
    210
    211                                                submit(cm.getId(), StreetsideCache.Type.CUBEMAP_UP, ignoreDownload);
    212
    213                                        }
    214                                }
    215                        }
    216                   break;
    217           default:
    218             submit(cm.getId(), StreetsideCache.Type.CUBEMAP_UP, ignoreDownload);
    219             break;
    220         }
    221        }*/
    222
    223/*public static void downloadCubemapDown(StreetsideImage cm, PICTURE pic) {
    224         switch (pic) {
    225           case CUBEMAP:
    226                   for (int i = 0; i < 6; i++) {
    227                                for (int j = 0; j < 4; j++) {
    228                                        for (int k = 0; k < 4; k++) {
    229                                                String cubeface = CubemapUtils.getFaceNumberForCount(i);
    230                                                String tileNr = CubemapUtils.rowCol2StreetsideCellAddressMap
    231                                                                .get(Integer.toString(j) + Integer.toString(k));
    232                                                long tileId = Long.parseLong(cm.getId() + cubeface + tileNr);
    233
    234                                                submit(cm.getId(), StreetsideCache.Type.CUBEMAP_DOWN, ignoreDownload);
    235
    236                                        }
    237                                }
    238                        }
    239                   break;
    240           default:
    241             submit(cm.getId(), StreetsideCache.Type.CUBEMAP_DOWN, ignoreDownload);
    242             break;
    243         }
    244        }*/
    245
    246/**
    247* Requests the picture with the given key and quality and uses the given
    248* listener.
    249*
    250* @param key
    251*          The key of the picture to be requested.
    252* @param type
    253*          The quality of the picture to be requested.
    254* @param lis
    255*          The listener that is going to receive the picture.
    256*/
    257public static void submit(String key, StreetsideCache.Type type,
    258   ICachedLoaderListener lis) {
    259 try {
    260   new StreetsideCache(key, type).submit(lis, false);
    261 } catch (IOException e) {
    262   Logging.error(e);
    263 }
    264}
    265
    266private static class IgnoreDownload implements ICachedLoaderListener {
    267
    268 @Override
    269 public void loadingFinished(CacheEntry arg0, CacheEntryAttributes arg1, LoadResult arg2) {
    270   // Ignore download
    271 }
    272}
    273}