Implement the gui category filters as query language fragments instead of hard-coding them. This allows implementing other kinds of filtering (ie:on directory) just by changing a configuration file

This commit is contained in:
Jean-Francois Dockes 2012-02-18 11:21:09 +01:00
parent fab69fc3a1
commit ef00bfae70
11 changed files with 151 additions and 26 deletions

View File

@ -576,6 +576,24 @@ string RclConfig::getMimeHandlerDef(const string &mtype, bool filtertypes)
return hs;
}
bool RclConfig::getGuiFilterNames(list<string>& cats)
{
if (!mimeconf)
return false;
cats = mimeconf->getNamesShallow("guifilters");
return true;
}
bool RclConfig::getGuiFilter(const string& catfiltername, string& frag)
{
frag.clear();
if (!mimeconf)
return false;
if (!mimeconf->get(catfiltername, frag, "guifilters"))
return false;
return true;
}
bool RclConfig::valueSplitAttributes(const string& whole, string& value,
ConfSimple& attrs)
{

View File

@ -202,6 +202,11 @@ class RclConfig {
/** mimeconf: get list of mime types for category */
bool getMimeCatTypes(const string& cat, list<string>&);
/** mimeconf: get list of gui filters (doc cats by default */
bool getGuiFilterNames(list<string>&);
/** mimeconf: get query lang frag for named filter */
bool getGuiFilter(const string& filtername, string& frag);
/** fields: get field prefix from field name */
bool getFieldTraits(const string& fldname, const FieldTraits **);
const set<string>& getStoredFields() {return m_storedFields;}

View File

@ -176,7 +176,7 @@ void RclMain::init()
connect(bgrp, SIGNAL(buttonClicked(int)), this, SLOT(catgFilter(int)));
allRDB->setChecked(true);
list<string> cats;
theconfig->getMimeCategories(cats);
theconfig->getGuiFilterNames(cats);
// Text for button 0 is not used. Next statement just avoids unused
// variable compiler warning for catg_strings
m_catgbutvec.push_back(catg_strings[0]);
@ -1626,7 +1626,7 @@ void RclMain::showDocHistory()
new DocSequenceHistory(rcldb, g_dynconf,
string(tr("Document history").toUtf8()));
src->setDescription((const char *)tr("History data").toUtf8());
DocSource *source = new DocSource(RefCntr<DocSequence>(src));
DocSource *source = new DocSource(theconfig, RefCntr<DocSequence>(src));
m_source = RefCntr<DocSequence>(source);
m_source->setSortSpec(m_sortspec);
m_source->setFiltSpec(m_filtspec);
@ -1695,7 +1695,8 @@ void RclMain::showQueryDetails()
return;
string oq = breakIntoLines(m_source->getDescription(), 100, 50);
QString str;
QString desc = tr("Result count (est.)") + ": " + str.setNum(m_source->getResCnt()) + "<br>";
QString desc = tr("Result count (est.)") + ": " +
str.setNum(m_source->getResCnt()) + "<br>";
desc += tr("Query details") + ": " + QString::fromUtf8(oq.c_str());
QMessageBox::information(this, tr("Query details"), desc);
}
@ -1711,12 +1712,11 @@ void RclMain::catgFilter(int id)
if (id != 0) {
string catg = m_catgbutvec[id];
list<string> tps;
theconfig->getMimeCatTypes(catg, tps);
for (list<string>::const_iterator it = tps.begin();
it != tps.end(); it++)
m_filtspec.orCrit(DocSeqFiltSpec::DSFS_MIMETYPE, *it);
string frag;
theconfig->getGuiFilter(catg, frag);
m_filtspec.orCrit(DocSeqFiltSpec::DSFS_QLANG, frag);
}
LOGDEB(("RclMain::catgFilter: calling setFiltSpec\n"));
if (m_source.isNotNull())
m_source->setFiltSpec(m_filtspec);
initiateQuery();

View File

@ -315,7 +315,7 @@ extern "C" int XFlush(void *);
void ResList::setDocSource(RefCntr<DocSequence> nsource)
{
LOGDEB(("ResList::setDocSource()\n"));
m_source = RefCntr<DocSequence>(new DocSource(nsource));
m_source = RefCntr<DocSequence>(new DocSource(theconfig, nsource));
}
// A query was executed, or the filtering/sorting parameters changed,

View File

@ -298,7 +298,7 @@ void RecollModel::setDocSource(RefCntr<DocSequence> nsource)
if (nsource.isNull()) {
m_source = RefCntr<DocSequence>();
} else {
m_source = RefCntr<DocSequence>(new DocSource(nsource));
m_source = RefCntr<DocSequence>(new DocSource(theconfig, nsource));
m_hdata.reset();
m_source->getTerms(m_hdata.terms, m_hdata.groups, m_hdata.gslks);
}

View File

@ -62,7 +62,7 @@ bool DocSource::buildStack()
} else {
if (m_fspec.isNotNull()) {
m_seq =
RefCntr<DocSequence>(new DocSeqFiltered(m_seq, m_fspec));
RefCntr<DocSequence>(new DocSeqFiltered(m_config, m_seq, m_fspec));
}
}

