fb2read.cpp 18 KB

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