winix/plugins/group/groupinfo.cpp

179 lines
3.3 KiB
C++
Executable File

/*
* This file is a part of Winix
* and is not publicly distributed
*
* Copyright (c) 2011-2012, Tomasz Sowa
* All rights reserved.
*
*/
#include "groupinfo.h"
#include "core/log.h"
#include "core/system.h"
namespace GroupItem
{
GroupInfo::GroupInfo()
{
mount_par_group_conf = -1;
}
void GroupInfo::SetSystem(System * psystem)
{
system = psystem;
}
void GroupInfo::SetConfig(Config * pconfig)
{
config = pconfig;
}
Groups * GroupInfo::FindGroups(long dir_id)
{
GroupsWrap::iterator i = groups_wrap.find(dir_id);
if( i == groups_wrap.end() )
return 0;
return &(i->second.groups);
}
bool GroupInfo::ParseGroups(const std::wstring & str, Groups & groups)
{
groups.Clear();
PT::Space & space = *groups.GetSpace();
conf_parser.SetSpace(space);
conf_parser.UTF8(config->utf8);
if( conf_parser.ParseString(str) == PT::SpaceParser::ok )
{
groups.Reindex();
}
else
{
slog << logerror << "Syntax error in line: " << conf_parser.line << logend;
log << log1 << "Syntax error in line: " << conf_parser.line << logend;
groups.Clear();
}
return conf_parser.status == PT::SpaceParser::ok;
}
void GroupInfo::MarkAllGroupsToDelete()
{
GroupsWrap::iterator i = groups_wrap.begin();
for( ; i != groups_wrap.end() ; ++i)
i->second.to_delete = true;
}
void GroupInfo::DeleteAllMarkedGroups()
{
GroupsWrap::iterator inext;
GroupsWrap::iterator i = groups_wrap.begin();
while( i != groups_wrap.end() )
{
inext = i;
++inext;
if( i->second.to_delete )
{
log << log3 << "GroupItem: deleting group for dir id: " << i->first << logend;
groups_wrap.erase(i);
}
i = inext;
}
}
bool GroupInfo::GetConfContent(const std::wstring & path)
{
int status = system->FollowAllLinks(path, config_dir_tab, config_file, false, false, false);
if( status != 1 )
{
log << log1 << "GroupItem: problem with reading a config file: " << path << ", status: " << status << logend;
return false;
}
return true;
}
// if skip_existing_configs is true then only new config files will be parsed
void GroupInfo::ReadGroupsConf(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_group_conf].defined &&
mount.param[mount_par_group_conf].arg.size() == 1 )
{
const std::wstring & file_name = mount.param[mount_par_group_conf].arg[0];
GroupsWrap::iterator c = groups_wrap.find(mount.dir_id);
bool exists = (c != groups_wrap.end() && c->second.file_name == file_name);
if( exists )
c->second.to_delete = false;
if( !(skip_existing_configs && exists) )
{
if( GetConfContent(file_name) )
{
log << log3 << "GroupItem: parsing conf file: " << config_file.url << logend;
groups_wrap[mount.dir_id].file_name = file_name;
if( !ParseGroups(config_file.content, groups_wrap[mount.dir_id].groups) )
groups_wrap[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 GroupInfo::ReadGroupsConfigs(bool skip_existing_configs)
{
MarkAllGroupsToDelete();
ReadGroupsConf(system->mounts, skip_existing_configs);
DeleteAllMarkedGroups();
}
}