~ubuntu-branches/ubuntu/oneiric/strigi/oneiric

« back to all changes in this revision

Viewing changes to src/daemon/daemonconfigurator.cpp

  • Committer: Package Import Robot
  • Author(s): Felix Geyer
  • Date: 2011-09-24 17:12:15 UTC
  • mfrom: (1.2.6 upstream)
  • mto: This revision was merged to the branch mainline in revision 44.
  • Revision ID: package-import@ubuntu.com-20110924171215-zmbi1f77jntvz65h
Tags: upstream-0.7.6
ImportĀ upstreamĀ versionĀ 0.7.6

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
 
 *                    Jos van den Oever <jos@vandenoever.info>
5
 
 *
6
 
 * This library is free software; you can redistribute it and/or
7
 
 * modify it under the terms of the GNU Library General Public
8
 
 * License as published by the Free Software Foundation; either
9
 
 * version 2 of the License, or (at your option) any later version.
10
 
 *
11
 
 * This library is distributed in the hope that it will be useful,
12
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 
 * Library General Public License for more details.
15
 
 *
16
 
 * You should have received a copy of the GNU Library General Public License
17
 
 * along with this library; see the file COPYING.LIB.  If not, write to
18
 
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19
 
 * Boston, MA 02110-1301, USA.
20
 
 */
21
 
 
22
 
#include "daemonconfigurator.h"
23
 
#include "../strigilogging.h"
24
 
 
25
 
#include <algorithm>
26
 
#include <fstream>
27
 
#include <sstream>
28
 
#include <stdlib.h>
29
 
 
30
 
using namespace std;
31
 
using namespace Strigi;
32
 
 
33
 
class FindRepository {
34
 
private:
35
 
    string m_repoName;
36
 
 
37
 
public:
38
 
    FindRepository(string repoName) { m_repoName = repoName; }
39
 
 
40
 
    bool operator()(Repository repo) {
41
 
        if (repo.a_name.compare(m_repoName) == 0) {
42
 
            return true;
43
 
        }
44
 
        return false;
45
 
    }
46
 
};
47
 
 
48
 
DaemonConfigurator::DaemonConfigurator (const string& confFile)
49
 
        : m_confFile (confFile) {
50
 
    // find the strigi directory
51
 
    string strigidir;
52
 
    string::size_type slashpos = confFile.rfind('/');
53
 
    if (slashpos != string::npos) {
54
 
        strigidir = confFile.substr(0, slashpos);
55
 
    } else {
56
 
        strigidir = getenv("HOME") + string("/.strigi");
57
 
    }
58
 
 
59
 
    stringbuf xml;
60
 
    ifstream f(confFile.c_str(), ios::binary);
61
 
    f.get(xml, '\0');
62
 
    f.close();
63
 
 
64
 
    if (xml.str().length()) {
65
 
        XMLStream stream(xml.str());
66
 
        stream >> *this;
67
 
    }
68
 
    FindRepository findRepository("localhost");
69
 
    list<Repository>::iterator match = find_if(e_repository.begin(),
70
 
                                               e_repository.end(),
71
 
                                               findRepository);
72
 
 
73
 
    // entry for localhost repository doesn't exists
74
 
    if (match == e_repository.end()) {
75
 
        a_useDBus = true;
76
 
        Repository r;
77
 
        r.a_name = "localhost";
78
 
        string home;
79
 
        if (getenv("HOME")) {
80
 
            home.assign(getenv("HOME"));
81
 
        }
82
 
        r.a_indexdir = strigidir + "/clucene";
83
 
        r.a_writeable = true;
84
 
        r.a_type = "clucene";
85
 
        r.a_pollingInterval = DEFAULT_POLLING_INTERVAL;
86
 
 
87
 
        Path p;
88
 
        p.a_path = home;                 r.e_path.push_back(p);
89
 
        p.a_path = home + "/.kde";       r.e_path.push_back(p);
90
 
        p.a_path = home + "/.kde4";      r.e_path.push_back(p);
91
 
        p.a_path = home + "/.gnome2";    r.e_path.push_back(p);
92
 
        p.a_path = home + "/.evolution"; r.e_path.push_back(p);
93
 
        p.a_path = home + "/.mozilla";   r.e_path.push_back(p);
94
 
        p.a_path = home + "/.mozilla-thunderbird";   r.e_path.push_back(p);
95
 
        e_repository.push_back(r);
96
 
 
97
 
        // add pattern to ignore hidden directories and hidden files
98
 
        Filter f;
99
 
        f.a_include = true;
100
 
        f.a_pattern = ".*.directory/"; // mail box directory
101
 
        e_filters.e_filter.push_back(f);
102
 
        f.a_include = false;
103
 
        f.a_pattern = ".*/";
104
 
        e_filters.e_filter.push_back(f);
105
 
        f.a_pattern = ".*";
106
 
        e_filters.e_filter.push_back(f);
107
 
 
108
 
        STRIGI_LOG_WARNING ("DaemonConfigurator",
109
 
                            "created default config for indexed dirs");
110
 
    }
111
 
    save();
112
 
}
113
 
void
114
 
