Many extensions can be added through a simple configuration file.
Currently, only user's home directory is used to load such configuration.

This patch add support for some standard locations in system.
The behavior can be affected by XDG_DATA_HOME and XDG_DATA_DIRS standard
environment variables.

Signed-off-by: Guilhem Bonnefille <guilhem.bonnefi...@gmail.com>

---
 Makefile.am                     |    2 +-
 configure.ac                    |    1 +
 data/Makefile.am                |    4 ++
 data/external_tools.xml         |   21 +++++++
 data/goto_tools.xml             |   33 +++++++++++
 data/maps.xml                   |   78 ++++++++++++++++++++++++++
 doc/examples/external_tools.xml |   22 +++-----
 doc/examples/goto_tools.xml     |    9 +--
 doc/examples/maps.xml           |   51 ++---------------
 doc/viking.xml                  |   29 ++++++++++
 help/C/viking.xml               |    9 ++-
 src/Makefile.am                 |    4 ++
 src/curl_download.c             |    1 +
 src/dir.c                       |  118 +++++++++++++++++++++++++++++++++++++++
 src/dir.h                       |   31 ++++++++++
 src/file.c                      |   34 +----------
 src/file.h                      |    2 +-
 src/modules.c                   |   46 +++++++++++++--
 src/preferences.c               |    1 +
 19 files changed, 384 insertions(+), 112 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index ef31dde..b50ced2 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,4 +1,4 @@
-SUBDIRS = src test doc po help
+SUBDIRS = src test data doc po help
 
 INTLTOOL = \
        intltool-extract.in \
diff --git a/configure.ac b/configure.ac
index 753f460..98d8368 100644
--- a/configure.ac
+++ b/configure.ac
@@ -348,6 +348,7 @@ AC_CONFIG_FILES([Makefile
                 po/Makefile.in
                  test/Makefile
                  help/Makefile
+                 data/Makefile
                  doc/Makefile
                  doc/examples/Makefile])
 if test x$enable_gtk_doc = xyes; then
