* WIP: contact listing / searching.

this is the first part, adding a cache for all the contacts in mails
  (~/.mu/cache/contacts), which is a .ini-type file.
This commit is contained in:
Dirk-Jan C. Binnema 2011-03-02 00:13:24 +02:00
parent bbbba9157b
commit deeaf41f33
11 changed files with 134 additions and 61 deletions

3
TODO
View File

@ -14,6 +14,9 @@
- [ ] better 'usage' info - [ ] better 'usage' info
** release 0.9.4
- [ ] add 'mu cfind' to find contacts
* Releases already done * Releases already done
** release 0.9.3 [100%] ** release 0.9.3 [100%]

View File

@ -55,6 +55,8 @@ libmu_la_SOURCES= \
mu-cmd.h \ mu-cmd.h \
mu-config.c \ mu-config.c \
mu-config.h \ mu-config.h \
mu-contacts.h \
mu-contacts.c \
mu-index.c \ mu-index.c \
mu-index.h \ mu-index.h \
mu-log.c \ mu-log.c \

View File

@ -351,7 +351,9 @@ cmd_index_or_cleanup (MuConfig *opts)
return MU_EXITCODE_ERROR; return MU_EXITCODE_ERROR;
err = NULL; err = NULL;
if (!(midx = mu_index_new (mu_runtime_xapian_dir(), &err))) if (!(midx = mu_index_new (mu_runtime_xapian_dir(),
mu_runtime_contacts_cache_file(),
&err)))
return handle_index_error_and_free (err); return handle_index_error_and_free (err);
mu_index_set_max_msg_size (midx, opts->max_msg_size); mu_index_set_max_msg_size (midx, opts->max_msg_size);

View File

