~ubuntu-branches/ubuntu/feisty/strigi/feisty-backports

« back to all changes in this revision

Viewing changes to src/daemon/filtermanager.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Fathi Boudra
  • Date: 2006-11-12 19:23:58 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20061112192358-bopz8iu9cr8bosyc
Tags: 0.3.9-1
* New upstream release
* Remove patch to inotify support option, merged upstream
* Add poppler-utils and wv as Depends
* Include utils.mk to rules
* Add deepfind, deepgrep, luceneindexer and xmlindexer to
  strigi-daemon.install
* Update strigi-daemon.lintian-overrides for 0.3.9 version

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* This file is part of Strigi Desktop Search
2
 
 *
3
 
 * Copyright (C) 2006 Flavio Castelli <flavio.castelli@gmail.com>
4
 
 *
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.
9
 
 *
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.
14
 
 *
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.
19
 
 */
20
 
 
21
 
#include "filtermanager.h"
22
 
 
23
 
#include "filters.h"
24
 
#include "strigilogging.h"
25
 
 
26
 
#include <fstream>
27
 
 
28
 
using namespace std;
29
 
 
30
 
FilterManager::FilterManager()
31
 
{
32
 
    pthread_mutex_init(&m_mutex, NULL);
33
 
}
34
 
 
35
 
FilterManager::~ FilterManager()
36
 
{
37
 
    saveFilter();
38
 
    clearRules();
39
 
    pthread_mutex_destroy (&m_mutex);
40
 
}
41
 
 
42
 
void FilterManager::clearRules()
43
 
{
44
 
    for (unsigned int i = 0; i < m_rules.size(); i++)
45
 
        delete m_rules[i];
46
 
    
47
 
    m_rules.clear();
48
 
}
49
 
 
50
 
void FilterManager::setConfFile (string& patternRules, string& pathRules)
51
 
{
52
 
    m_patternFile = patternRules;
53
 
    m_pathFile = pathRules;
54
 
   
55
 
    // clear old rules
56
 
    // TODO: remove when weì'll have a single configuration file
57
 
    clearRules();
58
 
    
59
 
    loadFilter(patternRules, PatternFilter::RTTI);
60
 
    loadFilter(pathRules, PathFilter::RTTI);
61
 
}
62
 
 
63
 
void FilterManager::loadFilter(string& file, unsigned int filterRTTI)
64
 
{
65
 
    fstream confFile;
66
 
    string rule;
67
 
    char buffer [500];
68
 
   
69
 
    confFile.open(file.c_str(), ios::in);
70
 
   
71
 
    // clear old rules
72
 
    // TODO: restore when weì'll have a single configuration file
73
 
    //clearRules();
74
 
   
75
 
    if (confFile.is_open())
76
 
    {
77
 
        pthread_mutex_lock (&m_mutex);
78
 
       
79
 
        // read filter rules
80
 
        while (!confFile.eof())
81
 
        {
82
 
            confFile.getline(buffer, 500);
83
 
            rule = buffer;
84
 
            
85
 
            if (rule.size() > 0)
86
 
            {
87
 
                switch (filterRTTI)
88
 
                {
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) + "|")
92
 
                        break;
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) + "|")
96
 
                        break;
97
 
                    default:
98
 
                        STRIGI_LOG_ERROR ("strigi.filtermanager.loadFilter", "unknown rule RTTI")
99
 
                        break;
100
 
                }
101
 
            }
102
 
        }
103
 
       
104
 
        pthread_mutex_unlock (&m_mutex);
105
 
       
106
 
        confFile.close();
107
 
    }
108
 
   
109
 
    snprintf (buffer, 500*sizeof (char), "%i", m_rules.size());
110
 
   
111
 
    STRIGI_LOG_INFO ("strigi.filtermanager", "added " + string(buffer) + " filtering rules")
112
 
}
113
 
 
114
 
void FilterManager::saveFilter()
115
 
