Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / runtime / khelpcenter / khc_indexbuilder.cpp
blobe215980cfece40f82fecc2ea643bee439c638b9d
1 /*
2 This file is part of the KDE Help Center
4 Copyright (c) 2003 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,
19 MA 02110-1301, USA
22 #include "khc_indexbuilder.h"
24 #include "version.h"
26 #include <kaboutdata.h>
27 #include <klocale.h>
28 #include <kcmdlineargs.h>
29 #include <kuniqueapplication.h>
30 #include <kdebug.h>
31 #include <kprocess.h>
32 #include <kconfig.h>
33 #include <kshell.h>
35 #include <QFile>
36 #include <QTextStream>
37 #include <QDBusMessage>
38 #include <QDBusConnection>
40 #include <unistd.h>
41 #include <stdlib.h>
42 #include <iostream>
44 using namespace KHC;
46 IndexBuilder::IndexBuilder(const QString& cmdFile)
48 m_cmdFile = cmdFile;
49 kDebug(1402) << "IndexBuilder()";
52 void IndexBuilder::buildIndices()
54 QFile f( m_cmdFile );
55 if ( !f.open( QIODevice::ReadOnly ) ) {
56 kError() << "Unable to open file '" << m_cmdFile << "'" << endl;
57 exit( 1 );
59 kDebug(1402) << "Opened file '" << m_cmdFile << "'";
60 QTextStream ts( &f );
61 QString line = ts.readLine();
62 while ( !line.isNull() ) {
63 kDebug(1402) << "LINE: " << line;
64 mCmdQueue.append( line );
65 line = ts.readLine();
68 processCmdQueue();
71 void IndexBuilder::processCmdQueue()
73 kDebug(1402) << "IndexBuilder::processCmdQueue()";
75 QStringList::Iterator it = mCmdQueue.begin();
77 if ( it == mCmdQueue.end() ) {
78 quit();
79 return;
82 QString cmd = *it;
84 kDebug(1402) << "PROCESS: " << cmd;
86 KProcess *proc = new KProcess;
88 *proc << KShell::splitArgs(cmd);
90 connect( proc, SIGNAL( finished( int, QProcess::ExitStatus) ),
91 SLOT( slotProcessExited( int, QProcess::ExitStatus) ) );
93 mCmdQueue.erase( it );
95 proc->start();
97 if ( !proc->waitForStarted() ) {
98 sendErrorSignal( i18n("Unable to start command '%1'.", cmd ) );
99 processCmdQueue();
100 delete proc;
104 void IndexBuilder::slotProcessExited( int exitCode, QProcess::ExitStatus exitStatus )
106 KProcess *proc = static_cast<KProcess *>(sender());
108 if ( exitStatus != QProcess::NormalExit ) {
109 kError(1402) << "Process failed" << endl;
110 kError(1402) << "stdout output:" << proc->readAllStandardOutput();
111 kError(1402) << "stderr output:" << proc->readAllStandardError();
113 else if (exitCode != 0 ) {
114 kError(1402) << "running" << proc->program() << "failed with exitCode" << exitCode;
115 kError(1402) << "stdout output:" << proc->readAllStandardOutput();
116 kError(1402) << "stderr output:" << proc->readAllStandardError();
118 delete proc;
120 sendProgressSignal();
122 processCmdQueue();
125 void IndexBuilder::sendErrorSignal( const QString &error )
127 kDebug(1402) << "IndexBuilder::sendErrorSignal()";
128 QDBusMessage message =
129 QDBusMessage::createSignal("/kcmhelpcenter", "org.kde.kcmhelpcenter", "buildIndexError");
130 message <<error;
131 QDBusConnection::sessionBus().send(message);
135 void IndexBuilder::sendProgressSignal()
137 kDebug(1402) << "IndexBuilder::sendProgressSignal()";
138 QDBusMessage message =
139 QDBusMessage::createSignal("/kcmhelpcenter", "org.kde.kcmhelpcenter", "buildIndexProgress");
140 QDBusConnection::sessionBus().send(message);
143 void IndexBuilder::quit()
145 kDebug(1402) << "IndexBuilder::quit()";
147 qApp->quit();
151 int main( int argc, char **argv )
153 KAboutData aboutData( "khc_indexbuilder", 0,
154 ki18n("KHelpCenter Index Builder"),
155 HELPCENTER_VERSION,
156 ki18n("The KDE Help Center"),
157 KAboutData::License_GPL,
158 ki18n("(c) 2003, The KHelpCenter developers") );
160 aboutData.addAuthor( ki18n("Cornelius Schumacher"), KLocalizedString(), "schumacher@kde.org" );
162 KCmdLineArgs::init( argc, argv, &aboutData );
164 KCmdLineOptions options;
165 options.add("+cmdfile", ki18n("Document to be indexed"));
166 options.add("+indexdir", ki18n("Index directory"));
167 KCmdLineArgs::addCmdLineOptions( options );
169 // Note: no KComponentData seems necessary
170 QCoreApplication app( KCmdLineArgs::qtArgc(), KCmdLineArgs::qtArgv() );
172 KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
174 if ( args->count() != 2 ) {
175 kDebug(1402) << "Wrong number of arguments.";
176 return 1;
179 QString cmdFile = args->arg( 0 );
180 QString indexDir = args->arg( 1 );
182 kDebug(1402) << "cmdFile: " << cmdFile;
183 kDebug(1402) << "indexDir: " << indexDir;
185 QFile file( indexDir + "/testaccess" );
186 if ( !file.open( QIODevice::WriteOnly ) || !file.putChar( ' ' ) ) {
187 kDebug(1402) << "access denied";
188 return 2;
189 } else {
190 kDebug(1402) << "can access";
191 file.remove();
194 IndexBuilder builder(cmdFile);
196 QTimer::singleShot(0, &builder, SLOT(buildIndices()));
198 return app.exec();
201 #include "khc_indexbuilder.moc"
203 // vim:ts=2:sw=2:et