瀏覽代碼

Try to save FB2 document

Kandrashin Denis 13 年之前
父節點
當前提交
6bfafe4e00
共有 6 個文件被更改,包括 37 次插入21 次删除
  1. 8 6
      source/fb2main.cpp
  2. 3 1
      source/fb2read.cpp
  3. 8 2
      source/fb2save.cpp
  4. 1 0
      source/fb2save.h
  5. 13 9
      source/fb2view.cpp
  6. 4 3
      source/fb2view.h

+ 8 - 6
source/fb2main.cpp

@@ -538,7 +538,7 @@ bool Fb2MainWindow::maybeSave()
 {
     if (textEdit && textEdit->isModified()) {
         QMessageBox::StandardButton ret;
-        ret = QMessageBox::warning(this, tr("SDI"),
+        ret = QMessageBox::warning(this, qApp->applicationName(),
                      tr("The document has been modified. Do you want to save your changes?"),
                      QMessageBox::Save | QMessageBox::Discard
 		     | QMessageBox::Cancel);
@@ -552,22 +552,24 @@ bool Fb2MainWindow::maybeSave()
 
 bool Fb2MainWindow::saveFile(const QString &fileName)
 {
-    return false;
-
     QFile file(fileName);
     if (!file.open(QFile::WriteOnly | QFile::Text)) {
-        QMessageBox::warning(this, tr("SDI"), tr("Cannot write file %1: %2.").arg(fileName).arg(file.errorString()));
+        QMessageBox::warning(this, qApp->applicationName(), tr("Cannot write file %1: %2.").arg(fileName).arg(file.errorString()));
         return false;
     }
 
+    if (textEdit) return textEdit->save(file);
+    return true;
+
+/*
     QTextStream out(&file);
     QApplication::setOverrideCursor(Qt::WaitCursor);
-//    out << textEdit->toPlainText();
+    out << textEdit->toPlainText();
     QApplication::restoreOverrideCursor();
 
     setCurrentFile(fileName);
     statusBar()->showMessage(tr("File saved"), 2000);
-    return true;
+*/
 }
 
 void Fb2MainWindow::setCurrentFile(const QString &filename)

+ 3 - 1
source/fb2read.cpp

@@ -92,6 +92,7 @@ FB2_END_KEYHASH
 Fb2ReadHandler::RootHandler::RootHandler(Fb2ReadWriter &writer, const QString &name)
     : BaseHandler(writer, name)
 {
+    m_writer.writeStartDocument();
     m_writer.writeStartElement("html");
     m_writer.writeStartElement("body");
 }
@@ -111,6 +112,7 @@ void Fb2ReadHandler::RootHandler::EndTag(const QString &name)
     Q_UNUSED(name);
     m_writer.writeEndElement();
     m_writer.writeEndElement();
+    m_writer.writeEndDocument();
 }
 
 //---------------------------------------------------------------------------
@@ -130,7 +132,7 @@ Fb2ReadHandler::HeadHandler::HeadHandler(Fb2ReadWriter &writer, const QString &n
     if (hide) m_writer.writeAttribute("style", "display:none");
     int count = atts.count();
     for (int i = 0; i < count; i++) {
-        m_writer.writeAttribute("fb2:" + atts.qName(i), atts.value(i));
+        m_writer.writeAttribute("fb2_" + atts.qName(i), atts.value(i));
     }
 }
 

+ 8 - 2
source/fb2save.cpp

@@ -14,6 +14,12 @@ Fb2SaveWriter::Fb2SaveWriter(Fb2WebView &view, QIODevice &device)
 {
     setAutoFormatting(true);
     setAutoFormattingIndent(2);
+    writeStartDocument();
+}
+
+Fb2SaveWriter::~Fb2SaveWriter()
+{
+    writeEndDocument();
 }
 
 //---------------------------------------------------------------------------
@@ -23,7 +29,7 @@ Fb2SaveWriter::Fb2SaveWriter(Fb2WebView &view, QIODevice &device)
 FB2_BEGIN_KEYHASH(Fb2SaveHandler::BodyHandler)
     FB2_KEY( Section , "div"    );
     FB2_KEY( Anchor  , "a"      );
-    FB2_KEY( Image   , "image"  );
+    FB2_KEY( Image   , "img"  );
     FB2_KEY( Table   , "table"  );
     FB2_KEY( Parag   , "p"      );
     FB2_KEY( Strong  , "b"      );
@@ -68,7 +74,7 @@ Fb2XmlHandler::NodeHandler * Fb2SaveHandler::BodyHandler::NewTag(const QString &
 {
     QString tag, style;
     switch (toKeyword(name)) {
-        case Section   : tag = atts.value("style") ; break;
+        case Section   : tag = atts.value("class") ; break;
         case Anchor    : return new AnchorHandler(this, name, atts);
         case Image     : return new ImageHandler(this, name, atts);
         case Parag     : return new ParagHandler(this, name, atts);

+ 1 - 0
source/fb2save.h

@@ -15,6 +15,7 @@ class Fb2SaveWriter : public QXmlStreamWriter
 {
 public:
     explicit Fb2SaveWriter(Fb2WebView &view, QIODevice &device);
+    virtual ~Fb2SaveWriter();
 private:
     Fb2WebView &m_view;
 };

+ 13 - 9
source/fb2view.cpp

@@ -65,6 +65,7 @@ QWebElement Fb2WebView::doc()
 
 QString Fb2WebView::toXml()
 {
+    return toBodyXml();
     return doc().toOuterXml();
 }
 
@@ -97,22 +98,25 @@ void Fb2WebView::load(const QString &filename)
 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);
+    if (file.open(QFile::WriteOnly | QFile::Text)) return save(file);
+    qCritical() << QObject::tr("Cannot write file %1: %2.").arg(filename).arg(file.errorString());
+    return false;
+}
+
+bool Fb2WebView::save(QIODevice &device)
+{
+    Fb2SaveHandler handler(*this, device);
+    QXmlInputSource source;
+    source.setData(toBodyXml());
     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);
+    TemporaryMap::const_iterator it = m_files.find(name);
     if (it == m_files.end()) {
         it = m_files.insert(name, new QTemporaryFile());
         it.value()->open();
@@ -135,7 +139,7 @@ void Fb2WebView::data(QString name, QByteArray data)
 
 void Fb2WebView::html(QString name, QString html)
 {
-    setHtml(html, QUrl::fromLocalFile(name));
+    setHtml(html);
     if (m_thread) m_thread->deleteLater();
     m_thread = 0;
 }

+ 4 - 3
source/fb2view.h

@@ -2,7 +2,7 @@
 #define FB2VIEW_H
 
 #include <QByteArray>
-#include <QHash>
+#include <QMap>
 #include <QResizeEvent>
 #include <QTemporaryFile>
 #include <QTimer>
@@ -60,6 +60,7 @@ public:
     virtual ~Fb2WebView();
     void load(const QString &filename);
     bool save(const QString &filename);
+    bool save(QIODevice &device);
     QString toBodyXml();
     QString toXml();
 
@@ -92,8 +93,8 @@ private:
     QWebElement doc();
 
 private:
-    typedef QHash<QString, QTemporaryFile*> TemporaryHash;
-    TemporaryHash m_files;
+    typedef QMap<QString, QTemporaryFile*> TemporaryMap;
+    TemporaryMap m_files;
     QThread *m_thread;
 };