{
116
 
    fstream pathFile;
117
 
    fstream patternFile;
118
 
    pathFile.open(m_pathFile.c_str(), ios::out | ios::trunc);
119
 
    patternFile.open(m_patternFile.c_str(), ios::out | ios::trunc);
120
 
   
121
 
    pthread_mutex_lock (&m_mutex);
122
 
   
123
 
    // TODO: fix when we'll use a single conf file
124
 
    if (pathFile.is_open() && patternFile.is_open())
125
 
    {
126
 
        for (vector<Filter*>::iterator iter = m_rules.begin(); iter != m_rules.end(); iter++)
127
 
        {
128
 
            Filter* filter = *iter;
129
 
 
130
 
            switch (filter->rtti())
131
 
            {
132
 
                case PathFilter::RTTI:
133
 
                    pathFile << filter->rule() << endl;
134
 
                    break;
135
 
                case PatternFilter::RTTI:
136
 
                    patternFile << filter->rule() << endl;
137
 
                    break;
138
 
                default:
139
 
                    STRIGI_LOG_ERROR ("strigi.filtermanager.saveFilter", "unknown rule RTTI")
140
 
                    break;
141
 
            }
142
 
        }
143
 
       
144
 
        patternFile.close();
145
 
        pathFile.close();
146
 
       
147
 
        STRIGI_LOG_DEBUG ("strigi.filtermanager", "successfully saved filtering rules")
148
 
    }
149
 
    else
150
 
        STRIGI_LOG_ERROR ("strigi.filtermanager.saveFilter", "unable to save filtering rules");
151
 
   
152
 
    pthread_mutex_unlock (&m_mutex);
153
 
}
154
 
 
155
 
bool FilterManager::findMatch(const char* text)
156
 
{
157
 
    string t (text);
158
 
    return findMatch (t);
159
 
}
160
 
 
161
 
bool FilterManager::findMatch (string& text)
162
 
{
163
 
    pthread_mutex_lock (&m_mutex);
164
 
   
165
 
    for (vector<Filter*>::iterator iter = m_rules.begin(); iter != m_rules.end(); iter++)
166
 
    {
167
 
        Filter* filter = *iter;
168
 
        if (filter->match (text))
169
 
        {
170
 
            pthread_mutex_unlock (&m_mutex);
171
 
            return true;
172
 
        }
173
 
    }
174
 
   
175
 
    pthread_mutex_unlock (&m_mutex);
176
 
    STRIGI_LOG_DEBUG ("strigi.filtermanager", text + " didn't match any pattern")
177
 
    return false;
178
 
}
179
 
 
180
 
multimap<int,string> FilterManager::getFilteringRules()
181
 
{
182
 
    multimap<int,string> rules;
183
 
    
184
 
    for (vector<Filter*>::iterator iter = m_rules.begin(); iter != m_rules.end(); iter++)
185
 
    {
186
 
        Filter* filter = *iter;
187
 
        rules.insert(make_pair (int(filter->rtti()),filter->rule()));
188
 
    }
189
 
 
190
 
    return rules;
191
 
}
192
 
 
193
 
void FilterManager::setFilteringRules(const multimap<int, string>& rules)
194
 
{
195
 
    clearRules();
196
 
 
197
 
    map<int,string>::const_iterator iter;
198
 
    for (iter = rules.begin(); iter != rules.end(); iter++)
199
 
    {
200
 
        switch (iter->first)
201
 
        {
202
 
            case PathFilter::RTTI:
203
 
                m_rules.push_back (new PathFilter (iter->second));
204
 
                break;
205
 
            case PatternFilter::RTTI:
206
 
                m_rules.push_back (new PatternFilter (iter->second));
207
 
                break;
208
 
            default:
209
 
                STRIGI_LOG_ERROR ("strigi.filtermanager.setFilteringRules", "unknown rule RTTI")
210
 
        }
211
 
    }
212
 
}