diff --git a/src/mainwindow.c b/src/mainwindow.c index 49b0463..c069a3d 100644 --- a/src/mainwindow.c +++ b/src/mainwindow.c @@ -149,10 +149,11 @@ static gint mainWindow_sort_date_compare_func(GtkTreeModel *model, void mainWindow_add_recent_workspace(struct mainWindow *mainWindow, gchar *path) { - GtkTreeIter iter, iter2; + GtkTreeIter iter, iter_delete; GRegex *regex; GMatchInfo *match_info; gchar *temp; + gboolean del_iter = FALSE; regex = g_regex_new("(.*)\\/(.*)", G_REGEX_RAW, 0, NULL); if (g_regex_match(regex, path, 0, &match_info) && @@ -160,6 +161,20 @@ 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)) { + g_free(temp); + del_iter = TRUE; + break; + } + g_free(temp); + } + temp = g_strdup_printf("%s\n%s", g_match_info_fetch(match_info, 2), g_match_info_fetch(match_info, 1)); @@ -170,13 +185,11 @@ void mainWindow_add_recent_workspace(struct mainWindow *mainWindow, gchar *path) -1); g_free(temp); - if (gtk_tree_model_iter_n_children(GTK_TREE_MODEL(mainWindow->workspaceListStore), NULL) - > FILE_LIST_MAX_LEN) { - while (gtk_tree_model_iter_next(GTK_TREE_MODEL(mainWindow->workspaceListStore), &iter)) { - memcpy(&iter2, &iter, sizeof(iter)); /* TODO: A bit of a hack if you ask me... */ - } - gtk_list_store_remove(mainWindow->workspaceListStore, &iter2); - } + 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) + gtk_list_store_remove(mainWindow->workspaceListStore, &iter_delete); } g_regex_unref(regex); @@ -703,12 +716,14 @@ static void mainWindow_switch_workspace(struct mainWindow *mainWindow, gchar *pa gtk_popover_popdown(GTK_POPOVER(mainWindow->popoverWorkspace)); mainWindow_set_meta_information(mainWindow, 0, 0, TRUE); mdiary_reset_store(mainWindow->entryListStore, mainWindow->completionListStore); - if (mdiary_scan_to_store(path, mainWindow->entryListStore, mainWindow->completionListStore)) + if (mdiary_scan_to_store(path, mainWindow->entryListStore, mainWindow->completionListStore)) { mainWindow_set_meta_information(mainWindow, mdiary_get_time_earliest(), mdiary_get_time_latest(), FALSE); - else + mainWindow_add_recent_workspace(mainWindow, path); + } else { mainWindow_show_error(mainWindow, "Could not open workspace or no entries were found in it."); + } }