fb2page.cpp 12 KB

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