Browse Source

Modify class Fb2SaveWriter

Kandrashin Denis 13 years ago
parent
commit
b8b72964cc
4 changed files with 30 additions and 123 deletions
  1. 8 84
      source/fb2save.cpp
  2. 4 39
      source/fb2save.h
  3. 17 0
      source/fb2view.cpp
  4. 1 0
      source/fb2view.h

+ 8 - 84
source/fb2save.cpp

@@ -2,98 +2,19 @@
 #include <QtDebug>
 
 #include "fb2save.h"
-
-//---------------------------------------------------------------------------
-//  Fb2SaveThread
-//---------------------------------------------------------------------------
-
-Fb2SaveThread::Fb2SaveThread(QObject *parent, const QString &filename)
-    : QThread(parent)
-    , m_filename(filename)
-    , m_abort(false)
-{
-}
-
-Fb2SaveThread::~Fb2SaveThread()
-{
-    stop();
-    wait();
-}
-
-void Fb2SaveThread::stop()
-{
-    QMutexLocker locker(&mutex);
-    Q_UNUSED(locker);
-    m_abort = true;
-}
-
-void Fb2SaveThread::run()
-{
-    if (parse()) emit html(m_filename, m_html);
-}
-
-void Fb2SaveThread::onFile(const QString &name, const QString &path)
-{
-    emit file(name, path);
-}
-
-bool Fb2SaveThread::parse()
-{
-    QFile file(m_filename);
-    if (!file.open(QFile::ReadOnly | QFile::Text)) {
-        qCritical() << QObject::tr("Cannot read file %1: %2.").arg(m_filename).arg(file.errorString());
-        return false;
-    }
-    Fb2SaveHandler handler(*this);
-    QXmlSimpleReader reader;
-    reader.setContentHandler(&handler);
-    reader.setErrorHandler(&handler);
-    QXmlInputSource source(&file);
-    return reader.parse(source);
-}
+#include "fb2view.h"
 
 //---------------------------------------------------------------------------
 //  Fb2SaveWriter
 //---------------------------------------------------------------------------
 
-Fb2SaveWriter::Fb2SaveWriter(Fb2SaveThread &thread)
-    : QXmlStreamWriter(thread.data())
-    , m_thread(thread)
-    , m_id(0)
+Fb2SaveWriter::Fb2SaveWriter(QIODevice &device)
+    : QXmlStreamWriter(&device)
 {
     setAutoFormatting(true);
     setAutoFormattingIndent(2);
 }
 
-QString Fb2SaveWriter::addFile(const QString &name, const QByteArray &data)
-{
-    QString path = getFile(name);
-    QFile file(path);
-    if (file.open(QIODevice::WriteOnly)) {
-        file.write(data);
-        m_thread.onFile(name, path);
-    }
-    return path;
-}
-
-QString Fb2SaveWriter::getFile(const QString &name)
-{
-    StringHash::const_iterator i = m_hash.find(name);
-    if (i == m_hash.end()) {
-        QTemporaryFile file;
-        file.setAutoRemove(false);
-        file.open();
-        return m_hash.insert(name, file.fileName()).value();
-    } else {
-        return i.value();
-    }
-}
-
-QString Fb2SaveWriter::newId()
-{
-    return QString("FB2E%1").arg(++m_id);
-}
-
 //---------------------------------------------------------------------------
 //  Fb2SaveHandler::BodyHandler
 //---------------------------------------------------------------------------
@@ -214,20 +135,23 @@ Fb2SaveHandler::AnchorHandler::AnchorHandler(BodyHandler *parent, const QString
 Fb2SaveHandler::ImageHandler::ImageHandler(BodyHandler *parent, const QString &name, const QXmlAttributes &atts)
     : BodyHandler(parent, name, atts, "img")
 {
+/*
     QString href = Value(atts, "href");
     while (href.left(1) == "#") href.remove(0, 1);
     QString path = m_writer.getFile(href);
     m_writer.writeAttribute("src", path);
     m_writer.writeAttribute("alt", href);
+*/
 }
 
 //---------------------------------------------------------------------------
 //  Fb2SaveHandler
 //---------------------------------------------------------------------------
 
-Fb2SaveHandler::Fb2SaveHandler(Fb2SaveThread &thread)
+Fb2SaveHandler::Fb2SaveHandler(Fb2WebView &view, QIODevice &device)
     : Fb2XmlHandler()
-    , m_writer(thread)
+    , m_writer(device)
+    , m_view(view)
 {
 }
 

+ 4 - 39
source/fb2save.h

@@ -9,54 +9,18 @@
 #include <QXmlDefaultHandler>
 #include <QXmlStreamWriter>
 
-class Fb2SaveThread : public QThread
-{
-    Q_OBJECT
-
-public:
-    Fb2SaveThread(QObject *parent, const QString &filename);
-    ~Fb2SaveThread();
-    void onFile(const QString &name, const QString &path);
-    QString * data() { return &m_html; }
-
-signals:
-    void file(QString name, QString path);
-    void html(QString name, QString html);
-
-public slots:
-    void stop();
-
-protected:
-    void run();
-
-private:
-    bool parse();
-
-private:
-    const QString m_filename;
-    QString m_html;
-    bool m_abort;
-    QMutex mutex;
-};
+class Fb2WebView;
 
 class Fb2SaveWriter : public QXmlStreamWriter
 {
 public:
-    explicit Fb2SaveWriter(Fb2SaveThread &thread);
-    QString addFile(const QString &name, const QByteArray &data);
-    QString getFile(const QString &name);
-    QString newId();
-private:
-    typedef QHash<QString, QString> StringHash;
-    Fb2SaveThread &m_thread;
-    StringHash m_hash;
-    int m_id;
+    explicit Fb2SaveWriter(QIODevice &device);
 };
 
 class Fb2SaveHandler : public Fb2XmlHandler
 {
 public:
-    explicit Fb2SaveHandler(Fb2SaveThread &thread);
+    explicit Fb2SaveHandler(Fb2WebView &view, QIODevice &device);
 
 private:
     class BodyHandler : public NodeHandler
@@ -106,6 +70,7 @@ protected:
 
 private:
     Fb2SaveWriter m_writer;
+    Fb2WebView &m_view;
 };
 
 #endif // Fb2Save_H

+ 17 - 0
source/fb2view.cpp

@@ -1,5 +1,6 @@
 #include "fb2view.h"
 #include "fb2read.h"
+#include "fb2save.h"
 
 #include <QAction>
 #include <QtDebug>
@@ -93,6 +94,22 @@ void Fb2WebView::load(const QString &filename)
     m_thread->start();
 }
 
+bool Fb2WebView::save(const QString &filename)
+{
+    QFile file(filename);
+    if (!file.open(QFile::WriteOnly | QFile::Text)) {
+        qCritical() << QObject::tr("Cannot write file %1: %2.").arg(filename).arg(file.errorString());
+        return false;
+    }
+    Fb2SaveHandler handler(*this, file);
+    QXmlSimpleReader reader;
+    reader.setContentHandler(&handler);
+    reader.setErrorHandler(&handler);
+    QXmlInputSource source;
+    source.setData(toBodyXml());
+    return reader.parse(source);
+}
+
 QTemporaryFile * Fb2WebView::file(const QString &name)
 {
     TemporaryHash::const_iterator it = m_files.find(name);

+ 1 - 0
source/fb2view.h

@@ -59,6 +59,7 @@ public:
     explicit Fb2WebView(QWidget *parent = 0);
     virtual ~Fb2WebView();
     void load(const QString &filename);
+    bool save(const QString &filename);
     QString toBodyXml();
     QString toXml();