delay a few things on startup, such as setting the visibility mode, which ensures...
[personal-kdebase.git] / runtime / khelpcenter / searchhandler.cpp
blob31020a487fcbc951403bc9dc8aeb215706814648
1 /*
2 This file is part of KHelpCenter.
4 Copyright (c) 2005 Cornelius Schumacher <schumacher@kde.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "searchhandler.h"
23 #include "searchengine.h"
24 #include "prefs.h"
25 #include "docentry.h"
27 #include <kdesktopfile.h>
28 #include <KProcess>
29 #include <kdebug.h>
30 #include <kmessagebox.h>
31 #include <klocale.h>
32 #include <kstandarddirs.h>
33 #include <KShell>
35 #include <stdlib.h>
37 using namespace KHC;
39 SearchJob::SearchJob(DocEntry *entry) : mEntry( entry ), mProcess( 0 ), mKioJob( 0 )
43 bool SearchJob::startLocal(const QString &cmdString)
45 mProcess = new KProcess;
46 *mProcess << KShell::splitArgs(cmdString);
48 connect( mProcess, SIGNAL( finished(int, QProcess::ExitStatus) ),
49 this, SLOT( searchExited(int, QProcess::ExitStatus) ) );
51 mProcess->setOutputChannelMode(KProcess::SeparateChannels);
52 mProcess->start();
53 if (!mProcess->waitForStarted()) {
54 QString txt = i18n("Error executing search command '%1'.", cmdString );
55 emit searchError( this, mEntry, txt );
56 return false;
58 return true;
61 bool SearchJob::startRemote(const QString &urlString)
63 KIO::TransferJob *job = KIO::get( KUrl( urlString ) );
64 connect( job, SIGNAL( result( KJob * ) ),
65 this, SLOT( slotJobResult( KJob * ) ) );
66 connect( job, SIGNAL( data( KIO::Job *, const QByteArray & ) ),
67 this, SLOT( slotJobData( KIO::Job *, const QByteArray & ) ) );
69 mKioJob = job;
70 return true;
73 SearchJob::~SearchJob()
75 delete mProcess;
76 delete mKioJob;
79 void SearchJob::searchExited( int exitCode, QProcess::ExitStatus exitStatus )
81 if ( exitStatus == QProcess::NormalExit && exitCode == 0 ) {
82 mResult = mProcess->readAllStandardOutput();
83 emit searchFinished( this, mEntry, mResult );
84 } else {
85 mError = mProcess->readAllStandardError();
86 QString error = QLatin1String("<em>") + mCmd + QLatin1String("</em>\n") + mError;
87 emit searchError( this, mEntry, error );
91 void SearchJob::slotJobResult( KJob *job )
93 QString result;
94 //DocEntry *entry = 0;
96 if ( job->error() ) {
97 emit searchError( this, mEntry, i18n("Error: %1", job->errorString() ) );
98 } else {
99 emit searchFinished( this, mEntry, mResult );
103 void SearchJob::slotJobData( KIO::Job *job, const QByteArray &data )
105 Q_UNUSED(job);
106 mResult += data.data();
110 SearchHandler::SearchHandler( const KConfigGroup &cg )
112 mLang = KGlobal::locale()->language().left( 2 );
113 mDocumentTypes = cg.readEntry( "DocumentTypes" , QStringList() );
116 SearchHandler::~SearchHandler()
120 SearchHandler *SearchHandler::initFromFile( const QString &filename )
122 KDesktopFile file( filename );
123 KConfigGroup dg = file.desktopGroup();
125 SearchHandler *handler = 0;
127 const QString type = dg.readEntry( "Type" );
128 if ( false ) {
129 } else {
130 handler = new ExternalProcessSearchHandler( dg );
133 return handler;
136 QStringList SearchHandler::documentTypes() const
138 return mDocumentTypes;
142 ExternalProcessSearchHandler::ExternalProcessSearchHandler( const KConfigGroup &cg )
143 : SearchHandler( cg )
145 mSearchCommand = cg.readEntry( "SearchCommand" );
146 mSearchUrl = cg.readEntry( "SearchUrl" );
147 mIndexCommand = cg.readEntry( "IndexCommand" );
150 QString ExternalProcessSearchHandler::indexCommand( const QString &identifier )
152 QString cmd = mIndexCommand;
153 cmd.replace( "%i", identifier );
154 cmd.replace( "%d", Prefs::indexDirectory() );
155 cmd.replace( "%l", mLang );
156 return cmd;
159 bool ExternalProcessSearchHandler::checkPaths() const
161 if ( !mSearchCommand.isEmpty() && !checkBinary( mSearchCommand ) )
162 return false;
164 if ( !mIndexCommand.isEmpty() && !checkBinary( mIndexCommand ) )
165 return false;
167 return true;
170 bool ExternalProcessSearchHandler::checkBinary( const QString &cmd ) const
172 QString binary;
174 int pos = cmd.indexOf( ' ' );
175 if ( pos < 0 ) binary = cmd;
176 else binary = cmd.left( pos );
178 return !KStandardDirs::findExe( binary ).isEmpty();
181 void ExternalProcessSearchHandler::search( DocEntry *entry, const QStringList &words,
182 int maxResults,
183 SearchEngine::Operation operation )
185 kDebug() << entry->identifier();
187 if ( !mSearchCommand.isEmpty() ) {
188 QString cmdString = SearchEngine::substituteSearchQuery( mSearchCommand,
189 entry->identifier(), words, maxResults, operation, mLang );
191 kDebug() << "CMD:" << cmdString;
193 SearchJob *searchJob = new SearchJob(entry);
194 connect(searchJob, SIGNAL(searchFinished( SearchJob *, DocEntry *, const QString & )),
195 this, SLOT(slotSearchFinished( SearchJob *, DocEntry *, const QString & )));
196 connect(searchJob, SIGNAL(searchError( SearchJob *, DocEntry *, const QString & )),
197 this, SLOT(slotSearchError( SearchJob *, DocEntry *, const QString & )));
198 searchJob->startLocal(cmdString);
200 } else if ( !mSearchUrl.isEmpty() ) {
201 QString urlString = SearchEngine::substituteSearchQuery( mSearchUrl,
202 entry->identifier(), words, maxResults, operation, mLang );
204 kDebug() << "URL:" << urlString;
206 SearchJob *searchJob = new SearchJob(entry);
207 connect(searchJob, SIGNAL(searchFinished( SearchJob *, DocEntry *, const QString & )),
208 this, SLOT(slotSearchFinished( SearchJob *, DocEntry *, const QString & )));
209 connect(searchJob, SIGNAL(searchError( SearchJob *, DocEntry *, const QString & )),
210 this, SLOT(slotSearchError( SearchJob *, DocEntry *, const QString & )));
211 searchJob->startRemote(urlString);
213 } else {
214 QString txt = i18n("No search command or URL specified.");
215 emit searchFinished( this, entry, txt );
219 void ExternalProcessSearchHandler::slotSearchFinished( SearchJob *job, DocEntry *entry, const QString &result )
221 emit searchFinished( this, entry, result);
222 job->deleteLater();
225 void ExternalProcessSearchHandler::slotSearchError( SearchJob *job, DocEntry *entry, const QString &error )
227 emit searchError(this, entry, error);
228 job->deleteLater();
231 #include "searchhandler.moc"