Subversion Repositories tpanel

Rev

Rev 446 | Details | Compare with Previous | Last modification | View Log | RSS feed

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