summaryrefslogtreecommitdiff
path: root/core/apps/helpbrowser/magictextbrowser.cpp
Unidiff
Diffstat (limited to 'core/apps/helpbrowser/magictextbrowser.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--core/apps/helpbrowser/magictextbrowser.cpp99
1 files changed, 99 insertions, 0 deletions
diff --git a/core/apps/helpbrowser/magictextbrowser.cpp b/core/apps/helpbrowser/magictextbrowser.cpp
new file mode 100644
index 0000000..8ce0325
--- a/dev/null
+++ b/core/apps/helpbrowser/magictextbrowser.cpp
@@ -0,0 +1,99 @@
1#include <qfile.h>
2#include <qstring.h>
3#include <qdragobject.h>
4#include <qregexp.h>
5
6/* need to get Global::helpPath() */
7#define QTOPIA_INTERNAL_LANGLIST
8
9#include <qtopia/global.h>
10#include <qtopia/mimetype.h>
11#include <qtopia/applnk.h>
12
13#include "magictextbrowser.h"
14
15
16
17MagicTextBrowser::MagicTextBrowser(QWidget* parent) :
18 QTextBrowser(parent){
19}
20
21void MagicTextBrowser::setSource( const QString& source ) {
22 QTextBrowser::setSource(source);
23 if ( magicQpe(source,"applications") || magicQpe(source,"games") || magicQpe(source,"settings") || magicQpe(source, "1Pim") ) // No tr
24 return;
25 if ( magicOpe(source, "applets") || magicOpe(source, "input") )
26 return;
27 // Just those are magic (for now). Could do CGI here,
28 // or in Qtopia's mime source factory.
29}
30
31bool MagicTextBrowser::magicQpe(const QString& source, const QString& name) {
32 if ( name+".html" == source || "help/"+name+".html" == source) {
33 QString fn = mimeSourceFactory()->makeAbsolute( source, context() );
34 const QMimeSource* m = mimeSourceFactory()->data( fn, context() );
35 if ( m ) {
36 QString txt;
37 if ( QTextDrag::decode(m,txt) ) {
38 QRegExp re("<qtopia-"+name+">.*</qtopia-"+name+">");
39 int start,len;
40 if ( (start=re.match(txt,0,&len))>=0 ) {
41 QString generated = generateQpe(name);
42 txt.replace(start,len,generated);
43 setText(txt);
44 return true;
45 }
46 }
47 }
48 }
49 return false;
50}
51bool MagicTextBrowser::magicOpe( const QString& source, const QString& name ) {
52 if ( name+".html" != source && "help/"+name+".html" != source) return false;
53
54 QString fn = mimeSourceFactory()->makeAbsolute( source, context() );
55 const QMimeSource* m = mimeSourceFactory()->data(fn, context() );
56 if (!m) return false;
57
58 QString txt;
59 if ( !QTextDrag::decode(m, txt ) ) return false;
60
61 QRegExp re("<opie-"+name+">.*</opie-"+name+">");
62 int start,len;
63 if ( (start=re.match(txt,0,&len))>=0 ) {
64 QString generated = generateOpe(name);
65 txt.replace(start,len,generated);
66 setText(txt);
67 return true;
68 }
69 return false;
70}
71QString MagicTextBrowser::generateOpe(const QString& name)const {
72 if ( name == QString::fromLatin1("applets") ) {
73 return QString::fromLatin1("<h3>No Applets found</h3>");
74 }else if ( name == QString::fromLatin1("input") ) {
75 return QString::fromLatin1("<h3>No input methods available</h3>");
76 }else
77 return QString::null;
78}
79
80QString MagicTextBrowser::generateQpe(const QString& name) const {
81 QString dir = MimeType::appsFolderName()+"/"+name[0].upper()+name.mid(1);
82 AppLnkSet lnkset(dir);
83 AppLnk* lnk;
84 QString r;
85 for (QListIterator<AppLnk> it(lnkset.children()); (lnk=it.current()); ++it) {
86 QString name = lnk->name();
87 QString icon = lnk->icon();
88 QString helpFile = lnk->exec()+".html";
89 QStringList helpPath = Global::helpPath();
90 bool helpExists = FALSE;
91 for (QStringList::ConstIterator it=helpPath.begin(); it!=helpPath.end() && !helpExists; ++it)
92 helpExists = QFile::exists( *it + "/" + helpFile );
93
94 if ( helpExists ) {
95 r += "<h3><a href="+helpFile+"><img src="+icon+">"+name+"</a></h3>\n";
96 }
97 }
98 return r;
99}