winix/winixd/plugins/group/groups.cpp

255 lines
5.5 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-2018, 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 <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;
while( seti < space.spaces.size() )
{
const std::wstring & name = space.spaces[seti]->name;
if( set_index.find(name) == set_index.end() )
{
set_index[name] = seti;
group_index_tab.push_back(GroupIndex());
ReindexGroups(group_index_tab.back(), *space.spaces[seti]);
seti += 1;
}
else
{
slog << logerror << "set: " << name << " already defined (skipping)" << logend;
space.spaces.erase(space.spaces.begin() + seti);
}
}
}
void Groups::ReindexGroups(GroupIndex & group_index, PT::Space & set)
{
size_t i, v;
// loop through all groups in the set
for( i=0 ; i < set.spaces.size() ; ++i )
{
PT::Space & group = *set.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
const std::wstring & key = group.Text(L"key", L"value");
// loop through all values in the group
for(v=0 ; v<group.spaces.size() ; )
{
std::wstring * vali = group.spaces[v]->GetFirstValue(key);
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.spaces.erase(group.spaces.begin() + v);
}
}
else
{
log << log1 << "key: " << key << " was not found" << logend;
slog << logwarning << "set: " << set.name
<< " has a group without a value (skipping)" << logend;
group.spaces.erase(group.spaces.begin() + v);
}
}
SortValues(group);
}
}
void Groups::SortValues(PT::Space & group)
{
sort_by = group.Text(L"sort_by");
sort_asc = (group.Text(L"sort_asc", L"true") == L"true");
if( !sort_by.empty() )
{
group.ListText(L"sort", sort_value);
std::sort(group.spaces.begin(), group.spaces.end(), SortFunHelper(this));
}
}
bool Groups::SortFunHelper::operator()(PT::Space * sp1, PT::Space * sp2)
{
const std::wstring & val1 = sp1->Text(groups->sort_by, L"");
const std::wstring & val2 = sp2->Text(groups->sort_by, L"");
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( seti < space.spaces.size() )
{
PT::Space & groups = *space.spaces[seti];
if( groupi < groups.spaces.size() )
{
PT::Space & value = *groups.spaces[groupi];
if( valuei < value.spaces.size() )
return value.spaces[valuei]->TextRef(option);
}
}
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( seti < space.spaces.size() )
{
PT::Space & groups = *space.spaces[seti];
if( groupi < groups.spaces.size() )
return groups.spaces[groupi]->spaces.size();
}
return 0;
}
void Groups::Clear()
{
space.Clear();
set_index.clear();
group_index_tab.clear();
}
}
} // namespace Winix