utils/mu-date: remove

Remove mu-date.[ch] and convert its last users to use time_to_string instead.
This commit is contained in:
Dirk-Jan C. Binnema
2022-02-22 22:48:29 +02:00
parent a51272a2e6
commit af87cde217
9 changed files with 133 additions and 294 deletions

View File

@ -1,5 +1,5 @@
/*
** Copyright (C) 2012-2020 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
** Copyright (C) 2012-2022 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
@ -20,11 +20,12 @@
#include "config.h"
#include <string.h>
#include <utils/mu-utils.hh>
#include "gmime/gmime-signature.h"
#include "mu-msg.hh"
#include "mu-msg-priv.hh"
#include "mu-msg-part.hh"
#include "utils/mu-date.h"
#include <gmime/gmime.h>
#include <gmime/gmime-multipart-signed.h>
@ -96,12 +97,12 @@ get_cert_data(GMimeCertificate* cert)
}
return g_strdup_printf("signer:%s, key:%s (%s,%s), trust:%s",
name ? name : "?",
/* email ? email : "?", */
keyid,
pubkey_algo,
digest_algo,
trust);
name ? name : "?",
/* email ? email : "?", */
keyid,
pubkey_algo,
digest_algo,
trust);
}
static char*
@ -134,9 +135,9 @@ get_signature_status(GMimeSignatureStatus status)
continue;
g_string_append_printf(descr,
"%s%s",
descr->len > 0 ? ", " : "",
status_info[n].name);
"%s%s",
descr->len > 0 ? ", " : "",
status_info[n].name);
}
return g_string_free(descr, FALSE);
@ -146,26 +147,28 @@ get_signature_status(GMimeSignatureStatus status)
static char*
get_verdict_report(GMimeSignature* msig)
{
time_t t;
const char * created, *expires;
gchar * certdata, *report, *status;
GMimeSignatureStatus sigstat;
gchar * certdata, *report, *status;
GMimeSignatureStatus sigstat;
sigstat = g_mime_signature_get_status(msig);
status = get_signature_status(sigstat);
t = g_mime_signature_get_created(msig);
created = (t == 0 || t == (time_t)-1) ? "?" : mu_date_str_s("%x", t);
auto date_str = [](time_t t)->std::string {
if (t == 0 || t == static_cast<time_t>(-1))
return "?";
else
return time_to_string("%x", t);
};
t = g_mime_signature_get_expires(msig);
expires = (t == 0 || t == (time_t)-1) ? "?" : mu_date_str_s("%x", t);
const auto created = date_str(g_mime_signature_get_created(msig));
const auto expires = date_str(g_mime_signature_get_expires(msig));
certdata = get_cert_data(g_mime_signature_get_certificate(msig));
report = g_strdup_printf("%s; created:%s, expires:%s, %s",
status,
created,
expires,
certdata ? certdata : "?");
status,
created.c_str(),
expires.c_str(),
certdata ? certdata : "?");
g_free(certdata);
g_free(status);
@ -220,15 +223,15 @@ get_status_report(GMimeSignatureList* sigs)
status != MU_MSG_PART_SIG_STATUS_ERROR)
status = MU_MSG_PART_SIG_STATUS_ERROR;
else if ((sigstat & GMIME_SIGNATURE_STATUS_RED) &&
status == MU_MSG_PART_SIG_STATUS_GOOD)
status == MU_MSG_PART_SIG_STATUS_GOOD)
status = MU_MSG_PART_SIG_STATUS_BAD;
rep = get_verdict_report(msig);
report = g_strdup_printf("%s%s%d: %s",
report ? report : "",
report ? "; " : "",
i + 1,
rep);
report ? report : "",
report ? "; " : "",
i + 1,
rep);
g_free(rep);
cert = g_mime_signature_get_certificate(msig);
@ -263,9 +266,9 @@ static inline void
tag_with_sig_status(GObject* part, MuMsgPartSigStatusReport* report)
{
g_object_set_data_full(part,
SIG_STATUS_REPORT,
report,
(GDestroyNotify)mu_msg_part_sig_status_report_destroy);
SIG_STATUS_REPORT,
report,
(GDestroyNotify)mu_msg_part_sig_status_report_destroy);
}
void
@ -316,10 +319,10 @@ check_decrypt_result(GMimeMultipartEncrypted* part, GMimeDecryptResult* res, GEr
GMimeObject* /* this is declared in mu-msg-priv.h */
Mu::mu_msg_crypto_decrypt_part(GMimeMultipartEncrypted* enc,
MuMsgOptions opts,
MuMsgPartPasswordFunc func,
gpointer user_data,
GError** err)
MuMsgOptions opts,
MuMsgPartPasswordFunc func,
gpointer user_data,
GError** err)
{
GMimeObject* dec;
GMimeDecryptResult* res;

View File

@ -28,7 +28,6 @@
#include "utils/mu-util.h"
#include "utils/mu-str.h"
#include "utils/mu-date.h"
#include "utils/mu-utils.hh"
#include "utils/mu-xapian-utils.hh"

View File

@ -54,8 +54,6 @@ libmu_utils_la_SOURCES= \
mu-async-queue.hh \
mu-command-parser.cc \
mu-command-parser.hh \
mu-date.c \
mu-date.h \
mu-error.hh \
mu-logger.cc \
mu-logger.hh \

View File

@ -19,8 +19,6 @@ lib_mu_utils=static_library('mu-utils', [
'mu-async-queue.hh',
'mu-command-parser.cc',
'mu-command-parser.hh',
'mu-date.c',
'mu-date.h',
'mu-error.hh',
'mu-logger.cc',
'mu-logger.hh',

View File

@ -1,91 +0,0 @@
/*
** Copyright (C) 2012 <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.
**
*/
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "mu-util.h"
#include "mu-date.h"
#include "mu-str.h"
const char*
mu_date_str_s (const char* frm, time_t t)
{
struct tm *tmbuf;
static char buf[128];
static int is_utf8 = -1;
size_t len;
if (G_UNLIKELY(is_utf8 == -1))
is_utf8 = mu_util_locale_is_utf8 () ? 1 : 0;
g_return_val_if_fail (frm, NULL);
tmbuf = localtime(&t);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
len = strftime (buf, sizeof(buf) - 1, frm, tmbuf);
#pragma GCC diagnostic pop
if (len == 0)
return ""; /* not necessarily an error... */
if (!is_utf8) {
/* charset is _not_ utf8, so we need to convert it, so
* the date could contain locale-specific characters*/
gchar *conv;
GError *err;
err = NULL;
conv = g_locale_to_utf8 (buf, -1, NULL, NULL, &err);
if (err) {
g_warning ("conversion failed: %s", err->message);
g_error_free (err);
strcpy (buf, "<error>");
} else {
strncpy (buf, conv, sizeof(buf)-1);
buf[sizeof(buf)-1] = '\0';
}
g_free (conv);
}
return buf;
}
char*
mu_date_str (const char *frm, time_t t)
{
return g_strdup (mu_date_str_s(frm, t));
}
const char*
mu_date_display_s (time_t t)
{
time_t now;
static const time_t SECS_IN_DAY = 24 * 60 * 60;
now = time (NULL);
if (ABS(now - t) > SECS_IN_DAY)
return mu_date_str_s ("%x", t);
else
return mu_date_str_s ("%X", t);
}

View File

@ -1,67 +0,0 @@
/*
** Copyright (C) 2012-2013 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.
**
*/
#include <glib.h>
#ifndef __MU_DATE_H__
#define __MU_DATE_H__
G_BEGIN_DECLS
/**
* @addtogroup MuDate
* Date-related functions
* @{
*/
/**
* get a string for a given time_t
*
* mu_date_str_s returns a ptr to a static buffer,
* while mu_date_str returns dynamically allocated
* memory that must be freed after use.
*
* @param frm the format of the string (in strftime(3) format)
* @param t the time as time_t
*
* @return a string representation of the time; see above for what to
* do with it. Length is max. 128 bytes, inc. the ending \0. if the
* format is too long, the value will be truncated. in practice this
* should not happen.
*/
const char* mu_date_str_s (const char* frm, time_t t) G_GNUC_CONST;
char* mu_date_str (const char* frm, time_t t) G_GNUC_WARN_UNUSED_RESULT;
/**
* get a display string for a given time_t; if the given is less than
* 24h from the current time, we display the time, otherwise the date,
* using the preferred date/time for the current locale
*
* mu_str_display_date_s returns a ptr to a static buffer,
*
* @param t the time as time_t
*
* @return a string representation of the time/date
*/
const char* mu_date_display_s (time_t t);
G_END_DECLS
#endif /*__MU_DATE_H__*/