Webdar 1.0.0
Web user interface to libdar
bibliotheque.hpp
Go to the documentation of this file.
1/*********************************************************************/
2// webdar - a web server and interface program to libdar
3// Copyright (C) 2013-2025 Denis Corbin
4//
5// This file is part of Webdar
6//
7// Webdar is free software: you can redistribute it and/or modify
8// it under the terms of the GNU General Public License as published by
9// the Free Software Foundation, either version 3 of the License, or
10// (at your option) any later version.
11//
12// Webdar is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with Webdar. If not, see <http://www.gnu.org/licenses/>
19//
20//----
21// to contact the author: dar.linux@free.fr
22/*********************************************************************/
23
24#ifndef BIBLIOTHEQUE_HPP
25#define BIBLIOTHEQUE_HPP
26
27 // C system header files
28#include "my_config.h"
29extern "C"
30{
31
32}
33
34 // C++ system header files
35#include <map>
36#include <string>
37#include <memory>
38#include <set>
39
40 // webdar headers
41#include "jsoner.hpp"
42#include "events.hpp"
43
45
46
48
54
55class bibliotheque : public jsoner, public events
56{
57public:
58
60
63 {
64 filefilter = 0,
65 pathfilter = 1,
66 command = 2,
67 repo = 3,
68 compress = 4,
69 confsave = 5,
70 conftest = 6,
71 confdiff = 7,
72 conflist = 8,
73 confrest = 9,
74 confmerge = 10,
75 confrepair = 11,
76 confcommon = 12,
77 slicing = 13,
78 ciphering = 14,
79 delta_sig = 15,
80 over_policy = 16,
81 confread = 17,
82 confisolate = 18,
83 EOE = 19
84 };
85
87 static constexpr const char* default_config_name = "default";
88 static constexpr const char* default_no_compression = "no-compress";
89 static constexpr const char* default_full_from_diff = "full from diff";
90
91
93 static std::string changed(category cat);
94
95
97 static std::string category_to_string(category cat);
98
100 static category string_to_category(const std::string & s);
101
103 static std::string category_description(category cat, bool capitalized);
104
105
108 {
109 category cat;
110 std::string confname;
111
112 coordinates(category c, const std::string & name): cat(c), confname(name) {};
113 bool operator < (const coordinates & ref) const { return cat < ref.cat || (cat == ref.cat && confname < ref.confname); };
114 };
115
117 typedef std::set<coordinates> using_set;
118
119 bibliotheque();
120 bibliotheque(const bibliotheque & ref) = default;
121 bibliotheque(bibliotheque && ref) noexcept(false) = default;
122 bibliotheque & operator = (const bibliotheque & ref) = default;
123 bibliotheque & operator = (bibliotheque && ref) noexcept(false) = default;
124 virtual ~bibliotheque() = default;
125
127
129 void add_config(category categ, const std::string & name, const json & config, const using_set & refs = std::set<coordinates>());
130
132
134 void add_external_ref_to(category categ, const std::string & name, const void* from_where);
135
137
139 void update_config(category categ, const std::string & name, const json & config, const using_set & refs = std::set<coordinates>());
140
142
144 void delete_config(category categ, const std::string & name);
145
146
148
150 void delete_external_ref_to(category categ, const std::string & name, const void* from_where);
151
153 bool has_config(category categ, const std::string & name) const;
154
156
158 json fetch_config(category categ, const std::string & name) const;
159
160
162 std::string display_config(category categ, const std::string & name) const;
163
165 std::deque<std::string> listing(category categ) const;
166
168
171 bool get_saved_status() const { return saved; };
172
174
178 bool get_autosave_status() const { return autosave; };
179
181
183 void set_autosave_status(bool val) { autosave = val; };
184
186 void clear();
187
189 bool is_empty() const;
190
192 virtual void load_json(const json & source) override;
193
195 virtual json save_json() const override;
196
198 virtual void clear_json() override { clear(); };
199
200private:
201
203 // DATASTRUCTURE FOR INTERNAL DATA
204 //
205
207 struct linked_config
208 {
209 json config;
210 using_set dependency;
211
212 linked_config() {};
213 linked_config(const json & conf) { config = conf; };
214 linked_config(const json & conf, const std::set<coordinates> & deps): config(conf), dependency(deps) {};
215 };
216
218 typedef std::map<std::string, linked_config> asso;
219
221 typedef std::map<category, asso> table;
222
223 table content;
224 mutable bool saved;
225
227 // GLOBAL PROPERTIES
228 bool autosave;
229
230
231
233 // DATASTRUCTURE STORING REFERENCE
234 // FROM EXTERNAL DATA
235 //
236
237 typedef std::set<const void*> refs;
238
239 std::map<coordinates, refs> outside;
240
242
243 void init();
244
246
252 bool lookup(category cat,
253 const std::string & name,
254 asso::iterator & it,
255 table::iterator & catit) const;
256
257 void add_dependency_for(coordinates user, const using_set & referred);
258 void remove_dependency_for(coordinates user);
259 using_set get_direct_dependencies_of(coordinates user) const;
260 void check_against_cyclic_dependencies(coordinates source, const std::set<coordinates> & seen);
261
263 static constexpr const unsigned int format_version = 1;
264 static constexpr const char* myclass_id = "bibliotheque";
265
266 static constexpr const char* category_label = "category";
267 static constexpr const char* asso_label = "list";
268 static constexpr const char* config_label = "name";
269 static constexpr const char* config_def_label = "config";
270 static constexpr const char* config_depend = "used-by";
271
272 static constexpr const char* jlabel_categprop = "categories";
273 static constexpr const char* jlabel_globalprop = "global";
274
275 static constexpr const char* jlabel_autosave = "auto-save";
276
277};
278
280
281#endif
class bibliotheque
Definition: bibliotheque.hpp:56
std::set< coordinates > using_set
list of configs a given config depends on
Definition: bibliotheque.hpp:117
virtual json save_json() const override
inherited from jsoner
Definition: bibliotheque.cpp:477
virtual void load_json(const json &source) override
inherited from jsoner
Definition: bibliotheque.cpp:374
void update_config(category categ, const std::string &name, const json &config, const using_set &refs=std::set< coordinates >())
update an existing configuration for that category (name must exist)
Definition: bibliotheque.cpp:212
void delete_config(category categ, const std::string &name)
remove a configuration
Definition: bibliotheque.cpp:243
virtual void clear_json() override
inherited from jsoner
Definition: bibliotheque.hpp:198
static std::string changed(category cat)
change event per category
Definition: bibliotheque.cpp:45
static constexpr const char * default_config_name
name of default configuration for options
Definition: bibliotheque.hpp:87
static std::string category_to_string(category cat)
convert category to json used string
Definition: bibliotheque.cpp:619
bool has_config(category categ, const std::string &name) const
tells whether a given cat/config exists
Definition: bibliotheque.cpp:307
void clear()
clear all stored configurations
Definition: bibliotheque.cpp:345
void add_external_ref_to(category categ, const std::string &name, const void *from_where)
add external reference to a config (avoiding it to be deleted)
Definition: bibliotheque.cpp:175
json fetch_config(category categ, const std::string &name) const
provide a stored configuration
Definition: bibliotheque.cpp:315
static std::string category_description(category cat, bool capitalized)
provide user displayable category description
Definition: bibliotheque.cpp:712
std::deque< std::string > listing(category categ) const
list existing configurations under the provided category
Definition: bibliotheque.cpp:331
category
change event is replaced by a static method with category in argument
Definition: bibliotheque.hpp:63
@ EOE
not a valid value, used for interating in the enum
Definition: bibliotheque.hpp:83
bool get_autosave_status() const
get the autosave property
Definition: bibliotheque.hpp:178
void delete_external_ref_to(category categ, const std::string &name, const void *from_where)
remove external ref to a configuration
Definition: bibliotheque.cpp:287
void add_config(category categ, const std::string &name, const json &config, const using_set &refs=std::set< coordinates >())
add a new configuration, the name must not exist for that category
Definition: bibliotheque.cpp:141
bool is_empty() const
whether the bibliotheque has some configuration in one or more categories
Definition: bibliotheque.cpp:354
bool get_saved_status() const
get saved status
Definition: bibliotheque.hpp:171
static category string_to_category(const std::string &s)
convert json used string to category
Definition: bibliotheque.cpp:668
void set_autosave_status(bool val)
set the autosave property
Definition: bibliotheque.hpp:183
std::string display_config(category categ, const std::string &name) const
display beautifulized json data for the given category and name
Definition: bibliotheque.cpp:326
class events
Definition: events.hpp:52
class jsoner
Definition: jsoner.hpp:73
defines the event class
defines jsoner class and class exception_json
complete configuration designation
Definition: bibliotheque.hpp:108