mdiary/src/settings.c
2017-02-15 19:25:42 +01:00

103 lines
2.8 KiB
C

#include "settings.h"
static void mdiary_settings_parse_line(gchar *line, struct mdiary_settings *settings)
{
GRegex *regex;
GMatchInfo *match_info;
regex = g_regex_new("workspace = (.*)", G_REGEX_RAW, 0, NULL);
if (g_regex_match(regex, line, 0, &match_info) &&
g_regex_get_capture_count(regex) > 0 &&
g_match_info_matches(match_info)) {
mainWindow_add_recent_workspace(settings->mainWindow, g_match_info_fetch(match_info, 1), TRUE);
}
g_regex_unref(regex);
}
gboolean mdiary_load_settings(gchar *filename, struct mdiary_settings *settings)
{
GFile *file;
GError *err = NULL;
GFileInputStream *stream;
GDataInputStream *dstream;
gchar *line;
GtkTreeIter iter;
gchar *temp;
file = g_file_new_for_path(filename);
stream = g_file_read(file, NULL, &err);
if (err != NULL) {
g_print("SETTINGS: %s\n", err->message);
g_error_free(err);
return FALSE;
}
dstream = g_data_input_stream_new(G_INPUT_STREAM(stream));
while (line = g_data_input_stream_read_line(G_DATA_INPUT_STREAM(dstream), NULL, NULL, NULL))
mdiary_settings_parse_line(line, settings);
g_input_stream_close(G_INPUT_STREAM(dstream), NULL, NULL);
g_input_stream_close(G_INPUT_STREAM(stream), NULL, NULL);
g_object_unref(file);
if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(settings->mainWindow->workspaceListStore), &iter)) {
gtk_tree_model_get(GTK_TREE_MODEL(settings->mainWindow->workspaceListStore), &iter,
WSCOL_PATH, &temp,
-1);
mainWindow_switch_workspace(settings->mainWindow, temp, FALSE);
g_free(temp);
}
}
gboolean mdiary_save_settings(gchar *filename, struct mdiary_settings *settings)
{
GtkTreeIter iter;
gchar *temp;
gchar *output_buffer;
GFileOutputStream *stream;
GFile *file;
file = g_file_new_for_path(filename);
stream = g_file_replace(
file, NULL, FALSE,
G_FILE_CREATE_NONE,
NULL, NULL);
if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(settings->mainWindow->workspaceListStore), &iter)) {
do {
gtk_tree_model_get(GTK_TREE_MODEL(settings->mainWindow->workspaceListStore), &iter,
WSCOL_PATH, &temp,
-1);
output_buffer = g_strdup_printf("workspace = %s\n", temp);
g_output_stream_write(G_OUTPUT_STREAM(stream),
output_buffer, strlen(output_buffer), NULL, NULL);
g_free(output_buffer);
g_free(temp);
} while (gtk_tree_model_iter_next(GTK_TREE_MODEL(settings->mainWindow->workspaceListStore), &iter));
}
g_output_stream_close(G_OUTPUT_STREAM(stream), NULL, NULL);
g_object_unref(stream);
g_object_unref(file);
}
struct mdiary_settings *mdiary_settings_new(struct mainWindow *mainWindow)
{
struct mdiary_settings *settings;
settings = malloc(sizeof(struct mdiary_settings));
settings->mainWindow = mainWindow;
return settings;
}
void mdiary_settings_free(struct mdiary_settings *settings)
{
free(settings);
}