winix/winixd/plugins/group/groups.cpp

276 lines
6.1 KiB
C++

/*
* This file is a part of Winix
* and is distributed under the 2-Clause BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2011-2021, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <algorithm>
#include <limits>
#include "groups.h"
#include "core/log.h"
namespace Winix
{
namespace GroupItem
{
PT::Space * Groups::GetSpace()
{
return &space;
}
void Groups::Reindex()
{
set_index.clear();
group_index_tab.clear();
size_t seti = 0;
if( space.child_spaces )
{
while( seti < space.child_spaces->size() )
{
PT::Space & sp = *(*space.child_spaces)[seti];
if( sp.name )
{
if( set_index.find(*sp.name) == set_index.end() )
{
set_index[*sp.name] = seti;
group_index_tab.push_back(GroupIndex());
ReindexGroups(group_index_tab.back(), sp);
seti += 1;
}
else
{
slog << logerror << "set: " << sp.name << " already defined (skipping)" << logend;
space.remove_child_space(seti);
}
}
}
}
}
void Groups::ReindexGroups(GroupIndex & group_index, PT::Space & set)
{
size_t i, v;
if( set.child_spaces )
{
// loop through all groups in the set
for( i=0 ; i < set.child_spaces->size() ; ++i )
{
PT::Space & group = *(*set.child_spaces)[i];
// !! IMPROVE ME will be safer to copy the value out
// if we used accidently the group.Text later the key
// would be overwritten
std::wstring key = group.to_wstr(L"key", L"value");
if( group.child_spaces )
{
// loop through all values in the group
for(v=0 ; v<group.child_spaces->size() ; )
{
std::wstring * vali = (*group.child_spaces)[v]->get_wstr(key.c_str());
if( vali )
{
GroupIndex::iterator g = group_index.find(*vali);
if( g == group_index.end() )
{
group_index[*vali] = i;
v += 1;
}
else
{
slog << logwarning << "set: " << set.name << " has a group with a duplicated value: "
<< *vali << " (skipping)" << logend;
group.remove_child_space(v);
}
}
else
{
log << log1 << "key: " << key << " was not found" << logend;
slog << logwarning << "set: " << set.name
<< " has a group without a value (skipping)" << logend;
group.remove_child_space(v);
}
}
}
SortValues(group);
}
}
}
void Groups::SortValues(PT::Space & group)
{
sort_by = group.to_wstr(L"sort_by");
sort_asc = group.is_equal(L"sort_asc", L"true");
if( !sort_by.empty() && group.child_spaces )
{
group.to_list(L"sort", sort_value, true);
std::sort(group.child_spaces->begin(), group.child_spaces->end(), SortFunHelper(this));
}
}
bool Groups::SortFunHelper::operator()(PT::Space * sp1, PT::Space * sp2)
{
const std::wstring * val1 = sp1->get_wstr(groups->sort_by.c_str());
const std::wstring * val2 = sp2->get_wstr(groups->sort_by.c_str());
if( !val1 || !val2 )
return false;
if( groups->sort_asc )
return SortValue(*val1) < SortValue(*val2);
else
return SortValue(*val1) > SortValue(*val2);
}
size_t Groups::SortFunHelper::SortValue(const std::wstring & val)
{
for(size_t i=0 ; i<groups->sort_value.size() ; ++i)
if( val == groups->sort_value[i] )
return i;
return std::numeric_limits<size_t>::max();
}
bool Groups::Find(const std::wstring & set, const std::wstring & value, size_t & seti, size_t & groupi)
{
SetIndex::iterator i = set_index.find(set);
if( i != set_index.end() )
{
if( i->second < group_index_tab.size() )
{
GroupIndex::iterator viter = group_index_tab[i->second].find(value);
if( viter != group_index_tab[i->second].end() )
{
seti = i->second;
groupi = viter->second;
return true;
}
}
}
return false;
}
const std::wstring & Groups::GetOption(size_t seti, size_t groupi, size_t valuei, const wchar_t * option)
{
if( space.child_spaces && seti < space.child_spaces->size() )
{
PT::Space & groups = *(*space.child_spaces)[seti];
if( groups.child_spaces && groupi < groups.child_spaces->size() )
{
PT::Space & value = *(*groups.child_spaces)[groupi];
if( value.child_spaces && valuei < value.child_spaces->size() )
{
const std::wstring * res = (*value.child_spaces)[valuei]->get_wstr(option);
if( res )
return *res;
}
}
}
return empty_str;
}
const std::wstring & Groups::GetOption(size_t seti, size_t groupi, size_t valuei, const std::wstring & option)
{
return GetOption(seti, groupi, valuei, option.c_str());
}
size_t Groups::Size(size_t seti, size_t groupi)
{
if( space.child_spaces && seti < space.child_spaces->size() )
{
PT::Space & groups = *(*space.child_spaces)[seti];
if( groups.child_spaces && groupi < groups.child_spaces->size() )
return (*groups.child_spaces)[groupi]->child_spaces->size();
}
return 0;
}
void Groups::Clear()
{
space.clear();
set_index.clear();
group_index_tab.clear();
}
}
} // namespace Winix