DaemonConfigurator::setIndexedDirectories ( set<string>& dirs,
115
 
                                            const string& repositoryName,
116
 
                                            bool  merge) {
117
 
    FindRepository findRepository (repositoryName);
118
 
 
119
 
    list<Repository>::iterator match = find_if (e_repository.begin(),
120
 
                                                e_repository.end(),
121
 
                                                findRepository);
122
 
 
123
 
    Repository* r;
124
 
 
125
 
    if (match != e_repository.end()) {
126
 
        r = &(*match);
127
 
    } else {
128
 
        // create a new repository
129
 
        Repository repo;
130
 
        repo.a_name = repositoryName;
131
 
        e_repository.push_back(repo);
132
 
        return setIndexedDirectories (dirs, repositoryName, merge);
133
 
    }
134
 
 
135
 
    // clear old path
136
 
    if (!merge) {
137
 
        r->e_path.clear();
138
 
    }
139
 
 
140
 
    for (set<string>::const_iterator iter = dirs.begin(); iter != dirs.end();
141
 
            ++iter) {
142
 
        Path p;
143
 
        p.a_path = *iter;
144
 
        r->e_path.push_back(p);
145
 
    }
146
 
}
147
 
set<string>
148
 
DaemonConfigurator::getIndexedDirectories(const string& repositoryName) {
149
 
    set<string> dirs;
150
 
 
151
 
    FindRepository findRepository (repositoryName);
152
 
 
153
 
    list<Repository>::iterator match = find_if (e_repository.begin(),
154
 
                                                e_repository.end(),
155
 
                                                findRepository);
156
 
 
157
 
    if (match == e_repository.end()) {
158
 
        STRIGI_LOG_WARNING ("DaemonConfigurator.getIndexedDirs",
159
 
                       "cannot find repository name: |" + repositoryName + '|');
160
 
        return dirs;
161
 
    }
162
 
 
163
 
    Repository* r = &(*match);
164
 
    
165
 
    for (list<Path>::const_iterator iter = r->e_path.begin();
166
 
         iter != r->e_path.end(); ++iter) {
167
 
        dirs.insert (iter->a_path);
168
 
    }
169
 
 
170
 
    return dirs;
171
 
}
172
 
void
173
 
DaemonConfigurator::save(const char* file) {
174
 
    ofstream f;
175
 
    if (file == NULL) {
176
 
        f.open(m_confFile.c_str(), ios::binary);
177
 
    } else {
178
 
        f.open(file, ios::binary);
179
 
    }
180
 
    f << *this;
181
 
    f.close();
182
 
}
183
 
string
184
 
DaemonConfigurator::getWriteableIndexType() const {
185
 
    return (e_repository.size()) ?e_repository.begin()->a_type : "";
186
 
}
187
 
string
188
 
DaemonConfigurator::getWriteableIndexDir() const {
189
 
    return (e_repository.size()) ?e_repository.begin()->a_indexdir : "";
190
 
}
191
 
list<Repository>
192
 
DaemonConfigurator::getReadOnlyRepositories() const {
193
 
    list<Repository> ro;
194
 
    list<Repository>::const_iterator i;
195
 
    for (i = e_repository.begin(); i != e_repository.end(); ++i) {
196
 
        if (!i->a_writeable) {
197
 
            ro.push_back(*i);
198
 
        }
199
 
    }
200
 
    return ro;
201
 
}
202
 
void
203
 
DaemonConfigurator::setPollingInterval (unsigned int value,
204
 
                                        const string& repositoryName) {
205
 
    FindRepository findRepository (repositoryName);
206
 
 
207
 
    list<Repository>::iterator match = find_if (e_repository.begin(),
208
 
                                                e_repository.end(),
209
 
                                                findRepository);
210
 
 
211
 
    if (match != e_repository.end()) {
212
 
        Repository* r = &(*match);
213
 
        r->a_pollingInterval = value;
214
 
    } else {
215
 
        // create a new repository
216
 
        Repository repo;
217
 
        repo.a_name = repositoryName;
218
 
        repo.a_pollingInterval = value;
219
 
        e_repository.push_back(repo);
220
 
    }
221
 
}
222
 
 
223
 
unsigned int
224
 
DaemonConfigurator::getPollingInterval(const string& repositoryName) {
225
 
    FindRepository findRepository (repositoryName);
226
 
 
227
 
    list<Repository>::iterator match = find_if (e_repository.begin(),
228
 
                                                e_repository.end(),
229
 
                                                findRepository);
230
 
 
231
 
    Repository* r;
232
 
 
233
 
    if (match != e_repository.end()) {
234
 
        r = &(*match);
235
 
    } else {
236
 
        // create a new repository
237
 
        Repository repo;
238
 
        repo.a_name = repositoryName;
239
 
        e_repository.push_back(repo);
240
 
        r = &repo;
241
 
    }
242
 
 
243
 
    // check minimum polling interval
244
 
    if (r->a_pollingInterval < 5)
245
 
    {
246
 
        r->a_pollingInterval = DEFAULT_POLLING_INTERVAL;
247
 
        return DEFAULT_POLLING_INTERVAL;
248
 
    } else {
249
 
        return r->a_pollingInterval;
250
 
    }
251
 
}
252
 
void
253
 
DaemonConfigurator::loadFilteringRules(AnalyzerConfiguration& config) {
254
 
    vector<pair<bool,string> > filters;
255
 
    list<Filter>::const_iterator i;
256
 
    for (i = e_filters.e_filter.begin(); i != e_filters.e_filter.end(); ++i) {
257
 
        filters.push_back(make_pair<bool,string>(i->a_include, i->a_pattern));
258
 
    }
259
 
    config.setFilters(filters);
260
 
}
261
 
void
262
 
DaemonConfigurator::saveFilteringRules(const vector<pair<bool,string> >& f) {
263
 
    e_filters.e_filter.clear();
264
 
    vector<pair<bool,string> >::const_iterator i;
265
 
    for (i = f.begin(); i != f.end(); ++i) {
266
 
        Filter f;
267
 
        f.a_include = i->first;
268
 
        f.a_pattern = i->second;
269
 
        e_filters.e_filter.push_back(f);
270
 
    }
271
 
}