fb2page.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. #include "fb2page.hpp"
  2. #include <QTimer>
  3. #include <QWebFrame>
  4. #include <QtDebug>
  5. #include "fb2read.hpp"
  6. #include "fb2save.hpp"
  7. #include "fb2imgs.hpp"
  8. #include "fb2utils.h"
  9. #include "fb2html.h"
  10. #include "fb2xml2.h"
  11. //---------------------------------------------------------------------------
  12. // FbTextLogger
  13. //---------------------------------------------------------------------------
  14. void FbTextLogger::trace(const QString &text)
  15. {
  16. qCritical() << text;
  17. }
  18. //---------------------------------------------------------------------------
  19. // FbTextPage
  20. //---------------------------------------------------------------------------
  21. FbTextPage::FbTextPage(QObject *parent)
  22. : QWebPage(parent)
  23. , m_logger(this)
  24. {
  25. QWebSettings *s = settings();
  26. s->setAttribute(QWebSettings::AutoLoadImages, true);
  27. s->setAttribute(QWebSettings::DeveloperExtrasEnabled, true);
  28. s->setAttribute(QWebSettings::JavaEnabled, false);
  29. s->setAttribute(QWebSettings::JavascriptEnabled, true);
  30. s->setAttribute(QWebSettings::PrivateBrowsingEnabled, true);
  31. s->setAttribute(QWebSettings::PluginsEnabled, false);
  32. s->setAttribute(QWebSettings::ZoomTextOnly, true);
  33. s->setUserStyleSheetUrl(QUrl("qrc:style.css"));
  34. QString html = block("body", block("section", p()));
  35. mainFrame()->setHtml(html, createUrl());
  36. setContentEditable(true);
  37. setNetworkAccessManager(new FbNetworkAccessManager(this));
  38. connect(this, SIGNAL(loadFinished(bool)), SLOT(loadFinished()));
  39. connect(this, SIGNAL(contentsChanged()), SLOT(fixContents()));
  40. connect(this, SIGNAL(selectionChanged()), SLOT(showStatus()));
  41. }
  42. FbNetworkAccessManager *FbTextPage::manager()
  43. {
  44. return qobject_cast<FbNetworkAccessManager*>(networkAccessManager());
  45. }
  46. bool FbTextPage::read(const QString &html)
  47. {
  48. QXmlInputSource *source = new QXmlInputSource();
  49. source->setData(html);
  50. FbReadThread::execute(this, source, 0);
  51. return true;
  52. }
  53. bool FbTextPage::read(QIODevice *device)
  54. {
  55. FbReadThread::execute(this, 0, device);
  56. return true;
  57. }
  58. void FbTextPage::html(const QString &html, FbStore *store)
  59. {
  60. QWebSettings::clearMemoryCaches();
  61. QUrl url = FbTextPage::createUrl();
  62. manager()->setStore(url, store);
  63. mainFrame()->setHtml(html, url);
  64. }
  65. bool FbTextPage::acceptNavigationRequest(QWebFrame *frame, const QNetworkRequest &request, NavigationType type)
  66. {
  67. Q_UNUSED(frame);
  68. if (type == NavigationTypeLinkClicked) {
  69. qCritical() << request.url().fragment();
  70. return false;
  71. }
  72. return QWebPage::acceptNavigationRequest(frame, request, type);
  73. }
  74. QUrl FbTextPage::createUrl()
  75. {
  76. static int number = 0;
  77. return QString("fb2:/%1/").arg(number++);
  78. }
  79. QString FbTextPage::block(const QString &name)
  80. {
  81. return block(name, p());
  82. }
  83. QString FbTextPage::block(const QString &name, const QString &text)
  84. {
  85. return QString("<fb:%1>%2</fb:%1>").arg(name).arg(text);
  86. }
  87. QString FbTextPage::p(const QString &text)
  88. {
  89. return QString("<p>%1</p>").arg(text);
  90. }
  91. FbTextElement FbTextPage::body()
  92. {
  93. return doc().findFirst("body");
  94. }
  95. FbTextElement FbTextPage::doc()
  96. {
  97. return mainFrame()->documentElement();
  98. }
  99. void FbTextPage::push(QUndoCommand * command, const QString &text)
  100. {
  101. undoStack()->beginMacro(text);
  102. undoStack()->push(command);
  103. undoStack()->endMacro();
  104. }
  105. void FbTextPage::update()
  106. {
  107. emit contentsChanged();
  108. emit selectionChanged();
  109. }
  110. FbTextElement FbTextPage::appendSection(const FbTextElement &parent)
  111. {
  112. QString html = block("section", block("title", p()) + p());
  113. FbTextElement element = parent;
  114. element.appendInside(html);
  115. element = parent.lastChild();
  116. QUndoCommand * command = new FbInsertCmd(element);
  117. push(command, tr("Append section"));
  118. return element;
  119. }
  120. FbTextElement FbTextPage::appendTitle(const FbTextElement &parent)
  121. {
  122. QString html = block("title", p());
  123. FbTextElement element = parent;
  124. element.prependInside(html);
  125. element = parent.firstChild();
  126. QUndoCommand * command = new FbInsertCmd(element);
  127. push(command, tr("Append section"));
  128. return element;
  129. }
  130. FbTextElement FbTextPage::appendText(const FbTextElement &parent)
  131. {
  132. FbTextElement element = parent;
  133. element.appendInside(p());
  134. return element.lastChild();
  135. }
  136. void FbTextPage::insertBody()
  137. {
  138. QString html = block("body", block("title", p()) + block("section", block("title", p()) + p()));
  139. FbTextElement element = body();
  140. element.appendInside(html);
  141. element = element.lastChild();
  142. QUndoCommand * command = new FbInsertCmd(element);
  143. push(command, tr("Append body"));
  144. }
  145. void FbTextPage::insertSection()
  146. {
  147. FbTextElement element = current();
  148. while (!element.isNull()) {
  149. if (element.isSection() || element.isBody()) {
  150. appendSection(element);
  151. break;
  152. }
  153. element = element.parent();
  154. }
  155. }
  156. void FbTextPage::insertTitle()
  157. {
  158. FbTextElement element = current();
  159. while (!element.isNull()) {
  160. FbTextElement parent = element.parent();
  161. if ((parent.isSection() || parent.isBody()) && !parent.hasTitle()) {
  162. QString html = block("title", p());
  163. parent.prependInside(html);
  164. element = parent.firstChild();
  165. QUndoCommand * command = new FbInsertCmd(element);
  166. push(command, tr("Insert title"));
  167. break;
  168. }
  169. element = parent;
  170. }
  171. }
  172. void FbTextPage::insertSubtitle()
  173. {
  174. FbTextElement element = current();
  175. while (!element.isNull()) {
  176. FbTextElement parent = element.parent();
  177. if (parent.isSection()) {
  178. QString html = block("subtitle", p());
  179. if (element.isTitle()) {
  180. element.appendOutside(html);
  181. element = element.nextSibling();
  182. } else {
  183. element.prependOutside(html);
  184. element = element.previousSibling();
  185. }
  186. QUndoCommand * command = new FbInsertCmd(element);
  187. push(command, tr("Insert subtitle"));
  188. break;
  189. }
  190. element = parent;
  191. }
  192. }
  193. void FbTextPage::insertPoem()
  194. {
  195. FbTextElement element = current();
  196. while (!element.isNull()) {
  197. FbTextElement parent = element.parent();
  198. if (parent.isSection()) {
  199. QString html = block("poem", block("stanza", p()));
  200. if (element.isTitle()) {
  201. element.appendOutside(html);
  202. element = element.nextSibling();
  203. } else {
  204. element.prependOutside(html);
  205. element = element.previousSibling();
  206. }
  207. QUndoCommand * command = new FbInsertCmd(element);
  208. push(command, tr("Insert poem"));
  209. break;
  210. }
  211. element = parent;
  212. }
  213. }
  214. void FbTextPage::insertStanza()
  215. {
  216. FbTextElement element = current();
  217. while (!element.isNull()) {
  218. if (element.isStanza()) {
  219. QString html = block("stanza", p());
  220. element.appendOutside(html);
  221. element = element.nextSibling();
  222. QUndoCommand * command = new FbInsertCmd(element);
  223. push(command, tr("Append stanza"));
  224. break;
  225. }
  226. element = element.parent();
  227. }
  228. }
  229. void FbTextPage::insertAnnot()
  230. {
  231. }
  232. void FbTextPage::insertAuthor()
  233. {
  234. }
  235. void FbTextPage::insertEpigraph()
  236. {
  237. const QString type = "epigraph";
  238. FbTextElement element = current();
  239. while (!element.isNull()) {
  240. if (element.hasSubtype(type)) {
  241. QString html = block("epigraph", p());
  242. element = element.insertInside(type, html);
  243. QUndoCommand * command = new FbInsertCmd(element);
  244. push(command, tr("Insert epigraph"));
  245. break;
  246. }
  247. element = element.parent();
  248. }
  249. }
  250. void FbTextPage::insertDate()
  251. {
  252. }
  253. void FbTextPage::insertText()
  254. {
  255. }
  256. void FbTextPage::createBlock(const QString &name)
  257. {
  258. QString style = name;
  259. QString js1 = jScript("section_get.js");
  260. QString result = mainFrame()->evaluateJavaScript(js1).toString();
  261. QStringList list = result.split("|");
  262. if (list.count() < 2) return;
  263. const QString location = list[0];
  264. const QString position = list[1];
  265. if (style == "title" && position.left(2) != "0,") style.prepend("sub");
  266. FbTextElement original = element(location);
  267. FbTextElement duplicate = original.clone();
  268. original.appendOutside(duplicate);
  269. original.takeFromDocument();
  270. QString js2 = jScript("section_new.js") + ";f(this,'fb:%1',%2)";
  271. duplicate.evaluateJavaScript(js2.arg(style).arg(position));
  272. QUndoCommand * command = new FbReplaceCmd(original, duplicate);
  273. push(command, tr("Create <%1>").arg(style));
  274. }
  275. void FbTextPage::createSection()
  276. {
  277. createBlock("section");
  278. }
  279. void FbTextPage::deleteSection()
  280. {
  281. FbTextElement element = current();
  282. while (!element.isNull()) {
  283. if (element.isSection()) {
  284. if (element.parent().isBody()) return;
  285. FbTextElement original = element.parent();
  286. FbTextElement duplicate = original.clone();
  287. int index = element.index();
  288. original.appendOutside(duplicate);
  289. original.takeFromDocument();
  290. element = duplicate.child(index);
  291. if (index) {
  292. FbTextElement title = element.firstChild();
  293. if (title.isTitle()) {
  294. title.removeClass("title");
  295. title.addClass("subtitle");
  296. }
  297. }
  298. QString xml = element.toInnerXml();
  299. element.setOuterXml(xml);
  300. QUndoCommand * command = new FbReplaceCmd(original, duplicate);
  301. push(command, tr("Remove section"));
  302. element.select();
  303. break;
  304. }
  305. element = element.parent();
  306. }
  307. }
  308. void FbTextPage::createTitle()
  309. {
  310. createBlock("title");
  311. }
  312. FbTextElement FbTextPage::current()
  313. {
  314. return element(location());
  315. }
  316. FbTextElement FbTextPage::element(const QString &location)
  317. {
  318. if (location.isEmpty()) return FbTextElement();
  319. QStringList list = location.split(",", QString::SkipEmptyParts);
  320. QStringListIterator iterator(list);
  321. QWebElement result = doc();
  322. while (iterator.hasNext()) {
  323. QString str = iterator.next();
  324. int pos = str.indexOf("=");
  325. QString tag = str.left(pos);
  326. int key = str.mid(pos + 1).toInt();
  327. if (key < 0) break;
  328. result = result.firstChild();
  329. while (0 < key--) result = result.nextSibling();
  330. }
  331. return result;
  332. }
  333. QString FbTextPage::location()
  334. {
  335. QString javascript = "location(document.getSelection().anchorNode)";
  336. return mainFrame()->evaluateJavaScript(javascript).toString();
  337. }
  338. void FbTextPage::showStatus()
  339. {
  340. QString javascript = jScript("get_status.js");
  341. QString text = mainFrame()->evaluateJavaScript(javascript).toString();
  342. text.replace("FB:", "");
  343. emit status(text);
  344. }
  345. void FbTextPage::loadFinished()
  346. {
  347. mainFrame()->addToJavaScriptWindowObject("logger", &m_logger);
  348. QString style = "p:after{display:inline;content:'\\A0\\B6';color:gray;}";
  349. mainFrame()->findFirstElement("html>head>style#inline").setInnerXml(style);
  350. body().select();
  351. }
  352. void FbTextPage::fixContents()
  353. {
  354. foreach (QWebElement span, doc().findAll("span.apple-style-span[style]")) {
  355. span.removeAttribute("style");
  356. }
  357. foreach (QWebElement span, doc().findAll("[style]")) {
  358. span.removeAttribute("style");
  359. }
  360. }