File: shumate-data-source.c

package info (click to toggle)
libshumate 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 7,788 kB
  • sloc: ansic: 21,592; xml: 38; javascript: 9; makefile: 9
file content (393 lines) | stat: -rw-r--r-- 12,325 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
 * Copyright (C) 2021 James Westman <james@jwestman.net>
 *
 * 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, see <https://www.gnu.org/licenses/>.
 */

typedef struct {
  int min_zoom_level;
  int max_zoom_level;
} ShumateDataSourcePrivate;

#include "shumate-data-source.h"
#include "shumate-data-source-request.h"

/**
 * ShumateDataSource:
 *
 * The base class used to retrieve tiles as [struct@GLib.Bytes].
 */

G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (ShumateDataSource, shumate_data_source, G_TYPE_OBJECT)

enum {
  PROP_0,
  PROP_MIN_ZOOM_LEVEL,
  PROP_MAX_ZOOM_LEVEL,
  N_PROPS,
};

static GParamSpec *properties[N_PROPS];

enum
{
  RECEIVED_DATA,
  LAST_SIGNAL
};
static guint signals[LAST_SIGNAL] = { 0, };


static void
shumate_data_source_get_property (GObject    *object,
                                  guint       prop_id,
                                  GValue     *value,
                                  GParamSpec *pspec)
{
  ShumateDataSource *data_source = SHUMATE_DATA_SOURCE (object);

  switch (prop_id)
    {
      case PROP_MIN_ZOOM_LEVEL:
        g_value_set_uint (value, shumate_data_source_get_min_zoom_level (data_source));
        break;

      case PROP_MAX_ZOOM_LEVEL:
        g_value_set_uint (value, shumate_data_source_get_max_zoom_level (data_source));
        break;

      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
shumate_data_source_set_property (GObject      *object,
                                  guint         prop_id,
                                  const GValue *value,
                                  GParamSpec   *pspec)
{
  ShumateDataSource *data_source = SHUMATE_DATA_SOURCE (object);

  switch (prop_id)
    {
      case PROP_MIN_ZOOM_LEVEL:
        shumate_data_source_set_min_zoom_level (data_source,
                                                g_value_get_uint (value));
        break;

      case PROP_MAX_ZOOM_LEVEL:
        shumate_data_source_set_max_zoom_level (data_source,
                                                g_value_get_uint (value));
        break;

      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static GBytes *
shumate_data_source_real_get_tile_data_finish (ShumateDataSource  *self,
                                               GAsyncResult       *result,
                                               GError            **error)
{
  g_return_val_if_fail (SHUMATE_IS_DATA_SOURCE (self), FALSE);
  g_return_val_if_fail (g_task_is_valid (result, self), FALSE);

  return g_task_propagate_pointer (G_TASK (result), error);
}

static void
on_get_tile_data_done (GObject      *source,
                       GAsyncResult *result,
                       gpointer      user_data)
{
  ShumateDataSource *self = (ShumateDataSource *)source;
  g_autoptr(ShumateDataSourceRequest) req = user_data;
  g_autoptr(GError) error = NULL;
  g_autoptr(GBytes) data = NULL;

  data = shumate_data_source_get_tile_data_finish (self, result, &error);

  if (data)
    shumate_data_source_request_emit_data (req, data, TRUE);
  else
    shumate_data_source_request_emit_error (req, error);
}

static ShumateDataSourceRequest *
shumate_data_source_real_start_request (ShumateDataSource *self,
                                        int                x,
                                        int                y,
                                        int                zoom_level,
                                        GCancellable      *cancellable)
{
  ShumateDataSourceRequest *req = shumate_data_source_request_new (x, y, zoom_level);
  shumate_data_source_get_tile_data_async (self,
                                           x, y, zoom_level,
                                           cancellable,
                                           on_get_tile_data_done,
                                           g_object_ref (req));
  return req;
}

static void
shumate_data_source_class_init (ShumateDataSourceClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->get_property = shumate_data_source_get_property;
  object_class->set_property = shumate_data_source_set_property;

  klass->get_tile_data_async = NULL;
  klass->get_tile_data_finish = shumate_data_source_real_get_tile_data_finish;
  klass->start_request = shumate_data_source_real_start_request;

  /**
   * ShumateDataSource:min-zoom-level:
   *
   * The minimum zoom level
   *
   * Since: 1.1
   */
  properties[PROP_MIN_ZOOM_LEVEL] =
    g_param_spec_uint ("min-zoom-level",
                       "min-zoom-level",
                       "min-zoom-level",
                       0, 30, 0,
                       G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_EXPLICIT_NOTIFY);

  /**
   * ShumateDataSource:max-zoom-level:
   *
   * The maximum zoom level
   *
   * Since: 1.1
   */
  properties[PROP_MAX_ZOOM_LEVEL] =
    g_param_spec_uint ("max-zoom-level",
                       "max-zoom-level",
                       "max-zoom-level",
                       0, 30, 30,
                       G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_EXPLICIT_NOTIFY);

  g_object_class_install_properties (object_class,
                                     N_PROPS,
                                     properties);

  /**
   * ShumateDataSource::received-data:
   * @self: the [class@DataSource] emitting the signal
   * @x: the X coordinate of the tile
   * @y: the Y coordinate of the tile
   * @zoom_level: the zoom level of the tile
   * @bytes: the received data
   *
   * Emitted when data is received for any tile. This includes any intermediate
   * steps, such as data from the file cache, as well as the final result.
   *
   * Deprecated: 1.1: Use [method@DataSource.start_request] and connect to the
   * notify signals of the resulting [class@DataSourceRequest].
   */
  signals[RECEIVED_DATA] =
    g_signal_new ("received-data",
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST | G_SIGNAL_DEPRECATED,
                  0, NULL, NULL,
                  NULL,
                  G_TYPE_NONE,
                  4, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT, G_TYPE_BYTES);
}

static void
shumate_data_source_init (ShumateDataSource *self)
{
}


/**
 * shumate_data_source_get_tile_data_async:
 * @self: a [class@DataSource]
 * @x: the X coordinate to fetch
 * @y: the Y coordinate to fetch
 * @zoom_level: the Z coordinate to fetch
 * @cancellable: a #GCancellable
 * @callback: a #GAsyncReadyCallback to execute upon completion
 * @user_data: closure data for @callback
 *
 * Gets the data for the tile at the given coordinates.
 *
 * Some data sources may return data multiple times. For example,
 * [class@TileDownloader] may return data from a cache, then return updated
 * data from the network. [signal@ShumateDataSource::received-data] is emitted
 * each time data is received, then @callback is called after the last update.
 */
void
shumate_data_source_get_tile_data_async (ShumateDataSource   *self,
                                         int                  x,
                                         int                  y,
                                         int                  zoom_level,
                                         GCancellable        *cancellable,
                                         GAsyncReadyCallback  callback,
                                         gpointer             user_data)
{
  g_return_if_fail (SHUMATE_IS_DATA_SOURCE (self));
  g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));

  return SHUMATE_DATA_SOURCE_GET_CLASS (self)->get_tile_data_async (self, x, y, zoom_level, cancellable, callback, user_data);
}


/**
 * shumate_data_source_get_tile_data_finish:
 * @self: a [class@DataSource]
 * @result: a #GAsyncResult provided to callback
 * @error: a location for a #GError, or %NULL
 *
 * Gets the final result of a request started with
 * shumate_data_source_get_tile_data_async().
 *
 * Returns: (transfer full) (nullable): The requested data, or %NULL if an
 * error occurred
 */
GBytes *
shumate_data_source_get_tile_data_finish (ShumateDataSource  *self,
                                          GAsyncResult       *result,
                                          GError            **error)
{
  g_return_val_if_fail (SHUMATE_IS_DATA_SOURCE (self), FALSE);

  return SHUMATE_DATA_SOURCE_GET_CLASS (self)->get_tile_data_finish (self, result, error);
}


/**
 * shumate_data_source_start_request:
 * @self: a [class@DataSource]
 * @x: X coordinate to request
 * @y: Y coordinate to request
 * @zoom_level: zoom level to request
 * @cancellable: for cancelling the request
 *
 * Begins a request for a tile.
 *
 * Returns: (transfer full): a [class@DataSourceRequest] object for tracking
 * the request.
 *
 * Since: 1.1
 */
ShumateDataSourceRequest *
shumate_data_source_start_request (ShumateDataSource         *self,
                                   int                        x,
                                   int                        y,
                                   int                        zoom_level,
                                   GCancellable              *cancellable)
{
  g_return_val_if_fail (SHUMATE_IS_DATA_SOURCE (self), NULL);

  return SHUMATE_DATA_SOURCE_GET_CLASS (self)->start_request (self, x, y, zoom_level, cancellable);
}


/**
 * shumate_data_source_get_min_zoom_level:
 * @self: a [class@DataSource]
 *
 * Gets the data source's minimum zoom level.
 *
 * Returns: the minimum zoom level this data source supports
 *
 * Since: 1.1
 */
guint
shumate_data_source_get_min_zoom_level (ShumateDataSource *self)
{
  ShumateDataSourcePrivate *priv = shumate_data_source_get_instance_private (self);

  g_return_val_if_fail (SHUMATE_IS_DATA_SOURCE (self), 0);

  return priv->min_zoom_level;
}

/**
 * shumate_data_source_set_min_zoom_level:
 * @self: a [class@DataSource]
 * @zoom_level: the minimum zoom level
 *
 * Sets the data source's minimum zoom level.
 *
 * Since: 1.1
 */
void
shumate_data_source_set_min_zoom_level (ShumateDataSource *self,
                                        guint              zoom_level)
{
  ShumateDataSourcePrivate *priv = shumate_data_source_get_instance_private (self);

  g_return_if_fail (SHUMATE_IS_DATA_SOURCE (self));
  g_return_if_fail (zoom_level <= 30u);

  if (priv->min_zoom_level != zoom_level)
    {
      priv->min_zoom_level = zoom_level;

      g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_MIN_ZOOM_LEVEL]);
    }
}