View File

@ -49,7 +49,7 @@ class DocSeqSortSpec {
class DocSeqFiltSpec {
public:
DocSeqFiltSpec() {}
enum Crit {DSFS_MIMETYPE};
enum Crit {DSFS_MIMETYPE, DSFS_QLANG, DSFS_PASSALL};
void orCrit(Crit crit, const string& value) {
crits.push_back(crit);
values.push_back(value);
@ -191,12 +191,13 @@ protected:
RefCntr<DocSequence> m_seq;
};
class RclConfig;
// A DocSource can juggle docseqs of different kinds to implement
// sorting and filtering in ways depending on the base seqs capabilities
class DocSource : public DocSeqModifier {
public:
DocSource(RefCntr<DocSequence> iseq)
: DocSeqModifier(iseq)
DocSource(RclConfig *config, RefCntr<DocSequence> iseq)
: DocSeqModifier(iseq), m_config(config)
{}
virtual bool canFilter() {return true;}
virtual bool canSort() {return true;}
@ -218,6 +219,7 @@ public:
private:
bool buildStack();
void stripStack();
RclConfig *m_config;
DocSeqFiltSpec m_fspec;
DocSeqSortSpec m_sspec;
};

View File

@ -21,6 +21,7 @@
#include "rcldb.h"
#include "debuglog.h"
#include "internfile.h"
#include "wasatorcl.h"
DocSequenceDb::DocSequenceDb(RefCntr<Rcl::Query> q, const string &t,
RefCntr<Rcl::SearchData> sdata)
@ -126,6 +127,26 @@ bool DocSequenceDb::setFiltSpec(const DocSeqFiltSpec &fs)
switch (fs.crits[i]) {
case DocSeqFiltSpec::DSFS_MIMETYPE:
m_fsdata->addFiletype(fs.values[i]);
break;
case DocSeqFiltSpec::DSFS_QLANG:
{
if (m_q.isNull())
break;
string reason;
Rcl::SearchData *sd =
wasaStringToRcl(m_q->whatDb()->getConf(),
fs.values[i], reason);
if (sd) {
Rcl::SearchDataClauseSub *cl1 =
new Rcl::SearchDataClauseSub(Rcl::SCLT_SUB,
RefCntr<Rcl::SearchData>(sd));
m_fsdata->addClause(cl1);
}
}
break;
default:
break;
}
}
m_isFiltered = true;

View File

@ -14,39 +14,87 @@
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <algorithm>
#include "debuglog.h"
#include "filtseq.h"
#include "rclconfig.h"
using std::string;
static bool filter(const DocSeqFiltSpec& fs, const Rcl::Doc *x)
{
LOGDEB2((" Filter: ncrits %d\n", fs.crits.size()));
// Compare using each criterion in term. We're doing an or:
// 1st ok ends
for (unsigned int i = 0; i < fs.crits.size(); i++) {
switch (fs.crits[i]) {
case DocSeqFiltSpec::DSFS_MIMETYPE:
LOGDEB1((" MIMETYPE\n"));
LOGDEB2((" filter: MIMETYPE: me [%s] doc [%s]\n",
fs.values[i].c_str(), x->mimetype.c_str()));
if (x->mimetype == fs.values[i])
return 1;
return true;
break;
case DocSeqFiltSpec::DSFS_QLANG:
{
LOGDEB((" filter: QLANG [%s]!!\n", fs.values[i].c_str()));
}
break;
case DocSeqFiltSpec::DSFS_PASSALL:
return true;
}
}
// Did all comparisons
return 0;
return false;
}
DocSeqFiltered::DocSeqFiltered(RclConfig *conf, RefCntr<DocSequence> iseq,
DocSeqFiltSpec &filtspec)
: DocSeqModifier(iseq), m_config(conf)
{
setFiltSpec(filtspec);
}
bool DocSeqFiltered::setFiltSpec(DocSeqFiltSpec &filtspec)
{
m_spec = filtspec;
LOGDEB0(("DocSeqFiltered::setFiltSpec\n"));
for (unsigned int i = 0; i < filtspec.crits.size(); i++) {
switch (filtspec.crits[i]) {
case DocSeqFiltSpec::DSFS_MIMETYPE:
m_spec.orCrit(filtspec.crits[i], filtspec.values[i]);
break;
case DocSeqFiltSpec::DSFS_QLANG:
{
// There are very few lang constructs that we can
// interpret. The default config uses rclcat:value
// only. That will be all for now...
string val = filtspec.values[i];
if (val.find("rclcat:") == 0) {
string catg = val.substr(7);
list<string> tps;
m_config->getMimeCatTypes(catg, tps);
for (list<string>::const_iterator it = tps.begin();
it != tps.end(); it++) {
LOGDEB2(("Adding mime: [%s]\n", it->c_str()));
m_spec.orCrit(DocSeqFiltSpec::DSFS_MIMETYPE, *it);
}
}
}
break;
default:
break;
}
}
// If m_spec ends up empty, pass everything, better than filtering all.
if (m_spec.crits.empty()) {
m_spec.orCrit(DocSeqFiltSpec::DSFS_PASSALL, "");
}
m_dbindices.clear();
return true;
}
bool DocSeqFiltered::getDoc(int idx, Rcl::Doc &doc, string *)
{
LOGDEB1(("DocSeqFiltered: fetching %d\n", idx));
LOGDEB2(("DocSeqFiltered::getDoc() fetching %d\n", idx));
if (idx >= (int)m_dbindices.size()) {
// Have to fetch docs and filter until we get enough or

View File

@ -19,10 +19,13 @@
#include <vector>
#include <string>
using std::string;
using std::vector;
#include "refcntr.h"
#include "docseq.h"
class RclConfig;
/**
* A filtered sequence is created from another one by selecting entries
@ -30,17 +33,17 @@
*/
class DocSeqFiltered : public DocSeqModifier {
public:
DocSeqFiltered(RefCntr<DocSequence> iseq, DocSeqFiltSpec &filtspec)
: DocSeqModifier(iseq), m_spec(filtspec)
{}
DocSeqFiltered(RclConfig *conf, RefCntr<DocSequence> iseq,
DocSeqFiltSpec &filtspec);
virtual ~DocSeqFiltered() {}
virtual bool canFilter() {return true;}
virtual bool setFiltSpec(DocSeqFiltSpec &filtspec);
virtual bool getDoc(int num, Rcl::Doc &doc, string *sh = 0);
virtual int getResCnt() {return m_seq->getResCnt();}
private:
DocSeqFiltSpec m_spec;
vector<int> m_dbindices;
RclConfig *m_config;
DocSeqFiltSpec m_spec;
vector<int> m_dbindices;
};
#endif /* _FILTSEQ_H_INCLUDED_ */

View File

@ -194,7 +194,12 @@ text/x-purple-html-log = pidgin
text/x-python = text-x-python
[categories]
# Categories group mime types by "kind". They can be used from the query
# language as an "rclcat" clause. This is fully dynamic, you can change the
# names and groups as you wish, only the mime types are stored in the index.
#
# If you add/remove categories, you may also want to change the
# "guifilters" section below.
text = \
application/msword \
application/pdf \
@ -280,3 +285,26 @@ other = application/vnd.sun.xml.draw \
application/x-rar \
application/x-webarchive \
application/zip \
[guifilters]
# This defines the top level filters in the GUI (accessed by the the
# radiobuttons above the results area, or a toolbar combobox).
# Each entry defines a label and a query language fragment that will be
# applied to filter the current query if the option is activated.
#
# This does not really belong in mimeconf, but it does belong in the index
# config (not the GUI one), because it's not necessarily the same in all
# configs, it has to go somewhere, and it's not worth a separate config
# file...
#
# By default this filters by document category (see above), but any
# language fragment should be ok. Be aware though that the "document
# history" queries only know about simple "rclcat" filtering.
text = rclcat:text
spreadsheet = rclcat:spreadsheet
presentation = rclcat:presentation
media = rclcat:media
message = rclcat:message
other = rclcat:other