winix/plugins/ticket/ticketinfo.cpp

205 lines
3.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"
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::Clear()
{
item.Clear();
is_ticket = false;
ticket.Clear();
ticket_tab.clear();
}
void TicketInfo::ReadTicket(long dir_id)
{
if( tdb->GetTicketByDirId(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)mktime(&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::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();
}
} // namespace