diff --git a/data/Makefile.am b/data/Makefile.am
new file mode 100644
index 0000000..a726185
--- /dev/null
+++ b/data/Makefile.am
@@ -0,0 +1,4 @@
+pkgdata_DATA = maps.xml external_tools.xml goto_tools.xml
+
+EXTRA_DIST = \
+       $(pkgdata_DATA)
diff --git a/data/external_tools.xml b/data/external_tools.xml
new file mode 100644
index 0000000..493fd99
--- /dev/null
+++ b/data/external_tools.xml
@@ -0,0 +1,21 @@
+<objects>
+  <object class="VikWebtoolCenter">
+    <property name="label">Letuffe (Hiking)</property>
+    <property 
name="url">http://beta.letuffe.org/?layers=0B000FFFFFFFFFFFF&amp;lat=%s&amp;lon=%s&amp;zoom=%d</property>
+    <property name="id">42</property>
+  </object>
+  <object class="VikWebtoolCenter">
+    <property name="label">Osmose</property>
+    <property 
name="url">http://osmose.openstreetmap.fr/map/cgi-bin/index.py?layers=B00000FFFFFFFFFFFFT&amp;lat=%s&amp;lon=%s&amp;zoom=%d</property>
+    <property name="id">43</property>
+  </object>
+  <object class="VikWebtoolCenter">
+    <property name="label">NLS: Historic Map of GB</property>
+    <property 
name="url">http://nls.tileserver.com?lat=%s&amp;lng=%s&amp;zoom=%d</property>
+    <property name="id">44</property>
+  </object>
+  <object class="VikWebtoolCenter">
+    <property name="label">Yahoo! Maps</property>
+    <property 
name="url">http://maps.yahoo.com/#lat=%s&amp;lon=%s&amp;zoom=%d</property>
+  </object>
+</objects>
diff --git a/data/goto_tools.xml b/data/goto_tools.xml
new file mode 100644
index 0000000..2da6756
--- /dev/null
+++ b/data/goto_tools.xml
@@ -0,0 +1,33 @@
+<objects>
+  <!-- Latitude and longitude are XML elements -->
+  <object class="VikGotoXmlTool">
+    <property name="label">Geonames</property>
+    <property 
name="url-format">http://ws.geonames.org/search?q=%s&amp;maxRows=1&amp;lang=es&amp;style=short</property>
+    <property name="lat-path">/geonames/geoname/lat</property>
+    <property name="lon-path">/geonames/geoname/lng</property>
+  </object>
+  <!-- Latitude and longitude are XML attributes -->
+  <object class="VikGotoXmlTool">
+    <property name="label">Name finder</property>
+    <property 
name="url-format">http://gazetteer.openstreetmap.org/namefinder/search.xml?find=%s&amp;max=1</property>
+    <property name="lat-path">/searchresults/named</property>
+    <property name="lat-attr">lat</property>
+    <property name="lon-path">/searchresults/named</property>
+    <property name="lon-attr">lon</property>
+  </object>
+  <!-- Latitude and longitude are XML attributes (XPATH format) -->
+  <object class="VikGotoXmlTool">
+    <property name="label">Nominatim</property>
+    <property 
name="url-format">http://nominatim.openstreetmap.org/search?q=%s&amp;format=xml</property>
+    <property name="lat-path">/searchresults/place@lat</property>
+    <property name="lon-path">/searchresults/place@lon</property>
+  </object>
+  <object class="VikGotoXmlTool">
+    <property name="label">Mapquest Nominatim</property>
+    <property 
name="url-format">http://open.mapquestapi.com/nominatim/v1/search?format=xml&amp;limit=1&amp;q=%s</property>
+    <property name="lat-path">/searchresults/place</property>
+    <property name="lat-attr">lat</property>
+    <property name="lon-path">/searchresults/place</property>
+    <property name="lon-attr">lon</property>
+  </object>
+</objects>
diff --git a/data/maps.xml b/data/maps.xml
new file mode 100644
index 0000000..f75b2e3
--- /dev/null
+++ b/data/maps.xml
@@ -0,0 +1,78 @@
+<objects>
+<!-- Built in -->
+  <object class="VikSlippyMapSource">
+    <property name="label">OpenStreetMap (Mapnik)</property>
+    <property name="hostname">tile.openstreetmap.org</property>
+    <property name="url">/%d/%d/%d.png</property>
+    <property name="id">13</property>
+    <property name="copyright">© OpenStreetMap contributors</property>
+    <property name="license">CC-BY-SA</property>
+    <property 
name="license-url">http://www.openstreetmap.org/copyright</property>
+  </object>
+<!-- Built in -->
+  <object class="VikSlippyMapSource">
+    <property name="label">OpenStreetMap (Cycle)</property>
+    <property name="hostname">b.tile.opencyclemap.org</property>
+    <property name="url">/cycle/%d/%d/%d.png</property>
+    <property name="id">17</property>
+    <property name="copyright">© OpenStreetMap contributors</property>
+    <property name="license">CC-BY-SA</property>
+    <property 
name="license-url">http://www.openstreetmap.org/copyright</property>
+  </object>
+  <object class="VikWmscMapSource">
+    <property name="id">18</property>
+    <property name="label">OpenStreetMap (WMS)</property>
+    <property name="hostname">full.wms.geofabrik.de</property>
+    <property 
name="url">/std/demo_key?LAYERS=osm-full&amp;FORMAT=image/png&amp;SERVICE=WMS&amp;VERSION=1.1.1&amp;REQUEST=GetMap&amp;STYLES=&amp;EXCEPTIONS=&amp;SRS=EPSG:4326&amp;BBOX=%s,%s,%s,%s&amp;WIDTH=256&amp;HEIGHT=256</property>
+    <property name="check-file-server-time">FALSE</property>
+    <property name="copyright">© OpenStreetMap contributors</property>
+    <property name="license">CC-BY-SA</property>
+    <property 
name="license-url">http://www.openstreetmap.org/copyright</property>
+  </object>
+  <object class="VikTmsMapSource">
+    <property name="id">50</property>
+    <property name="label">SPOTMaps</property>
+    <property name="hostname">spotmaps.youmapps.org</property>
+    <property 
name="url">/spotmaps4osm/1.0.0/spotmaps4osm/%d/%d/%d.jpg</property>
+    <property name="check-file-server-time">FALSE</property>
+    <property name="copyright">© Cnes/Spot Image</property>
+    <property name="license">CC-by-SA 2.0 / ODBl</property>
+    <property 
name="license-url">http://www.youmapps.org/licenses/EULA-OSM-en.html</property>
+  </object>
+  <object class="VikSlippyMapSource">
+    <property name="label">Letuffe/hiking</property>
+    <property name="hostname">beta.letuffe.org</property>
+    <property name="url">/tiles/renderer.py/hiking/%d/%d/%d.png</property>
+    <property name="id">100</property>
+    <property name="copyright">© OpenStreetMap contributors</property>
+    <property name="license">CC-BY-SA</property>
+    <property 
name="license-url">http://www.openstreetmap.org/copyright</property>
+  </object>
+  <object class="VikSlippyMapSource">
+    <property name="label">Public Transport Map </property>
+    <property name="hostname">tile.xn--pnvkarte-m4a.de</property>
+    <property name="url">/tilegen/%0d/%d/%d.png</property>
+    <property name="id">101</property>
+    <property name="copyright">© OpenStreetMap contributors</property>
+    <property name="license">CC-BY-SA</property>
+    <property 
name="license-url">http://www.openstreetmap.org/copyright</property>
+  </object>
+  <object class="VikSlippyMapSource">
+    <property name="label">Cyclemap (Cloudmade)</property>
+    <property name="hostname">a.andy.sandbox.cloudmade.com</property>
+    <property name="url">/tiles/cycle/%d/%d/%d.png</property>
+    <property name="id">102</property>
+    <property name="copyright">© OpenStreetMap contributors</property>
+    <property name="license">CC-BY-SA</property>
+    <property 
name="license-url">http://www.openstreetmap.org/copyright</property>
+  </object>
+  <object class="VikSlippyMapSource">
+    <property name="label">Bridle Map</property>
+    <property name="hostname">opentiles.com</property>
+    <property name="url">/nop/tiles/%d/%d/%d.png</property>
+    <property name="id">103</property>
+    <property name="copyright">© OpenStreetMap contributors</property>
+    <property name="license">CC-BY-SA</property>
+    <property 
name="license-url">http://www.openstreetmap.org/copyright</property>
+  </object>
+</objects>
diff --git a/doc/examples/external_tools.xml b/doc/examples/external_tools.xml
index 493fd99..473f73c 100644
--- a/doc/examples/external_tools.xml
+++ b/doc/examples/external_tools.xml
@@ -1,21 +1,13 @@
+<!-- This file is an example for viking's extension system. -->
 <objects>
