2019-07-12 16:36:08 +02:00
|
|
|
/*
|
2020-11-07 13:06:23 +01:00
|
|
|
** Copyright (C) 2019-2020 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
2019-07-12 16:36:08 +02:00
|
|
|
**
|
|
|
|
** 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.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
2020-11-07 13:06:23 +01:00
|
|
|
#include "mu-runtime.hh"
|
2019-12-16 20:44:35 +01:00
|
|
|
#include "utils/mu-util.h"
|
2020-05-30 12:24:53 +02:00
|
|
|
#include "utils/mu-logger.hh"
|
2019-07-12 16:36:08 +02:00
|
|
|
|
|
|
|
#include <locale.h> /* for setlocale() */
|
2019-07-18 15:16:08 +02:00
|
|
|
|
|
|
|
#include <string>
|
2019-07-12 16:36:08 +02:00
|
|
|
#include <unordered_map>
|
|
|
|
static std::unordered_map<MuRuntimePath, std::string> RuntimePaths;
|
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
constexpr auto PartsDir = "parts";
|
|
|
|
constexpr auto LogDir = "log";
|
|
|
|
constexpr auto XapianDir = "xapian";
|
|
|
|
constexpr auto MuName = "mu";
|
|
|
|
constexpr auto Bookmarks = "bookmarks";
|
2019-07-12 16:36:08 +02:00
|
|
|
|
|
|
|
static const std::string Sepa{G_DIR_SEPARATOR_S};
|
|
|
|
|
|
|
|
static void
|
2021-10-20 11:18:15 +02:00
|
|
|
init_paths_xdg()
|
2019-07-12 16:36:08 +02:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
RuntimePaths.emplace(MU_RUNTIME_PATH_XAPIANDB,
|
|
|
|
g_get_user_cache_dir() + Sepa + MuName + Sepa + XapianDir);
|
|
|
|
RuntimePaths.emplace(MU_RUNTIME_PATH_CACHE, g_get_user_cache_dir() + Sepa + MuName);
|
|
|
|
RuntimePaths.emplace(MU_RUNTIME_PATH_MIMECACHE,
|
|
|
|
g_get_user_cache_dir() + Sepa + MuName + Sepa + PartsDir);
|
|
|
|
RuntimePaths.emplace(MU_RUNTIME_PATH_LOGDIR, g_get_user_cache_dir() + Sepa + MuName);
|
|
|
|
RuntimePaths.emplace(MU_RUNTIME_PATH_BOOKMARKS, g_get_user_config_dir() + Sepa + MuName);
|
2019-07-12 16:36:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-10-20 11:18:15 +02:00
|
|
|
init_paths_muhome(const char* muhome)
|
2019-07-12 16:36:08 +02:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
RuntimePaths.emplace(MU_RUNTIME_PATH_XAPIANDB, muhome + Sepa + XapianDir);
|
|
|
|
RuntimePaths.emplace(MU_RUNTIME_PATH_CACHE, muhome);
|
2019-07-12 16:36:08 +02:00
|
|
|
RuntimePaths.emplace(MU_RUNTIME_PATH_MIMECACHE, muhome + Sepa + PartsDir);
|
2021-10-20 11:18:15 +02:00
|
|
|
RuntimePaths.emplace(MU_RUNTIME_PATH_LOGDIR, muhome + Sepa + LogDir);
|
2019-07-12 16:36:08 +02:00
|
|
|
RuntimePaths.emplace(MU_RUNTIME_PATH_BOOKMARKS, muhome + Sepa + Bookmarks);
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
2021-10-20 11:18:15 +02:00
|
|
|
mu_runtime_init(const char* muhome, const char* name, gboolean debug)
|
2019-07-12 16:36:08 +02:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
g_return_val_if_fail(RuntimePaths.empty(), FALSE);
|
|
|
|
g_return_val_if_fail(name, FALSE);
|
|
|
|
|
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
|
|
|
|
if (muhome)
|
|
|
|
init_paths_muhome(muhome);
|
|
|
|
else
|
|
|
|
init_paths_xdg();
|
|
|
|
|
|
|
|
for (const auto& d : RuntimePaths) {
|
|
|
|
char* dir;
|
|
|
|
if (d.first == MU_RUNTIME_PATH_BOOKMARKS) // special case
|
|
|
|
dir = g_path_get_dirname(d.second.c_str());
|
|
|
|
else
|
|
|
|
dir = g_strdup(d.second.c_str());
|
|
|
|
|
|
|
|
auto ok = mu_util_create_dir_maybe(dir, 0700, TRUE);
|
|
|
|
if (!ok) {
|
|
|
|
g_critical("failed to create %s", dir);
|
|
|
|
g_free(dir);
|
|
|
|
mu_runtime_uninit();
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
g_free(dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto log_path = RuntimePaths[MU_RUNTIME_PATH_LOGDIR] + Sepa + name + ".log";
|
|
|
|
|
|
|
|
using namespace Mu;
|
|
|
|
LogOptions opts{LogOptions::None};
|
|
|
|
if (debug)
|
|
|
|
opts |= (LogOptions::Debug | LogOptions::None);
|
|
|
|
|
|
|
|
Mu::log_init(log_path, opts);
|
|
|
|
|
|
|
|
return TRUE;
|
2019-07-12 16:36:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-10-20 11:18:15 +02:00
|
|
|
mu_runtime_uninit(void)
|
2019-07-12 16:36:08 +02:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
RuntimePaths.clear();
|
|
|
|
Mu::log_uninit();
|
2019-07-12 16:36:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const char*
|
2021-10-20 11:18:15 +02:00
|
|
|
mu_runtime_path(MuRuntimePath path)
|
2019-07-12 16:36:08 +02:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
const auto it = RuntimePaths.find(path);
|
|
|
|
if (it == RuntimePaths.end())
|
|
|
|
return NULL;
|
|
|
|
else
|
|
|
|
return it->second.c_str();
|
2019-07-12 16:36:08 +02:00
|
|
|
}
|