1/*
2 This file is part of the KDE Help Center
3
4 Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org>
5
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.
10
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.
15
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
20*/
21
22#include "khc_indexbuilder.h"
23
24#include "version.h"
25
26#include <KAboutData>
27#include <KLocale>
28#include <KCmdLineArgs>
29#include <KUniqueApplication>
30#include <KDebug>
31#include <KProcess>
32#include <KConfig>
33#include <KShell>
34
35#include <QFile>
36#include <QTextStream>
37#include <QDBusMessage>
38#include <QDBusConnection>
39
40#include <unistd.h>
41#include <stdlib.h>
42#include <iostream>
43
44using namespace KHC;
45
46IndexBuilder::IndexBuilder(const QString& cmdFile)
47{
48 m_cmdFile = cmdFile;
49 kDebug(1402) << "IndexBuilder()";
50}
51
52void IndexBuilder::buildIndices()
53{
54 QFile f( m_cmdFile );
55 if ( !f.open( QIODevice::ReadOnly ) ) {
56 kError() << "Unable to open file '" << m_cmdFile << "'" << endl;
57 exit( 1 );
58 }
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();
66 }
67
68 processCmdQueue();
69}
70
71void IndexBuilder::processCmdQueue()
72{
73 kDebug(1402) << "IndexBuilder::processCmdQueue()";
74
75 QStringList::Iterator it = mCmdQueue.begin();
76
77 if ( it == mCmdQueue.end() ) {
78 quit();
79 return;
80 }
81
82 QString cmd = *it;
83
84 kDebug(1402) << "PROCESS: " << cmd;
85
86 KProcess *proc = new KProcess;
87
88 *proc << KShell::splitArgs(cmd);
89
90 connect( proc, SIGNAL( finished( int, QProcess::ExitStatus) ),
91 SLOT( slotProcessExited( int, QProcess::ExitStatus) ) );
92
93 mCmdQueue.erase( it );
94
95 proc->start();
96
97 if ( !proc->waitForStarted() ) {
98 sendErrorSignal( i18n("Unable to start command '%1'.", cmd ) );
99 processCmdQueue();
100 delete proc;
101 }
102}
103
104void IndexBuilder::slotProcessExited( int exitCode, QProcess::ExitStatus exitStatus )
105{
106 KProcess *proc = static_cast<KProcess *>(sender());
107
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();
112 }
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();
117 }
118 delete proc;
119
120 sendProgressSignal();
121
122 processCmdQueue();
123}
124
125void IndexBuilder::sendErrorSignal( const QString &error )
126{
127 kDebug(1402) << "IndexBuilder::sendErrorSignal()";
128 QDBusMessage message =
129 QDBusMessage::createSignal("/kcmhelpcenter", "org.kde.kcmhelpcenter", "buildIndexError");
130 message <<error;
131 QDBusConnection::sessionBus().send(message);
132
133}
134
135void IndexBuilder::sendProgressSignal()
136{
137 kDebug(1402) << "IndexBuilder::sendProgressSignal()";
138 QDBusMessage message =
139 QDBusMessage::createSignal("/kcmhelpcenter", "org.kde.kcmhelpcenter", "buildIndexProgress");
140 QDBusConnection::sessionBus().send(message);
141}
142
143void IndexBuilder::quit()
144{
145 kDebug(1402) << "IndexBuilder::quit()";
146
147 qApp->quit();
148}
149
150
151int main( int argc, char **argv )
152{
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") );
159
160 aboutData.addAuthor( ki18n("Cornelius Schumacher"), KLocalizedString(), "schumacher@kde.org" );
161
162 KCmdLineArgs::init( argc, argv, &aboutData );
163
164 KCmdLineOptions options;
165 options.add("+cmdfile", ki18n("Document to be indexed"));
166 options.add("+indexdir", ki18n("Index directory"));
167 KCmdLineArgs::addCmdLineOptions( options );
168
169 // Note: no KComponentData seems necessary
170 QCoreApplication app( KCmdLineArgs::qtArgc(), KCmdLineArgs::qtArgv() );
171
172 KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
173
174 if ( args->count() != 2 ) {
175 kDebug(1402) << "Wrong number of arguments.";
176 return 1;
177 }
178
179 QString cmdFile = args->arg( 0 );
180 QString indexDir = args->arg( 1 );
181
182 kDebug(1402) << "cmdFile: " << cmdFile;
183 kDebug(1402) << "indexDir: " << indexDir;
184
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();
192 }
193
194 IndexBuilder builder(cmdFile);
195
196 QTimer::singleShot(0, &builder, SLOT(buildIndices()));
197
198 return app.exec();
199}
200
201#include "khc_indexbuilder.moc"
202
203// vim:ts=2:sw=2:et
204