-  <object class="VikWebtoolCenter">
-    <property name="label">Letuffe (Hiking)</property>
-    <property 
name="url">http://beta.letuffe.org/?layers=0B000FFFFFFFFFFFF&amp;lat=%s&amp;lon=%s&amp;zoom=%d</property>
-    <property name="id">42</property>
-  </object>
-  <object class="VikWebtoolCenter">
-    <property name="label">Osmose</property>
-    <property 
name="url">http://osmose.openstreetmap.fr/map/cgi-bin/index.py?layers=B00000FFFFFFFFFFFFT&amp;lat=%s&amp;lon=%s&amp;zoom=%d</property>
-    <property name="id">43</property>
-  </object>
-  <object class="VikWebtoolCenter">
-    <property name="label">NLS: Historic Map of GB</property>
-    <property 
name="url">http://nls.tileserver.com?lat=%s&amp;lng=%s&amp;zoom=%d</property>
-    <property name="id">44</property>
-  </object>
+  <!-- The VikWebtoolCenter allows to declare any Webtool using a logic based 
on center coordinates and zoom level value. -->
   <object class="VikWebtoolCenter">
     <property name="label">Yahoo! Maps</property>
     <property 
name="url">http://maps.yahoo.com/#lat=%s&amp;lon=%s&amp;zoom=%d</property>
   </object>
