00001 #include <qfile.h>
00002 #include <qdragobject.h>
00003
00004
00005 #define QTOPIA_INTERNAL_LANGLIST
00006
00007 #include <qtopia/global.h>
00008 #include <qtopia/mimetype.h>
00009 #include <qtopia/applnk.h>
00010
00011 #include "magictextbrowser.h"
00012
00013
00014
00015 MagicTextBrowser::MagicTextBrowser(QWidget* parent) :
00016 QTextBrowser(parent){
00017 }
00018
00019 void MagicTextBrowser::setSource( const QString& source ) {
00020 QTextBrowser::setSource(source);
00021 if ( magicQpe(source,"applications") || magicQpe(source,"games") || magicQpe(source,"settings") || magicQpe(source, "1Pim") )
00022 return;
00023 if ( magicOpe(source, "applets") || magicOpe(source, "input") )
00024 return;
00025
00026
00027 }
00028
00029 bool MagicTextBrowser::magicQpe(const QString& source, const QString& name) {
00030 if ( name+".html" == source || "help/"+name+".html" == source) {
00031 QString fn = mimeSourceFactory()->makeAbsolute( source, context() );
00032 const QMimeSource* m = mimeSourceFactory()->data( fn, context() );
00033 if ( m ) {
00034 QString txt;
00035 if ( QTextDrag::decode(m,txt) ) {
00036 QRegExp re("<qtopia-"+name+">.*</qtopia-"+name+">");
00037 int start,len;
00038 if ( (start=re.match(txt,0,&len))>=0 ) {
00039 QString generated = generateQpe(name);
00040 txt.replace(start,len,generated);
00041 setText(txt);
00042 return true;
00043 }
00044 }
00045 }
00046 }
00047 return false;
00048 }
00049 bool MagicTextBrowser::magicOpe( const QString& source, const QString& name ) {
00050 if ( name+".html" != source && "help/"+name+".html" != source) return false;
00051
00052 QString fn = mimeSourceFactory()->makeAbsolute( source, context() );
00053 const QMimeSource* m = mimeSourceFactory()->data(fn, context() );
00054 if (!m) return false;
00055
00056 QString txt;
00057 if ( !QTextDrag::decode(m, txt ) ) return false;
00058
00059 QRegExp re("<opie-"+name+">.*</opie-"+name+">");
00060 int start,len;
00061 if ( (start=re.match(txt,0,&len))>=0 ) {
00062 QString generated = generateOpe(name);
00063 txt.replace(start,len,generated);
00064 setText(txt);
00065 return true;
00066 }
00067 return false;
00068 }
00069 QString MagicTextBrowser::generateOpe(const QString& name)const {
00070 if ( name == QString::fromLatin1("applets") ) {
00071 return QString::fromLatin1("<h3>No Applets found</h3>");
00072 }else if ( name == QString::fromLatin1("input") ) {
00073 return QString::fromLatin1("<h3>No input methods available</h3>");
00074 }else
00075 return QString::null;
00076 }
00077
00078 QString MagicTextBrowser::generateQpe(const QString& name) const {
00079 QString dir = MimeType::appsFolderName()+"/"+name[0].upper()+name.mid(1);
00080 AppLnkSet lnkset(dir);
00081 AppLnk* lnk;
00082 QString r;
00083 for (QListIterator<AppLnk> it(lnkset.children()); (lnk=it.current()); ++it) {
00084 QString name = lnk->name();
00085 QString icon = lnk->icon();
00086 QString exec = lnk->exec();
00087 QString helpFile = exec+".html";
00088 QStringList helpPath = Global::helpPath();
00089 bool helpExists = FALSE;
00090 for (QStringList::ConstIterator it=helpPath.begin(); it!=helpPath.end() && !helpExists; ++it) {
00091 helpExists = QFile::exists( *it + "/" + helpFile );
00092
00093 if( !helpExists && QFile::exists( *it + "/" + exec + "/" + helpFile ) ) {
00094 helpFile = exec + "/" + helpFile;
00095 helpExists = true;
00096 }
00097 }
00098
00099 if ( helpExists ) {
00100 r += "<h3><a href="+helpFile+"><img src="+icon+">"+name+"</a></h3>\n";
00101 }
00102 }
00103 return r;
00104 }