Skip to content

Commit

Permalink
low-level keyword-to-mark functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
nadvornik committed Dec 25, 2008
1 parent 4bf1446 commit 2dd4ade
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/filedata.c
Original file line number Diff line number Diff line change
Expand Up @@ -1050,14 +1050,25 @@ GList *filelist_recursive(FileData *dir_fd)
* 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;
}

Expand All @@ -1066,6 +1077,11 @@ void file_data_set_mark(FileData *fd, gint n, gboolean value)
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 */
Expand All @@ -1083,6 +1099,8 @@ void file_data_set_mark(FileData *fd, gint n, gboolean value)

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);
}

Expand All @@ -1108,6 +1126,37 @@ GList *file_data_filter_marks_list(GList *list, guint filter)
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;
Expand Down
6 changes: 6 additions & 0 deletions src/filedata.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ GList *filelist_filter(GList *list, gint is_dir_list);
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);
Expand Down
69 changes: 69 additions & 0 deletions src/metadata.c
Original file line number Diff line number Diff line change
Expand Up @@ -789,4 +789,73 @@ GList *string_to_keywords_list(const gchar *text)
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: */
3 changes: 3 additions & 0 deletions src/metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,8 @@ void metadata_set(FileData *fd, GList *new_keywords, gchar *new_comment, gboolea
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: */

0 comments on commit 2dd4ade

Please sign in to comment.