/*************************************************************************** * * * begin : 15 Jan 2004 * * copyright : (C) 2003 by Samokhvalov Anton :) * * * ***************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************/ #include #include #include #include #include #include #include "TopicTree.h" #include "TopicLeaf.h" #include "Def.h" #include "Index.h" TopicTree::TopicTree( QWidget* parent, const char* name ): KListView( parent, name ), d_index( new Index() ), d_codec( 0 ) { addColumn( i18n( "Topic" ) ); setRootIsDecorated( true ); setSorting( -1 ); header()->hide(); setFullWidth( true ); setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding ); connect( this, SIGNAL(doubleClicked(QListViewItem*,const QPoint&,int)), this, SLOT(slotDoubleClicked(QListViewItem*)) ); } TopicTree::~TopicTree() { delete d_index; } void TopicTree::slotLoad( chm::chmfile* file ) { clear(); const chm::chm_topics_tree* tree = file->get_topics_tree(); QString encoding; bool encoding_detected = false; QRegExp rx( "(<.*META.*CHARSET *= *)(.*)(;|>|\")", false ); // QRegExp rx1( "CHARSET *= *(.*)(?:;|\"|>)", false ); rx.setMinimal( true ); if ( tree ) { for ( std::list< chm::chm_topics_tree* >::const_reverse_iterator i = tree->children.rbegin(); i != tree->children.rend(); ++i ) { if ( !encoding_detected ) { if ( file->file_exists( (*i)->path ) ) { std::streamsize size = file->file_size( (*i)->path ); size = size > 1000?1000:size; std::auto_ptr buf( new char[size + 1] ); file->read( (*i)->path, buf.get(), size ); buf.get()[size] = 0; QString st = QString::fromLatin1( buf.get() ); delete buf.release(); if ( rx.search( st ) != -1 ) { QString enc = rx.cap( 2 ); d_codec = QTextCodec::codecForName( enc.latin1() ); if ( d_codec ) { encoding_detected = true; } } } } new TopicLeaf( this, *i, d_index ); } } updateCodec(); QTimer::singleShot( 300, this, SLOT(slotLoadDelayed()) ); } void TopicTree::slotGoUp() { QListViewItem* item = currentItem(); if ( !item ) { return; } item = item->parent(); if ( !item ) { return; } setCurrentItem( item ); } void TopicTree::slotLoadDelayed() { home(); } TopicLeaf* TopicTree::find( const QString& url ) { return d_index->find( url.latin1() ); } void TopicTree::home() { QListViewItem* item = firstChild(); if ( item ) { ensureItemVisible( item ); setCurrentItem( item ); } } void TopicTree::slotDoubleClicked( QListViewItem* item ) { if ( item ) { item->setOpen( !item->isOpen() ); } } QTextCodec* TopicTree::getCodec() { if ( d_codec ) { return d_codec; } return QTextCodec::codecForLocale(); } void TopicTree::setCodec( QTextCodec* codec ) { if ( codec != d_codec ) { d_codec = codec; updateCodec(); } } void TopicTree::updateCodec() { QListViewItemIterator it( this ); while ( it.current() ) { ((TopicLeaf*)(*it))->updateCodec(); ++it; } } #include "TopicTree.moc"