fb2read.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. #include <QtGui>
  2. #include <QTextEdit>
  3. #include <QtDebug>
  4. #include "fb2read.h"
  5. //---------------------------------------------------------------------------
  6. // Fb2ReadThread
  7. //---------------------------------------------------------------------------
  8. Fb2ReadThread::Fb2ReadThread(QObject *parent, const QString &filename)
  9. : QThread(parent)
  10. , m_document(NULL)
  11. , m_filename(filename)
  12. , m_abort(false)
  13. {
  14. }
  15. Fb2ReadThread::~Fb2ReadThread()
  16. {
  17. stop();
  18. wait();
  19. if (m_document) delete m_document;
  20. }
  21. QTextDocument * Fb2ReadThread::doc()
  22. {
  23. QMutexLocker locker(&mutex);
  24. Q_UNUSED(locker);
  25. return m_document ? m_document->clone() : NULL;
  26. }
  27. const QString & Fb2ReadThread::file()
  28. {
  29. QMutexLocker locker(&mutex);
  30. Q_UNUSED(locker);
  31. return m_filename;
  32. }
  33. void Fb2ReadThread::stop()
  34. {
  35. QMutexLocker locker(&mutex);
  36. Q_UNUSED(locker);
  37. m_abort = true;
  38. }
  39. void Fb2ReadThread::run()
  40. {
  41. QFile file(m_filename);
  42. if (!file.open(QFile::ReadOnly | QFile::Text)) {
  43. qCritical() << tr("Cannot read file %1:\n%2.").arg(m_filename).arg(file.errorString());
  44. return;
  45. }
  46. m_document = new QTextDocument();
  47. Fb2Handler handler(*m_document);
  48. QXmlSimpleReader reader;
  49. reader.setContentHandler(&handler);
  50. reader.setErrorHandler(&handler);
  51. QXmlInputSource source(&file);
  52. bool ok = reader.parse(source);
  53. m_document->moveToThread(QApplication::instance()->thread());
  54. if (ok) emit sendDocument();
  55. }
  56. //---------------------------------------------------------------------------
  57. // Fb2Handler::BaseHandler
  58. //---------------------------------------------------------------------------
  59. #define FB2_BEGIN_KEYHASH(x) \
  60. Fb2Handler::x::Keyword Fb2Handler::x::toKeyword(const QString &name) \
  61. { \
  62. static const KeywordHash map; \
  63. KeywordHash::const_iterator i = map.find(name); \
  64. return i == map.end() ? None : i.value(); \
  65. } \
  66. Fb2Handler::x::KeywordHash::KeywordHash() {
  67. #define FB2_END_KEYHASH }
  68. static QString Value(const QXmlAttributes &attributes, const QString &name)
  69. {
  70. int count = attributes.count();
  71. for (int i = 0; i < count; i++ ) {
  72. if (attributes.localName(i).compare(name, Qt::CaseInsensitive) == 0) {
  73. return attributes.value(i);
  74. }
  75. }
  76. return QString();
  77. }
  78. Fb2Handler::BaseHandler::~BaseHandler()
  79. {
  80. if (m_handler) delete m_handler;
  81. }
  82. bool Fb2Handler::BaseHandler::doStart(const QString &name, const QXmlAttributes &attributes)
  83. {
  84. if (m_handler) return m_handler->doStart(name, attributes);
  85. m_handler = NewTag(name, attributes); if (m_handler) return true;
  86. // qCritical() << QObject::tr("Unknown XML child tag: <%1> <%2>").arg(m_name).arg(name);
  87. m_handler = new BaseHandler(name);
  88. return true;
  89. }
  90. bool Fb2Handler::BaseHandler::doText(const QString &text)
  91. {
  92. if (m_handler) m_handler->doText(text); else TxtTag(text);
  93. return true;
  94. }
  95. bool Fb2Handler::BaseHandler::doEnd(const QString &name, bool & exists)
  96. {
  97. if (m_handler) {
  98. bool found = exists || name == m_name;
  99. m_handler->doEnd(name, found);
  100. if (m_handler->m_closed) { delete m_handler; m_handler = NULL; }
  101. if (found) { exists = true; return true; }
  102. }
  103. bool found = name == m_name;
  104. if (!found) qCritical() << QObject::tr("Conglict XML tags: <%1> - </%2>").arg(m_name).arg(name);
  105. m_closed = found || exists;
  106. if (m_closed) EndTag(m_name);
  107. exists = found;
  108. return true;
  109. }
  110. //---------------------------------------------------------------------------
  111. // Fb2Handler::RootHandler
  112. //---------------------------------------------------------------------------
  113. FB2_BEGIN_KEYHASH(RootHandler)
  114. insert("stylesheet", Style);
  115. insert("description", Descr);
  116. insert("body", Body);
  117. insert("binary", Binary);
  118. FB2_END_KEYHASH
  119. Fb2Handler::RootHandler::RootHandler(QTextDocument &document, const QString &name)
  120. : BaseHandler(name)
  121. , m_document(document)
  122. , m_cursor1(&document, false)
  123. , m_empty(true)
  124. {
  125. m_cursor1.beginEditBlock();
  126. }
  127. Fb2Handler::RootHandler::~RootHandler()
  128. {
  129. m_cursor1.endEditBlock();
  130. }
  131. Fb2Handler::BaseHandler * Fb2Handler::RootHandler::NewTag(const QString &name, const QXmlAttributes &attributes)
  132. {
  133. switch (toKeyword(name)) {
  134. case Body : { BaseHandler * handler = new BodyHandler(m_cursor1, name); m_empty = false; return handler; }
  135. case Descr : return new DescrHandler(name);
  136. case Binary : return new BinaryHandler(m_document, name, attributes);
  137. default: return NULL;
  138. }
  139. }
  140. //---------------------------------------------------------------------------
  141. // Fb2Handler::DescrHandler
  142. //---------------------------------------------------------------------------
  143. Fb2Handler::BaseHandler * Fb2Handler::DescrHandler::NewTag(const QString &name, const QXmlAttributes &attributes)
  144. {
  145. Q_UNUSED(name);
  146. Q_UNUSED(attributes);
  147. return new BaseHandler(name);
  148. }
  149. //---------------------------------------------------------------------------
  150. // Fb2Handler::TextHandler
  151. //---------------------------------------------------------------------------
  152. Fb2Handler::TextHandler::TextHandler(Fb2TextCursor &cursor, const QString &name)
  153. : BaseHandler(name)
  154. , m_cursor(cursor)
  155. , m_blockFormat(m_cursor.blockFormat())
  156. , m_charFormat(m_cursor.charFormat())
  157. {
  158. }
  159. Fb2Handler::TextHandler::TextHandler(TextHandler &parent, const QString &name)
  160. : BaseHandler(name)
  161. , m_cursor(parent.m_cursor)
  162. , m_blockFormat(m_cursor.blockFormat())
  163. , m_charFormat(m_cursor.charFormat())
  164. {
  165. }
  166. Fb2Handler::BaseHandler * Fb2Handler::TextHandler::NewTag(const QString & name, const QXmlAttributes &attributes)
  167. {
  168. return BaseHandler::NewTag(name, attributes);
  169. }
  170. void Fb2Handler::TextHandler::EndTag(const QString &name)
  171. {
  172. Q_UNUSED(name);
  173. cursor().setCharFormat(m_charFormat);
  174. }
  175. //---------------------------------------------------------------------------
  176. // Fb2Handler::BodyHandler
  177. //---------------------------------------------------------------------------
  178. FB2_BEGIN_KEYHASH(BodyHandler)
  179. insert("image", Image);
  180. insert("title", Title);
  181. insert("epigraph", Epigraph);
  182. insert("section", Section);
  183. insert("p", Paragraph);
  184. insert("poem", Poem);
  185. insert("stanza", Stanza);
  186. insert("v", Verse);
  187. FB2_END_KEYHASH
  188. Fb2Handler::BodyHandler::BodyHandler(Fb2TextCursor &cursor, const QString &name)
  189. : TextHandler(cursor, name)
  190. , m_feed(false)
  191. {
  192. QTextBlockFormat blockFormat;
  193. blockFormat.setTopMargin(4);
  194. blockFormat.setBottomMargin(4);
  195. cursor.setBlockFormat(blockFormat);
  196. }
  197. Fb2Handler::BaseHandler * Fb2Handler::BodyHandler::NewTag(const QString &name, const QXmlAttributes &attributes)
  198. {
  199. switch (toKeyword(name)) {
  200. case Paragraph : return new BlockHandler(*this, name, attributes);
  201. case Image : return new ImageHandler(*this, name, attributes);
  202. case Section : return new SectionHandler(*this, name, attributes);
  203. case Title : return new TitleHandler(*this, name, attributes);
  204. case Poem : return new SectionHandler(*this, name, attributes);
  205. case Stanza : return new SectionHandler(*this, name, attributes);
  206. default: return NULL;
  207. }
  208. }
  209. //---------------------------------------------------------------------------
  210. // Fb2Handler::SectionHandler
  211. //---------------------------------------------------------------------------
  212. FB2_BEGIN_KEYHASH(SectionHandler)
  213. insert("title", Title);
  214. insert("epigraph", Epigraph);
  215. insert("image", Image);
  216. insert("annotation", Annotation);
  217. insert("section", Section);
  218. insert("p", Paragraph);
  219. insert("poem", Poem);
  220. insert("subtitle", Subtitle);
  221. insert("cite", Cite);
  222. insert("empty-line", Emptyline);
  223. insert("table", Table);
  224. FB2_END_KEYHASH
  225. Fb2Handler::SectionHandler::SectionHandler(TextHandler &parent, const QString &name, const QXmlAttributes &attributes)
  226. : TextHandler(parent, name)
  227. , m_frame(cursor().currentFrame())
  228. , m_feed(false)
  229. {
  230. Q_UNUSED(attributes);
  231. QTextFrameFormat frameFormat;
  232. frameFormat.setBorder(1);
  233. frameFormat.setPadding(8);
  234. frameFormat.setTopMargin(4);
  235. frameFormat.setBottomMargin(4);
  236. if (name == "title") frameFormat.setWidth(QTextLength(QTextLength::PercentageLength, 80));
  237. cursor().insertFrame(frameFormat);
  238. QTextBlockFormat blockFormat;
  239. blockFormat.setTopMargin(4);
  240. blockFormat.setBottomMargin(4);
  241. cursor().setBlockFormat(blockFormat);
  242. }
  243. Fb2Handler::BaseHandler * Fb2Handler::SectionHandler::NewTag(const QString &name, const QXmlAttributes &attributes)
  244. {
  245. Keyword keyword = toKeyword(name);
  246. switch (keyword) {
  247. case Paragraph:
  248. case Emptyline:
  249. case Image:
  250. if (m_feed) cursor().insertBlock();
  251. m_feed = true;
  252. break;
  253. default:
  254. m_feed = false;
  255. }
  256. switch (keyword) {
  257. case Emptyline : return new BaseHandler(name); break;
  258. case Paragraph : return new BlockHandler(*this, name, attributes); break;
  259. case Section : return new SectionHandler(*this, name, attributes); break;
  260. case Title : return new TitleHandler(*this, name, attributes); break;
  261. case Poem : return new PoemHandler(*this, name, attributes); break;
  262. case Image : return new ImageHandler(*this, name, attributes); break;
  263. default : return new UnknowHandler(*this, name); break;
  264. }
  265. ;
  266. }
  267. void Fb2Handler::SectionHandler::EndTag(const QString &name)
  268. {
  269. Q_UNUSED(name);
  270. if (m_frame) cursor().setPosition(m_frame->lastPosition());
  271. }
  272. //---------------------------------------------------------------------------
  273. // Fb2Handler::TitleHandler
  274. //---------------------------------------------------------------------------
  275. FB2_BEGIN_KEYHASH(TitleHandler)
  276. insert("p", Paragraph);
  277. insert("empty-line", Emptyline);
  278. FB2_END_KEYHASH
  279. Fb2Handler::TitleHandler::TitleHandler(TextHandler &parent, const QString &name, const QXmlAttributes &attributes)
  280. : TextHandler(parent, name)
  281. , m_frame(cursor().currentFrame())
  282. , m_table(NULL)
  283. , m_feed(false)
  284. {
  285. Q_UNUSED(attributes);
  286. QTextTableFormat tableFormat;
  287. tableFormat.setBorder(0);
  288. tableFormat.setCellPadding(4);
  289. tableFormat.setCellSpacing(0);
  290. tableFormat.setWidth(QTextLength(QTextLength::PercentageLength, 100));
  291. m_table = cursor().insertTable(1, 1, tableFormat);
  292. QTextTableCellFormat cellFormat;
  293. cellFormat.setBackground(Qt::darkGreen);
  294. cellFormat.setForeground(Qt::white);
  295. m_table->cellAt(cursor()).setFormat(cellFormat);
  296. QTextBlockFormat blockFormat;
  297. blockFormat.setAlignment(Qt::AlignHCenter);
  298. cursor().mergeBlockFormat(blockFormat);
  299. }
  300. Fb2Handler::BaseHandler * Fb2Handler::TitleHandler::NewTag(const QString &name, const QXmlAttributes &attributes)
  301. {
  302. if (m_feed) cursor().insertBlock(); else m_feed = true;
  303. switch (toKeyword(name)) {
  304. case Paragraph : return new BlockHandler(*this, name, attributes); break;
  305. case Emptyline : return new BlockHandler(*this, name, attributes); break;
  306. default : return new UnknowHandler(*this, name); break;
  307. }
  308. }
  309. void Fb2Handler::TitleHandler::EndTag(const QString &name)
  310. {
  311. Q_UNUSED(name);
  312. if (m_frame) cursor().setPosition(m_frame->lastPosition());
  313. }
  314. //---------------------------------------------------------------------------
  315. // Fb2Handler::PoemHandler
  316. //---------------------------------------------------------------------------
  317. FB2_BEGIN_KEYHASH(PoemHandler)
  318. insert("title", Title);
  319. insert("epigraph", Epigraph);
  320. insert("stanza", Stanza);
  321. insert("author", Author);
  322. insert("date", Date);
  323. FB2_END_KEYHASH
  324. Fb2Handler::PoemHandler::PoemHandler(TextHandler &parent, const QString &name, const QXmlAttributes &attributes)
  325. : TextHandler(parent, name)
  326. , m_frame(cursor().currentFrame())
  327. , m_table(NULL)
  328. , m_feed(false)
  329. {
  330. Q_UNUSED(attributes);
  331. QTextTableFormat format;
  332. format.setBorder(1);
  333. format.setCellPadding(4);
  334. format.setCellSpacing(4);
  335. format.setBorderStyle(QTextFrameFormat::BorderStyle_Solid);
  336. m_table = cursor().insertTable(1, 1, format);
  337. }
  338. Fb2Handler::BaseHandler * Fb2Handler::PoemHandler::NewTag(const QString &name, const QXmlAttributes &attributes)
  339. {
  340. switch (toKeyword(name)) {
  341. case Title:
  342. case Epigraph:
  343. case Stanza:
  344. case Author:
  345. if (m_feed) m_table->appendRows(1);
  346. m_feed = true;
  347. return new StanzaHandler(*this, name, attributes);
  348. default:
  349. return NULL;
  350. }
  351. }
  352. void Fb2Handler::PoemHandler::EndTag(const QString &name)
  353. {
  354. Q_UNUSED(name);
  355. if (m_frame) cursor().setPosition(m_frame->lastPosition());
  356. }
  357. //---------------------------------------------------------------------------
  358. // Fb2Handler::StanzaHandler
  359. //---------------------------------------------------------------------------
  360. FB2_BEGIN_KEYHASH(StanzaHandler)
  361. insert("title", Title);
  362. insert("subtitle", Subtitle);
  363. insert("v", Verse);
  364. FB2_END_KEYHASH
  365. Fb2Handler::StanzaHandler::StanzaHandler(TextHandler &parent, const QString &name, const QXmlAttributes &attributes)
  366. : TextHandler(parent, name)
  367. , m_feed(false)
  368. {
  369. Q_UNUSED(attributes);
  370. }
  371. Fb2Handler::BaseHandler * Fb2Handler::StanzaHandler::NewTag(const QString &name, const QXmlAttributes &attributes)
  372. {
  373. Keyword keyword = toKeyword(name);
  374. switch (keyword) {
  375. case Title:
  376. case Subtitle:
  377. case Verse:
  378. if (m_feed) cursor().insertBlock();
  379. m_feed = true;
  380. default: ;
  381. }
  382. switch (keyword) {
  383. case Title:
  384. case Subtitle : return new TitleHandler(*this, name, attributes);
  385. case Verse : return new BlockHandler(*this, name, attributes);
  386. default: return NULL;
  387. }
  388. }
  389. //---------------------------------------------------------------------------
  390. // Fb2Handler::BlockHandler
  391. //---------------------------------------------------------------------------
  392. FB2_BEGIN_KEYHASH(BlockHandler)
  393. insert("strong" , Strong);
  394. insert("emphasis" , Emphasis);
  395. insert("style" , Style);
  396. insert("a" , Anchor);
  397. insert("strikethrough" , Strikethrough);
  398. insert("sub" , Sub);
  399. insert("sup" , Sup);
  400. insert("code" , Code);
  401. insert("image" , Image);
  402. FB2_END_KEYHASH
  403. Fb2Handler::BlockHandler::BlockHandler(TextHandler &parent, const QString &name, const QXmlAttributes &attributes)
  404. : TextHandler(parent, name)
  405. {
  406. Q_UNUSED(attributes);
  407. QTextBlockFormat blockFormat;
  408. blockFormat.setTopMargin(6);
  409. blockFormat.setBottomMargin(6);
  410. cursor().mergeBlockFormat(blockFormat);
  411. }
  412. Fb2Handler::BaseHandler * Fb2Handler::BlockHandler::NewTag(const QString &name, const QXmlAttributes &attributes)
  413. {
  414. Q_UNUSED(attributes);
  415. switch (toKeyword(name)) {
  416. default: return new UnknowHandler(*this, name); break;
  417. }
  418. }
  419. void Fb2Handler::BlockHandler::TxtTag(const QString &text)
  420. {
  421. cursor().insertText(text);
  422. }
  423. //---------------------------------------------------------------------------
  424. // Fb2Handler::ImageHandler
  425. //---------------------------------------------------------------------------
  426. Fb2Handler::ImageHandler::ImageHandler(TextHandler &parent, const QString &name, const QXmlAttributes &attributes)
  427. : TextHandler(parent, name)
  428. {
  429. QString image = Value(attributes, "href");
  430. while (image.left(1) == "#") image.remove(0, 1);
  431. if (!image.isEmpty()) cursor().insertImage(image);
  432. }
  433. Fb2Handler::BaseHandler * Fb2Handler::ImageHandler::NewTag(const QString &name, const QXmlAttributes &attributes)
  434. {
  435. Q_UNUSED(name);
  436. Q_UNUSED(attributes);
  437. return false;
  438. }
  439. //---------------------------------------------------------------------------
  440. // Fb2Handler::UnknowHandler
  441. //---------------------------------------------------------------------------
  442. Fb2Handler::UnknowHandler::UnknowHandler(TextHandler &parent, const QString &name)
  443. : TextHandler(parent, name)
  444. , m_parent(parent)
  445. {
  446. }
  447. Fb2Handler::BaseHandler * Fb2Handler::UnknowHandler::NewTag(const QString &name, const QXmlAttributes &attributes)
  448. {
  449. return m_parent.NewTag(name, attributes);
  450. }
  451. void Fb2Handler::UnknowHandler::TxtTag(const QString &text)
  452. {
  453. cursor().insertText(text);
  454. }
  455. //---------------------------------------------------------------------------
  456. // Fb2Handler::BinaryHandler
  457. //---------------------------------------------------------------------------
  458. Fb2Handler::BinaryHandler::BinaryHandler(QTextDocument &document, const QString &name, const QXmlAttributes &attributes)
  459. : BaseHandler(name)
  460. , m_document(document)
  461. , m_file(Value(attributes, "id"))
  462. {
  463. }
  464. void Fb2Handler::BinaryHandler::TxtTag(const QString &text)
  465. {
  466. m_text += text;
  467. }
  468. void Fb2Handler::BinaryHandler::EndTag(const QString &name)
  469. {
  470. Q_UNUSED(name);
  471. QByteArray in; in.append(m_text);
  472. QImage img = QImage::fromData(QByteArray::fromBase64(in));
  473. if (!m_file.isEmpty()) m_document.addResource(QTextDocument::ImageResource, QUrl(m_file), img);
  474. }
  475. //---------------------------------------------------------------------------
  476. // Fb2Handler
  477. //---------------------------------------------------------------------------
  478. Fb2Handler::Fb2Handler(QTextDocument & document)
  479. : m_document(document)
  480. , m_handler(NULL)
  481. {
  482. document.clear();
  483. }
  484. Fb2Handler::~Fb2Handler()
  485. {
  486. if (m_handler) delete m_handler;
  487. }
  488. bool Fb2Handler::startElement(const QString & namespaceURI, const QString & localName, const QString &qName, const QXmlAttributes &attributes)
  489. {
  490. Q_UNUSED(namespaceURI);
  491. Q_UNUSED(localName);
  492. const QString name = qName.toLower();
  493. if (m_handler) return m_handler->doStart(name, attributes);
  494. qCritical() << name;
  495. if (name == "fictionbook") {
  496. m_handler = new RootHandler(m_document, name);
  497. return true;
  498. } else {
  499. m_error = QObject::tr("The file is not an FB2 file.");
  500. return false;
  501. }
  502. }
  503. static bool isWhiteSpace(const QString &str)
  504. {
  505. return str.simplified().isEmpty();
  506. }
  507. bool Fb2Handler::characters(const QString &str)
  508. {
  509. QString s = str.simplified();
  510. if (s.isEmpty()) return true;
  511. if (isWhiteSpace(str.left(1))) s.prepend(" ");
  512. if (isWhiteSpace(str.right(1))) s.append(" ");
  513. return m_handler && m_handler->doText(s);
  514. }
  515. bool Fb2Handler::endElement(const QString & namespaceURI, const QString & localName, const QString &qName)
  516. {
  517. Q_UNUSED(namespaceURI);
  518. Q_UNUSED(localName);
  519. bool found = false;
  520. return m_handler && m_handler->doEnd(qName.toLower(), found);
  521. }
  522. bool Fb2Handler::fatalError(const QXmlParseException &exception)
  523. {
  524. qCritical() << QObject::tr("Parse error at line %1, column %2:\n%3")
  525. .arg(exception.lineNumber())
  526. .arg(exception.columnNumber())
  527. .arg(exception.message());
  528. return false;
  529. }
  530. QString Fb2Handler::errorString() const
  531. {
  532. return m_error;
  533. }
  534. #undef FB2_BEGIN_KEYHASH
  535. #undef FB2_END_KEYHASH