Add support for setting configuration file path.

This commit is contained in:
Hǎiliàng Wáng 2013-01-21 01:10:08 +08:00
parent 8e4b53dd78
commit 567cb930fd
1 changed files with 135 additions and 117 deletions

View File

@ -71,6 +71,7 @@ struct config_info {
char *browser; char *browser;
gboolean dynamic_title, urgent_on_bell, clickable_url; gboolean dynamic_title, urgent_on_bell, clickable_url;
int tag; int tag;
char *config_file;
}; };
struct keybind_info { struct keybind_info {
@ -101,6 +102,8 @@ static void get_vte_padding(VteTerminal *vte, int *w, int *h);
static char *check_match(VteTerminal *vte, int event_x, int event_y); static char *check_match(VteTerminal *vte, int event_x, int event_y);
static void load_config(GtkWindow *window, VteTerminal *vte, config_info *info, static void load_config(GtkWindow *window, VteTerminal *vte, config_info *info,
char **geometry); char **geometry);
static void set_config(GtkWindow *window, VteTerminal *vte, config_info *info,
char **geometry, GKeyFile *config);
static long first_row(VteTerminal *vte); static long first_row(VteTerminal *vte);
void launch_browser(char *browser, char *url) { void launch_browser(char *browser, char *url) {
@ -1032,21 +1035,37 @@ static void load_theme(VteTerminal *vte, GKeyFile *config, hint_info &hints) {
static void load_config(GtkWindow *window, VteTerminal *vte, config_info *info, static void load_config(GtkWindow *window, VteTerminal *vte, config_info *info,
char **geometry) { char **geometry) {
const std::string path = "/termite/config"; const std::string default_path = "/termite/config";
GKeyFile *config = g_key_file_new(); GKeyFile *config = g_key_file_new();
gboolean loaded; gboolean loaded = FALSE;
if (info->config_file) {
loaded = g_key_file_load_from_file(config, loaded = g_key_file_load_from_file(config,
(g_get_user_config_dir() + path).c_str(), info->config_file,
G_KEY_FILE_NONE, nullptr); G_KEY_FILE_NONE, nullptr);
}
if (!loaded) {
loaded = g_key_file_load_from_file(config,
(g_get_user_config_dir() + default_path).c_str(),
G_KEY_FILE_NONE, nullptr);
}
for (const char *const *dir = g_get_system_config_dirs(); for (const char *const *dir = g_get_system_config_dirs();
!loaded && *dir; dir++) { !loaded && *dir; dir++) {
loaded = g_key_file_load_from_file(config, (*dir + path).c_str(), loaded = g_key_file_load_from_file(config, (*dir + default_path).c_str(),
G_KEY_FILE_NONE, nullptr); G_KEY_FILE_NONE, nullptr);
} }
if (loaded) { if (loaded) {
set_config(window, vte, info, geometry, config);
}
g_key_file_free(config);
}
static void set_config(GtkWindow *window, VteTerminal *vte, config_info *info,
char **geometry, GKeyFile *config) {
if (geometry) { if (geometry) {
if (auto s = get_config_string(config, "options", "geometry")) { if (auto s = get_config_string(config, "options", "geometry")) {
*geometry = *s; *geometry = *s;
@ -1156,8 +1175,6 @@ static void load_config(GtkWindow *window, VteTerminal *vte, config_info *info,
} }
load_theme(vte, config, info->hints); load_theme(vte, config, info->hints);
}
g_key_file_free(config);
}/*}}}*/ }/*}}}*/
static void exit_with_status(VteTerminal *vte) { static void exit_with_status(VteTerminal *vte) {
@ -1173,7 +1190,7 @@ int main(int argc, char **argv) {
gboolean version = FALSE, hold = FALSE; gboolean version = FALSE, hold = FALSE;
GOptionContext *context = g_option_context_new(NULL); GOptionContext *context = g_option_context_new(NULL);
char *role = NULL, *geometry = NULL, *execute = NULL; char *role = NULL, *geometry = NULL, *execute = NULL, *config_file = NULL;
const GOptionEntry entries[] = { const GOptionEntry entries[] = {
{"role", 'r', 0, G_OPTION_ARG_STRING, &role, "The role to use", "ROLE"}, {"role", 'r', 0, G_OPTION_ARG_STRING, &role, "The role to use", "ROLE"},
{"geometry", 0, 0, G_OPTION_ARG_STRING, &geometry, "Window geometry", "GEOMETRY"}, {"geometry", 0, 0, G_OPTION_ARG_STRING, &geometry, "Window geometry", "GEOMETRY"},
@ -1181,6 +1198,7 @@ int main(int argc, char **argv) {
{"exec", 'e', 0, G_OPTION_ARG_STRING, &execute, "Command to execute", "COMMAND"}, {"exec", 'e', 0, G_OPTION_ARG_STRING, &execute, "Command to execute", "COMMAND"},
{"version", 'v', 0, G_OPTION_ARG_NONE, &version, "Version info", NULL}, {"version", 'v', 0, G_OPTION_ARG_NONE, &version, "Version info", NULL},
{"hold", 0, 0, G_OPTION_ARG_NONE, &hold, "Remain open after child process exits", NULL}, {"hold", 0, 0, G_OPTION_ARG_NONE, &hold, "Remain open after child process exits", NULL},
{"config", 'c', 0, G_OPTION_ARG_STRING, &config_file, "Path of config file", "CONFIG"},
{} {}
}; };
g_option_context_add_main_entries(context, entries, NULL); g_option_context_add_main_entries(context, entries, NULL);
@ -1250,7 +1268,7 @@ int main(int argc, char **argv) {
std::vector<url_data>()}, std::vector<url_data>()},
{vi_mode::insert, 0, 0, 0, 0}, {vi_mode::insert, 0, 0, 0, 0},
{{nullptr, nullptr, nullptr, nullptr, 0, 0, 0}, {{nullptr, nullptr, nullptr, nullptr, 0, 0, 0},
nullptr, FALSE, FALSE, FALSE, -1} nullptr, FALSE, FALSE, FALSE, -1, config_file}
}; };
load_config(GTK_WINDOW(window), vte, &info.config, &geometry); load_config(GTK_WINDOW(window), vte, &info.config, &geometry);