]> git.street.me.uk Git - andy/viking.git/commitdiff
Fix initialization so that preferences aren't accessed too early.
authorRob Norris <rw_norris@hotmail.com>
Wed, 18 Mar 2015 01:14:57 +0000 (01:14 +0000)
committerRob Norris <rw_norris@hotmail.com>
Wed, 18 Mar 2015 01:14:57 +0000 (01:14 +0000)
Use a two stage pass, since several initialization functions now read preferences as part of their process.

This is due to the first time a_preferences_get() is called it loads any preferences values from disk,
 but of course for preferences not registered yet it can't actually understand them.
Thus subsequent initial attempts to get unregistered preferences would not return the expected values.

src/babel.c
src/babel.h
src/background.c
src/background.h
src/main.c
src/modules.c
src/modules.h
src/preferences.c
src/vikmapniklayer.c
src/vikmapniklayer.h

index ea8a62ffe7049761077402f25cf03f1173748680..ff65bce5e4deba36e120e79b7965daca6231a027 100644 (file)
@@ -678,9 +678,7 @@ static VikLayerParam prefs[] = {
 /**
  * a_babel_init:
  * 
- * Initialises babel module.
- * Mainly check existence of gpsbabel progam
- * and load all features available in that version.
+ * Just setup preferences first
  */
 void a_babel_init ()
 {
@@ -697,7 +695,17 @@ void a_babel_init ()
   vlpd.s = "gpsbabel";
 #endif
   a_preferences_register(&prefs[0], vlpd, VIKING_PREFERENCES_IO_GROUP_KEY);
+}
 
+/**
+ * a_babel_post_init:
+ *
+ * Initialises babel module.
+ * Mainly check existence of gpsbabel progam
+ * and load all features available in that version.
+ */
+void a_babel_post_init ()
+{
   // Read the current preference
   const gchar *gpsbabel = a_preferences_get(VIKING_PREFERENCES_IO_NAMESPACE "gpsbabel")->s;
   // If setting is still the UNIX default then lookup in the path - otherwise attempt to use the specified value directly.
index 9138afa8eddcbc5448d53820117d38bc9649a811..baf2cd4499aa2a7a296d16fdab9238b7d87ba3c4 100644 (file)
@@ -108,6 +108,7 @@ gboolean a_babel_convert_from_url_or_shell ( VikTrwLayer *vt, const char *input,
 gboolean a_babel_convert_to( VikTrwLayer *vt, VikTrack *track, const char *babelargs, const char *file, BabelStatusFunc cb, gpointer user_data );
 
 void a_babel_init ();
+void a_babel_post_init ();
 void a_babel_uninit ();
 
 gboolean a_babel_available ();
index 76dab8f388279f0922940b1d37e489bc3b18061d..c49e517ff3463985f29785fa71133e3a9bd8fa79 100644 (file)
@@ -260,9 +260,24 @@ static VikLayerParam prefs_mapnik[] = {
 /**
  * a_background_init:
  *
+ * Just setup any preferences.
+ */
+void a_background_init ()
+{
+#ifdef HAVE_LIBMAPNIK
+  VikLayerParamData tmp;
+  // implicit use of 'MAPNIK_PREFS_NAMESPACE' to avoid dependency issues
+  tmp.u = 1; // Default to 1 thread due to potential crashing issues
+  a_preferences_register(&prefs_mapnik[0], tmp, "mapnik");
+#endif
+}
+
+/**
+ * a_background_post_init:
+ *
  * Initialize background feature.
  */
-void a_background_init()
+void a_background_post_init()
 {
   // initialize thread pools
   gint max_threads = 10;  /* limit maximum number of threads running at one time */
@@ -282,10 +297,7 @@ void a_background_init()
   thread_pool_local = g_thread_pool_new ( (GFunc) thread_helper, NULL, max_threads, FALSE, NULL );
 
 #ifdef HAVE_LIBMAPNIK
-  VikLayerParamData tmp;
   // implicit use of 'MAPNIK_PREFS_NAMESPACE' to avoid dependency issues
-  tmp.u = 1; // Default to 1 thread due to potential crashing issues
-  a_preferences_register(&prefs_mapnik[0], tmp, "mapnik");
   guint mapnik_threads = a_preferences_get("mapnik.background_max_threads_local_mapnik")->u;
   thread_pool_local_mapnik = g_thread_pool_new ( (GFunc) thread_helper, NULL, mapnik_threads, FALSE, NULL );
 #endif
index aaa11ff931aa7e442b77a0f96c150fa6721d5927..5608ee19cd6282e08017c66901b3ec15366995e6 100644 (file)
@@ -46,6 +46,7 @@ int a_background_thread_progress ( gpointer callbackdata, gdouble fraction );
 int a_background_testcancel ( gpointer callbackdata );
 void a_background_show_window ();
 void a_background_init ();
+void a_background_post_init ();
 void a_background_uninit ();
 void a_background_add_window (VikWindow *vw);
 void a_background_remove_window (VikWindow *vw);
index 84376f8169437e956c1fb8c2b4039cfb3c2a4f48..46cf5674c26e48a78fdfa698242cbfb91ee1df33 100644 (file)
@@ -179,6 +179,15 @@ int main( int argc, char *argv[] )
   a_settings_init ();
   a_preferences_init ();
 
+ /*
+  * First stage initialization
+  *
+  * Should not use a_preferences_get() yet
+  *
+  * Since the first time a_preferences_get() is called it loads any preferences values from disk,
+  *  but of course for preferences not registered yet it can't actually understand them
+  *  so subsequent initial attempts to get those preferences return the default value, until the values have changed
+  */
   a_vik_preferences_init ();
 
   a_layer_defaults_init ();
@@ -199,6 +208,16 @@ int main( int argc, char *argv[] )
   a_toolbar_init();
   vik_routing_prefs_init();
 
+  /*
+   * Second stage initialization
+   *
+   * Can now use a_preferences_get()
+   */
+  a_background_post_init ();
+  a_babel_post_init ();
+  modules_post_init ();
+
+  // May need to initialize the Positonal TimeZone lookup
   if ( a_vik_get_time_ref_frame() == VIK_TIME_REF_WORLD )
     vu_setup_lat_lon_tz_lookup();
 
index 9ab5d40c99b37bcbb5678655d956dfe2226833b4..21325245ec10ea10e8fba5e575145ded35999926 100644 (file)
@@ -228,6 +228,11 @@ register_loadable_types(void)
   g_debug("%d types loaded", (int)sizeof(types)/(int)sizeof(GType));
 }
 
+/**
+ * First stage initialization
+ * Can not use a_get_preferences() yet...
+ * See comment in main.c
+ */
 void modules_init()
 {
 #ifdef VIK_CONFIG_BING
@@ -266,6 +271,19 @@ void modules_init()
   modules_load_config ();
 }
 
+/**
+ * modules_post_init:
+ *
+ * Secondary stage initialization
+ * Can now use a_get_preferences()
+ */
+void modules_post_init ()
+{
+#ifdef HAVE_LIBMAPNIK
+  vik_mapnik_layer_post_init();
+#endif
+}
+
 /**
  *
  */
index 5f39bd00c8c394cedb6ee796d701697f0dfaec13..3672f190ac737d14fdbe8df680a97dba09a058ee 100644 (file)
@@ -23,7 +23,7 @@
 #define __VIKING_MODULES_H
 
 void modules_init();
-
+void modules_post_init ();
 void modules_uninit();
 
 #endif
index 4cf76ad8b3e032924f5840838ad670e13add5ccd..975f54d331474b2f35f6f2c34445f7872a146b62 100644 (file)
@@ -204,6 +204,9 @@ void a_preferences_show_window(GtkWindow *parent) {
 
 void a_preferences_register(VikLayerParam *pref, VikLayerParamData defaultval, const gchar *group_key )
 {
+  // All preferences should be registered before loading
+  if ( loaded )
+    g_critical ( "REGISTERING preference %s after LOADING from " VIKING_PREFS_FILE, pref->name );
   /* copy value */
   VikLayerParam *newpref = g_new(VikLayerParam,1);
   *newpref = *pref;
@@ -242,6 +245,7 @@ void a_preferences_uninit()
 VikLayerParamData *a_preferences_get(const gchar *key)
 {
   if ( ! loaded ) {
+    g_debug ( "%s: First time: %s\n", __FUNCTION__, key );
     /* since we can't load the file in a_preferences_init (no params registered yet),
      * do it once before we get the first key. */
     preferences_load_from_file();
index ca84ecd2468b7bc8654985859a94ed80f7920541..dc64985d3bcb726103ffe893de3a78925ad7ca2a 100644 (file)
@@ -272,7 +272,7 @@ static GHashTable *requests = NULL;
 /**
  * vik_mapnik_layer_init:
  *
- * Mostly to initialize preferences
+ * Just initialize preferences
  */
 void vik_mapnik_layer_init (void)
 {
@@ -293,7 +293,15 @@ void vik_mapnik_layer_init (void)
 
        tmp.s = "carto";
        a_preferences_register(&prefs[i++], tmp, MAPNIK_PREFS_GROUP_KEY);
+}
 
+/**
+ * vik_mapnik_layer_post_init:
+ *
+ * Initialize data structures - now that reading preferences is OK to perform
+ */
+void vik_mapnik_layer_post_init (void)
+{
        tp_mutex = vik_mutex_new();
 
        // Just storing keys only
index daf88ab13e85c329b357d3e41d2b1cf103b7d2e7..bb4690d1f92e39ba7792820fcfda69714164c759 100644 (file)
@@ -39,6 +39,7 @@ GType vik_mapnik_layer_get_type ();
 typedef struct _VikMapnikLayer VikMapnikLayer;
 
 void vik_mapnik_layer_init (void);
+void vik_mapnik_layer_post_init (void);
 void vik_mapnik_layer_uninit (void);
 
 G_END_DECLS