Now using listStore as data backend. Added sorting function for date.
This commit is contained in:
parent
cc56768804
commit
a4b028b928
140
src/mainwindow.c
140
src/mainwindow.c
@ -2,27 +2,65 @@
|
||||
#include "mdiary.h"
|
||||
|
||||
enum {
|
||||
COL_TITLE = 0,
|
||||
COL_DATE,
|
||||
COL_TAGS,
|
||||
COL_TIMESTAMP,
|
||||
COL_TITLE = 0, /* Title of the entry */
|
||||
COL_DATE_TEXT, /* Textual representation of the date (auto generated) */
|
||||
COL_TAGS_TEXT, /* Textual representation of the tags (auto generated) */
|
||||
COL_TIMESTAMP, /* GDateTime of the entry */
|
||||
COL_TAGLIST, /* GList<gchar *> of the tags */
|
||||
COL_TEXT, /* Raw text from the file */
|
||||
COL_COUNT
|
||||
};
|
||||
|
||||
static void mainWindow_list_add_entry(struct mainWindow *mainWindow, struct mdiary_entry *mdiary_entry) {
|
||||
GtkTreeIter iter;
|
||||
gchar *date_text;
|
||||
/**
|
||||
* Returns: The string, needs to be freed using g_free().
|
||||
*/
|
||||
static gchar *mainWindow_taglist_to_string(GList *list)
|
||||
{
|
||||
GString *str = NULL;
|
||||
gchar *ret = NULL;
|
||||
GList *l;
|
||||
|
||||
date_text = g_date_time_format(mdiary_entry->date, "%A, %e %B %Y %R");
|
||||
for (l = list; l != NULL; l = l->next)
|
||||
{
|
||||
if (str == NULL)
|
||||
str = g_string_new("");
|
||||
else
|
||||
str = g_string_append(str, ", ");
|
||||
str = g_string_append(str, l->data);
|
||||
}
|
||||
|
||||
ret = str->str;
|
||||
g_string_free(str, 0);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Note: This function creates a copyof each argument.
|
||||
*/
|
||||
static void mainWindow_list_add_entry(struct mainWindow *mainWindow,
|
||||
gchar *title,
|
||||
GDateTime *datetime,
|
||||
GList *tags,
|
||||
gchar *text) {
|
||||
GtkTreeIter iter;
|
||||
GDateTime *datetime_copy;
|
||||
gchar *date_text;
|
||||
gchar *taglistString;
|
||||
|
||||
datetime_copy = g_date_time_add(datetime, 0);
|
||||
date_text = g_date_time_format(datetime_copy, "%A, %e %B %Y %R");
|
||||
taglistString = mainWindow_taglist_to_string(tags);
|
||||
|
||||
gtk_list_store_append(mainWindow->entryListStore, &iter);
|
||||
gtk_list_store_set(mainWindow->entryListStore, &iter,
|
||||
COL_TITLE, mdiary_entry->title,
|
||||
COL_DATE, date_text,
|
||||
COL_TAGS, mdiary_entry->tags,
|
||||
COL_TIMESTAMP, g_date_time_to_unix(mdiary_entry->date),
|
||||
COL_TITLE, title,
|
||||
COL_DATE_TEXT, date_text,
|
||||
COL_TAGS_TEXT, taglistString,
|
||||
COL_TIMESTAMP, datetime_copy,
|
||||
COL_TAGLIST, NULL,
|
||||
COL_TEXT, g_strdup(text),
|
||||
-1);
|
||||
|
||||
g_free(date_text);
|
||||
}
|
||||
|
||||
@ -33,6 +71,30 @@ static gboolean mainWindow_list_entry_visible (GtkTreeModel *model, GtkTreeIter
|
||||
return 1;
|
||||
}
|
||||
|
||||
static gint mainWindow_sort_date_compare_func(GtkTreeModel *model,
|
||||
GtkTreeIter *a,
|
||||
GtkTreeIter *b,
|
||||
gpointer userdata)
|
||||
{
|
||||
GDateTime *datetime1;
|
||||
GDateTime *datetime2;
|
||||
guint time1;
|
||||
guint time2;
|
||||
|
||||
gtk_tree_model_get(model, a, COL_TIMESTAMP, &datetime1, -1);
|
||||
gtk_tree_model_get(model, b, COL_TIMESTAMP, &datetime2, -1);
|
||||
time1 = g_date_time_to_unix(datetime1);
|
||||
time2 = g_date_time_to_unix(datetime2);
|
||||
|
||||
if (time1 == time2)
|
||||
return 0;
|
||||
else if (time1 > time2)
|
||||
return 1;
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
static void mainWindow_configure_treeView(struct mainWindow *mainWindow)
|
||||
{
|
||||
GtkCellRenderer *renderer;
|
||||
@ -46,11 +108,14 @@ static void mainWindow_configure_treeView(struct mainWindow *mainWindow)
|
||||
G_TYPE_STRING,
|
||||
G_TYPE_STRING,
|
||||
G_TYPE_STRING,
|
||||
G_TYPE_UINT);
|
||||
G_TYPE_POINTER,
|
||||
G_TYPE_POINTER,
|
||||
G_TYPE_STRING);
|
||||
filtered = GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (
|
||||
GTK_TREE_MODEL (mainWindow->entryListStore), NULL));
|
||||
sorted = GTK_TREE_MODEL_SORT (gtk_tree_model_sort_new_with_model (
|
||||
GTK_TREE_MODEL (filtered)));
|
||||
|
||||
gtk_tree_model_filter_set_visible_func (filtered,
|
||||
(GtkTreeModelFilterVisibleFunc) mainWindow_list_entry_visible,
|
||||
mainWindow, NULL);
|
||||
@ -59,6 +124,8 @@ static void mainWindow_configure_treeView(struct mainWindow *mainWindow)
|
||||
|
||||
/* TODO: Unref mainWindow->entryListStore and filtered, sorted and model? */
|
||||
|
||||
sortable = GTK_TREE_SORTABLE(sorted);
|
||||
|
||||
renderer = gtk_cell_renderer_text_new();
|
||||
gtk_tree_view_insert_column_with_attributes(mainWindow->entryListView, -1, "Title", renderer,
|
||||
"text", COL_TITLE, NULL);
|
||||
@ -68,43 +135,40 @@ static void mainWindow_configure_treeView(struct mainWindow *mainWindow)
|
||||
|
||||
renderer = gtk_cell_renderer_text_new();
|
||||
gtk_tree_view_insert_column_with_attributes(mainWindow->entryListView, -1, "Date", renderer,
|
||||
"text", COL_DATE, NULL);
|
||||
col = gtk_tree_view_get_column(mainWindow->entryListView, COL_DATE);
|
||||
"text", COL_DATE_TEXT, NULL);
|
||||
col = gtk_tree_view_get_column(mainWindow->entryListView, COL_DATE_TEXT);
|
||||
gtk_tree_view_column_set_resizable(col, 1);
|
||||
gtk_tree_view_column_set_sort_column_id(col, COL_TIMESTAMP);
|
||||
gtk_tree_sortable_set_sort_func(sortable, COL_TIMESTAMP, mainWindow_sort_date_compare_func,
|
||||
GINT_TO_POINTER(COL_TIMESTAMP), NULL);
|
||||
gtk_tree_view_column_set_sort_column_id(col, COL_DATE_TEXT);
|
||||
|
||||
renderer = gtk_cell_renderer_text_new();
|
||||
gtk_tree_view_insert_column_with_attributes(mainWindow->entryListView, -1, "Tags", renderer,
|
||||
"text", COL_TAGS, NULL);
|
||||
col = gtk_tree_view_get_column(mainWindow->entryListView, COL_TAGS);
|
||||
"text", COL_TAGS_TEXT, NULL);
|
||||
col = gtk_tree_view_get_column(mainWindow->entryListView, COL_TAGS_TEXT);
|
||||
gtk_tree_view_column_set_resizable(col, 1);
|
||||
gtk_tree_view_column_set_sort_column_id(col, COL_TAGS);
|
||||
gtk_tree_view_column_set_sort_column_id(col, COL_TAGS_TEXT);
|
||||
|
||||
sortable = GTK_TREE_SORTABLE(mainWindow->entryListStore);
|
||||
gtk_tree_sortable_set_sort_column_id(sortable, COL_DATE, GTK_SORT_DESCENDING);
|
||||
gtk_tree_sortable_set_sort_column_id(sortable, COL_DATE_TEXT, GTK_SORT_DESCENDING);
|
||||
|
||||
/*
|
||||
* Test entries for debugging only
|
||||
*/
|
||||
|
||||
struct mdiary_entry test;
|
||||
test.title = "Hello";
|
||||
test.tags = "world";
|
||||
test.date = g_date_time_new_local(2017, 11, 21, 02, 03, 0);
|
||||
mainWindow_list_add_entry(mainWindow, &test);
|
||||
g_date_time_unref(test.date);
|
||||
GDateTime *dt;
|
||||
GList *list = NULL;
|
||||
|
||||
test.title = "Entry";
|
||||
test.tags = "Tag, B, C";
|
||||
test.date = g_date_time_new_local(2016, 10, 4, 5, 34, 0);
|
||||
mainWindow_list_add_entry(mainWindow, &test);
|
||||
g_date_time_unref(test.date);
|
||||
list = g_list_append(list, "Tag0");
|
||||
list = g_list_append(list, "Tag1");
|
||||
list = g_list_append(list, "Tag2");
|
||||
|
||||
test.title = "A Third One for Testing";
|
||||
test.tags = "Yey, stuff, works";
|
||||
test.date = g_date_time_new_local(2016, 12, 22, 1, 55, 0);
|
||||
mainWindow_list_add_entry(mainWindow, &test);
|
||||
g_date_time_unref(test.date);
|
||||
dt = g_date_time_new_local(2017, 11, 21, 02, 03, 0);
|
||||
mainWindow_list_add_entry(mainWindow, "Title", dt, list, "Text text text");
|
||||
g_date_time_unref(dt);
|
||||
|
||||
dt = g_date_time_new_local(2017, 2, 3, 6, 7, 0);
|
||||
mainWindow_list_add_entry(mainWindow, "AAA 2", dt, list, "fdfd Text text text");
|
||||
g_date_time_unref(dt);
|
||||
}
|
||||
|
||||
static void mainWindow_connect_signals(struct mainWindow *mainWindow)
|
||||
|
@ -1,12 +1,4 @@
|
||||
#ifndef MDIARY_H
|
||||
#define MDIARY_H
|
||||
|
||||
#include <glib.h>
|
||||
|
||||
struct mdiary_entry {
|
||||
gchar *title;
|
||||
GDateTime *date;
|
||||
gchar *tags;
|
||||
};
|
||||
|
||||
#endif /* MDIARY_H */
|
||||
|
Loading…
Reference in New Issue
Block a user