fb2read.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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("body");
  89. }
  90. Fb2XmlHandler::NodeHandler * Fb2ReadHandler::RootHandler::NewTag(const QString &name, const QXmlAttributes &atts)
  91. {
  92. switch (toKeyword(name)) {
  93. case Body : return new TextHandler(m_owner, name, atts, "div", name);
  94. case Descr : return new DescrHandler(m_owner, name, atts);
  95. case Binary : return new BinaryHandler(m_owner, name, atts);
  96. default: return NULL;
  97. }
  98. }
  99. void Fb2ReadHandler::RootHandler::EndTag(const QString &name)
  100. {
  101. Q_UNUSED(name);
  102. writer().writeEndElement();
  103. writer().writeEndElement();
  104. writer().writeEndDocument();
  105. }
  106. //---------------------------------------------------------------------------
  107. // Fb2ReadHandler::HeadHandler
  108. //---------------------------------------------------------------------------
  109. FB2_BEGIN_KEYHASH(Fb2ReadHandler::HeadHandler)
  110. FB2_KEY( Image , "image" );
  111. FB2_END_KEYHASH
  112. Fb2ReadHandler::HeadHandler::HeadHandler(Fb2ReadHandler &owner, const QString &name, const QXmlAttributes &atts)
  113. : BaseHandler(owner, name)
  114. , m_empty(true)
  115. {
  116. writer().writeStartElement("div");
  117. writer().writeAttribute("class", name);
  118. int count = atts.count();
  119. for (int i = 0; i < count; i++) {
  120. writer().writeAttribute("fb2:" + atts.qName(i), atts.value(i));
  121. }
  122. }
  123. Fb2XmlHandler::NodeHandler * Fb2ReadHandler::HeadHandler::NewTag(const QString &name, const QXmlAttributes &atts)
  124. {
  125. Q_UNUSED(atts);
  126. m_empty = false;
  127. switch (toKeyword(name)) {
  128. case Image: return new ImageHandler(m_owner, name, atts);
  129. default: return new HeadHandler(m_owner, name, atts);
  130. }
  131. }
  132. void Fb2ReadHandler::HeadHandler::TxtTag(const QString &text)
  133. {
  134. m_empty = false;
  135. writer().writeCharacters(text);
  136. }
  137. void Fb2ReadHandler::HeadHandler::EndTag(const QString &name)
  138. {
  139. Q_UNUSED(name);
  140. if (m_empty) writer().writeCharacters(" ");
  141. writer().writeEndElement();
  142. }
  143. //---------------------------------------------------------------------------
  144. // Fb2ReadHandler::DescrHandler
  145. //---------------------------------------------------------------------------
  146. FB2_BEGIN_KEYHASH(Fb2ReadHandler::DescrHandler)
  147. FB2_KEY( Title , "title-info" );
  148. FB2_KEY( Document , "document-info" );
  149. FB2_KEY( Publish , "publish-info" );
  150. FB2_KEY( Custom , "custom-info" );
  151. FB2_END_KEYHASH
  152. Fb2XmlHandler::NodeHandler * Fb2ReadHandler::DescrHandler::NewTag(const QString &name, const QXmlAttributes &atts)
  153. {
  154. Q_UNUSED(atts);
  155. switch (toKeyword(name)) {
  156. case Title :
  157. return new TitleHandler(m_owner, name, atts);
  158. case Document :
  159. case Publish :
  160. case Custom :
  161. return new HeadHandler(m_owner, name, atts);
  162. default:
  163. return NULL;
  164. }
  165. }
  166. //---------------------------------------------------------------------------
  167. // Fb2ReadHandler::TitleHandler
  168. //---------------------------------------------------------------------------
  169. Fb2XmlHandler::NodeHandler * Fb2ReadHandler::TitleHandler::NewTag(const QString &name, const QXmlAttributes &atts)
  170. {
  171. if (name == "annotation") return new TextHandler(m_owner, name, atts, "div", name);
  172. return new HeadHandler(m_owner, name, atts);
  173. }
  174. //---------------------------------------------------------------------------
  175. // Fb2ReadHandler::TextHandler
  176. //---------------------------------------------------------------------------
  177. FB2_BEGIN_KEYHASH(Fb2ReadHandler::TextHandler)
  178. FB2_KEY( Section , "annotation" );
  179. FB2_KEY( Section , "author" );
  180. FB2_KEY( Section , "cite" );
  181. FB2_KEY( Section , "date" );
  182. FB2_KEY( Section , "epigraph" );
  183. FB2_KEY( Section , "poem" );
  184. FB2_KEY( Section , "section" );
  185. FB2_KEY( Section , "stanza" );
  186. FB2_KEY( Section , "subtitle" );
  187. FB2_KEY( Section , "title" );
  188. FB2_KEY( Anchor , "a" );
  189. FB2_KEY( Table , "table" );
  190. FB2_KEY( Image , "image" );
  191. FB2_KEY( Parag , "empty-line" );
  192. FB2_KEY( Parag , "p" );
  193. FB2_KEY( Parag , "v" );
  194. FB2_KEY( Style , "style" );
  195. FB2_KEY( Strong , "strong" );
  196. FB2_KEY( Emphas , "emphasis" );
  197. FB2_KEY( Strike , "strikethrough" );
  198. FB2_KEY( Sub , "sub" );
  199. FB2_KEY( Sup , "sup" );
  200. FB2_KEY( Code , "code" );
  201. FB2_END_KEYHASH
  202. Fb2ReadHandler::TextHandler::TextHandler(Fb2ReadHandler &owner, const QString &name, const QXmlAttributes &atts, const QString &tag, const QString &style)
  203. : BaseHandler(owner, name)
  204. , m_parent(NULL)
  205. , m_tag(tag)
  206. , m_style(style)
  207. {
  208. Init(atts);
  209. }
  210. Fb2ReadHandler::TextHandler::TextHandler(TextHandler *parent, const QString &name, const QXmlAttributes &atts, const QString &tag, const QString &style)
  211. : BaseHandler(parent->m_owner, name)
  212. , m_parent(parent)
  213. , m_tag(tag)
  214. , m_style(style)
  215. {
  216. Init(atts);
  217. }
  218. void Fb2ReadHandler::TextHandler::Init(const QXmlAttributes &atts)
  219. {
  220. if (m_tag.isEmpty()) return;
  221. writer().writeStartElement(m_tag);
  222. QString id = Value(atts, "id");
  223. if (!m_style.isEmpty()) {
  224. writer().writeAttribute("class", m_style);
  225. if (m_style == "body" && Value(atts, "name").toLower() == "notes") {
  226. writer().writeAttribute("name", "notes");
  227. }
  228. }
  229. if (!id.isEmpty()) writer().writeAttribute("id", id);
  230. }
  231. Fb2XmlHandler::NodeHandler * Fb2ReadHandler::TextHandler::NewTag(const QString &name, const QXmlAttributes &atts)
  232. {
  233. QString tag, style;
  234. switch (toKeyword(name)) {
  235. case Anchor : return new AnchorHandler(this, name, atts);
  236. case Image : return new ImageHandler(m_owner, name, atts);
  237. case Section : tag = "div"; style = name; break;
  238. case Parag : tag = "p"; break;
  239. case Strong : tag = "b"; break;
  240. case Emphas : tag = "i"; break;
  241. case Strike : tag = "s"; break;
  242. case Code : tag = "tt"; break;
  243. case Sub : tag = "sub"; break;
  244. case Sup : tag = "sup"; break;
  245. default: ;
  246. }
  247. return new TextHandler(this, name, atts, tag, style);
  248. }
  249. void Fb2ReadHandler::TextHandler::TxtTag(const QString &text)
  250. {
  251. writer().writeCharacters(text);
  252. }
  253. void Fb2ReadHandler::TextHandler::EndTag(const QString &name)
  254. {
  255. Q_UNUSED(name);
  256. if (m_tag.isEmpty()) return;
  257. if (m_tag == "div") writer().writeCharacters(" ");
  258. writer().writeEndElement();
  259. }
  260. bool Fb2ReadHandler::TextHandler::isNotes() const
  261. {
  262. if (m_style == "notes") return true;
  263. return m_parent ? m_parent->isNotes() : false;
  264. }
  265. //---------------------------------------------------------------------------
  266. // Fb2ReadHandler::AnchorHandler
  267. //---------------------------------------------------------------------------
  268. Fb2ReadHandler::AnchorHandler::AnchorHandler(TextHandler *parent, const QString &name, const QXmlAttributes &atts)
  269. : TextHandler(parent, name, atts, "a")
  270. {
  271. QString href = Value(atts, "href");
  272. writer().writeAttribute("href", href);
  273. }
  274. //---------------------------------------------------------------------------
  275. // Fb2ReadHandler::ImageHandler
  276. //---------------------------------------------------------------------------
  277. Fb2ReadHandler::ImageHandler::ImageHandler(Fb2ReadHandler &owner, const QString &name, const QXmlAttributes &atts)
  278. : TextHandler(owner, name, atts, "img")
  279. {
  280. QString href = Value(atts, "href");
  281. while (href.left(1) == "#") href.remove(0, 1);
  282. QString path = m_owner.getFile(href);
  283. writer().writeAttribute("src", path);
  284. writer().writeAttribute("alt", href);
  285. }
  286. //---------------------------------------------------------------------------
  287. // Fb2ReadHandler::BinaryHandler
  288. //---------------------------------------------------------------------------
  289. Fb2ReadHandler::BinaryHandler::BinaryHandler(Fb2ReadHandler &owner, const QString &name, const QXmlAttributes &atts)
  290. : BaseHandler(owner, name)
  291. , m_file(Value(atts, "id"))
  292. {
  293. }
  294. void Fb2ReadHandler::BinaryHandler::TxtTag(const QString &text)
  295. {
  296. m_text += text;
  297. }
  298. void Fb2ReadHandler::BinaryHandler::EndTag(const QString &name)
  299. {
  300. Q_UNUSED(name);
  301. QByteArray in; in.append(m_text);
  302. if (!m_file.isEmpty()) m_owner.addFile(m_file, QByteArray::fromBase64(in));
  303. }
  304. //---------------------------------------------------------------------------
  305. // Fb2ReadHandler
  306. //---------------------------------------------------------------------------
  307. Fb2ReadHandler::Fb2ReadHandler(Fb2ReadThread &thread, QXmlStreamWriter &writer)
  308. : Fb2XmlHandler()
  309. , m_thread(thread)
  310. , m_writer(writer)
  311. {
  312. m_writer.setAutoFormatting(true);
  313. m_writer.setAutoFormattingIndent(2);
  314. }
  315. Fb2XmlHandler::NodeHandler * Fb2ReadHandler::CreateRoot(const QString &name, const QXmlAttributes &atts)
  316. {
  317. Q_UNUSED(atts);
  318. if (name == "fictionbook") return new RootHandler(*this, name);
  319. m_error = QObject::tr("The file is not an FB2 file.");
  320. return 0;
  321. }
  322. QString Fb2ReadHandler::getFile(const QString &name)
  323. {
  324. QString path;
  325. QMetaObject::invokeMethod(m_thread.parent(), "temp", Qt::DirectConnection, Q_RETURN_ARG(QString, path), Q_ARG(QString, name));
  326. return path;
  327. }
  328. void Fb2ReadHandler::addFile(const QString &name, const QByteArray &data)
  329. {
  330. QMetaObject::invokeMethod(m_thread.parent(), "data", Qt::QueuedConnection, Q_ARG(QString, name), Q_ARG(QByteArray, data));
  331. }