fb2read.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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. void Fb2Handler::TextHandler::EndTag(const QString &name)
  167. {
  168. Q_UNUSED(name);
  169. cursor().setCharFormat(m_charFormat);
  170. }
  171. //---------------------------------------------------------------------------
  172. // Fb2Handler::BodyHandler
  173. //---------------------------------------------------------------------------
  174. FB2_BEGIN_KEYHASH(BodyHandler)
  175. insert("image", Image);
  176. insert("title", Title);
  177. insert("epigraph", Epigraph);
  178. insert("section", Section);
  179. insert("p", Paragraph);
  180. insert("poem", Poem);
  181. insert("stanza", Stanza);
  182. insert("v", Verse);
  183. FB2_END_KEYHASH
  184. Fb2Handler::BodyHandler::BodyHandler(Fb2TextCursor &cursor, const QString &name)
  185. : TextHandler(cursor, name)
  186. , m_feed(false)
  187. {
  188. QTextBlockFormat blockFormat;
  189. blockFormat.setTopMargin(4);
  190. blockFormat.setBottomMargin(4);
  191. cursor.setBlockFormat(blockFormat);
  192. }
  193. Fb2Handler::BaseHandler * Fb2Handler::BodyHandler::NewTag(const QString &name, const QXmlAttributes &attributes)
  194. {
  195. switch (toKeyword(name)) {
  196. case Paragraph : return new BlockHandler(*this, name, attributes);
  197. case Image : return new ImageHandler(*this, name, attributes);
  198. case Section : return new SectionHandler(*this, name, attributes);
  199. case Title : return new TitleHandler(*this, name, attributes);
  200. case Poem : return new SectionHandler(*this, name, attributes);
  201. case Stanza : return new SectionHandler(*this, name, attributes);
  202. default: return NULL;
  203. }
  204. }
  205. //---------------------------------------------------------------------------
  206. // Fb2Handler::SectionHandler
  207. //---------------------------------------------------------------------------
  208. FB2_BEGIN_KEYHASH(SectionHandler)
  209. insert("title", Title);
  210. insert("epigraph", Epigraph);
  211. insert("image", Image);
  212. insert("annotation", Annotation);
  213. insert("section", Section);
  214. insert("p", Paragraph);
  215. insert("poem", Poem);
  216. insert("subtitle", Subtitle);
  217. insert("cite", Cite);
  218. insert("empty-line", Emptyline);
  219. insert("table", Table);
  220. FB2_END_KEYHASH
  221. Fb2Handler::SectionHandler::SectionHandler(TextHandler &parent, const QString &name, const QXmlAttributes &attributes)
  222. : TextHandler(parent, name)
  223. , m_frame(cursor().currentFrame())
  224. , m_feed(false)
  225. {
  226. Q_UNUSED(attributes);
  227. QTextFrameFormat frameFormat;
  228. frameFormat.setBorder(1);
  229. frameFormat.setPadding(8);
  230. frameFormat.setTopMargin(4);
  231. frameFormat.setBottomMargin(4);
  232. if (name == "title") frameFormat.setWidth(QTextLength(QTextLength::PercentageLength, 80));
  233. cursor().insertFrame(frameFormat);
  234. QTextBlockFormat blockFormat;
  235. blockFormat.setTopMargin(4);
  236. blockFormat.setBottomMargin(4);
  237. cursor().setBlockFormat(blockFormat);
  238. }
  239. Fb2Handler::BaseHandler * Fb2Handler::SectionHandler::NewTag(const QString &name, const QXmlAttributes &attributes)
  240. {
  241. Keyword keyword = toKeyword(name);
  242. switch (keyword) {
  243. case Paragraph:
  244. case Emptyline:
  245. case Image:
  246. if (m_feed) cursor().insertBlock();
  247. m_feed = true;
  248. break;
  249. default:
  250. m_feed = false;
  251. }
  252. switch (keyword) {
  253. case Emptyline : return new BaseHandler(name); break;
  254. case Paragraph : return new BlockHandler(*this, name, attributes); break;
  255. case Section : return new SectionHandler(*this, name, attributes); break;
  256. case Title : return new TitleHandler(*this, name, attributes); break;
  257. case Poem : return new PoemHandler(*this, name, attributes); break;
  258. case Image : return new ImageHandler(*this, name, attributes); break;
  259. default: return NULL;
  260. }
  261. ;
  262. }
  263. void Fb2Handler::SectionHandler::EndTag(const QString &name)
  264. {
  265. Q_UNUSED(name);
  266. if (m_frame) cursor().setPosition(m_frame->lastPosition());
  267. }
  268. //---------------------------------------------------------------------------
  269. // Fb2Handler::TitleHandler
  270. //---------------------------------------------------------------------------
  271. FB2_BEGIN_KEYHASH(TitleHandler)
  272. insert("p", Paragraph);
  273. insert("empty-line", Emptyline);
  274. FB2_END_KEYHASH
  275. Fb2Handler::TitleHandler::TitleHandler(TextHandler &parent, const QString &name, const QXmlAttributes &attributes)
  276. : TextHandler(parent, name)
  277. , m_frame(cursor().currentFrame())
  278. , m_table(NULL)
  279. , m_feed(false)
  280. {
  281. Q_UNUSED(attributes);
  282. QTextTableFormat format1;
  283. format1.setBorder(0);
  284. format1.setCellPadding(4);
  285. format1.setCellSpacing(4);
  286. format1.setWidth(QTextLength(QTextLength::PercentageLength, 100));
  287. m_table = cursor().insertTable(1, 1, format1);
  288. QTextTableCellFormat format2;
  289. format2.setBackground(Qt::darkGreen);
  290. format2.setForeground(Qt::white);
  291. m_table->cellAt(cursor()).setFormat(format2);
  292. }
  293. Fb2Handler::BaseHandler * Fb2Handler::TitleHandler::NewTag(const QString &name, const QXmlAttributes &attributes)
  294. {
  295. if (m_feed) cursor().insertBlock(); else m_feed = true;
  296. switch (toKeyword(name)) {
  297. case Paragraph : return new BlockHandler(*this, name, attributes); break;
  298. case Emptyline : return new BlockHandler(*this, name, attributes); break;
  299. default: return NULL;
  300. }
  301. }
  302. void Fb2Handler::TitleHandler::EndTag(const QString &name)
  303. {
  304. Q_UNUSED(name);
  305. if (m_frame) cursor().setPosition(m_frame->lastPosition());
  306. }
  307. //---------------------------------------------------------------------------
  308. // Fb2Handler::PoemHandler
  309. //---------------------------------------------------------------------------
  310. FB2_BEGIN_KEYHASH(PoemHandler)
  311. insert("title", Title);
  312. insert("epigraph", Epigraph);
  313. insert("stanza", Stanza);
  314. insert("author", Author);
  315. insert("date", Date);
  316. FB2_END_KEYHASH
  317. Fb2Handler::PoemHandler::PoemHandler(TextHandler &parent, const QString &name, const QXmlAttributes &attributes)
  318. : TextHandler(parent, name)
  319. , m_frame(cursor().currentFrame())
  320. , m_table(NULL)
  321. , m_feed(false)
  322. {
  323. Q_UNUSED(attributes);
  324. QTextTableFormat format;
  325. format.setBorder(1);
  326. format.setCellPadding(4);
  327. format.setCellSpacing(4);
  328. format.setBorderStyle(QTextFrameFormat::BorderStyle_Solid);
  329. m_table = cursor().insertTable(1, 1, format);
  330. }
  331. Fb2Handler::BaseHandler * Fb2Handler::PoemHandler::NewTag(const QString &name, const QXmlAttributes &attributes)
  332. {
  333. switch (toKeyword(name)) {
  334. case Title:
  335. case Epigraph:
  336. case Stanza:
  337. case Author:
  338. if (m_feed) m_table->appendRows(1);
  339. m_feed = true;
  340. return new StanzaHandler(*this, name, attributes);
  341. default:
  342. return NULL;
  343. }
  344. }
  345. void Fb2Handler::PoemHandler::EndTag(const QString &name)
  346. {
  347. Q_UNUSED(name);
  348. if (m_frame) cursor().setPosition(m_frame->lastPosition());
  349. }
  350. //---------------------------------------------------------------------------
  351. // Fb2Handler::StanzaHandler
  352. //---------------------------------------------------------------------------
  353. FB2_BEGIN_KEYHASH(StanzaHandler)
  354. insert("title", Title);
  355. insert("subtitle", Subtitle);
  356. insert("v", Verse);
  357. FB2_END_KEYHASH
  358. Fb2Handler::StanzaHandler::StanzaHandler(TextHandler &parent, const QString &name, const QXmlAttributes &attributes)
  359. : TextHandler(parent, name)
  360. , m_feed(false)
  361. {
  362. Q_UNUSED(attributes);
  363. }
  364. Fb2Handler::BaseHandler * Fb2Handler::StanzaHandler::NewTag(const QString &name, const QXmlAttributes &attributes)
  365. {
  366. Keyword keyword = toKeyword(name);
  367. switch (keyword) {
  368. case Title:
  369. case Subtitle:
  370. case Verse:
  371. if (m_feed) cursor().insertBlock();
  372. m_feed = true;
  373. default: ;
  374. }
  375. switch (keyword) {
  376. case Title:
  377. case Subtitle : return new TitleHandler(*this, name, attributes);
  378. case Verse : return new BlockHandler(*this, name, attributes);
  379. default: return NULL;
  380. }
  381. }
  382. //---------------------------------------------------------------------------
  383. // Fb2Handler::BlockHandler
  384. //---------------------------------------------------------------------------
  385. FB2_BEGIN_KEYHASH(BlockHandler)
  386. insert("strong" , Strong);
  387. insert("emphasis" , Emphasis);
  388. insert("style" , Style);
  389. insert("a" , Anchor);
  390. insert("strikethrough" , Strikethrough);
  391. insert("sub" , Sub);
  392. insert("sup" , Sup);
  393. insert("code" , Code);
  394. insert("image" , Image);
  395. FB2_END_KEYHASH
  396. Fb2Handler::BlockHandler::BlockHandler(TextHandler &parent, const QString &name, const QXmlAttributes &attributes)
  397. : TextHandler(parent, name)
  398. {
  399. Q_UNUSED(attributes);
  400. QTextBlockFormat blockFormat;
  401. blockFormat.setTopMargin(4);
  402. blockFormat.setBottomMargin(4);
  403. cursor().setBlockFormat(blockFormat);
  404. }
  405. Fb2Handler::BaseHandler * Fb2Handler::BlockHandler::NewTag(const QString &name, const QXmlAttributes &attributes)
  406. {
  407. Q_UNUSED(attributes);
  408. switch (toKeyword(name)) {
  409. default: return new BaseHandler(name); break;
  410. }
  411. }
  412. void Fb2Handler::BlockHandler::TxtTag(const QString &text)
  413. {
  414. cursor().insertText(text);
  415. }
  416. //---------------------------------------------------------------------------
  417. // Fb2Handler::ImageHandler
  418. //---------------------------------------------------------------------------
  419. Fb2Handler::ImageHandler::ImageHandler(TextHandler &parent, const QString &name, const QXmlAttributes &attributes)
  420. : TextHandler(parent, name)
  421. {
  422. QString image = Value(attributes, "href");
  423. while (image.left(1) == "#") image.remove(0, 1);
  424. if (!image.isEmpty()) cursor().insertImage(image);
  425. }
  426. Fb2Handler::BaseHandler * Fb2Handler::ImageHandler::NewTag(const QString &name, const QXmlAttributes &attributes)
  427. {
  428. Q_UNUSED(name);
  429. Q_UNUSED(attributes);
  430. return false;
  431. }
  432. //---------------------------------------------------------------------------
  433. // Fb2Handler::BinaryHandler
  434. //---------------------------------------------------------------------------
  435. Fb2Handler::BinaryHandler::BinaryHandler(QTextDocument &document, const QString &name, const QXmlAttributes &attributes)
  436. : BaseHandler(name)
  437. , m_document(document)
  438. , m_file(Value(attributes, "id"))
  439. {
  440. }
  441. void Fb2Handler::BinaryHandler::TxtTag(const QString &text)
  442. {
  443. m_text += text;
  444. }
  445. void Fb2Handler::BinaryHandler::EndTag(const QString &name)
  446. {
  447. Q_UNUSED(name);
  448. QByteArray in; in.append(m_text);
  449. QImage img = QImage::fromData(QByteArray::fromBase64(in));
  450. if (!m_file.isEmpty()) m_document.addResource(QTextDocument::ImageResource, QUrl(m_file), img);
  451. }
  452. //---------------------------------------------------------------------------
  453. // Fb2Handler
  454. //---------------------------------------------------------------------------
  455. Fb2Handler::Fb2Handler(QTextDocument & document)
  456. : m_document(document)
  457. , m_handler(NULL)
  458. {
  459. document.clear();
  460. }
  461. Fb2Handler::~Fb2Handler()
  462. {
  463. if (m_handler) delete m_handler;
  464. }
  465. bool Fb2Handler::startElement(const QString & namespaceURI, const QString & localName, const QString &qName, const QXmlAttributes &attributes)
  466. {
  467. Q_UNUSED(namespaceURI);
  468. Q_UNUSED(localName);
  469. const QString name = qName.toLower();
  470. if (m_handler) return m_handler->doStart(name, attributes);
  471. qCritical() << name;
  472. if (name == "fictionbook") {
  473. m_handler = new RootHandler(m_document, name);
  474. return true;
  475. } else {
  476. m_error = QObject::tr("The file is not an FB2 file.");
  477. return false;
  478. }
  479. }
  480. static bool isWhiteSpace(const QString &str)
  481. {
  482. return str.simplified().isEmpty();
  483. }
  484. bool Fb2Handler::characters(const QString &str)
  485. {
  486. QString s = str.simplified();
  487. if (s.isEmpty()) return true;
  488. if (isWhiteSpace(str.left(1))) s.prepend(" ");
  489. if (isWhiteSpace(str.right(1))) s.append(" ");
  490. return m_handler && m_handler->doText(s);
  491. }
  492. bool Fb2Handler::endElement(const QString & namespaceURI, const QString & localName, const QString &qName)
  493. {
  494. Q_UNUSED(namespaceURI);
  495. Q_UNUSED(localName);
  496. bool found = false;
  497. return m_handler && m_handler->doEnd(qName.toLower(), found);
  498. }
  499. bool Fb2Handler::fatalError(const QXmlParseException &exception)
  500. {
  501. qCritical() << QObject::tr("Parse error at line %1, column %2:\n%3")
  502. .arg(exception.lineNumber())
  503. .arg(exception.columnNumber())
  504. .arg(exception.message());
  505. return false;
  506. }
  507. QString Fb2Handler::errorString() const
  508. {
  509. return m_error;
  510. }
  511. #undef FB2_BEGIN_KEYHASH
  512. #undef FB2_END_KEYHASH