aheinecke@4: #include "certificatelist.h"
aheinecke@4: 
aheinecke@7: #include <QDebug>
aheinecke@7: 
aheinecke@9: #define PARSER_VERSION "1"
aheinecke@9: 
aheinecke@70: CertificateList::CertificateList() : mStatus(NoList)
aheinecke@70: {
aheinecke@70: }
aheinecke@70: 
aheinecke@70: list_status_t CertificateList::readList(const char *fileName)
aheinecke@4: {
aheinecke@7:     char *data = NULL;
aheinecke@7:     size_t size = 0;
aheinecke@4: 
aheinecke@31:     mStatus = read_and_verify_list(fileName, &data, &size);
aheinecke@4: 
aheinecke@9:     if (!isValid()) {
aheinecke@70:         return mStatus;
aheinecke@9:     }
aheinecke@9: 
aheinecke@9:     // Take the data into the Qt Universe where memory is plentiful
aheinecke@9:     // and CPU's are fast :)
aheinecke@84:     mData = QString::fromLatin1(data, size);
aheinecke@9:     free(data);
aheinecke@9:     data = NULL;
aheinecke@84:     QStringList lines = mData.split("\n");
aheinecke@9: 
aheinecke@9:     for (int i = 0; i < lines.size(); ++i) {
aheinecke@9:         QString curLine = lines[i].trimmed();
aheinecke@9:         if (curLine.startsWith("F:")) {
aheinecke@9:             if (curLine.right(1) != PARSER_VERSION) {
aheinecke@9:                 qDebug() << "Invalid Format Version";
aheinecke@9:                 mStatus = IncompatibleVersion;
aheinecke@70:                 return mStatus;
aheinecke@9:             }
aheinecke@9:         } else if (curLine.startsWith("D:")) {
aheinecke@9:             bool ok = false;
aheinecke@9:             qlonglong timestamp = 0;
aheinecke@9: 
aheinecke@9:             curLine.remove(0, 2);
aheinecke@9:             timestamp = curLine.toLongLong(&ok);
aheinecke@9:             if (!ok) {
aheinecke@9:                 qDebug() << "Invalid Date";
aheinecke@9:                 mStatus = InvalidFormat;
aheinecke@70:                 return mStatus;
aheinecke@9:             }
aheinecke@9: 
aheinecke@9:             mDate = QDateTime::fromMSecsSinceEpoch(timestamp * 1000);
aheinecke@9:         } else if (curLine.startsWith("I:")) {
aheinecke@84:             mCertificatesInstall << Certificate(curLine);
aheinecke@9:         } else if (curLine.startsWith("R:")) {
aheinecke@97:             mCertificatesRemove << Certificate(curLine);
aheinecke@9:         } else if (curLine.startsWith("S:")) {
aheinecke@31:             // Signature is verified in read_and_verify_list
aheinecke@9:             continue;
aheinecke@9:         } else if (!curLine.isEmpty()){
aheinecke@9:             qDebug () << "Don't know how to handle: " << curLine;
aheinecke@9:         }
aheinecke@7:     }
aheinecke@70:     return mStatus;
aheinecke@4: }
aheinecke@70: 
aheinecke@70: CertificateList::CertificateList(const char *fileName) : mStatus(NoList)
aheinecke@70: {
aheinecke@70:     readList(fileName);
aheinecke@70: }
aheinecke@79: 
aheinecke@79: const QList<Certificate>& CertificateList::getInstallCertificates() const {
aheinecke@79:     return mCertificatesInstall;
aheinecke@79: }
aheinecke@79: 
aheinecke@79: const QList<Certificate>& CertificateList::getRemoveCertificates() const {
aheinecke@79:     return mCertificatesRemove;
aheinecke@79: }