diff --git a/src/mainwindow.c b/src/mainwindow.c index 91128d8..18582bf 100644 --- a/src/mainwindow.c +++ b/src/mainwindow.c @@ -1,12 +1,6 @@ #include "mainwindow.h" #include "mdiary.h" -enum { - WSCOL_LABEL = 0, - WSCOL_PATH, - WSCOL_COUNT -}; - static gboolean mainWindow_workspace_entry_visible(GtkTreeModel *model, GtkTreeIter *iter, struct mainWindow *mainWindow) @@ -147,7 +141,7 @@ static gint mainWindow_sort_date_compare_func(GtkTreeModel *model, 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; GRegex *regex; @@ -161,34 +155,37 @@ void mainWindow_add_recent_workspace(struct mainWindow *mainWindow, gchar *path) g_match_info_matches(match_info)) { g_match_info_fetch(match_info, 1); - gtk_tree_model_get_iter_first(GTK_TREE_MODEL(mainWindow->workspaceListStore), &iter); - while (gtk_tree_model_iter_next(GTK_TREE_MODEL(mainWindow->workspaceListStore), &iter)) { - memcpy(&iter_delete, &iter, sizeof(iter)); /* Used for last element in case we need to reduce */ - gtk_tree_model_get(GTK_TREE_MODEL(mainWindow->workspaceListStore), &iter, - WSCOL_PATH, &temp, - -1); - if (!g_strcmp0(temp, path)) { + if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(mainWindow->workspaceListStore), &iter)) { + do { + memcpy(&iter_delete, &iter, sizeof(iter)); /* scan for last */ + gtk_tree_model_get(GTK_TREE_MODEL(mainWindow->workspaceListStore), &iter, + WSCOL_PATH, &temp, + -1); + if (!g_strcmp0(temp, path)) { + g_free(temp); + gtk_list_store_remove(mainWindow->workspaceListStore, &iter); + break; + } g_free(temp); - del_iter = TRUE; - break; - } - g_free(temp); + } while (gtk_tree_model_iter_next(GTK_TREE_MODEL(mainWindow->workspaceListStore), &iter)); } temp = g_strdup_printf("%s\n%s", g_match_info_fetch(match_info, 2), g_match_info_fetch(match_info, 1)); - gtk_list_store_prepend(mainWindow->workspaceListStore, &iter); + if (append) + gtk_list_store_append(mainWindow->workspaceListStore, &iter); + else + gtk_list_store_prepend(mainWindow->workspaceListStore, &iter); gtk_list_store_set(mainWindow->workspaceListStore, &iter, WSCOL_LABEL, temp, WSCOL_PATH, path, -1); g_free(temp); - if (del_iter) - gtk_list_store_remove(mainWindow->workspaceListStore, &iter_delete); - else if (gtk_tree_model_iter_n_children(GTK_TREE_MODEL(mainWindow->workspaceListStore), NULL) - > FILE_LIST_MAX_LEN) + + if (gtk_tree_model_iter_n_children(GTK_TREE_MODEL(mainWindow->workspaceListStore), NULL) + > FILE_LIST_MAX_LEN) 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, mdiary_get_time_earliest(), mdiary_get_time_latest(), FALSE); - mainWindow_add_recent_workspace(mainWindow, path); + mainWindow_add_recent_workspace(mainWindow, path, FALSE); } else { mainWindow_show_error(mainWindow, "Could not open workspace or no entries were found in it."); } diff --git a/src/mainwindow.h b/src/mainwindow.h index 5e7ba33..9b1d794 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -8,6 +8,12 @@ #define FILE_LIST_MAX_LEN 5 +enum { + WSCOL_LABEL = 0, + WSCOL_PATH, + WSCOL_COUNT +}; + struct mainWindow { GtkWidget *mainWindow; 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_checkDate(GtkWidget *widget, 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 */ diff --git a/src/settings.c b/src/settings.c index 5807010..a88e881 100644 --- a/src/settings.c +++ b/src/settings.c @@ -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) && 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)); + mainWindow_add_recent_workspace(settings->mainWindow, g_match_info_fetch(match_info, 1), TRUE); } 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(stream), NULL, NULL); g_object_unref(file); - } 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)