+  <!-- The VikWebtoolBounds allows to declare any Webtool using a logic based 
on bounds coordinates. -->
+  <object class="VikWebtoolBounds">
+    <property name="label">Local port 8111 (eg JOSM)</property>
+    <property 
name="url">http://localhost:8111/load_and_zoom?left=%s&amp;right=%s&amp;bottom=%s&amp;top=%s</property>
+  </object>
 </objects>
diff --git a/doc/examples/goto_tools.xml b/doc/examples/goto_tools.xml
index 2da6756..aded2b6 100644
--- a/doc/examples/goto_tools.xml
+++ b/doc/examples/goto_tools.xml
@@ -1,3 +1,4 @@
+<!-- This file is an example for goto engine definitions. -->
 <objects>
   <!-- Latitude and longitude are XML elements -->
   <object class="VikGotoXmlTool">
@@ -22,12 +23,4 @@
     <property name="lat-path">/searchresults/place@lat</property>
     <property name="lon-path">/searchresults/place@lon</property>
   </object>
-  <object class="VikGotoXmlTool">
-    <property name="label">Mapquest Nominatim</property>
-    <property 
name="url-format">http://open.mapquestapi.com/nominatim/v1/search?format=xml&amp;limit=1&amp;q=%s</property>
-    <property name="lat-path">/searchresults/place</property>
-    <property name="lat-attr">lat</property>
-    <property name="lon-path">/searchresults/place</property>
-    <property name="lon-attr">lon</property>
-  </object>
 </objects>
diff --git a/doc/examples/maps.xml b/doc/examples/maps.xml
index f75b2e3..c63870f 100644
--- a/doc/examples/maps.xml
+++ b/doc/examples/maps.xml
@@ -1,5 +1,6 @@
+<!-- This file is an example for map source definitions. -->
 <objects>
-<!-- Built in -->
+  <!-- The VikSlippyMapSource allows to declare any map's source working like 
OpenStreetMap. -->
   <object class="VikSlippyMapSource">
     <property name="label">OpenStreetMap (Mapnik)</property>
     <property name="hostname">tile.openstreetmap.org</property>
@@ -9,16 +10,7 @@
     <property name="license">CC-BY-SA</property>
     <property 
name="license-url">http://www.openstreetmap.org/copyright</property>
   </object>
-<!-- Built in -->
-  <object class="VikSlippyMapSource">
-    <property name="label">OpenStreetMap (Cycle)</property>
-    <property name="hostname">b.tile.opencyclemap.org</property>
-    <property name="url">/cycle/%d/%d/%d.png</property>
-    <property name="id">17</property>
-    <property name="copyright">© OpenStreetMap contributors</property>
-    <property name="license">CC-BY-SA</property>
-    <property 
name="license-url">http://www.openstreetmap.org/copyright</property>
-  </object>
+  <!-- The VikWmscMapSource allows to declare any WMS or WMS-C service. -->
   <object class="VikWmscMapSource">
     <property name="id">18</property>
     <property name="label">OpenStreetMap (WMS)</property>
@@ -29,6 +21,7 @@
     <property name="license">CC-BY-SA</property>
     <property 
name="license-url">http://www.openstreetmap.org/copyright</property>
   </object>
+  <!-- The VikTmsMapSource allows to declare any TMS service. -->
   <object class="VikTmsMapSource">
     <property name="id">50</property>
     <property name="label">SPOTMaps</property>
@@ -39,40 +32,4 @@
     <property name="license">CC-by-SA 2.0 / ODBl</property>
     <property 
name="license-url">http://www.youmapps.org/licenses/EULA-OSM-en.html</property>
   </object>
