* mu-msg-flags: overhaul; cleaned up things, and should be a bit faster

This commit is contained in:
Dirk-Jan C. Binnema
2010-11-14 16:40:43 +02:00
parent fdc67a021b
commit ece60f2e82
2 changed files with 138 additions and 90 deletions

View File

@ -1,5 +1,5 @@
/* /*
** Copyright (C) 2010 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl> ** Copyright (C) 2008-2010 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
** **
** This program is free software; you can redistribute it and/or modify it ** 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 ** under the terms of the GNU General Public License as published by the
@ -24,84 +24,100 @@
#include "mu-msg-flags.h" #include "mu-msg-flags.h"
static struct { static const MuMsgFlags ALL_FLAGS[] = {
char kar; MU_MSG_FLAG_NEW,
MuMsgFlags flag; MU_MSG_FLAG_PASSED,
gboolean file_flag; MU_MSG_FLAG_REPLIED,
} FLAG_CHARS[] = { MU_MSG_FLAG_SEEN,
{'D', MU_MSG_FLAG_DRAFT, TRUE}, MU_MSG_FLAG_TRASHED,
{'F', MU_MSG_FLAG_FLAGGED, TRUE}, MU_MSG_FLAG_DRAFT,
{'N', MU_MSG_FLAG_NEW, TRUE}, MU_MSG_FLAG_FLAGGED,
{'P', MU_MSG_FLAG_PASSED, TRUE}, MU_MSG_FLAG_SIGNED,
{'R', MU_MSG_FLAG_REPLIED, TRUE}, MU_MSG_FLAG_ENCRYPTED,
{'S', MU_MSG_FLAG_SEEN, TRUE}, MU_MSG_FLAG_HAS_ATTACH
{'T', MU_MSG_FLAG_TRASHED, TRUE},
{'a', MU_MSG_FLAG_HAS_ATTACH, FALSE},
{'s', MU_MSG_FLAG_SIGNED, FALSE},
{'x', MU_MSG_FLAG_ENCRYPTED, FALSE}
}; };
MuMsgFlags MuMsgFlags
mu_msg_flags_from_str (const char* str) mu_msg_flag_from_char (char k)
{ {
MuMsgFlags flags = 0; switch (k) {
while (str[0]) { case 'N': return MU_MSG_FLAG_NEW;
int i;
MuMsgFlags oneflag = MU_MSG_FLAG_UNKNOWN;
for (i = 0; i != G_N_ELEMENTS(FLAG_CHARS); ++i) {
if (str[0] == FLAG_CHARS[i].kar) {
oneflag = FLAG_CHARS[i].flag;
break;
}
}
if (oneflag == MU_MSG_FLAG_UNKNOWN)
return MU_MSG_FLAG_UNKNOWN;
else
flags |= oneflag;
++str; case 'P': return MU_MSG_FLAG_PASSED;
} case 'R': return MU_MSG_FLAG_REPLIED;
case 'S': return MU_MSG_FLAG_SEEN;
case 'T': return MU_MSG_FLAG_TRASHED;
case 'D': return MU_MSG_FLAG_DRAFT;
case 'F': return MU_MSG_FLAG_FLAGGED;
return flags; case 'Z': return MU_MSG_FLAG_SIGNED;
case 'X': return MU_MSG_FLAG_ENCRYPTED;
case 'A': return MU_MSG_FLAG_HAS_ATTACH;
default:
g_warning ("%s: unknown flag %c", __FUNCTION__, k);
return MU_MSG_FLAG_NONE;
}
} }
MuMsgFlags char
mu_msg_flags_from_char (char c) mu_msg_flag_to_char (MuMsgFlags flag)
{ {
char str[2]; switch (flag) {
case MU_MSG_FLAG_NEW: return 'N';
case MU_MSG_FLAG_PASSED: return 'P';
case MU_MSG_FLAG_REPLIED: return 'R';
case MU_MSG_FLAG_SEEN: return 'S';
case MU_MSG_FLAG_TRASHED: return 'T';
case MU_MSG_FLAG_DRAFT: return 'D';
case MU_MSG_FLAG_FLAGGED: return 'F';
str[0] = c; case MU_MSG_FLAG_SIGNED: return 'Z';
str[1] = '\0'; case MU_MSG_FLAG_ENCRYPTED: return 'X';
case MU_MSG_FLAG_HAS_ATTACH: return 'A';
return mu_msg_flags_from_str (str); default:
g_warning ("%s: unknown flag 0x%x", __FUNCTION__, flag);
return 0;
}
} }
const char* const char*
mu_msg_flags_to_str_s (MuMsgFlags flags) mu_msg_flags_to_str_s (MuMsgFlags flags)
{ {
int i = 0, j = 0; int i, j;
static char buf[G_N_ELEMENTS(FLAG_CHARS) + 1]; static char buf[16]; /* more than enough */
for (i = 0; i != G_N_ELEMENTS(FLAG_CHARS); ++i) for (i = j = 0; i != G_N_ELEMENTS(ALL_FLAGS); ++i) {
if (flags & FLAG_CHARS[i].flag) if (flags & ALL_FLAGS[i]) {
buf[j++] = FLAG_CHARS[i].kar; char k;
if ((k = mu_msg_flag_to_char (ALL_FLAGS[i])) == 0)
return NULL;
buf[j++] = k;
}
}
buf[j] = '\0'; buf[j] = '\0';
return buf; return buf;
} }
MuMsgFlags
gboolean mu_msg_flags_from_str (const char* str)
mu_msg_flags_is_file_flag (MuMsgFlags flag)
{ {
int i = 0; MuMsgFlags flags;
for (i = 0; i != G_N_ELEMENTS(FLAG_CHARS); ++i) for (flags = MU_MSG_FLAG_NONE; str && *str; ++str) {
if (flag == FLAG_CHARS[i].flag) MuMsgFlags flag;
return FLAG_CHARS[i].file_flag; if ((flag = mu_msg_flag_to_char (*str)) == 0) {
flags = 0;
break;
}
flags |= flag;
}
return FALSE; return flags;
} }
@ -166,9 +182,9 @@ mu_msg_flags_from_file (const char* path)
MsgType mtype; MsgType mtype;
char *info = NULL; char *info = NULL;
g_return_val_if_fail (path, MU_MSG_FLAG_UNKNOWN); g_return_val_if_fail (path, MU_MSG_FLAG_NONE);
g_return_val_if_fail (!g_str_has_suffix(path,G_DIR_SEPARATOR_S), g_return_val_if_fail (!g_str_has_suffix(path,G_DIR_SEPARATOR_S),
MU_MSG_FLAG_UNKNOWN); MU_MSG_FLAG_NONE);
mtype = check_msg_type (path, &info); mtype = check_msg_type (path, &info);
@ -185,16 +201,19 @@ mu_msg_flags_from_file (const char* path)
if (cursor && cursor[0]=='2' && cursor[1]==',') { if (cursor && cursor[0]=='2' && cursor[1]==',') {
cursor += 2; /* jump past 2, */ cursor += 2; /* jump past 2, */
while (*cursor) { while (*cursor) {
MuMsgFlags oneflag = switch (*cursor) {
mu_msg_flags_from_char (*cursor); case 'P': flags |= MU_MSG_FLAG_PASSED; break;
/* ignore anything but file flags */ case 'T': flags |= MU_MSG_FLAG_TRASHED; break;
if (mu_msg_flags_is_file_flag(oneflag)) case 'R': flags |= MU_MSG_FLAG_REPLIED; break;
flags |= oneflag; case 'S': flags |= MU_MSG_FLAG_SEEN; break;
case 'D': flags |= MU_MSG_FLAG_DRAFT; break;
case 'F': flags |= MU_MSG_FLAG_FLAGGED; break;
}
++cursor; ++cursor;
} }
} }
} }
g_free (info); g_free (info);
return flags; return flags;
} }

