2011-06-13 22:04:20 +02:00
|
|
|
/* -*-mode: c; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-*/
|
|
|
|
/*
|
|
|
|
** Copyright (C) 2011 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
|
|
|
**
|
|
|
|
** This program is free software; you can redistribute it and/or modify it
|
|
|
|
** under the terms of the GNU General Public License as published by the
|
|
|
|
** Free Software Foundation; either version 3, or (at your option) any
|
|
|
|
** later version.
|
|
|
|
**
|
|
|
|
** This program is distributed in the hope that it will be useful,
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
** GNU General Public License for more details.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
** along with this program; if not, write to the Free Software Foundation,
|
|
|
|
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
**
|
|
|
|
*/
|
2011-06-23 23:21:54 +02:00
|
|
|
#include <math.h> /* for log, ceil */
|
2011-06-13 22:04:20 +02:00
|
|
|
|
|
|
|
#include "mu-msg-threader.h"
|
2011-06-18 17:47:13 +02:00
|
|
|
#include "mu-str.h"
|
2011-06-13 22:04:20 +02:00
|
|
|
|
2011-06-19 20:02:41 +02:00
|
|
|
|
|
|
|
/* msg threading implementation based on JWZ's algorithm, as described in:
|
|
|
|
* http://www.jwz.org/doc/threading.html
|
|
|
|
*
|
|
|
|
* the implementation follows the terminology from that doc, so should
|
|
|
|
* be understandable from that... I did change things a bit though
|
|
|
|
*
|
|
|
|
* the end result of the threading operation is a hashtable which maps
|
|
|
|
* docids (ie., Xapian documents == messages) to 'thread paths'; a
|
|
|
|
* thread path is a string denoting the 2-dimensional place of a
|
|
|
|
* message in a list of messages,
|
|
|
|
*
|
|
|
|
* Msg1 => 00000
|
|
|
|
* Msg2 => 00001
|
|
|
|
* Msg3 (child of Msg2) => 00001:00000
|
|
|
|
* Msg4 (child of Msg2) => 00001:00001
|
|
|
|
* Msg5 (child of Msg4) => 00001:00001:00000
|
|
|
|
* Msg6 => 00002
|
|
|
|
*
|
2011-06-23 23:21:54 +02:00
|
|
|
* the padding-0's are added to make them easy to sort using strcmp;
|
|
|
|
* the number hexadecimal numbers, and the length of the 'segments'
|
|
|
|
* (the parts separated by the ':') is equal to ceil(log_16(matchnum))
|
2011-06-19 20:02:41 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-06-23 23:21:54 +02:00
|
|
|
|
2011-06-19 20:02:41 +02:00
|
|
|
/* Container data structure, as seen in the JWZ-doc; one differences
|
|
|
|
* is that I use GSLists for the children, rather than 'next'
|
|
|
|
* pointers
|
|
|
|
*
|
|
|
|
* */
|
2011-06-13 22:04:20 +02:00
|
|
|
struct _Container {
|
2011-06-18 17:47:13 +02:00
|
|
|
MuMsg *_msg;
|
|
|
|
unsigned int _docid;
|
|
|
|
struct _Container *_parent;
|
|
|
|
GSList *_children;
|
2011-06-23 23:21:54 +02:00
|
|
|
gboolean _dup;
|
2011-06-13 22:04:20 +02:00
|
|
|
};
|
2011-06-18 17:47:13 +02:00
|
|
|
typedef struct _Container Container;
|
2011-06-13 22:04:20 +02:00
|
|
|
|
2011-06-18 17:47:13 +02:00
|
|
|
static Container *container_new (MuMsg *msg, unsigned docid);
|
2011-06-13 22:04:20 +02:00
|
|
|
static void container_destroy (Container *c);
|
2011-06-18 17:47:13 +02:00
|
|
|
static void container_dump (Container *c);
|
2011-06-13 22:04:20 +02:00
|
|
|
/* breath-first recursive traversal */
|
2011-06-18 17:47:13 +02:00
|
|
|
typedef gboolean (*ContainerTraverseFunc) (Container *c, guint level,
|
|
|
|
gpointer user_data);
|
|
|
|
static gboolean container_traverse (Container *c,
|
|
|
|
guint level,
|
|
|
|
ContainerTraverseFunc func,
|
2011-06-13 22:04:20 +02:00
|
|
|
gpointer user_data);
|
2011-06-19 20:02:41 +02:00
|
|
|
static gboolean container_add_child (Container *c, Container *child);
|
2011-06-23 23:21:54 +02:00
|
|
|
static void container_splice_child (Container *c, Container *child);
|
|
|
|
static gboolean container_is_root (Container *c);
|
|
|
|
static void container_dump_tree (Container *c);
|
2011-06-18 17:47:13 +02:00
|
|
|
static void container_remove_child (Container *c, Container *child);
|
|
|
|
|
|
|
|
/* step 1 */ static GHashTable* create_containers (MuMsgIter *iter);
|
2011-06-23 23:21:54 +02:00
|
|
|
/* step 2 */ static Container *find_root (GHashTable *ids);
|
|
|
|
static void prune_empty_containers (Container *root);
|
2011-06-18 17:47:13 +02:00
|
|
|
/* static void group_root_set_by_subject (GSList *root_set); */
|
2011-06-23 23:21:54 +02:00
|
|
|
GHashTable* create_doc_id_thread_path_hash (Container *root, size_t match_num);
|
|
|
|
static void sort_by_date (Container *root);
|
2011-06-18 17:47:13 +02:00
|
|
|
|
|
|
|
/* msg threading algorithm, based on JWZ's algorithm,
|
|
|
|
* http://www.jwz.org/doc/threading.html */
|
|
|
|
GHashTable*
|
2011-06-19 20:02:41 +02:00
|
|
|
mu_msg_threader_calculate (MuMsgIter *iter, size_t matchnum)
|
2011-06-13 22:04:20 +02:00
|
|
|
{
|
2011-06-19 20:02:41 +02:00
|
|
|
GHashTable *id_table, *thread_ids;
|
2011-06-23 23:21:54 +02:00
|
|
|
Container *root;
|
2011-06-18 17:47:13 +02:00
|
|
|
|
|
|
|
g_return_val_if_fail (iter, FALSE);
|
|
|
|
|
|
|
|
/* step 1 */
|
|
|
|
id_table = create_containers (iter);
|
|
|
|
|
2011-06-23 23:21:54 +02:00
|
|
|
/* step 2 -- JWZ calls this the 'root-set'; in our case, the
|
|
|
|
* root_set is the list of children for the dummy
|
|
|
|
* root-container */
|
|
|
|
root = find_root (id_table);
|
2011-06-19 20:02:41 +02:00
|
|
|
/* step 3: skip until the end; we still need to containers */
|
2011-06-23 23:21:54 +02:00
|
|
|
//container_dump_tree (root);
|
|
|
|
|
2011-06-18 17:47:13 +02:00
|
|
|
/* step 4: prune empty containers */
|
2011-06-23 23:21:54 +02:00
|
|
|
prune_empty_containers (root);
|
2011-06-18 17:47:13 +02:00
|
|
|
|
|
|
|
/* recalculate root set */
|
2011-06-23 23:21:54 +02:00
|
|
|
sort_by_date (root);
|
|
|
|
|
|
|
|
//container_dump_tree (root);
|
|
|
|
|
2011-06-18 17:47:13 +02:00
|
|
|
|
|
|
|
/* step 5: group root set by subject */
|
2011-06-23 23:21:54 +02:00
|
|
|
//group_root_set_by_subject (root_set);
|
2011-06-18 17:47:13 +02:00
|
|
|
|
2011-06-19 20:02:41 +02:00
|
|
|
/* sort */
|
2011-06-18 17:47:13 +02:00
|
|
|
mu_msg_iter_reset (iter); /* go all the way back */
|
2011-06-19 20:02:41 +02:00
|
|
|
|
|
|
|
/* finally, deliver the docid => thread-path hash */
|
2011-06-23 23:21:54 +02:00
|
|
|
thread_ids = create_doc_id_thread_path_hash (root, matchnum);
|
2011-06-18 17:47:13 +02:00
|
|
|
g_hash_table_destroy (id_table); /* step 3*/
|
|
|
|
|
2011-06-23 23:21:54 +02:00
|
|
|
container_destroy (root);
|
|
|
|
|
2011-06-19 20:02:41 +02:00
|
|
|
return thread_ids;
|
2011-06-13 22:04:20 +02:00
|
|
|
}
|
|
|
|
|
2011-06-18 17:47:13 +02:00
|
|
|
static gboolean
|
|
|
|
check_exists (Container *c, guint level, Container *c2)
|
|
|
|
{
|
|
|
|
return c != c2;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
already_referenced (Container *c1, Container *c2)
|
2011-06-13 22:04:20 +02:00
|
|
|
{
|
2011-06-18 17:47:13 +02:00
|
|
|
gboolean notfound;
|
|
|
|
notfound = container_traverse ((Container*)c1, 0,
|
|
|
|
(ContainerTraverseFunc)check_exists,
|
|
|
|
c2);
|
|
|
|
|
|
|
|
return notfound ? FALSE : TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* find a container for the given msgid; if it does not exist yet,
|
|
|
|
* create a new one, and register it */
|
|
|
|
static Container*
|
|
|
|
find_or_create (GHashTable *id_table, const char* msgid, unsigned docid)
|
|
|
|
{
|
|
|
|
Container *c;
|
2011-06-13 22:04:20 +02:00
|
|
|
|
2011-06-23 23:21:54 +02:00
|
|
|
c = g_hash_table_lookup (id_table, msgid);
|
2011-06-18 17:47:13 +02:00
|
|
|
if (!c) {
|
|
|
|
c = container_new (NULL, docid);
|
|
|
|
g_hash_table_insert (id_table, (gpointer)msgid, c);
|
2011-06-23 23:21:54 +02:00
|
|
|
if (docid != 0)
|
|
|
|
g_print ("*1 %s => %u\n", msgid, docid);
|
|
|
|
} else if (c->_docid == 0) {
|
|
|
|
c->_docid = docid;
|
|
|
|
g_print ("*2 %s => %u\n", msgid, docid);
|
|
|
|
} else if (docid != 0) { /* duplicate message-id */
|
|
|
|
/* Container *c2; */
|
|
|
|
/* char *fake_msgid; */
|
|
|
|
/* g_print ("duplicate message-id %s\n", msgid); /\* FIXME: leak *\/ */
|
|
|
|
/* c2 = container_new (NULL, docid); */
|
|
|
|
/* c2->_parent = c; /\* make it a child of the other one...*\/ */
|
|
|
|
/* c2->_dup = TRUE; */
|
|
|
|
/* fake_msgid = g_strdup_printf ("%s_%u", msgid, docid); */
|
|
|
|
/* g_hash_table_insert (id_table, fake_msgid, c2); */
|
|
|
|
/* g_print ("*3 %s => %u\n", fake_msgid, docid); */
|
2011-06-19 20:02:41 +02:00
|
|
|
}
|
2011-06-23 23:21:54 +02:00
|
|
|
|
2011-06-18 17:47:13 +02:00
|
|
|
return c;
|
2011-06-13 22:04:20 +02:00
|
|
|
}
|
|
|
|
|
2011-06-18 17:47:13 +02:00
|
|
|
static void /* 1B */
|
|
|
|
handle_references (GHashTable *id_table, Container *c)
|
2011-06-13 22:04:20 +02:00
|
|
|
{
|
2011-06-18 17:47:13 +02:00
|
|
|
const GSList *refs, *cur;
|
|
|
|
|
|
|
|
refs = mu_msg_get_references (c->_msg);
|
|
|
|
if (!refs)
|
|
|
|
return; /* nothing to do */
|
|
|
|
|
|
|
|
/* go over over our list of refs */
|
|
|
|
for (cur = refs; cur && cur->next; cur = g_slist_next (cur)) {
|
|
|
|
Container *c1, *c2; /* two consecutive refs in the list;
|
|
|
|
* we register them as parent, child */
|
2011-06-23 23:21:54 +02:00
|
|
|
|
2011-06-18 17:47:13 +02:00
|
|
|
c1 = find_or_create (id_table, (gchar*)cur->data, 0);
|
|
|
|
c2 = find_or_create (id_table, (gchar*)cur->next->data, 0);
|
|
|
|
|
2011-06-19 20:02:41 +02:00
|
|
|
container_add_child (c1, c2);
|
2011-06-18 17:47:13 +02:00
|
|
|
}
|
|
|
|
|
2011-06-23 23:21:54 +02:00
|
|
|
/* now cur points to the final ref, which refers to our direct
|
2011-06-18 17:47:13 +02:00
|
|
|
* parent... register it */
|
|
|
|
if (cur) {
|
|
|
|
Container *parent;
|
|
|
|
parent = find_or_create (id_table, (gchar*)cur->data, 0);
|
2011-06-19 20:02:41 +02:00
|
|
|
container_add_child (parent, c);
|
2011-06-18 17:47:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-13 22:04:20 +02:00
|
|
|
|
2011-06-18 17:47:13 +02:00
|
|
|
/* step 1: create the containers, connect them, and fill the id_table */
|
|
|
|
static GHashTable*
|
|
|
|
create_containers (MuMsgIter *iter)
|
|
|
|
{
|
|
|
|
GHashTable *id_table;
|
|
|
|
id_table = g_hash_table_new_full (g_str_hash,
|
|
|
|
g_str_equal,
|
|
|
|
NULL, /* we don't copy msgid */
|
|
|
|
(GDestroyNotify)container_destroy);
|
|
|
|
|
2011-06-13 22:04:20 +02:00
|
|
|
for (mu_msg_iter_reset (iter); !mu_msg_iter_is_done (iter);
|
|
|
|
mu_msg_iter_next (iter)) {
|
2011-06-23 23:21:54 +02:00
|
|
|
|
2011-06-13 22:04:20 +02:00
|
|
|
Container *c;
|
2011-06-18 17:47:13 +02:00
|
|
|
MuMsg *msg;
|
|
|
|
unsigned docid;
|
|
|
|
const char *msgid;
|
|
|
|
|
2011-06-13 22:04:20 +02:00
|
|
|
/* 1.A */
|
2011-06-23 23:21:54 +02:00
|
|
|
msg = mu_msg_iter_get_msg (iter, NULL);
|
2011-06-18 17:47:13 +02:00
|
|
|
msgid = mu_msg_get_msgid (msg);
|
|
|
|
docid = mu_msg_iter_get_docid (iter);
|
|
|
|
|
|
|
|
if (!msgid) {
|
|
|
|
const char* path;
|
|
|
|
path = mu_msg_get_path(msg);
|
2011-06-19 20:02:41 +02:00
|
|
|
/* g_warning ("msg without msgid %s", path); */
|
2011-06-18 17:47:13 +02:00
|
|
|
msgid = path; /* fake it... */
|
2011-06-13 22:04:20 +02:00
|
|
|
}
|
|
|
|
|
2011-06-18 17:47:13 +02:00
|
|
|
c = find_or_create (id_table, msgid, docid);
|
|
|
|
if (!c->_msg)
|
|
|
|
c->_msg = mu_msg_ref (msg);
|
|
|
|
|
2011-06-13 22:04:20 +02:00
|
|
|
/* 1.B and C */
|
2011-06-18 17:47:13 +02:00
|
|
|
handle_references (id_table, c);
|
|
|
|
}
|
2011-06-13 22:04:20 +02:00
|
|
|
|
2011-06-18 17:47:13 +02:00
|
|
|
return id_table;
|
|
|
|
}
|
2011-06-13 22:04:20 +02:00
|
|
|
|
|
|
|
|
2011-06-18 17:47:13 +02:00
|
|
|
|
|
|
|
static void
|
2011-06-23 23:21:54 +02:00
|
|
|
filter_root_set (const gchar *msgid, Container *c, Container *root)
|
2011-06-18 17:47:13 +02:00
|
|
|
{
|
2011-06-23 23:21:54 +02:00
|
|
|
if (!c->_parent) /* this *before* adding it to the dummy root */
|
|
|
|
container_add_child (root, c);
|
2011-06-13 22:04:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-23 23:21:54 +02:00
|
|
|
/* 2. Find the root - this is dummy container which takes the
|
|
|
|
* until-now parentless Container-objects as children. JWZ calls this
|
|
|
|
* the 'root_set'
|
2011-06-18 17:47:13 +02:00
|
|
|
|
2011-06-23 23:21:54 +02:00
|
|
|
Walk over the elements of id_table, and gather a list of the
|
|
|
|
Container objects that have no parents, but do have children */
|
|
|
|
static Container*
|
|
|
|
find_root (GHashTable *ids)
|
2011-06-13 22:04:20 +02:00
|
|
|
{
|
2011-06-18 17:47:13 +02:00
|
|
|
GSList *lst;
|
2011-06-23 23:21:54 +02:00
|
|
|
Container *root;
|
2011-06-13 22:04:20 +02:00
|
|
|
|
2011-06-23 23:21:54 +02:00
|
|
|
root = container_new (NULL, 0);
|
2011-06-18 17:47:13 +02:00
|
|
|
|
2011-06-23 23:21:54 +02:00
|
|
|
lst = NULL;
|
|
|
|
g_hash_table_foreach (ids, (GHFunc)filter_root_set, root);
|
|
|
|
|
|
|
|
return root;
|
2011-06-13 22:04:20 +02:00
|
|
|
}
|
|
|
|
|
2011-06-18 17:47:13 +02:00
|
|
|
/* this function will mark 'containers', and the do the pruning on
|
|
|
|
* their children */
|
|
|
|
static void
|
2011-06-23 23:21:54 +02:00
|
|
|
prune_empty_containers (Container *container)
|
2011-06-13 22:04:20 +02:00
|
|
|
{
|
2011-06-18 17:47:13 +02:00
|
|
|
GSList *cur;
|
2011-06-13 22:04:20 +02:00
|
|
|
|
2011-06-23 23:21:54 +02:00
|
|
|
for (cur = container->_children; cur; cur = g_slist_next (cur)) {
|
|
|
|
|
|
|
|
Container *c;
|
|
|
|
c = (Container*)cur->data;
|
|
|
|
|
|
|
|
prune_empty_containers (c); /* recurse! */
|
|
|
|
|
2011-06-19 20:02:41 +02:00
|
|
|
/* don't touch containers with messages */
|
2011-06-23 23:21:54 +02:00
|
|
|
if (c->_msg)
|
2011-06-19 20:02:41 +02:00
|
|
|
continue;
|
2011-06-23 23:21:54 +02:00
|
|
|
|
2011-06-19 20:02:41 +02:00
|
|
|
/* A. If it is an msg-less container with no children, nuke it. */
|
2011-06-23 23:21:54 +02:00
|
|
|
if (!c->_children) {
|
|
|
|
container_remove_child (c->_parent, c);
|
|
|
|
continue;
|
|
|
|
}
|
2011-06-18 17:47:13 +02:00
|
|
|
/* B. If the Container has no Message, but does have
|
|
|
|
* children, remove this container but promote its
|
|
|
|
* children to this level (that is, splice them in to
|
|
|
|
* the current child list.)
|
|
|
|
*
|
|
|
|
* Do not promote the children if doing so would
|
|
|
|
* promote them to the root set -- unless there is
|
2011-06-23 23:21:54 +02:00
|
|
|
* only one child, in which case, do.
|
|
|
|
*/
|
|
|
|
if (container_is_root(container) &&
|
|
|
|
g_slist_length(c->_children) != 1)
|
|
|
|
continue;
|
2011-06-13 22:04:20 +02:00
|
|
|
|
2011-06-23 23:21:54 +02:00
|
|
|
container_splice_child (container, c);
|
2011-06-18 17:47:13 +02:00
|
|
|
}
|
2011-06-13 22:04:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-23 23:21:54 +02:00
|
|
|
|
2011-06-18 17:47:13 +02:00
|
|
|
#if 0
|
|
|
|
/* 5. group root set by subject */
|
|
|
|
static void
|
|
|
|
group_root_set_by_subject (GSList *root_set)
|
2011-06-13 22:04:20 +02:00
|
|
|
{
|
2011-06-18 17:47:13 +02:00
|
|
|
GHashTable *subject_table;
|
|
|
|
GSList *cur;
|
2011-06-13 22:04:20 +02:00
|
|
|
|
2011-06-18 17:47:13 +02:00
|
|
|
/* A: Construct a new hash table, subject_table, which
|
|
|
|
* associates subject strings with Container objects. */
|
|
|
|
|
|
|
|
subject_table = g_hash_table_new (g_str_hash, g_str_equal);
|
|
|
|
|
|
|
|
for (cur = root_set; cur; cur = g_slist_next (cur)) {
|
|
|
|
|
|
|
|
const char *subject, *subj;
|
|
|
|
/* subject without Re: Fwd: etc. */
|
2011-06-13 22:04:20 +02:00
|
|
|
|
2011-06-18 17:47:13 +02:00
|
|
|
/* Find the subject of that sub-tree: */
|
|
|
|
Container *c;
|
|
|
|
c = (Container*)cur->data;
|
|
|
|
|
|
|
|
|
|
|
|
if (c->_msg)
|
|
|
|
/* (i) if there is a message in the Container, the
|
|
|
|
* subject is the subject of that message. */
|
|
|
|
subject = mu_msg_get_subject (c->_msg);
|
|
|
|
else
|
|
|
|
/* (ii )If there is no message in the Container,
|
|
|
|
* then the Container will have at least one
|
|
|
|
* child Container, and that Container will
|
|
|
|
* have a message. Use the subject of that
|
|
|
|
* message instead. */
|
|
|
|
subject = mu_msg_get_subject (
|
|
|
|
((Container*)(c->_children->data))->_msg);
|
|
|
|
/* (iii) Strip ``Re:'', ``RE:'', ``RE[5]:'', ``Re:
|
|
|
|
* Re[4]: Re:'' and so on. */
|
|
|
|
subj = subject ? mu_str_subject_normalize (subject) : NULL;
|
2011-06-13 22:04:20 +02:00
|
|
|
|
2011-06-18 17:47:13 +02:00
|
|
|
/* (iv )If the subject is now "", give up on this
|
|
|
|
* Container. */
|
|
|
|
if (mu_str_is_empty (subj))
|
|
|
|
continue;
|
|
|
|
}
|
2011-06-13 22:04:20 +02:00
|
|
|
}
|
|
|
|
|
2011-06-18 17:47:13 +02:00
|
|
|
#endif
|
|
|
|
|
2011-06-23 23:21:54 +02:00
|
|
|
|
|
|
|
static gint
|
|
|
|
cmp_dates (Container *c1, Container *c2)
|
|
|
|
{
|
|
|
|
MuMsg *m1, *m2;
|
|
|
|
m1 = c1->_msg;
|
|
|
|
m2 = c2->_msg;
|
|
|
|
|
|
|
|
if (!m1)
|
|
|
|
return m2 ? 1 : 0;
|
|
|
|
if (!m2)
|
|
|
|
return m1 ? 0 : 1;
|
|
|
|
|
|
|
|
return mu_msg_get_date (m1) - mu_msg_get_date (m2);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sort_by_date (Container *container)
|
|
|
|
{
|
|
|
|
GSList *cur;
|
|
|
|
|
|
|
|
for (cur = container->_children; cur; cur = g_slist_next(cur)) {
|
|
|
|
Container *c;
|
|
|
|
c = (Container*)cur->data;
|
|
|
|
sort_by_date (c); /* recurse */
|
|
|
|
}
|
|
|
|
|
|
|
|
container->_children = g_slist_sort (container->_children,
|
|
|
|
(GCompareFunc)cmp_dates);
|
|
|
|
}
|
|
|
|
|
|
|
|
static MuMsgIterThreadInfo*
|
|
|
|
thread_info_new (gchar *threadpath, gboolean root,
|
|
|
|
gboolean first_child, gboolean empty_parent, gboolean is_dup)
|
|
|
|
{
|
|
|
|
MuMsgIterThreadInfo *ti;
|
|
|
|
|
|
|
|
ti = g_slice_new (MuMsgIterThreadInfo);
|
|
|
|
ti->threadpath = threadpath;
|
|
|
|
|
|
|
|
ti->prop = 0;
|
|
|
|
ti->prop |= root ? MU_MSG_ITER_THREAD_PROP_ROOT : 0;
|
|
|
|
ti->prop |= first_child ? MU_MSG_ITER_THREAD_PROP_FIRST_CHILD : 0;
|
|
|
|
ti->prop |= empty_parent ? MU_MSG_ITER_THREAD_PROP_EMPTY_PARENT : 0;
|
|
|
|
ti->prop |= is_dup ? MU_MSG_ITER_THREAD_PROP_DUP : 0;
|
|
|
|
|
|
|
|
if (is_dup)
|
|
|
|
g_print ("dup: %s\n", threadpath);
|
|
|
|
|
|
|
|
return ti;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
thread_info_destroy (MuMsgIterThreadInfo *ti)
|
|
|
|
{
|
|
|
|
if (ti) {
|
|
|
|
g_free (ti->threadpath);
|
|
|
|
g_slice_free (MuMsgIterThreadInfo, ti);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-18 17:47:13 +02:00
|
|
|
struct _ThreadInfo {
|
2011-06-23 23:21:54 +02:00
|
|
|
GHashTable *hash;
|
|
|
|
GQueue *idqueue;
|
|
|
|
unsigned prev_level;
|
|
|
|
const char* format;
|
2011-06-18 17:47:13 +02:00
|
|
|
};
|
2011-06-23 23:21:54 +02:00
|
|
|
typedef struct _ThreadInfo ThreadInfo;
|
|
|
|
|
|
|
|
|
|
|
|
struct _TP {
|
|
|
|
char *threadpath;
|
|
|
|
const char *frmt;
|
|
|
|
};
|
|
|
|
typedef struct _TP TP;
|
|
|
|
|
2011-06-18 17:47:13 +02:00
|
|
|
|
2011-06-19 20:02:41 +02:00
|
|
|
static void
|
2011-06-23 23:21:54 +02:00
|
|
|
accumulate_path (int seq, TP *tp)
|
2011-06-19 20:02:41 +02:00
|
|
|
{
|
2011-06-23 23:21:54 +02:00
|
|
|
char segm[16];
|
|
|
|
snprintf (segm, sizeof(segm), tp->frmt, seq);
|
|
|
|
|
|
|
|
if (tp->threadpath) {
|
2011-06-19 20:02:41 +02:00
|
|
|
char *path;
|
2011-06-23 23:21:54 +02:00
|
|
|
path = g_strdup_printf ("%s:%s", tp->threadpath, segm);
|
|
|
|
g_free (tp->threadpath);
|
|
|
|
tp->threadpath = path;
|
2011-06-19 20:02:41 +02:00
|
|
|
} else
|
2011-06-23 23:21:54 +02:00
|
|
|
tp->threadpath = g_strdup (segm);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
add_to_thread_info_hash (GHashTable *thread_info_hash, Container *c,
|
|
|
|
char *threadpath)
|
|
|
|
{
|
|
|
|
gboolean is_root, first_child, empty_parent;
|
|
|
|
|
|
|
|
/* 'root' means we're a child of the dummy root-container */
|
|
|
|
is_root = container_is_root (c);
|
|
|
|
|
|
|
|
first_child = is_root ? FALSE : (c->_parent->_children->data == c);
|
|
|
|
empty_parent = is_root ? FALSE : (!c->_parent->_msg);
|
|
|
|
|
|
|
|
g_hash_table_insert (thread_info_hash,
|
|
|
|
GUINT_TO_POINTER(c->_docid),
|
|
|
|
thread_info_new (threadpath,
|
|
|
|
container_is_root (c),
|
|
|
|
first_child,
|
|
|
|
empty_parent,
|
|
|
|
c->_dup));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* device a format string that is the minimum size to fit up to
|
|
|
|
* matchnum matches -- returns static memory */
|
|
|
|
const char*
|
|
|
|
thread_segment_format_string (size_t matchnum)
|
|
|
|
{
|
|
|
|
unsigned digitnum;
|
|
|
|
static char frmt[16];
|
|
|
|
|
|
|
|
/* get the number of digits needed in a hex-representation of
|
|
|
|
* matchnum */
|
|
|
|
digitnum = (unsigned) (ceil (log(matchnum)/log(16)));
|
|
|
|
snprintf (frmt, sizeof(frmt),"%%0%ux", digitnum);
|
|
|
|
|
|
|
|
return frmt;
|
2011-06-19 20:02:41 +02:00
|
|
|
}
|
|
|
|
|
2011-06-18 17:47:13 +02:00
|
|
|
static gboolean
|
2011-06-23 23:21:54 +02:00
|
|
|
add_thread_info (Container *c, guint level, ThreadInfo *ti)
|
2011-06-13 22:04:20 +02:00
|
|
|
{
|
2011-06-23 23:21:54 +02:00
|
|
|
TP tp;
|
2011-06-19 20:02:41 +02:00
|
|
|
unsigned i;
|
2011-06-23 23:21:54 +02:00
|
|
|
|
|
|
|
/* ignore our dummy root container */
|
|
|
|
if (!c->_parent)
|
|
|
|
return TRUE;
|
2011-06-19 20:02:41 +02:00
|
|
|
|
|
|
|
if (level > ti->prev_level) {
|
|
|
|
for (i = ti->prev_level; i != level; ++i)
|
|
|
|
g_queue_push_tail (ti->idqueue, GUINT_TO_POINTER(0));
|
|
|
|
} else if (level <= ti->prev_level) {
|
|
|
|
int oldseq;
|
2011-06-23 23:21:54 +02:00
|
|
|
for (i = level; i < ti->prev_level; ++i)
|
|
|
|
g_queue_pop_tail (ti->idqueue);
|
|
|
|
|
|
|
|
if (g_queue_is_empty (ti->idqueue))
|
|
|
|
g_queue_push_tail(ti->idqueue, GUINT_TO_POINTER(0));
|
|
|
|
else {
|
|
|
|
oldseq = GPOINTER_TO_UINT(g_queue_pop_tail(ti->idqueue));
|
|
|
|
g_queue_push_tail(ti->idqueue, GUINT_TO_POINTER(1 + oldseq));
|
|
|
|
}
|
2011-06-18 17:47:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ti->prev_level = level;
|
2011-06-23 23:21:54 +02:00
|
|
|
|
|
|
|
if (!c->_docid)
|
|
|
|
return TRUE; /* nothing more to do - it's a 'virtual'
|
|
|
|
* message */
|
|
|
|
|
|
|
|
tp.threadpath = NULL;
|
|
|
|
tp.frmt = ti->format;
|
|
|
|
|
|
|
|
g_queue_foreach (ti->idqueue, (GFunc)accumulate_path, &tp);
|
|
|
|
|
|
|
|
add_to_thread_info_hash (ti->hash, c, tp.threadpath);
|
2011-06-18 17:47:13 +02:00
|
|
|
|
|
|
|
return TRUE;
|
2011-06-13 22:04:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-18 17:47:13 +02:00
|
|
|
GHashTable*
|
2011-06-23 23:21:54 +02:00
|
|
|
create_doc_id_thread_path_hash (Container *root, size_t matchnum)
|
2011-06-13 22:04:20 +02:00
|
|
|
{
|
2011-06-18 17:47:13 +02:00
|
|
|
ThreadInfo ti;
|
|
|
|
|
|
|
|
/* create hash docid => thread-path */
|
|
|
|
ti.hash = g_hash_table_new_full (g_direct_hash, g_direct_equal,
|
|
|
|
NULL,
|
2011-06-23 23:21:54 +02:00
|
|
|
(GDestroyNotify)thread_info_destroy);
|
2011-06-19 20:02:41 +02:00
|
|
|
ti.idqueue = g_queue_new ();
|
|
|
|
|
|
|
|
ti.prev_level = 0;
|
2011-06-23 23:21:54 +02:00
|
|
|
ti.format = thread_segment_format_string (matchnum);
|
|
|
|
|
|
|
|
container_traverse (root, 0, (ContainerTraverseFunc)add_thread_info,
|
|
|
|
&ti);
|
2011-06-13 22:04:20 +02:00
|
|
|
|
2011-06-19 20:02:41 +02:00
|
|
|
g_queue_free (ti.idqueue);
|
2011-06-18 17:47:13 +02:00
|
|
|
return ti.hash;
|
|
|
|
}
|
2011-06-13 22:04:20 +02:00
|
|
|
|
2011-06-18 17:47:13 +02:00
|
|
|
|
2011-06-13 22:04:20 +02:00
|
|
|
static Container*
|
2011-06-18 17:47:13 +02:00
|
|
|
container_new (MuMsg *msg, unsigned docid)
|
2011-06-13 22:04:20 +02:00
|
|
|
{
|
|
|
|
Container *c;
|
|
|
|
|
2011-06-23 23:21:54 +02:00
|
|
|
c = g_slice_new (Container);
|
|
|
|
|
|
|
|
c->_msg = msg ? mu_msg_ref (msg) : NULL;
|
|
|
|
c->_docid = docid;
|
|
|
|
c->_dup = FALSE;
|
|
|
|
c->_children = NULL;
|
|
|
|
c->_parent = NULL;
|
|
|
|
|
2011-06-18 17:47:13 +02:00
|
|
|
|
2011-06-13 22:04:20 +02:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
container_destroy (Container *c)
|
|
|
|
{
|
|
|
|
if (!c)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (c->_msg)
|
|
|
|
mu_msg_unref (c->_msg);
|
|
|
|
|
2011-06-23 23:21:54 +02:00
|
|
|
c->_parent = (void*)0xdeadbeef;
|
2011-06-19 20:02:41 +02:00
|
|
|
g_slist_free (c->_children);
|
|
|
|
g_slice_free (Container, c);
|
2011-06-13 22:04:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* depth-first traverse all children, grand-children ... n-children of
|
|
|
|
* container */
|
|
|
|
static gboolean
|
2011-06-18 17:47:13 +02:00
|
|
|
container_traverse (Container *c, guint level, ContainerTraverseFunc func,
|
|
|
|
gpointer user_data)
|
2011-06-13 22:04:20 +02:00
|
|
|
{
|
2011-06-18 17:47:13 +02:00
|
|
|
GSList *cur;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
g_return_val_if_fail (c, FALSE);
|
2011-06-23 23:21:54 +02:00
|
|
|
|
2011-06-18 17:47:13 +02:00
|
|
|
if (!func (c, level, user_data))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
for (i = 0, cur = c->_children; cur; cur = g_slist_next (cur)) {
|
|
|
|
if (!container_traverse ((Container*)cur->data, level + 1,
|
|
|
|
func, user_data))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
}
|
2011-06-13 22:04:20 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2011-06-19 20:02:41 +02:00
|
|
|
static gboolean
|
|
|
|
container_add_child (Container *parent, Container *child)
|
2011-06-13 22:04:20 +02:00
|
|
|
{
|
2011-06-19 20:02:41 +02:00
|
|
|
g_return_val_if_fail (parent != child, FALSE);
|
|
|
|
g_return_val_if_fail (child, FALSE);
|
2011-06-23 23:21:54 +02:00
|
|
|
|
2011-06-19 20:02:41 +02:00
|
|
|
if (already_referenced(child, parent) ||
|
2011-06-23 23:21:54 +02:00
|
|
|
already_referenced(parent, child)) {
|
|
|
|
/* g_print ("already ref'd\n"); */
|
2011-06-19 20:02:41 +02:00
|
|
|
return FALSE;
|
2011-06-23 23:21:54 +02:00
|
|
|
}
|
|
|
|
|
2011-06-19 20:02:41 +02:00
|
|
|
child->_parent = parent;
|
|
|
|
parent->_children = g_slist_prepend (parent->_children,
|
|
|
|
child);
|
2011-06-23 23:21:54 +02:00
|
|
|
|
2011-06-19 20:02:41 +02:00
|
|
|
return TRUE;
|
2011-06-13 22:04:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2011-06-23 23:21:54 +02:00
|
|
|
container_splice_child (Container *c, Container *child)
|
2011-06-18 17:47:13 +02:00
|
|
|
{
|
|
|
|
GSList *iter;
|
|
|
|
|
|
|
|
g_return_if_fail (c != child);
|
|
|
|
g_return_if_fail (child);
|
2011-06-23 23:21:54 +02:00
|
|
|
g_return_if_fail (!child->_msg);
|
|
|
|
|
|
|
|
for (iter = child->_children; iter; iter = g_slist_next(iter))
|
|
|
|
((Container*)iter->data)->_parent = c; /* reparent
|
|
|
|
* grandchildren */
|
|
|
|
/*
|
|
|
|
* remove the old child
|
|
|
|
*/
|
|
|
|
c->_children = g_slist_remove (c->_children, child);
|
|
|
|
/*
|
|
|
|
* put child's children first, so we can use this while
|
|
|
|
* iterating over c->_children (we are already past the merged
|
|
|
|
* child's children)
|
|
|
|
*/
|
|
|
|
c->_children = g_slist_concat (child->_children, c->_children);
|
2011-06-18 17:47:13 +02:00
|
|
|
|
2011-06-19 20:02:41 +02:00
|
|
|
child->_children = NULL;
|
2011-06-23 23:21:54 +02:00
|
|
|
child->_parent = NULL;
|
2011-06-18 17:47:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
container_remove_child (Container *c, Container *child)
|
2011-06-13 22:04:20 +02:00
|
|
|
{
|
|
|
|
g_return_if_fail (c != child);
|
|
|
|
g_return_if_fail (child);
|
2011-06-23 23:21:54 +02:00
|
|
|
g_return_if_fail (!child->_children);
|
|
|
|
|
2011-06-19 20:02:41 +02:00
|
|
|
c->_children = g_slist_remove (c->_children, child);
|
2011-06-13 22:04:20 +02:00
|
|
|
}
|
2011-06-23 23:21:54 +02:00
|
|
|
|
2011-06-13 22:04:20 +02:00
|
|
|
|
2011-06-23 23:21:54 +02:00
|
|
|
static gboolean
|
|
|
|
container_is_root (Container *c)
|
|
|
|
{
|
|
|
|
return (!c->_parent || !c->_parent->_parent);
|
|
|
|
}
|
2011-06-18 17:47:13 +02:00
|
|
|
|
2011-06-19 20:02:41 +02:00
|
|
|
G_GNUC_UNUSED static void
|
2011-06-18 17:47:13 +02:00
|
|
|
container_dump (Container *c)
|
2011-06-23 23:21:54 +02:00
|
|
|
{
|
|
|
|
g_print ("[%s] { %p parent=%p msg=%p docid=%u [%s] children: %d }\n",
|
2011-06-18 17:47:13 +02:00
|
|
|
c->_msg ? mu_msg_get_subject(c->_msg) : "<empty>",
|
|
|
|
(void*)c,
|
|
|
|
(void*)c->_parent, (void*)c->_msg,
|
2011-06-23 23:21:54 +02:00
|
|
|
c->_docid,
|
2011-06-18 17:47:13 +02:00
|
|
|
c->_msg ? mu_msg_get_msgid(c->_msg) : "",
|
2011-06-23 23:21:54 +02:00
|
|
|
g_slist_length (c->_children));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
each_container (Container *c, guint level)
|
|
|
|
{
|
|
|
|
while (level--)
|
|
|
|
fputs (" ", stdout);
|
|
|
|
|
|
|
|
container_dump (c);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
G_GNUC_UNUSED static void
|
|
|
|
container_dump_tree (Container *c)
|
|
|
|
{
|
|
|
|
container_traverse (c, 0, (ContainerTraverseFunc)each_container, NULL);
|
2011-06-18 17:47:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|