/**
 * shumate_data_source_get_max_zoom_level:
 * @self: a [class@DataSource]
 *
 * Gets the data source's maximum zoom level.
 *
 * Returns: the maximum zoom level this data source supports
 *
 * Since: 1.1
 */
guint
shumate_data_source_get_max_zoom_level (ShumateDataSource *self)
{
  ShumateDataSourcePrivate *priv = shumate_data_source_get_instance_private (self);

  g_return_val_if_fail (SHUMATE_IS_DATA_SOURCE (self), 0);

  return priv->max_zoom_level;
}

/**
 * shumate_data_source_set_max_zoom_level:
 * @self: a [class@DataSource]
 * @zoom_level: the maximum zoom level
 *
 * Sets the data source's maximum zoom level.
 *
 * Since: 1.1
 */
void
shumate_data_source_set_max_zoom_level (ShumateDataSource *self,
                                        guint              zoom_level)
{
  ShumateDataSourcePrivate *priv = shumate_data_source_get_instance_private (self);

  g_return_if_fail (SHUMATE_IS_DATA_SOURCE (self));
  g_return_if_fail (zoom_level <= 30u);

  if (priv->max_zoom_level != zoom_level)
    {
      priv->max_zoom_level = zoom_level;

      g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_MAX_ZOOM_LEVEL]);
    }
}