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 "tprjresources.h"
20
#include "terror.h"
21
 
22
using std::string;
23
using std::vector;
24
 
25
TPrjResources::TPrjResources(vector<RESOURCE_LIST_T>& list)
26
        : mResources(list)
27
{
28
    DECL_TRACER("TPrjResources::TPrjResources(vector<RESOURCE_LIST_T>& list)");
29
}
30
 
31
TPrjResources::~TPrjResources()
32
{
33
    DECL_TRACER("TPrjResources::~TPrjResources()");
34
}
35
 
36
RESOURCE_LIST_T TPrjResources::findResourceType(const std::string& type)
37
{
38
    DECL_TRACER("TPrjResources::findResourceType(const std::string& type)");
39
 
97 andreas 40
    if (mResources.size() == 0)
41
        return RESOURCE_LIST_T();
42
 
8 andreas 43
    vector<RESOURCE_LIST_T>::iterator iter;
44
 
45
    for (iter = mResources.begin(); iter != mResources.end(); iter++)
46
    {
47
        if (iter->type.compare(type) == 0)
48
            return *iter;
49
    }
50
 
51
    return RESOURCE_LIST_T();
52
}
53
 
21 andreas 54
RESOURCE_T TPrjResources::findResource(int idx, const std::string& name)
55
{
56
    DECL_TRACER("TPrjResources::findResource(int idx, const std::string& name)");
57
 
58
    if (idx < 1 || mResources.size() < (size_t)idx)
59
    {
60
        MSG_ERROR("Invalid index " << idx << "!");
61
        return RESOURCE_T();
62
    }
63
 
64
    RESOURCE_LIST_T list = mResources[idx-1];
65
 
66
    if (list.type.empty())
67
    {
68
        MSG_ERROR("Resource list " << idx << " is empty!");
69
        return RESOURCE_T();
70
    }
71
 
72
    vector<RESOURCE_T>::iterator iter;
73
 
74
    for (iter = list.ressource.begin(); iter != list.ressource.end(); iter++)
75
    {
76
        MSG_DEBUG("Resource: " << iter->name);
77
        if (iter->name.compare(name) == 0)
78
            return *iter;
79
    }
80
 
81
    MSG_WARNING("Resource " << name << " not found!");
82
    return RESOURCE_T();
83
}
84
 
8 andreas 85
RESOURCE_T TPrjResources::findResource(const std::string& type, const std::string& name)
86
{
87
    DECL_TRACER("TPrjResources::findResource(const std::string& type, const std::string& name)");
88
 
89
    RESOURCE_LIST_T list = findResourceType(type);
90
 
91
    if (list.type.empty())
92
        return RESOURCE_T();
93
 
94
    vector<RESOURCE_T>::iterator iter;
95
 
96
    for (iter = list.ressource.begin(); iter != list.ressource.end(); iter++)
97
    {
98
        if (iter->name.compare(name) == 0)
99
            return *iter;
100
    }
101
 
102
    return RESOURCE_T();
103
}
104
 
105
RESOURCE_T TPrjResources::findResource(const std::string& name)
106
{
107
    DECL_TRACER("TPrjResources::findResource(const std::string& name)");
108
 
109
    if (mResources.size() == 0 || mResources[0].ressource.size() == 0)
110
        return RESOURCE_T();
111
 
112
    vector<RESOURCE_T> list = mResources[0].ressource;
113
    vector<RESOURCE_T>::iterator iter;
114
 
115
    for (iter = list.begin(); iter != list.end(); iter++)
116
    {
117
        if (iter->name.compare(name) == 0)
118
            return *iter;
119
    }
120
 
121
    return RESOURCE_T();
122
}
21 andreas 123
 
97 andreas 124
size_t TPrjResources::getResourceIndex(const string& type)
125
{
126
    DECL_TRACER("TPrjResources::getResourceIndex(const string& type)");
127
 
128
    if (mResources.size() == 0)
129
        return npos;
130
 
131
    vector<RESOURCE_LIST_T>::iterator iter;
132
    size_t idx = 0;
133
 
134
    for (iter = mResources.begin(); iter != mResources.end(); iter++)
135
    {
136
        idx++;
137
 
138
        if (iter->type.compare(type) == 0)
139
            return idx;
140
    }
141
 
142
    return npos;
143
}
144
 