-  <object class="VikSlippyMapSource">
-    <property name="label">Letuffe/hiking</property>
-    <property name="hostname">beta.letuffe.org</property>
-    <property name="url">/tiles/renderer.py/hiking/%d/%d/%d.png</property>
-    <property name="id">100</property>
-    <property name="copyright">© OpenStreetMap contributors</property>
-    <property name="license">CC-BY-SA</property>
-    <property 
name="license-url">http://www.openstreetmap.org/copyright</property>
-  </object>
-  <object class="VikSlippyMapSource">
-    <property name="label">Public Transport Map </property>
-    <property name="hostname">tile.xn--pnvkarte-m4a.de</property>
-    <property name="url">/tilegen/%0d/%d/%d.png</property>
-    <property name="id">101</property>
-    <property name="copyright">© OpenStreetMap contributors</property>
-    <property name="license">CC-BY-SA</property>
-    <property 
name="license-url">http://www.openstreetmap.org/copyright</property>
-  </object>
-  <object class="VikSlippyMapSource">
-    <property name="label">Cyclemap (Cloudmade)</property>
-    <property name="hostname">a.andy.sandbox.cloudmade.com</property>
-    <property name="url">/tiles/cycle/%d/%d/%d.png</property>
-    <property name="id">102</property>
-    <property name="copyright">© OpenStreetMap contributors</property>
-    <property name="license">CC-BY-SA</property>
-    <property 
name="license-url">http://www.openstreetmap.org/copyright</property>
-  </object>
-  <object class="VikSlippyMapSource">
-    <property name="label">Bridle Map</property>
-    <property name="hostname">opentiles.com</property>
-    <property name="url">/nop/tiles/%d/%d/%d.png</property>
-    <property name="id">103</property>
-    <property name="copyright">© OpenStreetMap contributors</property>
-    <property name="license">CC-BY-SA</property>
-    <property 
name="license-url">http://www.openstreetmap.org/copyright</property>
-  </object>
 </objects>
diff --git a/doc/viking.xml b/doc/viking.xml
index 94051b8..ce7ccde 100644
--- a/doc/viking.xml
+++ b/doc/viking.xml
@@ -348,6 +348,35 @@ and docbook-xsl in your Build-Depends control field.
       </varlistentry>
     </variablelist>
 
+       <para>Extension files (<filename>maps.xml</filename>,
+       <filename>goto_tools.xml</filename>,
+       <filename>external_tools.xml</filename>)
+       are also searched in <filename>/etc/viking</filename> and
+       <filename>/usr/share/viking</filename> directories (or related in your 
system).</para>
+
+  </refsect1>
+  <refsect1>
+    <title>ENVIRONMENT</title>
+
+    <variablelist>
+      <varlistentry>
+        <term><envar>XDG_DATA_HOME</envar></term>
+        <listitem>
+          <para>Optional directory to look for extension files
+          (<filename>maps.xml</filename>, <filename>goto_tools.xml</filename>,
+          <filename>external_tools.xml</filename>).</para>
+       </listitem>
+      </varlistentry>
+      <varlistentry>
+        <term><envar>XDG_DATA_DIRS</envar></term>
+        <listitem>
+          <para>Path used to change the directories scanned for extension files
+          (<filename>maps.xml</filename>, <filename>goto_tools.xml</filename>,
+          <filename>external_tools.xml</filename>).</para>
+       </listitem>
+      </varlistentry>
+    </variablelist>
+
   </refsect1>
   <refsect1>
     <title>AUTHOR</title>
diff --git a/help/C/viking.xml b/help/C/viking.xml
index 996d923..94e9fe8 100644
--- a/help/C/viking.xml
+++ b/help/C/viking.xml
@@ -1494,7 +1494,7 @@ See <ulink 
url="http://wiki.openstreetmap.org/wiki/API_v0.6#GPS_Traces"/> for fu
       </section>
 
       <section>
-        <title>Webtools</title>
+        <title>External tools</title>
         <para>It is possible to add new external tools. The file is 
<filename>~/.viking/external_tools.xml</filename>.</para>
         <para>An example of the file in the distribution 
<filename>doc/examples/external_tools.xml</filename>.</para>
         <para>Currently, there is a single object class available: 
VikWebtoolCenter. Such feature allows to declare any Webtool using a logic 
based on center coordinates and zoom level value.</para>
@@ -1515,6 +1515,13 @@ See <ulink 
url="http://wiki.openstreetmap.org/wiki/API_v0.6#GPS_Traces"/> for fu
           </variablelist>
         </para>
       </section>
