]> git.street.me.uk Git - andy/viking.git/blame - src/dir.c
Use our standard yes/no dialog box and enable i18n of the string.
[andy/viking.git] / src / dir.c
CommitLineData
29f1598c
GB
1/*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3 *
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
5 * Copyright (C) 2012, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26#include "viking.h"
27
28#include <stdlib.h>
29#ifdef HAVE_UNISTD_H
30#include <unistd.h>
31#endif
32#include <glib.h>
33#include <glib/gstdio.h>
34
20015a31
RN
35/**
36 * For external use, free the result
37 * Made externally available primarily to detect when Viking is first run
38 */
39gchar *a_get_viking_dir_no_create()
29f1598c 40{
29f1598c
GB
41 // TODO: use g_get_user_config_dir ?
42
20015a31
RN
43 const gchar *home = g_getenv("HOME");
44 if (!home || g_access(home, W_OK))
45 home = g_get_home_dir ();
29f1598c 46#ifdef HAVE_MKDTEMP
20015a31 47 if (!home || g_access(home, W_OK))
29f1598c
GB
48 {
49 static gchar temp[] = {"/tmp/vikXXXXXX"};
50 home = mkdtemp(temp);
51 }
52#endif
20015a31
RN
53 if (!home || g_access(home, W_OK))
54 /* Fatal error */
55 g_critical("Unable to find a base directory");
29f1598c
GB
56
57 /* Build the name of the directory */
58#ifdef __APPLE__
20015a31 59 return g_build_filename(home, "/Library/Application Support/Viking", NULL);
29f1598c 60#else
20015a31 61 return g_build_filename(home, ".viking", NULL);
29f1598c 62#endif
20015a31
RN
63}
64
65static gchar *viking_dir = NULL;
66
67const gchar *a_get_viking_dir()
68{
69 if (!viking_dir) {
70 viking_dir = a_get_viking_dir_no_create ();
29f1598c 71 if (g_file_test(viking_dir, G_FILE_TEST_EXISTS) == FALSE)
fd437981
RN
72 if ( g_mkdir(viking_dir, 0755) != 0 )
73 g_warning ( "%s: Failed to create directory %s", __FUNCTION__, viking_dir );
29f1598c 74 }
29f1598c
GB
75 return viking_dir;
76}
77
78/**
79 * a_get_viking_data_home:
80 *
81 * Retrieves the XDG compliant user's data directory.
82 *
83 * Retuns: the directory (can be NULL). Should be freed with g_free.
84 */
85gchar *
86a_get_viking_data_home()
87{
88 const gchar *xdg_data_home = g_getenv("XDG_DATA_HOME");
89 if (xdg_data_home)
90 {
37fe180a 91 return g_build_filename(xdg_data_home, PACKAGE, NULL);
29f1598c
GB
92 }
93 else
94 {
95 return NULL;
96 }
97}
98
99/**
100 * a_get_viking_data_path:
101 *
102 * Retrieves the configuration path.
103 *
104 * Returns: list of directories to scan for data. Should be freed with g_strfreev.
105 */
106gchar **
107a_get_viking_data_path()
108{
ab2d70a6
RN
109#ifdef WINDOWS
110 // Try to use from the install directory - normally the working directory of Viking is where ever it's install location is
111 const gchar *xdg_data_dirs = "./data";
112 //const gchar *xdg_data_dirs = g_strdup ( "%s/%s/data", g_getenv("ProgramFiles"), PACKAGE );
113#else
29f1598c 114 const gchar *xdg_data_dirs = g_getenv("XDG_DATA_DIRS");
ab2d70a6 115#endif
29f1598c
GB
116 if (xdg_data_dirs == NULL)
117 {
118 /* Default value specified in
119 http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
120 */
121 xdg_data_dirs = "/usr/local/share/:/usr/share/";
122 }
9d5382ab
RN
123
124 gchar **data_path = g_strsplit(xdg_data_dirs, G_SEARCHPATH_SEPARATOR_S, 0);
125
ab2d70a6 126#ifndef WINDOWS
29f1598c
GB
127 /* Append the viking dir */
128 gchar **path;
129 for (path = data_path ; *path != NULL ; path++)
130 {
131 gchar *dir = *path;
132 *path = g_build_filename(dir, PACKAGE, NULL);
133 g_free(dir);
134 }
ab2d70a6 135#endif
29f1598c
GB
136 return data_path;
137}