fb2read.cpp 12 KB

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