+      
+      <section>
+        <title>More</title>
+        <para>Note that, on <trademark>UNIX</trademark> like systems, the 
extension files (<filename>maps.xml</filename>, 
<filename>goto_tools.xml</filename>, <filename>external_tools.xml</filename>) 
are also searched in <filename>/etc/viking</filename> and 
<filename>/usr/share/viking</filename> directories (or related in your 
system).</para>
+        <para>The <envar>XDG_DATA_DIRS</envar> environment variable can be 
used to change these directories.</para>
+        <para>The <envar>XDG_DATA_HOME</envar> environment variable s also 
used (if set) to look for these extension files.</para>
+      </section>
 
 </section>
 
diff --git a/src/Makefile.am b/src/Makefile.am
index f5e9228..3262309 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -60,6 +60,7 @@ libviking_a_SOURCES = \
        coords.c coords.h \
        gpsmapper.c gpsmapper.h \
        gpspoint.c gpspoint.h \
+       dir.c dir.h \
        file.c file.h \
        authors.h \
        documenters.h \
@@ -184,6 +185,9 @@ LDADD           = libviking.a $(PACKAGE_LIBS) @EXPAT_LIBS@ 
@LIBCURL@ icons/libic
 AM_CFLAGS              = -Wall -g -D_GNU_SOURCE \
        $(PACKAGE_CFLAGS) \
        @EXPAT_CFLAGS@ @LIBCURL_CPPFLAGS@
+AM_CPPFLAGS            = \
+       -DVIKING_DATADIR=\""$(pkgdatadir)"\" \
+       -DVIKING_SYSCONFDIR=\""$(sysconfdir)/$(PACKAGE)"\"
 
 @INTLTOOL_DESKTOP_RULE@
 
diff --git a/src/curl_download.c b/src/curl_download.c
index 961959c..2ef4c5b 100644
--- a/src/curl_download.c
+++ b/src/curl_download.c
@@ -41,6 +41,7 @@
 #include <curl/curl.h>
 
 #include "background.h"
+#include "dir.h"
 #include "file.h"
 #include "globals.h"
 #include "curl_download.h"
