2009-11-25 21:55:06 +01:00
|
|
|
/*
|
2010-01-01 14:56:45 +01:00
|
|
|
** Copyright (C) 2010 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
2009-11-25 21:55:06 +01:00
|
|
|
**
|
|
|
|
** 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 of the License, 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.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <glib.h>
|
2010-09-11 10:19:58 +02:00
|
|
|
#include <string.h>
|
2010-01-28 21:21:57 +01:00
|
|
|
|
2009-11-25 21:55:06 +01:00
|
|
|
#include "mu-msg-str.h"
|
|
|
|
#include "mu-msg-flags.h"
|
|
|
|
|
2010-09-11 10:19:58 +02:00
|
|
|
|
2009-11-25 21:55:06 +01:00
|
|
|
const char*
|
|
|
|
mu_msg_str_date_s (time_t t)
|
|
|
|
{
|
|
|
|
struct tm *tmbuf;
|
|
|
|
static char buf[64];
|
|
|
|
|
|
|
|
tmbuf = localtime(&t);
|
|
|
|
|
2010-01-28 21:21:57 +01:00
|
|
|
strftime (buf, sizeof(buf), "%c", tmbuf);
|
2009-11-25 21:55:06 +01:00
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
char*
|
|
|
|
mu_msg_str_date (time_t t)
|
|
|
|
{
|
|
|
|
return g_strdup (mu_msg_str_date_s(t));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char*
|
|
|
|
mu_msg_str_size_s (size_t s)
|
|
|
|
{
|
|
|
|
static char buf[32];
|
2010-08-15 12:32:43 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_GLIB216
|
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
tmp = g_format_size_for_display ((goffset)s);
|
|
|
|
strncpy (buf, tmp, sizeof(buf));
|
|
|
|
buf[sizeof(buf) -1] = '\0'; /* just in case */
|
|
|
|
g_free (tmp);
|
|
|
|
|
|
|
|
#else
|
2009-11-25 21:55:06 +01:00
|
|
|
if (s >= 1000 * 1000)
|
2010-01-28 21:21:57 +01:00
|
|
|
g_snprintf(buf, sizeof(buf), "%.1f MB", (double)s/(1000*1000));
|
2009-11-25 21:55:06 +01:00
|
|
|
else
|
2010-01-28 21:21:57 +01:00
|
|
|
g_snprintf(buf, sizeof(buf), "%.1f kB", (double)s/(1000));
|
2010-08-15 12:32:43 +02:00
|
|
|
#endif /*HAVE_GLIB216*/
|
2010-01-22 18:17:00 +01:00
|
|
|
|
2010-08-15 12:32:43 +02:00
|
|
|
|
2009-11-25 21:55:06 +01:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
char*
|
|
|
|
mu_msg_str_size (size_t s)
|
|
|
|
{
|
|
|
|
return g_strdup (mu_msg_str_size_s(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
const char*
|
|
|
|
mu_msg_str_flags_s (MuMsgFlags flags)
|
|
|
|
{
|
|
|
|
return mu_msg_flags_to_str_s (flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
char*
|
|
|
|
mu_msg_str_flags (MuMsgFlags flags)
|
|
|
|
{
|
|
|
|
return g_strdup (mu_msg_str_flags_s(flags));
|
|
|
|
}
|
|
|
|
|
|
|
|
const char*
|
2010-08-24 23:57:16 +02:00
|
|
|
mu_msg_str_prio (MuMsgPrio prio)
|
2009-11-25 21:55:06 +01:00
|
|
|
{
|
|
|
|
switch (prio) {
|
2009-12-08 23:03:53 +01:00
|
|
|
|
2010-08-24 23:57:16 +02:00
|
|
|
case MU_MSG_PRIO_LOW:
|
2009-12-08 23:03:53 +01:00
|
|
|
return "low";
|
|
|
|
|
2010-08-24 23:57:16 +02:00
|
|
|
case MU_MSG_PRIO_NONE:
|
|
|
|
case MU_MSG_PRIO_NORMAL:
|
2009-12-08 23:03:53 +01:00
|
|
|
return "normal";
|
|
|
|
|
2010-08-24 23:57:16 +02:00
|
|
|
case MU_MSG_PRIO_HIGH:
|
2009-12-08 23:03:53 +01:00
|
|
|
return "high";
|
|
|
|
|
2009-11-25 21:55:06 +01:00
|
|
|
default:
|
|
|
|
g_warning ("%s: invalid priority %d", __FUNCTION__, prio);
|
2010-01-28 21:21:57 +01:00
|
|
|
return NULL;
|
2009-11-25 21:55:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-11 10:19:58 +02:00
|
|
|
char*
|
|
|
|
mu_msg_str_summarize (const char* str, size_t max_lines)
|
|
|
|
{
|
|
|
|
char *summary;
|
|
|
|
size_t nl_seen;
|
|
|
|
unsigned i,j;
|
|
|
|
gboolean last_was_blank;
|
|
|
|
|
|
|
|
g_return_val_if_fail (str, NULL);
|
|
|
|
g_return_val_if_fail (max_lines > 0, NULL);
|
|
|
|
|
|
|
|
/* len for summary <= original len */
|
|
|
|
summary = g_new (gchar, strlen(str) + 1);
|
|
|
|
|
|
|
|
/* copy the string up to max_lines lines, replace CR/LF/tab with
|
|
|
|
* single space */
|
|
|
|
for (i = j = 0, nl_seen = 0, last_was_blank = TRUE;
|
|
|
|
nl_seen < max_lines && str[i] != '\0'; ++i) {
|
|
|
|
|
|
|
|
if (str[i] == '\n' || str[i] == '\r' ||
|
|
|
|
str[i] == '\t' || str[i] == ' ' ) {
|
|
|
|
|
|
|
|
if (str[i] == '\n')
|
|
|
|
++nl_seen;
|
|
|
|
|
|
|
|
/* no double-blanks or blank at end of str */
|
|
|
|
if (!last_was_blank && str[i+1] != '\0')
|
|
|
|
summary[j++] = ' ';
|
|
|
|
|
|
|
|
last_was_blank = TRUE;
|
|
|
|
} else {
|
|
|
|
|
|
|
|
summary[j++] = str[i];
|
|
|
|
last_was_blank = FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
summary[j] = '\0';
|
|
|
|
return summary;
|
|
|
|
}
|
|
|
|
|