champlain-tile-cache.c 8.04 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright (C) 2010 Jiri Techet <techet@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

19 20 21 22 23 24 25 26 27
/**
 * SECTION:champlain-tile-cache
 * @short_description: A base class of tile caches
 *
 * This class defines properties and methods common to all caches (that is, map
 * sources that permit storage and retrieval of tiles). Tiles are typically
 * stored by #ChamplainTileSource objects.
 */

28 29
#include "champlain-tile-cache.h"

30
G_DEFINE_TYPE (ChamplainTileCache, champlain_tile_cache, CHAMPLAIN_TYPE_MAP_SOURCE)
31 32 33 34 35 36 37 38 39 40 41

#define GET_PRIVATE(obj)    (G_TYPE_INSTANCE_GET_PRIVATE((obj), CHAMPLAIN_TYPE_TILE_CACHE, ChamplainTileCachePrivate))


static const gchar *get_id (ChamplainMapSource *map_source);
static const gchar *get_name (ChamplainMapSource *map_source);
static const gchar *get_license (ChamplainMapSource *map_source);
static const gchar *get_license_uri (ChamplainMapSource *map_source);
static guint get_min_zoom_level (ChamplainMapSource *map_source);
static guint get_max_zoom_level (ChamplainMapSource *map_source);
static guint get_tile_size (ChamplainMapSource *map_source);
42
static ChamplainMapProjection get_projection (ChamplainMapSource *map_source);
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80


static void
champlain_tile_cache_dispose (GObject *object)
{
  G_OBJECT_CLASS (champlain_tile_cache_parent_class)->dispose (object);
}

static void
champlain_tile_cache_finalize (GObject *object)
{
  G_OBJECT_CLASS (champlain_tile_cache_parent_class)->finalize (object);
}

static void
champlain_tile_cache_constructed  (GObject *object)
{
  G_OBJECT_CLASS (champlain_tile_cache_parent_class)->constructed (object);
}

static void
champlain_tile_cache_class_init (ChamplainTileCacheClass *klass)
{
  GObjectClass* object_class = G_OBJECT_CLASS (klass);
  ChamplainMapSourceClass *map_source_class = CHAMPLAIN_MAP_SOURCE_CLASS (klass);
  ChamplainTileCacheClass *tile_cache_class = CHAMPLAIN_TILE_CACHE_CLASS (klass);

  object_class->finalize = champlain_tile_cache_finalize;
  object_class->dispose = champlain_tile_cache_dispose;
  object_class->constructed = champlain_tile_cache_constructed;

  map_source_class->get_id = get_id;
  map_source_class->get_name = get_name;
  map_source_class->get_license = get_license;
  map_source_class->get_license_uri = get_license_uri;
  map_source_class->get_min_zoom_level = get_min_zoom_level;
  map_source_class->get_max_zoom_level = get_max_zoom_level;
  map_source_class->get_tile_size = get_tile_size;
81
  map_source_class->get_projection = get_projection;
82 83 84 85 86 87 88 89 90 91 92 93 94 95

  map_source_class->fill_tile = NULL;

  tile_cache_class->refresh_tile_time = NULL;
  tile_cache_class->on_tile_filled = NULL;
  tile_cache_class->store_tile = NULL;
}

static void
champlain_tile_cache_init (ChamplainTileCache *tile_cache)
{
}


96 97 98 99 100 101 102 103 104 105 106
/**
 * champlain_tile_cache_store_tile:
 * @tile_cache: a #ChamplainTileCache
 * @tile: a #ChamplainTile
 * @contents: the tile contents that should be stored
 * @size: size of the contents in bytes
 *
 * Stores the tile including the metadata into the cache.
 *
 * Since: 0.6
 */
107 108
void
champlain_tile_cache_store_tile (ChamplainTileCache *tile_cache,
Jiří Techet's avatar
Jiří Techet committed
109 110 111
    ChamplainTile *tile,
    const gchar *contents,
    gsize size)
112 113 114
{
  g_return_if_fail (CHAMPLAIN_IS_TILE_CACHE (tile_cache));

115
  CHAMPLAIN_TILE_CACHE_GET_CLASS (tile_cache)->store_tile (tile_cache, tile, contents, size);
116 117
}

118 119 120 121 122 123 124 125 126
/**
 * champlain_tile_cache_refresh_tile_time:
 * @tile_cache: a #ChamplainTileCache
 * @tile: a #ChamplainTile
 *
 * Refreshes the tile access time in the cache.
 *
 * Since: 0.6
 */
127
void
Jiří Techet's avatar
Jiří Techet committed
128 129
champlain_tile_cache_refresh_tile_time (ChamplainTileCache *tile_cache,
    ChamplainTile *tile)
130 131 132
{
  g_return_if_fail (CHAMPLAIN_IS_TILE_CACHE (tile_cache));

133
  CHAMPLAIN_TILE_CACHE_GET_CLASS (tile_cache)->refresh_tile_time (tile_cache, tile);
134 135
}

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
/**
 * champlain_tile_cache_on_tile_filled:
 * @tile_cache: a #ChamplainTileCache
 * @tile: a #ChamplainTile
 *
 * When a cache fills a tile and the next source in the chain is a tile cache,
 * it should call this function on the next source. This way all the caches
 * preceding a tile source in the chain get informed that the tile was used and
 * can modify their metadata accordingly in the implementation of this function.
 * In addition, the call of this function should be chained so within the
 * implementation of this function it should be called on the next source
 * in the chain when next source is a tile cache.
 *
 * Since: 0.6
 */