diff --git a/src/dir.c b/src/dir.c
new file mode 100644
index 0000000..723d0bc
--- /dev/null
+++ b/src/dir.c
@@ -0,0 +1,118 @@
+/*
+ * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
+ *
+ * Copyright (C) 2003-2005, Evan Battaglia <gtoe...@gmx.net>
+ * Copyright (C) 2012, Guilhem Bonnefille <guilhem.bonnefi...@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+#include "viking.h"
+
+#include <stdlib.h>
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+#include <glib.h>
+#include <glib/gstdio.h>
+
+const gchar *a_get_viking_dir()
+{
+  static gchar *viking_dir = NULL;
+
+  // TODO: use g_get_user_config_dir ?
+
+  if (!viking_dir) {
+    const gchar *home = g_getenv("HOME");
+    if (!home || g_access(home, W_OK))
+      home = g_get_home_dir ();
+#ifdef HAVE_MKDTEMP
+    if (!home || g_access(home, W_OK))
+    {
+      static gchar temp[] = {"/tmp/vikXXXXXX"};
+      home = mkdtemp(temp);
+    }
+#endif
+    if (!home || g_access(home, W_OK))
+      /* Fatal error */
+      g_critical("Unable to find a base directory");
+
+    /* Build the name of the directory */
+#ifdef __APPLE__
+    viking_dir = g_build_filename(home, "/Library/Application Support/Viking", 
NULL);
+#else
+    viking_dir = g_build_filename(home, ".viking", NULL);
+#endif
+    if (g_file_test(viking_dir, G_FILE_TEST_EXISTS) == FALSE)
+      g_mkdir(viking_dir, 0755);
+  }
+
+  return viking_dir;
+}
+
+/**
+ * a_get_viking_data_home:
+ * 
+ * Retrieves the XDG compliant user's data directory.
+ * 
+ * Retuns: the directory (can be NULL). Should be freed with g_free.
+ */
+gchar *
+a_get_viking_data_home()
+{
+       const gchar *xdg_data_home = g_getenv("XDG_DATA_HOME");
+       if (xdg_data_home)
+       {
+               return g_build_filename(xdg_data_home, PACKAGE);
+       }
+       else
+       {
+               return NULL;
+       }
+}
+
+/**
+ * a_get_viking_data_path:
+ * 
+ * Retrieves the configuration path.
+ *
+ * Returns: list of directories to scan for data. Should be freed with 
g_strfeev.
+ */
+gchar **
+a_get_viking_data_path()
+{
+  const gchar *xdg_data_dirs = g_getenv("XDG_DATA_DIRS");
+  if (xdg_data_dirs == NULL)
+  {
+    /* Default value specified in 
+     http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
+     */
+    xdg_data_dirs = "/usr/local/share/:/usr/share/";
+  }
+  gchar **data_path = g_strsplit(xdg_data_dirs, ":", 0);
+  /* Append the viking dir */
+  gchar **path;
+  for (path = data_path ; *path != NULL ; path++)
+  {
+    gchar *dir = *path;
+    *path = g_build_filename(dir, PACKAGE, NULL);
+    g_free(dir);
+  }
+  return data_path;
+}
diff --git a/src/dir.h b/src/dir.h
new file mode 100644
index 0000000..7c221c3
--- /dev/null
+++ b/src/dir.h
@@ -0,0 +1,31 @@
+/*
+ * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
+ *
+ * Copyright (C) 2003-2005, Evan Battaglia <gtoe...@gmx.net>
+ * Copyright (C) 2012, Guilhem Bonnefille <guilhem.bonnefi...@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+#ifndef _VIKING_DIR_H
+#define _VIKING_DIR_H
+
+#include <glib.h>
+
+const gchar *a_get_viking_dir();
+gchar ** a_get_viking_data_path();
+
+#endif
diff --git a/src/file.c b/src/file.c
index 3fb200b..8e34a7e 100644
--- a/src/file.c
+++ b/src/file.c
@@ -2,6 +2,7 @@
  * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
  *
  * Copyright (C) 2003-2005, Evan Battaglia <gtoe...@gmx.net>
+ * Copyright (C) 2012, Guilhem Bonnefille <guilhem.bonnefi...@gmail.com>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -735,36 +736,3 @@ gboolean a_file_export ( VikTrwLayer *vtl, const gchar 
*filename, VikFileType_t
   return FALSE;
 }
 
-const gchar *a_get_viking_dir()
-{
-  static gchar *viking_dir = NULL;
-
-  // TODO: use g_get_user_config_dir ?
-
-  if (!viking_dir) {
-    const gchar *home = g_getenv("HOME");
-    if (!home || g_access(home, W_OK))
-      home = g_get_home_dir ();
-#ifdef HAVE_MKDTEMP
-    if (!home || g_access(home, W_OK))
-    {
-      static gchar temp[] = {"/tmp/vikXXXXXX"};
-      home = mkdtemp(temp);
-    }
-#endif
-    if (!home || g_access(home, W_OK))
-      /* Fatal error */
-      g_critical("Unable to find a base directory");
-
-    /* Build the name of the directory */
-#ifdef __APPLE__
-    viking_dir = g_build_filename(home, "/Library/Application Support/Viking", 
NULL);
-#else
-    viking_dir = g_build_filename(home, ".viking", NULL);
-#endif
-    if (g_file_test(viking_dir, G_FILE_TEST_EXISTS) == FALSE)
-      g_mkdir(viking_dir, 0755);
-  }
-
-  return viking_dir;
-}
diff --git a/src/file.h b/src/file.h
index 57c5005..4107cbf 100644
--- a/src/file.h
+++ b/src/file.h
@@ -2,6 +2,7 @@
  * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
  *
  * Copyright (C) 2003-2005, Evan Battaglia <gtoe...@gmx.net>
