kolabformatchecker.cpp 2.47 KB
Newer Older
Laurent Montel's avatar
Laurent Montel committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (C) 2012  Sofia Balicka <balicka@kolabsys.com>
 * Copyright (C) 2013  Christian Mollekopf <mollekopf@kolabsys.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
16
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
Laurent Montel's avatar
Laurent Montel committed
17 18 19 20 21 22 23 24 25 26
 */

#include <iostream>
#include <string>
#include <vector>
#include <QString>
#include <QFile>
#include <kolabformat.h>
#include "kolabformat/errorhandler.h"
#include "kolabformat/kolabobject.h"
Laurent Montel's avatar
Laurent Montel committed
27
#include "pimkolab_debug.h"
Laurent Montel's avatar
Laurent Montel committed
28 29
using namespace std;

Laurent Montel's avatar
Laurent Montel committed
30
KMime::Message::Ptr readMimeFile(const QString &fileName, bool &ok)
Laurent Montel's avatar
Laurent Montel committed
31
{
Laurent Montel's avatar
Laurent Montel committed
32 33
    QFile file(fileName);
    ok = file.open(QFile::ReadOnly);
Laurent Montel's avatar
Laurent Montel committed
34
    if (!ok) {
Laurent Montel's avatar
Laurent Montel committed
35
        cout << "failed to open file: " << fileName.toStdString() << endl;
Laurent Montel's avatar
Laurent Montel committed
36 37 38 39
        return KMime::Message::Ptr();
    }
    const QByteArray data = file.readAll();
    KMime::Message::Ptr msg = KMime::Message::Ptr(new KMime::Message);
Laurent Montel's avatar
Laurent Montel committed
40
    msg->setContent(KMime::CRLFtoLF(data));
Laurent Montel's avatar
Laurent Montel committed
41 42 43 44 45 46 47
    msg->parse();
    return msg;
}

int main(int argc, char *argv[])
{
    vector<string> inputFiles;
Laurent Montel's avatar
Laurent Montel committed
48
    inputFiles.reserve(argc - 1);
49 50 51 52
    for (int i = 1; i < argc; ++i) {
        inputFiles.push_back(argv[i]);
    }
    if (inputFiles.empty()) {
Laurent Montel's avatar
Laurent Montel committed
53 54 55 56 57 58 59 60
        cout << "Specify input-file\n";
        return -1;
    }

    int returnValue = 0;

    cout << endl;

Laurent Montel's avatar
Laurent Montel committed
61
    for (vector<string>::const_iterator it = inputFiles.begin();
Laurent Montel's avatar
Laurent Montel committed
62
         it != inputFiles.end(); ++it) {
Laurent Montel's avatar
Laurent Montel committed
63 64 65
        cout << "File: " << *it << endl;

        bool ok;
Laurent Montel's avatar
Laurent Montel committed
66
        KMime::Message::Ptr message = readMimeFile(QString::fromStdString(*it), ok);
Laurent Montel's avatar
Laurent Montel committed
67

Laurent Montel's avatar
Laurent Montel committed
68
        if (!ok) {
Laurent Montel's avatar
Laurent Montel committed
69 70 71 72 73 74 75
            returnValue = -1;
            cout << endl;
            continue;
        }

        Kolab::KolabObjectReader reader(message);

Laurent Montel's avatar
Laurent Montel committed
76
        if (Kolab::ErrorHandler::errorOccured()) {
Yuri Chornoivan's avatar
Yuri Chornoivan committed
77
            cout << "Errors occurred during parsing." << endl;
Laurent Montel's avatar
Laurent Montel committed
78 79 80 81 82 83 84 85 86 87
            returnValue = -1;
        } else {
            cout << "Parsed message without error." << endl;
        }

        cout << endl;
    }

    return returnValue;
}