1
0

fb2read.cpp 12 KB

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