diff --git a/htmlarea.cpp b/htmlarea.cpp
index 2d3e2c9..0340519 100644
--- a/htmlarea.cpp
+++ b/htmlarea.cpp
@@ -34,6 +34,18 @@
log_define("qvido.htmlarea");
+bool UserEventFilter::eventFilter(QObject *obj, QEvent *event)
+{
+ if (event->type() == QEvent::KeyPress) {
+ QKeyEvent *keyEvent = static_cast(event);
+ log_debug("Ate key press " << keyEvent->key());
+ return true;
+ } else {
+ // standard event processing
+ return QObject::eventFilter(obj, event);
+ }
+}
+
///////////////////////////////////////////
// Functions related to htmlarea
void HtmlArea::linking( const QUrl &txt ){
@@ -43,12 +55,19 @@ void HtmlArea::linking( const QUrl &txt ){
if (str.startsWith("#") == 1){
this->scrollToAnchor(str);
}else{
+ log_debug(str.toUtf8().data());
this->setSource(this->source());
this->setNewContent(1, str);
}
}
void HtmlArea::setNewContent( int method, QString str = "" ){
+
+ qobject_cast(this->parentWidget())->setEnabled(false);
+ QString current = this->toHtml();
+ this->setText("
Loading ...
");
+ qApp->processEvents();
+
zim::Article article;
if (method == 0){
@@ -60,18 +79,22 @@ void HtmlArea::setNewContent( int method, QString str = "" ){
zim::Search::Results result = this->searchArticleFromTitle(str);
// if result = 1 display article
if (result.size() == 1){
+ log_debug("result size 1");
//create ZIM file accessor
zim::File file = this->get_file();
article = file.getArticle(result[0].getArticle().getIndex());
}
// if result > 1 display options return at end
else if (result.size() > 1){
+ log_debug("more than one result");
QString res;
for (unsigned i = 0; i < result.size(); ++i)
{
- res.append("" + QString::fromStdString(result[i].getArticle().getTitle()) + "");
+ res.append("" + QString::fromUtf8(result[i].getArticle().getTitle().c_str()) + "");
}
this->setText(res);
+ qApp->processEvents();
+ qobject_cast(this->parentWidget())->setEnabled(true);
return;
}
@@ -79,13 +102,31 @@ void HtmlArea::setNewContent( int method, QString str = "" ){
//check if article is valid, if display if not show error
if (article.good()){
+ log_debug("article is good");
+ this->url = QString::fromUtf8(article.getUrl().c_str());
+ log_debug("Url set");
//TODO build history, set Title
- this->setText(QString::fromStdString(article.getPage()));
+ //TODO certain zimfiles break here
+ // example full german file breaks here with article "Trockenrasierer"
+ std::string content = article.getPage();
+ this->setText(QString::fromUtf8(content.c_str()));
+
+ log_debug("Text set");
+// log_debug("index: " << article.getIndex());
+// log_debug("enabled: " << this->isEnabled());
+ log_debug("string: " << str.toUtf8().data());
+ qApp->processEvents();
+ qobject_cast(this->parentWidget())->setEnabled(true);
+
}else{
+ this->setText(current);
//construct error text
QString errText = "The article you requested (";
errText.append(str);
errText.append(") could not be found.");
+ log_debug("string: " << str.toUtf8().data());
+ qApp->processEvents();
+ qobject_cast(this->parentWidget())->setEnabled(true);
//construct and display error box
QMessageBox::information(qobject_cast(this->parentWidget()), "Error",
@@ -99,16 +140,62 @@ void HtmlArea::setNewContent( int method, QString str = "" ){
// // TODO ...is there any way to connect setNewContent to Actions directly using default values?
void HtmlArea::getRandom(){
log_debug("getRandom called");
- this->setNewContent( 0 );
+ if(this->isEnabled()){
+ this->setNewContent( 0 );
+ }
}
void HtmlArea::searchArticle(QString term){
log_debug("searchArticle called");
- this->setNewContent( 2 , term);
+ if(this->isEnabled()){
+ this->setNewContent( 2 , term);
+ }
}
//
////////////////////////////////////////////
+////////////////////////////////////////////
+// Debug functions
+void HtmlArea::sourceChange( const QUrl &txt ){
+ QString str = txt.toString();
+ log_debug("source: " << str.toStdString());
+}
+
+
+//
+////////////////////////////////////////////
+
+//reimplimentations
+QVariant HtmlArea::loadResource(int type, const QUrl &name)
+{
+
+ QVariant qv;
+
+ // check if requested resource is an image
+ if (type == 2){
+ zim::Article article;
+ article = getArticleFromUrl(name.toString());
+
+ if(article.good()){
+ QString mtype(article.getMimeType().c_str());
+
+ QByteArray data = QByteArray::fromRawData(article.getData().data(), article.getData().size());
+
+ QImage img;
+
+ mtype.remove("image/");
+ img.loadFromData(data, mtype.toUpper().toLatin1());
+
+// log_debug("index: " << article.getIndex() << " height: " << article.getData().size() << " mtype: " << std::string(mtype.toUpper().toLatin1()));
+ qv = img;
+
+ }
+ }
+
+ return qv;
+}
+
+
////////////////////////////////////////////
// Zimlib functions
@@ -126,7 +213,6 @@ const zim::File& HtmlArea::get_file()
}
//get random article
-//TODO get new article if article returned is current article
zim::Article HtmlArea::getRandomArticle(){
//get file
@@ -134,24 +220,29 @@ zim::Article HtmlArea::getRandomArticle(){
//create empty article
zim::Article article;
-
-// do
-// {
+// log_debug("before random loop");
+ do
+ {
// generate random number
unsigned int seed = static_cast(time(0));
zim::size_type idx = static_cast(static_cast(file.getCountArticles()) * rand_r(&seed) / RAND_MAX);
//retrieve article
article = file.getArticle(idx);
-
+// article = file.getArticle(151);
+ log_debug("idx: " << idx);
//loop in case article is redirect
+// log_debug("before redirect loop");
do
{
article = article.getRedirectArticle();
+ log_debug("random url: " << article.getUrl());
}while(article.isRedirect());
-// }while(article.getUrl() == current_url);
-
+ }while(article.getUrl() == this->url.toStdString());
+
+// log_debug("after random loop");
+
return article;
}
@@ -174,6 +265,9 @@ zim::Article HtmlArea::getArticleFromUrl(QString url)
//check for different namespace
if (term.contains("/"))
{
+ if(term.startsWith("/")){
+ term.remove(0,1);
+ }
int index = term.indexOf("/");
if ( index == 1)
{
@@ -194,7 +288,11 @@ zim::Article HtmlArea::getArticleFromUrl(QString url)
// // // try to retrieve article
try
{
- article = file.getArticle(ns, term.toStdString());
+ article = file.getArticle(ns, term.toUtf8().data());
+// QString mtype(article.getMimeType().c_str());
+// if(mtype.contains("image/") == false){
+// log_debug("term :" << term.toStdString());
+// }
return article;
}
catch (const std::exception& e)
@@ -215,47 +313,9 @@ zim::Search::Results HtmlArea::searchArticleFromTitle(QString phrase)
zim::Search::Results result;
zim::Search search(file);
search.setSearchLimit(25);
- search.find(result, ns, phrase.toStdString());
+ char *terms = phrase.toUtf8().data();
+ search.find(result, ns, terms);
+ log_debug("term :" << phrase.toUtf8().data());
return result;
}
-
-// zim::Article HtmlArea::getArticleFromTitle(QString *phrase)
-// {
-// // historyCall = false;
-// // screenblock(1);
-//
-// if( result.size() == 0)
-// {
-// // show_message("Error", "The article you requested (" + term + ") was not found.");
-// // screenblock(0);
-// return article;
-// }else{
-//
-// if (result.size() == 1){
-// log_debug("one article in result");
-//
-// article = z.getArticle(result[0].getArticle().getIndex());
-//
-// //loop in case article is redirect
-// do
-// {
-// article = article.getRedirectArticle();
-// }while(article.isRedirect());
-//
-// return article;
-// }
-// else
-// {
-// log_debug("more than one article in result");
-// for (unsigned i = 0; i < result.size(); ++i)
-// {
-// res += "" + result[i].getArticle().getUrl() + "";
-// }
-//
-//
-// }
-//
-// // fill_gtkhtml(res, url, title);
-// }
-// }
diff --git a/htmlarea.h b/htmlarea.h
index 91441d8..1bcd095 100644
--- a/htmlarea.h
+++ b/htmlarea.h
@@ -23,6 +23,17 @@
#include
#include
+class UserEventFilter : public QObject
+{
+ Q_OBJECT
+
+ public:
+ UserEventFilter() : QObject() {}
+
+ protected:
+ bool eventFilter(QObject *obj, QEvent *event);
+};
+
class HtmlArea : public QTextBrowser
{
Q_OBJECT
@@ -32,14 +43,17 @@ class HtmlArea : public QTextBrowser
void searchArticle(QString term);
private:
- QString *url;
+ QString url;
+ QObject filter;
zim::Article getArticleFromUrl(QString url);
zim::Search::Results searchArticleFromTitle(QString phrase);
zim::Article getRandomArticle();
const zim::File& get_file();
void setNewContent( int method, QString str );
-
+ QVariant loadResource(int type, const QUrl &name);
+
public Q_SLOTS:
void linking( const QUrl &txt );
+ void sourceChange( const QUrl &txt );
void getRandom();
-};
\ No newline at end of file
+};
diff --git a/moc_htmlarea.cpp b/moc_htmlarea.cpp
index efe45fa..251ce73 100644
--- a/moc_htmlarea.cpp
+++ b/moc_htmlarea.cpp
@@ -1,35 +1,87 @@
/****************************************************************************
** Meta object code from reading C++ file 'htmlarea.h'
**
-** Created: Mon Mar 29 11:57:07 2010
+** Created: Wed Apr 7 16:52:55 2010
** by: The Qt Meta Object Compiler version 61 (Qt 4.5.2)
**
** WARNING! All changes made in this file will be lost!
*****************************************************************************/
#include "htmlarea.h"
+#if !defined(Q_MOC_OUTPUT_REVISION)
+#error "The header file 'htmlarea.h' doesn't include ."
+#elif Q_MOC_OUTPUT_REVISION != 61
+#error "This file was generated using the moc from 4.5.2. It"
+#error "cannot be used with the include files from this version of Qt."
+#error "(The moc has changed too much.)"
+#endif
QT_BEGIN_MOC_NAMESPACE
+static const uint qt_meta_data_UserEventFilter[] = {
+
+ // content:
+ 2, // revision
+ 0, // classname
+ 0, 0, // classinfo
+ 0, 0, // methods
+ 0, 0, // properties
+ 0, 0, // enums/sets
+ 0, 0, // constructors
+
+ 0 // eod
+};
+
+static const char qt_meta_stringdata_UserEventFilter[] = {
+ "UserEventFilter\0"
+};
+
+const QMetaObject UserEventFilter::staticMetaObject = {
+ { &QObject::staticMetaObject, qt_meta_stringdata_UserEventFilter,
+ qt_meta_data_UserEventFilter, 0 }
+};
+
+const QMetaObject *UserEventFilter::metaObject() const
+{
+ return &staticMetaObject;
+}
+
+void *UserEventFilter::qt_metacast(const char *_clname)
+{
+ if (!_clname) return 0;
+ if (!strcmp(_clname, qt_meta_stringdata_UserEventFilter))
+ return static_cast(const_cast< UserEventFilter*>(this));
+ return QObject::qt_metacast(_clname);
+}
+
+int UserEventFilter::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
+{
+ _id = QObject::qt_metacall(_c, _id, _a);
+ if (_id < 0)
+ return _id;
+ return _id;
+}
static const uint qt_meta_data_HtmlArea[] = {
// content:
2, // revision
0, // classname
0, 0, // classinfo
- 2, 12, // methods
+ 3, 12, // methods
0, 0, // properties
0, 0, // enums/sets
0, 0, // constructors
// slots: signature, parameters, type, tag, flags
14, 10, 9, 9, 0x0a,
- 28, 9, 9, 9, 0x0a,
+ 28, 10, 9, 9, 0x0a,
+ 47, 9, 9, 9, 0x0a,
0 // eod
};
static const char qt_meta_stringdata_HtmlArea[] = {
- "HtmlArea\0\0txt\0linking(QUrl)\0getRandom()\0"
+ "HtmlArea\0\0txt\0linking(QUrl)\0"
+ "sourceChange(QUrl)\0getRandom()\0"
};
const QMetaObject HtmlArea::staticMetaObject = {
@@ -58,10 +110,11 @@ int HtmlArea::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
if (_c == QMetaObject::InvokeMetaMethod) {
switch (_id) {
case 0: linking((*reinterpret_cast< const QUrl(*)>(_a[1]))); break;
- case 1: getRandom(); break;
+ case 1: sourceChange((*reinterpret_cast< const QUrl(*)>(_a[1]))); break;
+ case 2: getRandom(); break;
default: ;
}
- _id -= 2;
+ _id -= 3;
}
return _id;
}
diff --git a/moc_qmain.cpp b/moc_qmain.cpp
index a6db185..6717eca 100644
--- a/moc_qmain.cpp
+++ b/moc_qmain.cpp
@@ -1,13 +1,20 @@
/****************************************************************************
** Meta object code from reading C++ file 'qmain.h'
**
-** Created: Mon Mar 29 11:57:07 2010
+** Created: Wed Apr 7 16:52:55 2010
** by: The Qt Meta Object Compiler version 61 (Qt 4.5.2)
**
** WARNING! All changes made in this file will be lost!
*****************************************************************************/
#include "qmain.h"
+#if !defined(Q_MOC_OUTPUT_REVISION)
+#error "The header file 'qmain.h' doesn't include ."
+#elif Q_MOC_OUTPUT_REVISION != 61
+#error "This file was generated using the moc from 4.5.2. It"
+#error "cannot be used with the include files from this version of Qt."
+#error "(The moc has changed too much.)"
+#endif
QT_BEGIN_MOC_NAMESPACE
static const uint qt_meta_data_QMain[] = {
diff --git a/qmain.cpp b/qmain.cpp
index 084a3fc..5cd0886 100644
--- a/qmain.cpp
+++ b/qmain.cpp
@@ -37,7 +37,9 @@ QMain::QMain(std::string File)
registerCommands();
QObject::connect(centralWidget,SIGNAL(anchorClicked(const QUrl &)),
centralWidget,SLOT(linking(const QUrl&)));
-
+ QObject::connect(centralWidget,SIGNAL(sourceChanged(const QUrl &)),
+ centralWidget,SLOT(sourceChange(const QUrl&)));
+
}
QMain::~QMain(){}