00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #define QTOPIA_INTERNAL_LANGLIST
00022
00023 #include "helpbrowser.h"
00024 #include "magictextbrowser.h"
00025
00026
00027 #include <opie2/odebug.h>
00028 #include <opie2/oresource.h>
00029
00030 #include <qpe/qpeapplication.h>
00031
00032 using namespace Opie::Core;
00033
00034
00035 #include <qmenubar.h>
00036 #include <qtoolbar.h>
00037 #include <qpe/qcopenvelope_qws.h>
00038 #include <qfileinfo.h>
00039 #include <qaction.h>
00040
00041 HelpBrowser::HelpBrowser( QWidget* parent, const char *name, WFlags f )
00042 : QMainWindow( parent, name, f ),
00043 selectedURL()
00044 {
00045 init( "index.html" );
00046 }
00047
00048
00049
00050 void HelpBrowser::init( const QString& _home )
00051 {
00052 setIcon( Opie::Core::OResource::loadPixmap( "HelpBrowser", Opie::Core::OResource::SmallIcon ) );
00053 setBackgroundMode( PaletteButton );
00054
00055 browser = new MagicTextBrowser( this );
00056 browser->setFrameStyle( QFrame::Panel | QFrame::Sunken );
00057 connect( browser, SIGNAL( textChanged() ),
00058 this, SLOT( textChanged() ) );
00059
00060 setCentralWidget( browser );
00061 setToolBarsMovable( FALSE );
00062
00063 if ( !_home.isEmpty() )
00064 browser->setSource( _home );
00065
00066 QToolBar* toolbar = new QToolBar( this );
00067 toolbar->setHorizontalStretchable( TRUE );
00068 QMenuBar *menu = new QMenuBar( toolbar );
00069
00070 toolbar = new QToolBar( this );
00071
00072
00073 QPopupMenu* go = new QPopupMenu( this );
00074 backAction = new QAction( tr( "Backward" ), Opie::Core::OResource::loadPixmap( "back", Opie::Core::OResource::SmallIcon ),
00075 QString::null, 0, this, 0 );
00076 connect( backAction, SIGNAL( activated() ), browser, SLOT( backward() ) );
00077 connect( browser, SIGNAL( backwardAvailable(bool) ),
00078 backAction, SLOT( setEnabled(bool) ) );
00079 backAction->addTo( go );
00080 backAction->addTo( toolbar );
00081 backAction->setEnabled( FALSE );
00082
00083 forwardAction = new QAction( tr( "Forward" ), Opie::Core::OResource::loadPixmap( "forward", Opie::Core::OResource::SmallIcon ),
00084 QString::null, 0, this, 0 );
00085 connect( forwardAction, SIGNAL( activated() ), browser, SLOT( forward() ) );
00086 connect( browser, SIGNAL( forwardAvailable(bool) ),
00087 forwardAction, SLOT( setEnabled(bool) ) );
00088 forwardAction->addTo( go );
00089 forwardAction->addTo( toolbar );
00090 forwardAction->setEnabled( FALSE );
00091
00092 QAction *a = new QAction( tr( "Home" ), Opie::Core::OResource::loadPixmap( "home", Opie::Core::OResource::SmallIcon ),
00093 QString::null, 0, this, 0 );
00094 connect( a, SIGNAL( activated() ), browser, SLOT( home() ) );
00095 a->addTo( go );
00096 a->addTo( toolbar );
00097
00098 bookm = new QPopupMenu( this );
00099 bookm->insertItem( tr( "Add Bookmark" ), this, SLOT( addBookmark() ) );
00100 bookm->insertItem( tr( "Remove from Bookmarks" ), this, SLOT( removeBookmark() ) );
00101 bookm->insertSeparator();
00102 connect( bookm, SIGNAL( activated(int) ),
00103 this, SLOT( bookmChosen(int) ) );
00104
00105 readBookmarks();
00106
00107 menu->insertItem( tr("Go"), go );
00108 menu->insertItem( tr( "Bookmarks" ), bookm );
00109
00110 resize( 240, 300 );
00111 browser->setFocus();
00112 browser->setFrameStyle( QFrame::NoFrame );
00113
00114 #if !defined(QT_NO_COP)
00115 QCopChannel *addressChannel = new QCopChannel("QPE/HelpBrowser" , this );
00116 connect (addressChannel, SIGNAL( received(const QCString&,const QByteArray&)),
00117 this, SLOT ( appMessage(const QCString&,const QByteArray&) ) );
00118 #endif
00119
00120 connect( qApp, SIGNAL(appMessage(const QCString&,const QByteArray&)),
00121 this, SLOT(appMessage(const QCString&,const QByteArray&)) );
00122 }
00123
00124 void HelpBrowser::appMessage(const QCString& msg, const QByteArray& data)
00125 {
00126 if ( msg == "showFile(QString)" ) {
00127 QDataStream ds(data,IO_ReadOnly);
00128 QString fn;
00129 ds >> fn;
00130 setDocument( fn );
00131
00132 QPEApplication::setKeepRunning();
00133
00134 showMaximized();
00135 setActiveWindow();
00136 raise();
00137 }
00138 }
00139
00140 void HelpBrowser::setDocument( const QString &doc )
00141 {
00142 if ( !doc.isEmpty() )
00143 browser->setSource( doc );
00144 raise();
00145 }
00146
00147
00148 void HelpBrowser::textChanged()
00149 {
00150 if ( browser->documentTitle().isNull() )
00151 setCaption( tr("Help Browser") );
00152 else
00153 setCaption( browser->documentTitle() ) ;
00154
00155 selectedURL = caption();
00156 }
00157
00158 HelpBrowser::~HelpBrowser()
00159 {
00160 QStringList bookmarks;
00161 QMap<int, Bookmark>::Iterator it2 = mBookmarks.begin();
00162 for ( ; it2 != mBookmarks.end(); ++it2 )
00163 bookmarks.append( (*it2).name + "=" + (*it2).file );
00164
00165 QFile f2( Global::applicationFileName("helpbrowser", "bookmarks") );
00166 if ( f2.open( IO_WriteOnly ) ) {
00167 QDataStream s2( &f2 );
00168 s2 << bookmarks;
00169 f2.close();
00170 }
00171 }
00172
00173 void HelpBrowser::pathSelected( const QString &_path )
00174 {
00175 browser->setSource( _path );
00176 }
00177
00178 void HelpBrowser::readBookmarks()
00179 {
00180 QString file = Global::applicationFileName("helpbrowser", "bookmarks");
00181 if ( QFile::exists( file ) ) {
00182 QStringList bookmarks;
00183 QFile f( file );
00184 if ( f.open( IO_ReadOnly ) ) {
00185 QDataStream s( &f );
00186 s >> bookmarks;
00187 f.close();
00188 }
00189 QStringList::Iterator it = bookmarks.begin();
00190 for ( ; it != bookmarks.end(); ++it ) {
00191 Bookmark b;
00192 QString current = *it;
00193 int equal = current.find( "=" );
00194 if ( equal < 1 || equal == (int)current.length() - 1 )
00195 continue;
00196 b.name = current.left( equal );
00197 b.file = current.mid( equal + 1 );
00198 mBookmarks[ bookm->insertItem( b.name ) ] = b;
00199 }
00200 }
00201 }
00202
00203 void HelpBrowser::bookmChosen( int i )
00204 {
00205 if ( mBookmarks.contains( i ) )
00206 browser->setSource( mBookmarks[ i ].file );
00207 }
00208
00209 void HelpBrowser::addBookmark()
00210 {
00211 Bookmark b;
00212 b.name = browser->documentTitle();
00213 b.file = browser->source();
00214 if (b.name.isEmpty() ) {
00215 b.name = b.file.left( b.file.length() - 5 );
00216 }
00217 QMap<int, Bookmark>::Iterator it;
00218 for( it = mBookmarks.begin(); it != mBookmarks.end(); ++it )
00219 if ( (*it).file == b.file ) return;
00220 mBookmarks[ bookm->insertItem( b.name ) ] = b;
00221 }
00222
00223 void HelpBrowser::removeBookmark()
00224 {
00225 QString file = browser->source();
00226 QMap<int, Bookmark>::Iterator it = mBookmarks.begin();
00227 for( ; it != mBookmarks.end(); ++it )
00228 if ( (*it).file == file ) {
00229 bookm->removeItem( it.key() );
00230 mBookmarks.remove( it );
00231 break;
00232 }
00233 }