fb2temp.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. #include "fb2temp.hpp"
  2. #include <QAbstractListModel>
  3. #include <QBuffer>
  4. #include <QCryptographicHash>
  5. #include <QFileInfo>
  6. #include <QImageReader>
  7. #include <QUrl>
  8. #include <QWebFrame>
  9. #include <QVBoxLayout>
  10. #include <QtDebug>
  11. #include "fb2page.hpp"
  12. #include "fb2text.hpp"
  13. //---------------------------------------------------------------------------
  14. // FbBinary
  15. //---------------------------------------------------------------------------
  16. FbBinary::FbBinary(const QString &name)
  17. : QTemporaryFile()
  18. , m_name(name)
  19. , m_size(0)
  20. {
  21. }
  22. qint64 FbBinary::write(QByteArray &data)
  23. {
  24. open();
  25. if (m_hash.isEmpty()) m_hash = md5(data);
  26. m_size = QTemporaryFile::write(data);
  27. QBuffer buffer(&data);
  28. buffer.open(QIODevice::ReadOnly);
  29. m_type = QImageReader::imageFormat(&buffer);
  30. close();
  31. return m_size;
  32. }
  33. QString FbBinary::md5(const QByteArray &data)
  34. {
  35. return QCryptographicHash::hash(data, QCryptographicHash::Md5).toBase64();
  36. }
  37. QByteArray FbBinary::data()
  38. {
  39. open();
  40. QByteArray data = readAll();
  41. close();
  42. return data;
  43. }
  44. //---------------------------------------------------------------------------
  45. // FbStore
  46. //---------------------------------------------------------------------------
  47. FbStore::FbStore(QObject *parent)
  48. : QObject(parent)
  49. {
  50. }
  51. FbStore::~FbStore()
  52. {
  53. FbTemporaryIterator it(*this);
  54. while (it.hasNext()) delete it.next();
  55. }
  56. void FbStore::binary(const QString &name, const QByteArray &data)
  57. {
  58. set(name, data);
  59. }
  60. QString FbStore::add(const QString &path, QByteArray &data)
  61. {
  62. QString hash = FbBinary::md5(data);
  63. QString name = this->name(hash);
  64. if (name.isEmpty()) {
  65. name = newName(path);
  66. FbBinary * temp = new FbBinary(name);
  67. temp->setHash(hash);
  68. temp->write(data);
  69. append(temp);
  70. }
  71. return name;
  72. }
  73. QString FbStore::newName(const QString &path)
  74. {
  75. QFileInfo info(path);
  76. QString name = info.fileName();
  77. if (!exists(name)) return name;
  78. QString base = info.baseName();
  79. QString suff = info.suffix();
  80. for (int i = 1; ; i++) {
  81. QString name = QString("%1(%2).%3").arg(base).arg(i).arg(suff);
  82. if (exists(name)) continue;
  83. return name;
  84. }
  85. }
  86. FbBinary * FbStore::get(const QString &name) const
  87. {
  88. FbTemporaryIterator it(*this);
  89. while (it.hasNext()) {
  90. FbBinary * file = it.next();
  91. if (file->name() == name) return file;
  92. }
  93. return NULL;
  94. }
  95. QByteArray FbStore::data(const QString &name) const
  96. {
  97. FbTemporaryIterator it(*this);
  98. while (it.hasNext()) {
  99. FbBinary *file = it.next();
  100. if (file->name() == name) return file->data();
  101. }
  102. return QByteArray();
  103. }
  104. const QString & FbStore::set(const QString &name, QByteArray data, const QString &hash)
  105. {
  106. FbBinary * file = get(name);
  107. if (!file) append(file = new FbBinary(name));
  108. file->setHash(hash);
  109. file->write(data);
  110. return file->hash();
  111. }
  112. QString FbStore::name(const QString &hash) const
  113. {
  114. FbTemporaryIterator it(*this);
  115. while (it.hasNext()) {
  116. FbBinary *file = it.next();
  117. if (file->hash() == hash) return file->name();
  118. }
  119. return QString();
  120. }
  121. bool FbStore::exists(const QString &name) const
  122. {
  123. FbTemporaryIterator it(*this);
  124. while (it.hasNext()) {
  125. FbBinary *file = it.next();
  126. if (file->name() == name) return true;
  127. }
  128. return false;
  129. }
  130. #if 0
  131. //---------------------------------------------------------------------------
  132. // FbNetworkDiskCache
  133. //---------------------------------------------------------------------------
  134. QNetworkCacheMetaData FbNetworkDiskCache::metaData(const QUrl &url)
  135. {
  136. qCritical() << url.toString();
  137. return QNetworkDiskCache::metaData(url);
  138. }
  139. QIODevice * FbNetworkDiskCache::data(const QUrl &url)
  140. {
  141. qCritical() << url.toString();
  142. return QNetworkDiskCache::data(url);
  143. }
  144. #endif
  145. //---------------------------------------------------------------------------
  146. // FbImageReply
  147. //---------------------------------------------------------------------------
  148. FbImageReply::FbImageReply(QNetworkAccessManager::Operation op, const QNetworkRequest &request, const QByteArray &data)
  149. : QNetworkReply()
  150. , content(data)
  151. , offset(0)
  152. {
  153. setOperation(op);
  154. setRequest(request);
  155. setUrl(request.url());
  156. open(ReadOnly | Unbuffered);
  157. setHeader(QNetworkRequest::ContentLengthHeader, QVariant(content.size()));
  158. setAttribute(QNetworkRequest::CacheSaveControlAttribute, QVariant(false));
  159. QMetaObject::invokeMethod(this, "readyRead", Qt::QueuedConnection);
  160. QMetaObject::invokeMethod(this, "finished", Qt::QueuedConnection);
  161. }
  162. qint64 FbImageReply::readData(char *data, qint64 maxSize)
  163. {
  164. if (offset >= content.size()) return -1;
  165. QMetaObject::invokeMethod(this, "readyRead", Qt::QueuedConnection);
  166. qint64 number = qMin(maxSize, content.size() - offset);
  167. memcpy(data, content.constData() + offset, number);
  168. offset += number;
  169. return number;
  170. }
  171. //---------------------------------------------------------------------------
  172. // FbNetworkAccessManager
  173. //
  174. // http://doc.trolltech.com/qq/32/qq32-webkit-protocols.html
  175. //---------------------------------------------------------------------------
  176. FbNetworkAccessManager::FbNetworkAccessManager(QObject *parent)
  177. : QNetworkAccessManager(parent)
  178. , m_store(new FbStore(this))
  179. {
  180. }
  181. void FbNetworkAccessManager::setStore(const QUrl url, FbStore *store)
  182. {
  183. m_path = url.path();
  184. if (m_store) delete m_store;
  185. if (!store) store = new FbStore(this);
  186. store->setParent(this);
  187. m_store = store;
  188. }
  189. QNetworkReply * FbNetworkAccessManager::createRequest(Operation op, const QNetworkRequest &request, QIODevice *outgoingData)
  190. {
  191. if (m_store) {
  192. const QUrl &url = request.url();
  193. const QString path = url.path();
  194. if (url.scheme() == "fb2" && path == m_path) {
  195. QString name = request.url().fragment();
  196. QByteArray data = m_store->data(name);
  197. return new FbImageReply(op, request, data);
  198. }
  199. }
  200. return QNetworkAccessManager::createRequest(op, request, outgoingData);
  201. }
  202. QVariant FbNetworkAccessManager::info(int row, int col) const
  203. {
  204. if (!m_store) return QVariant();
  205. if (0 <= row && row < count()) {
  206. FbBinary *file = m_store->at(row);
  207. switch (col) {
  208. case 2: return file->type();
  209. case 3: return file->size();
  210. }
  211. return m_store->at(row)->name();
  212. }
  213. return QVariant();
  214. }
  215. QByteArray FbNetworkAccessManager::data(int index) const
  216. {
  217. if (!m_store) return QByteArray();
  218. if (0 <= index && index < count()) {
  219. return m_store->at(index)->data();
  220. }
  221. return QByteArray();
  222. }
  223. //---------------------------------------------------------------------------
  224. // FbListModel
  225. //---------------------------------------------------------------------------
  226. FbListModel::FbListModel(FbTextEdit *text, QObject *parent)
  227. : QAbstractListModel(parent)
  228. , m_text(text)
  229. {
  230. }
  231. int FbListModel::columnCount(const QModelIndex &parent) const
  232. {
  233. Q_UNUSED(parent);
  234. return 4;
  235. }
  236. int FbListModel::rowCount(const QModelIndex &parent) const
  237. {
  238. Q_UNUSED(parent);
  239. FbNetworkAccessManager * f = files();
  240. return f ? f->count() : 0;
  241. }
  242. QVariant FbListModel::headerData(int section, Qt::Orientation orientation, int role) const
  243. {
  244. if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
  245. switch (section) {
  246. case 1: return tr("File name");
  247. case 2: return tr("Type");
  248. case 3: return tr("Size");
  249. }
  250. }
  251. return QVariant();
  252. }
  253. FbNetworkAccessManager * FbListModel::files() const
  254. {
  255. if (FbTextPage *page = qobject_cast<FbTextPage*>(m_text->page())) {
  256. return qobject_cast<FbNetworkAccessManager*>(page->networkAccessManager());
  257. } else {
  258. return 0;
  259. }
  260. }
  261. QVariant FbListModel::data(const QModelIndex &index, int role) const
  262. {
  263. if (index.isValid()) {
  264. switch (role) {
  265. case Qt::DisplayRole: {
  266. FbNetworkAccessManager * f = files();
  267. return f ? f->info(index.row(), index.column()) : QVariant();
  268. } break;
  269. case Qt::TextAlignmentRole: {
  270. switch (index.column()) {
  271. case 3: return Qt::AlignRight;
  272. default: return Qt::AlignLeft;
  273. }
  274. }
  275. }
  276. }
  277. return QVariant();
  278. }
  279. //---------------------------------------------------------------------------
  280. // FbListView
  281. //---------------------------------------------------------------------------
  282. #include <QSplitter>
  283. #include <QScrollArea>
  284. FbListView::FbListView(QWidget *parent)
  285. : QTreeView(parent)
  286. {
  287. setAllColumnsShowFocus(true);
  288. }
  289. void FbListView::currentChanged(const QModelIndex &current, const QModelIndex &previous)
  290. {
  291. QTreeView::currentChanged(current, previous);
  292. QModelIndex index = model()->index(current.row(), 0);
  293. emit showImage(model()->data(index).toString());
  294. }
  295. FbListModel * FbListView::model() const
  296. {
  297. return qobject_cast<FbListModel*>(QTreeView::model());
  298. }
  299. //---------------------------------------------------------------------------
  300. // FbListWidget
  301. //---------------------------------------------------------------------------
  302. FbListWidget::FbListWidget(FbTextEdit *text, QWidget* parent)
  303. : QWidget(parent)
  304. , m_text(text)
  305. {
  306. QVBoxLayout *layout = new QVBoxLayout(this);
  307. layout->setSpacing(0);
  308. layout->setContentsMargins(0, 0, 0, 0);
  309. QSplitter *splitter = new QSplitter(Qt::Vertical, this);
  310. m_list = new FbListView(splitter);
  311. splitter->addWidget(m_list);
  312. FbTextFrame *frame = new FbTextFrame(splitter);
  313. splitter->addWidget(frame);
  314. m_view = new FbTextBase(frame);
  315. m_view->page()->setNetworkAccessManager(text->page()->networkAccessManager());
  316. frame->layout()->addWidget(m_view);
  317. splitter->setSizes(QList<int>() << 100 << 100);
  318. layout->addWidget(splitter);
  319. connect(m_text, SIGNAL(loadFinished(bool)), SLOT(loadFinished()));
  320. connect(m_list, SIGNAL(showImage(QString)), SLOT(showImage(QString)));
  321. loadFinished();
  322. }
  323. void FbListWidget::loadFinished()
  324. {
  325. m_view->load(QUrl());
  326. m_list->setModel(new FbListModel(m_text, this));
  327. m_list->reset();
  328. m_list->resizeColumnToContents(1);
  329. m_list->resizeColumnToContents(2);
  330. m_list->resizeColumnToContents(3);
  331. m_list->setColumnHidden(0, true);
  332. }
  333. void FbListWidget::showImage(const QString &name)
  334. {
  335. QUrl url = m_text->url();
  336. url.setFragment(name);
  337. QString html = QString("<img src=%1 valign=center align=center width=100%>").arg(url.toString());
  338. m_view->setHtml(html);
  339. }