/*
 * Copyright (C) 2017 ~ 2017 Deepin Technology Co., Ltd.
 *
 * Author:     Iceyer <me@iceyer.net>
 *
 * Maintainer: Iceyer <me@iceyer.net>
 *
 * 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
 * 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, see <http://www.gnu.org/licenses/>.
 */

#include <QDebug>
#include <QCoreApplication>
#include <QtDBus/QtDBus>
#include <DLog>

#include "licenseagent.h"
#include "licenseinfo.h"
#include "licensekernel.h"

static void licenseModuleInit(LicenseAgent *la)
{
    la->loadLicenseFile();
}

int main(int argc, char *argv[])
{
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));

    QCoreApplication app(argc, argv);

    app.setOrganizationName("deepin");

    Dtk::Core::DLogManager::registerConsoleAppender();
    Dtk::Core::DLogManager::registerFileAppender();

    LicenseKernel lk;
    LicenseInfo li(&lk);
    LicenseAgent la(&lk, &li);

    if (!QDBusConnection::systemBus().isConnected()) {
        qDebug() << "QDBusConnection::systemBus().isConnected() failed";
        return 0x0001;
    }

    if (!QDBusConnection::systemBus().registerService(SERVICE_NAME)) {
        qDebug() << "registerService Error" << QDBusConnection::systemBus().lastError();
        exit(0x0002);
    }

    if (!QDBusConnection::systemBus().registerObject(AGENT_SERVICE_PATH,  &la,
            QDBusConnection::ExportAllSlots |
            QDBusConnection::ExportAllSignals |
            QDBusConnection::ExportAllProperties)) {
        qDebug() << "registerObject LicenseDaemon Error" << QDBusConnection::systemBus().lastError();
        exit(0x0003);
    }

    if (!QDBusConnection::systemBus().registerObject(INFO_SERVICE_PATH,  &li,
            QDBusConnection::ExportAllSlots |
            QDBusConnection::ExportAllSignals |
            QDBusConnection::ExportAllProperties)) {
        qDebug() << "registerObject LicenseInfo Error" << QDBusConnection::systemBus().lastError();
        exit(0x0004);
    }

    licenseModuleInit(&la);

    app.exec();

    return 0;
}
