Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
8 andreas 1
/*
21 andreas 2
 * Copyright (C) 2020, 2021 by Andreas Theofilu <andreas@theosys.at>
8 andreas 3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, write to the Free Software Foundation,
16
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
17
 */
18
 
19
#include "ticons.h"
20
#include "terror.h"
75 andreas 21
#include "texpat++.h"
8 andreas 22
#include "tconfig.h"
23
 
24
using std::string;
75 andreas 25
using std::vector;
8 andreas 26
using std::map;
27
using std::pair;
75 andreas 28
using namespace Expat;
8 andreas 29
 
30
TIcons::TIcons()
31
{
32
    DECL_TRACER("TIcons::TIcons()");
33
 
34
    initialize();
35
}
36
 
37
TIcons::~TIcons()
38
{
39
    DECL_TRACER("TIcons::~TIcons()");
40
}
41
 
42
void TIcons::initialize()
43
{
44
    DECL_TRACER("TIcons::initialize()");
45
 
11 andreas 46
    if (mIcons.size() > 0)
47
        mIcons.clear();
48
 
8 andreas 49
    string path = makeFileName(TConfig::getProjectPath(), "icon.xma");
50
 
51
    if (!isValidFile())
52
    {
53
        MSG_ERROR("File " << path << " doesn't exist or is not readable!");
54
        TError::setError();
55
        return;
56
    }
57
 
75 andreas 58
    TExpat xml(path);
59
    xml.setEncoding(ENC_CP1250);
8 andreas 60
 
75 andreas 61
    if (!xml.parse())
8 andreas 62
        return;
63
 
75 andreas 64
    int depth = 0;
65
    size_t index = 0;
8 andreas 66
 
75 andreas 67
    if ((index = xml.getElementIndex("iconList", &depth)) == TExpat::npos)
8 andreas 68
    {
69
        MSG_DEBUG("File does not contain the element \"iconList\"!");
70
        TError::setError();
71
        return;
72
    }
73
 
75 andreas 74
    depth++;
75
    string name, content;
76
    vector<ATTRIBUTE_t> attrs;
8 andreas 77
 
75 andreas 78
    while ((index = xml.getNextElementFromIndex(index, &name, &content, &attrs)) != TExpat::npos)
8 andreas 79
    {
75 andreas 80
        if (name.compare("maxIcons") == 0)
81
            mEntries = xml.convertElementToInt(content);
82
        else if (name.compare("icon") == 0)
8 andreas 83
        {
75 andreas 84
            int number = xml.getAttributeInt("number", attrs);
85
            index = xml.getNextElementFromIndex(index, &name, &content, nullptr);
8 andreas 86
 
75 andreas 87
            if (index != TExpat::npos)
8 andreas 88
            {
75 andreas 89
                if (name.compare("file") == 0)
8 andreas 90
                {
75 andreas 91
                    string file = content;
8 andreas 92
 
93
                    if (number > 0 && !file.empty())
94
                        mIcons.insert(pair<int, string>(number, file));
95
                }
96
            }
75 andreas 97
 
98
            index++;
8 andreas 99
        }
100
    }
101
}
102
 
103
string TIcons::getFile(int number)
104
{
105
    DECL_TRACER("TIcons::getFile(int number)");
106
 
107
    if (mIcons.size() == 0)
108
    {
109
        MSG_WARNING("No icons in cache!");
110
        return string();
111
    }
112
 
113
    map<int, string>::iterator iter = mIcons.find(number);
114
 
115
    if (iter == mIcons.end())
116
        return string();
117
 
118
    return iter->second;
119
}
14 andreas 120
 
121
int TIcons::getNumber(const string& file)
122
{
123
    DECL_TRACER("TIcons::getNumber(const string& file)");
124
 
125
    if (mIcons.size() == 0)
126
    {
127
        MSG_WARNING("No icons in cache!");
128
        return -1;
129
    }
130
 
131
    map<int, string>::iterator iter;
132
 
118 andreas 133
    for (iter = mIcons.begin(); iter != mIcons.end(); ++iter)
14 andreas 134
    {
135
        if (iter->second.compare(file) == 0)
136
            return iter->first;
137
    }
138
 
139
    return -1;
140
}