2010-09-30 00:41:34 +02:00
|
|
|
|
/*
|
|
|
|
|
* This file is a part of Winix
|
|
|
|
|
* and is not publicly distributed
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2010, Tomasz Sowa
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2010-09-30 22:58:20 +02:00
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <ctime>
|
2010-09-29 23:24:34 +02:00
|
|
|
|
#include "ticketinfo.h"
|
|
|
|
|
#include "core/error.h"
|
2010-10-11 22:42:49 +02:00
|
|
|
|
#include "core/log.h"
|
2010-10-19 02:31:20 +02:00
|
|
|
|
#include "core/misc.h"
|
|
|
|
|
|
2010-09-29 23:24:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace Ticket
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TicketInfo::TicketInfo()
|
|
|
|
|
{
|
|
|
|
|
Clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void TicketInfo::SetTDb(TDb * ptdb)
|
|
|
|
|
{
|
|
|
|
|
tdb = ptdb;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void TicketInfo::SetDb(Db * pdb)
|
|
|
|
|
{
|
|
|
|
|
db = pdb;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-09-30 22:58:20 +02:00
|
|
|
|
void TicketInfo::SetSystem(System * psystem)
|
|
|
|
|
{
|
|
|
|
|
system = psystem;
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-19 02:31:20 +02:00
|
|
|
|
|
|
|
|
|
void TicketInfo::SetRequest(Request * prequest)
|
|
|
|
|
{
|
|
|
|
|
request = prequest;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-09-29 23:24:34 +02:00
|
|
|
|
void TicketInfo::Clear()
|
|
|
|
|
{
|
|
|
|
|
item.Clear();
|
|
|
|
|
is_ticket = false;
|
|
|
|
|
ticket.Clear();
|
|
|
|
|
ticket_tab.clear();
|
2010-10-19 02:31:20 +02:00
|
|
|
|
|
|
|
|
|
cur_conf_wrap = &cur_conf_wrap_empty;
|
|
|
|
|
cur_conf = &cur_conf_empty;
|
2010-09-29 23:24:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void TicketInfo::ReadTicket(long dir_id)
|
|
|
|
|
{
|
2010-10-19 02:31:20 +02:00
|
|
|
|
if( tdb->GetTicket(dir_id, ticket) == WINIX_ERR_OK )
|
2010-09-29 23:24:34 +02:00
|
|
|
|
{
|
|
|
|
|
is_ticket = true;
|
2010-10-19 02:31:20 +02:00
|
|
|
|
//db->GetItemById(ticket.item_id, item);
|
2010-09-29 23:24:34 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-09-30 22:58:20 +02:00
|
|
|
|
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);
|
2010-10-24 01:12:47 +02:00
|
|
|
|
i->sort_id = ( dir ) ? (unsigned long)Time(dir->date_creation) : 0;
|
2010-09-30 22:58:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::sort(ticket_tab.begin(), ticket_tab.end(), SortTicketsFun);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-10-11 22:42:49 +02:00
|
|
|
|
|
|
|
|
|
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::string & 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::string & 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::string & 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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-10-19 02:31:20 +02:00
|
|
|
|
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 char parstr[] = "ticketparam"; // !! dodac do konfiga? i szablony tez niech bior<6F> z konfiga
|
|
|
|
|
size_t parlen = sizeof(parstr) / sizeof(char) - 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 = atoi(i->first.c_str() + parlen);
|
|
|
|
|
param.value = atoi(i->second.c_str());
|
|
|
|
|
|
|
|
|
|
if( CheckMinMaxValue(param) )
|
|
|
|
|
ticket.par_tab.push_back(param);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-10-11 22:42:49 +02:00
|
|
|
|
|
2010-09-29 23:24:34 +02:00
|
|
|
|
} // namespace
|