Date:2010-03-29 12:03:08 (10 years 4 months ago)
Author:Mirko Lindner
Commit:68c4971a2eea0a0102fe78fb7b75e68698358996
Message:initial upload

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

Change Details

htmlarea.cpp
1/***************************************************************************
2 * Copyright (C) 2010 by Mirko Lindner *
3 * mirko@sharism.cc *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21// #include <iostream>
22#include <QtGui>
23#include <QMessageBox>
24#include <QTextBrowser>
25#include <string>
26#include <sstream>
27// #include <vector>
28#include "qmain.h"
29#include <zim/file.h>
30#include <zim/search.h>
31#include <zim/fileimpl.h>
32
33#include <cxxtools/log.h>
34
35log_define("qvido.htmlarea");
36
37///////////////////////////////////////////
38// Functions related to htmlarea
39void HtmlArea::linking( const QUrl &txt ){
40    QString str = txt.toString();
41
42// // check if url is an internal anchor if yes jump if not end request and query zimlib
43    if (str.startsWith("#") == 1){
44        this->scrollToAnchor(str);
45    }else{
46        this->setSource(this->source());
47        this->setNewContent(1, str);
48    }
49}
50
51void HtmlArea::setNewContent( int method, QString str = "" ){
52    zim::Article article;
53
54    if (method == 0){
55        article = this->getRandomArticle();
56    }else if( method == 1){
57        article = this->getArticleFromUrl(str);
58    }else if( method == 2){
59        // first search
60        zim::Search::Results result = this->searchArticleFromTitle(str);
61        // if result = 1 display article
62        if (result.size() == 1){
63            //create ZIM file accessor
64            zim::File file = this->get_file();
65            article = file.getArticle(result[0].getArticle().getIndex());
66        }
67        // if result > 1 display options return at end
68        else if (result.size() > 1){
69            QString res;
70            for (unsigned i = 0; i < result.size(); ++i)
71        {
72          res.append("<li><a href='" + QString::fromStdString(result[i].getArticle().getUrl()) + "'>" + QString::fromStdString(result[i].getArticle().getTitle()) + "</a></li>");
73        }
74            this->setText(res);
75            return;
76        }
77
78    }
79
80    //check if article is valid, if display if not show error
81    if (article.good()){
82        //TODO build history, set Title
83        this->setText(QString::fromStdString(article.getPage()));
84    }else{
85        //construct error text
86        QString errText = "The article you requested (";
87        errText.append(str);
88        errText.append(") could not be found.");
89
90        //construct and display error box
91        QMessageBox::information(qobject_cast<QMain *>(this->parentWidget()), "Error",
92                                                         errText,
93                                                         QMessageBox::Close);
94    }
95
96}
97
98// // slots
99// // TODO ...is there any way to connect setNewContent to Actions directly using default values?
100void HtmlArea::getRandom(){
101    log_debug("getRandom called");
102    this->setNewContent( 0 );
103}
104
105void HtmlArea::searchArticle(QString term){
106    log_debug("searchArticle called");
107    this->setNewContent( 2 , term);
108}
109
110//
111////////////////////////////////////////////
112////////////////////////////////////////////
113// Zimlib functions
114
115const zim::File& HtmlArea::get_file()
116{
117  static zim::File zimFile;
118
119  if (!zimFile.good())
120  {
121    zimFile = zim::File(qobject_cast<QMain *>(this->parentWidget())->fileLocation);
122    log_debug("number of articles: " << zimFile.getCountArticles());
123  }
124
125  return zimFile;
126}
127
128//get random article
129//TODO get new article if article returned is current article
130zim::Article HtmlArea::getRandomArticle(){
131
132        //get file
133        zim::File file = this->get_file();
134
135        //create empty article
136        zim::Article article;
137
138// do
139// {
140                // generate random number
141                unsigned int seed = static_cast<unsigned int>(time(0));
142                zim::size_type idx = static_cast<zim::size_type>(static_cast<double>(file.getCountArticles()) * rand_r(&seed) / RAND_MAX);
143
144                //retrieve article
145                article = file.getArticle(idx);
146
147                //loop in case article is redirect
148                do
149                {
150                        article = article.getRedirectArticle();
151                }while(article.isRedirect());
152
153// }while(article.getUrl() == current_url);
154
155        return article;
156}
157
158zim::Article HtmlArea::getArticleFromUrl(QString url)
159{
160    // TODO unescape url
161
162        //create ZIM file accessor
163    zim::File file = this->get_file();
164
165        //create empty article
166        zim::Article article;
167
168        // remove '+' signs
169    QString term = url.replace( '+', ' ');
170
171        //set default namespace
172    char ns = 'A';
173
174        //check for different namespace
175    if (term.contains("/"))
176    {
177                int index = term.indexOf("/");
178        if ( index == 1)
179        {
180            ns = term[0].toLatin1();
181            term.remove(0, 2);
182        }
183                 //TODO what was this for?
184                /*
185        else
186        {
187            term.erase(1);
188            ns = term[0];
189            found2 = term.find("/");
190            term.erase(0, term.indexOf("/"));
191        }*/
192    }
193
194// // // try to retrieve article
195    try
196    {
197            article = file.getArticle(ns, term.toStdString());
198            return article;
199    }
200    catch (const std::exception& e)
201    {
202        std::cerr << e.what() << std::endl;
203    }
204}
205
206zim::Search::Results HtmlArea::searchArticleFromTitle(QString phrase)
207{
208        char ns;
209    ns = 'A';
210
211        zim::File file = get_file();
212
213        zim::Article article;
214
215    zim::Search::Results result;
216    zim::Search search(file);
217    search.setSearchLimit(25);
218    search.find(result, ns, phrase.toStdString());
219
220        return result;
221}
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
1/***************************************************************************
2 * Copyright (C) 2010 by Mirko Lindner *
3 * mirko@sharism.cc *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21#include <QTextBrowser>
22#include <QWidget>
23#include <zim/file.h>
24#include <zim/search.h>
25
26class HtmlArea : public QTextBrowser
27{
28    Q_OBJECT
29
30    public:
31        HtmlArea(QWidget * parent = 0) : QTextBrowser(parent) {}
32        void searchArticle(QString term);
33
34    private:
35        QString *url;
36        zim::Article getArticleFromUrl(QString url);
37        zim::Search::Results searchArticleFromTitle(QString phrase);
38        zim::Article getRandomArticle();
39        const zim::File& get_file();
40        void setNewContent( int method, QString str );
41
42    public Q_SLOTS:
43        void linking( const QUrl &txt );
44        void getRandom();
45};
main.cpp
1#include <QApplication>
2#include "qmain.h"
3#include <cxxtools/loginit.h>
4#include <cxxtools/log.h>
5log_define("qvido.main")
6
7int main(int argc, char *argv[])
8{
9    log_init();
10    log_debug("launching with arguments" << argv[1] );
11    if (argc == 2)
12    {
13        QApplication app(argc, argv);
14
15        QMain QMain(argv[1]);
16    QMain.show();
17    return app.exec();
18    }else{
19        std::cout << "usage: " << argv[0] << " [PATH TO ZIM FILE]" << std::endl;
20    }
21}
moc_htmlarea.cpp
1/****************************************************************************
2** Meta object code from reading C++ file 'htmlarea.h'
3**
4** Created: Mon Mar 29 11:57:07 2010
5** by: The Qt Meta Object Compiler version 61 (Qt 4.5.2)
6**
7** WARNING! All changes made in this file will be lost!
8*****************************************************************************/
9
10#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
18
19QT_BEGIN_MOC_NAMESPACE
20static const uint qt_meta_data_HtmlArea[] = {
21
22 // content:
23       2, // revision
24       0, // classname
25       0, 0, // classinfo
26       2, 12, // methods
27       0, 0, // properties
28       0, 0, // enums/sets
29       0, 0, // constructors
30
31 // slots: signature, parameters, type, tag, flags
32      14, 10, 9, 9, 0x0a,
33      28, 9, 9, 9, 0x0a,
34
35       0 // eod
36};
37
38static const char qt_meta_stringdata_HtmlArea[] = {
39    "HtmlArea\0\0txt\0linking(QUrl)\0getRandom()\0"
40};
41
42const QMetaObject HtmlArea::staticMetaObject = {
43    { &QTextBrowser::staticMetaObject, qt_meta_stringdata_HtmlArea,
44      qt_meta_data_HtmlArea, 0 }
45};
46
47const QMetaObject *HtmlArea::metaObject() const
48{
49    return &staticMetaObject;
50}
51
52void *HtmlArea::qt_metacast(const char *_clname)
53{
54    if (!_clname) return 0;
55    if (!strcmp(_clname, qt_meta_stringdata_HtmlArea))
56        return static_cast<void*>(const_cast< HtmlArea*>(this));
57    return QTextBrowser::qt_metacast(_clname);
58}
59
60int HtmlArea::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
61{
62    _id = QTextBrowser::qt_metacall(_c, _id, _a);
63    if (_id < 0)
64        return _id;
65    if (_c == QMetaObject::InvokeMetaMethod) {
66        switch (_id) {
67        case 0: linking((*reinterpret_cast< const QUrl(*)>(_a[1]))); break;
68        case 1: getRandom(); break;
69        default: ;
70        }
71        _id -= 2;
72    }
73    return _id;
74}
75QT_END_MOC_NAMESPACE
moc_qmain.cpp
1/****************************************************************************
2** Meta object code from reading C++ file 'qmain.h'
3**
4** Created: Mon Mar 29 11:57:07 2010
5** by: The Qt Meta Object Compiler version 61 (Qt 4.5.2)
6**
7** WARNING! All changes made in this file will be lost!
8*****************************************************************************/
9
10#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
18
19QT_BEGIN_MOC_NAMESPACE
20static const uint qt_meta_data_QMain[] = {
21
22 // content:
23       2, // revision
24       0, // classname
25       0, 0, // classinfo
26       2, 12, // methods
27       0, 0, // properties
28       0, 0, // enums/sets
29       0, 0, // constructors
30
31 // slots: signature, parameters, type, tag, flags
32       7, 6, 6, 6, 0x08,
33      21, 6, 6, 6, 0x08,
34
35       0 // eod
36};
37
38static const char qt_meta_stringdata_QMain[] = {
39    "QMain\0\0displayHelp()\0searchDialog()\0"
40};
41
42const QMetaObject QMain::staticMetaObject = {
43    { &QMainWindow::staticMetaObject, qt_meta_stringdata_QMain,
44      qt_meta_data_QMain, 0 }
45};
46
47const QMetaObject *QMain::metaObject() const
48{
49    return &staticMetaObject;
50}
51
52void *QMain::qt_metacast(const char *_clname)
53{
54    if (!_clname) return 0;
55    if (!strcmp(_clname, qt_meta_stringdata_QMain))
56        return static_cast<void*>(const_cast< QMain*>(this));
57    return QMainWindow::qt_metacast(_clname);
58}
59
60int QMain::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
61{
62    _id = QMainWindow::qt_metacall(_c, _id, _a);
63    if (_id < 0)
64        return _id;
65    if (_c == QMetaObject::InvokeMetaMethod) {
66        switch (_id) {
67        case 0: displayHelp(); break;
68        case 1: searchDialog(); break;
69        default: ;
70        }
71        _id -= 2;
72    }
73    return _id;
74}
75QT_END_MOC_NAMESPACE
qmain.cpp
1/***************************************************************************
2 * Copyright (C) 2010 by Mirko Lindner *
3 * mirko@sharism.cc *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21#include <QtGui>
22#include <QWidget>
23#include <QMessageBox>
24#include "qmain.h"
25
26QMain::QMain(std::string File)
27{
28
29        this->resize(320, 240);
30
31        fileLocation = File;
32
33        centralWidget = new HtmlArea(this);
34    setCentralWidget(centralWidget);
35
36        centralWidget->getRandom();
37        registerCommands();
38        QObject::connect(centralWidget,SIGNAL(anchorClicked(const QUrl &)),
39                                         centralWidget,SLOT(linking(const QUrl&)));
40
41}
42
43void QMain::searchDialog(){
44        bool ok;
45        QString text = QInputDialog::getText(this, tr("Search Article"),
46                                          tr("Article Title"), QLineEdit::Normal,
47                                          "", &ok);
48        if (ok && !text.isEmpty())
49            this->centralWidget->searchArticle(text);
50
51}
52
53void QMain::registerCommands()
54{
55        //get random article
56        randomArticleAct = new QAction(tr("Load &Random Article"), this);
57        randomArticleAct->setShortcut(tr("Ctrl+R"));
58        randomArticleAct->setStatusTip(tr("Random Article"));
59        connect(randomArticleAct, SIGNAL(triggered()), this->centralWidget, SLOT(getRandom()));
60        this->addAction(randomArticleAct);
61
62        //get search dialog
63        searchArticleAct = new QAction(tr("&Search Dialog"), this);
64        searchArticleAct->setShortcut(tr("Ctrl+S"));
65        searchArticleAct->setStatusTip(tr("Search for Article"));
66        connect(searchArticleAct, SIGNAL(triggered()), this, SLOT(searchDialog()));
67        this->addAction(searchArticleAct);
68
69        //display help
70        displayHelpAct = new QAction(tr("Display &H"), this);
71        displayHelpAct->setShortcut(tr("F1"));
72        displayHelpAct->setStatusTip(tr("Display Help"));
73        connect(displayHelpAct, SIGNAL(triggered()), this, SLOT(displayHelp()));
74        this->addAction(displayHelpAct);
75}
76
77void QMain::displayHelp(){
78    QString txt = "Usage: \n";
79    txt.append("Ctrl + R = Display random article\n");
80    txt.append("Ctrl + S = Search for article\n");
81    txt.append("Ctrl + T = Go to articles top\n");
82    txt.append("Tab = Rotate through links\n");
83    txt.append("Enter = Activate link\n");
84    txt.append("Ctrl + H = Display history\n");
85    txt.append("Ctrl + B = Go back in history\n");
86    txt.append("Ctrl + F = Go forward in history\n");
87    txt.append("Ctrl + Q = Quit Vido\n");
88    txt.append("F1 = Display this help\n");
89
90    QMessageBox::information(this, "Help",
91                                                     txt,
92                           QMessageBox::Close);
93
94}
qmain.h
1/***************************************************************************
2 * Copyright (C) 2010 by Mirko Lindner *
3 * mirko@sharism.cc *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21#ifndef QMAIN_H
22#define QMAIN_H
23
24#include <QMainWindow>
25#include <string>
26#include "htmlarea.h"
27
28
29class QMain : public QMainWindow
30{
31    Q_OBJECT
32
33public:
34    QMain(std::string);
35        std::string fileLocation;
36
37private:
38        void registerCommands();
39        HtmlArea *centralWidget;
40        QAction *randomArticleAct;
41        QAction *searchArticleAct;
42        QAction *displayHelpAct;
43
44private Q_SLOTS:
45        void displayHelp();
46        void searchDialog();
47};
48
49#endif
qvido.pro
1######################################################################
2# Automatically generated by qmake (2.01a) Sat Mar 27 14:19:19 2010
3######################################################################
4
5TEMPLATE = app
6TARGET =
7DEPENDPATH += .
8INCLUDEPATH += . /usr/include
9LIBS = -L/usr/local/lib -lzim -lcxxtools
10; CONFIG = warn_on
11
12# Input
13SOURCES += main.cpp htmlarea.cpp qmain.cpp
14HEADERS += htmlarea.h qmain.h

Archive Download the corresponding diff file

Branches:
master



interactive