Improved last workspaces list + added saving of settings

This commit is contained in:
Markus Koch 2017-02-11 18:33:09 +01:00
parent 0cb3999495
commit 8d16aca056
3 changed files with 59 additions and 28 deletions

View File

@ -1,12 +1,6 @@
#include "mainwindow.h" #include "mainwindow.h"
#include "mdiary.h" #include "mdiary.h"
enum {
WSCOL_LABEL = 0,
WSCOL_PATH,
WSCOL_COUNT
};
static gboolean mainWindow_workspace_entry_visible(GtkTreeModel *model, static gboolean mainWindow_workspace_entry_visible(GtkTreeModel *model,
GtkTreeIter *iter, GtkTreeIter *iter,
struct mainWindow *mainWindow) struct mainWindow *mainWindow)
@ -147,7 +141,7 @@ static gint mainWindow_sort_date_compare_func(GtkTreeModel *model,
return -1; return -1;
} }
void mainWindow_add_recent_workspace(struct mainWindow *mainWindow, gchar *path) void mainWindow_add_recent_workspace(struct mainWindow *mainWindow, gchar *path, gboolean append)
{ {
GtkTreeIter iter, iter_delete; GtkTreeIter iter, iter_delete;
GRegex *regex; GRegex *regex;
@ -161,23 +155,27 @@ void mainWindow_add_recent_workspace(struct mainWindow *mainWindow, gchar *path)
g_match_info_matches(match_info)) { g_match_info_matches(match_info)) {
g_match_info_fetch(match_info, 1); g_match_info_fetch(match_info, 1);
gtk_tree_model_get_iter_first(GTK_TREE_MODEL(mainWindow->workspaceListStore), &iter); if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(mainWindow->workspaceListStore), &iter)) {
while (gtk_tree_model_iter_next(GTK_TREE_MODEL(mainWindow->workspaceListStore), &iter)) { do {
memcpy(&iter_delete, &iter, sizeof(iter)); /* Used for last element in case we need to reduce */ memcpy(&iter_delete, &iter, sizeof(iter)); /* scan for last */
gtk_tree_model_get(GTK_TREE_MODEL(mainWindow->workspaceListStore), &iter, gtk_tree_model_get(GTK_TREE_MODEL(mainWindow->workspaceListStore), &iter,
WSCOL_PATH, &temp, WSCOL_PATH, &temp,
-1); -1);
if (!g_strcmp0(temp, path)) { if (!g_strcmp0(temp, path)) {
g_free(temp); g_free(temp);
del_iter = TRUE; gtk_list_store_remove(mainWindow->workspaceListStore, &iter);
break; break;
} }
g_free(temp); g_free(temp);
} while (gtk_tree_model_iter_next(GTK_TREE_MODEL(mainWindow->workspaceListStore), &iter));
} }
temp = g_strdup_printf("<b>%s</b>\n<span weight=\"ultralight\">%s</span>", temp = g_strdup_printf("<b>%s</b>\n<span weight=\"ultralight\">%s</span>",
g_match_info_fetch(match_info, 2), g_match_info_fetch(match_info, 2),
g_match_info_fetch(match_info, 1)); g_match_info_fetch(match_info, 1));
if (append)
gtk_list_store_append(mainWindow->workspaceListStore, &iter);
else
gtk_list_store_prepend(mainWindow->workspaceListStore, &iter); gtk_list_store_prepend(mainWindow->workspaceListStore, &iter);
gtk_list_store_set(mainWindow->workspaceListStore, &iter, gtk_list_store_set(mainWindow->workspaceListStore, &iter,
WSCOL_LABEL, temp, WSCOL_LABEL, temp,
@ -185,9 +183,8 @@ void mainWindow_add_recent_workspace(struct mainWindow *mainWindow, gchar *path)
-1); -1);
g_free(temp); g_free(temp);
if (del_iter)
gtk_list_store_remove(mainWindow->workspaceListStore, &iter_delete); if (gtk_tree_model_iter_n_children(GTK_TREE_MODEL(mainWindow->workspaceListStore), NULL)
else if (gtk_tree_model_iter_n_children(GTK_TREE_MODEL(mainWindow->workspaceListStore), NULL)
> FILE_LIST_MAX_LEN) > FILE_LIST_MAX_LEN)
gtk_list_store_remove(mainWindow->workspaceListStore, &iter_delete); gtk_list_store_remove(mainWindow->workspaceListStore, &iter_delete);
} }
@ -718,7 +715,7 @@ static void mainWindow_switch_workspace(struct mainWindow *mainWindow, gchar *pa
mainWindow_set_meta_information(mainWindow, mainWindow_set_meta_information(mainWindow,
mdiary_get_time_earliest(), mdiary_get_time_latest(), mdiary_get_time_earliest(), mdiary_get_time_latest(),
FALSE); FALSE);
mainWindow_add_recent_workspace(mainWindow, path); mainWindow_add_recent_workspace(mainWindow, path, FALSE);
} else { } else {
mainWindow_show_error(mainWindow, "Could not open workspace or no entries were found in it."); mainWindow_show_error(mainWindow, "Could not open workspace or no entries were found in it.");
} }

