1
0
mirror of git://projects.qi-hardware.com/qvido.git synced 2024-11-01 09:16:14 +02:00

lots of updates regarding encoding, splitting and decoding :)

Signed-off-by: Mirko Lindner <mirko@sharism.cc>
This commit is contained in:
Mirko Lindner 2010-04-07 18:22:54 +02:00
parent 99b29f0d1f
commit c8f470aed3
5 changed files with 200 additions and 64 deletions

View File

@ -34,6 +34,18 @@
log_define("qvido.htmlarea"); log_define("qvido.htmlarea");
bool UserEventFilter::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
log_debug("Ate key press " << keyEvent->key());
return true;
} else {
// standard event processing
return QObject::eventFilter(obj, event);
}
}
/////////////////////////////////////////// ///////////////////////////////////////////
// Functions related to htmlarea // Functions related to htmlarea
void HtmlArea::linking( const QUrl &txt ){ void HtmlArea::linking( const QUrl &txt ){
@ -43,12 +55,19 @@ void HtmlArea::linking( const QUrl &txt ){
if (str.startsWith("#") == 1){ if (str.startsWith("#") == 1){
this->scrollToAnchor(str); this->scrollToAnchor(str);
}else{ }else{
log_debug(str.toUtf8().data());
this->setSource(this->source()); this->setSource(this->source());
this->setNewContent(1, str); this->setNewContent(1, str);
} }
} }
void HtmlArea::setNewContent( int method, QString str = "" ){ void HtmlArea::setNewContent( int method, QString str = "" ){
qobject_cast<QMain *>(this->parentWidget())->setEnabled(false);
QString current = this->toHtml();
this->setText("<html><body><br/><br/><br/><br/><p>Loading ... </p></body></html>");
qApp->processEvents();
zim::Article article; zim::Article article;
if (method == 0){ if (method == 0){
@ -60,18 +79,22 @@ void HtmlArea::setNewContent( int method, QString str = "" ){
zim::Search::Results result = this->searchArticleFromTitle(str); zim::Search::Results result = this->searchArticleFromTitle(str);
// if result = 1 display article // if result = 1 display article
if (result.size() == 1){ if (result.size() == 1){
log_debug("result size 1");
//create ZIM file accessor //create ZIM file accessor
zim::File file = this->get_file(); zim::File file = this->get_file();
article = file.getArticle(result[0].getArticle().getIndex()); article = file.getArticle(result[0].getArticle().getIndex());
} }
// if result > 1 display options return at end // if result > 1 display options return at end
else if (result.size() > 1){ else if (result.size() > 1){
log_debug("more than one result");
QString res; QString res;
for (unsigned i = 0; i < result.size(); ++i) for (unsigned i = 0; i < result.size(); ++i)
{ {
res.append("<li><a href='" + QString::fromStdString(result[i].getArticle().getUrl()) + "'>" + QString::fromStdString(result[i].getArticle().getTitle()) + "</a></li>"); res.append("<li><a href='" + QString::fromUtf8(result[i].getArticle().getUrl().c_str()) + "'>" + QString::fromUtf8(result[i].getArticle().getTitle().c_str()) + "</a></li>");
} }
this->setText(res); this->setText(res);
qApp->processEvents();
qobject_cast<QMain *>(this->parentWidget())->setEnabled(true);
return; return;
} }
@ -79,13 +102,31 @@ void HtmlArea::setNewContent( int method, QString str = "" ){
//check if article is valid, if display if not show error //check if article is valid, if display if not show error
if (article.good()){ 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 //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<QMain *>(this->parentWidget())->setEnabled(true);
}else{ }else{
this->setText(current);
//construct error text //construct error text
QString errText = "The article you requested ("; QString errText = "The article you requested (";
errText.append(str); errText.append(str);
errText.append(") could not be found."); errText.append(") could not be found.");
log_debug("string: " << str.toUtf8().data());
qApp->processEvents();
qobject_cast<QMain *>(this->parentWidget())->setEnabled(true);
//construct and display error box //construct and display error box
QMessageBox::information(qobject_cast<QMain *>(this->parentWidget()), "Error", QMessageBox::information(qobject_cast<QMain *>(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? // // TODO ...is there any way to connect setNewContent to Actions directly using default values?
void HtmlArea::getRandom(){ void HtmlArea::getRandom(){
log_debug("getRandom called"); log_debug("getRandom called");
this->setNewContent( 0 ); if(this->isEnabled()){
this->setNewContent( 0 );
}
} }
void HtmlArea::searchArticle(QString term){ void HtmlArea::searchArticle(QString term){
log_debug("searchArticle called"); 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 // Zimlib functions
@ -126,7 +213,6 @@ const zim::File& HtmlArea::get_file()
} }
//get random article //get random article
//TODO get new article if article returned is current article
zim::Article HtmlArea::getRandomArticle(){ zim::Article HtmlArea::getRandomArticle(){
//get file //get file
@ -134,24 +220,29 @@ zim::Article HtmlArea::getRandomArticle(){
//create empty article //create empty article
zim::Article article; zim::Article article;
// log_debug("before random loop");
// do do
// { {
// generate random number // generate random number
unsigned int seed = static_cast<unsigned int>(time(0)); unsigned int seed = static_cast<unsigned int>(time(0));
zim::size_type idx = static_cast<zim::size_type>(static_cast<double>(file.getCountArticles()) * rand_r(&seed) / RAND_MAX); zim::size_type idx = static_cast<zim::size_type>(static_cast<double>(file.getCountArticles()) * rand_r(&seed) / RAND_MAX);
//retrieve article //retrieve article
article = file.getArticle(idx); article = file.getArticle(idx);
// article = file.getArticle(151);
log_debug("idx: " << idx);
//loop in case article is redirect //loop in case article is redirect
// log_debug("before redirect loop");
do do
{ {
article = article.getRedirectArticle(); article = article.getRedirectArticle();
log_debug("random url: " << article.getUrl());
}while(article.isRedirect()); }while(article.isRedirect());
// }while(article.getUrl() == current_url); }while(article.getUrl() == this->url.toStdString());
// log_debug("after random loop");
return article; return article;
} }
@ -174,6 +265,9 @@ zim::Article HtmlArea::getArticleFromUrl(QString url)
//check for different namespace //check for different namespace
if (term.contains("/")) if (term.contains("/"))
{ {
if(term.startsWith("/")){
term.remove(0,1);
}
int index = term.indexOf("/"); int index = term.indexOf("/");
if ( index == 1) if ( index == 1)
{ {
@ -194,7 +288,11 @@ zim::Article HtmlArea::getArticleFromUrl(QString url)
// // // try to retrieve article // // // try to retrieve article
try 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; return article;
} }
catch (const std::exception& e) catch (const std::exception& e)
@ -215,47 +313,9 @@ zim::Search::Results HtmlArea::searchArticleFromTitle(QString phrase)
zim::Search::Results result; zim::Search::Results result;
zim::Search search(file); zim::Search search(file);
search.setSearchLimit(25); 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; 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 += "<li><a href='" + result[i].getArticle().getUrl() + "'>" + result[i].getArticle().getUrl() + "</a></li>";
// }
//
//
// }
//
// // fill_gtkhtml(res, url, title);
// }
// }

View File

@ -23,6 +23,17 @@
#include <zim/file.h> #include <zim/file.h>
#include <zim/search.h> #include <zim/search.h>
class UserEventFilter : public QObject
{
Q_OBJECT
public:
UserEventFilter() : QObject() {}
protected:
bool eventFilter(QObject *obj, QEvent *event);
};
class HtmlArea : public QTextBrowser class HtmlArea : public QTextBrowser
{ {
Q_OBJECT Q_OBJECT
@ -32,14 +43,17 @@ class HtmlArea : public QTextBrowser
void searchArticle(QString term); void searchArticle(QString term);
private: private:
QString *url; QString url;
QObject filter;
zim::Article getArticleFromUrl(QString url); zim::Article getArticleFromUrl(QString url);
zim::Search::Results searchArticleFromTitle(QString phrase); zim::Search::Results searchArticleFromTitle(QString phrase);
zim::Article getRandomArticle(); zim::Article getRandomArticle();
const zim::File& get_file(); const zim::File& get_file();
void setNewContent( int method, QString str ); void setNewContent( int method, QString str );
QVariant loadResource(int type, const QUrl &name);
public Q_SLOTS: public Q_SLOTS:
void linking( const QUrl &txt ); void linking( const QUrl &txt );
void sourceChange( const QUrl &txt );
void getRandom(); void getRandom();
}; };

View File

@ -1,35 +1,87 @@
/**************************************************************************** /****************************************************************************
** Meta object code from reading C++ file 'htmlarea.h' ** 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) ** by: The Qt Meta Object Compiler version 61 (Qt 4.5.2)
** **
** WARNING! All changes made in this file will be lost! ** WARNING! All changes made in this file will be lost!
*****************************************************************************/ *****************************************************************************/
#include "htmlarea.h" #include "htmlarea.h"
#if !defined(Q_MOC_OUTPUT_REVISION)
#error "The header file 'htmlarea.h' doesn't include <QObject>."
#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 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<void*>(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[] = { static const uint qt_meta_data_HtmlArea[] = {
// content: // content:
2, // revision 2, // revision
0, // classname 0, // classname
0, 0, // classinfo 0, 0, // classinfo
2, 12, // methods 3, 12, // methods
0, 0, // properties 0, 0, // properties
0, 0, // enums/sets 0, 0, // enums/sets
0, 0, // constructors 0, 0, // constructors
// slots: signature, parameters, type, tag, flags // slots: signature, parameters, type, tag, flags
14, 10, 9, 9, 0x0a, 14, 10, 9, 9, 0x0a,
28, 9, 9, 9, 0x0a, 28, 10, 9, 9, 0x0a,
47, 9, 9, 9, 0x0a,
0 // eod 0 // eod
}; };
static const char qt_meta_stringdata_HtmlArea[] = { 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 = { const QMetaObject HtmlArea::staticMetaObject = {
@ -58,10 +110,11 @@ int HtmlArea::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
if (_c == QMetaObject::InvokeMetaMethod) { if (_c == QMetaObject::InvokeMetaMethod) {
switch (_id) { switch (_id) {
case 0: linking((*reinterpret_cast< const QUrl(*)>(_a[1]))); break; 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: ; default: ;
} }
_id -= 2; _id -= 3;
} }
return _id; return _id;
} }

View File

@ -1,13 +1,20 @@
/**************************************************************************** /****************************************************************************
** Meta object code from reading C++ file 'qmain.h' ** 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) ** by: The Qt Meta Object Compiler version 61 (Qt 4.5.2)
** **
** WARNING! All changes made in this file will be lost! ** WARNING! All changes made in this file will be lost!
*****************************************************************************/ *****************************************************************************/
#include "qmain.h" #include "qmain.h"
#if !defined(Q_MOC_OUTPUT_REVISION)
#error "The header file 'qmain.h' doesn't include <QObject>."
#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 QT_BEGIN_MOC_NAMESPACE
static const uint qt_meta_data_QMain[] = { static const uint qt_meta_data_QMain[] = {

View File

@ -37,7 +37,9 @@ QMain::QMain(std::string File)
registerCommands(); registerCommands();
QObject::connect(centralWidget,SIGNAL(anchorClicked(const QUrl &)), QObject::connect(centralWidget,SIGNAL(anchorClicked(const QUrl &)),
centralWidget,SLOT(linking(const QUrl&))); centralWidget,SLOT(linking(const QUrl&)));
QObject::connect(centralWidget,SIGNAL(sourceChanged(const QUrl &)),
centralWidget,SLOT(sourceChange(const QUrl&)));
} }
QMain::~QMain(){} QMain::~QMain(){}