Subversion Repositories tpanel

Rev

Rev 11 | Blame | Last modification | View Log | RSS feed

/*
 * Copyright (C) 2020, 2021 by Andreas Theofilu <andreas@theosys.at>
 *
 * 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
 * (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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 */

#include <unistd.h>
#include "tsettings.h"
#include "treadxml.h"

using std::string;
using std::vector;

TSettings::TSettings(const string& path)
    : mPath(path)
{
    DECL_TRACER("TSettings::TSettings(const string& path)");
    loadSettings(true);
}

bool TSettings::loadSettings(bool initial)
{
    DECL_TRACER("TSettings::loadSettings()");

    if (!initial)
    {
        mResourceLists.clear();
    }

    TError::clear();
    string fname = makeFileName(mPath, "prj.xma");

    if (!isValidFile())
    {
        MSG_ERROR("Error: File " << fname << " doesn't exist or can't be opened!");
        TError::setError();
        return false;
    }

    TReadXML txml(fname);

    if (TError::isError())
        return false;

    string value;
    mSetup.portCount = atoi(txml.findElement("portCount").c_str());
    mSetup.setupPort = atoi(txml.findElement("setupPort").c_str());
    mSetup.addressCount = atoi(txml.findElement("addressCount").c_str());
    mSetup.channelCount = atoi(txml.findElement("channelCount").c_str());
    mSetup.levelCount = atoi(txml.findElement("levelCount").c_str());
    mSetup.powerUpPage = txml.findElement("powerUpPage");

    value = txml.findElement("powerUpPopup");

    if (!value.empty())
    {
        mSetup.powerUpPopup.push_back(value);

        do
        {
            value = txml.findNextElement("powerUpPopup");

            if (txml.success())
                mSetup.powerUpPopup.push_back(value);
        }
        while (txml.success());
    }

    mSetup.feedbackBlinkRate = atoi(txml.findElement("feedbackBlinkRate").c_str());
    mSetup.startupString = txml.findElement("startupString");
    mSetup.wakeupString = txml.findElement("wakeupString");
    mSetup.sleepString = txml.findElement("sleepString");
    mSetup.standbyString = txml.findElement("standbyString");
    mSetup.shutdownString = txml.findElement("shutdownString");
    mSetup.idlePage = txml.findElement("idlePage");
    mSetup.idleTimeout = atoi(txml.findElement("idleTimeout").c_str());
    mSetup.extButtonsKey = atoi(txml.findElement("extButtonsKey").c_str());
    mSetup.screenWidth = atoi(txml.findElement("screenWidth").c_str());
    mSetup.screenHeight = atoi(txml.findElement("screenHeight").c_str());
    mSetup.screenRefresh = atoi(txml.findElement("screenRefresh").c_str());
    mSetup.screenRotate = atoi(txml.findElement("screenRotate").c_str());
    mSetup.screenDescription = txml.findElement("screenDescription");
    mSetup.pageTracking = atoi(txml.findElement("pageTracking").c_str());
    mSetup.cursor = atoi(txml.findElement("cursor").c_str());
    mSetup.brightness = atoi(txml.findElement("brightness").c_str());
    mSetup.lightSensorLevelPort = atoi(txml.findElement("lightSensorLevelPort").c_str());
    mSetup.lightSensorLevelCode = atoi(txml.findElement("lightSensorLevelCode").c_str());
    mSetup.lightSensorChannelPort = atoi(txml.findElement("lightSensorChannelPort").c_str());
    mSetup.lightSensorChannelCode = atoi(txml.findElement("lightSensorChannelCode").c_str());
    mSetup.motionSensorChannelPort = atoi(txml.findElement("motionSensorChannelPort").c_str());
    mSetup.motionSensorChannelCode = atoi(txml.findElement("motionSensorChannelCode").c_str());
    mSetup.batteryLevelPort = atoi(txml.findElement("batteryLevelPort").c_str());
    mSetup.batteryLevelCode = atoi(txml.findElement("batteryLevelCode").c_str());
    mSetup.irPortAMX38Emit = atoi(txml.findElement("irPortAMX38Emit").c_str());
    mSetup.irPortAMX455Emit = atoi(txml.findElement("irPortAMX455Emit").c_str());
    mSetup.irPortAMX38Recv = atoi(txml.findElement("irPortAMX38Recv").c_str());
    mSetup.irPortAMX455Recv = atoi(txml.findElement("irPortAMX455Recv").c_str());
    mSetup.irPortUser1 = atoi(txml.findElement("irPortUser1").c_str());
    mSetup.irPortUser2 = atoi(txml.findElement("irPortUser2").c_str());
    mSetup.cradleChannelPort = atoi(txml.findElement("cradleChannelPort").c_str());
    mSetup.cradleChannelCode = atoi(txml.findElement("cradleChannelCode").c_str());
    mSetup.uniqueID = atoi(txml.findElement("uniqueID").c_str());
    mSetup.appCreated = txml.findElement("appCreated");
    mSetup.buildNumber = atoi(txml.findElement("buildNumber").c_str());
    mSetup.appModified = txml.findElement("appModified");
    mSetup.buildNumberMod = atoi(txml.findElement("buildNumberMod").c_str());
    mSetup.buildStatusMod = txml.findElement("buildStatusMod");
    mSetup.activePalette = atoi(txml.findElement("activePalette").c_str());
    mSetup.marqueeSpeed = atoi(txml.findElement("marqueeSpeed").c_str());
    mSetup.setupPagesProject = atoi(txml.findElement("setupPagesProject").c_str());
    mSetup.voipCommandPort = atoi(txml.findElement("voipCommandPort").c_str());

    txml.findElement("resourceList");

    while (txml.success())
    {
        string type = txml.getAttribute("type");
        RESOURCE_LIST_T list = findResourceType(type);

        if (mResourceLists.size() == 0 || list.type.empty())
        {
            list.type = type;
            list.ressource.clear();
            mResourceLists.push_back(list);
        }

        mxml_node_t *node = txml.getFirstChild();
        RESOURCE_T resource;

        while (node)
        {
            mxml_node_t *n = txml.getFirstChild(node);

            while (n)
            {
                string e = txml.getElementName(n);

                if (e.compare("name") == 0)
                    resource.name = txml.getTextFromNode(n);
                else if (e.compare("protocol") == 0)
                    resource.protocol = txml.getTextFromNode(n);
                else if (e.compare("host") == 0)
                    resource.host = txml.getTextFromNode(n);
                else if (e.compare("file") == 0)
                    resource.file = txml.getTextFromNode(n);
                else if (e.compare("password") == 0)
                {
                    resource.password = txml.getTextFromNode(n);
                    int enc = atoi(txml.getAttributeFromNode(n, "encrypted").c_str());

                    if (enc != 0)
                        resource.encrypted = true;
                    else
                        resource.encrypted = false;
                }
                else if (e.compare("user") == 0)
                    resource.user = txml.getTextFromNode(n);
                else if (e.compare("path") == 0)
                    resource.path = txml.getTextFromNode(n);
                else if (e.compare("refresh") == 0)
                    resource.refresh = txml.getIntFromNode(n);
                else if (e.compare("dynamo") == 0)
                    resource.dynamo = ((txml.getIntFromNode(n) == 0) ? false : true);
                else if (e.compare("preserve") == 0)
                    resource.preserve = ((txml.getIntFromNode(n) == 0) ? false : true);

                n = txml.getNextChild(n);
            }

            list.ressource.push_back(resource);
            node = txml.getNextChild();
        }

        vector<RESOURCE_LIST_T>::iterator itResList;

        for (itResList = mResourceLists.begin(); itResList != mResourceLists.end(); itResList++)
        {
            if (itResList->type.compare(type) == 0)
            {
                mResourceLists.erase(itResList);
                mResourceLists.push_back(list);
                break;
            }
        }

        txml.findNextElement("resourceList");
    }

    txml.findElement("paletteList");

    if (!txml.success())
    {
        MSG_WARNING("There exists no color palette! There will be only the system colors available.");
        return true;
    }

    mxml_node_t *node = txml.getFirstChild();

    while (node)
    {
        PALETTE_SETUP ps;
        mxml_node_t *n = txml.getFirstChild(node);

        while (n)
        {
            string e = txml.getElementName(n);

            if (e.compare("name") == 0)
                ps.name = txml.getTextFromNode(n);
            else if (e.compare("file") == 0)
                ps.file = txml.getTextFromNode(n);
            else if (e.compare("paletteID") == 0)
                ps.paletteID = txml.getIntFromNode(n);

            n = txml.getNextChild(n);
        }

        mSetup.palettes.push_back(ps);
        node = txml.getNextChild();
    }

    return true;
}

RESOURCE_LIST_T TSettings::findResourceType(const string& type)
{
    DECL_TRACER ("TSettings::findResourceType(const string& type)");

    vector<RESOURCE_LIST_T>::iterator iter;

    for (iter = mResourceLists.begin(); iter != mResourceLists.end(); iter++)
    {
        if (iter->type.compare(type) == 0)
            return *iter;
    }

    return RESOURCE_LIST_T();
}