delay a few things on startup, such as setting the visibility mode, which ensures...
[personal-kdebase.git] / runtime / khelpcenter / glossary.cpp
blob42c2736dd27cf7742554f35f74e4b9c3418a2391
1 /*
2 * This file is part of the KDE Help Center
4 * Copyright (C) 2002 Frerich Raabe (raabe@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.
20 #include "glossary.h"
21 #include "view.h"
23 #include <kapplication.h>
24 #include <kconfig.h>
25 #include <kdebug.h>
26 #include <kiconloader.h>
27 #include <klocale.h>
28 #include <kxmlguiwindow.h>
29 #include <kprocess.h>
30 #include <kstandarddirs.h>
31 #include <kstatusbar.h>
33 #include <Qt3Support/Q3Header>
34 //Added by qt3to4:
35 #include <QFrame>
36 #include <QTextStream>
38 #include <sys/stat.h>
39 #include <kglobal.h>
41 using namespace KHC;
43 class SectionItem : public K3ListViewItem
45 public:
46 SectionItem( Q3ListViewItem *parent, const QString &text )
47 : K3ListViewItem( parent, text )
49 setOpen( false );
52 virtual void setOpen( bool open )
54 K3ListViewItem::setOpen(open);
56 // TODO: 2nd was contents2 -> needs to be changed to help-contents-alternate or similar
57 setPixmap( 0, SmallIcon( QLatin1String( open ? "help-contents" : "help-contents" ) ) );
62 class EntryItem : public K3ListViewItem
64 public:
65 EntryItem( SectionItem *parent, const QString &term, const QString &id )
66 : K3ListViewItem( parent, term ),
67 m_id( id )
71 QString id() const { return m_id; }
73 private:
74 QString m_id;
77 bool Glossary::m_alreadyWarned = false;
79 Glossary::Glossary( QWidget *parent ) : K3ListView( parent )
81 m_initialized = false;
83 setFrameStyle( QFrame::NoFrame );
85 connect( this, SIGNAL( clicked( Q3ListViewItem * ) ),
86 this, SLOT( treeItemSelected( Q3ListViewItem * ) ) );
87 connect( this, SIGNAL( returnPressed( Q3ListViewItem * ) ),
88 this, SLOT( treeItemSelected( Q3ListViewItem * ) ) );
90 addColumn( QString() );
91 header()->hide();
92 setAllColumnsShowFocus( true );
93 setRootIsDecorated( true );
95 m_byTopicItem = new K3ListViewItem( this, i18n( "By Topic" ) );
96 m_byTopicItem->setPixmap( 0, SmallIcon( "help-contents" ) );
98 m_alphabItem = new K3ListViewItem( this, i18n( "Alphabetically" ) );
99 m_alphabItem->setPixmap( 0, SmallIcon( "character-set" ) );
101 m_cacheFile = KStandardDirs::locateLocal( "cache", "help/glossary.xml" );
103 m_sourceFile = View::langLookup( QLatin1String( "khelpcenter/glossary/index.docbook" ) );
105 m_config = KGlobal::config();
106 //m_config->setGroup( "Glossary" );
110 void Glossary::showEvent(QShowEvent *event)
112 if ( !m_initialized ) {
113 if ( cacheStatus() == NeedRebuild )
114 rebuildGlossaryCache();
115 else
116 buildGlossaryTree();
117 m_initialized = true;
119 K3ListView::showEvent(event);
122 Glossary::~Glossary()
124 qDeleteAll( m_glossEntries );
127 const GlossaryEntry &Glossary::entry( const QString &id ) const
129 return *m_glossEntries[ id ];
132 Glossary::CacheStatus Glossary::cacheStatus() const
134 if ( !QFile::exists( m_cacheFile ) ||
135 m_config->group("Glossary").readPathEntry( "CachedGlossary", QString() ) != m_sourceFile ||
136 m_config->group("Glossary").readEntry( "CachedGlossaryTimestamp" ).toInt() != glossaryCTime() )
137 return NeedRebuild;
139 return CacheOk;
142 int Glossary::glossaryCTime() const
144 struct stat stat_buf;
145 stat( QFile::encodeName( m_sourceFile ).data(), &stat_buf );
147 return stat_buf.st_ctime;
150 void Glossary::rebuildGlossaryCache()
152 KXmlGuiWindow *mainWindow = dynamic_cast<KXmlGuiWindow *>( kapp->activeWindow() );
153 if (mainWindow)
154 mainWindow->statusBar()->showMessage( i18n( "Rebuilding glossary cache..." ) );
156 KProcess *meinproc = new KProcess;
157 connect( meinproc, SIGNAL( finished(int,QProcess::ExitStatus) ),
158 this, SLOT( meinprocFinished(int,QProcess::ExitStatus) ) );
160 *meinproc << KStandardDirs::locate( "exe", QLatin1String( "meinproc4" ) );
161 *meinproc << QLatin1String( "--output" ) << m_cacheFile;
162 *meinproc << QLatin1String( "--stylesheet" )
163 << KStandardDirs::locate( "data", QLatin1String( "khelpcenter/glossary.xslt" ) );
164 *meinproc << m_sourceFile;
166 meinproc->setOutputChannelMode(KProcess::OnlyStderrChannel);
167 meinproc->start();
168 if (!meinproc->waitForStarted()) {
169 kError() << "could not start process" << meinproc->program();
170 if (mainWindow && !m_alreadyWarned) {
171 ; // add warning message box with don't display again option
172 // http://api.kde.org/4.0-api/kdelibs-apidocs/kdeui/html/classKDialog.html
173 m_alreadyWarned = true;
175 delete meinproc;
179 void Glossary::meinprocFinished( int exitCode, QProcess::ExitStatus exitStatus )
181 KProcess *meinproc = static_cast<KProcess *>(sender());
182 KXmlGuiWindow *mainWindow = dynamic_cast<KXmlGuiWindow *>( kapp->activeWindow() );
184 if (exitStatus != QProcess::NormalExit || exitCode != 0) {
185 kError() << "running" << meinproc->program() << "failed with exitCode" << exitCode;
186 kError() << "stderr output:" << meinproc->readAllStandardError();
187 if (mainWindow && !m_alreadyWarned) {
188 ; // add warning message box with don't display again option
189 // http://api.kde.org/4.0-api/kdelibs-apidocs/kdeui/html/classKDialog.html
190 m_alreadyWarned = true;
192 delete meinproc;
193 return;
195 delete meinproc;
197 if ( !QFile::exists( m_cacheFile ) )
198 return;
200 m_config->group("Glossary").writePathEntry( "CachedGlossary", m_sourceFile );
201 m_config->group("Glossary").writeEntry( "CachedGlossaryTimestamp", glossaryCTime() );
202 m_config->sync();
204 m_status = CacheOk;
206 if (mainWindow)
207 mainWindow->statusBar()->showMessage( i18n( "Rebuilding cache... done." ), 2000 );
209 buildGlossaryTree();
212 void Glossary::buildGlossaryTree()
214 QFile cacheFile(m_cacheFile);
215 if ( !cacheFile.open( QIODevice::ReadOnly ) )
216 return;
218 QDomDocument doc;
219 if ( !doc.setContent( &cacheFile ) )
220 return;
222 QDomNodeList sectionNodes = doc.documentElement().elementsByTagName( QLatin1String( "section" ) );
223 for ( int i = 0; i < sectionNodes.count(); i++ ) {
224 QDomElement sectionElement = sectionNodes.item( i ).toElement();
225 QString title = sectionElement.attribute( QLatin1String( "title" ) );
226 SectionItem *topicSection = new SectionItem( m_byTopicItem, title );
228 QDomNodeList entryNodes = sectionElement.elementsByTagName( QLatin1String( "entry" ) );
229 for ( int j = 0; j < entryNodes.count(); j++ ) {
230 QDomElement entryElement = entryNodes.item( j ).toElement();
232 QString entryId = entryElement.attribute( QLatin1String( "id" ) );
233 if ( entryId.isNull() )
234 continue;
236 QDomElement termElement = childElement( entryElement, QLatin1String( "term" ) );
237 QString term = termElement.text().simplified();
239 EntryItem *entry = new EntryItem(topicSection, term, entryId );
240 m_idDict.insert( entryId, entry );
242 SectionItem *alphabSection = 0L;
243 for ( Q3ListViewItemIterator it( m_alphabItem ); it.current(); it++ )
244 if ( it.current()->text( 0 ) == QString( term[ 0 ].toUpper() ) ) {
245 alphabSection = static_cast<SectionItem *>( it.current() );
246 break;
249 if ( !alphabSection )
250 alphabSection = new SectionItem( m_alphabItem, QString( term[ 0 ].toUpper() ) );
252 new EntryItem( alphabSection, term, entryId );
254 QDomElement definitionElement = childElement( entryElement, QLatin1String( "definition" ) );
255 QString definition = definitionElement.text().simplified();
257 GlossaryEntryXRef::List seeAlso;
259 QDomElement referencesElement = childElement( entryElement, QLatin1String( "references" ) );
260 QDomNodeList referenceNodes = referencesElement.elementsByTagName( QLatin1String( "reference" ) );
261 if ( referenceNodes.count() > 0 )
262 for ( int k = 0; k < referenceNodes.count(); k++ ) {
263 QDomElement referenceElement = referenceNodes.item( k ).toElement();
265 QString term = referenceElement.attribute( QLatin1String( "term" ) );
266 QString id = referenceElement.attribute( QLatin1String( "id" ) );
268 seeAlso += GlossaryEntryXRef( term, id );
271 m_glossEntries.insert( entryId, new GlossaryEntry( term, definition, seeAlso ) );
276 void Glossary::treeItemSelected( Q3ListViewItem *item )
278 if ( !item )
279 return;
281 if ( EntryItem *i = dynamic_cast<EntryItem *>( item ) )
282 emit entrySelected( entry( i->id() ) );
284 item->setOpen( !item->isOpen() );
287 QDomElement Glossary::childElement( const QDomElement &element, const QString &name )
289 QDomElement e;
290 for ( e = element.firstChild().toElement(); !e.isNull(); e = e.nextSibling().toElement() )
291 if ( e.tagName() == name )
292 break;
293 return e;
296 QString Glossary::entryToHtml( const GlossaryEntry &entry )
298 QFile htmlFile( KStandardDirs::locate("data", "khelpcenter/glossary.html.in" ) );
299 if (!htmlFile.open(QIODevice::ReadOnly))
300 return QString( "<html><head></head><body><h3>%1</h3>%2</body></html>" )
301 .arg( i18n( "Error" ) )
302 .arg( i18n( "Unable to show selected glossary entry: unable to open "
303 "file 'glossary.html.in'!" ) );
305 QString seeAlso;
306 if (!entry.seeAlso().isEmpty()) {
307 seeAlso = i18n("See also: ");
308 GlossaryEntryXRef::List seeAlsos = entry.seeAlso();
309 GlossaryEntryXRef::List::ConstIterator it = seeAlsos.constBegin();
310 GlossaryEntryXRef::List::ConstIterator end = seeAlsos.constEnd();
311 for (; it != end; ++it) {
312 seeAlso += QLatin1String("<a href=\"glossentry:");
313 seeAlso += (*it).id();
314 seeAlso += QLatin1String("\">") + (*it).term();
315 seeAlso += QLatin1String("</a>, ");
317 seeAlso = seeAlso.left(seeAlso.length() - 2);
320 QTextStream htmlStream(&htmlFile);
321 return htmlStream.readAll()
322 .arg( i18n( "KDE Glossary" ) )
323 .arg( entry.term() )
324 .arg( entry.definition() )
325 .arg( seeAlso)
326 .arg( View::langLookup( QLatin1String("khelpcenter/kdelogo2.png") ) );
329 void Glossary::slotSelectGlossEntry( const QString &id )
331 if ( !m_idDict.contains( id ) )
332 return;
334 EntryItem *newItem = m_idDict.value( id );
335 EntryItem *curItem = dynamic_cast<EntryItem *>( currentItem() );
336 if ( curItem != 0 ) {
337 if ( curItem->id() == id )
338 return;
339 curItem->parent()->setOpen( false );
342 setCurrentItem( newItem );
343 ensureItemVisible( newItem );
346 #include "glossary.moc"
347 // vim:ts=4:sw=4:et