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
 
186 andreas 24
#if __cplusplus < 201402L
25
#   error "This module requires at least C++14 standard!"
26
#else
27
#   if __cplusplus < 201703L
28
#       include <experimental/filesystem>
29
namespace fs = std::experimental::filesystem;
30
#       warning "Support for C++14 and experimental filesystem will be removed in a future version!"
31
#   else
32
#       include <filesystem>
33
#       ifdef __ANDROID__
34
namespace fs = std::__fs::filesystem;
35
#       else
36
namespace fs = std::filesystem;
37
#       endif
38
#   endif
39
#endif
40
 
8 andreas 41
using std::string;
75 andreas 42
using std::vector;
8 andreas 43
using std::map;
44
using std::pair;
75 andreas 45
using namespace Expat;
8 andreas 46
 
47
TIcons::TIcons()
48
{
49
    DECL_TRACER("TIcons::TIcons()");
50
 
51
    initialize();
52
}
53
 
54
TIcons::~TIcons()
55
{
56
    DECL_TRACER("TIcons::~TIcons()");
57
}
58
 
59
void TIcons::initialize()
60
{
61
    DECL_TRACER("TIcons::initialize()");
62
 
11 andreas 63
    if (mIcons.size() > 0)
64
        mIcons.clear();
65
 
186 andreas 66
    string projectPath = TConfig::getProjectPath();
8 andreas 67
 
186 andreas 68
    if (!fs::exists(projectPath + "/prj.xma"))
69
        projectPath += "/__system";
70
 
71
    string path = makeFileName(projectPath, "icon.xma");
72
 
8 andreas 73
    if (!isValidFile())
74
    {
75
        MSG_ERROR("File " << path << " doesn't exist or is not readable!");
76
        TError::setError();
77
        return;
78
    }
79
 
75 andreas 80
    TExpat xml(path);
81
    xml.setEncoding(ENC_CP1250);
8 andreas 82
 
75 andreas 83
    if (!xml.parse())
8 andreas 84
        return;
85
 
75 andreas 86
    int depth = 0;
87
    size_t index = 0;
8 andreas 88
 
75 andreas 89
    if ((index = xml.getElementIndex("iconList", &depth)) == TExpat::npos)
8 andreas 90
    {
91
        MSG_DEBUG("File does not contain the element \"iconList\"!");
92
        TError::setError();
93
        return;
94
    }
95
 
75 andreas 96
    depth++;
97
    string name, content;
98
    vector<ATTRIBUTE_t> attrs;
8 andreas 99
 
75 andreas 100
    while ((index = xml.getNextElementFromIndex(index, &name, &content, &attrs)) != TExpat::npos)
8 andreas 101
    {
75 andreas 102
        if (name.compare("maxIcons") == 0)
103
            mEntries = xml.convertElementToInt(content);
104
        else if (name.compare("icon") == 0)
8 andreas 105
        {
75 andreas 106
            int number = xml.getAttributeInt("number", attrs);
107
            index = xml.getNextElementFromIndex(index, &name, &content, nullptr);
8 andreas 108
 
75 andreas 109
            if (index != TExpat::npos)
8 andreas 110
            {
75 andreas 111
                if (name.compare("file") == 0)
8 andreas 112
                {
75 andreas 113
                    string file = content;
8 andreas 114
 
115
                    if (number > 0 && !file.empty())
116
                        mIcons.insert(pair<int, string>(number, file));
117
                }
118
            }
75 andreas 119
 
120
            index++;
8 andreas 121
        }
122
    }
123
}
124
 
125
string TIcons::getFile(int number)
126
{
127
    DECL_TRACER("TIcons::getFile(int number)");
128
 
129
    if (mIcons.size() == 0)
130
    {
131
        MSG_WARNING("No icons in cache!");
132
        return string();
133
    }
134
 
135
    map<int, string>::iterator iter = mIcons.find(number);
136
 
137
    if (iter == mIcons.end())
138
        return string();
139
 
140
    return iter->second;
141
}
14 andreas 142
 
143
int TIcons::getNumber(const string& file)
144
{
145
    DECL_TRACER("TIcons::getNumber(const string& file)");
146
 
147
    if (mIcons.size() == 0)
148
    {
149
        MSG_WARNING("No icons in cache!");
150
        return -1;
151
    }
152
 
153
    map<int, string>::iterator iter;
154
 
118 andreas 155
    for (iter = mIcons.begin(); iter != mIcons.end(); ++iter)
14 andreas 156
    {
157
        if (iter->second.compare(file) == 0)
158
            return iter->first;
159
    }
160
 
161
    return -1;
162
}