fb2page.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. #include "fb2page.hpp"
  2. #include <QTimer>
  3. #include <QWebFrame>
  4. #include <QtDebug>
  5. #include "fb2read.hpp"
  6. #include "fb2save.hpp"
  7. #include "fb2temp.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::fromLocalFile(":style.css"));
  34. setContentEditable(true);
  35. setNetworkAccessManager(new FbNetworkAccessManager(this));
  36. connect(this, SIGNAL(loadFinished(bool)), SLOT(loadFinished()));
  37. connect(this, SIGNAL(contentsChanged()), SLOT(fixContents()));
  38. }
  39. FbNetworkAccessManager *FbTextPage::temp()
  40. {
  41. return qobject_cast<FbNetworkAccessManager*>(networkAccessManager());
  42. }
  43. void FbTextPage::binary(const QString &name, const QByteArray &data)
  44. {
  45. if (FbNetworkAccessManager *t = temp()) t->data(name, data);
  46. }
  47. bool FbTextPage::load(const QString &filename, const QString &xml)
  48. {
  49. QXmlInputSource source;
  50. if (xml.isEmpty()) {
  51. QFile file(filename);
  52. if (!file.open(QFile::ReadOnly | QFile::Text)) {
  53. qCritical() << QObject::tr("Cannot read file %1: %2.").arg(filename).arg(file.errorString());
  54. return false;
  55. }
  56. source.setData(file.readAll());
  57. } else {
  58. source.setData(xml);
  59. }
  60. bool ok = FbReadHandler::load(this, source, m_html);
  61. if (ok) QTimer::singleShot(1000, this, SLOT(onTimer()));
  62. return ok;
  63. }
  64. void FbTextPage::onTimer()
  65. {
  66. static int number = 0;
  67. QUrl url(QString("fb2:/%1/").arg(number++));
  68. temp()->setPath(url.path());
  69. mainFrame()->setHtml(m_html, url);
  70. }
  71. void FbTextPage::html(const QString &html, const QUrl &url)
  72. {
  73. mainFrame()->setHtml(html, url);
  74. temp()->setPath(url.path());
  75. }
  76. bool FbTextPage::acceptNavigationRequest(QWebFrame *frame, const QNetworkRequest &request, NavigationType type)
  77. {
  78. Q_UNUSED(frame);
  79. if (type == NavigationTypeLinkClicked) {
  80. qCritical() << request.url().fragment();
  81. return false;
  82. }
  83. return QWebPage::acceptNavigationRequest(frame, request, type);
  84. }
  85. QString FbTextPage::block(const QString &name)
  86. {
  87. return block(name, p());
  88. }
  89. QString FbTextPage::block(const QString &name, const QString &text)
  90. {
  91. return QString("<fb:%1>%2</fb:%1>").arg(name).arg(text);
  92. }
  93. QString FbTextPage::p(const QString &text)
  94. {
  95. return QString("<p>%1</p>").arg(text);
  96. }
  97. FbTextElement FbTextPage::body()
  98. {
  99. return doc().findFirst("body");
  100. }
  101. FbTextElement FbTextPage::doc()
  102. {
  103. return mainFrame()->documentElement();
  104. }
  105. void FbTextPage::push(QUndoCommand * command, const QString &text)
  106. {
  107. undoStack()->beginMacro(text);
  108. undoStack()->push(command);
  109. undoStack()->endMacro();
  110. }
  111. void FbTextPage::update()
  112. {
  113. emit contentsChanged();
  114. emit selectionChanged();
  115. }
  116. FbTextElement FbTextPage::appendSection(const FbTextElement &parent)
  117. {
  118. QString html = block("section", block("title", p()) + p());
  119. FbTextElement element = parent;
  120. element.appendInside(html);
  121. element = parent.lastChild();
  122. QUndoCommand * command = new FbInsertCmd(element);
  123. push(command, tr("Append section"));
  124. return element;
  125. }
  126. FbTextElement FbTextPage::appendTitle(const FbTextElement &parent)
  127. {
  128. QString html = block("title", p());
  129. FbTextElement element = parent;
  130. element.prependInside(html);
  131. element = parent.firstChild();
  132. QUndoCommand * command = new FbInsertCmd(element);
  133. push(command, tr("Append section"));
  134. return element;
  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. QString FbTextPage::status()
  339. {
  340. QString javascript = jScript("get_status.js");
  341. QString status = mainFrame()->evaluateJavaScript(javascript).toString();
  342. return status.replace("FB:", "");
  343. }
  344. void FbTextPage::loadFinished()
  345. {
  346. mainFrame()->addToJavaScriptWindowObject("logger", &m_logger);
  347. FbTextElement element = body().findFirst("fb\\:section");
  348. if (element.isNull()) element = body().findFirst("fb\\:body");
  349. if (element.isNull()) element = body();
  350. FbTextElement child = element.firstChild();
  351. if (child.isTitle()) child = child.nextSibling();
  352. if (!child.isNull()) element = child;
  353. element.select();
  354. }
  355. void FbTextPage::fixContents()
  356. {
  357. foreach (QWebElement span, doc().findAll("span.apple-style-span[style]")) {
  358. span.removeAttribute("style");
  359. }
  360. foreach (QWebElement span, doc().findAll("[style]")) {
  361. span.removeAttribute("style");
  362. }
  363. }