+ * Copyright (C) 2012, Guilhem Bonnefille <guilhem.bonnefi...@gmail.com>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -55,7 +56,6 @@ VikLoadType_t a_file_load ( VikAggregateLayer *top, 
VikViewport *vp, const gchar
 gboolean a_file_save ( VikAggregateLayer *top, gpointer vp, const gchar 
*filename );
 /* Only need to define VikTrack and trackname if the file type is 
FILE_TYPE_GPX_TRACK */
 gboolean a_file_export ( VikTrwLayer *vtl, const gchar *filename, 
VikFileType_t file_type, const gchar *trackname );
-const gchar *a_get_viking_dir();
 
 void file_write_layer_param ( FILE *f, const gchar *name, guint8 type, 
VikLayerParamData data );
 
diff --git a/src/modules.c b/src/modules.c
index 41818d5..ae7bb82 100644
--- a/src/modules.c
+++ b/src/modules.c
@@ -1,7 +1,7 @@
 /*
  * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
  *
- * Copyright (C) 2006-2010, Guilhem Bonnefille <guilhem.bonnefi...@gmail.com>
+ * Copyright (C) 2006-2012, Guilhem Bonnefille <guilhem.bonnefi...@gmail.com>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -40,7 +40,7 @@
 #include "osm-traces.h"
 #include "bluemarble.h"
 #include "geonames.h"
-#include "file.h"
+#include "dir.h"
 #include "vikmapslayer.h"
 #include "vikexttools.h"
 #include "vikgoto.h"
@@ -76,10 +76,12 @@ modules_register_gototools(VikGobjectBuilder *self, GObject 
*object)
 }
 
 static void
-modules_load_config(void)
+modules_load_config_dir(const gchar *dir)
 {
+  g_debug("Loading configurations from directory %s", dir);
+
   /* Maps sources */
-  gchar *maps = g_build_filename(a_get_viking_dir(), VIKING_MAPS_FILE, NULL);
+  gchar *maps = g_build_filename(dir, VIKING_MAPS_FILE, NULL);
   if (g_access (maps, R_OK) == 0)
   {
        VikGobjectBuilder *builder = vik_gobject_builder_new ();
@@ -89,7 +91,7 @@ modules_load_config(void)
   }
 
   /* External tools */
-  gchar *tools = g_build_filename(a_get_viking_dir(), VIKING_EXTTOOLS_FILE, 
NULL);
+  gchar *tools = g_build_filename(dir, VIKING_EXTTOOLS_FILE, NULL);
   if (g_access (tools, R_OK) == 0)
   {
        VikGobjectBuilder *builder = vik_gobject_builder_new ();
@@ -99,7 +101,7 @@ modules_load_config(void)
   }
 
   /* Go-to search engines */
-  gchar *go_to = g_build_filename(a_get_viking_dir(), VIKING_GOTOTOOLS_FILE, 
NULL);
+  gchar *go_to = g_build_filename(dir, VIKING_GOTOTOOLS_FILE, NULL);
   if (g_access (go_to, R_OK) == 0)
   {
        VikGobjectBuilder *builder = vik_gobject_builder_new ();
@@ -109,6 +111,38 @@ modules_load_config(void)
   }
 }
 
+static void
+modules_load_config(void)
+{
+  gchar * * data_dirs = a_get_viking_data_path();
+  /* Priority is standard:
+     left element is more important than right one.
+     But our logic is to load all existing files and overwrite
+     overlapping config with last recent one.
+     So, we have to process directories in reverse order. */
+  int size = 0;
+  gchar * * ptr = data_dirs;
+  while (*ptr != NULL) ptr++;
+  size = ptr - data_dirs;
+  for (; size > 0 ; size--)
+  {
+    modules_load_config_dir(data_dirs[size-1]);
+  }
+  g_strfreev(data_dirs);
+       
+  /* Check if system config is set */
+  modules_load_config_dir(VIKING_SYSCONFDIR);
+
+  const gchar *data_home = a_get_viking_data_home ();
+  if (data_home)
+  {
+    modules_load_config_dir(data_home);
+  }
+       
+  /* Check user's home config */
+  modules_load_config_dir(a_get_viking_dir());
+}
+
 void modules_init()
 {
 #ifdef VIK_CONFIG_BING
diff --git a/src/preferences.c b/src/preferences.c
index 5d41342..88cc4f2 100644
--- a/src/preferences.c
+++ b/src/preferences.c
@@ -25,6 +25,7 @@
 #include <stdio.h>
 #include <glib/gstdio.h>
 #include "preferences.h"
+#include "dir.h"
 #include "file.h"
 
 // TODO: register_group
-- 
tg: (6f931d0..) t/feature/use-many-configuration-dir (depends on: master)

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Viking-devel mailing list
Viking-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/viking-devel
Viking home page: http://viking.sf.net/

Reply via email to