/*
 * Copyright (C) 2023 Uniontech Software Technology Co., Ltd.
 *
 * Author:     liuzhangjian<liuzhangjian@uniontech.com>
 *
 * Maintainer: zhengyouge<zhengyouge@uniontech.com>
 *             liuzhangjian<liuzhangjian@uniontech.com>
 *
 * 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 "eetrustemblemplugin.h"
#include "grouppolicy.h"

#include <QLibrary>

#define LIB_PATH "/opt/apps/com.eetrust.wdaq/files/bin/SLClient/lib/libwdaqGetIconName.so"
#define EMBLEM_ENABLE_KEY "dfm.eetrust.emblem.enable"

USING_DFMEXT_NAMESPACE

EetrustEmblemPlugin::EetrustEmblemPlugin()
{
    init();
}

void EetrustEmblemPlugin::init()
{
    registerLocationEmblemIcons(std::bind(&EetrustEmblemPlugin::locationEmblemIcons, this,
                                          std::placeholders::_1, std::placeholders::_2));
    QLibrary library(LIB_PATH);
    getIconNameFunc = reinterpret_cast<GetIncoNameFunc>(library.resolve("wdaqGetIconName"));
}

DFMExtEmblem EetrustEmblemPlugin::locationEmblemIcons(const std::string &filePath, int systemIconCount)
{
    DFMExtEmblem emblem;
    bool enable = GroupPolicy::instance()->getValue(EMBLEM_ENABLE_KEY, false).toBool();
    if (!enable || !getIconNameFunc || systemIconCount >= 4)
        return emblem;

    char iconName[50] = { 0 };
    if (getIconNameFunc(const_cast<char *>(filePath.c_str()), iconName) != 0)
        return emblem;

    DFMExtEmblemIconLayout iconLayout(static_cast<DFMExtEmblemIconLayout::LocationType>(systemIconCount), iconName);
    emblem.setEmblem({ iconLayout });

    return emblem;
}
