/*
 * 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/>.
 */

#pragma once
#include <QDBusAbstractAdaptor>

#include "licensedbus.h"

#define SYSTEM_LICENSE                  "/var/deepin/license.key"

const QString LicenseStateUnauthorized = "Unauthorized";
const QString LicenseStateFreeTrail    = "FreeTrail";
const QString LicenseStateAuthorized   = "Authorized";

enum ActiveType {
    VolumeFileActive = 0,
    SingleFileActive = 1,
    SingleKeyActive = 2,
};

enum ErrorCode {
    Success             = 0x0000,
    OpenLicenseFile     = 0x0001,
    CopyLicenseFile,
    WriteLicenseFile,

    SetPermissions      = 0x00F0,
    OpenSystemLicenseFile,
    RemoveSystemLicense,

    VerifyLicense       = 0x0F00,
    InvailedLicenseFile = 0x0F01,

    UnknowError         = 0xFFFF,
};

class LicenseKernel;
class LicenseInfo;
class LicenseAgentPrivate;
class LicenseAgent: public QObject
{
    Q_OBJECT
    Q_CLASSINFO("D-Bus Interface", AGENT_INTERFACE_NAME)

    Q_PROPERTY(QString Version READ version)
    Q_PROPERTY(QString Copyright READ copyright)
    Q_PROPERTY(QString Edition READ edition)
    Q_PROPERTY(QString MachineCode READ machineCode)
    Q_PROPERTY(QString InstallDateTime READ installTime)
    Q_PROPERTY(int TrialRemind READ trialRemind)

public slots:
    Q_SCRIPTABLE quint32 ImportSerialKey(const QString &key);
    Q_SCRIPTABLE quint32 ImportLicenseFile(const QString &file);
    Q_SCRIPTABLE quint32 Deactive();
    Q_SCRIPTABLE void Quit();

public:
    explicit LicenseAgent(LicenseKernel *kernel, LicenseInfo *licenseInfo,  QObject *parent = 0);
    ~LicenseAgent();
    void loadLicenseFile();

protected:
    QString version();
    QString copyright();
    QString edition();
    QString machineCode();
    QString installTime();
    int trialRemind();

private:
    QScopedPointer<LicenseAgentPrivate> d_ptr;
    Q_DECLARE_PRIVATE_D(qGetPtrHelper(d_ptr), LicenseAgent)
};