@ -45,7 +45,7 @@ struct _MuIndex {
}; };
MuIndex* MuIndex*
mu_index_new (const char *xpath, GError **err) mu_index_new (const char *xpath, const char* contacts_cache, GError **err)
{ {
MuIndex *index; MuIndex *index;
@ -53,7 +53,7 @@ mu_index_new (const char *xpath, GError **err)
index = g_new0 (MuIndex, 1); index = g_new0 (MuIndex, 1);
index->_store = mu_store_new (xpath, err); index->_store = mu_store_new (xpath, contacts_cache, err);
if (!index->_store) { if (!index->_store) {
g_warning ("%s: failed to open xapian store (%s)", g_warning ("%s: failed to open xapian store (%s)",
__FUNCTION__, xpath); __FUNCTION__, xpath);

View File

@ -46,12 +46,14 @@ typedef struct _MuIndexStats MuIndexStats;
* *
* @param xpath path to the 'homedir'; the xapian directory will be * @param xpath path to the 'homedir'; the xapian directory will be
* this homedir/xapian * this homedir/xapian
* @param contacts_cache file to store the cache of contacts, or NULL
* @param err to receive error or NULL; there are only errors when this * @param err to receive error or NULL; there are only errors when this
* function returns NULL. Possible errors: see mu-error.h * function returns NULL. Possible errors: see mu-error.h
* *
* @return a new MuIndex instance, or NULL in case of error * @return a new MuIndex instance, or NULL in case of error
*/ */
MuIndex* mu_index_new (const char* muhome, GError **err) MuIndex* mu_index_new (const char* muhome, const char* contacts_cache,
GError **err)
G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT; G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT;

View File

@ -32,14 +32,23 @@
#include "mu-log.h" #include "mu-log.h"
#include "mu-util.h" #include "mu-util.h"
#define MU_XAPIAN_DIRNAME "xapian" enum {
#define MU_BOOKMARKS_FILENAME "bookmarks" MU_RUNTIME_STR_MU_HOMEPATH,
MU_RUNTIME_STR_XAPIAN_PATH,
MU_RUNTIME_STR_BOOKMARKS_PATH,
MU_RUNTIME_STR_CACHE_PATH,
MU_RUNTIME_STR_CONTACTS_PATH,
MU_RUNTIME_STR_NUM
};
#define MU_XAPIAN_DIRNAME "xapian"
#define MU_BOOKMARKS_FILENAME "bookmarks"
#define MU_CACHE_DIRNAME "cache"
#define MU_CONTACTS_FILENAME "contacts"
struct _MuRuntimeData { struct _MuRuntimeData {
gchar *_muhome; gchar *_str[MU_RUNTIME_STR_NUM];
gchar *_xapian_dir; MuConfig *_config;
gchar *_bookmarks_file;
MuConfig *_config;
}; };
typedef struct _MuRuntimeData MuRuntimeData; typedef struct _MuRuntimeData MuRuntimeData;
@ -50,7 +59,7 @@ static MuRuntimeData *_data = NULL;
static void runtime_free (void); static void runtime_free (void);
static gboolean static gboolean
mu_dir_is_readable_and_writable (const char* muhome) mu_dir_is_readable_and_writable (const char *muhome)
{ {
if (mu_util_create_dir_maybe (muhome, 0700)) if (mu_util_create_dir_maybe (muhome, 0700))
return TRUE; return TRUE;
@ -61,6 +70,39 @@ mu_dir_is_readable_and_writable (const char* muhome)
return FALSE; return FALSE;
} }
static gboolean
init_paths (const char* muhome, MuRuntimeData *data)
{
data->_str [MU_RUNTIME_STR_XAPIAN_PATH] =
g_strdup_printf ("%s%c%s", muhome,
G_DIR_SEPARATOR,
MU_XAPIAN_DIRNAME);
data->_str [MU_RUNTIME_STR_BOOKMARKS_PATH] =
g_strdup_printf ("%s%c%s", muhome,
G_DIR_SEPARATOR,
MU_BOOKMARKS_FILENAME);
data->_str [MU_RUNTIME_STR_CACHE_PATH] =
g_strdup_printf ("%s%c%s", muhome,
G_DIR_SEPARATOR,
MU_CACHE_DIRNAME);
if (!mu_util_create_dir_maybe
(_data->_str[MU_RUNTIME_STR_CACHE_PATH], 0700)) {
g_warning ("failed to create cache dir");
return FALSE;
}
data->_str [MU_RUNTIME_STR_CONTACTS_PATH] =
g_strdup_printf ("%s%c%s",
data->_str[MU_RUNTIME_STR_CACHE_PATH],
G_DIR_SEPARATOR,
MU_CONTACTS_FILENAME);
return TRUE;
}
gboolean gboolean
mu_runtime_init (const char* muhome_arg) mu_runtime_init (const char* muhome_arg)
{ {
@ -87,8 +129,9 @@ mu_runtime_init (const char* muhome_arg)
} }
_data = g_new0 (MuRuntimeData, 1); _data = g_new0 (MuRuntimeData, 1);
_data->_muhome = muhome; _data->_str[MU_RUNTIME_STR_MU_HOMEPATH] = muhome;
init_paths (muhome, _data);
mu_msg_gmime_init (); mu_msg_gmime_init ();
return _initialized = TRUE; return _initialized = TRUE;
@ -131,8 +174,10 @@ mu_runtime_init_from_cmdline (int *pargc, char ***pargv)
return FALSE; return FALSE;
} }
_data->_muhome = g_strdup (_data->_config->muhome); _data->_str[MU_RUNTIME_STR_MU_HOMEPATH] =
g_strdup (_data->_config->muhome);
init_paths (_data->_str[MU_RUNTIME_STR_MU_HOMEPATH], _data);
mu_msg_gmime_init (); mu_msg_gmime_init ();
return _initialized = TRUE; return _initialized = TRUE;
@ -142,9 +187,10 @@ mu_runtime_init_from_cmdline (int *pargc, char ***pargv)
static void static void
runtime_free (void) runtime_free (void)
{ {
g_free (_data->_xapian_dir); int i;
g_free (_data->_muhome);
g_free (_data->_bookmarks_file); for (i = 0; i != MU_RUNTIME_STR_NUM; ++i)
g_free (_data->_str[i]);
mu_config_destroy (_data->_config); mu_config_destroy (_data->_config);
@ -158,8 +204,7 @@ mu_runtime_uninit (void)
{ {
g_return_if_fail (_initialized); g_return_if_fail (_initialized);
mu_msg_gmime_uninit (); mu_msg_gmime_uninit ();
runtime_free (); runtime_free ();
_initialized = FALSE; _initialized = FALSE;
@ -170,43 +215,37 @@ const char*
mu_runtime_mu_home_dir (void) mu_runtime_mu_home_dir (void)
{ {
g_return_val_if_fail (_initialized, NULL); g_return_val_if_fail (_initialized, NULL);
return _data->_str[MU_RUNTIME_STR_MU_HOMEPATH];
return _data->_muhome;
} }
const char* const char*
mu_runtime_xapian_dir (void) mu_runtime_xapian_dir (void)
{ {
g_return_val_if_fail (_initialized, NULL); g_return_val_if_fail (_initialized, NULL);
return _data->_str[MU_RUNTIME_STR_XAPIAN_PATH];
if (!_data->_xapian_dir)
_data->_xapian_dir = g_strdup_printf ("%s%c%s",
_data->_muhome,
G_DIR_SEPARATOR,
MU_XAPIAN_DIRNAME);
return _data->_xapian_dir;
} }
const char* const char*
mu_runtime_bookmarks_file (void) mu_runtime_bookmarks_file (void)
{ {
g_return_val_if_fail (_initialized, NULL); g_return_val_if_fail (_initialized, NULL);
return _data->_str[MU_RUNTIME_STR_BOOKMARKS_PATH];
if (!_data->_bookmarks_file)
_data->_bookmarks_file =
g_strdup_printf ("%s%c%s",
_data->_muhome,
G_DIR_SEPARATOR,
MU_BOOKMARKS_FILENAME);
return _data->_bookmarks_file;
} }
const char*
mu_runtime_contacts_cache_file (void)
{
g_return_val_if_fail (_initialized, NULL);
return _data->_str[MU_RUNTIME_STR_CONTACTS_PATH];
}
MuConfig* MuConfig*
mu_runtime_config (void) mu_runtime_config (void)
{ {
g_return_val_if_fail (_initialized, NULL); g_return_val_if_fail (_initialized, NULL);
return _data->_config; return _data->_config;
} }

View File

@ -83,6 +83,16 @@ const char* mu_runtime_xapian_dir (void);
*/ */
const char* mu_runtime_bookmarks_file (void); const char* mu_runtime_bookmarks_file (void);
/**
* get the mu contacts cache file name (typically,
* ~/.mu/contacts.cache); this can only be called after
* mu_runtime_init and before mu_runtime_uninit
*
* @return the contacts cache file name as a string which should be not be
* modified, or NULL in case of error.
*/
const char* mu_runtime_contacts_cache_file (void);
/** /**
* get the mu configuration options (ie., the parsed command line * get the mu configuration options (ie., the parsed command line

View File

@ -31,7 +31,7 @@
#include "mu-util.h" #include "mu-util.h"
#include "mu-str.h" #include "mu-str.h"
#include "mu-msg-flags.h" #include "mu-msg-flags.h"
#include "mu-contacts.h"
/* by default, use transactions of 30000 messages */ /* by default, use transactions of 30000 messages */
#define MU_STORE_DEFAULT_TRX_SIZE 30000 #define MU_STORE_DEFAULT_TRX_SIZE 30000
@ -42,6 +42,9 @@
struct _MuStore { struct _MuStore {
Xapian::WritableDatabase *_db; Xapian::WritableDatabase *_db;
/* contacts object to cache all the contact information */
MuContacts *_contacts;
char *_version; char *_version;
/* transaction handling */ /* transaction handling */
@ -119,12 +122,12 @@ check_version (MuStore *store)
} }
MuStore* MuStore*
mu_store_new (const char* xpath, GError **err) mu_store_new (const char* xpath, const char *contacts_cache,
GError **err)
{ {
MuStore *store (0); MuStore *store (0);
g_return_val_if_fail (xpath, NULL); g_return_val_if_fail (xpath, NULL);
try { try {
store = g_new0(MuStore,1); store = g_new0(MuStore,1);
@ -136,6 +139,12 @@ mu_store_new (const char* xpath, GError **err)
return NULL; return NULL;
} }
if (contacts_cache) {
store->_contacts = mu_contacts_new (contacts_cache);
if (!store->_contacts) /* don't bail-out for this */
g_warning ("failed to init contacts cache");
}
/* keep count of processed docs */ /* keep count of processed docs */
store->_in_transaction = false; store->_in_transaction = false;
store->_processed = 0; store->_processed = 0;
@ -169,6 +178,8 @@ mu_store_destroy (MuStore *store)
MU_WRITE_LOG ("closing xapian database with %d documents", MU_WRITE_LOG ("closing xapian database with %d documents",
(int)store->_db->get_doccount()); (int)store->_db->get_doccount());
mu_contacts_destroy (store->_contacts);
g_free (store->_version); g_free (store->_version);
delete store->_db; delete store->_db;
g_free (store); g_free (store);
@ -403,6 +414,7 @@ add_terms_values_body (Xapian::Document& doc, MuMsg *msg,
struct _MsgDoc { struct _MsgDoc {
Xapian::Document *_doc; Xapian::Document *_doc;
MuMsg *_msg; MuMsg *_msg;
MuStore *_store;
}; };
typedef struct _MsgDoc MsgDoc; typedef struct _MsgDoc MsgDoc;
@ -438,10 +450,9 @@ add_terms_values (MuMsgFieldId mfid, MsgDoc* msgdoc)
static void static void
each_contact_info (MuMsgContact *contact, MsgDoc *data) each_contact_info (MuMsgContact *contact, MsgDoc *msgdoc)
{ {
const std::string *pfxp; const std::string *pfxp;
static const std::string to_pfx (1, static const std::string to_pfx (1,
mu_msg_field_xapian_prefix(MU_MSG_FIELD_ID_TO)); mu_msg_field_xapian_prefix(MU_MSG_FIELD_ID_TO));
static const std::string from_pfx (1, static const std::string from_pfx (1,
@ -454,13 +465,12 @@ each_contact_info (MuMsgContact *contact, MsgDoc *data)
case MU_MSG_CONTACT_TYPE_TO: pfxp = &to_pfx; break; case MU_MSG_CONTACT_TYPE_TO: pfxp = &to_pfx; break;
case MU_MSG_CONTACT_TYPE_FROM: pfxp = &from_pfx; break; case MU_MSG_CONTACT_TYPE_FROM: pfxp = &from_pfx; break;
case MU_MSG_CONTACT_TYPE_CC: pfxp = &cc_pfx; break; case MU_MSG_CONTACT_TYPE_CC: pfxp = &cc_pfx; break;
default: return; default: return; /* other types (like bcc) are ignored */
/* other types (like bcc) are ignored */
} }
if (contact->name && strlen(contact->name) > 0) { if (contact->name && strlen(contact->name) > 0) {
Xapian::TermGenerator termgen; Xapian::TermGenerator termgen;
termgen.set_document (*data->_doc); termgen.set_document (*msgdoc->_doc);
char *norm = mu_str_normalize (contact->name, TRUE); char *norm = mu_str_normalize (contact->name, TRUE);
termgen.index_text_without_positions (norm, 1, *pfxp); termgen.index_text_without_positions (norm, 1, *pfxp);
g_free (norm); g_free (norm);
@ -468,11 +478,16 @@ each_contact_info (MuMsgContact *contact, MsgDoc *data)
/* don't normalize e-mail address, but do lowercase it */ /* don't normalize e-mail address, but do lowercase it */
if (contact->address && contact->address[0] != '\0') { if (contact->address && contact->address[0] != '\0') {
char *escaped = char *escaped = mu_str_ascii_xapian_escape (contact->address);
mu_str_ascii_xapian_escape (contact->address); msgdoc->_doc->add_term
data->_doc->add_term
(std::string (*pfxp + escaped, 0, MU_STORE_MAX_TERM_LENGTH)); (std::string (*pfxp + escaped, 0, MU_STORE_MAX_TERM_LENGTH));
g_free (escaped); g_free (escaped);
/* store it also in our contacts cache */
if (msgdoc->_store->_contacts)
mu_contacts_add (msgdoc->_store->_contacts,
contact->name, contact->address,
mu_msg_get_date(msgdoc->_msg));
} }
} }
@ -502,9 +517,9 @@ mu_store_store (MuStore *store, MuMsg *msg)
try { try {
Xapian::Document newdoc; Xapian::Document newdoc;
Xapian::docid id; Xapian::docid id;
MsgDoc msgdoc = { &newdoc, msg }; MsgDoc msgdoc = { &newdoc, msg, store };
const std::string uid(get_message_uid(msg)); const std::string uid(get_message_uid(msg));
begin_trx_if (store, !store->_in_transaction); begin_trx_if (store, !store->_in_transaction);
/* we must add a unique term, so we can replace /* we must add a unique term, so we can replace
* matching documents */ * matching documents */

View File

@ -35,14 +35,14 @@ typedef struct _MuStore MuStore;
* create a new Xapian store, a place to store documents * create a new Xapian store, a place to store documents
* *
* @param path the path to the database * @param path the path to the database
* @param batchsize size of batch before committing * @param ccachepath path where to cache the contacts information, or NULL
* @param err to receive error info or NULL. err->code can be found in * @param err to receive error info or NULL. err->code can be found in
* mu-error.h * mu-error.h
* *
* @return a new MuStore object, or NULL in case of error * @return a new MuStore object, or NULL in case of error
*/ */
MuStore* mu_store_new (const char *path, GError **err) MuStore* mu_store_new (const char *xpath, const char *ccachepath,
G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT; GError **err) G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT;
/** /**

View File

@ -110,7 +110,7 @@ test_mu_index (void)
xpath = g_strdup_printf ("%s%c%s", muhome, G_DIR_SEPARATOR, "xapian"); xpath = g_strdup_printf ("%s%c%s", muhome, G_DIR_SEPARATOR, "xapian");
store = mu_store_new (xpath, NULL); store = mu_store_new (xpath, NULL, NULL);
g_assert (store); g_assert (store);
g_assert_cmpuint (mu_store_count (store), ==, 5); g_assert_cmpuint (mu_store_count (store), ==, 5);

View File

@ -42,7 +42,7 @@ test_mu_store_new_destroy (void)
g_assert (tmpdir); g_assert (tmpdir);
err = NULL; err = NULL;
store = mu_store_new (tmpdir, &err); store = mu_store_new (tmpdir, NULL, &err);
g_assert (store); g_assert (store);
g_assert (err == NULL); g_assert (err == NULL);
@ -67,7 +67,7 @@ test_mu_store_version (void)
g_assert (tmpdir); g_assert (tmpdir);
err = NULL; err = NULL;
store = mu_store_new (tmpdir, &err); store = mu_store_new (tmpdir, NULL, &err);
g_assert (store); g_assert (store);
g_assert (err == NULL); g_assert (err == NULL);
@ -90,7 +90,7 @@ test_mu_store_store_and_count (void)
tmpdir = test_mu_common_get_random_tmpdir(); tmpdir = test_mu_common_get_random_tmpdir();
g_assert (tmpdir); g_assert (tmpdir);
store = mu_store_new (tmpdir, NULL); store = mu_store_new (tmpdir, NULL, NULL);
g_assert (store); g_assert (store);
g_assert_cmpuint (0,==,mu_store_count (store)); g_assert_cmpuint (0,==,mu_store_count (store));
@ -138,7 +138,7 @@ test_mu_store_store_remove_and_count (void)
tmpdir = test_mu_common_get_random_tmpdir(); tmpdir = test_mu_common_get_random_tmpdir();
g_assert (tmpdir); g_assert (tmpdir);
store = mu_store_new (tmpdir, NULL); store = mu_store_new (tmpdir, NULL, NULL);
g_assert (store); g_assert (store);
g_assert_cmpuint (0,==,mu_store_count (store)); g_assert_cmpuint (0,==,mu_store_count (store));