Parent: [62f4f7] (diff)

Download this file

fragbuts.cpp    217 lines (196 with data), 6.5 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
/* Copyright (C) 2005 J.F.Dockes
* 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 "autoconfig.h"
#include "safesysstat.h"
#include <string>
#include <vector>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QCheckBox>
#include <QRadioButton>
#include <QButtonGroup>
#include <QMessageBox>
#include <QXmlDefaultHandler>
#include "fragbuts.h"
#include "pathut.h"
#include "smallut.h"
#include "recoll.h"
#include "log.h"
#include "readfile.h"
#include "copyfile.h"
using namespace std;
class FragButsParser : public QXmlDefaultHandler {
public:
FragButsParser(FragButs *_parent, vector<FragButs::ButFrag>& _buttons)
: parent(_parent), vlw(new QVBoxLayout(parent)),
vl(new QVBoxLayout()), buttons(_buttons),
hl(0), bg(0), radio(false)
{
}
bool startElement(const QString & /* namespaceURI */,
const QString & /* localName */,
const QString &qName,
const QXmlAttributes &attributes);
bool endElement(const QString & /* namespaceURI */,
const QString & /* localName */,
const QString &qName);
bool characters(const QString &str)
{
currentText += str;
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;
QVBoxLayout *vl;
vector<FragButs::ButFrag>& buttons;
// Temporary data while parsing.
QHBoxLayout *hl;
QButtonGroup *bg;
QString currentText;
QString label;
string frag;
bool radio;
};
bool FragButsParser::startElement(const QString & /* namespaceURI */,
const QString & /* localName */,
const QString &qName,
const QXmlAttributes &/*attributes*/)
{
currentText = "";
if (qName == "buttons") {
radio = false;
hl = new QHBoxLayout();
} else if (qName == "radiobuttons") {
radio = true;
bg = new QButtonGroup(parent);
hl = new QHBoxLayout();
}
return true;
}
bool FragButsParser::endElement(const QString & /* namespaceURI */,
const QString & /* localName */,
const QString &qName)
{
if (qName == "label") {
label = currentText;
} else if (qName == "frag") {
frag = qs2utf8s(currentText);
} else if (qName == "fragbut") {
string slab = qs2utf8s(label);
trimstring(slab, " \t\n\t");
label = QString::fromUtf8(slab.c_str());
QAbstractButton *abut;
if (radio) {
QRadioButton *but = new QRadioButton(label, parent);
bg->addButton(but);
if (bg->buttons().length() == 1)
but->setChecked(true);
abut = but;
} else {
QCheckBox *but = new QCheckBox(label, parent);
abut = but;
}
abut->setToolTip(currentText);
buttons.push_back(FragButs::ButFrag(abut, frag));
hl->addWidget(abut);
} else if (qName == "buttons" || qName == "radiobuttons") {
vl->addLayout(hl);
hl = 0;
} else if (qName == "fragbuts") {
vlw->addLayout(vl);
}
return true;
}
FragButs::FragButs(QWidget* parent)
: QWidget(parent), m_reftime(0), m_ok(false)
{
m_fn = path_cat(theconfig->getConfDir(), "fragbuts.xml");
string data, reason;
if (!path_exists(m_fn)) {
// config does not exist: try to create it from sample
string src = path_cat(theconfig->getDatadir(), "examples");
src = path_cat(src, "fragbuts.xml");
copyfile(src.c_str(), m_fn.c_str(), reason);
}
if (!file_to_string(m_fn, data, &reason)) {
QMessageBox::warning(0, "Recoll",
tr("%1 not found.").arg(
QString::fromLocal8Bit(m_fn.c_str())));
LOGERR("Fragbuts:: can't read [" << (m_fn) << "]\n" );
return;
}
FragButsParser parser(this, m_buttons);
QXmlSimpleReader reader;
reader.setContentHandler(&parser);
reader.setErrorHandler(&parser);
QXmlInputSource xmlInputSource;
xmlInputSource.setData(QString::fromUtf8(data.c_str()));
if (!reader.parse(xmlInputSource)) {
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();
it != m_buttons.end(); it++) {
connect(it->button, SIGNAL(clicked(bool)),
this, SLOT(onButtonClicked(bool)));
}
setWindowTitle(tr("Query Fragments"));
isStale(&m_reftime);
m_ok = true;
}
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: [" << (int(on)) << "]\n" );
emit fragmentsChanged();
}
void FragButs::getfrags(std::vector<std::string>& frags)
{
for (vector<ButFrag>::iterator it = m_buttons.begin();
it != m_buttons.end(); it++) {
if (it->button->isChecked() && !it->fragment.empty()) {
LOGDEB("FragButs: fragment [" << (it->fragment) << "]\n" );
frags.push_back(it->fragment);
}
}
}