1
/* This file is part of Strigi Desktop Search
3
* Copyright (C) 2006 Flavio Castelli <flavio.castelli@gmail.com>
5
* This library is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU Library General Public
7
* License as published by the Free Software Foundation; either
8
* version 2 of the License, or (at your option) any later version.
10
* This library is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* Library General Public License for more details.
15
* You should have received a copy of the GNU Library General Public License
16
* along with this library; see the file COPYING.LIB. If not, write to
17
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18
* Boston, MA 02110-1301, USA.
21
#include "filtermanager.h"
24
#include "strigilogging.h"
30
FilterManager::FilterManager()
32
pthread_mutex_init(&m_mutex, NULL);
35
FilterManager::~ FilterManager()
39
pthread_mutex_destroy (&m_mutex);
42
void FilterManager::clearRules()
44
for (unsigned int i = 0; i < m_rules.size(); i++)
50
void FilterManager::setConfFile (string& patternRules, string& pathRules)
52
m_patternFile = patternRules;
53
m_pathFile = pathRules;
56
// TODO: remove when weì'll have a single configuration file
59
loadFilter(patternRules, PatternFilter::RTTI);
60
loadFilter(pathRules, PathFilter::RTTI);
63
void FilterManager::loadFilter(string& file, unsigned int filterRTTI)
69
confFile.open(file.c_str(), ios::in);
72
// TODO: restore when weì'll have a single configuration file
75
if (confFile.is_open())
77
pthread_mutex_lock (&m_mutex);
80
while (!confFile.eof())
82
confFile.getline(buffer, 500);
89
case PathFilter::RTTI:
90
m_rules.push_back (new PathFilter (string(buffer)));
91
STRIGI_LOG_DEBUG ("strigi.filtermanager.loadFilter", "added path filter: |" + string(buffer) + "|")
93
case PatternFilter::RTTI:
94
m_rules.push_back (new PatternFilter (string(buffer)));
95
STRIGI_LOG_DEBUG ("strigi.filtermanager.loadFilter", "added pattern filter: |" + string(buffer) + "|")
98
STRIGI_LOG_ERROR ("strigi.filtermanager.loadFilter", "unknown rule RTTI")
104
pthread_mutex_unlock (&m_mutex);
109
snprintf (buffer, 500*sizeof (char), "%i", m_rules.size());
111
STRIGI_LOG_INFO ("strigi.filtermanager", "added " + string(buffer) + " filtering rules")
114
void FilterManager::saveFilter()
118
pathFile.open(m_pathFile.c_str(), ios::out | ios::trunc);
119
patternFile.open(m_patternFile.c_str(), ios::out | ios::trunc);
121
pthread_mutex_lock (&m_mutex);
123
// TODO: fix when we'll use a single conf file
124
if (pathFile.is_open() && patternFile.is_open())
126
for (vector<Filter*>::iterator iter = m_rules.begin(); iter != m_rules.end(); iter++)
128
Filter* filter = *iter;
130
switch (filter->rtti())
132
case PathFilter::RTTI:
133
pathFile << filter->rule() << endl;
135
case PatternFilter::RTTI:
136
patternFile << filter->rule() << endl;
139
STRIGI_LOG_ERROR ("strigi.filtermanager.saveFilter", "unknown rule RTTI")
147
STRIGI_LOG_DEBUG ("strigi.filtermanager", "successfully saved filtering rules")
150
STRIGI_LOG_ERROR ("strigi.filtermanager.saveFilter", "unable to save filtering rules");
152
pthread_mutex_unlock (&m_mutex);
155
bool FilterManager::findMatch(const char* text)
158
return findMatch (t);
161
bool FilterManager::findMatch (string& text)
163
pthread_mutex_lock (&m_mutex);
165
for (vector<Filter*>::iterator iter = m_rules.begin(); iter != m_rules.end(); iter++)
167
Filter* filter = *iter;
168
if (filter->match (text))
170
pthread_mutex_unlock (&m_mutex);
175
pthread_mutex_unlock (&m_mutex);
176
STRIGI_LOG_DEBUG ("strigi.filtermanager", text + " didn't match any pattern")
180
multimap<int,string> FilterManager::getFilteringRules()
182
multimap<int,string> rules;
184
for (vector<Filter*>::iterator iter = m_rules.begin(); iter != m_rules.end(); iter++)
186
Filter* filter = *iter;
187
rules.insert(make_pair (int(filter->rtti()),filter->rule()));
193
void FilterManager::setFilteringRules(const multimap<int, string>& rules)
197
map<int,string>::const_iterator iter;
198
for (iter = rules.begin(); iter != rules.end(); iter++)
202
case PathFilter::RTTI:
203
m_rules.push_back (new PathFilter (iter->second));
205
case PatternFilter::RTTI:
206
m_rules.push_back (new PatternFilter (iter->second));
209
STRIGI_LOG_ERROR ("strigi.filtermanager.setFilteringRules", "unknown rule RTTI")