This commit is contained in:
dockes
2006-01-20 10:01:59 +00:00
parent e93dd142d6
commit 8513847790
10 changed files with 204 additions and 103 deletions
+59 -43
View File
@@ -1,5 +1,5 @@
#ifndef lint
static char rcsid[] = "@(#$Id: rclconfig.cpp,v 1.19 2006-01-19 17:11:46 dockes Exp $ (C) 2004 J.F.Dockes";
static char rcsid[] = "@(#$Id: rclconfig.cpp,v 1.20 2006-01-20 10:01:59 dockes Exp $ (C) 2004 J.F.Dockes";
#endif
#include <unistd.h>
#include <stdio.h>
@@ -20,43 +20,9 @@ static char rcsid[] = "@(#$Id: rclconfig.cpp,v 1.19 2006-01-19 17:11:46 dockes E
using namespace std;
#endif /* NO_NAMESPACES */
static const char *configfiles[] = {"recoll.conf", "mimemap", "mimeconf"};
static int ncffiles = sizeof(configfiles) / sizeof(char *);
static bool createConfig(const string &datadir, string &reason)
{
// Samples directory
string exdir = path_cat(datadir, "examples");
// User's
string recolldir = path_tildexpand("~/.recoll");
if (mkdir(recolldir.c_str(), 0755) < 0) {
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(), reason)) {
LOGERR(("Copyfile failed: %s\n", reason.c_str()));
return false;
}
}
return true;
}
RclConfig::RclConfig()
: m_ok(false), m_conf(0), mimemap(0), mimeconf(0), mimemap_local(0),
stopsuffixes(0)
{
static int loginit = 0;
if (!loginit) {
DebugLog::setfilename("stderr");
DebugLog::getdbl()->setloglevel(10);
loginit = 1;
}
zeroMe();
// Compute our data dir name, typically /usr/local/share/recoll
const char *cdatadir = getenv("RECOLL_DATADIR");
if (cdatadir == 0) {
@@ -77,7 +43,7 @@ RclConfig::RclConfig()
if (access(m_confdir.c_str(), 0) != 0 ||
access(cfilename.c_str(), 0) != 0) {
if (!createConfig(m_datadir, reason))
if (!initUserConfig())
return;
}
@@ -86,7 +52,7 @@ RclConfig::RclConfig()
if (m_conf == 0 ||
(m_conf->getStatus() != ConfSimple::STATUS_RO &&
m_conf->getStatus() != ConfSimple::STATUS_RW)) {
reason = string("No main configuration file: ") + cfilename +
m_reason = string("No main configuration file: ") + cfilename +
" does not exist or cannot be parsed";
return;
}
@@ -100,7 +66,7 @@ RclConfig::RclConfig()
if (mimemap == 0 ||
(mimemap->getStatus() != ConfSimple::STATUS_RO &&
mimemap->getStatus() != ConfSimple::STATUS_RW)) {
reason = string("No mime map configuration file: ") + mpath +
m_reason = string("No mime map configuration file: ") + mpath +
" does not exist or cannot be parsed";
return;
}
@@ -115,13 +81,13 @@ RclConfig::RclConfig()
if (mimeconf == 0 ||
(mimeconf->getStatus() != ConfSimple::STATUS_RO &&
mimeconf->getStatus() != ConfSimple::STATUS_RW)) {
reason = string("No mime configuration file: ") + mpath +
m_reason = string("No mime configuration file: ") + mpath +
" does not exist or cannot be parsed";
return;
}
// mimeconf->list();
setKeyDir(string(""));
setKeyDir("");
m_ok = true;
return;
@@ -173,7 +139,7 @@ bool RclConfig::getStopSuffixes(list<string>& sufflist)
{
if (stopsuffixes == 0 && (stopsuffixes = new list<string>) != 0) {
string stp;
if (mimemap->get("recoll_noindex", stp, keydir)) {
if (mimemap && mimemap->get("recoll_noindex", stp, m_keydir)) {
stringToStrings(stp, *stopsuffixes);
}
}
@@ -188,7 +154,7 @@ bool RclConfig::getStopSuffixes(list<string>& sufflist)
string RclConfig::getMimeTypeFromSuffix(const string &suff)
{
string mtype;
mimemap->get(suff, mtype, keydir);
mimemap->get(suff, mtype, m_keydir);
return mtype;
}
@@ -281,3 +247,53 @@ bool RclConfig::getUncompressor(const string &mtype, list<string>& cmd)
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;
}
+89 -43
View File
@@ -1,6 +1,6 @@
#ifndef _RCLCONFIG_H_INCLUDED_
#define _RCLCONFIG_H_INCLUDED_
/* @(#$Id: rclconfig.h,v 1.12 2006-01-19 17:11:46 dockes Exp $ (C) 2004 J.F.Dockes */
/* @(#$Id: rclconfig.h,v 1.13 2006-01-20 10:01:59 dockes Exp $ (C) 2004 J.F.Dockes */
#include <list>
@@ -11,76 +11,97 @@ class RclConfig {
public:
RclConfig();
~RclConfig() {
delete m_conf;
delete mimemap;
delete mimeconf;
delete mimemap_local;
delete stopsuffixes;
bool ok() {return m_ok;}
const string &getReason() {return m_reason;}
/** Return the directory where this config is stored */
string getConfDir() {return m_confdir;}
/** Set current directory reference, and fetch automatic parameters. */
void setKeyDir(const string &dir)
{
m_keydir = dir;
m_conf->get("defaultcharset", defcharset, m_keydir);
string str;
m_conf->get("guesscharset", str, m_keydir);
guesscharset = stringToBool(str);
}
bool ok() {return m_ok;}
const string &getReason() {return reason;}
string getConfDir() {return m_confdir;}
//ConfTree *getConfig() {return m_ok ? conf : 0;}
/// Get generic configuration parameter according to current keydir
/** Get generic configuration parameter according to current keydir */
bool getConfParam(const string &name, string &value)
{
if (m_conf == 0)
return false;
return m_conf->get(name, value, keydir);
return m_conf->get(name, value, m_keydir);
}
/*
* Variants with autoconversion
*/
/** Variant with autoconversion to int */
bool getConfParam(const std::string &name, int *value);
/** Variant with autoconversion to bool */
bool getConfParam(const std::string &name, bool *value);
/** Get default charset for current keydir (was set during setKeydir) */
const string &getDefCharset() {return defcharset;}
/** Get guessCharset for current keydir (was set during setKeydir) */
bool getGuessCharset() {return guesscharset;}
/// Set current directory reference, and fetch automatic parameters.
void setKeyDir(const string &dir)
{
keydir = dir;
m_conf->get("defaultcharset", defcharset, keydir);
string str;
m_conf->get("guesscharset", str, keydir);
guesscharset = stringToBool(str);
}
/**
* Check if input mime type is a compressed one, and return command to
* uncompress if it is
* Get list of ignored suffixes from mimemap
*
* The list is initialized on first call, and not changed for subsequent
* setKeydirs.
*/
bool getStopSuffixes(std::list<std::string>& sufflist);
/**
* Check in mimeconf if input mime type is a compressed one, and
* return command to uncompress if it is.
*
* The returned command has substitutable places for input file name
* and temp dir name, and will return output name
*/
bool getUncompressor(const std::string &mtpe, std::list<std::string>& cmd);
bool getStopSuffixes(std::list<std::string>& sufflist);
/** Use mimemap to compute mimetype */
std::string getMimeTypeFromSuffix(const std::string &suffix);
std::string getMimeHandlerDef(const std::string &mtype);
/**
* Return external viewer exec string for given mime type
*/
std::string getMimeViewerDef(const std::string &mtype);
/**
* Return icon name for mime type
*/
/** Get input filter from mimeconf for mimetype */
std::string getMimeHandlerDef(const std::string &mimetype);
/** Get external viewer exec string from mimeconf for mimetype */
std::string getMimeViewerDef(const std::string &mimetype);
/** Get icon name from mimeconf for mimetype */
string getMimeIconName(const string &mtype);
const string &getDefCharset() {return defcharset;}
bool getGuessCharset() {return guesscharset;}
/** Get a list of all indexable mime types defined in mimemap */
std::list<string> getAllMimeTypes();
/** Find exec file for external filter. cmd is the command name from the
* command string returned by getMimeHandlerDef */
std::string findFilter(const std::string& cmd);
~RclConfig() {
freeAll();
}
RclConfig(const RclConfig &r) {
initFrom(r);
}
RclConfig& operator=(const RclConfig &r) {
if (this != &r) {
freeAll();
initFrom(r);
}
return *this;
}
private:
int m_ok;
string reason; // Explanation for bad state
string m_reason; // Explanation for bad state
string m_confdir; // Directory where the files are stored
string m_datadir; // Example: /usr/local/share/recoll
string m_keydir; // Current directory used for parameter fetches.
ConfTree *m_conf; // Parsed main configuration
string keydir; // Current directory used for parameter fetches.
ConfTree *mimemap; // These are independant of current keydir.
ConfTree *mimeconf;
ConfTree *mimemap_local; //
@@ -89,6 +110,31 @@ class RclConfig {
// Parameters auto-fetched on setkeydir
string defcharset; // These are stored locally to avoid
bool guesscharset; // They are fetched initially or on setKeydir()
/** Create initial user configuration */
bool initUserConfig();
/** Copy from other */
void initFrom(const RclConfig& r);
/** Init pointers to 0 */
void zeroMe() {
m_ok = false;
m_conf = 0;
mimemap = 0;
mimeconf = 0;
mimemap_local = 0;
stopsuffixes = 0;
}
/** Free data then zero pointers */
void freeAll() {
delete m_conf;
delete mimemap;
delete mimeconf;
delete mimemap_local;
delete stopsuffixes;
// just in case
zeroMe();
}
};