Parent: [dbe29d] (diff)

Child: [0b649a] (diff)

Download this file

htmlif.cpp    203 lines (181 with data), 5.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
#ifndef lint
static char rcsid[] = "@(#$Id: htmlif.cpp,v 1.5 2008-12-02 13:14:01 dockes Exp $ (C) 2005 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 <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string>
using namespace std;
#include <kdebug.h>
#include <kstandarddirs.h>
#include "rclconfig.h"
#include "rcldb.h"
#include "rclinit.h"
#include "pathut.h"
#include "searchdata.h"
#include "rclquery.h"
#include "wasastringtoquery.h"
#include "wasatorcl.h"
#include "kio_recoll.h"
#include "docseqdb.h"
#include "readfile.h"
#include "smallut.h"
using namespace KIO;
bool RecollKioPager::append(const string& data)
{
if (!m_parent)
return false;
m_parent->data(QByteArray(data.c_str()));
return true;
}
string RecollKioPager::detailsLink()
{
string chunk = "<a href=\"recoll://command/QueryDetails\">";
chunk += tr("(show query)") + "</a>";
return chunk;
}
const static string parformat =
"<a href=\"%U\"><img src=\"%I\" align=\"left\"></a>"
"%R %S "
"<a href=\"%U\">Open</a>&nbsp;&nbsp;<b>%T</b><br>"
"%M&nbsp;%D&nbsp;&nbsp; <i>%U</i><br>"
"%A %K";
const string& RecollKioPager::parFormat()
{
return parformat;
}
string RecollKioPager::pageTop()
{
return "<p align=\"center\"><a href=\"recoll:///welcome\">New Search</a></p>";
}
string RecollProtocol::makeQueryUrl(int page)
{
char buf[100];
sprintf(buf, "recoll://search/query?q=%s&qtp=%s&p=%d",
(const char*)m_srchStr.toUtf8(), (const char*)m_opt.toUtf8(),
page);
return string(buf);
}
string RecollKioPager::nextUrl()
{
int pagenum = pageNumber();
if (pagenum < 0)
pagenum = 0;
else
pagenum++;
return m_parent->makeQueryUrl(pagenum);
}
string RecollKioPager::prevUrl()
{
int pagenum = pageNumber();
if (pagenum <= 0)
pagenum = 0;
else
pagenum--;
return m_parent->makeQueryUrl(pagenum);
}
static string welcomedata;
void RecollProtocol::welcomePage()
{
kDebug();
mimeType("text/html");
if (welcomedata.empty()) {
QString location =
KStandardDirs::locate("data", "kio_recoll/welcome.html");
string reason;
if (location.isEmpty() ||
!file_to_string((const char *)location.toUtf8(),
welcomedata, &reason)) {
welcomedata = "<html><head><title>Recoll Error</title></head>"
"<body><p>Could not locate Recoll welcome.html file: ";
welcomedata += reason;
welcomedata += "</p></body></html>";
}
}
string catgq;
#if 0
// Catg filtering. A bit complicated to do because of the
// stateless thing (one more thing to compare to check if same
// query) right now. Would be easy by adding to the query
// language, but not too useful in this case, so scrap it for now.
list<string> cats;
if (o_rclconfig->getMimeCategories(cats) && !cats.empty()) {
catgq = "<p>Filter on types: "
"<input type=\"radio\" name=\"ct\" value=\"All\" checked>All";
for (list<string>::iterator it = cats.begin(); it != cats.end();it++) {
catgq += "\n<input type=\"radio\" name=\"ct\" value=\"" +
*it + "\">" + *it ;
}
}
#endif
string tmp;
map<char, string> subs;
subs['Q'] = "";
subs['C'] = catgq;
subs['S'] = "";
pcSubst(welcomedata, tmp, subs);
data(tmp.c_str());
}
void RecollProtocol::queryDetails()
{
kDebug();
mimeType("text/html");
QByteArray array;
QTextStream os(&array, QIODevice::WriteOnly);
os << "<html><head>" << endl;
os << "<meta http-equiv=\"Content-Type\" content=\"text/html;"
"charset=utf-8\">" << endl;
os << "<title>" << "Recoll query details" << "</title>\n" << endl;
os << "</head>" << endl;
os << "<body><h3>Query details:</h3>" << endl;
os << "<p>" << m_pager.queryDescription().c_str() <<"</p>"<< endl;
os << "<p><a href=\"" << makeQueryUrl(m_pager.pageNumber()).c_str() <<
"\">Return to results</a>" << endl;
os << "</body></html>" << endl;
data(array);
}
void RecollProtocol::htmlDoSearch(const QString& q, const QString &opt, int page)
{
kDebug() << "q" << q << "opt" << opt << "page" << page;
if (m_source.isNull())
kDebug() << "Null source";
// Check if same search or need to start new
if (q.compare(m_srchStr) || opt.compare(m_opt)) {
if (!doSearch(q, opt))
return;
m_pager.setDocSource(m_source);
// goto page 0
m_pager.resultPageNext();
}
// Check / adjust page number
if (page > m_pager.pageNumber()) {
int npages = page - m_pager.pageNumber();
for (int i = 0; i < npages; i++)
m_pager.resultPageNext();
} else if (page < m_pager.pageNumber()) {
int npages = m_pager.pageNumber() - page;
for (int i = 0; i < npages; i++)
m_pager.resultPageBack();
}
// Display
mimeType("text/html");
m_pager.displayPage();
kDebug() << "done";
}