1
0

fb2read.cpp 12 KB

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