151
void
Jiří Techet's avatar
Jiří Techet committed
152 153
champlain_tile_cache_on_tile_filled (ChamplainTileCache *tile_cache,
    ChamplainTile *tile)
154 155 156
{
  g_return_if_fail (CHAMPLAIN_IS_TILE_CACHE (tile_cache));

157
  CHAMPLAIN_TILE_CACHE_GET_CLASS (tile_cache)->on_tile_filled (tile_cache, tile);
158 159 160 161 162 163 164
}

static const gchar *
get_id (ChamplainMapSource *map_source)
{
  g_return_val_if_fail (CHAMPLAIN_IS_TILE_CACHE (map_source), NULL);

Jiří Techet's avatar
Jiří Techet committed
165
  ChamplainMapSource *next_source = champlain_map_source_get_next_source (map_source);
166 167 168

  g_return_val_if_fail (CHAMPLAIN_IS_MAP_SOURCE (next_source), NULL);

Jiří Techet's avatar
Jiří Techet committed
169
  return champlain_map_source_get_id (next_source);
170 171 172 173 174 175 176
}

static const gchar *
get_name (ChamplainMapSource *map_source)
{
  g_return_val_if_fail (CHAMPLAIN_IS_TILE_CACHE (map_source), NULL);

Jiří Techet's avatar
Jiří Techet committed
177
  ChamplainMapSource *next_source = champlain_map_source_get_next_source (map_source);
178 179 180

  g_return_val_if_fail (CHAMPLAIN_IS_MAP_SOURCE (next_source), NULL);

Jiří Techet's avatar
Jiří Techet committed
181
  return champlain_map_source_get_name (next_source);
182 183 184 185 186 187 188
}

static const gchar *
get_license (ChamplainMapSource *map_source)
{
  g_return_val_if_fail (CHAMPLAIN_IS_TILE_CACHE (map_source), NULL);

Jiří Techet's avatar
Jiří Techet committed
189
  ChamplainMapSource *next_source = champlain_map_source_get_next_source (map_source);
190 191 192

  g_return_val_if_fail (CHAMPLAIN_IS_MAP_SOURCE (next_source), NULL);

Jiří Techet's avatar
Jiří Techet committed
193
  return champlain_map_source_get_license (next_source);
194 195 196 197 198 199 200
}

static const gchar *
get_license_uri (ChamplainMapSource *map_source)
{
  g_return_val_if_fail (CHAMPLAIN_IS_TILE_CACHE (map_source), NULL);

Jiří Techet's avatar
Jiří Techet committed
201
  ChamplainMapSource *next_source = champlain_map_source_get_next_source (map_source);
202 203 204

  g_return_val_if_fail (CHAMPLAIN_IS_MAP_SOURCE (next_source), NULL);

Jiří Techet's avatar
Jiří Techet committed
205
  return champlain_map_source_get_license_uri (next_source);
206 207 208 209 210 211 212
}

static guint
get_min_zoom_level (ChamplainMapSource *map_source)
{
  g_return_val_if_fail (CHAMPLAIN_IS_TILE_CACHE (map_source), 0);

Jiří Techet's avatar
Jiří Techet committed
213
  ChamplainMapSource *next_source = champlain_map_source_get_next_source (map_source);
214 215 216

  g_return_val_if_fail (CHAMPLAIN_IS_MAP_SOURCE (next_source), 0);

Jiří Techet's avatar
Jiří Techet committed
217
  return champlain_map_source_get_min_zoom_level (next_source);
218 219 220 221 222 223 224
}

static guint
get_max_zoom_level (ChamplainMapSource *map_source)
{
  g_return_val_if_fail (CHAMPLAIN_IS_TILE_CACHE (map_source), 0);

Jiří Techet's avatar
Jiří Techet committed
225
  ChamplainMapSource *next_source = champlain_map_source_get_next_source (map_source);
226 227 228

  g_return_val_if_fail (CHAMPLAIN_IS_MAP_SOURCE (next_source), 0);

Jiří Techet's avatar
Jiří Techet committed
229
  return champlain_map_source_get_max_zoom_level (next_source);
230 231 232 233 234 235 236
}

static guint
get_tile_size (ChamplainMapSource *map_source)
{
  g_return_val_if_fail (CHAMPLAIN_IS_TILE_CACHE (map_source), 0);

Jiří Techet's avatar
Jiří Techet committed
237
  ChamplainMapSource *next_source = champlain_map_source_get_next_source (map_source);
238 239 240

  g_return_val_if_fail (CHAMPLAIN_IS_MAP_SOURCE (next_source), 0);

Jiří Techet's avatar
Jiří Techet committed
241
  return champlain_map_source_get_tile_size (next_source);
242
}
243 244 245 246 247 248 249 250 251 252 253 254

static ChamplainMapProjection
get_projection (ChamplainMapSource *map_source)
{
  g_return_val_if_fail (CHAMPLAIN_IS_TILE_CACHE (map_source), CHAMPLAIN_MAP_PROJECTION_MERCATOR);

  ChamplainMapSource *next_source = champlain_map_source_get_next_source (map_source);

  g_return_val_if_fail (CHAMPLAIN_IS_MAP_SOURCE (next_source), CHAMPLAIN_MAP_PROJECTION_MERCATOR);

  return champlain_map_source_get_projection (next_source);
}