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"
21
#include "treadxml.h"
22
#include "tconfig.h"
23
 
24
using std::string;
25
using std::map;
26
using std::pair;
27
 
28
TIcons::TIcons()
29
{
30
    DECL_TRACER("TIcons::TIcons()");
31
 
32
    initialize();
33
}
34
 
35
TIcons::~TIcons()
36
{
37
    DECL_TRACER("TIcons::~TIcons()");
38
}
39
 
40
void TIcons::initialize()
41
{
42
    DECL_TRACER("TIcons::initialize()");
43
 
11 andreas 44
    if (mIcons.size() > 0)
45
        mIcons.clear();
46
 
8 andreas 47
    string path = makeFileName(TConfig::getProjectPath(), "icon.xma");
48
 
49
    if (!isValidFile())
50
    {
51
        MSG_ERROR("File " << path << " doesn't exist or is not readable!");
52
        TError::setError();
53
        return;
54
    }
55
 
56
    TReadXML reader(path);
57
 
58
    if (TError::isError())
59
    {
60
        MSG_ERROR("Stopped scanning file " << path << " because of previous errors!");
61
        return;
62
    }
63
 
64
    reader.findElement("iconList");
65
 
66
    if (!reader.success())
67
    {
68
        MSG_DEBUG("File does not contain the element \"iconList\"!");
69
        TError::setError();
70
        return;
71
    }
72
 
73
    mxml_node_t *node = reader.getFirstChild();
74
 
75
    while (node)
76
    {
77
        string ename = reader.getElementName(node);
78
 
79
        if (ename.compare("maxIcons") == 0)
80
            mEntries = reader.getIntFromNode(node);
81
        else if (ename.compare("icon") == 0)
82
        {
83
            int number = atoi(reader.getAttributeFromNode(node, "number").c_str());
84
            mxml_node_t *n = reader.getFirstChild(node);
85
 
86
            if (n)
87
            {
88
                string e = reader.getElementName(n);
89
 
90
                if (e.compare("file") == 0)
91
                {
92
                    string file = reader.getTextFromNode(n);
93
 
94
                    if (number > 0 && !file.empty())
95
                        mIcons.insert(pair<int, string>(number, file));
96
                }
97
            }
98
        }
99
 
100
        node = reader.getNextChild();
101
    }
102
}
103
 
104
string TIcons::getFile(int number)
105
{
106
    DECL_TRACER("TIcons::getFile(int number)");
107
 
108
    if (mIcons.size() == 0)
109
    {
110
        MSG_WARNING("No icons in cache!");
111
        return string();
112
    }
113
 
114
    map<int, string>::iterator iter = mIcons.find(number);
115
 
116
    if (iter == mIcons.end())
117
        return string();
118
 
119
    return iter->second;
120
}
14 andreas 121
 
122
int TIcons::getNumber(const string& file)
123
{
124
    DECL_TRACER("TIcons::getNumber(const string& file)");
125
 
126
    if (mIcons.size() == 0)
127
    {
128
        MSG_WARNING("No icons in cache!");
129
        return -1;
130
    }
131
 
132
    map<int, string>::iterator iter;
133
 
134
    for (iter = mIcons.begin(); iter != mIcons.end(); iter++)
135
    {
136
        if (iter->second.compare(file) == 0)
137
            return iter->first;
138
    }
139
 
140
    return -1;
141
}