Parent: [2a3075] (diff)

Child: [462000] (diff)

Download this file

rclconfig.cpp    352 lines (316 with data), 9.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#ifndef lint
static char rcsid[] = "@(#$Id: rclconfig.cpp,v 1.23 2006-03-20 09:51:45 dockes Exp $ (C) 2004 J.F.Dockes";
#endif
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <iostream>
#include "pathut.h"
#include "rclconfig.h"
#include "conftree.h"
#include "debuglog.h"
#include "smallut.h"
#include "copyfile.h"
#ifndef NO_NAMESPACES
using namespace std;
#endif /* NO_NAMESPACES */
RclConfig::RclConfig()
{
zeroMe();
// Compute our data dir name, typically /usr/local/share/recoll
const char *cdatadir = getenv("RECOLL_DATADIR");
if (cdatadir == 0) {
// If not in environment, use the compiled-in constant.
m_datadir = RECOLL_DATADIR;
} else {
m_datadir = cdatadir;
}
const char *cp = getenv("RECOLL_CONFDIR");
if (cp) {
m_confdir = cp;
} else {
m_confdir = path_home();
m_confdir += ".recoll/";
}
string cfilename = path_cat(m_confdir, "recoll.conf");
if (access(m_confdir.c_str(), 0) != 0 ||
access(cfilename.c_str(), 0) != 0) {
if (!initUserConfig())
return;
}
// Open readonly here so as not to casually create a config file
m_conf = new ConfTree(cfilename.c_str(), true);
if (m_conf == 0 ||
(m_conf->getStatus() != ConfSimple::STATUS_RO &&
m_conf->getStatus() != ConfSimple::STATUS_RW)) {
m_reason = string("No main configuration file: ") + cfilename +
" does not exist or cannot be parsed";
return;
}
string mimemapfile;
if (!m_conf->get("mimemapfile", mimemapfile, "")) {
mimemapfile = "mimemap";
}
string mpath = path_cat(m_confdir, mimemapfile);
mimemap = new ConfTree(mpath.c_str(), true);
if (mimemap == 0 ||
(mimemap->getStatus() != ConfSimple::STATUS_RO &&
mimemap->getStatus() != ConfSimple::STATUS_RW)) {
m_reason = string("No mime map configuration file: ") + mpath +
" does not exist or cannot be parsed";
return;
}
// mimemap->list();
string mimeconffile;
if (!m_conf->get("mimeconffile", mimeconffile, "")) {
mimeconffile = "mimeconf";
}
mpath = path_cat(m_confdir, mimeconffile);
mimeconf = new ConfTree(mpath.c_str(), true);
if (mimeconf == 0 ||
(mimeconf->getStatus() != ConfSimple::STATUS_RO &&
mimeconf->getStatus() != ConfSimple::STATUS_RW)) {
m_reason = string("No mime configuration file: ") + mpath +
" does not exist or cannot be parsed";
return;
}
// mimeconf->list();
setKeyDir("");
m_ok = true;
return;
}
bool RclConfig::getConfParam(const std::string &name, int *ivp)
{
string value;
if (!getConfParam(name, value))
return false;
errno = 0;
long lval = strtol(value.c_str(), 0, 0);
if (lval == 0 && errno)
return 0;
if (ivp)
*ivp = int(lval);
return true;
}
bool RclConfig::getConfParam(const std::string &name, bool *bvp)
{
if (!bvp)
return false;
*bvp = false;
string s;
if (!getConfParam(name, s))
return false;
*bvp = stringToBool(s);
return true;
}
// If defcharset was set (from the config or a previous call), use it.
// Else, try to guess it from LANG.
// Use iso8859-1 as ultimate default
// defcharset is reset on setKeyDir()
const string& RclConfig::getDefCharset()
{
if (defcharset.empty()) {
const char *cp;
if ((cp = getenv("LANG"))) {
cp = strrchr(cp, '.');
if (cp) {
cp++;
if (*cp)
defcharset = string(cp);
}
}
if (defcharset.empty())
defcharset = string("ISO8859-1");
}
return defcharset;
}
// Get all known document mime values. We get them from the mimeconf
// 'index' submap: values not in there (ie from mimemap or idfile) can't
// possibly belong to documents in the database.
std::list<string> RclConfig::getAllMimeTypes()
{
std::list<string> lst;
if (mimeconf == 0)
return lst;
// mimeconf->sortwalk(mtypesWalker, &lst);
lst = mimeconf->getNames("index");
lst.sort();
lst.unique();
return lst;
}
bool RclConfig::getStopSuffixes(list<string>& sufflist)
{
if (stopsuffixes == 0 && (stopsuffixes = new list<string>) != 0) {
string stp;
if (mimemap && mimemap->get("recoll_noindex", stp, m_keydir)) {
stringToStrings(stp, *stopsuffixes);
}
}
if (stopsuffixes) {
sufflist = *stopsuffixes;
return true;
}
return false;
}
string RclConfig::getMimeTypeFromSuffix(const string &suff)
{
string mtype;
mimemap->get(suff, mtype, m_keydir);
return mtype;
}
string RclConfig::getMimeHandlerDef(const std::string &mtype)
{
string hs;
if (!mimeconf->get(mtype, hs, "index")) {
LOGDEB(("getMimeHandler: no handler for '%s'\n", mtype.c_str()));
}
return hs;
}
string RclConfig::getMimeViewerDef(const string &mtype)
{
string hs;
mimeconf->get(mtype, hs, "view");
return hs;
}
/**
* Return icon name and path
*/
string RclConfig::getMimeIconName(const string &mtype, string *path)
{
string iconname;
mimeconf->get(mtype, iconname, "icons");
if (iconname.empty())
iconname = "document";
if (path) {
string iconsdir;
getConfParam("iconsdir", iconsdir);
if (iconsdir.empty()) {
iconsdir = path_cat(m_datadir, "images");
} else {
iconsdir = path_tildexpand(iconsdir);
}
*path = path_cat(iconsdir, iconname) + ".png";
}
return iconname;
}
// Look up an executable filter. We look in $RECOLL_FILTERSDIR,
// filtersdir in config file, then let the system use the PATH
string RclConfig::findFilter(const string &icmd)
{
// If the path is absolute, this is it
if (icmd[0] == '/')
return icmd;
string cmd;
const char *cp;
// Filters dir from environment ?
if ((cp = getenv("RECOLL_FILTERSDIR"))) {
cmd = path_cat(cp, icmd);
if (access(cmd.c_str(), X_OK) == 0)
return cmd;
}
// Filters dir as configuration parameter?
if (getConfParam(string("filtersdir"), cmd)) {
cmd = path_cat(cmd, icmd);
if (access(cmd.c_str(), X_OK) == 0)
return cmd;
}
// Filters dir as datadir subdir. Actually the standard case, but
// this is normally the same value found in config file (previous step)
cmd = path_cat(m_datadir, "filters");
cmd = path_cat(cmd, icmd);
if (access(cmd.c_str(), X_OK) == 0)
return cmd;
// Last resort for historical reasons: check in personal config
// directory
cmd = path_cat(getConfDir(), icmd);
if (access(cmd.c_str(), X_OK) == 0)
return cmd;
// Let the shell try to find it...
return icmd;
}
/**
* Return decompression command line for given mime type
*/
bool RclConfig::getUncompressor(const string &mtype, list<string>& cmd)
{
string hs;
mimeconf->get(mtype, hs, "");
if (hs.empty())
return false;
list<string> tokens;
stringToStrings(hs, tokens);
if (tokens.empty()) {
LOGERR(("getUncompressor: empty spec for mtype %s\n", mtype.c_str()));
return false;
}
if (stringlowercmp("uncompress", tokens.front()))
return false;
list<string>::iterator it = tokens.begin();
cmd.assign(++it, tokens.end());
return true;
}
// Create initial user config by copying sample files
static const char *configfiles[] = {"recoll.conf", "mimemap", "mimeconf"};
static int ncffiles = sizeof(configfiles) / sizeof(char *);
bool RclConfig::initUserConfig()
{
// Samples directory
string exdir = path_cat(m_datadir, "examples");
// User's
string recolldir = path_tildexpand("~/.recoll");
if (mkdir(recolldir.c_str(), 0755) < 0) {
m_reason += string("mkdir(") + recolldir + ") failed: " +
strerror(errno);
return false;
}
for (int i = 0; i < ncffiles; i++) {
string src = path_cat((const string&)exdir, string(configfiles[i]));
string dst = path_cat((const string&)recolldir, string(configfiles[i]));
if (!copyfile(src.c_str(), dst.c_str(), m_reason)) {
LOGERR(("Copyfile failed: %s\n", m_reason.c_str()));
return false;
}
}
return true;
}
void RclConfig::initFrom(const RclConfig& r)
{
zeroMe();
if (!(m_ok = r.m_ok))
return;
m_reason = r.m_reason;
m_confdir = r.m_confdir;
m_datadir = r.m_datadir;
m_keydir = r.m_datadir;
// We should use reference-counted objects instead!
if (r.m_conf)
m_conf = new ConfTree(*(r.m_conf));
if (r.mimemap)
mimemap = new ConfTree(*(r.mimemap));
if (r.mimeconf)
mimeconf = new ConfTree(*(r.mimeconf));
if (r.mimemap_local)
mimemap_local = new ConfTree(*(r.mimemap_local));
if (r.stopsuffixes)
stopsuffixes = new std::list<std::string>(*(r.stopsuffixes));
defcharset = r.defcharset;
guesscharset = r.guesscharset;
}