301 lines
5.8 KiB
C++
Executable File
301 lines
5.8 KiB
C++
Executable File
/*
|
||
* This file is a part of Winix
|
||
* and is not publicly distributed
|
||
*
|
||
* Copyright (c) 2010, Tomasz Sowa
|
||
* All rights reserved.
|
||
*
|
||
*/
|
||
|
||
#include <algorithm>
|
||
#include <ctime>
|
||
#include "ticketinfo.h"
|
||
#include "core/error.h"
|
||
#include "core/log.h"
|
||
#include "core/misc.h"
|
||
|
||
|
||
|
||
namespace Ticket
|
||
{
|
||
|
||
|
||
|
||
TicketInfo::TicketInfo()
|
||
{
|
||
Clear();
|
||
}
|
||
|
||
|
||
void TicketInfo::SetTDb(TDb * ptdb)
|
||
{
|
||
tdb = ptdb;
|
||
}
|
||
|
||
|
||
void TicketInfo::SetDb(Db * pdb)
|
||
{
|
||
db = pdb;
|
||
}
|
||
|
||
|
||
void TicketInfo::SetSystem(System * psystem)
|
||
{
|
||
system = psystem;
|
||
}
|
||
|
||
|
||
void TicketInfo::SetRequest(Request * prequest)
|
||
{
|
||
request = prequest;
|
||
}
|
||
|
||
|
||
void TicketInfo::Clear()
|
||
{
|
||
item.Clear();
|
||
is_ticket = false;
|
||
ticket.Clear();
|
||
ticket_tab.clear();
|
||
|
||
cur_conf_wrap = &cur_conf_wrap_empty;
|
||
cur_conf = &cur_conf_empty;
|
||
}
|
||
|
||
|
||
void TicketInfo::ReadTicket(long dir_id)
|
||
{
|
||
if( tdb->GetTicket(dir_id, ticket) == WINIX_ERR_OK )
|
||
{
|
||
is_ticket = true;
|
||
//db->GetItemById(ticket.item_id, item);
|
||
}
|
||
}
|
||
|
||
|
||
bool TicketInfo::SortTicketsFun(const Ticket & t1, const Ticket & t2)
|
||
{
|
||
return t1.sort_id > t2.sort_id;
|
||
}
|
||
|
||
|
||
void TicketInfo::SortTickets()
|
||
{
|
||
std::vector<Ticket>::iterator i;
|
||
|
||
for(i=ticket_tab.begin() ; i!=ticket_tab.end() ; ++i)
|
||
{
|
||
Item * dir = system->dirs.GetDir(i->dir_id);
|
||
i->sort_id = ( dir ) ? (unsigned long)Time(dir->date_creation) : 0;
|
||
}
|
||
|
||
std::sort(ticket_tab.begin(), ticket_tab.end(), SortTicketsFun);
|
||
}
|
||
|
||
|
||
|
||
void TicketInfo::MarkAllConfToDelete()
|
||
{
|
||
ConfTab::iterator i = conf_tab.begin();
|
||
|
||
for( ; i!=conf_tab.end() ; ++i)
|
||
i->second.to_delete = true;
|
||
}
|
||
|
||
|
||
void TicketInfo::DeleteAllMarkedConf()
|
||
{
|
||
ConfTab::iterator inext;
|
||
ConfTab::iterator i = conf_tab.begin();
|
||
|
||
while( i != conf_tab.end() )
|
||
{
|
||
inext = i;
|
||
++inext;
|
||
|
||
if( i->second.to_delete )
|
||
{
|
||
log << log3 << "Ticket: deleting ticket conf for dir id: " << i->first << logend;
|
||
conf_tab.erase(i);
|
||
}
|
||
|
||
i = inext;
|
||
}
|
||
}
|
||
|
||
|
||
bool TicketInfo::GetConfContent(const std::wstring & path)
|
||
{
|
||
long path_dir_id;
|
||
|
||
if( system->dirs.AnalyzePath(path, path_dir_id, path_dir, path_file) != 0 )
|
||
{
|
||
log << log1 << "Ticket: there is no file: " << path << logend;
|
||
return false;
|
||
}
|
||
|
||
Error err = db->GetItem(path_dir_id, path_file, item_conf);
|
||
|
||
if( err == WINIX_ERR_NO_ITEM )
|
||
{
|
||
log << log1 << "Ticket: there is no file: " << path << " in the database" << logend;
|
||
return false;
|
||
}
|
||
|
||
if( err != WINIX_ERR_OK )
|
||
{
|
||
log << log1 << "Ticket: db problem with fetching: " << path << logend;
|
||
return false;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
|
||
bool TicketInfo::ParseTicketConf(long mount_dir_id, const std::wstring & path)
|
||
{
|
||
log << log3 << "Ticket: parsing conf file: " << path << logend;
|
||
|
||
int code = ticket_parser.Parse(item_conf.content, conf_tab[mount_dir_id].conf);
|
||
conf_tab[mount_dir_id].file_name = path;
|
||
|
||
return code == WINIX_TICKET_ERR_OK;
|
||
}
|
||
|
||
|
||
// if skip_existing_configs is true then only new config files will be parsed
|
||
void TicketInfo::ReadTicketConf(Mounts & mounts, bool skip_existing_configs)
|
||
{
|
||
Mounts::MountTab::const_iterator i;
|
||
const Mounts::MountTab * mtab = mounts.GetMountTab();
|
||
|
||
// loop through all mount points
|
||
for(i=mtab->begin() ; i!=mtab->end() ; ++i)
|
||
{
|
||
const Mount & mount = i->second;
|
||
|
||
if( mount.param[mount_par_ticket_conf].defined &&
|
||
mount.param[mount_par_ticket_conf].arg.size() == 1 )
|
||
{
|
||
const std::wstring & file_name = mount.param[mount_par_ticket_conf].arg[0];
|
||
ConfTab::iterator c = conf_tab.find(mount.dir_id);
|
||
bool exists = (c != conf_tab.end() && c->second.file_name == file_name);
|
||
|
||
if( exists )
|
||
c->second.to_delete = false;
|
||
|
||
if( !(skip_existing_configs && exists) )
|
||
{
|
||
if( GetConfContent(file_name) )
|
||
{
|
||
if( !ParseTicketConf(mount.dir_id, file_name) )
|
||
conf_tab[mount.dir_id].to_delete = true;
|
||
}
|
||
else
|
||
{
|
||
if( exists )
|
||
c->second.to_delete = true;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
|
||
// if skip_existing_configs is true then only new config files will be parsed
|
||
void TicketInfo::ReadTicketConf(bool skip_existing_configs)
|
||
{
|
||
MarkAllConfToDelete();
|
||
ReadTicketConf(system->mounts, skip_existing_configs);
|
||
DeleteAllMarkedConf();
|
||
}
|
||
|
||
|
||
void TicketInfo::FindCurrentConf()
|
||
{
|
||
if( !system->mounts.pmount )
|
||
return;
|
||
|
||
long dir_id = system->mounts.pmount->dir_id;
|
||
|
||
ConfTab::iterator i = conf_tab.find(dir_id);
|
||
|
||
if( i != conf_tab.end() )
|
||
{
|
||
cur_conf_wrap = &i->second;
|
||
cur_conf = &i->second.conf;
|
||
}
|
||
}
|
||
|
||
|
||
bool TicketInfo::CheckMinMaxValue(const TicketConf::TicketItem & ticket_item, Ticket::TicketParam & par)
|
||
{
|
||
if( ticket_item.type == TicketConf::TicketItem::TypeInteger )
|
||
{
|
||
if( par.value < ticket_item.integer_min )
|
||
par.value = ticket_item.integer_min;
|
||
|
||
if( par.value > ticket_item.integer_max )
|
||
par.value = ticket_item.integer_max;
|
||
}
|
||
else
|
||
if( ticket_item.type == TicketConf::TicketItem::TypeSelect )
|
||
{
|
||
for(size_t a=0 ; a<ticket_item.select.size() ; ++a)
|
||
{
|
||
if( ticket_item.select[a].id == par.value )
|
||
return true;
|
||
}
|
||
|
||
log << log1 << "Ticket: incorrect select's value, param: "
|
||
<< par.param << ", value: " << par.value << " (ignored)" << logend;
|
||
|
||
return false;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
|
||
bool TicketInfo::CheckMinMaxValue(Ticket::TicketParam & par)
|
||
{
|
||
for(size_t i=0 ; i<cur_conf->tab.size() ; ++i)
|
||
{
|
||
if( par.param == cur_conf->tab[i].id )
|
||
return CheckMinMaxValue(cur_conf->tab[i], par);
|
||
}
|
||
|
||
log << log1 << "Ticket: unknown param: " << par.param << " (ignored)" << logend;
|
||
|
||
return false;
|
||
}
|
||
|
||
|
||
void TicketInfo::ReadTicketParams()
|
||
{
|
||
PostTab::iterator i;
|
||
const wchar_t parstr[] = L"ticketparam"; // !! dodac do konfiga? i szablony tez niech bior<6F> z konfiga
|
||
size_t parlen = sizeof(parstr) / sizeof(wchar_t) - 1;
|
||
Ticket::TicketParam param;
|
||
|
||
ticket.par_tab.clear();
|
||
|
||
for(i=request->post_tab.begin() ; i!=request->post_tab.end() ; ++i)
|
||
{
|
||
if( IsSubString(parstr, i->first.c_str()) )
|
||
{
|
||
param.param = Toi(i->first.c_str() + parlen);
|
||
param.value = Toi(i->second);
|
||
|
||
if( CheckMinMaxValue(param) )
|
||
ticket.par_tab.push_back(param);
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
} // namespace
|