fb2read.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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 tableFormat;
  283. tableFormat.setBorder(0);
  284. tableFormat.setCellPadding(4);
  285. tableFormat.setCellSpacing(0);
  286. tableFormat.setWidth(QTextLength(QTextLength::PercentageLength, 100));
  287. m_table = cursor().insertTable(1, 1, tableFormat);
  288. QTextTableCellFormat cellFormat;
  289. cellFormat.setBackground(Qt::darkGreen);
  290. cellFormat.setForeground(Qt::white);
  291. m_table->cellAt(cursor()).setFormat(cellFormat);
  292. QTextBlockFormat blockFormat;
  293. blockFormat.setAlignment(Qt::AlignHCenter);
  294. cursor().mergeBlockFormat(blockFormat);
  295. }
  296. Fb2Handler::BaseHandler * Fb2Handler::TitleHandler::NewTag(const QString &name, const QXmlAttributes &attributes)
  297. {
  298. if (m_feed) cursor().insertBlock(); else m_feed = true;
  299. switch (toKeyword(name)) {
  300. case Paragraph : return new BlockHandler(*this, name, attributes); break;
  301. case Emptyline : return new BlockHandler(*this, name, attributes); break;
  302. default: return NULL;
  303. }
  304. }
  305. void Fb2Handler::TitleHandler::EndTag(const QString &name)
  306. {
  307. Q_UNUSED(name);
  308. if (m_frame) cursor().setPosition(m_frame->lastPosition());
  309. }
  310. //---------------------------------------------------------------------------
  311. // Fb2Handler::PoemHandler
  312. //---------------------------------------------------------------------------
  313. FB2_BEGIN_KEYHASH(PoemHandler)
  314. insert("title", Title);
  315. insert("epigraph", Epigraph);
  316. insert("stanza", Stanza);
  317. insert("author", Author);
  318. insert("date", Date);
  319. FB2_END_KEYHASH
  320. Fb2Handler::PoemHandler::PoemHandler(TextHandler &parent, const QString &name, const QXmlAttributes &attributes)
  321. : TextHandler(parent, name)
  322. , m_frame(cursor().currentFrame())
  323. , m_table(NULL)
  324. , m_feed(false)
  325. {
  326. Q_UNUSED(attributes);
  327. QTextTableFormat format;
  328. format.setBorder(1);
  329. format.setCellPadding(4);
  330. format.setCellSpacing(4);
  331. format.setBorderStyle(QTextFrameFormat::BorderStyle_Solid);
  332. m_table = cursor().insertTable(1, 1, format);
  333. }
  334. Fb2Handler::BaseHandler * Fb2Handler::PoemHandler::NewTag(const QString &name, const QXmlAttributes &attributes)
  335. {
  336. switch (toKeyword(name)) {
  337. case Title:
  338. case Epigraph:
  339. case Stanza:
  340. case Author:
  341. if (m_feed) m_table->appendRows(1);
  342. m_feed = true;
  343. return new StanzaHandler(*this, name, attributes);
  344. default:
  345. return NULL;
  346. }
  347. }
  348. void Fb2Handler::PoemHandler::EndTag(const QString &name)
  349. {
  350. Q_UNUSED(name);
  351. if (m_frame) cursor().setPosition(m_frame->lastPosition());
  352. }
  353. //---------------------------------------------------------------------------
  354. // Fb2Handler::StanzaHandler
  355. //---------------------------------------------------------------------------
  356. FB2_BEGIN_KEYHASH(StanzaHandler)
  357. insert("title", Title);
  358. insert("subtitle", Subtitle);
  359. insert("v", Verse);
  360. FB2_END_KEYHASH
  361. Fb2Handler::StanzaHandler::StanzaHandler(TextHandler &parent, const QString &name, const QXmlAttributes &attributes)
  362. : TextHandler(parent, name)
  363. , m_feed(false)
  364. {
  365. Q_UNUSED(attributes);
  366. }
  367. Fb2Handler::BaseHandler * Fb2Handler::StanzaHandler::NewTag(const QString &name, const QXmlAttributes &attributes)
  368. {
  369. Keyword keyword = toKeyword(name);
  370. switch (keyword) {
  371. case Title:
  372. case Subtitle:
  373. case Verse:
  374. if (m_feed) cursor().insertBlock();
  375. m_feed = true;
  376. default: ;
  377. }
  378. switch (keyword) {
  379. case Title:
  380. case Subtitle : return new TitleHandler(*this, name, attributes);
  381. case Verse : return new BlockHandler(*this, name, attributes);
  382. default: return NULL;
  383. }
  384. }
  385. //---------------------------------------------------------------------------
  386. // Fb2Handler::BlockHandler
  387. //---------------------------------------------------------------------------
  388. FB2_BEGIN_KEYHASH(BlockHandler)
  389. insert("strong" , Strong);
  390. insert("emphasis" , Emphasis);
  391. insert("style" , Style);
  392. insert("a" , Anchor);
  393. insert("strikethrough" , Strikethrough);
  394. insert("sub" , Sub);
  395. insert("sup" , Sup);
  396. insert("code" , Code);
  397. insert("image" , Image);
  398. FB2_END_KEYHASH
  399. Fb2Handler::BlockHandler::BlockHandler(TextHandler &parent, const QString &name, const QXmlAttributes &attributes)
  400. : TextHandler(parent, name)
  401. {
  402. Q_UNUSED(attributes);
  403. QTextBlockFormat blockFormat;
  404. blockFormat.setTopMargin(6);
  405. blockFormat.setBottomMargin(6);
  406. cursor().mergeBlockFormat(blockFormat);
  407. }
  408. Fb2Handler::BaseHandler * Fb2Handler::BlockHandler::NewTag(const QString &name, const QXmlAttributes &attributes)
  409. {
  410. Q_UNUSED(attributes);
  411. switch (toKeyword(name)) {
  412. default: return new BaseHandler(name); break;
  413. }
  414. }
  415. void Fb2Handler::BlockHandler::TxtTag(const QString &text)
  416. {
  417. cursor().insertText(text);
  418. }
  419. //---------------------------------------------------------------------------
  420. // Fb2Handler::ImageHandler
  421. //---------------------------------------------------------------------------
  422. Fb2Handler::ImageHandler::ImageHandler(TextHandler &parent, const QString &name, const QXmlAttributes &attributes)
  423. : TextHandler(parent, name)
  424. {
  425. QString image = Value(attributes, "href");
  426. while (image.left(1) == "#") image.remove(0, 1);
  427. if (!image.isEmpty()) cursor().insertImage(image);
  428. }
  429. Fb2Handler::BaseHandler * Fb2Handler::ImageHandler::NewTag(const QString &name, const QXmlAttributes &attributes)
  430. {
  431. Q_UNUSED(name);
  432. Q_UNUSED(attributes);
  433. return false;
  434. }
  435. //---------------------------------------------------------------------------
  436. // Fb2Handler::BinaryHandler
  437. //---------------------------------------------------------------------------
  438. Fb2Handler::BinaryHandler::BinaryHandler(QTextDocument &document, const QString &name, const QXmlAttributes &attributes)
  439. : BaseHandler(name)
  440. , m_document(document)
  441. , m_file(Value(attributes, "id"))
  442. {
  443. }
  444. void Fb2Handler::BinaryHandler::TxtTag(const QString &text)
  445. {
  446. m_text += text;
  447. }
  448. void Fb2Handler::BinaryHandler::EndTag(const QString &name)
  449. {
  450. Q_UNUSED(name);
  451. QByteArray in; in.append(m_text);
  452. QImage img = QImage::fromData(QByteArray::fromBase64(in));
  453. if (!m_file.isEmpty()) m_document.addResource(QTextDocument::ImageResource, QUrl(m_file), img);
  454. }
  455. //---------------------------------------------------------------------------
  456. // Fb2Handler
  457. //---------------------------------------------------------------------------
  458. Fb2Handler::Fb2Handler(QTextDocument & document)
  459. : m_document(document)
  460. , m_handler(NULL)
  461. {
  462. document.clear();
  463. }
  464. Fb2Handler::~Fb2Handler()
  465. {
  466. if (m_handler) delete m_handler;
  467. }
  468. bool Fb2Handler::startElement(const QString & namespaceURI, const QString & localName, const QString &qName, const QXmlAttributes &attributes)
  469. {
  470. Q_UNUSED(namespaceURI);
  471. Q_UNUSED(localName);
  472. const QString name = qName.toLower();
  473. if (m_handler) return m_handler->doStart(name, attributes);
  474. qCritical() << name;
  475. if (name == "fictionbook") {
  476. m_handler = new RootHandler(m_document, name);
  477. return true;
  478. } else {
  479. m_error = QObject::tr("The file is not an FB2 file.");
  480. return false;
  481. }
  482. }
  483. static bool isWhiteSpace(const QString &str)
  484. {
  485. return str.simplified().isEmpty();
  486. }
  487. bool Fb2Handler::characters(const QString &str)
  488. {
  489. QString s = str.simplified();
  490. if (s.isEmpty()) return true;
  491. if (isWhiteSpace(str.left(1))) s.prepend(" ");
  492. if (isWhiteSpace(str.right(1))) s.append(" ");
  493. return m_handler && m_handler->doText(s);
  494. }
  495. bool Fb2Handler::endElement(const QString & namespaceURI, const QString & localName, const QString &qName)
  496. {
  497. Q_UNUSED(namespaceURI);
  498. Q_UNUSED(localName);
  499. bool found = false;
  500. return m_handler && m_handler->doEnd(qName.toLower(), found);
  501. }
  502. bool Fb2Handler::fatalError(const QXmlParseException &exception)
  503. {
  504. qCritical() << QObject::tr("Parse error at line %1, column %2:\n%3")
  505. .arg(exception.lineNumber())
  506. .arg(exception.columnNumber())
  507. .arg(exception.message());
  508. return false;
  509. }
  510. QString Fb2Handler::errorString() const
  511. {
  512. return m_error;
  513. }
  514. #undef FB2_BEGIN_KEYHASH
  515. #undef FB2_END_KEYHASH