champlain-tile-cache.c 8.08 KB
Newer Older
1
/*
Jiří Techet's avatar
Jiří Techet committed
2
 * Copyright (C) 2010-2013 Jiri Techet <techet@gmail.com>
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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
/**
 * SECTION:champlain-tile-cache
 * @short_description: A base class of tile caches
 *
23
 * This class defines properties and methods commons to all caches (that is, map
24 25 26 27
 * 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_ABSTRACT_TYPE (ChamplainTileCache, champlain_tile_cache, CHAMPLAIN_TYPE_MAP_SOURCE)
31

32
#define GET_PRIVATE(obj) \
33
  (G_TYPE_INSTANCE_GET_PRIVATE ((obj), CHAMPLAIN_TYPE_TILE_CACHE, ChamplainTileCachePrivate))
34 35


36
static const gchar *get_id (ChamplainMapSource * map_source);
37 38 39 40 41 42
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);
43
static ChamplainMapProjection get_projection (ChamplainMapSource *map_source);
44 45 46 47 48 49 50 51


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

52

53 54 55 56 57 58
static void
champlain_tile_cache_finalize (GObject *object)
{
  G_OBJECT_CLASS (champlain_tile_cache_parent_class)->finalize (object);
}

59

60
static void
61
champlain_tile_cache_constructed (GObject *object)
62 63 64 65
{
  G_OBJECT_CLASS (champlain_tile_cache_parent_class)->constructed (object);
}

66

67 68 69
static void
champlain_tile_cache_class_init (ChamplainTileCacheClass *klass)
{
70
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
71 72 73 74 75 76 77 78 79 80 81 82 83 84
  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;
85
  map_source_class->get_projection = get_projection;
86 87 88 89 90 91 92 93

  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;
}

94

95 96 97 98 99 100
static void
champlain_tile_cache_init (ChamplainTileCache *tile_cache)
{
}


101 102 103 104 105 106 107 108 109 110 111
/**
 * 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
 */
112 113
void
champlain_tile_cache_store_tile (ChamplainTileCache *tile_cache,
Jiří Techet's avatar
Jiří Techet committed
114 115 116
    ChamplainTile *tile,
    const gchar *contents,
    gsize size)
117 118 119
{
  g_return_if_fail (CHAMPLAIN_IS_TILE_CACHE (tile_cache));

120
  CHAMPLAIN_TILE_CACHE_GET_CLASS (tile_cache)->store_tile (tile_cache, tile, contents, size);
121 122
}

123

124 125 126 127 128 129 130 131 132
/**
 * champlain_tile_cache_refresh_tile_time:
 * @tile_cache: a #ChamplainTileCache
 * @tile: a #ChamplainTile
 *
 * Refreshes the tile access time in the cache.
 *
 * Since: 0.6
 */
133
void
Jiří Techet's avatar
Jiří Techet committed
134 135
champlain_tile_cache_refresh_tile_time (ChamplainTileCache *tile_cache,
    ChamplainTile *tile)
136 137 138
{
  g_return_if_fail (CHAMPLAIN_IS_TILE_CACHE (tile_cache));

139
  CHAMPLAIN_TILE_CACHE_GET_CLASS (tile_cache)->refresh_tile_time (tile_cache, tile);
140 141
}

142

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
/**
 * 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
 */
158
void
Jiří Techet's avatar
Jiří Techet committed
159 160
champlain_tile_cache_on_tile_filled (ChamplainTileCache *tile_cache,
    ChamplainTile *tile)
161 162 163
{
  g_return_if_fail (CHAMPLAIN_IS_TILE_CACHE (tile_cache));

164
  CHAMPLAIN_TILE_CACHE_GET_CLASS (tile_cache)->on_tile_filled (tile_cache, tile);
165 166
}

167

168 169 170 171 172
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
173
  ChamplainMapSource *next_source = champlain_map_source_get_next_source (map_source);
174 175 176

  g_return_val_if_fail (CHAMPLAIN_IS_MAP_SOURCE (next_source), NULL);

Jiří Techet's avatar
Jiří Techet committed
177
  return champlain_map_source_get_id (next_source);
178 179
}

180

181 182 183 184 185
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
186
  ChamplainMapSource *next_source = champlain_map_source_get_next_source (map_source);
187 188 189

  g_return_val_if_fail (CHAMPLAIN_IS_MAP_SOURCE (next_source), NULL);

Jiří Techet's avatar
Jiří Techet committed
190
  return champlain_map_source_get_name (next_source);
191 192
}

193

194 195 196 197 198
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
199
  ChamplainMapSource *next_source = champlain_map_source_get_next_source (map_source);
200 201 202

  g_return_val_if_fail (CHAMPLAIN_IS_MAP_SOURCE (next_source), NULL);

Jiří Techet's avatar
Jiří Techet committed
203
  return champlain_map_source_get_license (next_source);
204 205
}

206

207 208 209 210 211
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
212
  ChamplainMapSource *next_source = champlain_map_source_get_next_source (map_source);
213 214 215

  g_return_val_if_fail (CHAMPLAIN_IS_MAP_SOURCE (next_source), NULL);

Jiří Techet's avatar
Jiří Techet committed
216
  return champlain_map_source_get_license_uri (next_source);
217 218
}

219

220 221 222 223 224
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
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_min_zoom_level (next_source);
230 231
}

232

233 234 235 236 237
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
238
  ChamplainMapSource *next_source = champlain_map_source_get_next_source (map_source);
239 240 241

  g_return_val_if_fail (CHAMPLAIN_IS_MAP_SOURCE (next_source), 0);

Jiří Techet's avatar
Jiří Techet committed
242
  return champlain_map_source_get_max_zoom_level (next_source);
243 244
}

245

246 247 248 249 250
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
251
  ChamplainMapSource *next_source = champlain_map_source_get_next_source (map_source);
252 253 254

  g_return_val_if_fail (CHAMPLAIN_IS_MAP_SOURCE (next_source), 0);

Jiří Techet's avatar
Jiří Techet committed
255
  return champlain_map_source_get_tile_size (next_source);
256
}
257

258

259 260 261 262 263 264 265 266 267 268 269
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);
}