1
0

fb2page.cpp 11 KB

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