/*
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 .
*/
#include "main.h"
#include
#include
#include
#include
#include
#include
#include
std::string replace_home_var(const std::string &path)
{
std::string home = getenv("HOME"), tmp_path = path;
for(std::string::size_type p1 = tmp_path.find("~"); p1 != std::string::npos; p1 = tmp_path.find("~", p1))
tmp_path.replace(p1, 1, home);
return tmp_path;
}
void storage_impl::on_modules_loaded()
{
//just checking if we working fine via core
//test set
/* {
std::string d = "some string\ndfjoghdfyigehygerityertoweuyrtghyuewitgweyuitgweiurtgeiutrerytiewgytyewuirtgewrgyutygergytiu";
std::vector data;
for(auto i = d.begin(), end = d.end(); i != end; ++i)
{
data.push_back(*i);
}
api->metadata_set(this, "test_setting", data);
} */
//test get
/* {
std::vector data;
api->metadata_get(this, "test_setting", data);
std::string s;
for(auto i = data.begin(), end = data.end(); i != end; ++i)
{
s.push_back(*i);
}
std::cout<<"we have read:\n"<metadata_remove(this, "test_setting");
} */
}
void storage_impl::load(core_api *a)
{
api = a;
info.name = "flat_files_metadata";
info.description = "this module provide metadata storage in flat files";
info.version = "0.0.0.1draft";
info.default_settings["data_path"].value = "~/.local/share/udm/metadata";
info.on_modules_loaded = boost::bind(&storage_impl::on_modules_loaded, this); //optional definition of function which is called after all modules loaded
//std::cout<<"flat_files metadata module succesfully loaded\n"; //working fine
}
const module_info_base &storage_impl::get_module_info()
{
return info;
}
void storage_impl::set_module_settings(const std::map &settings)
{
this->settings = settings;
parsed_data_path = replace_home_var(this->settings["data_path"].value);
}
bool storage_impl::set(const std::string &module_name, const std::string &setting_name, const std::vector &data)
{
//print data
/* std::cout<<"printing data in metadata_flat_files set api:\n";
for(auto i = data.begin(), end = data.end(); i != end; ++i)
{
std::cout<<*i;
}
std::cout< &data)
{
std::string in_file_path = parsed_data_path;
in_file_path += "/";
in_file_path += module_name;
in_file_path += "/";
in_file_path += setting_name;
if(!boost::filesystem::exists(in_file_path) || !boost::filesystem::is_regular(in_file_path))
{
//data does not exists
return false;
}
std::ifstream is(in_file_path, std::ios::binary);
boost::archive::binary_iarchive ar(is);
ar>>data;
return true;
}
bool storage_impl::remove(const std::string &module_name, const std::string &setting_name)
{
//TODO
std::string file_path = parsed_data_path;
file_path += "/";
file_path += module_name;
file_path += "/";
file_path += setting_name;
if(!boost::filesystem::exists(file_path) || !boost::filesystem::is_regular(file_path))
{
return false;
}
boost::filesystem::remove(file_path);
return true;
}
std::list storage_impl::setting_list(const std::string &module_name)
{
std::list l;
std::string settings_dir = parsed_data_path;
settings_dir += "/";
settings_dir += module_name;
for(auto i = boost::filesystem::directory_iterator(settings_dir), end = boost::filesystem::directory_iterator(); i != end; ++i)
{
l.push_back(i->path().string().substr(settings_dir.length() + 1));
}
return l;
}
storage_impl::storage_impl()
{
}
extern "C" module_metadata_storage* udm_metadata_module_load()
{
return new storage_impl;
}