* mu-cfind: some cleanups / better help

This commit is contained in:
djcb
2012-10-24 23:47:47 +03:00
parent 7f20ab33b9
commit a01196499f
8 changed files with 260 additions and 260 deletions

View File

@ -55,30 +55,6 @@ struct _MuContacts {
};
/*
* we use the e-mail address to create a key in the GKeyFile, but we
* have to mutilate a bit so that it's (a) *cough* practically-unique
* and (b) valid as a GKeyFile group name (ie., valid utf8, no control
* chars, no '[' or ']')
*/
static const char*
encode_email_address (const char *addr)
{
static char enc[254 + 1]; /* max size for an e-mail addr */
char *cur;
if (!addr)
return FALSE;
/* make sure chars are with {' ' .. '~'}, and not '[' ']' */
for (cur = strncpy(enc, addr, sizeof(enc)); *cur != '\0'; ++cur)
if (!isalnum(*cur)) {
*cur = 'A' + (*cur % ('Z' - 'A'));
} else
*cur = tolower(*cur);
return enc;
}
static GKeyFile*
load_key_file (const char *path)
@ -235,31 +211,53 @@ mu_contacts_clear (MuContacts *self)
}
/*
* we use the e-mail address to create a key in the GKeyFile, but we
* have to mutilate a bit so that it's (a) *cough* practically-unique
* and (b) valid as a GKeyFile group name (ie., valid utf8, no control
* chars, no '[' or ']')
*/
static const char*
encode_email_address (const char *addr)
{
static char enc[254 + 1]; /* max size for an e-mail addr */
char *cur;
if (!addr)
return FALSE;
/* make sure chars are with {' ' .. '~'}, and not '[' ']' */
for (cur = strncpy(enc, addr, sizeof(enc)); *cur != '\0'; ++cur)
if (!isalnum(*cur)) {
*cur = 'A' + (*cur % ('Z' - 'A'));
} else
*cur = tolower(*cur);
return enc;
}
gboolean
mu_contacts_add (MuContacts *self, const char *email, const char *name,
mu_contacts_add (MuContacts *self, const char *addr, const char *name,
gboolean personal, time_t tstamp)
{
ContactInfo *cinfo;
const char* group;
const char *group;
g_return_val_if_fail (self, FALSE);
g_return_val_if_fail (email, FALSE);
g_return_val_if_fail (addr, FALSE);
/* add the info, if either there is no info for this email
* yet, *OR* the new one is more recent and does not have an
* empty name */
group = encode_email_address (email);
group = encode_email_address (addr);
cinfo = (ContactInfo*) g_hash_table_lookup (self->_hash, group);
if (!cinfo || (cinfo->_tstamp < tstamp && !mu_str_is_empty(name))) {
ContactInfo *ci;
ci = contact_info_new (g_strdup(email),
ci = contact_info_new (g_strdup(addr),
name ? g_strdup(name) : NULL, personal,
tstamp);
g_hash_table_insert (self->_hash, g_strdup(group), ci);
return self->_dirty = TRUE;
}

View File

@ -625,102 +625,6 @@ mu_str_convert_to_utf8 (const char* buffer, const char *charset)
}
gchar*
mu_str_guess_last_name (const char *name)
{
const gchar *lastsp;
if (!name)
return g_strdup ("");
lastsp = g_strrstr (name, " ");
return g_strdup (lastsp ? lastsp + 1 : "");
}
gchar*
mu_str_guess_first_name (const char *name)
{
const gchar *lastsp;
if (!name)
return g_strdup ("");
lastsp = g_strrstr (name, " ");
if (lastsp)
return g_strndup (name, lastsp - name);
else
return g_strdup (name);
}
static gchar*
cleanup_str (const char* str)
{
gchar *s;
const gchar *cur;
unsigned i;
if (mu_str_is_empty(str))
return g_strdup ("");
s = g_new0 (char, strlen(str) + 1);
for (cur = str, i = 0; *cur; ++cur) {
if (ispunct(*cur) || isspace(*cur))
continue;
else
s[i++] = *cur;
}
return s;
}
gchar*
mu_str_guess_nick (const char* name)
{
gchar *fname, *lname, *nick;
gchar initial[7];
fname = mu_str_guess_first_name (name);
lname = mu_str_guess_last_name (name);
/* if there's no last name, use first name as the nick */
if (mu_str_is_empty(fname) || mu_str_is_empty(lname)) {
g_free (lname);
nick = fname;
goto leave;
}
memset (initial, 0, sizeof(initial));
/* couldn't we get an initial for the last name? */
if (g_unichar_to_utf8 (g_utf8_get_char (lname), initial) == 0) {
g_free (lname);
nick = fname;
goto leave;
}
nick = g_strdup_printf ("%s%s", fname, initial);
g_free (fname);
g_free (lname);
leave:
{
gchar *tmp;
tmp = cleanup_str (nick);
g_free (nick);
nick = tmp;
}
return nick;
}
gchar*
mu_str_quoted_from_strv (const gchar **params)
{

View File

@ -319,45 +319,6 @@ void mu_str_free_list (GSList *lst);
const gchar* mu_str_subject_normalize (const gchar* str);
/**
* guess some nick name for the given name; if we can determine an
* first name, last name, the nick will be first name + the first char
* of the last name. otherwise, it's just the first name. clearly,
* this is just a rough guess for setting an initial value for nicks.
*
* @param name a name
*
* @return the guessed nick, as a newly allocated string (free with g_free)
*/
gchar* mu_str_guess_nick (const char* name)
G_GNUC_WARN_UNUSED_RESULT;
/**
* guess the first name for the given name; clearly,
* this is just a rough guess for setting an initial value.
*
* @param name a name
*
* @return the first name, as a newly allocated string (free with
* g_free)
*/
gchar* mu_str_guess_first_name (const char* name)
G_GNUC_WARN_UNUSED_RESULT;
/**
* guess the last name for the given name; clearly,
* this is just a rough guess for setting an initial value.
*
* @param name a name
*
* @return the last name, as a newly allocated string (free with
* g_free)
*/
gchar* mu_str_guess_last_name (const char* name)
G_GNUC_WARN_UNUSED_RESULT;
/**
* take a list of strings, and return the concatenation of their
* quoted forms

View File

@ -368,77 +368,77 @@ test_mu_str_to_list_strip (void)
static void
test_mu_str_guess_first_name (void)
{
int i;
/* static void */
/* test_mu_str_guess_first_name (void) */
/* { */
/* int i; */
struct {
char *src, *exp;
} tests[] = {
{ "Richard M. Stallman", "Richard M." },
{ "John Rambo", "John" },
{ "Ivanhoe", "Ivanhoe" },
{ "", "" }
};
/* struct { */
/* char *src, *exp; */
/* } tests[] = { */
/* { "Richard M. Stallman", "Richard M." }, */
/* { "John Rambo", "John" }, */
/* { "Ivanhoe", "Ivanhoe" }, */
/* { "", "" } */
/* }; */
for (i = 0; i != G_N_ELEMENTS(tests); ++i) {
gchar *s;
/* for (i = 0; i != G_N_ELEMENTS(tests); ++i) { */
/* gchar *s; */
s = mu_str_guess_first_name (tests[i].src);
g_assert_cmpstr (s, ==, tests[i].exp);
g_free (s);
}
}
/* s = mu_str_guess_first_name (tests[i].src); */
/* g_assert_cmpstr (s, ==, tests[i].exp); */
/* g_free (s); */
/* } */
/* } */
static void
test_mu_str_guess_last_name (void)
{
int i;
/* static void */
/* test_mu_str_guess_last_name (void) */
/* { */
/* int i; */
struct {
char *src, *exp;
} tests[] = {
{ "Richard M. Stallman", "Stallman" },
{ "John Rambo", "Rambo" },
{ "Ivanhoe", "" },
{ "", "" }
};
/* struct { */
/* char *src, *exp; */
/* } tests[] = { */
/* { "Richard M. Stallman", "Stallman" }, */
/* { "John Rambo", "Rambo" }, */
/* { "Ivanhoe", "" }, */
/* { "", "" } */
/* }; */
for (i = 0; i != G_N_ELEMENTS(tests); ++i) {
gchar *s;
/* for (i = 0; i != G_N_ELEMENTS(tests); ++i) { */
/* gchar *s; */
s = mu_str_guess_last_name (tests[i].src);
g_assert_cmpstr (s, ==, tests[i].exp);
g_free (s);
}
}
/* s = mu_str_guess_last_name (tests[i].src); */
/* g_assert_cmpstr (s, ==, tests[i].exp); */
/* g_free (s); */
/* } */
/* } */
static void
test_mu_str_guess_nick (void)
{
int i;
/* static void */
/* test_mu_str_guess_nick (void) */
/* { */
/* int i; */
struct {
char *src, *exp;
} tests[] = {
{ "Richard M. Stallman", "RichardMS" },
{ "John Rambo", "JohnR" },
{ "Ivanhoe", "Ivanhoe" },
{ "", "" }
};
/* struct { */
/* char *src, *exp; */
/* } tests[] = { */
/* { "Richard M. Stallman", "RichardMS" }, */
/* { "John Rambo", "JohnR" }, */
/* { "Ivanhoe", "Ivanhoe" }, */
/* { "", "" } */
/* }; */
for (i = 0; i != G_N_ELEMENTS(tests); ++i) {
gchar *s;
/* for (i = 0; i != G_N_ELEMENTS(tests); ++i) { */
/* gchar *s; */
s = mu_str_guess_nick (tests[i].src);
g_assert_cmpstr (s, ==, tests[i].exp);
g_free (s);
}
}
/* s = mu_str_guess_nick (tests[i].src); */
/* g_assert_cmpstr (s, ==, tests[i].exp); */
/* g_free (s); */
/* } */
/* } */
@ -509,12 +509,12 @@ main (int argc, char *argv[])
g_test_add_func ("/mu-str/mu-str-esc-to-list",
test_mu_str_esc_to_list);
g_test_add_func ("/mu-str/mu_str_guess_first_name",
test_mu_str_guess_first_name);
g_test_add_func ("/mu-str/mu_str_guess_last_name",
test_mu_str_guess_last_name);
g_test_add_func ("/mu-str/mu_str_guess_nick",
test_mu_str_guess_nick);
/* g_test_add_func ("/mu-str/mu_str_guess_first_name", */
/* test_mu_str_guess_first_name); */
/* g_test_add_func ("/mu-str/mu_str_guess_last_name", */
/* test_mu_str_guess_last_name); */
/* g_test_add_func ("/mu-str/mu_str_guess_nick", */
/* test_mu_str_guess_nick); */
g_test_add_func ("/mu-str/mu_str_subject_normalize",
test_mu_str_subject_normalize);