View File

@ -1,5 +1,5 @@
/* /*
** Copyright (C) 2010 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl> ** Copyright (C) 2008-2010 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
** **
** This program is free software; you can redistribute it and/or modify it ** 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 ** under the terms of the GNU General Public License as published by the
@ -24,31 +24,50 @@
G_BEGIN_DECLS G_BEGIN_DECLS
/* the MU_MSG_FLAG_-flags are retrieved from the filename of
* the message, as per the maildir spec:
* http://cr.yp.to/proto/maildir.html not all (possibly not any) mail
* clients respect this fully -- therefore, we map these flags to
* MU_MSG_FLAG values.
*
* the other flags are determined from the contents of the
* message. Note that these flags are far from orthogonal,
* unfortunately.
*
*/
enum _MuMsgFlags { enum _MuMsgFlags {
MU_MSG_FLAG_UNKNOWN = 0, MU_MSG_FLAG_NONE = 0,
MU_MSG_FLAG_NONE = 1 << 0,
/* these we get from the file */ /* unlike the other MAILDIR flags, the NEW flag is not
MU_MSG_FLAG_NEW = 1 << 1, * determined by the file name but by the dir: if the message
MU_MSG_FLAG_SEEN = 1 << 2, * is in the 'new' directory, it's considered... new */
MU_MSG_FLAG_UNREAD = 1 << 3, MU_MSG_FLAG_NEW = 1 << 0,
MU_MSG_FLAG_REPLIED = 1 << 4,
MU_MSG_FLAG_FLAGGED = 1 << 5, /* "P"->resent,forwarded,bounced message */
MU_MSG_FLAG_TRASHED = 1 << 6, MU_MSG_FLAG_PASSED = 1 << 1,
MU_MSG_FLAG_DRAFT = 1 << 7, /* "R"->replied message */
MU_MSG_FLAG_PASSED = 1 << 8, MU_MSG_FLAG_REPLIED = 1 << 2,
/* "S"->seen message */
MU_MSG_FLAG_SEEN = 1 << 3,
/* "T"->trashed message */
MU_MSG_FLAG_TRASHED = 1 << 4,
/* "D"->draft message */
MU_MSG_FLAG_DRAFT = 1 << 5,
/* "F"->flagged message */
MU_MSG_FLAG_FLAGGED = 1 << 6,
/* these we get from the contents */ /* these we get from the contents */
MU_MSG_FLAG_SIGNED = 1 << 10,
MU_MSG_FLAG_ENCRYPTED = 1 << 11,
MU_MSG_FLAG_HAS_ATTACH = 1 << 12
/* any new fields go here */ /* "Z"->signed message */
/* so the existing numbers stay valid note that we're also */ MU_MSG_FLAG_SIGNED = 1 << 7,
/* using these numbers in the database, so they should not change */ /* "X"->encrypted message */
MU_MSG_FLAG_ENCRYPTED = 1 << 8,
/* "A"->message has attachment */
MU_MSG_FLAG_HAS_ATTACH = 1 << 9
}; };
typedef enum _MuMsgFlags MuMsgFlags; typedef enum _MuMsgFlags MuMsgFlags;
/** /**
* convert the char-per-flag description into a MuMsgFlags value; the characters * convert the char-per-flag description into a MuMsgFlags value; the characters
* D=draft,F=flagged,N=new,P=passed,R=replied,S=seen,T=trashed * D=draft,F=flagged,N=new,P=passed,R=replied,S=seen,T=trashed
@ -85,6 +104,16 @@ MuMsgFlags mu_msg_flags_from_char (char c) G_GNUC_CONST;
*/ */
const char* mu_msg_flags_to_str_s (MuMsgFlags flags) G_GNUC_CONST; const char* mu_msg_flags_to_str_s (MuMsgFlags flags) G_GNUC_CONST;
/**
* get the character for some msg flag, or 0 in case it's not found
*
* @param flags on specific flag (not OR'ed)
*
* @return the char for this flag, or 0 if not found
*/
char mu_msg_flags_char (MuMsgFlags flag);
/** /**
* get the Maildir flags from a mailfile. The flags are as specified * get the Maildir flags from a mailfile. The flags are as specified
* in http://cr.yp.to/proto/maildir.html, plus MU_MSG_FLAG_NEW for * in http://cr.yp.to/proto/maildir.html, plus MU_MSG_FLAG_NEW for