2017-02-09 16:14:38 +01:00
|
|
|
#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)) {
|
2017-02-11 18:33:09 +01:00
|
|
|
mainWindow_add_recent_workspace(settings->mainWindow, g_match_info_fetch(match_info, 1), TRUE);
|
2017-02-09 16:14:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2017-02-11 18:41:11 +01:00
|
|
|
GtkTreeIter iter;
|
|
|
|
gchar *temp;
|
|
|
|
|
2017-02-09 16:14:38 +01:00
|
|
|
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));
|
|
|
|
|
2017-02-15 19:25:42 +01:00
|
|
|
while (line = g_data_input_stream_read_line(G_DATA_INPUT_STREAM(dstream), NULL, NULL, NULL))
|
2017-02-09 16:14:38 +01:00
|
|
|
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);
|
2017-02-11 18:41:11 +01:00
|
|
|
|
|
|
|
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);
|
2017-02-15 19:16:55 +01:00
|
|
|
mainWindow_switch_workspace(settings->mainWindow, temp, FALSE);
|
2017-02-11 18:41:11 +01:00
|
|
|
g_free(temp);
|
|
|
|
}
|
2017-02-09 16:14:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
gboolean mdiary_save_settings(gchar *filename, struct mdiary_settings *settings)
|
|
|
|
{
|
2017-02-11 18:33:09 +01:00
|
|
|
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));
|
|
|
|
}
|
2017-02-15 19:25:42 +01:00
|
|
|
g_output_stream_close(G_OUTPUT_STREAM(stream), NULL, NULL);
|
2017-02-11 18:33:09 +01:00
|
|
|
g_object_unref(stream);
|
|
|
|
g_object_unref(file);
|
2017-02-09 16:14:38 +01:00
|
|
|
}
|
|
|
|
|
2017-02-11 18:09:07 +01:00
|
|
|
struct mdiary_settings *mdiary_settings_new(struct mainWindow *mainWindow)
|
2017-02-09 16:14:38 +01:00
|
|
|
{
|
|
|
|
struct mdiary_settings *settings;
|
|
|
|
|
|
|
|
settings = malloc(sizeof(struct mdiary_settings));
|
2017-02-11 18:09:07 +01:00
|
|
|
settings->mainWindow = mainWindow;
|
2017-02-09 16:14:38 +01:00
|
|
|
|
|
|
|
return settings;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mdiary_settings_free(struct mdiary_settings *settings)
|
|
|
|
{
|
2017-02-11 18:09:07 +01:00
|
|
|
free(settings);
|
2017-02-09 16:14:38 +01:00
|
|
|
}
|