Commit 64b0c9ca authored by Jean-Francois Dockes's avatar Jean-Francois Dockes
Browse files

Split monster file rclmain_w.cpp. No code changes

parent 12682b7a
/* 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 <QMessageBox>
#include <QTimer>
#include "execmd.h"
#include "debuglog.h"
#include "transcode.h"
#include "indexer.h"
#include "rclmain_w.h"
using namespace std;
void RclMain::idxStatus()
{
ConfSimple cs(theconfig->getIdxStatusFile().c_str(), 1);
QString msg = tr("Indexing in progress: ");
DbIxStatus status;
string val;
cs.get("phase", val);
status.phase = DbIxStatus::Phase(atoi(val.c_str()));
cs.get("fn", status.fn);
cs.get("docsdone", val);
status.docsdone = atoi(val.c_str());
cs.get("filesdone", val);
status.filesdone = atoi(val.c_str());
cs.get("dbtotdocs", val);
status.dbtotdocs = atoi(val.c_str());
QString phs;
switch (status.phase) {
case DbIxStatus::DBIXS_NONE:phs=tr("None");break;
case DbIxStatus::DBIXS_FILES: phs=tr("Updating");break;
case DbIxStatus::DBIXS_PURGE: phs=tr("Purge");break;
case DbIxStatus::DBIXS_STEMDB: phs=tr("Stemdb");break;
case DbIxStatus::DBIXS_CLOSING:phs=tr("Closing");break;
case DbIxStatus::DBIXS_DONE:phs=tr("Done");break;
case DbIxStatus::DBIXS_MONITOR:phs=tr("Monitor");break;
default: phs=tr("Unknown");break;
}
msg += phs + " ";
if (status.phase == DbIxStatus::DBIXS_FILES) {
char cnts[100];
if (status.dbtotdocs > 0)
sprintf(cnts,"(%d/%d/%d) ", status.docsdone, status.filesdone,
status.dbtotdocs);
else
sprintf(cnts, "(%d/%d) ", status.docsdone, status.filesdone);
msg += QString::fromUtf8(cnts) + " ";
}
string mf;int ecnt = 0;
string fcharset = theconfig->getDefCharset(true);
if (!transcode(status.fn, mf, fcharset, "UTF-8", &ecnt) || ecnt) {
mf = url_encode(status.fn, 0);
}
msg += QString::fromUtf8(mf.c_str());
statusBar()->showMessage(msg, 4000);
}
// This is called by a periodic timer to check the status of
// indexing, a possible need to exit, and cleanup exited viewers
void RclMain::periodic100()
{
LOGDEB2(("Periodic100\n"));
if (m_idxproc) {
// An indexing process was launched. If its' done, see status.
int status;
bool exited = m_idxproc->maybereap(&status);
if (exited) {
deleteZ(m_idxproc);
if (status) {
if (m_idxkilled) {
QMessageBox::warning(0, "Recoll",
tr("Indexing interrupted"));
m_idxkilled = false;
} else {
QMessageBox::warning(0, "Recoll",
tr("Indexing failed"));
}
} else {
// On the first run, show missing helpers. We only do this once
if (m_firstIndexing)
showMissingHelpers();
}
string reason;
maybeOpenDb(reason, 1);
} else {
// update/show status even if the status file did not
// change (else the status line goes blank during
// lengthy operations).
idxStatus();
}
}
// Update the "start/stop indexing" menu entry, can't be done from
// the "start/stop indexing" slot itself
IndexerState prevstate = m_indexerState;
if (m_idxproc) {
m_indexerState = IXST_RUNNINGMINE;
fileToggleIndexingAction->setText(tr("Stop &Indexing"));
fileToggleIndexingAction->setEnabled(true);
fileRetryFailedAction->setEnabled(false);
fileRebuildIndexAction->setEnabled(false);
periodictimer->setInterval(200);
} else {
Pidfile pidfile(theconfig->getPidfile());
if (pidfile.open() == 0) {
m_indexerState = IXST_NOTRUNNING;
fileToggleIndexingAction->setText(tr("Update &Index"));
fileRetryFailedAction->setEnabled(true);
fileToggleIndexingAction->setEnabled(true);
fileRebuildIndexAction->setEnabled(true);
periodictimer->setInterval(1000);
} else {
// Real time or externally started batch indexer running
m_indexerState = IXST_RUNNINGNOTMINE;
fileToggleIndexingAction->setText(tr("Stop &Indexing"));
fileToggleIndexingAction->setEnabled(true);
fileRetryFailedAction->setEnabled(false);
fileRebuildIndexAction->setEnabled(false);
periodictimer->setInterval(200);
}
}
if ((prevstate == IXST_RUNNINGMINE || prevstate == IXST_RUNNINGNOTMINE)
&& m_indexerState == IXST_NOTRUNNING) {
showTrayMessage("Indexing done");
}
// Possibly cleanup the dead viewers
for (vector<ExecCmd*>::iterator it = m_viewers.begin();
it != m_viewers.end(); it++) {
int status;
if ((*it)->maybereap(&status)) {
deleteZ(*it);
}
}
vector<ExecCmd*> v;
for (vector<ExecCmd*>::iterator it = m_viewers.begin();
it != m_viewers.end(); it++) {
if (*it)
v.push_back(*it);
}
m_viewers = v;
if (recollNeedsExit)
fileExit();
}
// This gets called when the "update index" action is activated. It executes
// the requested action, and disables the menu entry. This will be
// re-enabled by the indexing status check
void RclMain::toggleIndexing()
{
switch (m_indexerState) {
case IXST_RUNNINGMINE:
if (m_idxproc) {
// Indexing was in progress, request stop. Let the periodic
// routine check for the results.
int pid = m_idxproc->getChildPid();
if (pid > 0) {
kill(pid, SIGTERM);
m_idxkilled = true;
}
}
break;
case IXST_RUNNINGNOTMINE:
{
int rep =
QMessageBox::information(0, tr("Warning"),
tr("The current indexing process "
"was not started from this "
"interface. Click Ok to kill it "
"anyway, or Cancel to leave it alone"),
QMessageBox::Ok,
QMessageBox::Cancel,
QMessageBox::NoButton);
if (rep == QMessageBox::Ok) {
Pidfile pidfile(theconfig->getPidfile());
pid_t pid = pidfile.open();
if (pid > 0)
kill(pid, SIGTERM);
}
}
break;
case IXST_NOTRUNNING:
{
// Could also mean that no helpers are missing, but then we
// won't try to show a message anyway (which is what
// firstIndexing is used for)
string mhd;
m_firstIndexing = !theconfig->getMissingHelperDesc(mhd);
vector<string> args;
string badpaths;
args.push_back("recollindex");
args.push_back("-E");
ExecCmd::backtick(args, badpaths);
if (!badpaths.empty()) {
int rep =
QMessageBox::warning(0, tr("Bad paths"),
tr("Bad paths in configuration file:\n") +
QString::fromLocal8Bit(badpaths.c_str()),
QMessageBox::Ok,
QMessageBox::Cancel,
QMessageBox::NoButton);
if (rep == QMessageBox::Cancel)
return;
}
args.clear();
args.push_back("-c");
args.push_back(theconfig->getConfDir());
if (fileRetryFailedAction->isChecked())
args.push_back("-k");
m_idxproc = new ExecCmd;
m_idxproc->startExec("recollindex", args, false, false);
}
break;
case IXST_UNKNOWN:
return;
}
}
void RclMain::rebuildIndex()
{
switch (m_indexerState) {
case IXST_UNKNOWN:
case IXST_RUNNINGMINE:
case IXST_RUNNINGNOTMINE:
return; //?? Should not have been called
case IXST_NOTRUNNING:
{
int rep =
QMessageBox::warning(0, tr("Erasing index"),
tr("Reset the index and start "
"from scratch ?"),
QMessageBox::Ok,
QMessageBox::Cancel,
QMessageBox::NoButton);
if (rep == QMessageBox::Ok) {
// Could also mean that no helpers are missing, but then we
// won't try to show a message anyway (which is what
// firstIndexing is used for)
string mhd;
m_firstIndexing = !theconfig->getMissingHelperDesc(mhd);
vector<string> args;
args.push_back("-c");
args.push_back(theconfig->getConfDir());
args.push_back("-z");
m_idxproc = new ExecCmd;
m_idxproc->startExec("recollindex", args, false, false);
}
}
break;
}
}
void RclMain::updateIdxForDocs(vector<Rcl::Doc>& docs)
{
if (m_idxproc) {
QMessageBox::warning(0, tr("Warning"),
tr("Can't update index: indexer running"),
QMessageBox::Ok,
QMessageBox::NoButton);
return;
}
vector<string> paths;
if (ConfIndexer::docsToPaths(docs, paths)) {
vector<string> args;
args.push_back("-c");
args.push_back(theconfig->getConfDir());
args.push_back("-e");
args.push_back("-i");
args.insert(args.end(), paths.begin(), paths.end());
m_idxproc = new ExecCmd;
m_idxproc->startExec("recollindex", args, false, false);
fileToggleIndexingAction->setText(tr("Stop &Indexing"));
}
fileToggleIndexingAction->setEnabled(false);
fileRetryFailedAction->setEnabled(false);
}
/* 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 <QMessageBox>
#include <QShortcut>
#include "debuglog.h"
#include "internfile.h"
#include "rclzg.h"
#include "rclmain_w.h"
static const QKeySequence quitKeySeq("Ctrl+q");
// If a preview (toplevel) window gets closed by the user, we need to
// clean up because there is no way to reopen it. And check the case
// where the current one is closed
void RclMain::previewClosed(Preview *w)
{
LOGDEB(("RclMain::previewClosed(%p)\n", w));
if (w == curPreview) {
LOGDEB(("Active preview closed\n"));
curPreview = 0;
} else {
LOGDEB(("Old preview closed\n"));
}
delete w;
}
// Document up to date check. The main problem we try to solve is
// displaying the wrong message from a compacted mail folder.
//
// Also we should re-run the query after updating the index because
// the ipaths may be wrong in the current result list. For now, the
// user does this by clicking search again once the indexing is done
//
// We only do this for the main index, else jump and prey (cant update
// anyway, even the makesig() call might not make sense for our base
// config)
bool RclMain::containerUpToDate(Rcl::Doc& doc)
{
// If ipath is empty, we decide we don't care. Also, we need an index,
if (doc.ipath.empty() || rcldb == 0)
return true;
string udi;
doc.getmeta(Rcl::Doc::keyudi, &udi);
if (udi.empty()) {
// Whatever...
return true;
}
string sig;
if (!FileInterner::makesig(theconfig, doc, sig)) {
QMessageBox::warning(0, "Recoll", tr("Can't access file: ") +
QString::fromLocal8Bit(doc.url.c_str()));
// Let's try the preview anyway...
return true;
}
if (!rcldb->needUpdate(udi, sig)) {
// Alles ist in ordnung
return true;
}
// We can only run indexing on the main index (dbidx 0)
bool ismainidx = rcldb->whatDbIdx(doc) == 0;
// Indexer already running?
bool ixnotact = (m_indexerState == IXST_NOTRUNNING);
QString msg = tr("Index not up to date for this file. "
"Refusing to risk showing the wrong entry. ");
if (ixnotact && ismainidx) {
msg += tr("Click Ok to update the "
"index for this file, then you will need to "
"re-run the query when indexing is done. ");
} else if (ismainidx) {
msg += tr("The indexer is running so things should "
"improve when it's done. ");
} else if (ixnotact) {
// Not main index
msg += tr("The document belongs to an external index"
"which I can't update. ");
}
msg += tr("Click Cancel to return to the list. "
"Click Ignore to show the preview anyway. ");
QMessageBox::StandardButtons bts =
QMessageBox::Ignore | QMessageBox::Cancel;
if (ixnotact &&ismainidx)
bts |= QMessageBox::Ok;
int rep =
QMessageBox::warning(0, tr("Warning"), msg, bts,
(ixnotact && ismainidx) ?
QMessageBox::Cancel : QMessageBox::NoButton);
if (m_indexerState == IXST_NOTRUNNING && rep == QMessageBox::Ok) {
LOGDEB(("Requesting index update for %s\n", doc.url.c_str()));
vector<Rcl::Doc> docs(1, doc);
updateIdxForDocs(docs);
}
if (rep != QMessageBox::Ignore)
return false;
return true;
}
/**
* Open a preview window for a given document, or load it into new tab of
* existing window.
*
* @param docnum db query index
* @param mod keyboards modifiers like ControlButton, ShiftButton
*/
void RclMain::startPreview(int docnum, Rcl::Doc doc, int mod)
{
LOGDEB(("startPreview(%d, doc, %d)\n", docnum, mod));
if (!containerUpToDate(doc))
return;
// Do the zeitgeist thing
zg_send_event(ZGSEND_PREVIEW, doc);
if (mod & Qt::ShiftModifier) {
// User wants new preview window
curPreview = 0;
}
if (curPreview == 0) {
HighlightData hdata;
m_source->getTerms(hdata);
curPreview = new Preview(reslist->listId(), hdata);
if (curPreview == 0) {
QMessageBox::warning(0, tr("Warning"),
tr("Can't create preview window"),
QMessageBox::Ok,
QMessageBox::NoButton);
return;
}
connect(new QShortcut(quitKeySeq, curPreview), SIGNAL (activated()),
this, SLOT (fileExit()));
connect(curPreview, SIGNAL(previewClosed(Preview *)),
this, SLOT(previewClosed(Preview *)));
connect(curPreview, SIGNAL(wordSelect(QString)),
sSearch, SLOT(addTerm(QString)));
connect(curPreview, SIGNAL(showNext(Preview *, int, int)),
this, SLOT(previewNextInTab(Preview *, int, int)));
connect(curPreview, SIGNAL(showPrev(Preview *, int, int)),
this, SLOT(previewPrevInTab(Preview *, int, int)));
connect(curPreview, SIGNAL(previewExposed(Preview *, int, int)),
this, SLOT(previewExposed(Preview *, int, int)));
connect(curPreview, SIGNAL(saveDocToFile(Rcl::Doc)),
this, SLOT(saveDocToFile(Rcl::Doc)));
curPreview->setWindowTitle(getQueryDescription());
curPreview->show();
}
curPreview->makeDocCurrent(doc, docnum);
}
/**
* Open a preview window for a given document, no linking to result list
*
* This is used to show ie parent documents, which have no corresponding
* entry in the result list.
*
*/
void RclMain::startPreview(Rcl::Doc doc)
{
Preview *preview = new Preview(0, HighlightData());
if (preview == 0) {
QMessageBox::warning(0, tr("Warning"),
tr("Can't create preview window"),
QMessageBox::Ok,
QMessageBox::NoButton);
return;
}
connect(new QShortcut(quitKeySeq, preview), SIGNAL (activated()),
this, SLOT (fileExit()));
connect(preview, SIGNAL(wordSelect(QString)),
sSearch, SLOT(addTerm(QString)));
// Do the zeitgeist thing
zg_send_event(ZGSEND_PREVIEW, doc);
preview->show();
preview->makeDocCurrent(doc, 0);
}
// Show next document from result list in current preview tab
void RclMain::previewNextInTab(Preview * w, int sid, int docnum)
{
previewPrevOrNextInTab(w, sid, docnum, true);
}
// Show previous document from result list in current preview tab
void RclMain::previewPrevInTab(Preview * w, int sid, int docnum)
{
previewPrevOrNextInTab(w, sid, docnum, false);
}
// Combined next/prev from result list in current preview tab
void RclMain::previewPrevOrNextInTab(Preview * w, int sid, int docnum, bool nxt)
{
LOGDEB(("RclMain::previewNextInTab sid %d docnum %d, listId %d\n",
sid, docnum, reslist->listId()));
if (w == 0) // ??
return;
if (sid != reslist->listId()) {
QMessageBox::warning(0, "Recoll",
tr("This search is not active any more"));
return;
}
if (nxt)
docnum++;
else
docnum--;
if (docnum < 0 || m_source.isNull() || docnum >= m_source->getResCnt()) {
QApplication::beep();
return;
}
Rcl::Doc doc;
if (!reslist->getDoc(docnum, doc)) {
QMessageBox::warning(0, "Recoll",
tr("Cannot retrieve document info from database"));
return;
}
w->makeDocCurrent(doc, docnum, true);
}
// Preview tab exposed: if the preview comes from the currently
// displayed result list, tell reslist (to color the paragraph)
void RclMain::previewExposed(Preview *, int sid, int docnum)
{
LOGDEB2(("RclMain::previewExposed: sid %d docnum %d, m_sid %d\n",
sid, docnum, reslist->listId()));
if (sid != reslist->listId()) {
return;
}
reslist->previewExposed(docnum);
}
/* 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 <list>
#include <QMessageBox>
#include "debuglog.h"
#include "fileudi.h"
#include "execmd.h"
#include "transcode.h"
#include "docseqhist.h"
#include "docseqdb.h"
#include "internfile.h"
#include "rclmain_w.h"
#include "rclzg.h"
using namespace std;
// Start native viewer or preview for input Doc. This is used to allow
// using recoll from another app (e.g. Unity Scope) to view embedded
// result docs (docs with an ipath). . We act as a proxy to extract
// the data and start a viewer. The Url are encoded as
// file://path#ipath
void RclMain::viewUrl()
{
if (m_urltoview.isEmpty() || !rcldb)
return;
QUrl qurl(m_urltoview);
LOGDEB(("RclMain::viewUrl: Path [%s] fragment [%s]\n",
(const char *)qurl.path().toLocal8Bit(),
(const char *)qurl.fragment().toLocal8Bit()));
/* In theory, the url might not be for a file managed by the fs
indexer so that the make_udi() call here would be
wrong(). When/if this happens we'll have to hide this part
inside internfile and have some url magic to indicate the
appropriate indexer/identification scheme */
string udi;
make_udi((const char *)qurl.path().toLocal8Bit(),
(const char *)qurl.fragment().toLocal8Bit(), udi);