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"
27 #include <kdesktopfile.h>
30 #include <kmessagebox.h>
32 #include <kstandarddirs.h>
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
);
53 if (!mProcess
->waitForStarted()) {
54 QString txt
= i18n("Error executing search command '%1'.", cmdString
);
55 emit
searchError( this, mEntry
, txt
);
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
& ) ) );
73 SearchJob::~SearchJob()
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
);
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
)
94 //DocEntry *entry = 0;
97 emit
searchError( this, mEntry
, i18n("Error: %1", job
->errorString() ) );
99 emit
searchFinished( this, mEntry
, mResult
);
103 void SearchJob::slotJobData( KIO::Job
*job
, const QByteArray
&data
)
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" );
130 handler
= new ExternalProcessSearchHandler( dg
);
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
);
159 bool ExternalProcessSearchHandler::checkPaths() const
161 if ( !mSearchCommand
.isEmpty() && !checkBinary( mSearchCommand
) )
164 if ( !mIndexCommand
.isEmpty() && !checkBinary( mIndexCommand
) )
170 bool ExternalProcessSearchHandler::checkBinary( const QString
&cmd
) const
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
,
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
);
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
);
225 void ExternalProcessSearchHandler::slotSearchError( SearchJob
*job
, DocEntry
*entry
, const QString
&error
)
227 emit
searchError(this, entry
, error
);
231 #include "searchhandler.moc"