fb2read.cpp 12 KB

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