* mu_runtime: update getting standard dirs / paths, use them
This commit is contained in:
@ -167,7 +167,7 @@ run_cmd_cfind (const char* pattern, OutputFormat format)
|
||||
MuContacts *contacts;
|
||||
size_t num;
|
||||
|
||||
contacts = mu_contacts_new (mu_runtime_contacts_cache_file());
|
||||
contacts = mu_contacts_new (mu_runtime_path(MU_RUNTIME_PATH_CONTACTS));
|
||||
if (!contacts) {
|
||||
g_warning ("could not retrieve contacts");
|
||||
return MU_EXITCODE_ERROR;
|
||||
|
||||
@ -229,7 +229,7 @@ query_params_valid (MuConfig *opts)
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
xpath = mu_runtime_xapian_dir();
|
||||
xpath = mu_runtime_path (MU_RUNTIME_PATH_XAPIANDB);
|
||||
|
||||
if (mu_util_check_dir (xpath, TRUE, FALSE))
|
||||
return TRUE;
|
||||
@ -247,7 +247,7 @@ resolve_bookmark (MuConfig *opts)
|
||||
char* val;
|
||||
const gchar *bmfile;
|
||||
|
||||
bmfile = mu_runtime_bookmarks_file();
|
||||
bmfile = mu_runtime_path (MU_RUNTIME_PATH_BOOKMARKS);
|
||||
bm = mu_bookmarks_new (bmfile);
|
||||
if (!bm) {
|
||||
g_warning ("failed to open bookmarks file '%s'", bmfile);
|
||||
@ -321,7 +321,7 @@ get_query_obj (void)
|
||||
const char* xpath;
|
||||
MuQuery *mquery;
|
||||
|
||||
xpath = mu_runtime_xapian_dir ();
|
||||
xpath = mu_runtime_path (MU_RUNTIME_PATH_XAPIANDB);
|
||||
if (!db_is_ready(xpath)) {
|
||||
g_warning ("database '%s' is not ready", xpath);
|
||||
return NULL;
|
||||
|
||||
@ -173,7 +173,7 @@ database_version_check_and_update (MuConfig *opts)
|
||||
{
|
||||
const gchar *xpath;
|
||||
|
||||
xpath = mu_runtime_xapian_dir ();
|
||||
xpath = mu_runtime_path (MU_RUNTIME_PATH_XAPIANDB);
|
||||
|
||||
if (mu_util_xapian_is_empty (xpath))
|
||||
return TRUE;
|
||||
@ -249,7 +249,8 @@ cmd_cleanup (MuIndex *midx, MuConfig *opts, MuIndexStats *stats,
|
||||
MuResult rv;
|
||||
time_t t;
|
||||
|
||||
g_message ("cleaning up messages [%s]", mu_runtime_xapian_dir());
|
||||
g_message ("cleaning up messages [%s]",
|
||||
mu_runtime_path (MU_RUNTIME_PATH_XAPIANDB));
|
||||
|
||||
t = time (NULL);
|
||||
rv = mu_index_cleanup (midx, stats,
|
||||
@ -276,7 +277,7 @@ cmd_index (MuIndex *midx, MuConfig *opts, MuIndexStats *stats,
|
||||
time_t t;
|
||||
|
||||
g_message ("indexing messages under %s [%s]", opts->maildir,
|
||||
mu_runtime_xapian_dir());
|
||||
mu_runtime_path (MU_RUNTIME_PATH_XAPIANDB));
|
||||
|
||||
t = time (NULL);
|
||||
rv = mu_index_run (midx, opts->maildir, opts->reindex, stats,
|
||||
@ -349,8 +350,8 @@ init_mu_index (MuConfig *opts, MuExitCode *code)
|
||||
}
|
||||
|
||||
err = NULL;
|
||||
midx = mu_index_new (mu_runtime_xapian_dir(),
|
||||
mu_runtime_contacts_cache_file(),
|
||||
midx = mu_index_new (mu_runtime_path (MU_RUNTIME_PATH_XAPIANDB),
|
||||
mu_runtime_path (MU_RUNTIME_PATH_CONTACTS),
|
||||
&err);
|
||||
if (!midx) {
|
||||
*code = handle_index_error_and_free (err);
|
||||
|
||||
127
src/mu-runtime.c
127
src/mu-runtime.c
@ -32,22 +32,15 @@
|
||||
#include "mu-log.h"
|
||||
#include "mu-util.h"
|
||||
|
||||
enum {
|
||||
MU_RUNTIME_STR_MU_HOMEPATH,
|
||||
MU_RUNTIME_STR_XAPIAN_PATH,
|
||||
MU_RUNTIME_STR_BOOKMARKS_PATH,
|
||||
MU_RUNTIME_STR_CACHE_PATH,
|
||||
MU_RUNTIME_STR_CONTACTS_PATH,
|
||||
MU_RUNTIME_STR_NUM
|
||||
};
|
||||
|
||||
#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_STR_NUM];
|
||||
gchar *_str[MU_RUNTIME_PATH_NUM];
|
||||
MuConfig *_config;
|
||||
};
|
||||
typedef struct _MuRuntimeData MuRuntimeData;
|
||||
@ -57,6 +50,7 @@ static gboolean _initialized = FALSE;
|
||||
static MuRuntimeData *_data = NULL;
|
||||
|
||||
static void runtime_free (void);
|
||||
static gboolean init_paths (const char* muhome, MuRuntimeData *data);
|
||||
|
||||
static gboolean
|
||||
mu_dir_is_readable_and_writable (const char *muhome)
|
||||
@ -70,38 +64,6 @@ mu_dir_is_readable_and_writable (const char *muhome)
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
init_paths (const char* muhome, MuRuntimeData *data)
|
||||
{
|
||||
data->_str [MU_RUNTIME_STR_XAPIAN_PATH] =
|
||||
g_strdup_printf ("%s%c%s", muhome,
|
||||
G_DIR_SEPARATOR,
|
||||
MU_XAPIAN_DIRNAME);
|
||||
|
||||
data->_str [MU_RUNTIME_STR_BOOKMARKS_PATH] =
|
||||
g_strdup_printf ("%s%c%s", muhome,
|
||||
G_DIR_SEPARATOR,
|
||||
MU_BOOKMARKS_FILENAME);
|
||||
|
||||
data->_str [MU_RUNTIME_STR_CACHE_PATH] =
|
||||
g_strdup_printf ("%s%c%s", muhome,
|
||||
G_DIR_SEPARATOR,
|
||||
MU_CACHE_DIRNAME);
|
||||
|
||||
if (!mu_util_create_dir_maybe
|
||||
(_data->_str[MU_RUNTIME_STR_CACHE_PATH], 0700)) {
|
||||
g_warning ("failed to create cache dir");
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
data->_str [MU_RUNTIME_STR_CONTACTS_PATH] =
|
||||
g_strdup_printf ("%s%c%s",
|
||||
data->_str[MU_RUNTIME_STR_CACHE_PATH],
|
||||
G_DIR_SEPARATOR,
|
||||
MU_CONTACTS_FILENAME);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
gboolean
|
||||
mu_runtime_init (const char* muhome_arg)
|
||||
@ -129,7 +91,7 @@ mu_runtime_init (const char* muhome_arg)
|
||||
}
|
||||
|
||||
_data = g_new0 (MuRuntimeData, 1);
|
||||
_data->_str[MU_RUNTIME_STR_MU_HOMEPATH] = muhome;
|
||||
_data->_str[MU_RUNTIME_PATH_MUHOME] = muhome;
|
||||
init_paths (muhome, _data);
|
||||
|
||||
mu_msg_gmime_init ();
|
||||
@ -174,9 +136,9 @@ mu_runtime_init_from_cmdline (int *pargc, char ***pargv)
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
_data->_str[MU_RUNTIME_STR_MU_HOMEPATH] =
|
||||
_data->_str[MU_RUNTIME_PATH_MUHOME] =
|
||||
g_strdup (_data->_config->muhome);
|
||||
init_paths (_data->_str[MU_RUNTIME_STR_MU_HOMEPATH], _data);
|
||||
init_paths (_data->_str[MU_RUNTIME_PATH_MUHOME], _data);
|
||||
|
||||
mu_msg_gmime_init ();
|
||||
|
||||
@ -189,7 +151,7 @@ runtime_free (void)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i != MU_RUNTIME_STR_NUM; ++i)
|
||||
for (i = 0; i != MU_RUNTIME_PATH_NUM; ++i)
|
||||
g_free (_data->_str[i]);
|
||||
|
||||
mu_config_destroy (_data->_config);
|
||||
@ -211,38 +173,65 @@ mu_runtime_uninit (void)
|
||||
}
|
||||
|
||||
|
||||
const char*
|
||||
mu_runtime_mu_home_dir (void)
|
||||
static gboolean
|
||||
create_dirs_maybe (MuRuntimeData *data)
|
||||
{
|
||||
g_return_val_if_fail (_initialized, NULL);
|
||||
return _data->_str[MU_RUNTIME_STR_MU_HOMEPATH];
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
const char*
|
||||
mu_runtime_xapian_dir (void)
|
||||
mu_runtime_path (MuRuntimePath path)
|
||||
{
|
||||
g_return_val_if_fail (_initialized, NULL);
|
||||
return _data->_str[MU_RUNTIME_STR_XAPIAN_PATH];
|
||||
g_return_val_if_fail (path < MU_RUNTIME_PATH_NUM, NULL);
|
||||
|
||||
return _data->_str[path];
|
||||
}
|
||||
|
||||
const char*
|
||||
mu_runtime_bookmarks_file (void)
|
||||
{
|
||||
g_return_val_if_fail (_initialized, NULL);
|
||||
return _data->_str[MU_RUNTIME_STR_BOOKMARKS_PATH];
|
||||
}
|
||||
|
||||
|
||||
const char*
|
||||
mu_runtime_contacts_cache_file (void)
|
||||
{
|
||||
g_return_val_if_fail (_initialized, NULL);
|
||||
return _data->_str[MU_RUNTIME_STR_CONTACTS_PATH];
|
||||
}
|
||||
|
||||
|
||||
|
||||
MuConfig*
|
||||
mu_runtime_config (void)
|
||||
{
|
||||
|
||||
@ -55,6 +55,28 @@ gboolean mu_runtime_init_from_cmdline (int *pargc, char ***pargv);
|
||||
*/
|
||||
void mu_runtime_uninit (void);
|
||||
|
||||
|
||||
enum _MuRuntimePath {
|
||||
MU_RUNTIME_PATH_MUHOME, /* mu home path */
|
||||
MU_RUNTIME_PATH_XAPIANDB, /* mu xapian db path */
|
||||
MU_RUNTIME_PATH_BOOKMARKS, /* mu bookmarks file path */
|
||||
MU_RUNTIME_PATH_CACHE, /* mu cache path */
|
||||
MU_RUNTIME_PATH_LOG, /* mu path for log files */
|
||||
MU_RUNTIME_PATH_CONTACTS, /* mu path to the contacts cache */
|
||||
|
||||
MU_RUNTIME_PATH_NUM
|
||||
};
|
||||
typedef enum _MuRuntimePath MuRuntimePath;
|
||||
|
||||
/**
|
||||
* get a file system path to some 'special' file or directory
|
||||
*
|
||||
* @return ma string which should be not be modified/freed, or NULL in
|
||||
* case of error.
|
||||
*/
|
||||
const char* mu_runtime_path (MuRuntimePath path);
|
||||
|
||||
|
||||
/**
|
||||
* get the mu home directory (typically, ~/.mu); this can only be
|
||||
* called after mu_runtime_init and before mu_runtime_uninit
|
||||
|
||||
@ -64,9 +64,9 @@ test_mu_runtime_data (void)
|
||||
|
||||
g_assert (mu_runtime_init (homedir) == TRUE);
|
||||
|
||||
g_assert_cmpstr (homedir, ==, mu_runtime_mu_home_dir ());
|
||||
g_assert_cmpstr (xdir, ==, mu_runtime_xapian_dir ());
|
||||
g_assert_cmpstr (bmfile, ==, mu_runtime_bookmarks_file ());
|
||||
g_assert_cmpstr (homedir, ==, mu_runtime_path (MU_RUNTIME_PATH_MUHOME));
|
||||
g_assert_cmpstr (xdir, ==, mu_runtime_path (MU_RUNTIME_PATH_XAPIANDB));
|
||||
g_assert_cmpstr (bmfile, ==, mu_runtime_path (MU_RUNTIME_PATH_BOOKMARKS));
|
||||
|
||||
mu_runtime_uninit ();
|
||||
|
||||
@ -94,3 +94,5 @@ main (int argc, char *argv[])
|
||||
|
||||
return g_test_run ();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -152,7 +152,8 @@ on_shortcut_clicked (GtkWidget * w, const gchar * query, MugData * mdata)
|
||||
static GtkWidget *
|
||||
mug_shortcuts_bar (MugData * data)
|
||||
{
|
||||
data->shortcuts = mug_shortcuts_new (mu_runtime_bookmarks_file ());
|
||||
data->shortcuts = mug_shortcuts_new
|
||||
(mu_runtime_path(MU_RUNTIME_PATH_BOOKMARKS));
|
||||
|
||||
g_signal_connect (G_OBJECT (data->shortcuts), "clicked",
|
||||
G_CALLBACK (on_shortcut_clicked), data);
|
||||
@ -263,7 +264,8 @@ mug_query_area (MugData * mugdata)
|
||||
queryarea = gtk_vbox_new (FALSE, 2);
|
||||
paned = gtk_vpaned_new ();
|
||||
|
||||
mugdata->mlist = mug_msg_list_view_new (mu_runtime_xapian_dir ());
|
||||
mugdata->mlist = mug_msg_list_view_new
|
||||
(mu_runtime_path(MU_RUNTIME_PATH_XAPIANDB));
|
||||
scrolled = gtk_scrolled_window_new (NULL, NULL);
|
||||
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled),
|
||||
GTK_POLICY_AUTOMATIC,
|
||||
|
||||
@ -62,12 +62,13 @@ reindex (MugData *mugdata)
|
||||
MuIndex *midx;
|
||||
GError *err;
|
||||
|
||||
if (mu_util_xapian_is_locked (mu_runtime_xapian_dir()))
|
||||
if (mu_util_xapian_is_locked
|
||||
(mu_runtime_path(MU_RUNTIME_PATH_XAPIANDB)))
|
||||
return;
|
||||
|
||||
err = NULL;
|
||||
midx = mu_index_new (mu_runtime_xapian_dir(),
|
||||
mu_runtime_contacts_cache_file(),
|
||||
midx = mu_index_new (mu_runtime_path(MU_RUNTIME_PATH_XAPIANDB),
|
||||
mu_runtime_path(MU_RUNTIME_PATH_CONTACTS),
|
||||
&err);
|
||||
if (!midx) {
|
||||
if (err && err->code == MU_ERROR_XAPIAN_CANNOT_GET_WRITELOCK) {
|
||||
@ -201,7 +202,8 @@ on_shortcut_clicked (GtkWidget * w, const gchar * query, MugData * mdata)
|
||||
static GtkWidget *
|
||||
mug_shortcuts_bar (MugData * data)
|
||||
{
|
||||
data->shortcuts = mug_shortcuts_new (mu_runtime_bookmarks_file ());
|
||||
data->shortcuts = mug_shortcuts_new
|
||||
(mu_runtime_path(MU_RUNTIME_PATH_BOOKMARKS));
|
||||
|
||||
g_signal_connect (G_OBJECT (data->shortcuts), "clicked",
|
||||
G_CALLBACK (on_shortcut_clicked), data);
|
||||
@ -308,7 +310,8 @@ mug_query_area (MugData * mugdata)
|
||||
queryarea = gtk_vbox_new (FALSE, 2);
|
||||
paned = gtk_vpaned_new ();
|
||||
|
||||
mugdata->mlist = mug_msg_list_view_new (mu_runtime_xapian_dir ());
|
||||
mugdata->mlist = mug_msg_list_view_new
|
||||
(mu_runtime_path(MU_RUNTIME_PATH_XAPIANDB));
|
||||
scrolled = gtk_scrolled_window_new (NULL, NULL);
|
||||
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled),
|
||||
GTK_POLICY_AUTOMATIC,
|
||||
|
||||
Reference in New Issue
Block a user