1
0

fb2read.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. #include <QtGui>
  2. #include <QtDebug>
  3. #include "fb2read.hpp"
  4. #include "fb2xml2.h"
  5. //---------------------------------------------------------------------------
  6. // FbReadThread
  7. //---------------------------------------------------------------------------
  8. FbReadThread::FbReadThread(QObject *parent, const QString &filename, const QString &xml)
  9. : QThread(parent)
  10. , m_temp(0)
  11. , m_filename(filename)
  12. , m_xml(xml)
  13. , m_abort(false)
  14. {
  15. connect(this, SIGNAL(html(QString)), parent, SLOT(html(QString)));
  16. }
  17. FbReadThread::~FbReadThread()
  18. {
  19. stop();
  20. wait();
  21. }
  22. void FbReadThread::stop()
  23. {
  24. QMutexLocker locker(&mutex);
  25. Q_UNUSED(locker);
  26. m_abort = true;
  27. }
  28. void FbReadThread::run()
  29. {
  30. if (parse()) emit html(m_html);
  31. }
  32. #ifdef FB2_USE_LIBXML2
  33. bool FbReadThread::parse()
  34. {
  35. QXmlStreamWriter writer(&m_html);
  36. FbReadHandler handler(*this, writer);
  37. XML2::XmlReader reader;
  38. reader.setContentHandler(&handler);
  39. reader.setLexicalHandler(&handler);
  40. reader.setErrorHandler(&handler);
  41. if (m_xml.isEmpty()) {
  42. QFile file(m_filename);
  43. if (!file.open(QFile::ReadOnly | QFile::Text)) {
  44. qCritical() << QObject::tr("Cannot read file %1: %2.").arg(m_filename).arg(file.errorString());
  45. return false;
  46. }
  47. return reader.parse(file);
  48. } else {
  49. QXmlInputSource source;
  50. source.setData(m_xml);
  51. return reader.parse(source);
  52. }
  53. }
  54. #else
  55. bool FbReadThread::parse()
  56. {
  57. QXmlStreamWriter writer(&m_html);
  58. FbReadHandler handler(*this, writer);
  59. QXmlSimpleReader reader;
  60. reader.setContentHandler(&handler);
  61. reader.setLexicalHandler(&handler);
  62. reader.setErrorHandler(&handler);
  63. QXmlInputSource source;
  64. if (m_xml.isEmpty()) {
  65. QFile file(m_filename);
  66. if (!file.open(QFile::ReadOnly | QFile::Text)) {
  67. qCritical() << QObject::tr("Cannot read file %1: %2.").arg(m_filename).arg(file.errorString());
  68. return false;
  69. }
  70. source.setData(file.readAll());
  71. } else {
  72. source.setData(m_xml);
  73. }
  74. return reader.parse(source);
  75. }
  76. #endif
  77. //---------------------------------------------------------------------------
  78. // FbReadHandler::RootHandler
  79. //---------------------------------------------------------------------------
  80. FB2_BEGIN_KEYHASH(FbReadHandler::RootHandler)
  81. FB2_KEY( Style , "stylesheet" );
  82. FB2_KEY( Descr , "description" );
  83. FB2_KEY( Body , "body" );
  84. FB2_KEY( Binary , "binary" );
  85. FB2_END_KEYHASH
  86. FbReadHandler::RootHandler::RootHandler(FbReadHandler &owner, const QString &name)
  87. : BaseHandler(owner, name)
  88. , m_head(true)
  89. {
  90. }
  91. FbXmlHandler::NodeHandler * FbReadHandler::RootHandler::NewTag(const QString &name, const QXmlAttributes &atts)
  92. {
  93. switch (toKeyword(name)) {
  94. case Binary: return new BinaryHandler(m_owner, name, atts);
  95. case Style: return new StyleHandler(m_owner, name, m_style);
  96. default: ;
  97. }
  98. if (m_head) {
  99. writeHeader();
  100. m_head = false;
  101. }
  102. return new TextHandler(m_owner, name, atts, "fb:" + name);
  103. }
  104. void FbReadHandler::RootHandler::EndTag(const QString &name)
  105. {
  106. Q_UNUSED(name);
  107. if (!m_head) writer().writeEndElement();
  108. }
  109. void FbReadHandler::RootHandler::writeScript(const QString &src)
  110. {
  111. writer().writeStartElement("script");
  112. writer().writeAttribute("type", "text/javascript");
  113. writer().writeAttribute("src", src);
  114. writer().writeCharacters(" ");
  115. writer().writeEndElement();
  116. }
  117. void FbReadHandler::RootHandler::writeHeader()
  118. {
  119. writer().writeStartElement("head");
  120. writeScript("qrc:/js/jquery.js");
  121. writeScript("qrc:/js/location.js");
  122. if (!m_style.isEmpty()) {
  123. writer().writeStartElement("style");
  124. writer().writeAttribute("type", "text/css");
  125. writer().writeCharacters(m_style);
  126. writer().writeEndElement();
  127. }
  128. writer().writeEndElement();
  129. writer().writeStartElement("body");
  130. }
  131. //---------------------------------------------------------------------------
  132. // FbReadHandler::StyleHandler
  133. //---------------------------------------------------------------------------
  134. FbReadHandler::StyleHandler::StyleHandler(FbReadHandler &owner, const QString &name, QString &text)
  135. : BaseHandler(owner, name)
  136. , m_text(text)
  137. {
  138. }
  139. void FbReadHandler::StyleHandler::TxtTag(const QString &text)
  140. {
  141. m_text += text;
  142. }
  143. //---------------------------------------------------------------------------
  144. // FbReadHandler::TextHandler
  145. //---------------------------------------------------------------------------
  146. FB2_BEGIN_KEYHASH(FbReadHandler::TextHandler)
  147. FB2_KEY( Anchor , "a" );
  148. FB2_KEY( Image , "image" );
  149. FB2_KEY( Origin , "table" );
  150. FB2_KEY( Origin , "td" );
  151. FB2_KEY( Origin , "th" );
  152. FB2_KEY( Origin , "tr" );
  153. FB2_KEY( Parag , "empty-line" );
  154. FB2_KEY( Parag , "text-author" );
  155. FB2_KEY( Parag , "subtitle" );
  156. FB2_KEY( Parag , "p" );
  157. FB2_KEY( Parag , "v" );
  158. FB2_KEY( Style , "style" );
  159. FB2_KEY( Strong , "strong" );
  160. FB2_KEY( Emphas , "emphasis" );
  161. FB2_KEY( Strike , "strikethrough" );
  162. FB2_KEY( Sub , "sub" );
  163. FB2_KEY( Sup , "sup" );
  164. FB2_KEY( Code , "code" );
  165. FB2_END_KEYHASH
  166. FbReadHandler::TextHandler::TextHandler(FbReadHandler &owner, const QString &name, const QXmlAttributes &atts, const QString &tag)
  167. : BaseHandler(owner, name)
  168. , m_parent(NULL)
  169. , m_tag(tag)
  170. , m_empty(true)
  171. {
  172. Init(name, atts);
  173. }
  174. FbReadHandler::TextHandler::TextHandler(TextHandler *parent, const QString &name, const QXmlAttributes &atts, const QString &tag)
  175. : BaseHandler(parent->m_owner, name)
  176. , m_parent(parent)
  177. , m_tag(tag)
  178. , m_empty(true)
  179. {
  180. Init(name, atts);
  181. }
  182. void FbReadHandler::TextHandler::Init(const QString &name, const QXmlAttributes &atts)
  183. {
  184. Keyword key = toKeyword(name);
  185. writer().writeStartElement(m_tag);
  186. int count = atts.count();
  187. for (int i = 0; i < count; i++) {
  188. QString name = atts.qName(i);
  189. switch (key) {
  190. case Anchor: { if (atts.localName(i) == "href") name = "href"; break; }
  191. case Image: { if (atts.localName(i) == "href") name = "src"; break; }
  192. default: ;
  193. }
  194. writer().writeAttribute(name, atts.value(i));
  195. }
  196. if (m_tag == "p" && (name == "text-author" || name == "subtitle")) {
  197. writer().writeAttribute("fb:class", name);
  198. }
  199. }
  200. FbXmlHandler::NodeHandler * FbReadHandler::TextHandler::NewTag(const QString &name, const QXmlAttributes &atts)
  201. {
  202. m_empty = false;
  203. QString tag;
  204. switch (toKeyword(name)) {
  205. case Origin : tag = name; break;
  206. case Anchor : tag = "a"; break;
  207. case Image : tag = "img"; break;
  208. case Parag : tag = "p"; break;
  209. case Strong : tag = "b"; break;
  210. case Emphas : tag = "i"; break;
  211. case Strike : tag = "s"; break;
  212. case Code : tag = "tt"; break;
  213. case Sub : tag = "sub"; break;
  214. case Sup : tag = "sup"; break;
  215. case Style : tag = "span"; break;
  216. default : tag = "fb:" + name;
  217. }
  218. return new TextHandler(this, name, atts, tag);
  219. }
  220. void FbReadHandler::TextHandler::TxtTag(const QString &text)
  221. {
  222. writer().writeCharacters(text);
  223. m_empty = false;
  224. }
  225. void FbReadHandler::TextHandler::EndTag(const QString &name)
  226. {
  227. if (m_empty) {
  228. if (name == "p") {
  229. writer().writeEmptyElement("br");
  230. } else {
  231. writer().writeCharacters(" ");
  232. }
  233. }
  234. writer().writeEndElement();
  235. }
  236. bool FbReadHandler::TextHandler::isNotes() const
  237. {
  238. if (m_style == "notes") return true;
  239. return m_parent ? m_parent->isNotes() : false;
  240. }
  241. //---------------------------------------------------------------------------
  242. // FbReadHandler::BinaryHandler
  243. //---------------------------------------------------------------------------
  244. FbReadHandler::BinaryHandler::BinaryHandler(FbReadHandler &owner, const QString &name, const QXmlAttributes &atts)
  245. : BaseHandler(owner, name)
  246. , m_file(Value(atts, "id"))
  247. {
  248. }
  249. void FbReadHandler::BinaryHandler::TxtTag(const QString &text)
  250. {
  251. m_text += text;
  252. }
  253. void FbReadHandler::BinaryHandler::EndTag(const QString &name)
  254. {
  255. Q_UNUSED(name);
  256. QByteArray in; in.append(m_text);
  257. if (!m_file.isEmpty()) m_owner.addFile(m_file, QByteArray::fromBase64(in));
  258. }
  259. //---------------------------------------------------------------------------
  260. // FbReadHandler
  261. //---------------------------------------------------------------------------
  262. FbReadHandler::FbReadHandler(FbReadThread &thread, QXmlStreamWriter &writer)
  263. : FbXmlHandler()
  264. , m_thread(thread)
  265. , m_writer(writer)
  266. , m_temp(thread.temp())
  267. {
  268. m_writer.setAutoFormatting(true);
  269. m_writer.setAutoFormattingIndent(2);
  270. m_writer.writeStartElement("html");
  271. }
  272. FbReadHandler::~FbReadHandler()
  273. {
  274. m_writer.writeEndElement();
  275. }
  276. FbXmlHandler::NodeHandler * FbReadHandler::CreateRoot(const QString &name, const QXmlAttributes &atts)
  277. {
  278. Q_UNUSED(atts);
  279. if (name == "fictionbook") return new RootHandler(*this, name);
  280. m_error = QObject::tr("The file is not an FB2 file.");
  281. return 0;
  282. }
  283. bool FbReadHandler::comment(const QString& ch)
  284. {
  285. m_writer.writeComment(ch);
  286. return true;
  287. }
  288. void FbReadHandler::addFile(const QString &name, const QByteArray &data)
  289. {
  290. QMetaObject::invokeMethod(m_temp, "data", Qt::QueuedConnection, Q_ARG(QString, name), Q_ARG(QByteArray, data));
  291. }