Parent: [a448a0] (diff)

Child: [e93dd1] (diff)

Download this file

main.cpp    292 lines (250 with data), 7.9 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#ifndef lint
static char rcsid[] = "@(#$Id: main.cpp,v 1.28 2006-01-04 11:33:44 dockes Exp $ (C) 2005 J.F.Dockes";
#endif
#include <unistd.h>
#include <qapplication.h>
#include <qtranslator.h>
#include <qtextcodec.h>
#include <qthread.h>
#include <qtimer.h>
#include <qmessagebox.h>
#include <qsettings.h>
#include <qcheckbox.h>
#include "rcldb.h"
#ifndef NO_NAMESPACES
using Rcl::AdvSearchData;
#endif /* NO_NAMESPACES */
#include "rclconfig.h"
#include "pathut.h"
#include "recoll.h"
#include "smallut.h"
#include "wipedir.h"
#include "rclinit.h"
#include "history.h"
#include "debuglog.h"
#include "recollmain.h"
static const char *recollprefix = RECOLL_PREFIX;
RclConfig *rclconfig;
Rcl::Db *rcldb;
int recollNeedsExit;
string tmpdir;
string iconsdir;
RclDHistory *history;
static string dbdir;
static RecollMain *mainWindow;
static string recollsharedir;
/////////////////////////
// Global variables for user preferences. These are set in the user preference
// dialog and saved restored to the appropriate place in the qt settings
bool prefs_showicons;
int prefs_respagesize = 8;
QString prefs_reslistfontfamily;
int prefs_reslistfontsize;
QString prefs_queryStemLang;
int prefs_mainwidth;
int prefs_mainheight;
bool prefs_ssall;
QString prefs_htmlBrowser;
#define SETTING_RW(var, nm, tp, def) \
if (writing) { \
settings.writeEntry(nm , var); \
} else { \
var = settings.read##tp##Entry(nm, def); \
}
static void rwSettings(bool writing)
{
if (writing) {
if (!mainWindow)
return;
prefs_mainwidth = mainWindow->width();
prefs_mainheight = mainWindow->height();
prefs_ssall = mainWindow->allTermsCB->isChecked();
}
QSettings settings;
settings.setPath("Recoll.org", "Recoll");
SETTING_RW(prefs_mainwidth, "/Recoll/geometry/width", Num, 590);
SETTING_RW(prefs_mainheight, "/Recoll/geometry/height", Num, 810);
SETTING_RW(prefs_ssall, "/Recoll/prefs/simpleSearchAll", Bool, false);
SETTING_RW(prefs_showicons, "/Recoll/prefs/reslist/showicons", Bool, true);
SETTING_RW(prefs_respagesize, "/Recoll/prefs/reslist/pagelen", Num, 8);
SETTING_RW(prefs_reslistfontfamily, "/Recoll/prefs/reslist/fontFamily", ,
"");
SETTING_RW(prefs_reslistfontsize, "/Recoll/prefs/reslist/fontSize", Num,
0);
SETTING_RW(prefs_queryStemLang, "/Recoll/prefs/query/stemLang", ,
"english");
}
bool maybeOpenDb(string &reason)
{
if (!rcldb) {
reason = "Internal error: db not created";
return false;
}
if (!rcldb->isopen() && !rcldb->open(dbdir, Rcl::Db::DbRO)) {
reason = "Could not open database in " +
dbdir + " wait for indexing to complete?";
return false;
}
return true;
}
void recollCleanup()
{
rwSettings(true);
stop_idxthread();
delete rcldb;
rcldb = 0;
delete rclconfig;
rclconfig = 0;
if (tmpdir.length()) {
wipedir(tmpdir);
rmdir(tmpdir.c_str());
tmpdir.erase();
}
}
static void sigcleanup(int)
{
// fprintf(stderr, "sigcleanup\n");
// Cant call exit from here, because the atexit cleanup does some
// thread stuff that we can't do from signal context.
// Just set a flag and let the watchdog timer do the work
recollNeedsExit = 1;
}
int main( int argc, char ** argv )
{
QApplication a(argc, argv);
// translation file for Qt
QTranslator qt( 0 );
qt.load( QString( "qt_" ) + QTextCodec::locale(), "." );
a.installTranslator( &qt );
// Translations for Recoll
recollsharedir = path_cat(recollprefix, "share/recoll");
string translatdir = path_cat(recollsharedir, "translations");
QTranslator translator( 0 );
// QTextCodec::locale() returns $LANG
translator.load( QString("recoll_") + QTextCodec::locale(),
translatdir.c_str() );
a.installTranslator( &translator );
rwSettings(false);
// Create main window and set its size to previous session's
RecollMain w;
mainWindow = &w;
QSize s(prefs_mainwidth, prefs_mainheight);
w.resize(s);
w.allTermsCB->setChecked(prefs_ssall);
string reason;
rclconfig = recollinit(recollCleanup, sigcleanup, reason);
if (!rclconfig || !rclconfig->ok()) {
QString msg = a.translate("Main", "Configuration problem: ");
msg += reason;
QMessageBox::critical(0, "Recoll", msg);
exit(1);
}
if (rclconfig->getConfParam(string("dbdir"), dbdir) == 0) {
// Note: this will have to be replaced by a call to a
// configuration buildin dialog for initial configuration
QMessageBox::critical(0, "Recoll",
a.translate("Main",
"No db directory in configuration"));
exit(1);
}
dbdir = path_tildexpand(dbdir);
rclconfig->getConfParam("iconsdir", iconsdir);
if (iconsdir.empty()) {
iconsdir = path_cat(recollsharedir, "images");
} else {
iconsdir = path_tildexpand(iconsdir);
}
if (!maketmpdir(tmpdir)) {
QMessageBox::critical(0, "Recoll",
a.translate("Main",
"Cannot create temporary directory"));
exit(1);
}
string historyfile = path_cat(rclconfig->getConfDir(), "history");
history = new RclDHistory(historyfile);
rcldb = new Rcl::Db;
// Connect exit handlers etc..
a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
QTimer *timer = new QTimer(&a);
w.connect(timer, SIGNAL(timeout()), &w, SLOT(periodic100()));
timer->start(100);
if (!rcldb || !rcldb->open(dbdir, Rcl::Db::DbRO)) {
startindexing = 1;
switch (QMessageBox::
question(0, "Recoll",
a.translate("Main", "Could not open database in ")+
QString(dbdir) +
a.translate("Main",
".\n"
"Click Cancel if you want to edit the configuration file before indexation starts, or Ok to let it proceed."),
"Ok",
"Cancel", 0, 0, 1 )) {
case 0: // Ok
break;
case 1: // Cancel
exit(0);
}
}
start_idxthread(rclconfig);
// Let's go
w.show();
return a.exec();
}
const static char *htmlbrowserlist =
"opera konqueror firefox mozilla netscape";
// Search for and launch an html browser for the documentation. If the
// user has set a preference, we use it directly instead of guessing.
bool startHelpBrowser(const string &iurl)
{
string url;
if (iurl.empty()) {
url = path_cat(recollsharedir, "doc");
url = path_cat(url, "usermanual.html");
url = string("file://") + url;
} else
url = iurl;
// If the user set a preference with an absolute path then things are
// simple
if (!prefs_htmlBrowser.isEmpty() && prefs_htmlBrowser.find('/') != -1) {
if (access(prefs_htmlBrowser.ascii(), X_OK) != 0) {
LOGERR(("startHelpBrowser: %s not found or not executable\n",
prefs_htmlBrowser.ascii()));
}
string cmd = string(prefs_htmlBrowser.ascii()) + " " + url + "&";
if (system(cmd.c_str()) == 0)
return true;
else
return false;
}
string searched;
if (prefs_htmlBrowser.isEmpty()) {
searched = htmlbrowserlist;
} else {
searched = prefs_htmlBrowser.ascii();
}
list<string> blist;
stringToTokens(searched, blist, " ");
const char *path = getenv("PATH");
if (path == 0)
path = "/bin:/usr/bin:/usr/bin/X11:/usr/X11R6/bin:/usr/local/bin";
list<string> pathl;
stringToTokens(path, pathl, ":");
// For each browser name, search path and exec/stop if found
for (list<string>::const_iterator bit = blist.begin();
bit != blist.end(); bit++) {
for (list<string>::const_iterator pit = pathl.begin();
pit != pathl.end(); pit++) {
string exefile = path_cat(*pit, *bit);
LOGDEB1(("startHelpBrowser: trying %s\n", exefile.c_str()));
if (access(exefile.c_str(), X_OK) == 0) {
string cmd = exefile + " " + url + "&";
if (system(cmd.c_str()) == 0) {
return true;
}
}
}
}
LOGERR(("startHelpBrowser: no html browser found. Looked for: %s\n",
searched.c_str()));
return false;
}