diff options
author | Guillaume Desmottes <guillaume.desmottes@collabora.co.uk> | 2016-08-31 08:25:22 (GMT) |
---|---|---|
committer | Guillaume Desmottes <guillaume.desmottes@collabora.co.uk> | 2016-09-06 13:44:03 (GMT) |
commit | 5c34442e44d4344205e5d86b4a6542c573b4ebe6 (patch) | |
tree | 77826d37d1eaece44088ed32a3480e7c9b62811c | |
parent | 3135f26eaf06d377218dfb9320d9329ae2b56503 (diff) | |
download | traprain-5c34442e44d4344205e5d86b4a6542c573b4ebe6.tar.gz traprain-5c34442e44d4344205e5d86b4a6542c573b4ebe6.tar.xz |
common: add TrpRoute as public API
Factor out most of TrpServiceRoute to TrpRoute in common.
This API will be used to implement the mock service API as well.
Only the bare minium is public to API users so far.
TrpRoute cannot check if the service is already published or not in
functions modifying the route (as TrpServiceRoute was doing) so I'm
introducing a new "writable" setting. It's TRUE by default and until the
service has been published then it become immutable.
Signed-off-by: Guillaume Desmottes <guillaume.desmottes@collabora.co.uk>
Reviewed-by: Mathieu Duponchelle <mathieu.duponchelle@collabora.co.uk>
Differential Revision: https://phabricator.apertis.org/D4170
-rw-r--r-- | Makefile.am | 53 | ||||
-rw-r--r-- | configure.ac | 6 | ||||
-rw-r--r-- | tests/test-helper.c | 46 | ||||
-rw-r--r-- | traprain-common/libtraprain-common.pc.in | 12 | ||||
-rw-r--r-- | traprain-common/route-internal.h | 43 | ||||
-rw-r--r-- | traprain-common/route.c | 174 | ||||
-rw-r--r-- | traprain-common/route.h | 37 | ||||
-rw-r--r-- | traprain-service/navigation.c | 41 | ||||
-rw-r--r-- | traprain-service/route-internal.h | 14 | ||||
-rw-r--r-- | traprain-service/route.c | 140 | ||||
-rw-r--r-- | traprain-service/route.h | 22 |
11 files changed, 383 insertions, 205 deletions
diff --git a/Makefile.am b/Makefile.am index 9e6d016..d86c54c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -72,15 +72,27 @@ BUILT_SOURCES += \ # Common lib +lib_LTLIBRARIES += traprain-common/libtraprain-common-@TRP_API_VERSION@.la # Internal lib on which the client and service libs will link statically noinst_LTLIBRARIES += traprain-common/libtraprain-common-internal.la +traprain_commonincludedir = $(includedir)/traprain-common-@TRP_API_VERSION@ +traprain_common_headers = \ + traprain-common/route.h \ + $(NULL) + traprain_common_private_headers = \ traprain-common/languages-map-internal.h \ + traprain-common/route-internal.h \ + $(NULL) + +nobase_traprain_commoninclude_HEADERS = \ + $(traprain_common_headers) \ $(NULL) traprain_common_sources = \ traprain-common/languages-map.c \ + traprain-common/route.c \ $(NULL) traprain_common_libtraprain_common_internal_la_SOURCES = \ @@ -112,6 +124,17 @@ traprain_common_libtraprain_common_internal_la_LDFLAGS = \ $(AM_LDFLAGS) \ $(NULL) +traprain_common_libtraprain_common_@TRP_API_VERSION@_la_SOURCES = $(NULL) +traprain_common_libtraprain_common_@TRP_API_VERSION@_la_LIBADD = traprain-common/libtraprain-common-internal.la + +traprain_common_libtraprain_common_@TRP_API_VERSION@_la_LDFLAGS = \ + -version-info $(TRP_LT_VERSION) \ + -export-symbols-regex "^trp_" \ + -no-undefined \ + $(WARN_LDFLAGS) \ + $(AM_LDFLAGS) \ + $(NULL) + # Service side lib # libtraprain-service is just a shared version of libtraprain-service-internal. @@ -255,6 +278,7 @@ traprain_client_libtraprain_client_@TRP_API_VERSION@_la_LDFLAGS = \ INTROSPECTION_GIRS = INTROSPECTION_SCANNER_ARGS = INTROSPECTION_COMPILER_ARGS = \ + --includedir=$(top_builddir)/traprain-common \ --includedir=$(top_builddir)/traprain-service \ --includedir=$(top_builddir)/traprain-client \ $(NULL) @@ -262,7 +286,26 @@ AM_DISTCHECK_CONFIGURE_FLAGS += --enable-introspection if HAVE_INTROSPECTION -traprain-service/TraprainService-@TRP_API_VERSION@.gir: traprain-service/libtraprain-service-@TRP_API_VERSION@.la +traprain-common/TraprainCommon-@TRP_API_VERSION@.gir: traprain-common/libtraprain-common-@TRP_API_VERSION@.la +traprain_common_TraprainCommon_@TRP_API_VERSION@_gir_INCLUDES = GLib-2.0 Gio-2.0 +traprain_common_TraprainCommon_@TRP_API_VERSION@_gir_CFLAGS = \ + $(traprain_common_libtraprain_common_internal_la_CPPFLAGS) \ + $(traprain_common_libtraprain_common_internal_la_CFLAGS) \ + $(NULL) +traprain_common_TraprainCommon_@TRP_API_VERSION@_gir_LIBS = traprain-common/libtraprain-common-@TRP_API_VERSION@.la +traprain_common_TraprainCommon_@TRP_API_VERSION@_gir_FILES = $(traprain_common_sources) $(traprain_common_headers) +traprain_common_TraprainCommon_@TRP_API_VERSION@_gir_NAMESPACE = TraprainCommon +traprain_common_TraprainCommon_@TRP_API_VERSION@_gir_EXPORT_PACKAGES = traprain-common-@TRP_API_VERSION@ +traprain_common_TraprainCommon_@TRP_API_VERSION@_gir_SCANNERFLAGS = \ + --nsversion=@TRP_API_VERSION@ \ + --identifier-prefix=Trp \ + --symbol-prefix=trp \ + $(WARN_SCANNERFLAGS) \ + $(NULL) + +INTROSPECTION_GIRS += traprain-common/TraprainCommon-@TRP_API_VERSION@.gir + +traprain-service/TraprainService-@TRP_API_VERSION@.gir: traprain-service/libtraprain-service-@TRP_API_VERSION@.la traprain-common/TraprainCommon-@TRP_API_VERSION@.gir traprain_service_TraprainService_@TRP_API_VERSION@_gir_INCLUDES = GLib-2.0 Gio-2.0 traprain_service_TraprainService_@TRP_API_VERSION@_gir_CFLAGS = \ $(traprain_service_libtraprain_service_internal_la_CPPFLAGS) \ @@ -277,12 +320,13 @@ traprain_service_TraprainService_@TRP_API_VERSION@_gir_SCANNERFLAGS = \ --identifier-prefix=TrpService \ --symbol-prefix=trp_service \ --c-include="traprain-service/navigation.h" \ + --include-uninstalled=$(top_builddir)/traprain-common/TraprainCommon-@TRP_API_VERSION@.gir \ $(WARN_SCANNERFLAGS) \ $(NULL) INTROSPECTION_GIRS += traprain-service/TraprainService-@TRP_API_VERSION@.gir -traprain-client/TraprainClient-@TRP_API_VERSION@.gir: traprain-client/libtraprain-client-@TRP_API_VERSION@.la +traprain-client/TraprainClient-@TRP_API_VERSION@.gir: traprain-client/libtraprain-client-@TRP_API_VERSION@.la traprain-common/TraprainCommon-@TRP_API_VERSION@.gir traprain_client_TraprainClient_@TRP_API_VERSION@_gir_INCLUDES = GLib-2.0 Gio-2.0 traprain_client_TraprainClient_@TRP_API_VERSION@_gir_CFLAGS = \ $(traprain_client_libtraprain_client_internal_la_CPPFLAGS) \ @@ -392,6 +436,8 @@ traprain_HOTDOC_FLAGS = --project-name Traprain \ $(addprefix $(top_srcdir)/, $(traprain_service_headers)) \ $(addprefix $(top_srcdir)/, $(traprain_client_sources)) \ $(addprefix $(top_srcdir)/, $(traprain_client_headers)) \ + $(addprefix $(top_srcdir)/, $(traprain_common_sources)) \ + $(addprefix $(top_srcdir)/, $(traprain_common_headers)) \ --c-include-directories @abs_top_srcdir@ @abs_top_builddir@ \ --pkg-config-packages glib-2.0 \ gio-2.0 \ @@ -431,6 +477,9 @@ dist-hook: # pkg-config data pkgconfigdir = $(libdir)/pkgconfig +pkgconfig_DATA += traprain-common/libtraprain-common-$(TRP_API_VERSION).pc +EXTRA_DIST += traprain-common/libtraprain-common.pc.in + pkgconfig_DATA += traprain-service/libtraprain-service-$(TRP_API_VERSION).pc EXTRA_DIST += traprain-service/libtraprain-service.pc.in diff --git a/configure.ac b/configure.ac index c901b39..dcf9abc 100644 --- a/configure.ac +++ b/configure.ac @@ -53,7 +53,10 @@ AX_PKG_CHECK_MODULES([TRAPRAIN], [],[], [TRP_PACKAGE_REQUIRES],[TRP_PACKAGE_REQUIRES_PRIVATE]) -AC_SUBST([TRP_SERVICE_PACKAGE_REQUIRES],[$TRP_PACKAGE_REQUIRES]) +AC_SUBST([TRP_COMMON_PACKAGE_REQUIRES],[$TRP_PACKAGE_REQUIRES]) +AC_SUBST([TRP_COMMON_PACKAGE_REQUIRES_PRIVATE],[$TRP_PACKAGE_REQUIRES_PRIVATE]) + +AC_SUBST([TRP_SERVICE_PACKAGE_REQUIRES],["$TRP_PACKAGE_REQUIRES traprain-common-$TRP_API_VERSION"]) AC_SUBST([TRP_SERVICE_PACKAGE_REQUIRES_PRIVATE],[$TRP_PACKAGE_REQUIRES_PRIVATE]) AC_SUBST([TRP_CLIENT_PACKAGE_REQUIRES],[$TRP_PACKAGE_REQUIRES]) @@ -109,6 +112,7 @@ AC_SUBST([ENABLE_INSTALLED_TESTS],[$enable_installed_tests]) AC_CONFIG_FILES([ Makefile +traprain-common/libtraprain-common-$TRP_API_VERSION.pc:traprain-common/libtraprain-common.pc.in traprain-service/libtraprain-service-$TRP_API_VERSION.pc:traprain-service/libtraprain-service.pc.in traprain-client/libtraprain-client-$TRP_API_VERSION.pc:traprain-client/libtraprain-client.pc.in ],[], diff --git a/tests/test-helper.c b/tests/test-helper.c index c45bc8b..0c1d252 100644 --- a/tests/test-helper.c +++ b/tests/test-helper.c @@ -13,48 +13,48 @@ TrpServiceRoute * create_first_service_route (void) { - TrpServiceRoute *route; + TrpRoute *route; guint segment; route = trp_service_route_new (FST_ROUTE_DISTANCE, FST_ROUTE_TIME); - trp_service_route_add_title (route, "fr_FR", FST_ROUTE_TITLE_FR); - trp_service_route_add_title (route, "en_US", FST_ROUTE_TITLE_EN); - segment = trp_service_route_add_segment (route, FST_ROUTE_FST_SEG_LAT, FST_ROUTE_FST_SEG_LON); - trp_service_route_add_segment_description (route, segment, "en_US", FST_ROUTE_FST_SEG_DESC_EN); - trp_service_route_add_segment_description (route, segment, "fr_FR", FST_ROUTE_FST_SEG_DESC_FR); - segment = trp_service_route_add_segment (route, FST_ROUTE_SND_SEG_LAT, FST_ROUTE_SND_SEG_LON); - trp_service_route_add_segment_description (route, segment, "en_US", FST_ROUTE_SND_SEG_DESC_EN); - - return route; + trp_route_add_title (route, "fr_FR", FST_ROUTE_TITLE_FR); + trp_route_add_title (route, "en_US", FST_ROUTE_TITLE_EN); + segment = trp_route_add_segment (route, FST_ROUTE_FST_SEG_LAT, FST_ROUTE_FST_SEG_LON); + trp_route_add_segment_description (route, segment, "en_US", FST_ROUTE_FST_SEG_DESC_EN); + trp_route_add_segment_description (route, segment, "fr_FR", FST_ROUTE_FST_SEG_DESC_FR); + segment = trp_route_add_segment (route, FST_ROUTE_SND_SEG_LAT, FST_ROUTE_SND_SEG_LON); + trp_route_add_segment_description (route, segment, "en_US", FST_ROUTE_SND_SEG_DESC_EN); + + return TRP_SERVICE_ROUTE (route); } TrpServiceRoute * create_second_service_route (void) { - TrpServiceRoute *route; + TrpRoute *route; guint segment; route = trp_service_route_new (SND_ROUTE_DISTANCE, SND_ROUTE_TIME); - trp_service_route_add_title (route, "fr_FR", SND_ROUTE_TITLE_FR); - trp_service_route_add_title (route, "en_US", SND_ROUTE_TITLE_EN); - segment = trp_service_route_add_segment (route, SND_ROUTE_FST_SEG_LAT, SND_ROUTE_FST_SEG_LON); - trp_service_route_add_segment_description (route, segment, "fr_FR", SND_ROUTE_FST_SEG_DESC_FR); - trp_service_route_add_segment (route, SND_ROUTE_SND_SEG_LAT, SND_ROUTE_SND_SEG_LON); + trp_route_add_title (route, "fr_FR", SND_ROUTE_TITLE_FR); + trp_route_add_title (route, "en_US", SND_ROUTE_TITLE_EN); + segment = trp_route_add_segment (route, SND_ROUTE_FST_SEG_LAT, SND_ROUTE_FST_SEG_LON); + trp_route_add_segment_description (route, segment, "fr_FR", SND_ROUTE_FST_SEG_DESC_FR); + trp_route_add_segment (route, SND_ROUTE_SND_SEG_LAT, SND_ROUTE_SND_SEG_LON); - return route; + return TRP_SERVICE_ROUTE (route); } TrpServiceRoute * create_third_service_route (void) { - TrpServiceRoute *route; + TrpRoute *route; guint segment; route = trp_service_route_new (THD_ROUTE_DISTANCE, THD_ROUTE_TIME); - trp_service_route_add_title (route, "en_US", THD_ROUTE_TITLE_EN); - segment = trp_service_route_add_segment (route, THD_ROUTE_FST_SEG_LAT, THD_ROUTE_FST_SEG_LON); - trp_service_route_add_segment_description (route, segment, "fr_FR", THD_ROUTE_FST_SEG_DESC_FR); - trp_service_route_add_segment (route, THD_ROUTE_SND_SEG_LAT, THD_ROUTE_SND_SEG_LON); + trp_route_add_title (route, "en_US", THD_ROUTE_TITLE_EN); + segment = trp_route_add_segment (route, THD_ROUTE_FST_SEG_LAT, THD_ROUTE_FST_SEG_LON); + trp_route_add_segment_description (route, segment, "fr_FR", THD_ROUTE_FST_SEG_DESC_FR); + trp_route_add_segment (route, THD_ROUTE_SND_SEG_LAT, THD_ROUTE_SND_SEG_LON); - return route; + return TRP_SERVICE_ROUTE (route); } diff --git a/traprain-common/libtraprain-common.pc.in b/traprain-common/libtraprain-common.pc.in new file mode 100644 index 0000000..2c50a43 --- /dev/null +++ b/traprain-common/libtraprain-common.pc.in @@ -0,0 +1,12 @@ +prefix=@prefix@ +exec_prefix=@exec_prefix@ +libdir=@libdir@ +includedir=@includedir@ + +Name: traprain-common-@TRP_API_VERSION@ +Description: Library implementing common bits shared by the client and service libraries +Version: @VERSION@ +Libs: -L${libdir} -ltraprain-common-@TRP_API_VERSION@ +Cflags: -I${includedir}/traprain-common-@TRP_API_VERSION@ +Requires: @TRP_COMMON_PACKAGE_REQUIRES@ +Requires.private: @TRP_COMMON_PACKAGE_REQUIRES_PRIVATE@ diff --git a/traprain-common/route-internal.h b/traprain-common/route-internal.h new file mode 100644 index 0000000..6ac70c2 --- /dev/null +++ b/traprain-common/route-internal.h @@ -0,0 +1,43 @@ +/* vim:set et sw=2 cin cino=t0,f0,(0,{s,>2s,n-s,^-s,e2s: */ +/* + * Copyright © 2016 Collabora Ltd. + * + * SPDX-License-Identifier: MPL-2.0 + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#ifndef __TRAPRAIN_COMMON_ROUTE_INTERNAL_H__ +#define __TRAPRAIN_COMMON_ROUTE_INTERNAL_H__ + +#include <glib-object.h> + +#include "route.h" + +#include "languages-map-internal.h" + +G_BEGIN_DECLS + +typedef struct +{ + gdouble latitude; + gdouble longitude; + _TrpLanguagesMap *description; /* owned */ +} _TrpSegment; + +struct _TrpRoute +{ + GObject parent; + + guint total_distance; + guint total_time; + _TrpLanguagesMap *title; /* owned */ + GPtrArray /* <owned _TrpSegment> */ *geometry; /* owned */ + + gboolean writable; +}; + +G_END_DECLS + +#endif /* __TRAPRAIN_COMMON_ROUTE_INTERNAL_H__ */ diff --git a/traprain-common/route.c b/traprain-common/route.c new file mode 100644 index 0000000..5c12e59 --- /dev/null +++ b/traprain-common/route.c @@ -0,0 +1,174 @@ +/* vim:set et sw=2 cin cino=t0,f0,(0,{s,>2s,n-s,^-s,e2s: */ +/* + * Copyright © 2016 Collabora Ltd. + * + * SPDX-License-Identifier: MPL-2.0 + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include "config.h" + +#include "route-internal.h" +#include "route.h" + +G_DEFINE_TYPE (TrpRoute, trp_route, G_TYPE_OBJECT) + +/** + * TrpRoute: + * + * Object representing a potential navigation route to expose on the bus. + * + * Routes are defined by ordered segments from the start point + * to the destination. Use trp_route_add_segment() + * and trp_route_add_segment_description() to build the + * route. + * + * Route objects should no longer be modified once they have been + * been exposed on the bus, generally by passing it to a + * #TrpServiceNavigation. + * + * Since: UNRELEASED + */ + +static _TrpSegment * +segment_new (gdouble latitude, + gdouble longitude) +{ + _TrpSegment *s = g_slice_new0 (_TrpSegment); + + s->latitude = latitude; + s->longitude = longitude; + s->description = _trp_languages_map_new (); + + return s; +} + +static void +segment_free (_TrpSegment *s) +{ + g_array_unref (s->description); + g_slice_free (_TrpSegment, s); +} + +static void +trp_route_init (TrpRoute *self) +{ + self->geometry = g_ptr_array_new_with_free_func ((GDestroyNotify) segment_free); + self->title = _trp_languages_map_new (); + + self->writable = TRUE; +} + +static void +trp_route_dispose (GObject *object) +{ + TrpRoute *route = (TrpRoute *) object; + + g_clear_pointer (&route->title, g_array_unref); + g_clear_pointer (&route->geometry, g_ptr_array_unref); + + G_OBJECT_CLASS (trp_route_parent_class) + ->dispose (object); +} + +static void +trp_route_class_init (TrpRouteClass *klass) +{ + GObjectClass *object_class = (GObjectClass *) klass; + + object_class->dispose = trp_route_dispose; +} + +/** + * trp_route_add_title: + * @route: a #TrpRoute + * @language: a language name code, in te POSIX locale format (ISO 15897) + * @title: a human-readable title of @route in @language + * + * Set a human-readable title of @route in @language. + * @language should be in the POSIX locale format with locale identifiers + * defined by ISO 15897, like fr_BE for example. + * + * You should not call this function once @route has been exposed on the bus. + * + * Since: UNRELEASED + */ +void +trp_route_add_title (TrpRoute *route, + const gchar *language, + const gchar *title) +{ + g_return_if_fail (route != NULL); + g_return_if_fail (language != NULL); + g_return_if_fail (title != NULL); + g_return_if_fail (route->writable); + + _trp_languages_map_add (route->title, language, title); +} + +/** + * trp_route_add_segment: + * @route: a #TrpRoute + * @latitude: the latitude of the segment, between -90.0 and 90.0 + * @longitude: the longitude of the segment, between -180.0 and 180.0 + * + * Add a segment to @route at the location defined by @latitude and @longitude. + * You should then use trp_route_add_segment_description() with the index + * returned by this function to provide a human-readable description of this + * segment, if available. + * + * You should not call this function once @route has been exposed on the bus. + * + * Returns: an index representing the position of this segment in @route + * Since: UNRELEASED + */ +guint +trp_route_add_segment (TrpRoute *route, + gdouble latitude, + gdouble longitude) +{ + g_return_val_if_fail (route != NULL, 0); + g_return_val_if_fail (latitude >= -90.0 && latitude <= 90.0, 0); + g_return_val_if_fail (longitude >= -180.0 && longitude <= 180.0, 0); + g_return_val_if_fail (route->writable, 0); + + g_ptr_array_add (route->geometry, segment_new (latitude, longitude)); + + return route->geometry->len - 1; +} + +/** + * trp_route_add_segment_description: + * @route: a #TrpRoute + * @index: the index of an existing segment in @route + * @language: a language name code, in te POSIX locale format (ISO 15897) + * @description: a human-readable description of this segment in @language + * + * Set a human-readable description of the segment indexed at @index + * of @route. + * @language should be in the POSIX locale format with locale identifiers + * defined by ISO 15897, like fr_BE for example. + * + * You should not call this function once @route has been exposed on the bus. + * + * Since: UNRELEASED + */ +void +trp_route_add_segment_description (TrpRoute *route, + guint index, + const gchar *language, + const gchar *description) +{ + _TrpSegment *s; + + g_return_if_fail (route != NULL); + g_return_if_fail (index < route->geometry->len); + g_return_if_fail (language != NULL); + g_return_if_fail (description != NULL); + g_return_if_fail (route->writable); + + s = g_ptr_array_index (route->geometry, index); + _trp_languages_map_add (s->description, language, description); +} diff --git a/traprain-common/route.h b/traprain-common/route.h new file mode 100644 index 0000000..1818799 --- /dev/null +++ b/traprain-common/route.h @@ -0,0 +1,37 @@ +/* vim:set et sw=2 cin cino=t0,f0,(0,{s,>2s,n-s,^-s,e2s: */ +/* + * Copyright © 2016 Collabora Ltd. + * + * SPDX-License-Identifier: MPL-2.0 + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#ifndef __TRAPRAIN_COMMON_ROUTE_H__ +#define __TRAPRAIN_COMMON_ROUTE_H__ + +#include <glib-object.h> + +G_BEGIN_DECLS + +#define TRP_TYPE_ROUTE (trp_route_get_type ()) +/* TrpRoute can only be subclassed internally so don't use + * G_DECLARE_DERIVABLE_TYPE() */ +G_DECLARE_FINAL_TYPE (TrpRoute, trp_route, TRP, ROUTE, GObject) + +void trp_route_add_title (TrpRoute *route, + const gchar *language, + const gchar *title); + +guint trp_route_add_segment (TrpRoute *route, + gdouble latitude, + gdouble longitude); +void trp_route_add_segment_description (TrpRoute *route, + guint index, + const gchar *language, + const gchar *description); + +G_END_DECLS + +#endif /* __TRAPRAIN_COMMON_ROUTE_H__ */ diff --git a/traprain-service/navigation.c b/traprain-service/navigation.c index bc579b6..fe6241e 100644 --- a/traprain-service/navigation.c +++ b/traprain-service/navigation.c @@ -36,19 +36,6 @@ * Since: UNRELEASED */ -/** - * TrpServiceRoute: - * - * Object representing a potential navigation route. - * - * Routes are defined by ordered segments from the start point - * to the destination. Use trp_service_route_add_segment() - * and trp_service_route_add_segment_description() to build the - * route. - * - * Since: UNRELEASED - */ - struct _TrpServiceNavigation { GObject parent; @@ -210,13 +197,14 @@ complete_init_tasks (TrpServiceNavigation *self) static gboolean service_route_export (TrpServiceNavigation *self, - TrpServiceRoute *route) + TrpServiceRoute *service_route) { + TrpRoute *route = (TrpRoute *) service_route; g_autofree gchar *path = NULL; GVariantBuilder geometry, desc; guint j; - g_return_val_if_fail (route->service == NULL, FALSE); + g_return_val_if_fail (service_route->service == NULL, FALSE); g_return_val_if_fail (self->service != NULL, FALSE); if (self->route_path_count == G_MAXUINT32) @@ -227,16 +215,17 @@ service_route_export (TrpServiceNavigation *self, path = g_strdup_printf (NAVIGATION_ROUTE_PATH, self->route_path_count++); - route->object = trp_object_skeleton_new (path); - route->service = trp_org_apertis_navigation1_route_skeleton_new (); - trp_object_skeleton_set_org_apertis_navigation1_route (route->object, route->service); + service_route->object = trp_object_skeleton_new (path); + service_route->service = trp_org_apertis_navigation1_route_skeleton_new (); + route->writable = FALSE; + trp_object_skeleton_set_org_apertis_navigation1_route (service_route->object, service_route->service); - trp_org_apertis_navigation1_route_set_total_distance (route->service, route->total_distance); - trp_org_apertis_navigation1_route_set_total_time (route->service, route->total_time); + trp_org_apertis_navigation1_route_set_total_distance (service_route->service, route->total_distance); + trp_org_apertis_navigation1_route_set_total_time (service_route->service, route->total_time); /* Title */ - trp_org_apertis_navigation1_route_set_title (route->service, + trp_org_apertis_navigation1_route_set_title (service_route->service, _trp_languages_map_to_variant (route->title)); /* Geometry and GeometryDescriptions */ @@ -245,7 +234,7 @@ service_route_export (TrpServiceNavigation *self, for (j = 0; j < route->geometry->len; j++) { - _TrpServiceSegment *s = g_ptr_array_index (route->geometry, j); + _TrpSegment *s = g_ptr_array_index (route->geometry, j); g_variant_builder_add (&geometry, "(dd)", s->latitude, s->longitude); @@ -258,10 +247,10 @@ service_route_export (TrpServiceNavigation *self, } } - trp_org_apertis_navigation1_route_set_geometry (route->service, g_variant_builder_end (&geometry)); - trp_org_apertis_navigation1_route_set_geometry_descriptions (route->service, g_variant_builder_end (&desc)); + trp_org_apertis_navigation1_route_set_geometry (service_route->service, g_variant_builder_end (&geometry)); + trp_org_apertis_navigation1_route_set_geometry_descriptions (service_route->service, g_variant_builder_end (&desc)); - g_dbus_object_manager_server_export (self->object_mgr, G_DBUS_OBJECT_SKELETON (route->object)); + g_dbus_object_manager_server_export (self->object_mgr, G_DBUS_OBJECT_SKELETON (service_route->object)); return TRUE; } @@ -499,7 +488,7 @@ trp_service_navigation_add_route (TrpServiceNavigation *self, g_return_val_if_fail (TRP_SERVICE_IS_NAVIGATION (self), -1); g_return_val_if_fail (route != NULL, -1); g_return_val_if_fail (index == -1 || (index >= 0 && (guint) index < self->routes->len), -1); - g_return_val_if_fail (route->geometry->len >= 2, -1); + g_return_val_if_fail (((TrpRoute *) route)->geometry->len >= 2, -1); g_return_val_if_fail (error == NULL || *error == NULL, -1); if (!check_invalidated (self, error)) diff --git a/traprain-service/route-internal.h b/traprain-service/route-internal.h index 5ebca49..b9df142 100644 --- a/traprain-service/route-internal.h +++ b/traprain-service/route-internal.h @@ -17,21 +17,9 @@ G_BEGIN_DECLS -typedef struct -{ - gdouble latitude; - gdouble longitude; - _TrpLanguagesMap *description; /* owned */ -} _TrpServiceSegment; - struct _TrpServiceRoute { - GObject parent; - - guint total_distance; - guint total_time; - _TrpLanguagesMap *title; /* owned */ - GPtrArray /* <owned _TrpServiceSegment> */ *geometry; /* owned */ + TrpRoute parent; TrpObjectSkeleton *object; /* owned */ TrpOrgApertisNavigation1Route *service; /* owned */ diff --git a/traprain-service/route.c b/traprain-service/route.c index 33739fc..f8c1be5 100644 --- a/traprain-service/route.c +++ b/traprain-service/route.c @@ -12,33 +12,23 @@ #include "route-internal.h" -G_DEFINE_TYPE (TrpServiceRoute, trp_service_route, G_TYPE_OBJECT) +G_DEFINE_TYPE (TrpServiceRoute, trp_service_route, TRP_TYPE_ROUTE) -static _TrpServiceSegment * -segment_new (gdouble latitude, - gdouble longitude) -{ - _TrpServiceSegment *s = g_slice_new0 (_TrpServiceSegment); - - s->latitude = latitude; - s->longitude = longitude; - s->description = _trp_languages_map_new (); - - return s; -} - -static void -segment_free (_TrpServiceSegment *s) -{ - g_array_unref (s->description); - g_slice_free (_TrpServiceSegment, s); -} +/** + * TrpServiceRoute: + * + * Object representing a potential navigation route to expose using + * #TrpServiceNavigation. + * + * Use the #TrpRoute API to build the route: trp_route_add_title(), + * trp_route_add_segment(), trp_route_add_segment_description(). + * + * Since: UNRELEASED + */ static void trp_service_route_init (TrpServiceRoute *self) { - self->geometry = g_ptr_array_new_with_free_func ((GDestroyNotify) segment_free); - self->title = _trp_languages_map_new (); } static void @@ -46,9 +36,6 @@ trp_service_route_dispose (GObject *object) { TrpServiceRoute *route = (TrpServiceRoute *) object; - g_clear_pointer (&route->title, g_array_unref); - g_clear_pointer (&route->geometry, g_ptr_array_unref); - g_clear_object (&route->service); g_clear_object (&route->object); @@ -71,8 +58,8 @@ trp_service_route_class_init (TrpServiceRouteClass *klass) * * Create a new TrpServiceRoute representing a potential navigation route. * - * One should generally create a route, set its title using trp_service_route_add_title() - * and add the geometry of the route using trp_service_route_add_segment(). + * One should generally create a route, set its title using trp_route_add_title() + * and add the geometry of the route using trp_route_add_segment(). * Once the route has been fully defined it should be passed to the #TrpServiceNavigation * for publishing using either trp_service_navigation_add_route() or * trp_service_navigation_set_routes(). @@ -80,109 +67,14 @@ trp_service_route_class_init (TrpServiceRouteClass *klass) * Returns: (transfer full): a new #TrpServiceRoute * Since: UNRELEASED */ -TrpServiceRoute * +TrpRoute * trp_service_route_new (guint total_distance, guint total_time) { - TrpServiceRoute *route = g_object_new (TRP_SERVICE_TYPE_ROUTE, NULL); + TrpRoute *route = g_object_new (TRP_SERVICE_TYPE_ROUTE, NULL); route->total_distance = total_distance; route->total_time = total_time; return route; } - -/** - * trp_service_route_add_title: - * @route: a #TrpServiceRoute - * @language: a language name code, in te POSIX locale format (ISO 15897) - * @title: a human-readable title of @route in @language - * - * Set a human-readable title of @route in @language. - * @language should be in the POSIX locale format with locale identifiers - * defined by ISO 15897, like fr_BE for example. - * - * You should not call this function once @route has been passed to the - * #TrpServiceNavigation. - * - * Since: UNRELEASED - */ -void -trp_service_route_add_title (TrpServiceRoute *route, - const gchar *language, - const gchar *title) -{ - g_return_if_fail (route != NULL); - g_return_if_fail (language != NULL); - g_return_if_fail (title != NULL); - g_return_if_fail (route->service == NULL); - - _trp_languages_map_add (route->title, language, title); -} - -/** - * trp_service_route_add_segment: - * @route: a #TrpServiceRoute - * @latitude: the latitude of the segment, between -90.0 and 90.0 - * @longitude: the longitude of the segment, between -180.0 and 180.0 - * - * Add a segment to @route at the location defined by @latitude and @longitude. - * You should then use trp_service_route_add_segment_description() with the index - * returned by this function to provide a human-readable description of this - * segment, if available. - * - * You should not call this function once @route has been passed to the - * #TrpServiceNavigation. - * - * Returns: an index representing the position of this segment in @route - * Since: UNRELEASED - */ -guint -trp_service_route_add_segment (TrpServiceRoute *route, - gdouble latitude, - gdouble longitude) -{ - g_return_val_if_fail (route != NULL, 0); - g_return_val_if_fail (route->service == NULL, 0); - g_return_val_if_fail (latitude >= -90.0 && latitude <= 90.0, 0); - g_return_val_if_fail (longitude >= -180.0 && longitude <= 180.0, 0); - - g_ptr_array_add (route->geometry, segment_new (latitude, longitude)); - - return route->geometry->len - 1; -} - -/** - * trp_service_route_add_segment_description: - * @route: a #TrpServiceRoute - * @index: the index of an existing segment in @route - * @language: a language name code, in te POSIX locale format (ISO 15897) - * @description: a human-readable description of this segment in @language - * - * Set a human-readable description of the segment indexed at @index - * of @route. - * @language should be in the POSIX locale format with locale identifiers - * defined by ISO 15897, like fr_BE for example. - * - * You should not call this function once @route has been passed to the - * #TrpServiceNavigation. - * - * Since: UNRELEASED - */ -void -trp_service_route_add_segment_description (TrpServiceRoute *route, - guint index, - const gchar *language, - const gchar *description) -{ - _TrpServiceSegment *s; - - g_return_if_fail (route != NULL); - g_return_if_fail (index < route->geometry->len); - g_return_if_fail (language != NULL); - g_return_if_fail (description != NULL); - g_return_if_fail (route->service == NULL); - - s = g_ptr_array_index (route->geometry, index); - _trp_languages_map_add (s->description, language, description); -} diff --git a/traprain-service/route.h b/traprain-service/route.h index f1cb144..cd7af4e 100644 --- a/traprain-service/route.h +++ b/traprain-service/route.h @@ -13,25 +13,15 @@ #include <glib-object.h> +#include "traprain-common/route-internal.h" + G_BEGIN_DECLS #define TRP_SERVICE_TYPE_ROUTE (trp_service_route_get_type ()) -G_DECLARE_FINAL_TYPE (TrpServiceRoute, trp_service_route, TRP_SERVICE, ROUTE, GObject) - -TrpServiceRoute *trp_service_route_new (guint total_distance, - guint total_time); - -void trp_service_route_add_title (TrpServiceRoute *route, - const gchar *language, - const gchar *title); - -guint trp_service_route_add_segment (TrpServiceRoute *route, - gdouble latitude, - gdouble longitude); -void trp_service_route_add_segment_description (TrpServiceRoute *route, - guint index, - const gchar *language, - const gchar *description); +G_DECLARE_FINAL_TYPE (TrpServiceRoute, trp_service_route, TRP_SERVICE, ROUTE, TrpRoute) + +TrpRoute *trp_service_route_new (guint total_distance, + guint total_time); G_END_DECLS |