View File

@ -8,6 +8,12 @@
#define FILE_LIST_MAX_LEN 5 #define FILE_LIST_MAX_LEN 5
enum {
WSCOL_LABEL = 0,
WSCOL_PATH,
WSCOL_COUNT
};
struct mainWindow { struct mainWindow {
GtkWidget *mainWindow; GtkWidget *mainWindow;
GtkWidget *mainPane; GtkWidget *mainPane;
@ -73,6 +79,6 @@ void mainWindow_filterChanged(GtkWidget *widget, gpointer user_data);
void mainWindow_workspace_search_changed(GtkWidget *widget, gpointer user_data); void mainWindow_workspace_search_changed(GtkWidget *widget, gpointer user_data);
void mainWindow_checkDate(GtkWidget *widget, gint event, gpointer user_data); void mainWindow_checkDate(GtkWidget *widget, gint event, gpointer user_data);
void mainWindow_dateIconPress(GtkWidget *widget, gint icon_pos, gint event, gpointer user_data); void mainWindow_dateIconPress(GtkWidget *widget, gint icon_pos, gint event, gpointer user_data);
void mainWindow_add_recent_workspace(struct mainWindow *mainWindow, gchar *path); void mainWindow_add_recent_workspace(struct mainWindow *mainWindow, gchar *path, gboolean append);
#endif /* MAINWINDOW_H */ #endif /* MAINWINDOW_H */

View File

@ -10,7 +10,7 @@ static void mdiary_settings_parse_line(gchar *line, struct mdiary_settings *sett
if (g_regex_match(regex, line, 0, &match_info) && if (g_regex_match(regex, line, 0, &match_info) &&
g_regex_get_capture_count(regex) > 0 && g_regex_get_capture_count(regex) > 0 &&
g_match_info_matches(match_info)) { g_match_info_matches(match_info)) {
mainWindow_add_recent_workspace(settings->mainWindow, g_match_info_fetch(match_info, 1)); mainWindow_add_recent_workspace(settings->mainWindow, g_match_info_fetch(match_info, 1), TRUE);
} }
g_regex_unref(regex); g_regex_unref(regex);
@ -40,12 +40,40 @@ gboolean mdiary_load_settings(gchar *filename, struct mdiary_settings *settings)
g_input_stream_close(G_INPUT_STREAM(dstream), NULL, NULL); g_input_stream_close(G_INPUT_STREAM(dstream), NULL, NULL);
g_input_stream_close(G_INPUT_STREAM(stream), NULL, NULL); g_input_stream_close(G_INPUT_STREAM(stream), NULL, NULL);
g_object_unref(file); g_object_unref(file);
} }
gboolean mdiary_save_settings(gchar *filename, struct mdiary_settings *settings) gboolean mdiary_save_settings(gchar *filename, struct mdiary_settings *settings)
{ {
g_print("SETTINGS: NYI.\n"); 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 *mdiary_settings_new(struct mainWindow *mainWindow)