1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
/*
Copyright © 2015-2016 Gluzskiy Alexandr (sss)
This file is part of Unknown Download Manager (UDM).
UDM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
UDM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with UDM. If not, see <http://www.gnu.org/licenses/>.
*/
#include "modules_handler.h"
#include "main.h"
#include "utilities.h"
#include <unistd.h>
#include <dlfcn.h>
#include <string>
#include <boost/filesystem.hpp>
#include <iostream>
#include "config.h"
extern runtime_config_s runtime_config;
extern core_api *module_api;
void modules_handler::load_metadata_modules(const std::string &path)
{
if(boost::filesystem::exists(path) && boost::filesystem::is_directory(path))
{
for(boost::filesystem::directory_iterator i(path), end = boost::filesystem::directory_iterator(); i != end; ++i)
{
void *lib = dlopen(i->path().generic_string().c_str(), RTLD_LAZY |RTLD_GLOBAL);
if(!lib)
{
printf("failed to open library \"%s\" with error: %s\n", i->path().c_str(), dlerror());
continue;
}
void *fptr = dlsym(lib, "udm_metadata_module_load");
if(!fptr)
{
printf("failed to find symbol \"udm_metadata_module_load\" in library %s with error %s\n", i->path().c_str(), dlerror());
dlclose(lib);
continue;
}
module_metadata_storage *(*f)(void);
f = (module_metadata_storage *(*)(void))fptr;
//TODO: aditional sanity checks
module_metadata_storage *m = f();
m->load(module_api);
sync_module_settings(m);
metadata_modules.push_back(m);
}
}
}
void modules_handler::load_downloader_modules(const std::string &path)
{
if(boost::filesystem::exists(path) && boost::filesystem::is_directory(path))
{
for(boost::filesystem::directory_iterator i(path), end = boost::filesystem::directory_iterator(); i != end; ++i)
{
void *lib = dlopen(i->path().generic_string().c_str(), RTLD_LAZY |RTLD_GLOBAL);
if(!lib)
{
printf("failed to open library \"%s\" with error: %s\n", i->path().c_str(), dlerror());
continue;
}
void *fptr = dlsym(lib, "udm_downloader_module_load");
if(!fptr)
{
printf("failed to find symbol \"udm_downloader_module_load\" in library %s with error %s\n", i->path().c_str(), dlerror());
dlclose(lib);
continue;
}
module_downloader *(*f)(void);
f = (module_downloader *(*)(void))fptr;
//TODO: aditional sanity checks
module_downloader *m = f();
m->load(module_api);
sync_module_settings(m);
downloader_modules.push_back(m);
}
}
}
std::string modules_handler::get_self_path()
{
//TODO: dynamic buffer
char buf[1024];
readlink("/proc/self/exe", buf, 1023);
std::string s (buf);
return s;
}
std::string modules_handler::list_modules()
{
std::string buf;
buf += "Installed metadata modules:\n";
buf += list_modules_single_type_internal(metadata_modules);
buf += "\nInstalled downloader modules:\n";
buf += list_modules_single_type_internal(downloader_modules);
return buf;
}
std::string modules_handler::list_modules_single_type_internal(const std::list<module_base*> &modules)
{
std::string buf;
for(auto i : modules)
{
//TODO: additional info for each module type
buf += "\tName: ";
buf += i->get_module_info().name;
buf += "\n\tDescription: " + i->get_module_info().description;
buf += "\n\tVersion: " + i->get_module_info().version;
if(runtime_config.settings.verbosity >= 1)
{
buf += "\n\tAvailable options:";
for(auto i1 : i->get_module_info().default_settings)
{
buf += "\n\t\t";
buf += i1.first;
buf += " = ";
buf += i1.second.value;
}
}
buf += "\n\n";
}
return buf;
}
void modules_handler::sync_module_settings(module_base *m)
{
//update config file with available modules settings
for(auto i : m->get_module_info().default_settings)
{
std::string setting = "modules." + m->get_module_info().name + "." + i.first;
std::string current_val = runtime_config.config_file.get<std::string>(setting, "not set");
if(current_val == "not set")
runtime_config.config_file.put(setting, i.second.value);
}
}
modules_handler::modules_handler()
{
self_path = get_self_path();
}
void modules_handler::load_modules()
{
boost::filesystem::path p(self_path);
std::string self_dir = p.parent_path().generic_string();
load_metadata_modules(self_dir + "/modules/metadata");
load_metadata_modules(replace_home_var("~/.share/udm/modules/metadata"));
load_metadata_modules("/usr/lib/udm/modules/metadata");
load_downloader_modules(self_dir + "/modules/downloaders");
load_downloader_modules(replace_home_var("~/.share/udm/modules/downloaders"));
load_downloader_modules("/usr/lib/udm/modules/downloaders");
load_modules_settings();
}
void modules_handler::on_modules_loaded()
{
for(auto i : metadata_modules)
{
if(!i->get_module_info().on_modules_loaded.empty())
i->get_module_info().on_modules_loaded();
}
for(auto i : downloader_modules)
{
if(!i->get_module_info().on_modules_loaded.empty())
i->get_module_info().on_modules_loaded();
}
}
void modules_handler::sync_downloads(std::map<int, download_internal_s> &downloads)
{
int id = downloads.size();
for(auto i : downloader_modules)
{
module_downloader *d = static_cast<module_downloader*>(i);
for(auto it : d->get_downloads())
{
downloads[id].module_name = d->get_module_info().name;
downloads[id].module_id = it.id;
id++;
}
}
}
void modules_handler::load_modules_settings()
{
for(auto i : metadata_modules)
i->set_module_settings(module_api->get_module_settings(i));
for(auto i : downloader_modules)
i->set_module_settings(module_api->get_module_settings(i));
}
std::list<module_base*> &modules_handler::get_metadata_modules()
{
return metadata_modules;
}
std::list<module_base*> &modules_handler::get_downloader_modules()
{
return downloader_modules;
}
modules_handler::~modules_handler()
{
//dtor
}
|