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 |
|
|
|
40 |
vector<RESOURCE_LIST_T>::iterator iter;
|
|
|
41 |
|
|
|
42 |
for (iter = mResources.begin(); iter != mResources.end(); iter++)
|
|
|
43 |
{
|
|
|
44 |
if (iter->type.compare(type) == 0)
|
|
|
45 |
return *iter;
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
return RESOURCE_LIST_T();
|
|
|
49 |
}
|
|
|
50 |
|
21 |
andreas |
51 |
RESOURCE_T TPrjResources::findResource(int idx, const std::string& name)
|
|
|
52 |
{
|
|
|
53 |
DECL_TRACER("TPrjResources::findResource(int idx, const std::string& name)");
|
|
|
54 |
|
|
|
55 |
if (idx < 1 || mResources.size() < (size_t)idx)
|
|
|
56 |
{
|
|
|
57 |
MSG_ERROR("Invalid index " << idx << "!");
|
|
|
58 |
return RESOURCE_T();
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
RESOURCE_LIST_T list = mResources[idx-1];
|
|
|
62 |
|
|
|
63 |
if (list.type.empty())
|
|
|
64 |
{
|
|
|
65 |
MSG_ERROR("Resource list " << idx << " is empty!");
|
|
|
66 |
return RESOURCE_T();
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
vector<RESOURCE_T>::iterator iter;
|
|
|
70 |
|
|
|
71 |
for (iter = list.ressource.begin(); iter != list.ressource.end(); iter++)
|
|
|
72 |
{
|
|
|
73 |
MSG_DEBUG("Resource: " << iter->name);
|
|
|
74 |
if (iter->name.compare(name) == 0)
|
|
|
75 |
return *iter;
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
MSG_WARNING("Resource " << name << " not found!");
|
|
|
79 |
return RESOURCE_T();
|
|
|
80 |
}
|
|
|
81 |
|
8 |
andreas |
82 |
RESOURCE_T TPrjResources::findResource(const std::string& type, const std::string& name)
|
|
|
83 |
{
|
|
|
84 |
DECL_TRACER("TPrjResources::findResource(const std::string& type, const std::string& name)");
|
|
|
85 |
|
|
|
86 |
RESOURCE_LIST_T list = findResourceType(type);
|
|
|
87 |
|
|
|
88 |
if (list.type.empty())
|
|
|
89 |
return RESOURCE_T();
|
|
|
90 |
|
|
|
91 |
vector<RESOURCE_T>::iterator iter;
|
|
|
92 |
|
|
|
93 |
for (iter = list.ressource.begin(); iter != list.ressource.end(); iter++)
|
|
|
94 |
{
|
|
|
95 |
if (iter->name.compare(name) == 0)
|
|
|
96 |
return *iter;
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
return RESOURCE_T();
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
RESOURCE_T TPrjResources::findResource(const std::string& name)
|
|
|
103 |
{
|
|
|
104 |
DECL_TRACER("TPrjResources::findResource(const std::string& name)");
|
|
|
105 |
|
|
|
106 |
if (mResources.size() == 0 || mResources[0].ressource.size() == 0)
|
|
|
107 |
return RESOURCE_T();
|
|
|
108 |
|
|
|
109 |
vector<RESOURCE_T> list = mResources[0].ressource;
|
|
|
110 |
vector<RESOURCE_T>::iterator iter;
|
|
|
111 |
|
|
|
112 |
for (iter = list.begin(); iter != list.end(); iter++)
|
|
|
113 |
{
|
|
|
114 |
if (iter->name.compare(name) == 0)
|
|
|
115 |
return *iter;
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
return RESOURCE_T();
|
|
|
119 |
}
|
21 |
andreas |
120 |
|
|
|
121 |
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)
|
|
|
122 |
{
|
|
|
123 |
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)");
|
|
|
124 |
|
|
|
125 |
if (mResources.size() == 0 || mResources[0].ressource.size() == 0)
|
|
|
126 |
return false;
|
|
|
127 |
|
|
|
128 |
vector<RESOURCE_T> list = mResources[0].ressource;
|
|
|
129 |
vector<RESOURCE_T>::iterator iter;
|
|
|
130 |
|
|
|
131 |
for (iter = list.begin(); iter != list.end(); iter++)
|
|
|
132 |
{
|
|
|
133 |
if (iter->name.compare(name) == 0)
|
|
|
134 |
{
|
|
|
135 |
RESOURCE_T res = *iter;
|
|
|
136 |
|
|
|
137 |
if (!scheme.empty())
|
|
|
138 |
res.protocol = scheme;
|
|
|
139 |
|
|
|
140 |
if (!host.empty())
|
|
|
141 |
res.host = host;
|
|
|
142 |
|
|
|
143 |
if (!path.empty())
|
|
|
144 |
res.path = path;
|
|
|
145 |
|
|
|
146 |
if (!file.empty())
|
|
|
147 |
res.file = file;
|
|
|
148 |
|
|
|
149 |
if (!user.empty())
|
|
|
150 |
res.user = user;
|
|
|
151 |
|
|
|
152 |
if (!pw.empty())
|
|
|
153 |
res.password = pw;
|
|
|
154 |
|
|
|
155 |
if (refresh >= 0)
|
|
|
156 |
res.refresh = refresh;
|
|
|
157 |
|
|
|
158 |
list.erase(iter);
|
|
|
159 |
list.push_back(res);
|
|
|
160 |
RESOURCE_LIST_T rlist = mResources[0];
|
|
|
161 |
rlist.ressource.clear();
|
|
|
162 |
rlist.ressource = list;
|
|
|
163 |
mResources.erase(mResources.begin());
|
|
|
164 |
mResources.push_back(rlist);
|
|
|
165 |
return true;
|
|
|
166 |
}
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
return false;
|
|
|
170 |
}
|