Date:2010-04-07 18:22:54 (10 years 4 months ago)
Author:Mirko Lindner
Commit:c8f470aed355731bc128df9b0d5f550edcd6aeba
Message:lots of updates regarding encoding, splitting and decoding :)

Signed-off-by: Mirko Lindner <mirko@sharism.cc>
Files: htmlarea.cpp (10 diffs)
htmlarea.h (2 diffs)
moc_htmlarea.cpp (2 diffs)
moc_qmain.cpp (1 diff)
qmain.cpp (1 diff)

Change Details

htmlarea.cpp
3434
3535log_define("qvido.htmlarea");
3636
37bool UserEventFilter::eventFilter(QObject *obj, QEvent *event)
38{
39        if (event->type() == QEvent::KeyPress) {
40                QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
41                log_debug("Ate key press " << keyEvent->key());
42                return true;
43        } else {
44                // standard event processing
45                return QObject::eventFilter(obj, event);
46        }
47}
48
3749///////////////////////////////////////////
3850// Functions related to htmlarea
3951void HtmlArea::linking( const QUrl &txt ){
...... 
4355    if (str.startsWith("#") == 1){
4456        this->scrollToAnchor(str);
4557    }else{
58        log_debug(str.toUtf8().data());
4659        this->setSource(this->source());
4760        this->setNewContent(1, str);
4861    }
4962}
5063
5164void HtmlArea::setNewContent( int method, QString str = "" ){
65
66    qobject_cast<QMain *>(this->parentWidget())->setEnabled(false);
67    QString current = this->toHtml();
68    this->setText("<html><body><br/><br/><br/><br/><p>Loading ... </p></body></html>");
69    qApp->processEvents();
70
5271    zim::Article article;
5372
5473    if (method == 0){
...... 
6079        zim::Search::Results result = this->searchArticleFromTitle(str);
6180        // if result = 1 display article
6281        if (result.size() == 1){
82            log_debug("result size 1");
6383            //create ZIM file accessor
6484            zim::File file = this->get_file();
6585            article = file.getArticle(result[0].getArticle().getIndex());
6686        }
6787        // if result > 1 display options return at end
6888        else if (result.size() > 1){
89            log_debug("more than one result");
6990            QString res;
7091            for (unsigned i = 0; i < result.size(); ++i)
7192        {
72          res.append("<li><a href='" + QString::fromStdString(result[i].getArticle().getUrl()) + "'>" + QString::fromStdString(result[i].getArticle().getTitle()) + "</a></li>");
93          res.append("<li><a href='" + QString::fromUtf8(result[i].getArticle().getUrl().c_str()) + "'>" + QString::fromUtf8(result[i].getArticle().getTitle().c_str()) + "</a></li>");
7394        }
7495            this->setText(res);
96            qApp->processEvents();
97            qobject_cast<QMain *>(this->parentWidget())->setEnabled(true);
7598            return;
7699        }
77100
...... 
79102
80103    //check if article is valid, if display if not show error
81104    if (article.good()){
105        log_debug("article is good");
106        this->url = QString::fromUtf8(article.getUrl().c_str());
107        log_debug("Url set");
82108        //TODO build history, set Title
83        this->setText(QString::fromStdString(article.getPage()));
109        //TODO certain zimfiles break here
110        // example full german file breaks here with article "Trockenrasierer"
111        std::string content = article.getPage();
112        this->setText(QString::fromUtf8(content.c_str()));
113
114        log_debug("Text set");
115// log_debug("index: " << article.getIndex());
116// log_debug("enabled: " << this->isEnabled());
117        log_debug("string: " << str.toUtf8().data());
118        qApp->processEvents();
119        qobject_cast<QMain *>(this->parentWidget())->setEnabled(true);
120
84121    }else{
122        this->setText(current);
85123        //construct error text
86124        QString errText = "The article you requested (";
87125        errText.append(str);
88126        errText.append(") could not be found.");
127        log_debug("string: " << str.toUtf8().data());
128        qApp->processEvents();
129        qobject_cast<QMain *>(this->parentWidget())->setEnabled(true);
89130
90131        //construct and display error box
91132        QMessageBox::information(qobject_cast<QMain *>(this->parentWidget()), "Error",
...... 
99140// // TODO ...is there any way to connect setNewContent to Actions directly using default values?
100141void HtmlArea::getRandom(){
101142    log_debug("getRandom called");
102    this->setNewContent( 0 );
143    if(this->isEnabled()){
144        this->setNewContent( 0 );
145    }
103146}
104147
105148void HtmlArea::searchArticle(QString term){
106149    log_debug("searchArticle called");
107    this->setNewContent( 2 , term);
150    if(this->isEnabled()){
151        this->setNewContent( 2 , term);
152    }
108153}
109154
110155//
111156////////////////////////////////////////////
112157////////////////////////////////////////////
158// Debug functions
159void HtmlArea::sourceChange( const QUrl &txt ){
160    QString str = txt.toString();
161    log_debug("source: " << str.toStdString());
162}
163
164
165//
166////////////////////////////////////////////
167
168//reimplimentations
169QVariant HtmlArea::loadResource(int type, const QUrl &name)
170{
171
172        QVariant qv;
173
174        // check if requested resource is an image
175        if (type == 2){
176            zim::Article article;
177            article = getArticleFromUrl(name.toString());
178
179            if(article.good()){
180                QString mtype(article.getMimeType().c_str());
181
182                QByteArray data = QByteArray::fromRawData(article.getData().data(), article.getData().size());
183
184                QImage img;
185
186                mtype.remove("image/");
187                img.loadFromData(data, mtype.toUpper().toLatin1());
188
189// log_debug("index: " << article.getIndex() << " height: " << article.getData().size() << " mtype: " << std::string(mtype.toUpper().toLatin1()));
190                qv = img;
191
192            }
193        }
194
195        return qv;
196}
197
198
199////////////////////////////////////////////
113200// Zimlib functions
114201
115202const zim::File& HtmlArea::get_file()
...... 
126213}
127214
128215//get random article
129//TODO get new article if article returned is current article
130216zim::Article HtmlArea::getRandomArticle(){
131217
132218        //get file
...... 
134220
135221        //create empty article
136222        zim::Article article;
137
138// do
139// {
223// log_debug("before random loop");
224    do
225        {
140226                // generate random number
141227                unsigned int seed = static_cast<unsigned int>(time(0));
142228                zim::size_type idx = static_cast<zim::size_type>(static_cast<double>(file.getCountArticles()) * rand_r(&seed) / RAND_MAX);
143229
144230                //retrieve article
145231                article = file.getArticle(idx);
146
232// article = file.getArticle(151);
233                log_debug("idx: " << idx);
147234                //loop in case article is redirect
235// log_debug("before redirect loop");
148236                do
149237                {
150238                        article = article.getRedirectArticle();
239                        log_debug("random url: " << article.getUrl());
151240                }while(article.isRedirect());
152241
153// }while(article.getUrl() == current_url);
154
242        }while(article.getUrl() == this->url.toStdString());
243
244// log_debug("after random loop");
245
155246        return article;
156247}
157248
...... 
174265        //check for different namespace
175266    if (term.contains("/"))
176267    {
268                if(term.startsWith("/")){
269                    term.remove(0,1);
270                }
177271                int index = term.indexOf("/");
178272        if ( index == 1)
179273        {
...... 
194288// // // try to retrieve article
195289    try
196290    {
197            article = file.getArticle(ns, term.toStdString());
291            article = file.getArticle(ns, term.toUtf8().data());
292// QString mtype(article.getMimeType().c_str());
293// if(mtype.contains("image/") == false){
294// log_debug("term :" << term.toStdString());
295// }
198296            return article;
199297    }
200298    catch (const std::exception& e)
...... 
215313    zim::Search::Results result;
216314    zim::Search search(file);
217315    search.setSearchLimit(25);
218    search.find(result, ns, phrase.toStdString());
316        char *terms = phrase.toUtf8().data();
219317
318    search.find(result, ns, terms);
319        log_debug("term :" << phrase.toUtf8().data());
220320        return result;
221321}
222
223// zim::Article HtmlArea::getArticleFromTitle(QString *phrase)
224// {
225// // historyCall = false;
226// // screenblock(1);
227//
228// if( result.size() == 0)
229// {
230// // show_message("Error", "The article you requested (" + term + ") was not found.");
231// // screenblock(0);
232// return article;
233// }else{
234//
235// if (result.size() == 1){
236// log_debug("one article in result");
237//
238// article = z.getArticle(result[0].getArticle().getIndex());
239//
240// //loop in case article is redirect
241// do
242// {
243// article = article.getRedirectArticle();
244// }while(article.isRedirect());
245//
246// return article;
247// }
248// else
249// {
250// log_debug("more than one article in result");
251// for (unsigned i = 0; i < result.size(); ++i)
252// {
253// res += "<li><a href='" + result[i].getArticle().getUrl() + "'>" + result[i].getArticle().getUrl() + "</a></li>";
254// }
255//
256//
257// }
258//
259// // fill_gtkhtml(res, url, title);
260// }
261// }
htmlarea.h
2323#include <zim/file.h>
2424#include <zim/search.h>
2525
26class UserEventFilter : public QObject
27{
28        Q_OBJECT
29
30    public:
31        UserEventFilter() : QObject() {}
32
33    protected:
34        bool eventFilter(QObject *obj, QEvent *event);
35};
36
2637class HtmlArea : public QTextBrowser
2738{
2839    Q_OBJECT
...... 
3243        void searchArticle(QString term);
3344
3445    private:
35        QString *url;
46        QString url;
47        QObject filter;
3648        zim::Article getArticleFromUrl(QString url);
3749        zim::Search::Results searchArticleFromTitle(QString phrase);
3850        zim::Article getRandomArticle();
3951        const zim::File& get_file();
4052        void setNewContent( int method, QString str );
41
53        QVariant loadResource(int type, const QUrl &name);
54
4255    public Q_SLOTS:
4356        void linking( const QUrl &txt );
57        void sourceChange( const QUrl &txt );
4458        void getRandom();
45};
59};
moc_htmlarea.cpp
11/****************************************************************************
22** Meta object code from reading C++ file 'htmlarea.h'
33**
4** Created: Mon Mar 29 11:57:07 2010
4** Created: Wed Apr 7 16:52:55 2010
55** by: The Qt Meta Object Compiler version 61 (Qt 4.5.2)
66**
77** WARNING! All changes made in this file will be lost!
88*****************************************************************************/
99
1010#include "htmlarea.h"
11#if !defined(Q_MOC_OUTPUT_REVISION)
12#error "The header file 'htmlarea.h' doesn't include <QObject>."
13#elif Q_MOC_OUTPUT_REVISION != 61
14#error "This file was generated using the moc from 4.5.2. It"
15#error "cannot be used with the include files from this version of Qt."
16#error "(The moc has changed too much.)"
17#endif
1118
1219QT_BEGIN_MOC_NAMESPACE
20static const uint qt_meta_data_UserEventFilter[] = {
21
22 // content:
23       2, // revision
24       0, // classname
25       0, 0, // classinfo
26       0, 0, // methods
27       0, 0, // properties
28       0, 0, // enums/sets
29       0, 0, // constructors
30
31       0 // eod
32};
33
34static const char qt_meta_stringdata_UserEventFilter[] = {
35    "UserEventFilter\0"
36};
37
38const QMetaObject UserEventFilter::staticMetaObject = {
39    { &QObject::staticMetaObject, qt_meta_stringdata_UserEventFilter,
40      qt_meta_data_UserEventFilter, 0 }
41};
42
43const QMetaObject *UserEventFilter::metaObject() const
44{
45    return &staticMetaObject;
46}
47
48void *UserEventFilter::qt_metacast(const char *_clname)
49{
50    if (!_clname) return 0;
51    if (!strcmp(_clname, qt_meta_stringdata_UserEventFilter))
52        return static_cast<void*>(const_cast< UserEventFilter*>(this));
53    return QObject::qt_metacast(_clname);
54}
55
56int UserEventFilter::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
57{
58    _id = QObject::qt_metacall(_c, _id, _a);
59    if (_id < 0)
60        return _id;
61    return _id;
62}
1363static const uint qt_meta_data_HtmlArea[] = {
1464
1565 // content:
1666       2, // revision
1767       0, // classname
1868       0, 0, // classinfo
19       2, 12, // methods
69       3, 12, // methods
2070       0, 0, // properties
2171       0, 0, // enums/sets
2272       0, 0, // constructors
2373
2474 // slots: signature, parameters, type, tag, flags
2575      14, 10, 9, 9, 0x0a,
26      28, 9, 9, 9, 0x0a,
76      28, 10, 9, 9, 0x0a,
77      47, 9, 9, 9, 0x0a,
2778
2879       0 // eod
2980};
3081
3182static const char qt_meta_stringdata_HtmlArea[] = {
32    "HtmlArea\0\0txt\0linking(QUrl)\0getRandom()\0"
83    "HtmlArea\0\0txt\0linking(QUrl)\0"
84    "sourceChange(QUrl)\0getRandom()\0"
3385};
3486
3587const QMetaObject HtmlArea::staticMetaObject = {
...... 
58110    if (_c == QMetaObject::InvokeMetaMethod) {
59111        switch (_id) {
60112        case 0: linking((*reinterpret_cast< const QUrl(*)>(_a[1]))); break;
61        case 1: getRandom(); break;
113        case 1: sourceChange((*reinterpret_cast< const QUrl(*)>(_a[1]))); break;
114        case 2: getRandom(); break;
62115        default: ;
63116        }
64        _id -= 2;
117        _id -= 3;
65118    }
66119    return _id;
67120}
moc_qmain.cpp
11/****************************************************************************
22** Meta object code from reading C++ file 'qmain.h'
33**
4** Created: Mon Mar 29 11:57:07 2010
4** Created: Wed Apr 7 16:52:55 2010
55** by: The Qt Meta Object Compiler version 61 (Qt 4.5.2)
66**
77** WARNING! All changes made in this file will be lost!
88*****************************************************************************/
99
1010#include "qmain.h"
11#if !defined(Q_MOC_OUTPUT_REVISION)
12#error "The header file 'qmain.h' doesn't include <QObject>."
13#elif Q_MOC_OUTPUT_REVISION != 61
14#error "This file was generated using the moc from 4.5.2. It"
15#error "cannot be used with the include files from this version of Qt."
16#error "(The moc has changed too much.)"
17#endif
1118
1219QT_BEGIN_MOC_NAMESPACE
1320static const uint qt_meta_data_QMain[] = {
qmain.cpp
3737        registerCommands();
3838        QObject::connect(centralWidget,SIGNAL(anchorClicked(const QUrl &)),
3939                                         centralWidget,SLOT(linking(const QUrl&)));
40
40        QObject::connect(centralWidget,SIGNAL(sourceChanged(const QUrl &)),
41                                         centralWidget,SLOT(sourceChange(const QUrl&)));
42
4143}
4244
4345QMain::~QMain(){}

Archive Download the corresponding diff file

Branches:
master



interactive