fb2read.cpp 12 KB

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