summaryrefslogtreecommitdiff
path: root/lib/mu-runtime.c
blob: a8e78dd4c545fa3d781add1a9f18c5358407caa7 (plain)
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
/* -*- mode: c; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
**
** Copyright (C) 2010-2013 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** 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 3, 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**
*/

#include "mu-runtime.h"

#include <glib-object.h>
#include <locale.h> /* for setlocale() */
#include <stdio.h> /* for fileno() */
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

#include "mu-msg.h"
#include "mu-log.h"
#include "mu-util.h"

#define MU_XAPIAN_DIRNAME	"xapian"
#define MU_BOOKMARKS_FILENAME	"bookmarks"
#define MU_CACHE_DIRNAME        "cache"
#define MU_CONTACTS_FILENAME	"contacts"
#define MU_LOG_DIRNAME		"log"


struct _MuRuntimeData {
	gchar            *_str[MU_RUNTIME_PATH_NUM];
	gchar            *_name; /* e.g., 'mu', 'mug' */
};
typedef struct _MuRuntimeData	 MuRuntimeData;

/* static, global data for this singleton */
static gboolean		 _initialized = FALSE;
static MuRuntimeData	*_data	      = NULL;

static void        runtime_free (void);
static gboolean    init_paths (const char* muhome, MuRuntimeData *data);
static const char* runtime_path (MuRuntimePath path);


static gboolean
init_log (const char *muhome, const char *name, MuLogOptions opts)
{
	gboolean rv;
	char *logpath;

	logpath = g_strdup_printf ("%s%c%s%c%s.log",
				   muhome, G_DIR_SEPARATOR,
				   MU_LOG_DIRNAME, G_DIR_SEPARATOR,
				   name);

	rv = mu_log_init (logpath, opts);
	g_free (logpath);

	return rv;
}



gboolean
mu_runtime_init (const char* muhome_arg, const char *name)
{
	gchar *muhome;

	g_return_val_if_fail (!_initialized, FALSE);
	g_return_val_if_fail (name, FALSE);

	setlocale (LC_ALL, "");

#ifndef GLIB_VERSION_2_36
	g_type_init ();
#endif /*GLIB_VERSION_2_36*/

	if (muhome_arg)
		muhome = g_strdup (muhome_arg);
	else
		muhome = mu_util_guess_mu_homedir ();

	if (!mu_util_create_dir_maybe (muhome, 0700, TRUE)) {
		g_printerr ("mu: invalid mu homedir specified;"
			    " use --muhome=<dir>\n");
		runtime_free ();
		return FALSE;
	}

	_data = g_new0 (MuRuntimeData, 1);
	_data->_str[MU_RUNTIME_PATH_MUHOME] = muhome;

	init_paths (muhome, _data);
	_data->_name = g_strdup (name);

	if (!init_log (muhome, name, MU_LOG_OPTIONS_BACKUP)) {
		runtime_free ();
		g_free (muhome);
		return FALSE;
	}

	return _initialized = TRUE;
}

static void
runtime_free (void)
{
	int i;

	mu_log_uninit();

	if (!_data)
		return;

	for (i = 0; i != MU_RUNTIME_PATH_NUM; ++i)
		g_free (_data->_str[i]);

	g_free (_data->_name);
	g_free (_data);
}

void
mu_runtime_uninit (void)
{
	if (!_initialized)
		return;

	runtime_free ();

	_initialized = FALSE;
}


static gboolean
create_dirs_maybe (MuRuntimeData *data)
{
	if (!mu_util_create_dir_maybe
	    (data->_str[MU_RUNTIME_PATH_CACHE], 0700, TRUE)) {
		g_warning ("failed to create cache dir");
		return FALSE;
	}

	if (!mu_util_create_dir_maybe
	    (data->_str[MU_RUNTIME_PATH_LOG], 0700, TRUE)) {
		g_warning ("failed to create log dir");
		return FALSE;
	}

	return TRUE;
}



static gboolean
init_paths (const char* muhome, MuRuntimeData *data)
{
	data->_str [MU_RUNTIME_PATH_XAPIANDB] =
		g_strdup_printf ("%s%c%s", muhome, G_DIR_SEPARATOR,
				 MU_XAPIAN_DIRNAME);

	data->_str [MU_RUNTIME_PATH_BOOKMARKS] =
		g_strdup_printf ("%s%c%s", muhome, G_DIR_SEPARATOR,
				 MU_BOOKMARKS_FILENAME);

	data->_str [MU_RUNTIME_PATH_CACHE] =
		g_strdup_printf ("%s%c%s", muhome, G_DIR_SEPARATOR,
				 MU_CACHE_DIRNAME);

	data->_str [MU_RUNTIME_PATH_CONTACTS] =
		g_strdup_printf ("%s%c%s", data->_str[MU_RUNTIME_PATH_CACHE],
				 G_DIR_SEPARATOR, MU_CONTACTS_FILENAME);

	data->_str [MU_RUNTIME_PATH_LOG] =
		g_strdup_printf ("%s%c%s", muhome,
				 G_DIR_SEPARATOR, MU_LOG_DIRNAME);

	if (!create_dirs_maybe (data))
		return FALSE;

	return TRUE;
}

/* so we can called when _initialized is FALSE still */
static const char*
runtime_path (MuRuntimePath path)
{
	return _data->_str[path];
}



const char*
mu_runtime_path (MuRuntimePath path)
{
	g_return_val_if_fail (_initialized, NULL);
	g_return_val_if_fail (path < MU_RUNTIME_PATH_NUM, NULL);

	return runtime_path (path);
}