changeset 1222:31dc2f240afb

low-level keyword-to-mark functionality
author nadvornik
date Thu, 25 Dec 2008 12:28:04 +0000
parents 584eae03d747
children 18d08b10b80e
files src/filedata.c src/filedata.h src/metadata.c src/metadata.h
diffstat 4 files changed, 127 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/filedata.c	Thu Dec 25 12:19:37 2008 +0000
+++ b/src/filedata.c	Thu Dec 25 12:28:04 2008 +0000
@@ -1050,14 +1050,25 @@
  * marks and orientation
  */
 
+static FileDataGetMarkFunc file_data_get_mark_func[FILEDATA_MARKS_SIZE];
+static FileDataSetMarkFunc file_data_set_mark_func[FILEDATA_MARKS_SIZE];
+static gpointer file_data_mark_func_data[FILEDATA_MARKS_SIZE];
 
 gboolean file_data_get_mark(FileData *fd, gint n)
 {
+	if (file_data_get_mark_func[n]) 
+		{
+		gboolean value = (file_data_get_mark_func[n])(fd, n, file_data_mark_func_data[n]);
+		if (!value != !(fd->marks & (1 << n))) fd->marks = fd->marks ^ (1 << n);
+		}
+
 	return !!(fd->marks & (1 << n));
 }
 
 guint file_data_get_marks(FileData *fd)
 {
+	gint i;
+	for (i = 0; i < FILEDATA_MARKS_SIZE; i++) file_data_get_mark(fd, i);
 	return fd->marks;
 }
 
@@ -1066,6 +1077,11 @@
 	guint old = fd->marks;
 	if (!value == !(fd->marks & (1 << n))) return;
 
+	if (file_data_set_mark_func[n]) 
+		{
+		(file_data_set_mark_func[n])(fd, n, value, file_data_mark_func_data[n]);
+		}
+
 	fd->marks = fd->marks ^ (1 << n);
 	
 	if (old && !fd->marks) /* keep files with non-zero marks in memory */
@@ -1083,6 +1099,8 @@
 
 gboolean file_data_filter_marks(FileData *fd, guint filter)
 {
+	gint i;
+	for (i = 0; i < FILEDATA_MARKS_SIZE; i++) if (filter & (1 << i)) file_data_get_mark(fd, i);
 	return ((fd->marks & filter) == filter);
 }
 
@@ -1108,6 +1126,37 @@
 	return list;
 }
 
+static void file_data_notify_mark_func(gpointer key, gpointer value, gpointer user_data)
+{
+	FileData *fd = value;
+	file_data_increment_version(fd);
+	file_data_send_notification(fd, NOTIFY_TYPE_INTERNAL);
+}
+
+gboolean file_data_register_mark_func(gint n, FileDataGetMarkFunc get_mark_func, FileDataSetMarkFunc set_mark_func, gpointer data)
+{
+	if (n < 0 || n >= FILEDATA_MARKS_SIZE) return FALSE;
+		
+	file_data_get_mark_func[n] = get_mark_func;
+        file_data_set_mark_func[n] = set_mark_func;
+        file_data_mark_func_data[n] = data;
+        
+        if (get_mark_func)
+    		{
+    		/* this effectively changes all known files */
+    		g_hash_table_foreach(file_data_pool, file_data_notify_mark_func, NULL);
+    		}
+        
+        return TRUE;
+}
+
+void file_data_get_registered_mark_func(gint n, FileDataGetMarkFunc *get_mark_func, FileDataSetMarkFunc *set_mark_func, gpointer *data)
+{
+	if (get_mark_func) *get_mark_func = file_data_get_mark_func[n];
+	if (set_mark_func) *set_mark_func = file_data_set_mark_func[n];
+	if (data) *data = file_data_mark_func_data[n];
+}
+
 gint file_data_get_user_orientation(FileData *fd)
 {
 	return fd->user_orientation;
--- a/src/filedata.h	Thu Dec 25 12:19:37 2008 +0000
+++ b/src/filedata.h	Thu Dec 25 12:28:04 2008 +0000
@@ -63,6 +63,12 @@
 GList *filelist_sort_path(GList *list);
 GList *filelist_recursive(FileData *dir_fd);
 
+typedef gboolean (* FileDataGetMarkFunc)(FileData *fd, gint n, gpointer data);
+typedef gboolean (* FileDataSetMarkFunc)(FileData *fd, gint n, gboolean value, gpointer data);
+gboolean file_data_register_mark_func(gint n, FileDataGetMarkFunc get_mark_func, FileDataSetMarkFunc set_mark_func, gpointer data);
+void file_data_get_registered_mark_func(gint n, FileDataGetMarkFunc *get_mark_func, FileDataSetMarkFunc *set_mark_func, gpointer *data);
+
+
 gboolean file_data_get_mark(FileData *fd, gint n);
 guint file_data_get_marks(FileData *fd);
 void file_data_set_mark(FileData *fd, gint n, gboolean value);
--- a/src/metadata.c	Thu Dec 25 12:19:37 2008 +0000
+++ b/src/metadata.c	Thu Dec 25 12:28:04 2008 +0000
@@ -789,4 +789,73 @@
 	return list;
 }
 
+/*
+ * keywords to marks
+ */
+ 
+
+gboolean meta_data_get_keyword_mark(FileData *fd, gint n, gpointer data)
+{
+	GList *keywords;
+	gboolean found = FALSE;
+	if (metadata_read(fd, &keywords, NULL))
+		{
+		GList *work = keywords;
+
+		while (work)
+			{
+			gchar *kw = work->data;
+			work = work->next;
+			
+			if (strcmp(kw, data) == 0)
+				{
+				found = TRUE;
+				break;
+				}
+			}
+		string_list_free(keywords);
+		}
+	return found;
+}
+
+gboolean meta_data_set_keyword_mark(FileData *fd, gint n, gboolean value, gpointer data)
+{
+	GList *keywords = NULL;
+	gboolean found = FALSE;
+	gboolean changed = FALSE;
+	GList *work;
+	metadata_read(fd, &keywords, NULL);
+
+	work = keywords;
+
+	while (work)
+		{
+		gchar *kw = work->data;
+		
+		if (strcmp(kw, data) == 0)
+			{
+			found = TRUE;
+			if (!value) 
+				{
+				changed = TRUE;
+				keywords = g_list_delete_link(keywords, work);
+				g_free(kw);
+				}
+			break;
+			}
+		work = work->next;
+		}
+	if (value && !found) 
+		{
+		changed = TRUE;
+		keywords = g_list_append(keywords, g_strdup(data));
+		}
+	
+	if (changed) metadata_write(fd, &keywords, NULL);
+
+	string_list_free(keywords);
+	return TRUE;
+}
+
+
 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */
--- a/src/metadata.h	Thu Dec 25 12:19:37 2008 +0000
+++ b/src/metadata.h	Thu Dec 25 12:28:04 2008 +0000
@@ -28,5 +28,8 @@
 gboolean find_string_in_list(GList *list, const gchar *keyword);
 GList *string_to_keywords_list(const gchar *text);
 
+gboolean meta_data_get_keyword_mark(FileData *fd, gint n, gpointer data);
+gboolean meta_data_set_keyword_mark(FileData *fd, gint n, gboolean value, gpointer data);
+
 #endif
 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */