Diffstat (limited to 'core/apps/helpbrowser/helpbrowser.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | core/apps/helpbrowser/helpbrowser.cpp | 227 |
1 files changed, 227 insertions, 0 deletions
diff --git a/core/apps/helpbrowser/helpbrowser.cpp b/core/apps/helpbrowser/helpbrowser.cpp new file mode 100644 index 0000000..d32fc0b --- a/dev/null +++ b/core/apps/helpbrowser/helpbrowser.cpp | |||
@@ -0,0 +1,227 @@ | |||
1 | /********************************************************************** | ||
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | ||
3 | ** | ||
4 | ** This file is part of Qtopia Environment. | ||
5 | ** | ||
6 | ** This file may be distributed and/or modified under the terms of the | ||
7 | ** GNU General Public License version 2 as published by the Free Software | ||
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | ||
9 | ** packaging of this file. | ||
10 | ** | ||
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | ||
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
13 | ** | ||
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | ||
15 | ** | ||
16 | ** Contact info@trolltech.com if any conditions of this licensing are | ||
17 | ** not clear to you. | ||
18 | ** | ||
19 | **********************************************************************/ | ||
20 | |||
21 | #include "helpbrowser.h" | ||
22 | |||
23 | #include <qpe/qpeapplication.h> | ||
24 | #include <qpe/resource.h> | ||
25 | #include <qpe/global.h> | ||
26 | |||
27 | #include <qstatusbar.h> | ||
28 | #include <qdragobject.h> | ||
29 | #include <qpixmap.h> | ||
30 | #include <qpopupmenu.h> | ||
31 | #include <qpe/qpemenubar.h> | ||
32 | #include <qpe/qpetoolbar.h> | ||
33 | #include <qtoolbutton.h> | ||
34 | #include <qiconset.h> | ||
35 | #include <qfile.h> | ||
36 | #include <qtextstream.h> | ||
37 | #include <qstylesheet.h> | ||
38 | #include <qmessagebox.h> | ||
39 | #include <qfiledialog.h> | ||
40 | #include <qevent.h> | ||
41 | #include <qlineedit.h> | ||
42 | #include <qobjectlist.h> | ||
43 | #include <qfileinfo.h> | ||
44 | #include <qfile.h> | ||
45 | #include <qdatastream.h> | ||
46 | #include <qprinter.h> | ||
47 | #include <qsimplerichtext.h> | ||
48 | #include <qpaintdevicemetrics.h> | ||
49 | #include <qaction.h> | ||
50 | |||
51 | #include <ctype.h> | ||
52 | |||
53 | |||
54 | HelpBrowser::HelpBrowser( QWidget* parent, const char *name, WFlags f ) | ||
55 | : QMainWindow( parent, name, f ), | ||
56 | selectedURL() | ||
57 | { | ||
58 | init( "index.html" ); | ||
59 | } | ||
60 | |||
61 | void HelpBrowser::init( const QString& _home ) | ||
62 | { | ||
63 | setIcon( Resource::loadPixmap( "help_icon" ) ); | ||
64 | |||
65 | browser = new QTextBrowser( this ); | ||
66 | browser->setFrameStyle( QFrame::Panel | QFrame::Sunken ); | ||
67 | connect( browser, SIGNAL( textChanged() ), | ||
68 | this, SLOT( textChanged() ) ); | ||
69 | |||
70 | setCentralWidget( browser ); | ||
71 | setToolBarsMovable( FALSE ); | ||
72 | |||
73 | if ( !_home.isEmpty() ) | ||
74 | browser->setSource( _home ); | ||
75 | |||
76 | QPEToolBar* toolbar = new QPEToolBar( this ); | ||
77 | toolbar->setHorizontalStretchable( TRUE ); | ||
78 | QPEMenuBar *menu = new QPEMenuBar( toolbar ); | ||
79 | |||
80 | toolbar = new QPEToolBar( this ); | ||
81 | // addToolBar( toolbar, "Toolbar"); | ||
82 | |||
83 | //QPopupMenu* go = new QPopupMenu( this ); | ||
84 | backAction = new QAction( tr( "Backward" ), Resource::loadIconSet( "back" ), QString::null, 0, this, 0 ); | ||
85 | connect( backAction, SIGNAL( activated() ), browser, SLOT( backward() ) ); | ||
86 | connect( browser, SIGNAL( backwardAvailable( bool ) ), | ||
87 | backAction, SLOT( setEnabled( bool ) ) ); | ||
88 | //backAction->addTo( go ); | ||
89 | backAction->addTo( toolbar ); | ||
90 | backAction->setEnabled( FALSE ); | ||
91 | |||
92 | forwardAction = new QAction( tr( "Forward" ), Resource::loadIconSet( "forward" ), QString::null, 0, this, 0 ); | ||
93 | connect( forwardAction, SIGNAL( activated() ), browser, SLOT( forward() ) ); | ||
94 | connect( browser, SIGNAL( forwardAvailable( bool ) ), | ||
95 | forwardAction, SLOT( setEnabled( bool ) ) ); | ||
96 | //forwardAction->addTo( go ); | ||
97 | forwardAction->addTo( toolbar ); | ||
98 | forwardAction->setEnabled( FALSE ); | ||
99 | |||
100 | QAction *a = new QAction( tr( "Home" ), Resource::loadPixmap( "home" ), QString::null, 0, this, 0 ); | ||
101 | connect( a, SIGNAL( activated() ), browser, SLOT( home() ) ); | ||
102 | //a->addTo( go ); | ||
103 | a->addTo( toolbar ); | ||
104 | |||
105 | bookm = new QPopupMenu( this ); | ||
106 | bookm->insertItem( tr( "Add Bookmark" ), this, SLOT( addBookmark() ) ); | ||
107 | bookm->insertItem( tr( "Remove from Bookmarks" ), this, SLOT( removeBookmark() ) ); | ||
108 | bookm->insertSeparator(); | ||
109 | connect( bookm, SIGNAL( activated( int ) ), | ||
110 | this, SLOT( bookmChosen( int ) ) ); | ||
111 | |||
112 | readBookmarks(); | ||
113 | |||
114 | //menu->insertItem( tr("Go"), go ); | ||
115 | menu->insertItem( tr( "Bookmarks" ), bookm ); | ||
116 | |||
117 | resize( 240, 300 ); | ||
118 | browser->setFocus(); | ||
119 | |||
120 | connect( qApp, SIGNAL(appMessage(const QCString&, const QByteArray&)), | ||
121 | this, SLOT(appMessage(const QCString&, const QByteArray&)) ); | ||
122 | } | ||
123 | |||
124 | void HelpBrowser::appMessage(const QCString& msg, const QByteArray& data) | ||
125 | { | ||
126 | if ( msg == "showFile(QString)" ) { | ||
127 | QDataStream ds(data,IO_ReadOnly); | ||
128 | QString fn; | ||
129 | ds >> fn; | ||
130 | setDocument( fn ); | ||
131 | } | ||
132 | } | ||
133 | |||
134 | void HelpBrowser::setDocument( const QString &doc ) | ||
135 | { | ||
136 | if ( !doc.isEmpty() ) | ||
137 | browser->setSource( doc ); | ||
138 | raise(); | ||
139 | } | ||
140 | |||
141 | |||
142 | void HelpBrowser::textChanged() | ||
143 | { | ||
144 | if ( browser->documentTitle().isNull() ) | ||
145 | setCaption( tr("Help Browser") ); | ||
146 | else | ||
147 | setCaption( browser->documentTitle() ) ; | ||
148 | |||
149 | selectedURL = caption(); | ||
150 | } | ||
151 | |||
152 | HelpBrowser::~HelpBrowser() | ||
153 | { | ||
154 | QStringList bookmarks; | ||
155 | QMap<int, Bookmark>::Iterator it2 = mBookmarks.begin(); | ||
156 | for ( ; it2 != mBookmarks.end(); ++it2 ) | ||
157 | bookmarks.append( (*it2).name + "=" + (*it2).file ); | ||
158 | |||
159 | QFile f2( Global::applicationFileName("helpbrowser", "bookmarks") ); | ||
160 | if ( f2.open( IO_WriteOnly ) ) { | ||
161 | QDataStream s2( &f2 ); | ||
162 | s2 << bookmarks; | ||
163 | f2.close(); | ||
164 | } | ||
165 | } | ||
166 | |||
167 | void HelpBrowser::pathSelected( const QString &_path ) | ||
168 | { | ||
169 | browser->setSource( _path ); | ||
170 | } | ||
171 | |||
172 | void HelpBrowser::readBookmarks() | ||
173 | { | ||
174 | QString file = Global::applicationFileName("helpbrowser", "bookmarks"); | ||
175 | if ( QFile::exists( file ) ) { | ||
176 | QStringList bookmarks; | ||
177 | QFile f( file ); | ||
178 | if ( f.open( IO_ReadOnly ) ) { | ||
179 | QDataStream s( &f ); | ||
180 | s >> bookmarks; | ||
181 | f.close(); | ||
182 | } | ||
183 | QStringList::Iterator it = bookmarks.begin(); | ||
184 | for ( ; it != bookmarks.end(); ++it ) { | ||
185 | Bookmark b; | ||
186 | QString current = *it; | ||
187 | int equal = current.find( "=" ); | ||
188 | if ( equal < 1 || equal == (int)current.length() - 1 ) | ||
189 | continue; | ||
190 | b.name = current.left( equal ); | ||
191 | b.file = current.mid( equal + 1 ); | ||
192 | mBookmarks[ bookm->insertItem( b.name ) ] = b; | ||
193 | } | ||
194 | } | ||
195 | } | ||
196 | |||
197 | void HelpBrowser::bookmChosen( int i ) | ||
198 | { | ||
199 | if ( mBookmarks.contains( i ) ) | ||
200 | browser->setSource( mBookmarks[ i ].file ); | ||
201 | } | ||
202 | |||
203 | void HelpBrowser::addBookmark() | ||
204 | { | ||
205 | Bookmark b; | ||
206 | b.name = browser->documentTitle(); | ||
207 | b.file = browser->source(); | ||
208 | if (b.name.isEmpty() ) { | ||
209 | b.name = b.file.left( b.file.length() - 5 ); // remove .html | ||
210 | } | ||
211 | QMap<int, Bookmark>::Iterator it; | ||
212 | for( it = mBookmarks.begin(); it != mBookmarks.end(); ++it ) | ||
213 | if ( (*it).file == b.file ) return; | ||
214 | mBookmarks[ bookm->insertItem( b.name ) ] = b; | ||
215 | } | ||
216 | |||
217 | void HelpBrowser::removeBookmark() | ||
218 | { | ||
219 | QString file = browser->source(); | ||
220 | QMap<int, Bookmark>::Iterator it = mBookmarks.begin(); | ||
221 | for( ; it != mBookmarks.end(); ++it ) | ||
222 | if ( (*it).file == file ) { | ||
223 | bookm->removeItem( it.key() ); | ||
224 | mBookmarks.remove( it ); | ||
225 | break; | ||
226 | } | ||
227 | } | ||