    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        downloadPicture(cm, PICTURE.CUBEMAP);
    060}
    061
    062/**
    063* Downloads the picture of the given image. Does nothing when it is already
    064* in cache.
    065*
    066* @param img
    067*          The image to be downloaded.
    068* @param pic
    069*          The picture type to be downloaded (full quality, thumbnail or
    070*          both.)
    071*/
    072public static void downloadPicture(StreetsideImage img, PICTURE pic) {
    073 switch (pic) {
    074   case BOTH:
    075     if (new StreetsideCache(img.getId(), StreetsideCache.Type.THUMBNAIL).get() == null)
    076       submit(img.getId(), StreetsideCache.Type.THUMBNAIL, ignoreDownload);
    077     if (new StreetsideCache(img.getId(), StreetsideCache.Type.FULL_IMAGE).get() == null)
    078       submit(img.getId(), StreetsideCache.Type.FULL_IMAGE, ignoreDownload);
    079     break;
    080   case THUMBNAIL:
    081     submit(img.getId(), StreetsideCache.Type.THUMBNAIL, ignoreDownload);
    082     break;
    083   case FULL_IMAGE:
    084   // TODO: is this still useful? @rrh
    085   case CUBEMAP:
    086           if(img.getId()==null) {
    087                   Logging.error("Download cancelled. Image id is null.");
    088           } else {
    089                   CubemapBuilder.getInstance().downloadCubemapImages(img.getId());
    090           }
    091           break;
    092   default:
    093     submit(img.getId(), StreetsideCache.Type.FULL_IMAGE, ignoreDownload);
    094     break;
    095 }
    096}
    097
    098/**
    099* Downloads the picture of the given image. Does nothing when it is already
    100* in cache.
    101*
    102* @param cm
    103*          The cubemap to be downloaded.
    104* @param pic
    105*          The picture type to be downloaded (full quality, thumbnail, both, or cubemap.)
    106*/
    107/*public static void downloadCubemapFront(StreetsideImage cm, PICTURE pic) {
    108 switch (pic) {
    109   case CUBEMAP:
    110           for (int i = 0; i < 6; i++) {
    111                        for (int j = 0; j < 4; j++) {
    112                                for (int k = 0; k < 4; k++) {
    113                                        String cubeface = CubemapUtils.getFaceNumberForCount(i);
    114                                        String tileNr = CubemapUtils.rowCol2StreetsideCellAddressMap
    115                                                        .get(Integer.toString(j) + Integer.toString(k));
    116                                        long tileId = Long.parseLong(cm.getId() + cubeface + tileNr);
    117
    118                                        submit(cm.getId(), StreetsideCache.Type.CUBEMAP_FRONT, ignoreDownload);
    119
    120                                }
    121                        }
    122                }
    123           break;
    124   default:
    125     submit(cm.getId(), StreetsideCache.Type.CUBEMAP_FRONT, ignoreDownload);
    126     break;
    127 }
    128}*/
    129
    130/*public static void downloadCubemapRight(StreetsideImage cm, PICTURE pic) {
    131         switch (pic) {
    132           case CUBEMAP:
    133                   for (int i = 0; i < 6; i++) {
    134                                for (int j = 0; j < 4; j++) {
    135                                        for (int k = 0; k < 4; k++) {
    136                                                String cubeface = CubemapUtils.getFaceNumberForCount(i);
    137                                                String tileNr = CubemapUtils.rowCol2StreetsideCellAddressMap
    138                                                                .get(Integer.toString(j) + Integer.toString(k));
    139                                                long tileId = Long.parseLong(cm.getId() + cubeface + tileNr);
    140
    141                                                submit(cm.getId(), StreetsideCache.Type.CUBEMAP_RIGHT, ignoreDownload);
    142
    143                                        }
    144                                }
    145                        }
    146                   break;
    147           default:
    148             submit(cm.getId(), StreetsideCache.Type.CUBEMAP_RIGHT, ignoreDownload);
    149             break;
    150         }
    151        }*/
    152
    153/*public static void downloadCubemapBack(StreetsideImage cm, PICTURE pic) {
    154         switch (pic) {
    155           case CUBEMAP:
    156                   for (int i = 0; i < 6; i++) {
    157                                for (int j = 0; j < 4; j++) {
    158                                        for (int k = 0; k < 4; k++) {
    159                                                String cubeface = CubemapUtils.getFaceNumberForCount(i);
    160                                                String tileNr = CubemapUtils.rowCol2StreetsideCellAddressMap
    161                                                                .get(Integer.toString(j) + Integer.toString(k));
    162                                                long tileId = Long.parseLong(cm.getId() + cubeface + tileNr);
    163
    164                                                submit(cm.getId(), StreetsideCache.Type.CUBEMAP_BACK, ignoreDownload);
    165
    166                                        }
    167                                }
    168                        }
    169                   break;
    170           default:
    171             submit(cm.getId(), StreetsideCache.Type.CUBEMAP_BACK, ignoreDownload);
    172             break;
    173         }
    174        }*/
    175
    176/*public static void downloadCubemapLeft(StreetsideImage cm, PICTURE pic) {
    177         switch (pic) {
    178           case CUBEMAP:
    179                   for (int i = 0; i < 6; i++) {
    180                                for (int j = 0; j < 4; j++) {
    181                                        for (int k = 0; k < 4; k++) {
    182                                                String cubeface = CubemapUtils.getFaceNumberForCount(i);
    183                                                String tileNr = CubemapUtils.rowCol2StreetsideCellAddressMap
    184                                                                .get(Integer.toString(j) + Integer.toString(k));
    185                                                long tileId = Long.parseLong(cm.getId() + cubeface + tileNr);
    186
    187                                                submit(cm.getId(), StreetsideCache.Type.CUBEMAP_LEFT, ignoreDownload);
    188
    189                                        }
    190                                }
    191                        }
    192                   break;
    193           default:
    194             submit(cm.getId(), StreetsideCache.Type.CUBEMAP_LEFT, ignoreDownload);
    195             break;
    196         }
    197        }*/
    198
    199/*public static void downloadCubemapUp(StreetsideImage cm, PICTURE pic) {
    200         switch (pic) {
    201           case CUBEMAP:
    202                   for (int i = 0; i < 6; i++) {
    203                                for (int j = 0; j < 4; j++) {
    204                                        for (int k = 0; k < 4; k++) {
    205                                                String cubeface = CubemapUtils.getFaceNumberForCount(i);
    206                                                String tileNr = CubemapUtils.rowCol2StreetsideCellAddressMap
    207                                                                .get(Integer.toString(j) + Integer.toString(k));
    208                                                long tileId = Long.parseLong(cm.getId() + cubeface + tileNr);
    209
    210                                                submit(cm.getId(), StreetsideCache.Type.CUBEMAP_UP, ignoreDownload);
    211
    212                                        }
    213                                }
    214                        }
    215                   break;
    216           default:
    217             submit(cm.getId(), StreetsideCache.Type.CUBEMAP_UP, ignoreDownload);
    218             break;
    219         }
    220        }*/
    221
    222/*public static void downloadCubemapDown(StreetsideImage cm, PICTURE pic) {
    223         switch (pic) {
    224           case CUBEMAP:
    225                   for (int i = 0; i < 6; i++) {
    226                                for (int j = 0; j < 4; j++) {
    227                                        for (int k = 0; k < 4; k++) {
    228                                                String cubeface = CubemapUtils.getFaceNumberForCount(i);
    229                                                String tileNr = CubemapUtils.rowCol2StreetsideCellAddressMap
    230                                                                .get(Integer.toString(j) + Integer.toString(k));
    231                                                long tileId = Long.parseLong(cm.getId() + cubeface + tileNr);
    232
    233                                                submit(cm.getId(), StreetsideCache.Type.CUBEMAP_DOWN, ignoreDownload);
    234
    235                                        }
    236                                }
    237                        }
    238                   break;
    239           default:
    240             submit(cm.getId(), StreetsideCache.Type.CUBEMAP_DOWN, ignoreDownload);
    241             break;
    242         }
    243        }*/
    244
    245/**
    246* Requests the picture with the given key and quality and uses the given
    247* listener.
    248*
    249* @param key
    250*          The key of the picture to be requested.
    251* @param type
    252*          The quality of the picture to be requested.
    253* @param lis
    254*          The listener that is going to receive the picture.
    255*/
    256public static void submit(String key, StreetsideCache.Type type,
    257   ICachedLoaderListener lis) {
    258 try {
    259   new StreetsideCache(key, type).submit(lis, false);
    260 } catch (IOException e) {
    261   Logging.error(e);
    262 }
    263}
    264
    265private static class IgnoreDownload implements ICachedLoaderListener {
    266
    267 @Override
    268 public void loadingFinished(CacheEntry arg0, CacheEntryAttributes arg1, LoadResult arg2) {
    269   // Ignore download
    270 }
    271}
    272}