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,
22 #include "khc_indexbuilder.h"
26 #include <kaboutdata.h>
28 #include <kcmdlineargs.h>
29 #include <kuniqueapplication.h>
36 #include <QTextStream>
37 #include <QDBusMessage>
38 #include <QDBusConnection>
46 IndexBuilder::IndexBuilder(const QString
& cmdFile
)
49 kDebug(1402) << "IndexBuilder()";
52 void IndexBuilder::buildIndices()
55 if ( !f
.open( QIODevice::ReadOnly
) ) {
56 kError() << "Unable to open file '" << m_cmdFile
<< "'" << endl
;
59 kDebug(1402) << "Opened file '" << m_cmdFile
<< "'";
61 QString line
= ts
.readLine();
62 while ( !line
.isNull() ) {
63 kDebug(1402) << "LINE: " << line
;
64 mCmdQueue
.append( line
);
71 void IndexBuilder::processCmdQueue()
73 kDebug(1402) << "IndexBuilder::processCmdQueue()";
75 QStringList::Iterator it
= mCmdQueue
.begin();
77 if ( it
== mCmdQueue
.end() ) {
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
);
97 if ( !proc
->waitForStarted() ) {
98 sendErrorSignal( i18n("Unable to start command '%1'.", cmd
) );
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();
120 sendProgressSignal();
125 void IndexBuilder::sendErrorSignal( const QString
&error
)
127 kDebug(1402) << "IndexBuilder::sendErrorSignal()";
128 QDBusMessage message
=
129 QDBusMessage::createSignal("/kcmhelpcenter", "org.kde.kcmhelpcenter", "buildIndexError");
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()";
151 int main( int argc
, char **argv
)
153 KAboutData
aboutData( "khc_indexbuilder", 0,
154 ki18n("KHelpCenter Index Builder"),
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.";
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";
190 kDebug(1402) << "can access";
194 IndexBuilder
builder(cmdFile
);
196 QTimer::singleShot(0, &builder
, SLOT(buildIndices()));
201 #include "khc_indexbuilder.moc"