Query Fragments: re-read file if changed. Print informative error message if file cant be parsed

This commit is contained in:
Jean-Francois Dockes 2014-12-18 14:48:01 +01:00
parent 94e9567bb6
commit bfed6cb41a
3 changed files with 44 additions and 13 deletions

View File

@ -17,6 +17,8 @@
#include "autoconfig.h"
#include <sys/stat.h>
#include <string>
#include <vector>
@ -59,6 +61,19 @@ public:
return true;
}
bool error(const QXmlParseException& exception) {
fatalError(exception);
return false;
}
bool fatalError(const QXmlParseException& x) {
errorMessage = QString("%2 at line %3 column %4")
.arg(x.message())
.arg(x.lineNumber())
.arg(x.columnNumber());
return false;
}
QString errorMessage;
private:
QWidget *parent;
QVBoxLayout *vlw;
@ -127,19 +142,18 @@ bool FragButsParser::endElement(const QString & /* namespaceURI */,
}
FragButs::FragButs(QWidget* parent)
: QWidget(parent), m_ok(false)
: QWidget(parent), m_reftime(0), m_ok(false)
{
string conf = path_cat(theconfig->getConfDir(), "fragbuts.xml");
m_fn = path_cat(theconfig->getConfDir(), "fragbuts.xml");
string data, reason;
if (!file_to_string(conf, data, &reason)) {
if (!file_to_string(m_fn, data, &reason)) {
QMessageBox::warning(0, "Recoll",
tr("%1 not found.").arg(
QString::fromLocal8Bit(conf.c_str())));
LOGERR(("Fragbuts:: can't read [%s]\n", conf.c_str()));
QString::fromLocal8Bit(m_fn.c_str())));
LOGERR(("Fragbuts:: can't read [%s]\n", m_fn.c_str()));
return;
}
FragButsParser parser(this, m_buttons);
QXmlSimpleReader reader;
reader.setContentHandler(&parser);
@ -147,10 +161,9 @@ FragButs::FragButs(QWidget* parent)
QXmlInputSource xmlInputSource;
xmlInputSource.setData(QString::fromUtf8(data.c_str()));
if (!reader.parse(xmlInputSource)) {
QMessageBox::warning(0, "Recoll",
tr("%1 could not be parsed.").arg(
QString::fromLocal8Bit(conf.c_str())));
LOGERR(("FragButs:: parse failed for [%s]\n", conf.c_str()));
QMessageBox::warning(0, "Recoll", tr("%1:\n %2")
.arg(QString::fromLocal8Bit(m_fn.c_str()))
.arg(parser.errorMessage));
return;
}
for (vector<ButFrag>::iterator it = m_buttons.begin();
@ -159,6 +172,7 @@ FragButs::FragButs(QWidget* parent)
this, SLOT(onButtonClicked(bool)));
}
setWindowTitle(tr("Fragment Buttons"));
isStale(&m_reftime);
m_ok = true;
}
@ -166,6 +180,16 @@ FragButs::~FragButs()
{
}
bool FragButs::isStale(time_t *reftime)
{
struct stat st;
stat(m_fn.c_str(), &st);
bool ret = st.st_mtime != m_reftime;
if (reftime)
*reftime = st.st_mtime;
return ret;
}
void FragButs::onButtonClicked(bool on)
{
LOGDEB(("FragButs::onButtonClicked: [%d]\n", int(on)));

View File

@ -17,6 +17,9 @@
#ifndef _FRAGBUTS_H_INCLUDED_
#define _FRAGBUTS_H_INCLUDED_
#include <time.h>
#include <string>
#include <vector>
@ -47,6 +50,7 @@ public:
void getfrags(std::vector<std::string>&);
bool ok() {return m_ok;}
bool isStale(time_t *reftime);
private slots:
void onButtonClicked(bool);
@ -54,10 +58,10 @@ signals:
void fragmentsChanged();
private:
// Detect source file change
time_t m_reftime;
std::vector<ButFrag> m_buttons;
bool m_ok;
std::string m_fn;
time_t m_reftime;
bool m_ok;
};

View File

@ -1064,6 +1064,9 @@ void RclMain::showSpellDialog()
void RclMain::showFragButs()
{
if (fragbuts && fragbuts->isStale(0)) {
deleteZ(fragbuts);
}
if (fragbuts == 0) {
fragbuts = new FragButs(0);
if (fragbuts->ok()) {