Kaynağa Gözat

Parse by XML2

Kandrashin Denis 12 yıl önce
ebeveyn
işleme
d86d1516d2

+ 3 - 2
source/fb2code.cpp

@@ -434,9 +434,10 @@ void FbCodeEdit::clipboardDataChanged()
     }
 }
 
-bool FbCodeEdit::read(QIODevice &device)
+bool FbCodeEdit::read(QIODevice *device)
 {
-    QByteArray data = device.readAll();
+    QByteArray data = device->readAll();
+    delete device;
     QXmlInputSource source;
     source.setData(data);
     setPlainText(source.data());

+ 1 - 1
source/fb2code.hpp

@@ -90,7 +90,7 @@ public:
 
     QString text() const { return toPlainText(); }
 
-    bool read(QIODevice &device);
+    bool read(QIODevice *device);
 
     void load(const QByteArray data)
         { setPlainText(QString::fromUtf8(data.data())); }

+ 4 - 3
source/fb2dock.cpp

@@ -122,9 +122,10 @@ void FbMainDock::setModeHtml()
 
 bool FbMainDock::load(const QString &filename)
 {
-    QFile file(filename);
-    if (!file.open(QFile::ReadOnly | QFile::Text)) {
-        qCritical() << QObject::tr("Cannot read file %1: %2.").arg(filename).arg(file.errorString());
+    QFile *file = new QFile(filename);
+    if (!file->open(QFile::ReadOnly | QFile::Text)) {
+        qCritical() << QObject::tr("Cannot read file %1: %2.").arg(filename).arg(file->errorString());
+        delete file;
         return false;
     }
 

+ 2 - 55
source/fb2main.cpp

@@ -8,8 +8,6 @@
 #include "fb2code.hpp"
 #include "fb2dlgs.hpp"
 #include "fb2dock.hpp"
-#include "fb2page.hpp"
-#include "fb2read.hpp"
 #include "fb2save.hpp"
 #include "fb2text.hpp"
 #include "fb2utils.h"
@@ -45,13 +43,6 @@ FbMainWindow::FbMainWindow(const QString &filename, ViewMode mode)
     mainDock->load(filename);
 }
 
-/*
-FbTextPage * FbMainWindow::page()
-{
-    return textFrame ? textFrame->view()->page() : 0;
-}
-*/
-
 void FbMainWindow::logMessage(const QString &message)
 {
     if (!messageEdit) {
@@ -138,8 +129,8 @@ bool FbMainWindow::fileSaveAs()
 
 void FbMainWindow::about()
 {
-    QString text = tr("The <b>fb2edit</b> is application for editing FB2-files.");
-    text += QString("<br>") += FbApplication::lastCommit();
+    QString text = tr("<b>fb2edit</b> is an application for creating and editing FB2-files.");
+    text += QString("<br>") += QString("<br>") += FbApplication::lastCommit();
     QMessageBox::about(this, tr("About fb2edit"), text);
 }
 
@@ -582,50 +573,6 @@ FbMainWindow *FbMainWindow::findFbMainWindow(const QString &fileName)
     return 0;
 }
 /*
-void FbMainWindow::viewText(FbTextPage *page)
-{
-    if (textFrame && centralWidget() == textFrame) return;
-
-    if (textFrame) textFrame->hideInspector();
-
-    if (codeEdit) {
-        QString xml = codeEdit->text();
-        page = new FbTextPage(this);
-        if (!page->load(QString(), xml)) {
-            delete page;
-            return;
-        }
-        isSwitched = true;
-    }
-
-    FB2DELETE(codeEdit);
-    FB2DELETE(headTree);
-    if (textFrame) {
-        createTextToolbar();
-    } else {
-        textFrame = new FbTextFrame(this, actionInspect);
-    }
-    setCentralWidget(textFrame);
-
-    FbTextEdit *textEdit = textFrame->view();
-
-    if (page) {
-        page->setParent(textEdit);
-        textEdit->setPage(page);
-    }
-
-    connect(textEdit, SIGNAL(loadFinished(bool)), SLOT(createTextToolbar()));
-    connect(textEdit->pageAction(QWebPage::Undo), SIGNAL(changed()), SLOT(undoChanged()));
-    connect(textEdit->pageAction(QWebPage::Redo), SIGNAL(changed()), SLOT(redoChanged()));
-
-    actionContents->setEnabled(true);
-    actionPictures->setEnabled(true);
-    actionInspect->setEnabled(true);
-
-    textEdit->setFocus();
-    viewTree();
-}
-
 void FbMainWindow::viewHead()
 {
     if (headTree && centralWidget() == headTree) return;

+ 0 - 6
source/fb2main.hpp

@@ -15,12 +15,6 @@ class QWebInspector;
 QT_END_NAMESPACE
 
 class FbMainDock;
-class FbCodeEdit;
-class FbTreeView;
-class FbHeadEdit;
-class FbTextEdit;
-class FbTextFrame;
-class FbTextPage;
 
 class FbLogDock: public QDockWidget
 {

+ 7 - 7
source/fb2page.cpp

@@ -47,9 +47,11 @@ FbTextPage::FbTextPage(QObject *parent)
     connect(this, SIGNAL(loadFinished(bool)), SLOT(loadFinished()));
     connect(this, SIGNAL(contentsChanged()), SLOT(fixContents()));
 
-    QFile file(":blank.fb2");
-    if (file.open(QFile::ReadOnly | QFile::Text)) {
+    QFile *file = new QFile(":blank.fb2");
+    if (file->open(QFile::ReadOnly | QFile::Text)) {
         read(file);
+    } else {
+        delete file;
     }
 }
 
@@ -62,15 +64,13 @@ bool FbTextPage::read(const QString &html)
 {
     QXmlInputSource *source = new QXmlInputSource();
     source->setData(html);
-    FbReadThread::execute(this, source);
+    FbReadThread::execute(this, source, 0);
     return true;
 }
 
-bool FbTextPage::read(QIODevice &device)
+bool FbTextPage::read(QIODevice *device)
 {
-    QXmlInputSource *source = new QXmlInputSource();
-    source->setData(device.readAll());
-    FbReadThread::execute(this, source);
+    FbReadThread::execute(this, 0, device);
     return true;
 }
 

+ 1 - 1
source/fb2page.hpp

@@ -30,7 +30,7 @@ public:
     explicit FbTextPage(QObject *parent = 0);
     FbNetworkAccessManager *temp();
     bool read(const QString &html);
-    bool read(QIODevice &device);
+    bool read(QIODevice *device);
     void push(QUndoCommand * command, const QString &text = QString());
     FbTextElement element(const QString &location);
     FbTextElement current();

+ 18 - 4
source/fb2read.cpp

@@ -9,15 +9,16 @@
 //  FbReadThread
 //---------------------------------------------------------------------------
 
-void FbReadThread::execute(QObject *parent, QXmlInputSource *source)
+void FbReadThread::execute(QObject *parent, QXmlInputSource *source, QIODevice *device)
 {
-    FbReadThread *thread = new FbReadThread(parent, source);
+    FbReadThread *thread = new FbReadThread(parent, source, device);
     connect(thread, SIGNAL(html(QObject*,QString)), parent, SLOT(html(QObject*,QString)));
     thread->start();
 }
 
-FbReadThread::FbReadThread(QObject *parent, QXmlInputSource *source)
+FbReadThread::FbReadThread(QObject *parent, QXmlInputSource *source, QIODevice *device)
     : QThread(parent)
+    , m_device(device)
     , m_source(source)
 {
     m_temp = new FbNetworkAccessManager(this);
@@ -25,7 +26,8 @@ FbReadThread::FbReadThread(QObject *parent, QXmlInputSource *source)
 
 FbReadThread::~FbReadThread()
 {
-    delete m_source;
+    if (m_source) delete m_source;
+    if (m_device) delete m_device;
 }
 
 void FbReadThread::run()
@@ -55,7 +57,19 @@ bool FbReadThread::parse()
     reader.setLexicalHandler(&handler);
     reader.setErrorHandler(&handler);
 
+#ifdef FB2_USE_LIBXML2
+    if (m_device) {
+        return reader.parse(m_device);
+    } else {
+        return reader.parse(m_source);
+    }
+#else
+    if (m_device) {
+        m_source = new QXmlInputSource();
+        m_source->setData(m_device->readAll());
+    }
     return reader.parse(m_source);
+#endif
 }
 
 /*

+ 3 - 2
source/fb2read.hpp

@@ -15,7 +15,7 @@ class FbReadThread : public QThread
     Q_OBJECT
 
 public:
-    static void execute(QObject *parent, QXmlInputSource *source);
+    static void execute(QObject *parent, QXmlInputSource *source, QIODevice *device);
     ~FbReadThread();
 
 signals:
@@ -26,10 +26,11 @@ protected:
     void run();
 
 private:
-    explicit FbReadThread(QObject *parent, QXmlInputSource *source);
+    explicit FbReadThread(QObject *parent, QXmlInputSource *source, QIODevice *device);
     bool parse();
 
 private:
+    QIODevice *m_device;
     QXmlInputSource *m_source;
     FbNetworkAccessManager *m_temp;
     QString m_html;

+ 6 - 6
source/fb2xml2.cpp

@@ -396,8 +396,8 @@ private:
 
     static QString C2S(const xmlChar* text, int size = -1);
 
-    bool parse(const QXmlInputSource* input);
-    bool parse(QIODevice& input);
+    bool parse(const QXmlInputSource *input);
+    bool parse(QIODevice *input);
     void process(xmlTextReaderPtr reader);
 
     QScopedPointer<XmlReaderLocator> locator;
@@ -483,7 +483,7 @@ int XmlReaderPrivate::onRead(void * context, char * buffer, int len)
     return device->read(buffer, len);
 }
 
-bool XmlReaderPrivate::parse(const QXmlInputSource* input)
+bool XmlReaderPrivate::parse(const QXmlInputSource *input)
 {
     QByteArray arr = input->data().toUtf8();
     int options = XML_PARSE_RECOVER | XML_PARSE_NOERROR | XML_PARSE_NOWARNING | XML_PARSE_NONET;
@@ -495,10 +495,10 @@ bool XmlReaderPrivate::parse(const QXmlInputSource* input)
     return true;
 }
 
-bool XmlReaderPrivate::parse(QIODevice& input)
+bool XmlReaderPrivate::parse(QIODevice *input)
 {
     int options = XML_PARSE_RECOVER | XML_PARSE_NOERROR | XML_PARSE_NOWARNING | XML_PARSE_NONET;
-    m_reader = xmlReaderForIO(&XmlReaderPrivate::onRead, NULL, &input, NULL, NULL, options);
+    m_reader = xmlReaderForIO(&XmlReaderPrivate::onRead, NULL, input, NULL, NULL, options);
     if (!m_reader) return false;
     xmlTextReaderSetErrorHandler(m_reader, &XmlReaderPrivate::onError, this);
     while (xmlTextReaderRead(m_reader) == 1) process(m_reader);
@@ -635,7 +635,7 @@ bool XmlReader::parse(const QXmlInputSource* input)
     return true;
 }
 
-bool XmlReader::parse(QIODevice& input)
+bool XmlReader::parse(QIODevice *input)
 {
     Q_D(XmlReader);
 

+ 1 - 1
source/fb2xml2.h

@@ -91,7 +91,7 @@ public:
     virtual void setDeclHandler(QXmlDeclHandler* handler);
     virtual QXmlDeclHandler* declHandler(void) const;
 
-    virtual bool parse(QIODevice& input);
+    virtual bool parse(QIODevice *input);
     virtual bool parse(const QXmlInputSource&);
     virtual bool parse(const QXmlInputSource*);