2011-01-02 17:10:11 +01:00
|
|
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
|
|
|
|
**
|
2010-11-14 12:55:04 +01:00
|
|
|
** Copyright (C) 2010 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() */
|
2010-11-27 15:06:55 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <mu-msg.h>
|
|
|
|
|
2010-11-14 12:55:04 +01:00
|
|
|
#include "mu-config.h"
|
|
|
|
#include "mu-log.h"
|
|
|
|
#include "mu-util.h"
|
2011-03-01 23:13:24 +01:00
|
|
|
|
|
|
|
#define MU_XAPIAN_DIRNAME "xapian"
|
|
|
|
#define MU_BOOKMARKS_FILENAME "bookmarks"
|
|
|
|
#define MU_CACHE_DIRNAME "cache"
|
|
|
|
#define MU_CONTACTS_FILENAME "contacts"
|
2011-03-19 09:07:32 +01:00
|
|
|
#define MU_LOG_DIRNAME "log"
|
|
|
|
|
2010-11-14 12:55:04 +01:00
|
|
|
|
|
|
|
struct _MuRuntimeData {
|
2011-03-19 09:07:32 +01:00
|
|
|
gchar *_str[MU_RUNTIME_PATH_NUM];
|
2011-03-01 23:13:24 +01:00
|
|
|
MuConfig *_config;
|
2010-11-14 12:55:04 +01:00
|
|
|
};
|
2010-12-11 12:53:20 +01:00
|
|
|
typedef struct _MuRuntimeData MuRuntimeData;
|
2010-11-14 12:55:04 +01:00
|
|
|
|
|
|
|
/* static, global data for this singleton */
|
2010-12-11 12:53:20 +01:00
|
|
|
static gboolean _initialized = FALSE;
|
|
|
|
static MuRuntimeData *_data = NULL;
|
2010-11-14 12:55:04 +01:00
|
|
|
|
2011-03-19 09:07:32 +01:00
|
|
|
static void runtime_free (void);
|
|
|
|
static gboolean init_paths (const char* muhome, MuRuntimeData *data);
|
2010-11-14 12:55:04 +01:00
|
|
|
|
2011-01-02 17:10:11 +01:00
|
|
|
static gboolean
|
2011-03-01 23:13:24 +01:00
|
|
|
mu_dir_is_readable_and_writable (const char *muhome)
|
2011-01-02 17:10:11 +01:00
|
|
|
{
|
2011-01-10 23:44:03 +01:00
|
|
|
if (mu_util_create_dir_maybe (muhome, 0700))
|
2011-01-02 17:10:11 +01:00
|
|
|
return TRUE;
|
2011-01-02 17:45:16 +01:00
|
|
|
|
|
|
|
g_warning ("cannot use '%s' as a mu homedir", muhome);
|
2011-01-02 17:10:11 +01:00
|
|
|
g_warning ("use --muhome= to set a different one");
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2011-03-01 23:13:24 +01:00
|
|
|
|
2010-11-14 12:55:04 +01:00
|
|
|
gboolean
|
|
|
|
mu_runtime_init (const char* muhome_arg)
|
|
|
|
{
|
|
|
|
gchar *muhome;
|
|
|
|
|
|
|
|
g_return_val_if_fail (!_initialized, FALSE);
|
|
|
|
|
2010-12-11 12:53:20 +01:00
|
|
|
if (!mu_util_init_system())
|
2010-11-14 12:55:04 +01:00
|
|
|
return FALSE;
|
2010-11-27 13:56:25 +01:00
|
|
|
|
2010-11-14 12:55:04 +01:00
|
|
|
if (muhome_arg)
|
|
|
|
muhome = g_strdup (muhome_arg);
|
|
|
|
else
|
|
|
|
muhome = mu_util_guess_mu_homedir ();
|
|
|
|
|
2011-01-02 17:10:11 +01:00
|
|
|
if (!mu_dir_is_readable_and_writable (muhome)) {
|
|
|
|
runtime_free ();
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2010-11-14 12:55:04 +01:00
|
|
|
if (!mu_log_init (muhome, TRUE, FALSE, FALSE)) {
|
|
|
|
g_free (muhome);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
_data = g_new0 (MuRuntimeData, 1);
|
2011-03-19 09:07:32 +01:00
|
|
|
_data->_str[MU_RUNTIME_PATH_MUHOME] = muhome;
|
2011-03-01 23:13:24 +01:00
|
|
|
init_paths (muhome, _data);
|
2011-05-09 01:58:33 +02:00
|
|
|
|
2010-11-14 12:55:04 +01:00
|
|
|
return _initialized = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gboolean
|
2011-01-04 22:19:03 +01:00
|
|
|
init_log (MuConfig *opts)
|
2010-11-14 12:55:04 +01:00
|
|
|
{
|
|
|
|
if (opts->log_stderr)
|
|
|
|
return mu_log_init_with_fd (fileno(stderr), FALSE,
|
|
|
|
opts->quiet, opts->debug);
|
|
|
|
else
|
|
|
|
return mu_log_init (opts->muhome, TRUE, opts->quiet,
|
|
|
|
opts->debug);
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
mu_runtime_init_from_cmdline (int *pargc, char ***pargv)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (!_initialized, FALSE);
|
|
|
|
|
2010-12-11 12:53:20 +01:00
|
|
|
if (!mu_util_init_system())
|
2010-11-14 12:55:04 +01:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
_data = g_new0 (MuRuntimeData, 1);
|
2011-01-04 22:19:03 +01:00
|
|
|
_data->_config = mu_config_new (pargc, pargv);
|
|
|
|
if (!_data->_config) {
|
2010-11-14 12:55:04 +01:00
|
|
|
runtime_free ();
|
|
|
|
return FALSE;
|
2011-01-02 17:10:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!mu_dir_is_readable_and_writable (_data->_config->muhome)) {
|
|
|
|
runtime_free ();
|
|
|
|
return FALSE;
|
2010-11-14 12:55:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!init_log (_data->_config)) {
|
|
|
|
runtime_free ();
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2011-03-19 09:07:32 +01:00
|
|
|
_data->_str[MU_RUNTIME_PATH_MUHOME] =
|
2011-03-01 23:13:24 +01:00
|
|
|
g_strdup (_data->_config->muhome);
|
2011-03-19 09:07:32 +01:00
|
|
|
init_paths (_data->_str[MU_RUNTIME_PATH_MUHOME], _data);
|
2011-03-01 23:13:24 +01:00
|
|
|
|
2010-11-14 12:55:04 +01:00
|
|
|
return _initialized = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
runtime_free (void)
|
|
|
|
{
|
2011-03-01 23:13:24 +01:00
|
|
|
int i;
|
|
|
|
|
2011-03-19 09:07:32 +01:00
|
|
|
for (i = 0; i != MU_RUNTIME_PATH_NUM; ++i)
|
2011-03-01 23:13:24 +01:00
|
|
|
g_free (_data->_str[i]);
|
2011-02-06 09:50:39 +01:00
|
|
|
|
2011-01-04 22:19:03 +01:00
|
|
|
mu_config_destroy (_data->_config);
|
2010-11-14 12:55:04 +01:00
|
|
|
|
|
|
|
mu_log_uninit();
|
|
|
|
|
|
|
|
g_free (_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
mu_runtime_uninit (void)
|
|
|
|
{
|
|
|
|
g_return_if_fail (_initialized);
|
|
|
|
|
2010-11-27 15:06:55 +01:00
|
|
|
runtime_free ();
|
|
|
|
|
2010-11-14 12:55:04 +01:00
|
|
|
_initialized = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-19 09:07:32 +01:00
|
|
|
static gboolean
|
|
|
|
create_dirs_maybe (MuRuntimeData *data)
|
2010-11-14 12:55:04 +01:00
|
|
|
{
|
2011-03-19 09:07:32 +01:00
|
|
|
if (!mu_util_create_dir_maybe
|
|
|
|
(data->_str[MU_RUNTIME_PATH_CACHE], 0700)) {
|
|
|
|
g_warning ("failed to create cache dir");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mu_util_create_dir_maybe
|
|
|
|
(data->_str[MU_RUNTIME_PATH_LOG], 0700)) {
|
|
|
|
g_warning ("failed to create log dir");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
2010-11-14 12:55:04 +01:00
|
|
|
}
|
|
|
|
|
2011-03-01 23:13:24 +01:00
|
|
|
|
2010-11-14 12:55:04 +01:00
|
|
|
|
2011-03-19 09:07:32 +01:00
|
|
|
static gboolean
|
|
|
|
init_paths (const char* muhome, MuRuntimeData *data)
|
2010-11-14 12:55:04 +01:00
|
|
|
{
|
2011-03-19 09:07:32 +01:00
|
|
|
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] =
|
2011-04-24 15:03:40 +02:00
|
|
|
g_strdup_printf ("%s%c%s", muhome, G_DIR_SEPARATOR,
|
2011-03-19 09:07:32 +01:00
|
|
|
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;
|
2011-03-01 23:13:24 +01:00
|
|
|
}
|
2010-11-14 12:55:04 +01:00
|
|
|
|
2011-03-01 23:13:24 +01:00
|
|
|
|
|
|
|
const char*
|
2011-03-19 09:07:32 +01:00
|
|
|
mu_runtime_path (MuRuntimePath path)
|
2011-03-01 23:13:24 +01:00
|
|
|
{
|
|
|
|
g_return_val_if_fail (_initialized, NULL);
|
2011-03-19 09:07:32 +01:00
|
|
|
g_return_val_if_fail (path < MU_RUNTIME_PATH_NUM, NULL);
|
|
|
|
|
|
|
|
return _data->_str[path];
|
2010-11-14 12:55:04 +01:00
|
|
|
}
|
|
|
|
|
2011-01-04 22:19:03 +01:00
|
|
|
MuConfig*
|
|
|
|
mu_runtime_config (void)
|
2010-11-14 12:55:04 +01:00
|
|
|
{
|
2011-03-01 23:13:24 +01:00
|
|
|
g_return_val_if_fail (_initialized, NULL);
|
2010-11-14 12:55:04 +01:00
|
|
|
return _data->_config;
|
|
|
|
}
|