21 andreas 145
bool TPrjResources::setResource(const string& name, const string& scheme, const string& host, const string& path, const string& file, const string& user, const string& pw, int refresh)
146
{
147
    DECL_TRACER("TPrjResources::setResource(const string& name, const string& scheme, const string& host, const string& path, const string& file, const string& user, const string& pw, int refresh)");
148
 
97 andreas 149
    if (mResources.size() == 0)
21 andreas 150
        return false;
151
 
97 andreas 152
    vector<RESOURCE_LIST_T>::iterator itRlist;
153
 
154
    for (itRlist = mResources.begin(); itRlist != mResources.end(); ++itRlist)
155
    {
156
        if (itRlist->type.compare("image") == 0)
157
            break;
158
    }
159
 
160
    if (itRlist == mResources.end())
161
    {
162
        MSG_ERROR("There was no resource type \"image\" found in the resources!");
163
        return false;
164
    }
165
 
166
    vector<RESOURCE_T> list = itRlist->ressource;
21 andreas 167
    vector<RESOURCE_T>::iterator iter;
168
 
169
    for (iter = list.begin(); iter != list.end(); iter++)
170
    {
171
        if (iter->name.compare(name) == 0)
172
        {
173
            RESOURCE_T res = *iter;
174
 
175
            if (!scheme.empty())
176
                res.protocol = scheme;
177
 
178
            if (!host.empty())
179
                res.host = host;
180
 
181
            if (!path.empty())
182
                res.path = path;
183
 
184
            if (!file.empty())
185
                res.file = file;
186
 
187
            if (!user.empty())
188
                res.user = user;
189
 
190
            if (!pw.empty())
191
                res.password = pw;
192
 
193
            if (refresh >= 0)
194
                res.refresh = refresh;
195
 
196
            list.erase(iter);
197
            list.push_back(res);
97 andreas 198
 
199
            itRlist->ressource.clear();
200
            itRlist->ressource = list;
21 andreas 201
            return true;
202
        }
203
    }
204
 
205
    return false;
206
}
97 andreas 207
 
208
bool TPrjResources::addResource(const string& name, const string& scheme, const string& host, const string& path, const string& file, const string& user, const string& pw, int refresh)
209
{
210
    DECL_TRACER("TPrjResources::addResource(const string& name, const string& scheme, const string& host, const string& path, const string& file, const string& user, const string& pw, int refresh)");
211
 
212
    vector<RESOURCE_LIST_T>::iterator itRlist;
213
 
214
    if (mResources.size() == 0)
215
    {
216
        RESOURCE_LIST_T rl;
217
        rl.type = "image";
218
        mResources.push_back(rl);
219
        itRlist = mResources.begin();
220
    }
221
    else
222
    {
223
        // Find the resource container
224
        for (itRlist = mResources.begin(); itRlist != mResources.end(); ++itRlist)
225
        {
226
            if (itRlist->type.compare("image") == 0)
227
                break;
228
        }
229
    }
230
 
231
    if (itRlist == mResources.end())
232
    {
233
        MSG_ERROR("There is no resouce container called \"image\"!");
234
        return false;
235
    }
236
 
237
    RESOURCE_T r;
238
    r.name = name;
239
    r.protocol = scheme;
240
    r.host = host;
241
    r.path = path;
242
    r.file = file;
243
    r.user = user;
244
    r.password = pw;
245
    r.refresh = refresh;
246
 
247
    // Make sure the resource does not already exist
248
    if (itRlist->ressource.size() == 0)
249
    {
250
        itRlist->ressource.push_back(r);
251
        return true;
252
    }
253
 
254
    vector<RESOURCE_T>::iterator iter;
255
 
256
    for (iter = itRlist->ressource.begin(); iter != itRlist->ressource.end(); iter++)
257
    {
258
        if (iter->name == name)
259
        {
260
            iter->protocol = scheme;
261
            iter->host = host;
262
            iter->path = path;
263
            iter->file = file;
264
            iter->user = user;
265
            iter->password = pw;
266
            iter->refresh = refresh;
267
            return true;
268
        }
269
    }
270
 
271
    itRlist->ressource.push_back(r);
272
    return true;
273
}