Commit 799ea98a authored by Rolf Eike Beer's avatar Rolf Eike Beer Committed by Rolf Eike Beer

introduce Convert::toDateTime() to parse time specifications as returned from GnuPG

GnuPG will eventually switch to a ISO 8601 format. Be prepared to handle this,
as well as the empty strings that can already happen, e.g. if a key has no
expiration set.
parent 9ebc447e
/*
* Copyright (C) 2006 Jimmy Gilles <jimmygilles@gmail.com>
* Copyright (C) 2010,2013,2014 Rolf Eike Beer <kde@opensource.sf-tec.de>
* Copyright (C) 2010,2013,2014,2016 Rolf Eike Beer <kde@opensource.sf-tec.de>
*/
/***************************************************************************
* This program is free software; you can redistribute it and/or modify *
......@@ -265,6 +265,21 @@ KgpgSubKeyType toSubType(const QString& capString, bool upper)
return ret;
}
QDateTime toDateTime(const QString &s)
{
QDateTime ret;
if (s.isEmpty())
return ret;
if (s.contains(QLatin1Char('T')))
ret = QDateTime::fromString(s, QLatin1Literal("yyyyMMddTHHmmss"));
else
ret = QDateTime::fromTime_t(s.toUInt());
return ret;
}
} // namespace Convert
} // namespace KgpgCore
/*
* Copyright (C) 2006 Jimmy Gilles <jimmygilles@gmail.com>
* Copyright (C) 2010,2013,2014 Rolf Eike Beer <kde@opensource.sf-tec.de>
* Copyright (C) 2010,2013,2014,2016 Rolf Eike Beer <kde@opensource.sf-tec.de>
*/
/***************************************************************************
* This program is free software; you can redistribute it and/or modify *
......@@ -48,6 +48,16 @@ namespace Convert
* @param upper if the uppercase or lowercase version should be parsed
*/
KgpgSubKeyType toSubType(const QString &capString, bool upper);
/**
* @brief parse GnuPG date fields
* @param s the input string
* @return the parsed date
* @retval QDateTime() if s was empty or the string was invalid
*
* Both the seconds since epoch UTC as well as ISO 8601 formats are
* supported.
*/
QDateTime toDateTime(const QString &s);
}
} // namespace KgpgCore
......
......@@ -169,7 +169,7 @@ readPublicKeysProcess(GPGProc &p, KGpgKeyNode *readNode)
const QString curve = (items > 16) ? lsp.at(16) : QString();
publiclistkeys << KgpgKey(lsp.at(4), lsp.at(2).toUInt(), Convert::toTrust(lsp.at(1)),
Convert::toAlgo(lsp.at(3)), subtype, keytype,
QDateTime::fromTime_t(lsp.at(5).toUInt()), curve);
Convert::toDateTime(lsp.at(5)), curve);
publickey = &publiclistkeys.last();
......@@ -179,11 +179,7 @@ readPublicKeysProcess(GPGProc &p, KGpgKeyNode *readNode)
else
publickey->setOwnerTrust(Convert::toOwnerTrust(owTrust[0]));
const QString &endDate = lsp.at(6);
if (endDate.isEmpty())
publickey->setExpiration(QDateTime());
else
publickey->setExpiration(QDateTime::fromTime_t(endDate.toUInt()));
publickey->setExpiration(Convert::toDateTime(lsp.at(6)));
publickey->setValid(enabled); // disabled key
......@@ -203,7 +199,7 @@ readPublicKeysProcess(GPGProc &p, KGpgKeyNode *readNode)
const QString curve = (items > 16) ? lsp.at(16) : QString();
KgpgKeySub sub(lsp.at(4), lsp.at(2).toUInt(), Convert::toTrust(lsp.at(1)),
Convert::toAlgo(lsp.at(3)), subtype, QDateTime::fromTime_t(lsp.at(5).toUInt()),
Convert::toAlgo(lsp.at(3)), subtype, Convert::toDateTime(lsp.at(5)),
curve);
// FIXME: Please see kgpgkey.h, KgpgSubKey class
......@@ -212,10 +208,7 @@ readPublicKeysProcess(GPGProc &p, KGpgKeyNode *readNode)
else
sub.setValid(!lsp.at(11).contains(QLatin1Char( 'D' )));
if (lsp.at(6).isEmpty())
sub.setExpiration(QDateTime());
else
sub.setExpiration(QDateTime::fromTime_t(lsp.at(6).toUInt()));
sub.setExpiration(Convert::toDateTime(lsp.at(6)));
publickey->subList()->append(sub);
if (readNode == Q_NULLPTR)
......@@ -345,16 +338,14 @@ readSecretKeysProcess(GPGProc &p)
const QString curve = (items > 16) ? lsp.at(16) : QString();
result << KgpgKey(lsp.at(4), lsp.at(2).toUInt(), Convert::toTrust(lsp.at(1)),
Convert::toAlgo(lsp.at(3)), subtype, keytype,
QDateTime::fromTime_t(lsp.at(5).toUInt()), curve);
Convert::toDateTime(lsp.at(5)), curve);
secretkey = &result.last();
secretkey->setSecret(true);
if (lsp.at(6).isEmpty())
secretkey->setExpiration(QDateTime());
else
secretkey->setExpiration(QDateTime::fromTime_t(lsp.at(6).toUInt()));
secretkey->setExpiration(Convert::toDateTime(lsp.at(6)));
hasuid = true;
} else if ((lsp.at(0) == QLatin1String( "uid" )) && (items >= 10)) {
if (hasuid)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment